Skip to content

Commit

Permalink
Merge pull request quarkusio#22228 from stuartwdouglas/22214
Browse files Browse the repository at this point in the history
Add user-agent to resteasy reactive client
  • Loading branch information
geoand authored Dec 16, 2021
2 parents 12bfcd9 + f4413a0 commit 8865895
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -188,7 +189,7 @@ public ClientImpl build() {
followRedirects,
multiQueryParamMode,
loggingScope,
clientLogger);
clientLogger, userAgent);

}

Expand Down Expand Up @@ -268,4 +269,9 @@ public ClientBuilderImpl trustAll(boolean trustAll) {
this.trustAll = trustAll;
return this;
}

public ClientBuilderImpl setUserAgent(String userAgent) {
this.userAgent = userAgent;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 8865895

Please sign in to comment.