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

Fix providers not auto registered for JAX-RS Client #11448

Merged
merged 1 commit into from
Aug 27, 2020
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,123 @@
package io.quarkus.restclient.registerprovider;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.net.InetSocketAddress;

import javax.annotation.PostConstruct;
import javax.annotation.Priority;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.ws.rs.Priorities;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.ClientRequestContext;
import javax.ws.rs.client.ClientRequestFilter;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.ext.Provider;

import org.jboss.resteasy.specimpl.ResteasyUriInfo;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import com.sun.net.httpserver.HttpServer;

import io.quarkus.test.QuarkusUnitTest;

public class ProviderClientRegistrationTest {
@RegisterExtension
static QuarkusUnitTest test = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClasses(ProviderClientRegistrationTest.class, ClientBean.class, HelloFooProvider.class));

static HttpServer server;

@Inject
ClientBean client;

@BeforeAll
static void beforeAll() throws Exception {
// This Server is required, so we don't start a REST Application and test that we are able to add Providers
// without a class annotated with @Path, to a Client: https://github.com/quarkusio/quarkus/issues/11424
server = HttpServer.create(new InetSocketAddress(0), 0);
server.createContext("/hello", exchange -> {
ResteasyUriInfo uriInfo = new ResteasyUriInfo(exchange.getRequestURI());
String who = uriInfo.getQueryParameters().getFirst("name");
String response = "Hello ";
if (who == null) {
response = response + "Undefined";
} else {
response = response + who;
}

exchange.sendResponseHeaders(200, response.length());
exchange.getResponseHeaders().add(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN);
exchange.getResponseBody().write(response.getBytes());
exchange.getResponseBody().flush();
exchange.getResponseBody().close();
});

server.start();
}

@AfterAll
static void afterAll() {
server.stop(0);
}

@Test
void hello() {
assertEquals("Hello Naruto", client.hello("Naruto"));
}

@Test
void helloFoo() {
assertEquals("Hello Foo", client.hello());
}

// The Client needs to be deployed with the Quarkus app, so it inherits all of the RESTEasy configuration.
@ApplicationScoped
public static class ClientBean {
private WebTarget webTarget;

@PostConstruct
void init() {
webTarget = ClientBuilder.newClient().target("http://localhost:" + server.getAddress().getPort()).path("/hello");
;
}

String hello() {
return hello(null);
}

String hello(final String name) {
WebTarget webTarget = this.webTarget;
if (name != null) {
webTarget = webTarget.queryParam("name", name);
}

Response response = webTarget.request(MediaType.TEXT_PLAIN_TYPE).get(Response.class);

assertEquals(200, response.getStatus());
return response.readEntity(String.class);
}
}

@Provider
@Priority(Priorities.USER - 1)
public static class HelloFooProvider implements ClientRequestFilter {
@Override
public void filter(final ClientRequestContext requestContext) {
if (requestContext.getUri().getQuery() == null) {
requestContext.setUri(UriBuilder.fromUri(requestContext.getUri()).queryParam("name", "Foo").build());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public InjectorFactory getInjectorFactory() {
}

RestClientBuilderImpl.setProviderFactory(clientProviderFactory);
ResteasyProviderFactory.setInstance(clientProviderFactory);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't conflict with the server behavior when both are used? /cc @kenfinnigan

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. When the server starts (if any) it will replace the instance:

https://github.com/resteasy/Resteasy/blob/1570f6825de1fde1ea3c3a950dcb6297e3c9bd4f/resteasy-core/src/main/java/org/jboss/resteasy/core/ResteasyDeploymentImpl.java#L377

And it will work for both server and client with the right factory.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the ResteasyDeploymentImpl used in Quarkus, for some reason I'm thinking it isn't, but could easily be wrong?

If we do use it, then I think it should work fine. But certainly worth verifying

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for checking

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Famous Last Words™

providerFactory = clientProviderFactory;
}

Expand Down