forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 3
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 - MicroProfile 2.0 features
fixes quarkusio#15970
1 parent
ffc355d
commit 186ad86
Showing
27 changed files
with
662 additions
and
342 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
10 changes: 9 additions & 1 deletion
10
...t/src/main/java/io/quarkus/jaxrs/client/reactive/deployment/beanparam/QueryParamItem.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 |
---|---|---|
@@ -1,15 +1,23 @@ | ||
package io.quarkus.jaxrs.client.reactive.deployment.beanparam; | ||
|
||
import org.jboss.jandex.Type; | ||
|
||
public class QueryParamItem extends Item { | ||
|
||
private final String name; | ||
private final Type valueType; | ||
|
||
public QueryParamItem(String name, ValueExtractor extractor) { | ||
public QueryParamItem(String name, ValueExtractor extractor, Type valueType) { | ||
super(ItemType.QUERY_PARAM, extractor); | ||
this.name = name; | ||
this.valueType = valueType; | ||
} | ||
|
||
public String name() { | ||
return name; | ||
} | ||
|
||
public Type getValueType() { | ||
return valueType; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...eactive/runtime/src/main/java/io/quarkus/jaxrs/client/reactive/runtime/ToObjectArray.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.jaxrs.client.reactive.runtime; | ||
|
||
import java.util.Collection; | ||
|
||
/** | ||
* used by query param handling mechanism, in generated code | ||
*/ | ||
@SuppressWarnings("unused") | ||
public class ToObjectArray { | ||
|
||
public static Object[] collection(Collection<?> collection) { | ||
return collection.toArray(); | ||
} | ||
|
||
public static Object[] value(Object value) { | ||
return new Object[] { value }; | ||
} | ||
|
||
private ToObjectArray() { | ||
} | ||
} |
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
50 changes: 50 additions & 0 deletions
50
...ctive/deployment/src/test/java/io/quarkus/rest/client/reactive/redirect/RedirectTest.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,50 @@ | ||
package io.quarkus.rest.client.reactive.redirect; | ||
|
||
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.jboss.resteasy.reactive.client.api.QuarkusRestClientProperties; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
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 RedirectTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest TEST = new QuarkusUnitTest() | ||
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) | ||
.addClasses(RedirectingResourceClient.class, RedirectingResource.class)); | ||
|
||
@TestHTTPResource | ||
URI uri; | ||
|
||
@Test | ||
void shouldRedirect3Times_whenMax4() { | ||
RedirectingResourceClient client = RestClientBuilder.newBuilder() | ||
.baseUri(uri) | ||
.followRedirects(true) | ||
.property(QuarkusRestClientProperties.MAX_REDIRECTS, 4) | ||
.build(RedirectingResourceClient.class); | ||
Response call = client.call(3); | ||
assertThat(call.getStatus()).isEqualTo(200); | ||
} | ||
|
||
@Test | ||
void shouldNotRedirect3Times_whenMax2() { | ||
RedirectingResourceClient client = RestClientBuilder.newBuilder() | ||
.baseUri(uri) | ||
.followRedirects(true) | ||
.property(QuarkusRestClientProperties.MAX_REDIRECTS, 2) | ||
.build(RedirectingResourceClient.class); | ||
assertThat(client.call(3).getStatus()).isEqualTo(307); | ||
|
||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...eployment/src/test/java/io/quarkus/rest/client/reactive/redirect/RedirectingResource.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.redirect; | ||
|
||
import java.net.URI; | ||
|
||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.QueryParam; | ||
import javax.ws.rs.core.Response; | ||
|
||
@Path("/redirect") | ||
public class RedirectingResource { | ||
|
||
@GET | ||
public Response redirectedResponse(@QueryParam("redirects") Integer number) { | ||
if (number == null || 0 == number) { | ||
return Response.ok().build(); | ||
} else { | ||
return Response.temporaryRedirect(URI.create("/redirect?redirects=" + (number - 1))).build(); | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...ent/src/test/java/io/quarkus/rest/client/reactive/redirect/RedirectingResourceClient.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,12 @@ | ||
package io.quarkus.rest.client.reactive.redirect; | ||
|
||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.QueryParam; | ||
import javax.ws.rs.core.Response; | ||
|
||
@Path("/redirect") | ||
public interface RedirectingResourceClient { | ||
@GET | ||
Response call(@QueryParam("redirects") Integer numberOfRedirects); | ||
} |
18 changes: 18 additions & 0 deletions
18
...st-client-reactive/runtime/src/main/java/io/quarkus/rest/client/reactive/BeanGrabber.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,18 @@ | ||
package io.quarkus.rest.client.reactive; | ||
|
||
import io.quarkus.arc.Arc; | ||
import io.quarkus.arc.InstanceHandle; | ||
|
||
@SuppressWarnings("unused") | ||
public class BeanGrabber { | ||
public static <T> T getBeanIfDefined(Class<T> beanClass) { | ||
InstanceHandle<T> instance = Arc.container().instance(beanClass); | ||
if (instance.isAvailable()) { | ||
return instance.get(); | ||
} | ||
return null; | ||
} | ||
|
||
private BeanGrabber() { | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...in/java/io/quarkus/rest/client/reactive/DefaultMicroprofileRestClientExceptionMapper.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,27 @@ | ||
package io.quarkus.rest.client.reactive; | ||
|
||
import javax.ws.rs.WebApplicationException; | ||
import javax.ws.rs.core.MultivaluedMap; | ||
import javax.ws.rs.core.Response; | ||
|
||
import org.eclipse.microprofile.rest.client.ext.ResponseExceptionMapper; | ||
|
||
public class DefaultMicroprofileRestClientExceptionMapper implements ResponseExceptionMapper { | ||
|
||
public Throwable toThrowable(Response response) { | ||
try { | ||
response.bufferEntity(); | ||
} catch (Exception var3) { | ||
} | ||
|
||
return new WebApplicationException("Unknown error, status code " + response.getStatus(), response); | ||
} | ||
|
||
public boolean handles(int status, MultivaluedMap headers) { | ||
return status >= 400; | ||
} | ||
|
||
public int getPriority() { | ||
return Integer.MAX_VALUE; | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
...me/src/main/java/io/quarkus/rest/client/reactive/MicroProfileRestClientRequestFilter.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,91 @@ | ||
package io.quarkus.rest.client.reactive; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import javax.annotation.Priority; | ||
import javax.enterprise.context.RequestScoped; | ||
import javax.ws.rs.client.ClientRequestContext; | ||
import javax.ws.rs.client.ClientRequestFilter; | ||
import javax.ws.rs.core.MultivaluedHashMap; | ||
import javax.ws.rs.core.MultivaluedMap; | ||
|
||
import io.quarkus.rest.client.reactive.runtime.HeaderContainer; | ||
import org.eclipse.microprofile.rest.client.ext.ClientHeadersFactory; | ||
import org.eclipse.microprofile.rest.client.ext.DefaultClientHeadersFactoryImpl; | ||
|
||
import io.quarkus.arc.Arc; | ||
|
||
@Priority(Integer.MIN_VALUE) | ||
public class MicroProfileRestClientRequestFilter implements ClientRequestFilter { | ||
|
||
private static final MultivaluedMap<String, String> EMPTY_MAP = new MultivaluedHashMap<>(); | ||
|
||
private final ClientHeadersFactory clientHeadersFactory; | ||
|
||
public MicroProfileRestClientRequestFilter(ClientHeadersFactory clientHeadersFactory) { | ||
this.clientHeadersFactory = clientHeadersFactory; | ||
} | ||
|
||
@Override | ||
public void filter(ClientRequestContext requestContext) { | ||
HeaderFiller headerFiller = (HeaderFiller) requestContext.getProperty(HeaderFiller.class.getName()); | ||
|
||
// mutable collection of headers | ||
MultivaluedMap<String, String> headers = new MultivaluedHashMap<>(); | ||
|
||
// gather original headers | ||
for (Map.Entry<String, List<Object>> headerEntry : requestContext.getHeaders().entrySet()) { | ||
headers.put(headerEntry.getKey(), castToListOfStrings(headerEntry.getValue())); | ||
} | ||
|
||
// add headers from MP annotations | ||
if (headerFiller != null) { | ||
// add headers to a mutable headers collection | ||
headerFiller.addHeaders(headers); | ||
} | ||
|
||
MultivaluedMap<String, String> incomingHeaders = MicroProfileRestClientRequestFilter.EMPTY_MAP; | ||
if (Arc.container().getActiveContext(RequestScoped.class) != null) { | ||
HeaderContainer headerContainer = Arc.container().instance(HeaderContainer.class).get(); | ||
if (headerContainer != null) { | ||
incomingHeaders = headerContainer.getHeaders(); | ||
} | ||
} | ||
|
||
if (clientHeadersFactory instanceof DefaultClientHeadersFactoryImpl) { | ||
// When using the default factory, pass the proposed outgoing headers onto the request context. | ||
// Propagation with the default factory will then overwrite any values if required. | ||
for (Map.Entry<String, List<String>> headerEntry : headers.entrySet()) { | ||
requestContext.getHeaders().put(headerEntry.getKey(), castToListOfObjects(headerEntry.getValue())); | ||
} | ||
} | ||
|
||
if (clientHeadersFactory != null) { | ||
incomingHeaders = clientHeadersFactory.update(incomingHeaders, headers); | ||
} | ||
|
||
for (Map.Entry<String, List<String>> headerEntry : incomingHeaders.entrySet()) { | ||
requestContext.getHeaders().put(headerEntry.getKey(), castToListOfObjects(headerEntry.getValue())); | ||
} | ||
} | ||
|
||
private static List<String> castToListOfStrings(List<Object> values) { | ||
List<String> result = new ArrayList<>(); | ||
for (Object value : values) { | ||
if (value instanceof String) { | ||
result.add((String) value); | ||
} else { | ||
result.add(String.valueOf(value)); | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private static List<Object> castToListOfObjects(List<String> values) { | ||
return (List<Object>) (List<?>) values; | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
...e/src/main/java/io/quarkus/rest/client/reactive/MicroProfileRestClientResponseFilter.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,43 @@ | ||
package io.quarkus.rest.client.reactive; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
import javax.ws.rs.ProcessingException; | ||
import javax.ws.rs.client.ClientRequestContext; | ||
import javax.ws.rs.client.ClientResponseContext; | ||
import javax.ws.rs.client.ClientResponseFilter; | ||
|
||
import org.eclipse.microprofile.rest.client.ext.ResponseExceptionMapper; | ||
import org.jboss.resteasy.reactive.client.handlers.ClientResponseRestHandler; | ||
import org.jboss.resteasy.reactive.client.impl.ClientRequestContextImpl; | ||
import org.jboss.resteasy.reactive.client.impl.ClientResponseContextImpl; | ||
import org.jboss.resteasy.reactive.common.jaxrs.ResponseImpl; | ||
|
||
public class | ||
MicroProfileRestClientResponseFilter implements ClientResponseFilter { | ||
private final List<ResponseExceptionMapper<?>> exceptionMappers; | ||
|
||
public MicroProfileRestClientResponseFilter(List<ResponseExceptionMapper<?>> exceptionMappers) { | ||
if (exceptionMappers == null) { | ||
throw new NullPointerException("exceptionMappers cannot be null"); | ||
} | ||
this.exceptionMappers = exceptionMappers; | ||
} | ||
|
||
@Override | ||
public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException { | ||
for (ResponseExceptionMapper exceptionMapper : exceptionMappers) { | ||
if (exceptionMapper.handles(responseContext.getStatus(), responseContext.getHeaders())) { | ||
// we have an exception mapper, we don't need the response anymore, we can map it to response right away (I hope :D) | ||
ResponseImpl response = ClientResponseRestHandler.mapToResponse( | ||
((ClientRequestContextImpl) requestContext).getRestClientRequestContext(), | ||
(ClientResponseContextImpl) responseContext); | ||
Throwable throwable = exceptionMapper.toThrowable(response); | ||
if (throwable != null) { | ||
throw new ProcessingException(throwable); | ||
} | ||
} | ||
} | ||
} | ||
} |
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
24 changes: 24 additions & 0 deletions
24
...t-reactive/runtime/src/main/java/io/quarkus/rest/client/reactive/RestClientListeners.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,24 @@ | ||
package io.quarkus.rest.client.reactive; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.ServiceLoader; | ||
|
||
import org.eclipse.microprofile.rest.client.spi.RestClientListener; | ||
|
||
public class RestClientListeners { | ||
|
||
private RestClientListeners() { | ||
} | ||
|
||
static Collection<RestClientListener> get() { | ||
List<RestClientListener> result = new ArrayList<>(); | ||
ServiceLoader<RestClientListener> listeners = ServiceLoader.load(RestClientListener.class, | ||
RestClientListeners.class.getClassLoader()); | ||
for (RestClientListener listener : listeners) { | ||
result.add(listener); | ||
} | ||
return result; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...active/runtime/src/main/java/io/quarkus/rest/client/reactive/runtime/BuilderResolver.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
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
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
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
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
16 changes: 16 additions & 0 deletions
16
...n/runtime/src/main/java/org/jboss/resteasy/reactive/common/jaxrs/MultiQueryParamMode.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,16 @@ | ||
package org.jboss.resteasy.reactive.common.jaxrs; | ||
|
||
public enum MultiQueryParamMode { | ||
/** | ||
* <code>foo=v1&foo=v2&foo=v3</code> | ||
*/ | ||
MULTI_PAIRS, | ||
/** | ||
* <code>foo=v1,v2,v3</code> | ||
*/ | ||
COMMA_SEPARATED, | ||
/** | ||
* <code>foo[]=v1&foo[]=v2&foo[]=v3</code> | ||
*/ | ||
ARRAY_PAIRS | ||
} |
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
92 changes: 0 additions & 92 deletions
92
...t-reactive/src/test/java/io/quarkus/tck/restclient/CustomInvokeWithJsonBProviderTest.java
This file was deleted.
Oops, something went wrong.
156 changes: 0 additions & 156 deletions
156
...t-reactive/src/test/java/io/quarkus/tck/restclient/CustomInvokeWithJsonPProviderTest.java
This file was deleted.
Oops, something went wrong.