Skip to content

Commit cfc67d6

Browse files
authored
Merge pull request #62 from Sandro642/feature/wan-impl
Feature/wan impl
2 parents f5b81ba + e68b88c commit cfc67d6

4 files changed

Lines changed: 94 additions & 29 deletions

File tree

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ plugins {
55
}
66

77
group = 'fr.sandro642.github'
8-
version = '0.4.6-STABLE'
8+
version = '0.4.7-STABLE'
99

1010
// Générer une classe de version automatiquement
1111
task generateVersionClass {

src/main/java/fr/sandro642/github/ConnectLib.java

Lines changed: 87 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import fr.sandro642.github.spring.Application;
1616
import fr.sandro642.github.update.RetrieveLastVersion;
1717

18+
import java.io.IOException;
1819
import java.net.URI;
1920
import java.net.http.HttpClient;
2021
import java.net.http.HttpRequest;
@@ -156,47 +157,106 @@ public void webServices(int port, String nameDashboard) {
156157
}
157158

158159
/**
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
161164
*/
162-
public void wanImplement(String urlServ) {
165+
public void wanImplement(String urlServ, String nameComponent) {
166+
if (urlServ == null || urlServ.isBlank()) {
167+
return;
168+
}
163169
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);
170171

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";
172176

173-
Gson gson1 = new Gson();
174-
JsonObject root1 = gson1.fromJson(response1.body(), JsonObject.class);
177+
URI uri = URI.create(urlServLambda);
175178

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();
179184

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")
183191
.build();
184192

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+
}
186226

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+
}
189237

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);
194251
}
252+
} catch (Exception e) {
253+
Logger().ERROR(langManager.getMessage(CategoriesType.CONNECTLIB_CLASS, "wanimplement.errorfallback", Map.of("exception", e.getMessage())));
195254
}
196255

197-
SpringApp().startApplication().subscribe();
256+
} catch (IllegalArgumentException iae) {
257+
Logger().ERROR(langManager.getMessage(CategoriesType.CONNECTLIB_CLASS, "wanimplement.illegalargument", Map.of("exception", iae.getMessage())));
198258
} catch (Exception e) {
199-
e.printStackTrace();
259+
Logger().ERROR(langManager.getMessage(CategoriesType.CONNECTLIB_CLASS, "wanimplement.error", Map.of("exception", e.getMessage())));
200260
}
201261
}
202262

src/main/java/fr/sandro642/github/spring/controller/DataController.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ public static DataController getInstance() {
5858
public Map<String, Object> getStatus() {
5959
Map<String, Object> status = new HashMap<>();
6060
status.put("portInfo", connectLib.StoreAndRetrieve().get(connectLib.StoreAndRetrieve().DYNAMIC_PORT));
61+
String nameFromStore = connectLib.StoreAndRetrieve().get(connectLib.StoreAndRetrieve().NAME_DASHBOARD).toString();
62+
63+
status.put("nameComponent", nameFromStore);
6164
status.put("isInitialized", connectLib.isInitialized());
6265
status.put("resourceType", connectLib.HookManager().getResourceType());
6366
return status;
@@ -70,6 +73,7 @@ public Map<String, Object> getRoutes() {
7073

7174
// portInfo
7275
result.put("portInfo", connectLib.StoreAndRetrieve().get(connectLib.StoreAndRetrieve().DYNAMIC_PORT).toString());
76+
result.put("nameComponent", connectLib.StoreAndRetrieve().get(connectLib.StoreAndRetrieve().NAME_DASHBOARD));
7377

7478
List<Map<String, String>> routes = new ArrayList<>();
7579
if (map != null) {
@@ -94,6 +98,7 @@ public Map<String, Object> getRequests() {
9498
Map<String, Object> result = new HashMap<>();
9599

96100
result.put("portInfo", connectLib.StoreAndRetrieve().get(connectLib.StoreAndRetrieve().DYNAMIC_PORT).toString());
101+
result.put("nameComponent", connectLib.StoreAndRetrieve().get(connectLib.StoreAndRetrieve().NAME_DASHBOARD));
97102

98103
List<Request> requests = new ArrayList<>(requestsMap.values());
99104
result.put("requests", requests);

src/test/java/fr/sandro642/github/test/MainTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ public void pushsession() {
212212
try {
213213
connectLib.Logger().showLogs();
214214
connectLib.init(ResourceType.TEST_RESOURCES, LangType.ENGLISH, TestRoutes.class)
215-
.wanImplement("http://localhost:8080", "");
215+
.wanImplement("http://localhost:8080", "TestDashboard");
216216

217217
Thread.sleep(20000);
218218

0 commit comments

Comments
 (0)