-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rest Client Reactive: globally register providers annotated with @Pro…
- Loading branch information
1 parent
8df4cbb
commit dd69a26
Showing
13 changed files
with
357 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
...eployment/src/test/java/io/quarkus/rest/client/reactive/provider/GlobalRequestFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package io.quarkus.rest.client.reactive.provider; | ||
|
||
import javax.ws.rs.core.Response; | ||
import javax.ws.rs.ext.Provider; | ||
|
||
import org.jboss.resteasy.reactive.client.spi.ResteasyReactiveClientRequestContext; | ||
import org.jboss.resteasy.reactive.client.spi.ResteasyReactiveClientRequestFilter; | ||
|
||
@Provider | ||
public class GlobalRequestFilter implements ResteasyReactiveClientRequestFilter { | ||
public static final int STATUS = 233; | ||
|
||
public static boolean abort = false; | ||
|
||
@Override | ||
public void filter(ResteasyReactiveClientRequestContext requestContext) { | ||
if (abort) { | ||
requestContext.abortWith(Response.status(STATUS).build()); | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...java/io/quarkus/rest/client/reactive/provider/GlobalRequestFilterConstrainedToServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package io.quarkus.rest.client.reactive.provider; | ||
|
||
import javax.ws.rs.ConstrainedTo; | ||
import javax.ws.rs.RuntimeType; | ||
import javax.ws.rs.ext.Provider; | ||
|
||
import org.jboss.resteasy.reactive.client.spi.ResteasyReactiveClientRequestContext; | ||
import org.jboss.resteasy.reactive.client.spi.ResteasyReactiveClientRequestFilter; | ||
|
||
@Provider | ||
@ConstrainedTo(RuntimeType.SERVER) | ||
public class GlobalRequestFilterConstrainedToServer implements ResteasyReactiveClientRequestFilter { | ||
@Override | ||
public void filter(ResteasyReactiveClientRequestContext requestContext) { | ||
throw new RuntimeException("Invoked filter that is constrained to server"); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...ployment/src/test/java/io/quarkus/rest/client/reactive/provider/GlobalResponseFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.quarkus.rest.client.reactive.provider; | ||
|
||
import javax.annotation.Priority; | ||
import javax.ws.rs.client.ClientResponseContext; | ||
import javax.ws.rs.ext.Provider; | ||
|
||
import org.jboss.resteasy.reactive.client.spi.ResteasyReactiveClientRequestContext; | ||
import org.jboss.resteasy.reactive.client.spi.ResteasyReactiveClientResponseFilter; | ||
|
||
@Provider | ||
@Priority(20) | ||
public class GlobalResponseFilter implements ResteasyReactiveClientResponseFilter { | ||
|
||
public static final int STATUS = 222; | ||
|
||
@Override | ||
public void filter(ResteasyReactiveClientRequestContext requestContext, ClientResponseContext responseContext) { | ||
if (responseContext.getStatus() != GlobalRequestFilter.STATUS) { | ||
responseContext.setStatus(STATUS); | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...t/src/test/java/io/quarkus/rest/client/reactive/provider/GlobalResponseFilterLowPrio.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package io.quarkus.rest.client.reactive.provider; | ||
|
||
import javax.annotation.Priority; | ||
import javax.ws.rs.client.ClientResponseContext; | ||
import javax.ws.rs.ext.Provider; | ||
|
||
import org.jboss.resteasy.reactive.client.spi.ResteasyReactiveClientRequestContext; | ||
import org.jboss.resteasy.reactive.client.spi.ResteasyReactiveClientResponseFilter; | ||
|
||
@Priority(10) // lower prio here means executed later | ||
@Provider | ||
public class GlobalResponseFilterLowPrio implements ResteasyReactiveClientResponseFilter { | ||
|
||
public static final int STATUS = 244; | ||
public static boolean skip = false; | ||
|
||
@Override | ||
public void filter(ResteasyReactiveClientRequestContext requestContext, ClientResponseContext responseContext) { | ||
if (!skip) { | ||
responseContext.setStatus(STATUS); | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...active/deployment/src/test/java/io/quarkus/rest/client/reactive/provider/HelloClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package io.quarkus.rest.client.reactive.provider; | ||
|
||
import javax.ws.rs.Consumes; | ||
import javax.ws.rs.POST; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.Response; | ||
|
||
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient; | ||
|
||
@Path("/hello") | ||
@Produces(MediaType.TEXT_PLAIN) | ||
@Consumes(MediaType.TEXT_PLAIN) | ||
@RegisterRestClient | ||
public interface HelloClient { | ||
@POST | ||
Response echo(String name); | ||
} |
21 changes: 21 additions & 0 deletions
21
...loyment/src/test/java/io/quarkus/rest/client/reactive/provider/HelloClientWithFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package io.quarkus.rest.client.reactive.provider; | ||
|
||
import javax.ws.rs.Consumes; | ||
import javax.ws.rs.POST; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.Response; | ||
|
||
import org.eclipse.microprofile.rest.client.annotation.RegisterProvider; | ||
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient; | ||
|
||
@Path("/hello") | ||
@Produces(MediaType.TEXT_PLAIN) | ||
@Consumes(MediaType.TEXT_PLAIN) | ||
@RegisterProvider(ResponseFilterLowestPrio.class) | ||
@RegisterRestClient | ||
public interface HelloClientWithFilter { | ||
@POST | ||
Response echo(String name); | ||
} |
64 changes: 64 additions & 0 deletions
64
...ployment/src/test/java/io/quarkus/rest/client/reactive/provider/ProviderPriorityTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package io.quarkus.rest.client.reactive.provider; | ||
|
||
import static io.quarkus.rest.client.reactive.RestClientTestUtil.setUrlForClass; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import javax.ws.rs.core.Response; | ||
|
||
import org.eclipse.microprofile.rest.client.inject.RestClient; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.rest.client.reactive.HelloResource; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class ProviderPriorityTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) | ||
.addClasses(HelloResource.class, | ||
HelloClient.class, | ||
HelloClientWithFilter.class, | ||
ResponseFilterLowestPrio.class, | ||
GlobalResponseFilter.class, | ||
GlobalResponseFilterLowPrio.class) | ||
.addAsResource( | ||
new StringAsset(setUrlForClass(HelloClient.class) | ||
+ setUrlForClass(HelloClientWithFilter.class)), | ||
"application.properties")); | ||
|
||
@RestClient | ||
HelloClient helloClient; | ||
|
||
@RestClient | ||
HelloClientWithFilter helloClientWithFilter; | ||
|
||
@AfterEach | ||
void cleanUp() { | ||
GlobalResponseFilterLowPrio.skip = false; | ||
} | ||
|
||
@Test | ||
void shouldApplyLocalLowestPrioFilterLast() { | ||
Response response = helloClientWithFilter.echo("foo"); | ||
assertThat(response.getStatus()).isEqualTo(ResponseFilterLowestPrio.STATUS); | ||
} | ||
|
||
@Test | ||
void shouldApplyLowPrioFilterLast() { | ||
Response response = helloClient.echo("foo"); | ||
assertThat(response.getStatus()).isEqualTo(GlobalResponseFilterLowPrio.STATUS); | ||
} | ||
|
||
@Test | ||
void shouldApplyHighPrioFilter() { | ||
GlobalResponseFilterLowPrio.skip = true; | ||
Response response = helloClient.echo("foo"); | ||
assertThat(response.getStatus()).isEqualTo(GlobalResponseFilter.STATUS); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
...ctive/deployment/src/test/java/io/quarkus/rest/client/reactive/provider/ProviderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package io.quarkus.rest.client.reactive.provider; | ||
|
||
import static io.quarkus.rest.client.reactive.RestClientTestUtil.setUrlForClass; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.net.URI; | ||
|
||
import javax.ws.rs.core.Response; | ||
|
||
import org.eclipse.microprofile.rest.client.RestClientBuilder; | ||
import org.eclipse.microprofile.rest.client.inject.RestClient; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.rest.client.reactive.HelloResource; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.quarkus.test.common.http.TestHTTPResource; | ||
|
||
public class ProviderTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) | ||
.addClasses(HelloResource.class, HelloClient.class, GlobalRequestFilter.class, | ||
GlobalResponseFilter.class, GlobalRequestFilterConstrainedToServer.class) | ||
.addAsResource( | ||
new StringAsset(setUrlForClass(HelloClient.class)), | ||
"application.properties")); | ||
|
||
@RestClient | ||
HelloClient helloClient; | ||
|
||
@TestHTTPResource | ||
URI baseUri; | ||
|
||
@AfterEach | ||
public void cleanUp() { | ||
GlobalRequestFilter.abort = false; | ||
} | ||
|
||
@Test | ||
void shouldUseGlobalRequestFilterForInjectedClient() { | ||
GlobalRequestFilter.abort = true; | ||
Response response = helloClient.echo("Michał"); | ||
assertThat(response.getStatus()).isEqualTo(GlobalRequestFilter.STATUS); | ||
} | ||
|
||
@Test | ||
void shouldUseGlobalResponseFilterForInjectedClient() { | ||
Response response = helloClient.echo("Michał"); | ||
assertThat(response.getStatus()).isEqualTo(GlobalResponseFilter.STATUS); | ||
} | ||
|
||
@Test | ||
void shouldUseGlobalRequestFilterForBuiltClient() { | ||
GlobalRequestFilter.abort = true; | ||
Response response = helloClient().echo("Michał"); | ||
assertThat(response.getStatus()).isEqualTo(GlobalRequestFilter.STATUS); | ||
} | ||
|
||
@Test | ||
void shouldUseGlobalResponseFilterForBuiltClient() { | ||
Response response = helloClient().echo("Michał"); | ||
assertThat(response.getStatus()).isEqualTo(GlobalResponseFilter.STATUS); | ||
} | ||
|
||
private HelloClient helloClient() { | ||
return RestClientBuilder.newBuilder() | ||
.baseUri(baseUri) | ||
.build(HelloClient.class); | ||
} | ||
} |
Oops, something went wrong.