diff --git a/.gradle/buildOutputCleanup/buildOutputCleanup.lock b/.gradle/buildOutputCleanup/buildOutputCleanup.lock index d7e8a75e..b30b74cb 100644 Binary files a/.gradle/buildOutputCleanup/buildOutputCleanup.lock and b/.gradle/buildOutputCleanup/buildOutputCleanup.lock differ diff --git a/.gradle/buildOutputCleanup/cache.properties b/.gradle/buildOutputCleanup/cache.properties index 32e88cfb..e9e87e7d 100644 --- a/.gradle/buildOutputCleanup/cache.properties +++ b/.gradle/buildOutputCleanup/cache.properties @@ -1,2 +1,2 @@ -#Wed Sep 07 19:58:33 IST 2022 -gradle.version=7.2 +#Thu Sep 08 12:18:11 IST 2022 +gradle.version=7.3.3 diff --git a/build.gradle b/build.gradle index bc0cd170..66509e5b 100644 --- a/build.gradle +++ b/build.gradle @@ -117,6 +117,9 @@ dependencies { //Static Mocks testImplementation group: 'org.mockito', name: 'mockito-inline', version: '3.8.0' + // https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient + implementation group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.1' + } tasks.named('test') { diff --git a/src/main/java/com/neosoft/app/domain/utils/HttpGatewayUtil.java b/src/main/java/com/neosoft/app/domain/utils/HttpGatewayUtil.java new file mode 100644 index 00000000..32d258d3 --- /dev/null +++ b/src/main/java/com/neosoft/app/domain/utils/HttpGatewayUtil.java @@ -0,0 +1,199 @@ +package com.neosoft.app.domain.utils; + +import org.apache.http.HttpEntity; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpDelete; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpPut; +import org.apache.http.conn.HttpHostConnectException; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; +import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.neosoft.app.rest.errors.CustomException; +import com.neosoft.app.rest.errors.HttpStatusCode; + +public class HttpGatewayUtil { + private final Logger log = LoggerFactory.getLogger(HttpGatewayUtil.class); + private static final String CONTENT_TYPE = "Content-type"; + private static final String ACCEPT = "Accept"; + private static final String AUTHORIZATION = "Authorization"; + private static final String APPLICATION_JSON = "application/json"; + private static final String API_ENDPOINT = "API Endpoint : {}"; + private String apiEndpoint; + private Object requestBodyDTO; + + public String postRequestWithStringBody(String jwtToken) { + + String jsonObject = null; + log.debug("Post Request String Method Called in HttpGateway"); + log.debug(API_ENDPOINT ,apiEndpoint); + log.debug("Request Body - {}",requestBodyDTO); + isTokenNullOrValid(jwtToken); + try(CloseableHttpClient client = HttpClients.createDefault();){ + HttpPost http = new HttpPost(apiEndpoint); + String objJackson = requestBodyDTO.toString(); + StringEntity entity = new StringEntity(objJackson); + http.setEntity(entity); + http.setHeader(ACCEPT, APPLICATION_JSON ); + http.setHeader(CONTENT_TYPE, APPLICATION_JSON ); + http.setHeader(AUTHORIZATION, jwtToken); + CloseableHttpResponse response = client.execute(http); + HttpEntity entityResponse = response.getEntity(); + String result = EntityUtils.toString(entityResponse); + jsonObject = result; + } catch (Exception e) { + handleException(e); + log.error(e.toString()); + + } + + return jsonObject; + + } + + public String postRequest(String jwtToken) { + + log.debug("Post Request Method Called in HttpGateway"); + log.debug(API_ENDPOINT ,apiEndpoint); + log.debug("REQUEST BODY : {}", requestBodyDTO); + isTokenNullOrValid(jwtToken); + String result = "{}"; + try(CloseableHttpClient client = HttpClients.createDefault();){ + HttpPost http = new HttpPost(apiEndpoint); + ObjectMapper objectMapper = new ObjectMapper(); + String objJackson = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(requestBodyDTO); + StringEntity entity = new StringEntity(objJackson); + http.setEntity(entity); + http.setHeader(ACCEPT, APPLICATION_JSON ); + http.setHeader(CONTENT_TYPE, APPLICATION_JSON ); + http.setHeader(AUTHORIZATION, jwtToken); + log.debug("Sending POST request"); + CloseableHttpResponse response = client.execute(http); + HttpEntity entityResponse = response.getEntity(); + result = EntityUtils.toString(entityResponse); + } catch (Exception e) { + handleException(e); + log.error(e.toString()); + + } + + return result; + + } + + public String putRequest(String jwtToken) { + log.debug("Put Request Method Called in HttpGateway"); + log.debug(API_ENDPOINT, apiEndpoint); + isTokenNullOrValid(jwtToken); + String result = "{}"; + + try(CloseableHttpClient client = HttpClients.createDefault();){ + HttpPut http = new HttpPut(apiEndpoint); + ObjectMapper objectMapper = new ObjectMapper(); + String objJackson = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(requestBodyDTO); + StringEntity entity = new StringEntity(objJackson); + http.setEntity(entity); + http.setHeader(ACCEPT, APPLICATION_JSON ); + http.setHeader(CONTENT_TYPE, APPLICATION_JSON ); + http.setHeader(AUTHORIZATION, jwtToken); + log.debug("Sending DELETE request"); + CloseableHttpResponse response = client.execute(http); + HttpEntity entityResponse = response.getEntity(); + result = EntityUtils.toString(entityResponse); + } catch (Exception e) { + handleException(e); + log.error(e.toString()); + + } + + return result; + + } + + public String deleteRequest(String jwtToken) { + log.debug("Delete Request Method Called in HttpGateway"); + log.debug(API_ENDPOINT, apiEndpoint); + isTokenNullOrValid(jwtToken); + String result = "{}"; + try(CloseableHttpClient client = HttpClients.createDefault();){ + HttpDelete http = new HttpDelete(apiEndpoint); + http.setHeader(ACCEPT, APPLICATION_JSON ); + http.setHeader(CONTENT_TYPE, APPLICATION_JSON ); + http.setHeader(AUTHORIZATION, jwtToken); + CloseableHttpResponse response = client.execute(http); + HttpEntity entityResponse = response.getEntity(); + result = EntityUtils.toString(entityResponse); + } catch (Exception e) { + handleException(e); + log.error(e.toString()); + + } + + return result; + + + } + + public JSONObject getRequest(String jwtToken) { + + JSONObject jsonObject = null; + isTokenNullOrValid(jwtToken); + try(CloseableHttpClient client = HttpClients.createDefault();){ + HttpGet http = new HttpGet(apiEndpoint); + http.setHeader(ACCEPT, APPLICATION_JSON ); + http.setHeader(CONTENT_TYPE, APPLICATION_JSON ); + http.setHeader(AUTHORIZATION, jwtToken); + CloseableHttpResponse response = client.execute(http); + HttpEntity entityResponse = response.getEntity(); + String result = EntityUtils.toString(entityResponse); + jsonObject = new JSONObject(result); + + + } catch (Exception e) { + handleException(e); + log.error(e.toString()); + } + + return jsonObject; + } + + public String getRequestV2(String jwtToken) { + + String result = "{}"; + isTokenNullOrValid(jwtToken); + try(CloseableHttpClient client = HttpClients.createDefault();){ + HttpGet http = new HttpGet(apiEndpoint); + http.setHeader(ACCEPT, APPLICATION_JSON ); + http.setHeader(CONTENT_TYPE, APPLICATION_JSON ); + http.setHeader(AUTHORIZATION, jwtToken); + CloseableHttpResponse response = client.execute(http); + HttpEntity entityResponse = response.getEntity(); + result = EntityUtils.toString(entityResponse); + } catch (Exception e) { + handleException(e); + log.error(e.toString()); + + } + return result; + + } + + private void handleException(Exception exception) { + if(exception instanceof HttpHostConnectException) { + throw new CustomException(HttpStatusCode.SERVER_UNAVAILABLE.getCode(), + HttpStatusCode.SERVER_UNAVAILABLE ,"Unable to Connect With the Server"); + } + } + + public void isTokenNullOrValid(String jwtToken) { + if((null == jwtToken || jwtToken.isEmpty() || jwtToken.isBlank() || !jwtToken.startsWith("Bearer "))) + throw new CustomException(HttpStatusCode.FORBIDDEN_EXCEPTION.getCode(), + HttpStatusCode.FORBIDDEN_EXCEPTION, "Provide valid token"); + } +} diff --git a/src/main/java/com/neosoft/app/rest/errors/HttpStatusCode.java b/src/main/java/com/neosoft/app/rest/errors/HttpStatusCode.java index 2f49f62d..ec118870 100644 --- a/src/main/java/com/neosoft/app/rest/errors/HttpStatusCode.java +++ b/src/main/java/com/neosoft/app/rest/errors/HttpStatusCode.java @@ -43,7 +43,7 @@ public enum HttpStatusCode { UNAUTHORIZED_EXCEPTION(401, "Unauthorized To Perform Request"), FORBIDDEN_EXCEPTION(403, "Forbidden access attempted"), - + INTERNAL_SERVER_ERROR (500, "Internal Server Error Occured"), PROCESSING_NOT_COMPLETED (202, "Request cannot be Processed"),