|
15 | 15 | import fr.sandro642.github.spring.Application; |
16 | 16 | import fr.sandro642.github.update.RetrieveLastVersion; |
17 | 17 |
|
| 18 | +import java.io.IOException; |
18 | 19 | import java.net.URI; |
19 | 20 | import java.net.http.HttpClient; |
20 | 21 | import java.net.http.HttpRequest; |
@@ -156,47 +157,106 @@ public void webServices(int port, String nameDashboard) { |
156 | 157 | } |
157 | 158 |
|
158 | 159 | /** |
159 | | - * Implement WAN connection to retrieve the port from the server. |
160 | | - * @param urlServ the server URL |
| 160 | + * Implement WAN connectivity using the provided server URL and component name. |
| 161 | + * This method performs HTTP requests to establish a connection and retrieve necessary information. |
| 162 | + * @param urlServ the URL of the server to connect to |
| 163 | + * @param nameComponent the name of the component to be used in the connection |
161 | 164 | */ |
162 | | - public void wanImplement(String urlServ) { |
| 165 | + public void wanImplement(String urlServ, String nameComponent) { |
| 166 | + if (urlServ == null || urlServ.isBlank()) { |
| 167 | + return; |
| 168 | + } |
163 | 169 | try { |
164 | | - String codeWan = ""; |
165 | | - |
166 | | - HttpClient client1 = HttpClient.newHttpClient(); |
167 | | - HttpRequest request1 = HttpRequest.newBuilder() |
168 | | - .uri(URI.create(urlServ)) // remplacer par l'URL qui |
169 | | - .build(); |
| 170 | + StoreAndRetrieve().put(StoreAndRetrieve().NAME_DASHBOARD, nameComponent); |
170 | 171 |
|
171 | | - HttpResponse<String> response1 = client1.send(request1, HttpResponse.BodyHandlers.ofString()); |
| 172 | + String trimmed = urlServ.trim(); |
| 173 | + if (!trimmed.matches("(?i)^https?://.*")) trimmed = "http://" + trimmed; |
| 174 | + if (trimmed.endsWith("/")) trimmed = trimmed.substring(0, trimmed.length() - 1); |
| 175 | + String urlServLambda = trimmed + "/api"; |
172 | 176 |
|
173 | | - Gson gson1 = new Gson(); |
174 | | - JsonObject root1 = gson1.fromJson(response1.body(), JsonObject.class); |
| 177 | + URI uri = URI.create(urlServLambda); |
175 | 178 |
|
176 | | - if (root1 != null) { |
177 | | - codeWan = root1.get("code").getAsString(); |
178 | | - } |
| 179 | + HttpClient client = HttpClient.newBuilder() |
| 180 | + .version(HttpClient.Version.HTTP_1_1) |
| 181 | + .connectTimeout(java.time.Duration.ofSeconds(5)) |
| 182 | + .followRedirects(HttpClient.Redirect.NORMAL) |
| 183 | + .build(); |
179 | 184 |
|
180 | | - HttpClient client = HttpClient.newHttpClient(); |
181 | | - HttpRequest request = HttpRequest.newBuilder() |
182 | | - .uri(URI.create(urlServ + "/connect/" + codeWan)) // remplacer par l'URL qui renvoie le JSON |
| 185 | + HttpRequest req1 = HttpRequest.newBuilder() |
| 186 | + .uri(uri) |
| 187 | + .GET() |
| 188 | + .timeout(java.time.Duration.ofSeconds(5)) |
| 189 | + .header("Accept", "application/json") |
| 190 | + .header("User-Agent", "ConnectLib/1.0") |
183 | 191 | .build(); |
184 | 192 |
|
185 | | - HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); |
| 193 | + try { |
| 194 | + HttpResponse<String> resp1 = client.send(req1, HttpResponse.BodyHandlers.ofString()); |
| 195 | + |
| 196 | + if (resp1.statusCode() / 100 != 2 || resp1.body() == null || resp1.body().isBlank()) { |
| 197 | + return; |
| 198 | + } |
| 199 | + |
| 200 | + JsonObject root1 = new Gson().fromJson(resp1.body(), JsonObject.class); |
| 201 | + if (root1 == null || !root1.has("code") || root1.get("code").isJsonNull()) { |
| 202 | + return; |
| 203 | + } |
| 204 | + String codeWan = root1.get("code").getAsString(); |
| 205 | + |
| 206 | + String connectUrl = trimmed + "/api/connect/" + java.net.URLEncoder.encode(codeWan, java.nio.charset.StandardCharsets.UTF_8); |
| 207 | + HttpRequest req2 = HttpRequest.newBuilder() |
| 208 | + .uri(URI.create(connectUrl)) |
| 209 | + .GET() |
| 210 | + .timeout(java.time.Duration.ofSeconds(5)) |
| 211 | + .header("Accept", "application/json") |
| 212 | + .header("User-Agent", "ConnectLib/1.0") |
| 213 | + .build(); |
| 214 | + |
| 215 | + HttpResponse<String> resp2 = client.send(req2, HttpResponse.BodyHandlers.ofString()); |
| 216 | + |
| 217 | + if (resp2.statusCode() / 100 == 2 && resp2.body() != null && !resp2.body().isBlank()) { |
| 218 | + JsonObject root = new Gson().fromJson(resp2.body(), JsonObject.class); |
| 219 | + if (root != null && root.has("user")) { |
| 220 | + JsonObject user = root.getAsJsonObject("user"); |
| 221 | + if (user != null && user.has("port") && !user.get("port").isJsonNull()) { |
| 222 | + StoreAndRetrieve().put(StoreAndRetrieve().DYNAMIC_PORT, user.get("port").getAsString()); |
| 223 | + } |
| 224 | + } |
| 225 | + } |
186 | 226 |
|
187 | | - Gson gson = new Gson(); |
188 | | - JsonObject root = gson.fromJson(response.body(), JsonObject.class); |
| 227 | + SpringApp().startApplication().subscribe(); |
| 228 | + return; |
| 229 | + } catch (java.io.EOFException eof) { |
| 230 | + Logger().ERROR(langManager.getMessage(CategoriesType.CONNECTLIB_CLASS, "wanimplement.erroreefexception", Map.of("exception", eof.getMessage()))); |
| 231 | + } catch (IOException ioe) { |
| 232 | + Logger().ERROR(langManager.getMessage(CategoriesType.CONNECTLIB_CLASS, "wanimplement.errorioexception", Map.of("exception", ioe.getMessage()))); |
| 233 | + } catch (InterruptedException ie) { |
| 234 | + Thread.currentThread().interrupt(); |
| 235 | + return; |
| 236 | + } |
189 | 237 |
|
190 | | - if (root != null && root.has("user")) { |
191 | | - JsonObject user = root.getAsJsonObject("user"); |
192 | | - if (user != null && user.has("port") && !user.get("port").isJsonNull()) { |
193 | | - StoreAndRetrieve().put(StoreAndRetrieve().DYNAMIC_PORT, user.get("port").getAsString()); |
| 238 | + try { |
| 239 | + java.net.URL url = new java.net.URL(urlServLambda); |
| 240 | + java.net.HttpURLConnection conn = (java.net.HttpURLConnection) url.openConnection(); |
| 241 | + conn.setConnectTimeout(3000); |
| 242 | + conn.setReadTimeout(3000); |
| 243 | + conn.setRequestMethod("GET"); |
| 244 | + conn.setRequestProperty("Accept", "application/json"); |
| 245 | + conn.setRequestProperty("User-Agent", "ConnectLib/1.0"); |
| 246 | + conn.setRequestProperty("Connection", "close"); |
| 247 | + int code = conn.getResponseCode(); |
| 248 | + String body; |
| 249 | + try (java.io.InputStream is = code >= 400 ? conn.getErrorStream() : conn.getInputStream()) { |
| 250 | + body = is == null ? "<no-body>" : new String(is.readAllBytes(), java.nio.charset.StandardCharsets.UTF_8); |
194 | 251 | } |
| 252 | + } catch (Exception e) { |
| 253 | + Logger().ERROR(langManager.getMessage(CategoriesType.CONNECTLIB_CLASS, "wanimplement.errorfallback", Map.of("exception", e.getMessage()))); |
195 | 254 | } |
196 | 255 |
|
197 | | - SpringApp().startApplication().subscribe(); |
| 256 | + } catch (IllegalArgumentException iae) { |
| 257 | + Logger().ERROR(langManager.getMessage(CategoriesType.CONNECTLIB_CLASS, "wanimplement.illegalargument", Map.of("exception", iae.getMessage()))); |
198 | 258 | } catch (Exception e) { |
199 | | - e.printStackTrace(); |
| 259 | + Logger().ERROR(langManager.getMessage(CategoriesType.CONNECTLIB_CLASS, "wanimplement.error", Map.of("exception", e.getMessage()))); |
200 | 260 | } |
201 | 261 | } |
202 | 262 |
|
|
0 commit comments