From 60c682a1952af8dc0fa591629c3097819f6963f2 Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Thu, 15 Feb 2024 09:30:04 +0200 Subject: [PATCH] Allow setting the user agent programmatically --- .../headers/UserAgentProgrammaticTest.java | 56 +++++++++++++++++++ .../reactive/QuarkusRestClientBuilder.java | 5 ++ .../runtime/QuarkusRestClientBuilderImpl.java | 6 ++ .../runtime/RestClientBuilderImpl.java | 14 +++-- .../runtime/RestClientCDIDelegateBuilder.java | 2 +- .../RestClientCDIDelegateBuilderTest.java | 4 +- 6 files changed, 80 insertions(+), 7 deletions(-) create mode 100644 extensions/resteasy-reactive/rest-client-reactive/deployment/src/test/java/io/quarkus/rest/client/reactive/headers/UserAgentProgrammaticTest.java diff --git a/extensions/resteasy-reactive/rest-client-reactive/deployment/src/test/java/io/quarkus/rest/client/reactive/headers/UserAgentProgrammaticTest.java b/extensions/resteasy-reactive/rest-client-reactive/deployment/src/test/java/io/quarkus/rest/client/reactive/headers/UserAgentProgrammaticTest.java new file mode 100644 index 0000000000000..10af2eb4a39ec --- /dev/null +++ b/extensions/resteasy-reactive/rest-client-reactive/deployment/src/test/java/io/quarkus/rest/client/reactive/headers/UserAgentProgrammaticTest.java @@ -0,0 +1,56 @@ +package io.quarkus.rest.client.reactive.headers; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.net.URI; + +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.HeaderParam; +import jakarta.ws.rs.Path; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.rest.client.reactive.QuarkusRestClientBuilder; +import io.quarkus.test.QuarkusUnitTest; +import io.quarkus.test.common.http.TestHTTPResource; + +public class UserAgentProgrammaticTest { + @RegisterExtension + static final QuarkusUnitTest config = new QuarkusUnitTest() + .withApplicationRoot((jar) -> jar.addClasses(Resource.class, Client.class)) + .overrideRuntimeConfigKey("quarkus.rest-client.user-agent", "from-config"); + + @TestHTTPResource + URI baseUri; + + @Test + void test() { + Client client = QuarkusRestClientBuilder.newBuilder().baseUri(baseUri).userAgent("programmatic").build(Client.class); + assertThat(client.call()).isEqualTo("programmatic"); + + Client client2 = QuarkusRestClientBuilder.newBuilder().baseUri(baseUri).userAgent("programmatic2").build(Client.class); + assertThat(client2.call()).isEqualTo("programmatic2"); + + Client client3 = QuarkusRestClientBuilder.newBuilder().baseUri(baseUri).build(Client.class); + assertThat(client3.call()).isEqualTo("from-config"); + } + + @Path("/") + @ApplicationScoped + public static class Resource { + @GET + public String returnHeaders(@HeaderParam("user-agent") String header) { + return header; + } + } + + public interface Client { + + @Path("/") + @GET + String call(); + } + +} diff --git a/extensions/resteasy-reactive/rest-client-reactive/runtime/src/main/java/io/quarkus/rest/client/reactive/QuarkusRestClientBuilder.java b/extensions/resteasy-reactive/rest-client-reactive/runtime/src/main/java/io/quarkus/rest/client/reactive/QuarkusRestClientBuilder.java index d6563019c8bc1..a7c66165bea7f 100644 --- a/extensions/resteasy-reactive/rest-client-reactive/runtime/src/main/java/io/quarkus/rest/client/reactive/QuarkusRestClientBuilder.java +++ b/extensions/resteasy-reactive/rest-client-reactive/runtime/src/main/java/io/quarkus/rest/client/reactive/QuarkusRestClientBuilder.java @@ -279,6 +279,11 @@ static QuarkusRestClientBuilder newBuilder() { */ QuarkusRestClientBuilder trustAll(boolean trustAll); + /** + * Set the User-Agent header to be used + */ + QuarkusRestClientBuilder userAgent(String userAgent); + /** * Based on the configured QuarkusRestClientBuilder, creates a new instance of the given REST interface to invoke API calls * against. diff --git a/extensions/resteasy-reactive/rest-client-reactive/runtime/src/main/java/io/quarkus/rest/client/reactive/runtime/QuarkusRestClientBuilderImpl.java b/extensions/resteasy-reactive/rest-client-reactive/runtime/src/main/java/io/quarkus/rest/client/reactive/runtime/QuarkusRestClientBuilderImpl.java index 87424d184edd0..3ba9ad115f961 100644 --- a/extensions/resteasy-reactive/rest-client-reactive/runtime/src/main/java/io/quarkus/rest/client/reactive/runtime/QuarkusRestClientBuilderImpl.java +++ b/extensions/resteasy-reactive/rest-client-reactive/runtime/src/main/java/io/quarkus/rest/client/reactive/runtime/QuarkusRestClientBuilderImpl.java @@ -243,6 +243,12 @@ public QuarkusRestClientBuilder trustAll(boolean trustAll) { return this; } + @Override + public QuarkusRestClientBuilder userAgent(String userAgent) { + proxy.userAgent(userAgent); + return this; + } + @Override public T build(Class clazz) throws IllegalStateException, RestClientDefinitionException { return proxy.build(clazz); diff --git a/extensions/resteasy-reactive/rest-client-reactive/runtime/src/main/java/io/quarkus/rest/client/reactive/runtime/RestClientBuilderImpl.java b/extensions/resteasy-reactive/rest-client-reactive/runtime/src/main/java/io/quarkus/rest/client/reactive/runtime/RestClientBuilderImpl.java index cfd364b936e1f..3920c8db594c1 100644 --- a/extensions/resteasy-reactive/rest-client-reactive/runtime/src/main/java/io/quarkus/rest/client/reactive/runtime/RestClientBuilderImpl.java +++ b/extensions/resteasy-reactive/rest-client-reactive/runtime/src/main/java/io/quarkus/rest/client/reactive/runtime/RestClientBuilderImpl.java @@ -74,6 +74,7 @@ public class RestClientBuilderImpl implements RestClientBuilder { private Integer loggingBodyLimit; private Boolean trustAll; + private String userAgent; @Override public RestClientBuilderImpl baseUrl(URL url) { @@ -186,6 +187,11 @@ public RestClientBuilderImpl trustAll(boolean trustAll) { return this; } + public RestClientBuilderImpl userAgent(String userAgent) { + this.userAgent = userAgent; + return this; + } + @Override public RestClientBuilderImpl executorService(ExecutorService executor) { throw new IllegalArgumentException("Specifying executor service is not supported. " + @@ -386,10 +392,10 @@ public T build(Class aClass) throws IllegalStateException, RestClientDefi clientBuilder.trustAll(effectiveTrustAll); restClientsConfig.verifyHost.ifPresent(clientBuilder::verifyHost); - String userAgent = (String) getConfiguration().getProperty(QuarkusRestClientProperties.USER_AGENT); - if (userAgent != null) { - clientBuilder.setUserAgent(userAgent); - } else if (restClientsConfig.userAgent.isPresent()) { + String effectiveUserAgent = userAgent; + if (effectiveUserAgent != null) { + clientBuilder.setUserAgent(effectiveUserAgent); + } else if (restClientsConfig.userAgent.isPresent()) { // if config set and client obtained programmatically clientBuilder.setUserAgent(restClientsConfig.userAgent.get()); } diff --git a/extensions/resteasy-reactive/rest-client-reactive/runtime/src/main/java/io/quarkus/rest/client/reactive/runtime/RestClientCDIDelegateBuilder.java b/extensions/resteasy-reactive/rest-client-reactive/runtime/src/main/java/io/quarkus/rest/client/reactive/runtime/RestClientCDIDelegateBuilder.java index 4d7e34d0815f4..1424900d8a16c 100644 --- a/extensions/resteasy-reactive/rest-client-reactive/runtime/src/main/java/io/quarkus/rest/client/reactive/runtime/RestClientCDIDelegateBuilder.java +++ b/extensions/resteasy-reactive/rest-client-reactive/runtime/src/main/java/io/quarkus/rest/client/reactive/runtime/RestClientCDIDelegateBuilder.java @@ -121,7 +121,7 @@ private void configureCustomProperties(QuarkusRestClientBuilder builder) { Optional userAgent = oneOf(clientConfigByClassName().userAgent, clientConfigByConfigKey().userAgent, configRoot.userAgent); if (userAgent.isPresent()) { - builder.property(QuarkusRestClientProperties.USER_AGENT, userAgent.get()); + builder.userAgent(userAgent.get()); } Optional maxChunkSize = oneOf( diff --git a/extensions/resteasy-reactive/rest-client-reactive/runtime/src/test/java/io/quarkus/rest/client/reactive/runtime/RestClientCDIDelegateBuilderTest.java b/extensions/resteasy-reactive/rest-client-reactive/runtime/src/test/java/io/quarkus/rest/client/reactive/runtime/RestClientCDIDelegateBuilderTest.java index b107b47ef0ccc..9ba83692374bf 100644 --- a/extensions/resteasy-reactive/rest-client-reactive/runtime/src/test/java/io/quarkus/rest/client/reactive/runtime/RestClientCDIDelegateBuilderTest.java +++ b/extensions/resteasy-reactive/rest-client-reactive/runtime/src/test/java/io/quarkus/rest/client/reactive/runtime/RestClientCDIDelegateBuilderTest.java @@ -109,7 +109,7 @@ public void testClientSpecificConfigs() { Mockito.verify(restClientBuilderMock).nonProxyHosts("nonProxyHosts1"); Mockito.verify(restClientBuilderMock).connectTimeout(100, TimeUnit.MILLISECONDS); Mockito.verify(restClientBuilderMock).readTimeout(101, TimeUnit.MILLISECONDS); - Mockito.verify(restClientBuilderMock).property(QuarkusRestClientProperties.USER_AGENT, "agent1"); + Mockito.verify(restClientBuilderMock).userAgent("agent1"); Mockito.verify(restClientBuilderMock).property(QuarkusRestClientProperties.STATIC_HEADERS, Collections.singletonMap("header1", "value")); Mockito.verify(restClientBuilderMock).property(QuarkusRestClientProperties.CONNECTION_TTL, 10); // value converted to seconds @@ -152,7 +152,7 @@ public void testGlobalConfigs() { Mockito.verify(restClientBuilderMock).nonProxyHosts("nonProxyHosts2"); Mockito.verify(restClientBuilderMock).connectTimeout(200, TimeUnit.MILLISECONDS); Mockito.verify(restClientBuilderMock).readTimeout(201, TimeUnit.MILLISECONDS); - Mockito.verify(restClientBuilderMock).property(QuarkusRestClientProperties.USER_AGENT, "agent2"); + Mockito.verify(restClientBuilderMock).userAgent("agent2"); Mockito.verify(restClientBuilderMock).property(QuarkusRestClientProperties.STATIC_HEADERS, Collections.singletonMap("header2", "value")); Mockito.verify(restClientBuilderMock).property(QuarkusRestClientProperties.CONNECTION_TTL, 20);