Hi @TB3dock
You can use
CloseableHttpClient client = HttpClients.createDefault()
and execute the REST APIs on AEM side.
For this you will need to add the below dependency and rest all will work like butter 🙂
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.8</version>
</dependency>
A sample method that I personally use for making all the API requests from AEM:
public APIResponse makePostWSCall(String endPointURL, String requestBody, Map<String, String> requestParams, Map<String, String> headers) {
try (CloseableHttpClient client = HttpClients.createDefault()) {
final long startTime = System.nanoTime();
endPointURL = makeEndPointURL(endPointURL, requestParams);
final String endPoint = StringEscapeUtils.escapeJava(endPointURL);
log.debug("RestServiceImpl :: makePostWSCall :: EndPoint URL --> {}", endPoint);
HttpPost httpPost = new HttpPost(endPointURL);
if (null != headers && !headers.isEmpty()) {
for (Map.Entry<String, String> headerEntry : headers.entrySet()) {
httpPost.addHeader(headerEntry.getKey(), headerEntry.getValue());
}
}
requestBody = WebServiceUtil.getPrettyJsonString(requestBody);
log.debug("RestServiceImpl :: makePostWSCall :: Request Body --> {}", requestBody);
if (StringUtils.isNotEmpty(requestBody)) {
StringEntity stringEntity = new StringEntity(requestBody, ContentType.APPLICATION_JSON);
httpPost.setEntity(stringEntity);
}
APIResponse apiResponse = new APIResponse();
CloseableHttpResponse response = client.execute(httpPost);
final int statusCode = response.getStatusLine().getStatusCode();
apiResponse.setStatusCode(statusCode);
log.info("RestServiceImpl :: makePostWSCall :: Response Status Code --> {}", statusCode);
getResponse(endPointURL, startTime, apiResponse, response, "RestServiceImpl :: WSCall :: Response --> {}");
return apiResponse;
} catch (Exception e) {
log.error("RestServiceImpl :: makePostWSCall :: Exception --> ", e);
}
return null;
}
Hope this will be helpful!
Thanks!