public class HttpUtil {
private static final Logger logger = LoggerFactory.getLogger(HttpUtil.class);
+ private static final String LOG_FORMATTER = "[ {} ] {} ";
+ private static final String CLIENT_PRTOCOL_EXCEPTION = "Client protocol exception";
+ private static final String IO_EXCEPTION = "IO Exception occured";
+ private static final String EXCEPTION = "Exception occured";
+ private static final String HTTP_CLIENT_CLOSING_EXCEPTION = "Exception occured while closing httpClient";
/**
* common POST method for REST API calling by using map request body
String url,
Map<String, String> headerMap,
Map<String, String> requestBodyMap) {
- logger.info("[" + url + "]" + " API POST calling is starting......");
+ logger.info(LOG_FORMATTER ,url , " API POST calling is starting......");
HttpResponseResult responseResult = new HttpResponseResult(HttpStatus.SC_NOT_FOUND, BLANK);
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = httpClient.execute(httpPost);
setResponse(response, responseResult);
} catch (ClientProtocolException cpe) {
- logger.error(cpe.toString());
- cpe.printStackTrace();
+ logger.error(CLIENT_PRTOCOL_EXCEPTION,cpe);
} catch (IOException ioe) {
- logger.error(ioe.toString());
- ioe.printStackTrace();
+ logger.error(IO_EXCEPTION,ioe);
} catch (Exception e) {
- logger.error(e.toString());
- e.printStackTrace();
+ logger.error("EXCEPTION",e);
} finally {
try {
httpClient.close();
} catch (Exception e) {
- logger.error(e.toString());
- e.printStackTrace();
+ logger.error(HTTP_CLIENT_CLOSING_EXCEPTION,e);
}
}
- logger.info("[" + url + "]" + " API POST calling has finished!");
+ logger.info(LOG_FORMATTER ,url , " API POST calling has finished!");
return responseResult;
}
String url,
Map<String, String> headerMap,
String requestBodyJson) {
- logger.info("[" + url + "]" + " API POST calling is starting......");
+ logger.info(LOG_FORMATTER ,url , " API POST calling is starting......");
HttpResponseResult responseResult = new HttpResponseResult(HttpStatus.SC_NOT_FOUND, BLANK);
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = httpClient.execute(httpPost);
setResponse(response, responseResult);
} catch (ClientProtocolException cpe) {
- logger.error(cpe.toString());
- cpe.printStackTrace();
+ logger.error(CLIENT_PRTOCOL_EXCEPTION,cpe);
} catch (IOException ioe) {
- logger.error(ioe.toString());
- ioe.printStackTrace();
+ logger.error(IO_EXCEPTION,ioe);
} catch (Exception e) {
- logger.error(e.toString());
- e.printStackTrace();
+ logger.error(EXCEPTION,e);
} finally {
try {
httpClient.close();
} catch (Exception e) {
- logger.error(e.toString());
- e.printStackTrace();
+ logger.error(HTTP_CLIENT_CLOSING_EXCEPTION,e);
}
}
- logger.info("[" + url + "]" + " API POST calling has finished!");
+ logger.info(LOG_FORMATTER ,url , " API POST calling has finished!");
return responseResult;
}
public HttpResponseResult sendGetRequest(
String url,
Map<String, String> headerMap) {
- logger.info("[" + url + "]" + " API GET calling is starting......");
+ logger.info(LOG_FORMATTER ,url , "API GET calling is starting......");
HttpResponseResult responseResult = new HttpResponseResult(HttpStatus.SC_NOT_FOUND, BLANK);
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = httpClient.execute(httpGet);
setResponse(response, responseResult);
} catch (ClientProtocolException cpe) {
- logger.error(cpe.toString());
- cpe.printStackTrace();
+ logger.error(CLIENT_PRTOCOL_EXCEPTION,cpe);
} catch (IOException ioe) {
- logger.error(ioe.toString());
- ioe.printStackTrace();
+ logger.error(IO_EXCEPTION,ioe);
} catch (Exception e) {
- logger.error(e.toString());
- e.printStackTrace();
+ logger.error(EXCEPTION,e);
} finally {
try {
httpClient.close();
} catch (Exception e) {
- logger.error(e.toString());
- e.printStackTrace();
+ logger.error(HTTP_CLIENT_CLOSING_EXCEPTION,e);
}
}
- logger.info("[" + url + "]" + " API GET calling has finished!");
+ logger.info(LOG_FORMATTER ,url , "API GET calling has finished!");
return responseResult;
}
String url,
Map<String, String> headerMap,
Map<String, String> requestBodyMap) {
- logger.info("[" + url + "]" + " API PUT calling is starting......");
+ logger.info(LOG_FORMATTER ,url , "API PUT calling is starting......");
HttpResponseResult responseResult = new HttpResponseResult(HttpStatus.SC_NOT_FOUND, BLANK);
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = httpClient.execute(httpPut);
setResponse(response, responseResult);
} catch (ClientProtocolException cpe) {
- logger.error(cpe.toString());
- cpe.printStackTrace();
+ logger.error(CLIENT_PRTOCOL_EXCEPTION,cpe);
} catch (IOException ioe) {
- logger.error(ioe.toString());
- ioe.printStackTrace();
+ logger.error(IO_EXCEPTION,ioe);
} catch (Exception e) {
- logger.error(e.toString());
- e.printStackTrace();
+ logger.error(EXCEPTION,e);
} finally {
try {
httpClient.close();
} catch (Exception e) {
- logger.error(e.toString());
- e.printStackTrace();
+ logger.error(HTTP_CLIENT_CLOSING_EXCEPTION,e);
}
}
- logger.info("[" + url + "]" + " API PUT calling has finished!");
+ logger.info(LOG_FORMATTER ,url , " API PUT calling has finished!");
return responseResult;
}
String url,
Map<String, String> headerMap,
String requestBodyJson) {
- logger.info("[" + url + "]" + " API PUT calling is starting......");
+ logger.info(LOG_FORMATTER, url , "API PUT calling is starting......");
HttpResponseResult responseResult = new HttpResponseResult(HttpStatus.SC_NOT_FOUND, BLANK);
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = httpClient.execute(httpPut);
setResponse(response, responseResult);
} catch (ClientProtocolException cpe) {
- logger.error(cpe.toString());
- cpe.printStackTrace();
+ logger.error(CLIENT_PRTOCOL_EXCEPTION,cpe);
} catch (IOException ioe) {
- logger.error(ioe.toString());
- ioe.printStackTrace();
+ logger.error(IO_EXCEPTION,ioe);
} catch (Exception e) {
- logger.error(e.toString());
- e.printStackTrace();
+ logger.error(EXCEPTION,e);
} finally {
try {
httpClient.close();
} catch (Exception e) {
- logger.error(e.toString());
- e.printStackTrace();
+ logger.error(HTTP_CLIENT_CLOSING_EXCEPTION,e);
}
}
- logger.info("[" + url + "]" + " API PUT calling has finished!");
+ logger.info(LOG_FORMATTER,url , " API PUT calling has finished!");
return responseResult;
}
public HttpResponseResult sendDeleteRequest(
String url,
Map<String, String> headerMap) {
- logger.info("[" + url + "]" + " API DELETE calling is starting......");
+ logger.info(LOG_FORMATTER,url , " API DELETE calling is starting......");
HttpResponseResult responseResult = new HttpResponseResult(HttpStatus.SC_NOT_FOUND, BLANK);
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = httpClient.execute(httpDelete);
setResponse(response, responseResult);
} catch (ClientProtocolException cpe) {
- logger.error(cpe.toString());
- cpe.printStackTrace();
+ logger.error(CLIENT_PRTOCOL_EXCEPTION,cpe);
} catch (IOException ioe) {
- logger.error(ioe.toString());
- ioe.printStackTrace();
+ logger.error(IO_EXCEPTION,ioe);
} catch (Exception e) {
- logger.error(e.toString());
- e.printStackTrace();
+ logger.error(EXCEPTION,e);
} finally {
try {
httpClient.close();
} catch (Exception e) {
- logger.error(e.toString());
- e.printStackTrace();
+ logger.error(HTTP_CLIENT_CLOSING_EXCEPTION,e);
}
}
- logger.info("[" + url + "]" + " API DELETE calling has finished!");
+ logger.info(LOG_FORMATTER,url , " API DELETE calling has finished!");
return responseResult;
}
}
br.close();
} catch (IOException ioe) {
- ioe.printStackTrace();
+ logger.error("IO exception occured",ioe);
} finally {
if (null != br) {
try {
br.close();
} catch (IOException ioe) {
- ioe.printStackTrace();
+ logger.error("IO exception occured",ioe);
}
}
}