Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add user-agent to resteasy reactive client #22228

Merged
merged 1 commit into from
Dec 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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