package com.example.services; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.Properties; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import java.io.IOException; import java.io.InputStream; import java.net.URI; import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @Path("/proximis") public class ProximisService { private static final Logger logger = LoggerFactory.getLogger(ProximisService.class); @GET @Path("/project/fulfillment/{orderCode}") @Produces(MediaType.APPLICATION_JSON) public Response getOrderDetail(@PathParam("orderCode") String orderCode) { if (orderCode == null) { return Response.status(Response.Status.BAD_REQUEST).entity("{\"error\":\"orderCode is required\"}").build(); } String environment = loadEnvironment(); try { Properties apiProperties = loadApiProperties(); String url = apiProperties.getProperty(environment + ".proximis.api.url"); String urlApp = apiProperties.getProperty(environment + ".proximis.app.url"); String bearerToken = ""; DriverManager.setLoginTimeout(5); // Définir le timeout à 5 secondes try (DatabaseConnectOBI databaseConnection = new DatabaseConnectOBI()) { String query = "SELECT authorization_value FROM obi.obi_param_access_token WHERE authorization_header = ?" + " and cloud_type = 'PROXIMIS' AND application = 'OBI_ACCESS_TOKEN' AND version = 'V2'"; PreparedStatement stmt = databaseConnection.getConnection().prepareStatement(query); stmt.setString(1, "Authorization"); ResultSet rs = stmt.executeQuery(); if (rs.next()) { bearerToken = rs.getString(1); } } if (bearerToken == null || bearerToken.isEmpty()) { return Response.status(Response.Status.NOT_FOUND).entity("{\"error\":\"Empty bearer token\"}").build(); } else { HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder() .uri(URI.create(url + "/project/fulfillment/" + orderCode)) .header("Authorization", bearerToken) .GET() .build(); HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); if (response.statusCode() == 200) { JSONObject commonObject = new JSONObject(); commonObject.put("urlApp", urlApp); JSONObject jsonResponse = new JSONObject(); jsonResponse.put("common", commonObject); jsonResponse.put("oms", new JSONObject(response.body())); return Response.ok(jsonResponse.toString()).build(); } else if (response.statusCode() == 401) { return Response.status(Response.Status.UNAUTHORIZED).entity("{\"error\":\"Invalid token\"}").build(); } else { return Response.status(Response.Status.NOT_FOUND).entity("{\"error\":\"Failed to get order detail\"}").build(); } } } catch (Exception e) { return Response.status(Response.Status.SERVICE_UNAVAILABLE).entity("{\"error\":\"Unexpected error\"}").build(); } } private String loadEnvironment() { Properties envProperties = loadProperties("env.properties"); return envProperties.getProperty("environment"); } private Properties loadApiProperties() { Properties apiProperties = loadProperties("api.properties"); return apiProperties; } private Properties loadProperties(String fileName) { Properties properties = new Properties(); try (InputStream input = getClass().getClassLoader().getResourceAsStream(fileName)) { properties.load(input); } catch (IOException e) { e.printStackTrace(); // Handle the exception correctly for your application } return properties; } }