Skip to content

Commit 297dc9b

Browse files
committed
OPENNLP-1697 - GH action fail because of 403 returned by sourceforge
1 parent 2690b88 commit 297dc9b

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

opennlp-tools/src/test/java/opennlp/tools/EnabledWhenCDNAvailable.java

+25-5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import java.lang.annotation.RetentionPolicy;
2323
import java.net.InetSocketAddress;
2424
import java.net.Socket;
25+
import java.net.URL;
26+
import javax.net.ssl.HttpsURLConnection;
2527

2628
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
2729
import org.junit.jupiter.api.extension.ExecutionCondition;
@@ -51,14 +53,32 @@ public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext con
5153
final EnabledWhenCDNAvailable annotation = optional.get();
5254
final String host = annotation.hostname();
5355
try (Socket socket = new Socket()) {
54-
socket.connect(new InetSocketAddress(host, 80), TIMEOUT_MS);
55-
return ConditionEvaluationResult.enabled("Resource (CDN) reachable.");
56+
// First, try to establish a socket connection to ensure the host is reachable
57+
socket.connect(new InetSocketAddress(host, 443), TIMEOUT_MS);
58+
59+
// Then, try to check the HTTP status by making an HTTPS request
60+
final URL url = new URL("https://" + host);
61+
final HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
62+
connection.setConnectTimeout(TIMEOUT_MS);
63+
connection.setReadTimeout(TIMEOUT_MS);
64+
int statusCode = connection.getResponseCode();
65+
66+
// If the HTTP status code indicates success (2xx range), return enabled
67+
if (statusCode >= 200 && statusCode < 300) {
68+
return ConditionEvaluationResult.enabled(
69+
"Resource (CDN) reachable with status code: " + statusCode);
70+
} else {
71+
return ConditionEvaluationResult.disabled(
72+
"Resource (CDN) reachable, but HTTP status code: " + statusCode);
73+
74+
}
5675
} catch (IOException e) {
57-
// Unreachable, unresolvable or timeout
58-
return ConditionEvaluationResult.disabled("Resource (CDN) unreachable.");
76+
return ConditionEvaluationResult.disabled(
77+
"Resource (CDN) unreachable.");
5978
}
6079
}
61-
return ConditionEvaluationResult.enabled("Nothing annotated with DisabledWhenOffline.");
80+
return ConditionEvaluationResult.enabled(
81+
"Nothing annotated with EnabledWhenCDNAvailable.");
6282
}
6383
}
6484

0 commit comments

Comments
 (0)