This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push: new 1662b2c5cf9 CAMEL-21847: camel-http should close http client for oauth token requests when stopping 1662b2c5cf9 is described below commit 1662b2c5cf90e21dac8a69a683d192a66ca6090d Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Fri Mar 7 20:39:28 2025 +0100 CAMEL-21847: camel-http should close http client for oauth token requests when stopping --- .../camel/component/http/CompositeHttpConfigurer.java | 15 ++++++++++++++- .../org/apache/camel/component/http/HttpEndpoint.java | 4 ++-- .../camel/component/http/OAuth2ClientConfigurer.java | 18 ++++++++++++++++-- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/components/camel-http/src/main/java/org/apache/camel/component/http/CompositeHttpConfigurer.java b/components/camel-http/src/main/java/org/apache/camel/component/http/CompositeHttpConfigurer.java index 052ba009249..321307f65b8 100644 --- a/components/camel-http/src/main/java/org/apache/camel/component/http/CompositeHttpConfigurer.java +++ b/components/camel-http/src/main/java/org/apache/camel/component/http/CompositeHttpConfigurer.java @@ -19,9 +19,11 @@ package org.apache.camel.component.http; import java.util.ArrayList; import java.util.List; +import org.apache.camel.support.service.ServiceHelper; +import org.apache.camel.support.service.ServiceSupport; import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; -public class CompositeHttpConfigurer implements HttpClientConfigurer { +public class CompositeHttpConfigurer extends ServiceSupport implements HttpClientConfigurer { private final List<HttpClientConfigurer> configurers = new ArrayList<>(); @@ -51,4 +53,15 @@ public class CompositeHttpConfigurer implements HttpClientConfigurer { } } + @Override + protected void doStart() throws Exception { + super.doStart(); + ServiceHelper.startService(configurers); + } + + @Override + protected void doStop() throws Exception { + super.doStop(); + ServiceHelper.stopService(configurers); + } } diff --git a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java index 4129e00d2ea..21d2b289ecc 100644 --- a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java +++ b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java @@ -357,7 +357,7 @@ public class HttpEndpoint extends HttpCommonEndpoint implements LineNumberAware httpActivityListener = new LoggingHttpActivityListener(); } CamelContextAware.trySetCamelContext(httpActivityListener, getCamelContext()); - ServiceHelper.startService(httpActivityListener); + ServiceHelper.startService(httpActivityListener, httpClientConfigurer); } @Override @@ -369,7 +369,7 @@ public class HttpEndpoint extends HttpCommonEndpoint implements LineNumberAware if (httpClient instanceof Closeable closeable) { IOHelper.close(closeable); } - ServiceHelper.stopService(httpActivityListener); + ServiceHelper.stopService(httpActivityListener, httpClientConfigurer); super.doStop(); } diff --git a/components/camel-http/src/main/java/org/apache/camel/component/http/OAuth2ClientConfigurer.java b/components/camel-http/src/main/java/org/apache/camel/component/http/OAuth2ClientConfigurer.java index 2c582f411e6..25e825641fa 100644 --- a/components/camel-http/src/main/java/org/apache/camel/component/http/OAuth2ClientConfigurer.java +++ b/components/camel-http/src/main/java/org/apache/camel/component/http/OAuth2ClientConfigurer.java @@ -16,6 +16,7 @@ */ package org.apache.camel.component.http; +import java.io.Closeable; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; @@ -24,6 +25,8 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.atomic.AtomicReference; +import org.apache.camel.support.service.ServiceSupport; +import org.apache.camel.util.IOHelper; import org.apache.camel.util.json.DeserializationException; import org.apache.camel.util.json.JsonObject; import org.apache.camel.util.json.Jsoner; @@ -39,7 +42,7 @@ import org.apache.hc.core5.http.io.entity.EntityUtils; import org.apache.hc.core5.http.io.entity.StringEntity; import org.apache.hc.core5.http.protocol.HttpContext; -public class OAuth2ClientConfigurer implements HttpClientConfigurer { +public class OAuth2ClientConfigurer extends ServiceSupport implements HttpClientConfigurer { private final String clientId; private final String clientSecret; @@ -50,6 +53,7 @@ public class OAuth2ClientConfigurer implements HttpClientConfigurer { private final Long cachedTokensExpirationMarginSeconds; private final static ConcurrentMap<OAuth2URIAndCredentials, TokenCache> tokenCache = new ConcurrentHashMap<>(); private final String resourceIndicator; + private HttpClient httpClient; public OAuth2ClientConfigurer(String clientId, String clientSecret, String tokenEndpoint, String resourceIndicator, String scope, boolean cacheTokens, @@ -66,7 +70,9 @@ public class OAuth2ClientConfigurer implements HttpClientConfigurer { @Override public void configureHttpClient(HttpClientBuilder clientBuilder) { - HttpClient httpClient = clientBuilder.build(); + // create a new http client only used for oauth token requests + this.httpClient = clientBuilder.build(); + clientBuilder.addRequestInterceptorFirst((HttpRequest request, EntityDetails entity, HttpContext context) -> { URI requestUri = getUriFromRequest(request); OAuth2URIAndCredentials uriAndCredentials = new OAuth2URIAndCredentials(requestUri, clientId, clientSecret); @@ -191,4 +197,12 @@ public class OAuth2ClientConfigurer implements HttpClientConfigurer { private record OAuth2URIAndCredentials(URI uri, String clientId, String clientSecret) { } + @Override + protected void doStop() throws Exception { + super.doStop(); + if (httpClient instanceof Closeable closeable) { + IOHelper.close(closeable); + httpClient = null; + } + } }