agtemplateweb/src/main/java/br/com/agapesistemas/agtemplate/service/BaseService.java

119 lines
5.1 KiB
Java

package br.com.agapesistemas.agtemplate.service;
import br.com.agapesistemas.agtemplate.exception.base.BaseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import jakarta.enterprise.inject.Produces;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import java.io.Serializable;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
public class BaseService implements Serializable {
protected static final String DOMINIO_SERVICE = "http://localhost:8080";
@Produces
public static final ObjectMapper getDefaultObjectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());
return objectMapper;
}
protected static String convertObjectToJson(Object object) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.writeValueAsString(object);
}
protected static Object handleResponse(HttpResponse response, Object responseClass) throws IOException, Exception {
// Verifica se a resposta foi bem-sucedida (código 2xx)
int statusCode = response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
String responseBody = null;
if (entity != null) {
responseBody = EntityUtils.toString(entity);
if (statusCode >= 200 && statusCode < 300) {
if(responseBody.isEmpty()){
return null;
}
ObjectMapper objectMapper = getDefaultObjectMapper();
if(responseClass instanceof TypeReference){
return objectMapper.readValue(responseBody, (TypeReference)responseClass);
}
return objectMapper.readValue(responseBody, responseClass.getClass());
} else if (statusCode == 400) {
ObjectMapper objectMapper = getDefaultObjectMapper();
Problem problem = objectMapper.readValue(responseBody, Problem.class);
throw new BaseException(problem.getDetail());
} else {
throw new Exception("Falha na requisição. Código de status: " + statusCode);
}
}
return null;
}
public static <T> Object post(String token,String uri, String json, TypeReference<T> typeReference) throws IOException, Exception {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(uri);
httpPost.setEntity(new StringEntity(json, ContentType.APPLICATION_JSON));
HttpResponse postResponse = httpClient.execute(httpPost);
return handleResponse(postResponse, typeReference);
}
public static <T> Object put(String token,String uri, String json, TypeReference<T> typeReference) throws IOException, Exception {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPut httpPut = new HttpPut(uri);
httpPut.setEntity(new StringEntity(json, ContentType.APPLICATION_JSON));
HttpResponse postResponse = httpClient.execute(httpPut);
return handleResponse(postResponse, typeReference);
}
public static <T> void delete(String token,String uri) throws IOException, Exception {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpDelete httpDelete = new HttpDelete(uri);
HttpResponse postResponse = httpClient.execute(httpDelete);
handleResponse(postResponse, String.class);
}
public static <T> Object get(String token,String uri, TypeReference<T> typeReference) throws IOException, Exception {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(uri);
HttpResponse postResponse = httpClient.execute(httpGet);
return handleResponse(postResponse, typeReference);
}
public static String getUuidCliente(String token) {
Claims claims = getClaimsToken(token);
return claims.get("agc_codigo", String.class);
}
public static Claims getClaimsToken(String token) {
Claims claims = decodeJWTWithoutValidation(token);
return claims;
}
private static Claims decodeJWTWithoutValidation(String jwt) {
String[] parts = jwt.split("\\.");
if (parts.length != 3) {
throw new IllegalArgumentException("Invalid JWT token");
}
Claims jwsClaims = Jwts.parser().parseClaimsJwt(parts[0] + "." + parts[1] + ".").getBody();
return jwsClaims;
}
}