From 81600a57d3a42a1fae1a36005dfd6dcd288738bf Mon Sep 17 00:00:00 2001 From: Guus der Kinderen Date: Fri, 28 Jun 2024 13:55:15 +0200 Subject: [PATCH] Replace Apache HTTP Client 4.5 with 5.3 (and introduce caching) This updates the HTTP client used in our 'rest client'. The new client is configured to use client-sided caching, which hopefully reduces the amount of queries that are performed. --- pom.xml | 19 ++++++-- .../jivesoftware/webservices/RestClient.java | 48 ++++++++----------- 2 files changed, 35 insertions(+), 32 deletions(-) diff --git a/pom.xml b/pom.xml index 68fa1473..508ffe67 100644 --- a/pom.xml +++ b/pom.xml @@ -278,9 +278,15 @@ - org.apache.httpcomponents - httpclient - 4.5.14 + org.apache.httpcomponents.client5 + httpclient5 + 5.3.1 + + + + org.apache.httpcomponents.client5 + httpclient5-cache + 5.3.1 @@ -288,5 +294,12 @@ jsoup 1.15.4 + + + org.apache.httpcomponents + httpclient + 4.5.14 + test + diff --git a/src/main/java/org/jivesoftware/webservices/RestClient.java b/src/main/java/org/jivesoftware/webservices/RestClient.java index 91d35b85..3eb3c950 100644 --- a/src/main/java/org/jivesoftware/webservices/RestClient.java +++ b/src/main/java/org/jivesoftware/webservices/RestClient.java @@ -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; @@ -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; } - }