-
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.
Support Providers in REST Client Reactive from context
Fix #26003
- Loading branch information
Showing
6 changed files
with
263 additions
and
0 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
102 changes: 102 additions & 0 deletions
102
...ve/deployment/src/test/java/io/quarkus/rest/client/reactive/ProvidersFromContextTest.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,102 @@ | ||
package io.quarkus.rest.client.reactive; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.net.URI; | ||
|
||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.core.Response; | ||
import jakarta.ws.rs.ext.ContextResolver; | ||
import jakarta.ws.rs.ext.Provider; | ||
|
||
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient; | ||
import org.jboss.resteasy.reactive.client.spi.ResteasyReactiveClientRequestContext; | ||
import org.jboss.resteasy.reactive.client.spi.ResteasyReactiveClientRequestFilter; | ||
import org.junit.jupiter.api.BeforeEach; | ||
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 ProvidersFromContextTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest TEST = new QuarkusUnitTest(); | ||
|
||
@TestHTTPResource | ||
URI baseUri; | ||
|
||
private Client client; | ||
|
||
@BeforeEach | ||
public void before() { | ||
client = QuarkusRestClientBuilder.newBuilder() | ||
.baseUri(baseUri) | ||
.register(TestClientRequestFilter.class) | ||
.register(MyContextResolver.class) | ||
.build(Client.class); | ||
} | ||
|
||
@Test | ||
public void test() { | ||
Response response = client.get(); | ||
assertEquals(200, response.getStatus()); | ||
} | ||
|
||
@RegisterRestClient | ||
public interface Client { | ||
|
||
@GET | ||
@Path("test") | ||
Response get(); | ||
} | ||
|
||
@Path("test") | ||
public static class Endpoint { | ||
|
||
@GET | ||
public Response get() { | ||
return Response.ok().build(); | ||
} | ||
} | ||
|
||
public static class Person { | ||
public String name; | ||
} | ||
|
||
public static class MyContextResolver implements ContextResolver<Person> { | ||
|
||
@Override | ||
public Person getContext(Class<?> aClass) { | ||
return new Person(); | ||
} | ||
} | ||
|
||
@Provider | ||
public static class TestClientRequestFilter implements ResteasyReactiveClientRequestFilter { | ||
|
||
@Override | ||
public void filter(ResteasyReactiveClientRequestContext requestContext) { | ||
if (requestContext.getProviders() == null) { | ||
throw new RuntimeException("Providers was not injected"); | ||
} | ||
|
||
var readers = requestContext.getProviders().getMessageBodyReader(String.class, null, null, null); | ||
if (readers == null) { | ||
throw new RuntimeException("No readers were found"); | ||
} | ||
|
||
var writers = requestContext.getProviders().getMessageBodyWriter(String.class, null, null, null); | ||
if (writers == null) { | ||
throw new RuntimeException("No writers were found"); | ||
} | ||
|
||
ContextResolver<Person> contextResolver = requestContext.getProviders().getContextResolver(Person.class, null); | ||
if (contextResolver == null) { | ||
throw new RuntimeException("Context resolver was not found"); | ||
} | ||
} | ||
} | ||
} |
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
60 changes: 60 additions & 0 deletions
60
...e/client/runtime/src/main/java/org/jboss/resteasy/reactive/client/impl/ProvidersImpl.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,60 @@ | ||
package org.jboss.resteasy.reactive.client.impl; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Type; | ||
import java.util.List; | ||
|
||
import jakarta.ws.rs.RuntimeType; | ||
import jakarta.ws.rs.core.MediaType; | ||
import jakarta.ws.rs.ext.ContextResolver; | ||
import jakarta.ws.rs.ext.ExceptionMapper; | ||
import jakarta.ws.rs.ext.MessageBodyReader; | ||
import jakarta.ws.rs.ext.MessageBodyWriter; | ||
import jakarta.ws.rs.ext.Providers; | ||
|
||
public class ProvidersImpl implements Providers { | ||
|
||
private final RestClientRequestContext context; | ||
|
||
public ProvidersImpl(RestClientRequestContext context) { | ||
this.context = context; | ||
} | ||
|
||
@Override | ||
public <T> MessageBodyReader<T> getMessageBodyReader(Class<T> type, Type genericType, Annotation[] annotations, | ||
MediaType mediaType) { | ||
List<MessageBodyReader<?>> readers = context.getRestClient().getClientContext().getSerialisers() | ||
.findReaders(context.getConfiguration(), type, mediaType, RuntimeType.CLIENT); | ||
for (MessageBodyReader<?> reader : readers) { | ||
if (reader.isReadable(type, genericType, annotations, mediaType)) { | ||
return (MessageBodyReader<T>) reader; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public <T> MessageBodyWriter<T> getMessageBodyWriter(Class<T> type, Type genericType, Annotation[] annotations, | ||
MediaType mediaType) { | ||
List<MessageBodyWriter<?>> writers = context.getRestClient().getClientContext().getSerialisers() | ||
.findWriters(context.getConfiguration(), type, mediaType, RuntimeType.CLIENT); | ||
for (MessageBodyWriter<?> writer : writers) { | ||
if (writer.isWriteable(type, genericType, annotations, mediaType)) { | ||
return (MessageBodyWriter<T>) writer; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public <T extends Throwable> ExceptionMapper<T> getExceptionMapper(Class<T> type) { | ||
throw new UnsupportedOperationException( | ||
"`jakarta.ws.rs.ext.ExceptionMapper` are not supported in REST Client Reactive"); | ||
} | ||
|
||
@Override | ||
public <T> ContextResolver<T> getContextResolver(Class<T> contextType, MediaType mediaType) { | ||
// TODO: support getting context resolver by mediaType (which is provided using the `@Produces` annotation). | ||
return context.getConfiguration().getContextResolver(contextType); | ||
} | ||
} |
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