Skip to content

Commit

Permalink
Merge pull request #250 from guusdk/httpclient-5
Browse files Browse the repository at this point in the history
Replace Apache HTTP Client 4.5 with 5.3 (and introduce caching)
  • Loading branch information
akrherz authored Jun 28, 2024
2 parents 310ee85 + 81600a5 commit 48d8666
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 32 deletions.
19 changes: 16 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -278,15 +278,28 @@
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.14</version>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.3.1</version>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5-cache</artifactId>
<version>5.3.1</version>
</dependency>

<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.15.4</version>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.14</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
48 changes: 19 additions & 29 deletions src/main/java/org/jivesoftware/webservices/RestClient.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package org.jivesoftware.webservices;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.hc.client5.http.impl.cache.CacheConfig;
import org.apache.hc.client5.http.impl.cache.CachingHttpClients;
import org.apache.hc.client5.http.impl.classic.*;
import org.apache.hc.core5.http.*;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.support.ClassicRequestBuilder;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
Expand All @@ -16,37 +17,26 @@ public class RestClient {

private static final Logger Log = LoggerFactory.getLogger( RestClient.class );

private static final CacheConfig cacheConfig = CacheConfig.DEFAULT;

public JSONObject get(String url) {
JSONObject result = null;

HttpClient client = new HttpClient();
HttpMethod method = new GetMethod(url);

try
try (final CloseableHttpClient httpclient = CachingHttpClients.custom().setCacheConfig(cacheConfig).build())
{
// Execute the method.
int statusCode = client.executeMethod( method );

if ( statusCode != HttpStatus.SC_OK )
{
Log.warn( "Method (for '{}') failed: {}", url, method.getStatusLine() );
}

// Deal with the response.
// Use caution: ensure correct character encoding and is not binary data
String response = method.getResponseBodyAsString();
result = new JSONObject(response);
} catch (JSONException e ) {
Log.warn( "Invalid content while querying '{}'", url, e );
} catch (HttpException e) {
Log.warn( "Fatal protocol violation while querying '{}'", url, e );
final ClassicHttpRequest httpGet = ClassicRequestBuilder.get(url).build();
result = httpclient.execute(httpGet, response -> {
try {
return new JSONObject(EntityUtils.toString(response.getEntity()));
} catch (JSONException e) {
Log.warn("Invalid content while querying '{}'", url, e);
return null;
}
});
} catch (IOException e) {
Log.warn( "Fatal transport error while querying '{}'", url, e );
} finally {
method.releaseConnection();
Log.warn("Fatal transport error while querying '{}'", url, e);
}

return result;
}

}

0 comments on commit 48d8666

Please sign in to comment.