Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Apache HTTP Client 4.5 with 5.3 (and introduce caching) #250

Merged
merged 1 commit into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}

}
Loading