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 new file mode 100644 index 0000000000000..3e2c924ac9a05 --- /dev/null +++ b/extensions/resteasy-reactive/rest-client-reactive/deployment/src/test/java/io/quarkus/rest/client/reactive/headers/UserAgentTest.java @@ -0,0 +1,49 @@ +package io.quarkus.rest.client.reactive.headers; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.net.URI; + +import javax.enterprise.context.ApplicationScoped; +import javax.ws.rs.GET; +import javax.ws.rs.HeaderParam; +import javax.ws.rs.Path; + +import org.eclipse.microprofile.rest.client.RestClientBuilder; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.test.QuarkusUnitTest; +import io.quarkus.test.common.http.TestHTTPResource; + +public class UserAgentTest { + @RegisterExtension + static final QuarkusUnitTest config = new QuarkusUnitTest() + .withApplicationRoot((jar) -> jar.addClasses(Resource.class)); + + @TestHTTPResource + URI baseUri; + + @Test + void testHeadersWithSubresource() { + Client client = RestClientBuilder.newBuilder().baseUri(baseUri).build(Client.class); + assertThat(client.call()).isEqualTo("Resteasy Reactive Client"); + } + + @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/independent-projects/resteasy-reactive/client/runtime/src/main/java/org/jboss/resteasy/reactive/client/impl/ClientBuilderImpl.java b/independent-projects/resteasy-reactive/client/runtime/src/main/java/org/jboss/resteasy/reactive/client/impl/ClientBuilderImpl.java index 286d3884c3178..bbbfb827d3bc1 100644 --- a/independent-projects/resteasy-reactive/client/runtime/src/main/java/org/jboss/resteasy/reactive/client/impl/ClientBuilderImpl.java +++ b/independent-projects/resteasy-reactive/client/runtime/src/main/java/org/jboss/resteasy/reactive/client/impl/ClientBuilderImpl.java @@ -53,6 +53,7 @@ public class ClientBuilderImpl extends ClientBuilder { private HttpClientOptions httpClientOptions = new HttpClientOptions(); private ClientLogger clientLogger = new DefaultClientLogger(); + private String userAgent = "Resteasy Reactive Client"; @Override public ClientBuilder withConfig(Configuration config) { @@ -188,7 +189,7 @@ public ClientImpl build() { followRedirects, multiQueryParamMode, loggingScope, - clientLogger); + clientLogger, userAgent); } @@ -268,4 +269,9 @@ public ClientBuilderImpl trustAll(boolean trustAll) { this.trustAll = trustAll; return this; } + + public ClientBuilderImpl setUserAgent(String userAgent) { + this.userAgent = userAgent; + return this; + } } diff --git a/independent-projects/resteasy-reactive/client/runtime/src/main/java/org/jboss/resteasy/reactive/client/impl/ClientImpl.java b/independent-projects/resteasy-reactive/client/runtime/src/main/java/org/jboss/resteasy/reactive/client/impl/ClientImpl.java index 8be643580b3ee..8a02f9a0ecdd4 100644 --- a/independent-projects/resteasy-reactive/client/runtime/src/main/java/org/jboss/resteasy/reactive/client/impl/ClientImpl.java +++ b/independent-projects/resteasy-reactive/client/runtime/src/main/java/org/jboss/resteasy/reactive/client/impl/ClientImpl.java @@ -84,13 +84,15 @@ public class ClientImpl implements Client { final HandlerChain handlerChain; final Vertx vertx; private final MultiQueryParamMode multiQueryParamMode; + private final String userAgent; public ClientImpl(HttpClientOptions options, ConfigurationImpl configuration, ClientContext clientContext, HostnameVerifier hostnameVerifier, SSLContext sslContext, boolean followRedirects, MultiQueryParamMode multiQueryParamMode, LoggingScope loggingScope, - ClientLogger clientLogger) { + ClientLogger clientLogger, String userAgent) { + this.userAgent = userAgent; configuration = configuration != null ? configuration : new ConfigurationImpl(RuntimeType.CLIENT); // TODO: ssl context // TODO: hostnameVerifier @@ -174,6 +176,10 @@ void abortIfClosed() { throw new IllegalStateException("Client is closed"); } + public String getUserAgent() { + return userAgent; + } + @Override public WebTarget target(String uri) { // close is checked in the other target call 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 5d34b20c78eeb..5126b4ab0c428 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 @@ -20,6 +20,7 @@ import javax.ws.rs.core.CacheControl; import javax.ws.rs.core.Cookie; import javax.ws.rs.core.GenericType; +import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.Response; @@ -50,6 +51,9 @@ 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;