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

Allow overriding user agent for REST Client Reactive #24002

Merged
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
Expand Up @@ -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 {
Expand All @@ -44,6 +50,10 @@ public interface Client {
@Path("/")
@GET
String call();

@Path("/")
@GET
String callWithUserAgent(@HeaderParam("User-AgenT") String userAgent);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

Expand All @@ -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);
}
Expand Down Expand Up @@ -175,6 +175,7 @@ public CompletionStageRxInvoker rx() {

@Override
public <T extends RxInvoker> T rx(Class<T> clazz) {
setUserAgentIfNotSet();
if (clazz == MultiInvoker.class) {
return (T) new MultiInvoker(this);
} else if (clazz == UniInvoker.class) {
Expand All @@ -189,6 +190,13 @@ public <T extends RxInvoker> T rx(Class<T> 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());
Expand Down