Skip to content

Commit

Permalink
Attempt to fix httpclient issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ramari16 committed Nov 1, 2024
1 parent eb358c2 commit 967b892
Showing 1 changed file with 18 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicHeader;
import org.apache.http.ssl.SSLContexts;
import org.slf4j.Logger;
Expand All @@ -56,6 +57,8 @@ public static HttpResponse retrieveGetResponse(String uri, List<Header> headers)
return retrieveGetResponse(uri, headers.toArray(new Header[headers.size()]));
}

private static final HttpClient client = getConfiguredHttpClient();

/**
* resource level get, which will throw a <b>ResourceInterfaceException</b> if cannot get response back from the url
*
Expand All @@ -67,8 +70,7 @@ public static HttpResponse retrieveGetResponse(String uri, Header[] headers) {
try {
logger.debug("HttpClientUtil retrieveGetResponse()");

HttpClient client = getConfiguredHttpClient();
return simpleGet(client, uri, headers);
return simpleGet(uri, headers);
} catch (ApplicationException e) {
throw new ResourceInterfaceException(uri, e);
}
Expand Down Expand Up @@ -115,8 +117,7 @@ public static HttpResponse retrievePostResponse(String uri, Header[] headers, St
headerList = new ArrayList<>(Arrays.asList(headers));
headerList.add(new BasicHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON));

HttpClient client = getConfiguredHttpClient();
return simplePost(uri, client, new StringEntity(body), headerList.toArray(new Header[headerList.size()]));
return simplePost(uri, new StringEntity(body), headerList.toArray(new Header[headerList.size()]));
} catch (ApplicationException | UnsupportedEncodingException e) {
throw new ResourceInterfaceException(uri, e);
}
Expand Down Expand Up @@ -206,17 +207,13 @@ public static void throwInternalResponseError(HttpResponse response, String base
* Basic and general post function using Apache Http Client
*
* @param uri
* @param client
* @param requestBody
* @param headers
* @return HttpResponse
* @throws ApplicationException
*/
public static HttpResponse simplePost(String uri, HttpClient client, StringEntity requestBody, Header... headers)
public static HttpResponse simplePost(String uri, StringEntity requestBody, Header... headers)
throws ApplicationException {
if (client == null) {
client = getConfiguredHttpClient();
}

HttpPost post = new HttpPost(uri);
post.setHeaders(headers);
Expand All @@ -235,12 +232,11 @@ public static HttpResponse simplePost(String uri, HttpClient client, StringEntit
*
* @param uri
* @param requestBody
* @param client
* @param headers
* @return InputStream
*/
public static InputStream simplePost(String uri, StringEntity requestBody, HttpClient client, Header... headers) {
HttpResponse response = simplePost(uri, client, requestBody, headers);
public static InputStream simplePostStream(String uri, StringEntity requestBody, Header... headers) {
HttpResponse response = simplePost(uri, requestBody, headers);

try {
return response.getEntity().getContent();
Expand All @@ -250,41 +246,14 @@ public static InputStream simplePost(String uri, StringEntity requestBody, HttpC
}
}

/**
* only works if the POST method returns a JSON response body
*
* @param uri
* @param requestBody
* @param client
* @param objectMapper
* @param headers
* @return
*/
public static JsonNode simplePost(String uri, StringEntity requestBody, HttpClient client,
ObjectMapper objectMapper, Header... headers) {
try {
return objectMapper.readTree(simplePost(uri, requestBody, client, headers));
} catch (IOException ex) {
logger.error("simplePost() Exception: {}, cannot parse content from by POST from url: {}", ex.getMessage(),
uri);
throw new ApplicationException("Inner problem, please contact system admin and check the server log");
}
}

/**
* for general and basic use of GET function using Apache Http Client
*
* @param client
* @param uri
* @param headers
* @return
* @throws ApplicationException
*/
public static HttpResponse simpleGet(HttpClient client, String uri, Header... headers) throws ApplicationException {
if (client == null) {
client = getConfiguredHttpClient();
}

public static HttpResponse simpleGet(String uri, Header... headers) throws ApplicationException {
HttpGet get = new HttpGet(uri);
get.setHeaders(headers);

Expand All @@ -296,12 +265,12 @@ public static HttpResponse simpleGet(HttpClient client, String uri, Header... he
}
}

public static InputStream simpleGet(String uri, HttpClient client, Header... headers) {
return simpleGetWithConfig(uri, client, null, headers);
public static InputStream simpleGetStream(String uri, Header... headers) {
return simpleGetWithConfig(uri, null, headers);
}

public static InputStream simpleGetWithConfig(
String uri, HttpClient client, RequestConfig config, Header... headers
String uri, RequestConfig config, Header... headers
) throws ApplicationException {
HttpGet get = new HttpGet(uri);
get.setHeaders(headers);
Expand All @@ -319,29 +288,11 @@ public static InputStream simpleGetWithConfig(
}
}

/**
* only work if the GET method returns a JSON response body
*
* @param uri
* @param client
* @param objectMapper
* @param headers
* @return
*/
public static JsonNode simpleGet(String uri, HttpClient client, ObjectMapper objectMapper, Header... headers) {
try {
return objectMapper.readTree(simpleGet(uri, client, headers));
} catch (IOException ex) {
logger.error("simpleGet() cannot parse content from by GET from url: {}", uri, ex);
throw new ApplicationException("Inner problem, please contact system admin and check the server log");
}
}

public static JsonNode simpleGetWithConfig(
String uri, HttpClient client, ObjectMapper objectMapper, RequestConfig requestConfig, Header... headers
String uri, ObjectMapper objectMapper, RequestConfig requestConfig, Header... headers
) {
try {
return objectMapper.readTree(simpleGetWithConfig(uri, client, requestConfig, headers));
return objectMapper.readTree(simpleGetWithConfig(uri, requestConfig, headers));
} catch (IOException ex) {
logger.error("simpleGet() cannot parse content from by GET from url: {}", uri, ex);
throw new ApplicationException("Inner problem, please contact system admin and check the server log");
Expand All @@ -364,13 +315,17 @@ public static HttpClient getConfiguredHttpClient() {
limited.add(suite);
}
}
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
connectionManager.setMaxTotal(200); // Maximum total connections
connectionManager.setDefaultMaxPerRoute(50); // Maximum connections per route

return HttpClients.custom()
.setSSLSocketFactory(new SSLConnectionSocketFactory(
SSLContexts.createSystemDefault(),
new String[]{"TLSv1.2"},
limited.toArray(new String[limited.size()]),
SSLConnectionSocketFactory.getDefaultHostnameVerifier()))
.setConnectionManager(connectionManager)
.build();
} catch( NoSuchAlgorithmException | KeyManagementException e) {
logger.warn("Unable to establish SSL context. using default client", e);
Expand Down

0 comments on commit 967b892

Please sign in to comment.