This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch camel-4.10.x in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-4.10.x by this push: new 0aab1c0eebb CAMEL-21847: camel-http should close http client for oauth token requests when stopping 0aab1c0eebb is described below commit 0aab1c0eebb9e0c9e0029295d5ab2ade5dfd0bd3 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 | 19 +++++++++++++++++-- 3 files changed, 33 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 ee29c73b8c6..3e360f9afdc 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.HashMap; import java.util.Map; 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; @@ -49,6 +52,7 @@ public class OAuth2ClientConfigurer implements HttpClientConfigurer { private final Long cachedTokensDefaultExpirySeconds; private final Long cachedTokensExpirationMarginSeconds; private final static Map<OAuth2URIAndCredentials, TokenCache> tokenCache = new HashMap<>(); + private HttpClient httpClient; public OAuth2ClientConfigurer(String clientId, String clientSecret, String tokenEndpoint, String scope, boolean cacheTokens, long cachedTokensDefaultExpirySeconds, long cachedTokensExpirationMarginSeconds) { @@ -63,7 +67,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); @@ -91,6 +97,7 @@ public class OAuth2ClientConfigurer implements HttpClientConfigurer { } private JsonObject getAccessTokenResponse(HttpClient httpClient) throws IOException { + String bodyStr = "grant_type=client_credentials"; String url = tokenEndpoint; if (scope != null) { String sep = "?"; @@ -184,4 +191,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; + } + } }