ADD - RegisterSecurityRepository and ApiConfig
This commit is contained in:
parent
ec58209519
commit
81a070bd3a
@ -0,0 +1,87 @@
|
|||||||
|
package com.banesco.infraestructure.config;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class ApiConfig {
|
||||||
|
private String url;
|
||||||
|
private String path;
|
||||||
|
private Timeout timeout;
|
||||||
|
private Map<String, String> config;
|
||||||
|
|
||||||
|
public ApiConfig() {}
|
||||||
|
|
||||||
|
public ApiConfig(String url, String path, Timeout timeout, Map<String, String> config) {
|
||||||
|
this.url = url;
|
||||||
|
this.path = path;
|
||||||
|
this.timeout = timeout;
|
||||||
|
this.config = config;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUrl() {
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUrl(String url) {
|
||||||
|
this.url = url;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPath() {
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPath(String path) {
|
||||||
|
this.path = path;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Timeout getTimeout() {
|
||||||
|
return timeout;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTimeout(Timeout timeout) {
|
||||||
|
this.timeout = timeout;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, String> getConfig() {
|
||||||
|
return config;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setConfig(Map<String, String> config) {
|
||||||
|
this.config = config;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Timeout {
|
||||||
|
|
||||||
|
private int connect = 15000;
|
||||||
|
private int response = 15000;
|
||||||
|
|
||||||
|
public Timeout() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Timeout(int connect, int response) {
|
||||||
|
this.connect = connect;
|
||||||
|
this.response = response;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getConnect() {
|
||||||
|
return connect;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setConnect(int connect) {
|
||||||
|
this.connect = connect;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getResponse() {
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setResponse(int response) {
|
||||||
|
this.response = response;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "ApiConfig [url=" + url + ", path=" + path + ", timeout=" + timeout + ", config=" + config + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,90 @@
|
|||||||
|
package com.banesco.infraestructure.repository;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import com.banesco.infraestructure.config.ApiConfig;
|
||||||
|
import com.banesco.infraestructure.helpers.JsonHelper;
|
||||||
|
import com.banesco.infraestructure.helpers.LoggerHelper;
|
||||||
|
|
||||||
|
public class RegisterSecurityRepository {
|
||||||
|
|
||||||
|
private static final Logger logger = Logger.getLogger(RegisterSecurityRepository.class.getName());
|
||||||
|
private final LoggerHelper loggerHelper;
|
||||||
|
private final ApiConfig registerSecurityAPI;
|
||||||
|
private final ExecutorService executor = Executors.newCachedThreadPool();
|
||||||
|
|
||||||
|
public RegisterSecurityRepository(LoggerHelper loggerHelper, ApiConfig registerSecurityAPI) {
|
||||||
|
this.loggerHelper = loggerHelper;
|
||||||
|
this.registerSecurityAPI = registerSecurityAPI;
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> void writeTrice(String requestId, T registerSecurityRq) {
|
||||||
|
logger.info(loggerHelper.buildInfoPrivateRequest(requestId, "RegisterSecurityRq", registerSecurityRq));
|
||||||
|
|
||||||
|
CompletableFuture.runAsync(() -> {
|
||||||
|
try {
|
||||||
|
URL url = new URL(registerSecurityAPI.getUrl());
|
||||||
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||||
|
connection.setRequestMethod("POST");
|
||||||
|
connection.setRequestProperty("Content-Type", "application/json");
|
||||||
|
connection.setConnectTimeout(registerSecurityAPI.getTimeout().getConnect());
|
||||||
|
connection.setReadTimeout(registerSecurityAPI.getTimeout().getResponse());
|
||||||
|
connection.setDoOutput(true);
|
||||||
|
|
||||||
|
String jsonInputString = JsonHelper.getJsonFromObject(registerSecurityRq);
|
||||||
|
|
||||||
|
try (OutputStream os = connection.getOutputStream()) {
|
||||||
|
byte[] input = jsonInputString.getBytes(StandardCharsets.UTF_8);
|
||||||
|
os.write(input, 0, input.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
int responseCode = connection.getResponseCode();
|
||||||
|
if (responseCode == HttpURLConnection.HTTP_OK) {
|
||||||
|
try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
|
||||||
|
StringBuilder response = new StringBuilder();
|
||||||
|
String responseLine;
|
||||||
|
while ((responseLine = br.readLine()) != null) {
|
||||||
|
response.append(responseLine.trim());
|
||||||
|
}
|
||||||
|
logger.info(loggerHelper.buildInfoPrivateResponse(requestId, "RegisterSecurityRs", response.toString()));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getErrorStream(), StandardCharsets.UTF_8))) {
|
||||||
|
StringBuilder response = new StringBuilder();
|
||||||
|
String responseLine;
|
||||||
|
while ((responseLine = br.readLine()) != null) {
|
||||||
|
response.append(responseLine.trim());
|
||||||
|
}
|
||||||
|
logger.info(loggerHelper.buildInfoPrivateResponse(requestId, "RegisterSecurityRs", response.toString()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
connection.disconnect();
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
logger.log(Level.SEVERE, String.format("Error interno writeTrice: %s", e.getMessage()));
|
||||||
|
logger.info(loggerHelper.buildInfoPrivateResponse(requestId, "RegisterSecurityRs", e.getMessage()));
|
||||||
|
}
|
||||||
|
}, executor).exceptionally(ex -> {
|
||||||
|
logger.log(Level.SEVERE, String.format("Async Error interno writeTrice: %s", ex.getMessage()));
|
||||||
|
logger.info(loggerHelper.buildInfoPrivateResponse(requestId, "RegisterSecurityRs", ex.getMessage()));
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void shutdown() {
|
||||||
|
executor.shutdown();
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user