Skip to content

Commit

Permalink
Support RestResponse<T> as a return type for reactive rest client met…
Browse files Browse the repository at this point in the history
…hods

Closes: #19966
  • Loading branch information
geoand committed Sep 7, 2021
1 parent 7ccc007 commit bba9219
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import org.jboss.resteasy.reactive.client.impl.ClientResponseBuilderImpl;
import org.jboss.resteasy.reactive.client.impl.ClientResponseContextImpl;
import org.jboss.resteasy.reactive.client.impl.RestClientRequestContext;
Expand All @@ -22,7 +23,10 @@ public static ResponseImpl mapToResponse(RestClientRequestContext context, boole
builder.status(responseContext.getStatus(), responseContext.getReasonPhrase());
builder.setAllHeaders(responseContext.getHeaders());
builder.invocationState(context);
if (context.isResponseTypeSpecified() && parseContent) { // this case means that a specific response type was requested
if (context.isResponseTypeSpecified()
// when we are returning a RestResponse, we don't want to do any parsing
&& (Response.Status.Family.familyOf(context.getResponseStatus()) == Response.Status.Family.SUCCESSFUL)
&& parseContent) { // this case means that a specific response type was requested
Object entity = context.readEntity(responseContext.getEntityStream(),
context.getResponseType(),
responseContext.getMediaType(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import javax.ws.rs.client.InvocationCallback;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.Response;
import org.jboss.resteasy.reactive.RestResponse;
import org.jboss.resteasy.reactive.common.jaxrs.ConfigurationImpl;
import org.jboss.resteasy.reactive.common.util.types.Types;
import org.jboss.resteasy.reactive.spi.ThreadSetupAction;
Expand Down Expand Up @@ -280,12 +281,22 @@ private <T> Type getInvocationCallbackType(InvocationCallback<T> callback) {
public <T> CompletableFuture<T> mapResponse(CompletableFuture<Response> res, Class<?> responseType) {
if (responseType.equals(Response.class)) {
return (CompletableFuture<T>) res;
} else if (responseType.equals(RestResponse.class)) {
return res.thenApply(new Function<>() {
@Override
public T apply(Response response) {
return (T) RestResponse.ResponseBuilder.create(response.getStatusInfo(), response.getEntity())
.replaceAll(response.getHeaders()).build();
}
});
} else {
return res.thenApply(new Function<>() {
@Override
public T apply(Response response) {
return (T) response.getEntity();
}
});
}
return res.thenApply(new Function<Response, T>() {
@Override
public T apply(Response response) {
return (T) response.getEntity();
}
});

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.net.URI;
import java.util.Arrays;
Expand All @@ -29,6 +30,7 @@
import javax.ws.rs.ext.ReaderInterceptor;
import javax.ws.rs.ext.WriterInterceptor;
import org.jboss.resteasy.reactive.ClientWebApplicationException;
import org.jboss.resteasy.reactive.RestResponse;
import org.jboss.resteasy.reactive.client.spi.ClientRestHandler;
import org.jboss.resteasy.reactive.common.core.AbstractResteasyReactiveContext;
import org.jboss.resteasy.reactive.common.core.Serialisers;
Expand Down Expand Up @@ -102,9 +104,23 @@ public RestClientRequestContext(ClientImpl restClient,
this.responseTypeSpecified = false;
} else {
this.responseType = responseType;
boolean isJaxResponse = responseType.getRawType().equals(Response.class);
this.checkSuccessfulFamily = !isJaxResponse;
this.responseTypeSpecified = !isJaxResponse;
if (responseType.getRawType().equals(Response.class)) {
this.checkSuccessfulFamily = false;
this.responseTypeSpecified = false;
} else if (responseType.getRawType().equals(RestResponse.class)) {
if (responseType.getType() instanceof ParameterizedType) {
ParameterizedType type = (ParameterizedType) responseType.getType();
if (type.getActualTypeArguments().length == 1) {
Type restResponseType = type.getActualTypeArguments()[0];
this.responseType = new GenericType<>(restResponseType);
}
}
this.checkSuccessfulFamily = false;
this.responseTypeSpecified = true;
} else {
this.checkSuccessfulFamily = true;
this.responseTypeSpecified = true;
}
}
this.registerBodyHandler = registerBodyHandler;
this.result = new CompletableFuture<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import javax.ws.rs.core.MediaType;

import org.eclipse.microprofile.rest.client.annotation.RegisterProvider;
import org.jboss.resteasy.reactive.RestResponse;

import io.smallrye.mutiny.Uni;

Expand Down Expand Up @@ -54,4 +55,12 @@ public interface AppleClient {
@POST
@Produces(MediaType.APPLICATION_JSON)
Uni<String> uniStringApple();

@POST
@Produces(MediaType.APPLICATION_JSON)
RestResponse<Apple> restResponseApple();

@POST
@Produces(MediaType.APPLICATION_JSON)
Uni<RestResponse<Apple>> uniRestResponseApple();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.net.URI;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.stream.Collectors;

import javax.enterprise.context.ApplicationScoped;
Expand All @@ -12,6 +13,7 @@

import org.eclipse.microprofile.rest.client.RestClientBuilder;
import org.eclipse.microprofile.rest.client.inject.RestClient;
import org.jboss.resteasy.reactive.RestResponse;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -74,12 +76,15 @@ void init(@Observes Router router) {
Uni<Apple> apple8 = Uni.createFrom().completionStage(client.completionStringApple()).onItem()
.transform(this::toApple);
Uni<Apple> apple9 = client.uniStringApple().onItem().transform(this::toApple);
Uni.combine().all().unis(apple1, apple2, apple3, apple4, apple5, apple6, apple7, apple8, apple9).asTuple()
Uni<Apple> apple10 = Uni.createFrom().item(client.restResponseApple().getEntity());
Uni<Apple> apple11 = client.uniRestResponseApple().onItem().transform(RestResponse::getEntity);
Uni.combine().all().unis(apple1, apple2, apple3, apple4, apple5, apple6, apple7, apple8, apple9, apple10, apple11)
.combinedWith(Function.identity())
.subscribe()
.with(tuple -> {
.with(list -> {
try {
rc.response().putHeader("content-type", "application/json")
.end(mapper.writeValueAsString(tuple.asList()));
.end(mapper.writeValueAsString(list));
} catch (JsonProcessingException e) {
fail(rc, e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ void shouldMakeJsonRequest() {
.statusCode(200)
.contentType("application/json")
.extract().body().jsonPath().getList(".", Map.class);
assertThat(results).hasSize(9).allSatisfy(m -> {
assertThat(results).hasSize(11).allSatisfy(m -> {
assertThat(m).containsOnlyKeys("cultivar");
});
Map<Object, Long> valueByCount = results.stream().collect(Collectors.groupingBy(m -> m.get("cultivar"), counting()));
assertThat(valueByCount).containsOnly(entry("cortland", 3L), entry("lobo", 3L), entry("golden delicious", 3L));
assertThat(valueByCount).containsOnly(entry("cortland", 4L), entry("lobo", 4L), entry("golden delicious", 3L));
}

@Test
Expand Down

0 comments on commit bba9219

Please sign in to comment.