Skip to content

Commit

Permalink
Allow overriding user agent for REST Client Reactive
Browse files Browse the repository at this point in the history
fixes #23966
  • Loading branch information
michalszynkiewicz committed Feb 28, 2022
1 parent 4e56d41 commit ec4e267
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
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

0 comments on commit ec4e267

Please sign in to comment.