102 lines
3.8 KiB
Java
102 lines
3.8 KiB
Java
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.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 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 = ?";
|
|
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.INTERNAL_SERVER_ERROR).entity("{\"error\":\"Failed to access proximis API\"}").build();
|
|
} else {
|
|
HttpClient client = HttpClient.newHttpClient();
|
|
HttpRequest request = HttpRequest.newBuilder()
|
|
.uri(URI.create(url + "/project/fulfillment/" + orderCode))
|
|
.header("Authorization", bearerToken)
|
|
.GET()
|
|
.build();
|
|
|
|
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
|
|
|
if (response.statusCode() == 200) {
|
|
return Response.ok(response.body()).build();
|
|
} else {
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("{\"error\":\"Failed to get order detail\"}").build();
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
logger.error("Failed to get order detail", e);
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("{\"error\":\"Failed to get order detail\"}").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;
|
|
}
|
|
|
|
} |