18
18
*/
19
19
20
20
/*
21
- * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
21
+ * Copyright (c) 2021, 2023, Oracle and/or its affiliates. All rights reserved.
22
22
*/
23
23
package org .opengrok .indexer .util ;
24
24
25
+ import jakarta .ws .rs .ProcessingException ;
26
+ import jakarta .ws .rs .client .Client ;
27
+ import jakarta .ws .rs .client .ClientBuilder ;
28
+ import jakarta .ws .rs .core .HttpHeaders ;
29
+ import jakarta .ws .rs .core .MultivaluedHashMap ;
30
+ import jakarta .ws .rs .core .MultivaluedMap ;
31
+ import jakarta .ws .rs .core .Response ;
32
+ import org .jetbrains .annotations .Nullable ;
25
33
import org .opengrok .indexer .logger .LoggerFactory ;
26
34
27
- import java .io .IOException ;
28
- import java .net .InetAddress ;
29
- import java .net .InetSocketAddress ;
30
- import java .net .Socket ;
31
35
import java .net .URI ;
32
36
import java .net .URISyntaxException ;
33
- import java .net . UnknownHostException ;
37
+ import java .util . concurrent . TimeUnit ;
34
38
import java .util .logging .Level ;
35
39
import java .util .logging .Logger ;
36
40
@@ -55,33 +59,49 @@ public static int urlToPort(String urlStr) throws URISyntaxException {
55
59
return uri .getPort ();
56
60
}
57
61
58
- /**
59
- * @param urlStr URI
60
- * @return hostname
61
- * @throws URISyntaxException on error
62
- */
63
- public static String urlToHostname (String urlStr ) throws URISyntaxException {
64
- URI uri = new URI (urlStr );
65
- return uri .getHost ();
66
- }
62
+ private static boolean isWebAppReachable (String webappURI , int timeOutMillis , @ Nullable String token ) {
63
+ ClientBuilder clientBuilder = ClientBuilder .newBuilder ();
64
+ clientBuilder .connectTimeout (timeOutMillis , TimeUnit .MILLISECONDS );
65
+ clientBuilder .readTimeout (timeOutMillis , TimeUnit .MILLISECONDS );
67
66
68
- /**
69
- * @param addr IP address
70
- * @param port port number
71
- * @param timeOutMillis timeout in milliseconds
72
- * @return true if TCP connect works, false otherwise
73
- */
74
- public static boolean isReachable (InetAddress addr , int port , int timeOutMillis ) {
75
- try (Socket soc = new Socket ()) {
76
- soc .connect (new InetSocketAddress (addr , port ), timeOutMillis );
77
- } catch (IOException e ) {
67
+ // Here, IndexerUtil#getWebAppHeaders() is not used because at the point this method is called,
68
+ // the RuntimeEnvironment configuration used by getWebAppHeaders() is not set yet.
69
+ MultivaluedMap <String , Object > headers = new MultivaluedHashMap <>();
70
+ if (token != null ) {
71
+ headers .add (HttpHeaders .AUTHORIZATION , "Bearer " + token );
72
+ }
73
+
74
+ try (Client client = clientBuilder .build ()) {
75
+ Response response = client
76
+ .target (webappURI )
77
+ .path ("api" )
78
+ .path ("v1" )
79
+ .path ("configuration" )
80
+ .request ()
81
+ .headers (headers )
82
+ .get ();
83
+
84
+ if (response .getStatusInfo ().getFamily () != Response .Status .Family .SUCCESSFUL ) {
85
+ LOGGER .log (Level .SEVERE , "cannot reach OpenGrok web application on {0}: {1}" ,
86
+ new Object []{webappURI , response .getStatusInfo ()});
87
+ return false ;
88
+ }
89
+ } catch (ProcessingException e ) {
90
+ LOGGER .log (Level .SEVERE , String .format ("could not connect to %s" , webappURI ), e );
78
91
return false ;
79
92
}
80
93
81
94
return true ;
82
95
}
83
96
84
- public static boolean isReachable (String webappURI , int timeOutMillis ) {
97
+ /**
98
+ *
99
+ * @param webappURI URI of the web app
100
+ * @param timeOutMillis connect/read timeout in milliseconds
101
+ * @param token authentication token, can be {@code null}
102
+ * @return whether web app is reachable within given timeout on given URI
103
+ */
104
+ public static boolean isReachable (String webappURI , int timeOutMillis , @ Nullable String token ) {
85
105
boolean connectWorks = false ;
86
106
87
107
try {
@@ -91,15 +111,9 @@ public static boolean isReachable(String webappURI, int timeOutMillis) {
91
111
return false ;
92
112
}
93
113
94
- for (InetAddress addr : InetAddress .getAllByName (HostUtil .urlToHostname (webappURI ))) {
95
- if (HostUtil .isReachable (addr , port , timeOutMillis )) {
96
- LOGGER .log (Level .FINE , "URI " + webappURI + " is reachable via " + addr .toString ());
97
- connectWorks = true ;
98
- break ;
99
- }
100
- }
101
- } catch (URISyntaxException | UnknownHostException e ) {
102
- LOGGER .log (Level .WARNING , String .format ("URI not valid: %s" , webappURI ), e );
114
+ return isWebAppReachable (webappURI , timeOutMillis , token );
115
+ } catch (URISyntaxException e ) {
116
+ LOGGER .log (Level .SEVERE , String .format ("URI not valid: %s" , webappURI ), e );
103
117
}
104
118
105
119
return connectWorks ;
0 commit comments