From ec4e26752d9c75b6a3187fb21dbd62539487d897 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Szynkiewicz?= Date: Mon, 28 Feb 2022 09:41:42 +0100 Subject: [PATCH] Allow overriding user agent for REST Client Reactive fixes #23966 --- .../client/reactive/headers/UserAgentTest.java | 10 ++++++++++ .../client/impl/InvocationBuilderImpl.java | 14 +++++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/extensions/resteasy-reactive/rest-client-reactive/deployment/src/test/java/io/quarkus/rest/client/reactive/headers/UserAgentTest.java b/extensions/resteasy-reactive/rest-client-reactive/deployment/src/test/java/io/quarkus/rest/client/reactive/headers/UserAgentTest.java index 3e2c924ac9a05..d5341d80e3323 100644 --- a/extensions/resteasy-reactive/rest-client-reactive/deployment/src/test/java/io/quarkus/rest/client/reactive/headers/UserAgentTest.java +++ b/extensions/resteasy-reactive/rest-client-reactive/deployment/src/test/java/io/quarkus/rest/client/reactive/headers/UserAgentTest.java @@ -30,6 +30,12 @@ void testHeadersWithSubresource() { assertThat(client.call()).isEqualTo("Resteasy Reactive Client"); } + @Test + void testHeaderOverride() { + Client client = RestClientBuilder.newBuilder().baseUri(baseUri).build(Client.class); + assertThat(client.callWithUserAgent("custom-agent")).isEqualTo("custom-agent"); + } + @Path("/") @ApplicationScoped public static class Resource { @@ -44,6 +50,10 @@ public interface Client { @Path("/") @GET String call(); + + @Path("/") + @GET + String callWithUserAgent(@HeaderParam("User-AgenT") String userAgent); } } diff --git a/independent-projects/resteasy-reactive/client/runtime/src/main/java/org/jboss/resteasy/reactive/client/impl/InvocationBuilderImpl.java b/independent-projects/resteasy-reactive/client/runtime/src/main/java/org/jboss/resteasy/reactive/client/impl/InvocationBuilderImpl.java index 5126b4ab0c428..f9f25cf593cd0 100644 --- a/independent-projects/resteasy-reactive/client/runtime/src/main/java/org/jboss/resteasy/reactive/client/impl/InvocationBuilderImpl.java +++ b/independent-projects/resteasy-reactive/client/runtime/src/main/java/org/jboss/resteasy/reactive/client/impl/InvocationBuilderImpl.java @@ -51,9 +51,6 @@ public InvocationBuilderImpl(URI uri, ClientImpl restClient, HttpClient httpClie this.httpClient = httpClient; this.target = target; this.requestSpec = new RequestSpec(configuration); - if (restClient.getUserAgent() != null && !restClient.getUserAgent().isEmpty()) { - this.requestSpec.headers.header(HttpHeaders.USER_AGENT, restClient.getUserAgent()); - } this.configuration = configuration; this.handlerChain = handlerChain; this.requestContext = requestContext; @@ -67,11 +64,13 @@ public InvocationBuilderImpl(URI uri, ClientImpl restClient, HttpClient httpClie @Override public Invocation build(String method) { + setUserAgentIfNotSet(); return new InvocationImpl(method, async(), null); } @Override public Invocation build(String method, Entity entity) { + setUserAgentIfNotSet(); return new InvocationImpl(method, async(), entity); } @@ -97,6 +96,7 @@ public Invocation buildPut(Entity entity) { @Override public AsyncInvokerImpl async() { + setUserAgentIfNotSet(); return new AsyncInvokerImpl(restClient, httpClient, uri, requestSpec, configuration, properties, handlerChain, requestContext); } @@ -175,6 +175,7 @@ public CompletionStageRxInvoker rx() { @Override public T rx(Class clazz) { + setUserAgentIfNotSet(); if (clazz == MultiInvoker.class) { return (T) new MultiInvoker(this); } else if (clazz == UniInvoker.class) { @@ -189,6 +190,13 @@ public T rx(Class clazz) { return null; } + private void setUserAgentIfNotSet() { + if (!requestSpec.headers.getHeaders().containsKey(HttpHeaders.USER_AGENT) + && restClient.getUserAgent() != null && !restClient.getUserAgent().isEmpty()) { + this.requestSpec.headers.header(HttpHeaders.USER_AGENT, restClient.getUserAgent()); + } + } + @Override public Response get() { return unwrap(async().get());