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 case where REST Client returns JAX-RS Response from a JAX-RS endpoint #16999

Merged
merged 1 commit into from
May 10, 2021
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,101 @@
package io.quarkus.rest.client.reactive;

import static io.restassured.RestAssured.when;
import static org.hamcrest.CoreMatchers.containsString;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.eclipse.microprofile.rest.client.RestClientBuilder;
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
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 com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import io.quarkus.test.QuarkusUnitTest;
import io.smallrye.common.annotation.Blocking;

public class ClientAndServerSharingResponseTest {

@RegisterExtension
static final QuarkusUnitTest TEST = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClasses(Endpoint.class, HeadersService.class));

@Test
public void test() {
when().get("/test/client")
.then()
.statusCode(200)
.body(containsString("{\"Accept\":\"application/json\"}"));
}

@RegisterRestClient
public interface HeadersService {

@POST
@Path("test")
@Produces(MediaType.APPLICATION_JSON)
Response dumpHeaders();
}

@Path("test")
public static class Endpoint {

private final ObjectMapper mapper;
private final HeadersService headersService;

public Endpoint(ObjectMapper mapper,
@ConfigProperty(name = "quarkus.http.test-port", defaultValue = "8081") Integer testPort)
throws MalformedURLException {
this.mapper = mapper;
this.headersService = RestClientBuilder.newBuilder()
.baseUrl(new URL(String.format("http://localhost:%d", testPort)))
.readTimeout(1, TimeUnit.SECONDS)
.build(HeadersService.class);
;
}

@POST
@Produces(MediaType.APPLICATION_JSON)
public Response dumpHeaders(@Context HttpHeaders headers) {
ArrayNode array = mapper.createArrayNode();
for (Map.Entry<String, List<String>> header : headers.getRequestHeaders().entrySet()) {
ObjectNode node = mapper.createObjectNode();
List<String> strings = header.getValue();
if ((strings == null) || strings.isEmpty()) {
continue;
}
node.put(header.getKey(), strings.get(0));
array.add(node);
}
return Response.ok(array).build();
}

@GET
@Path("client")
@Produces(MediaType.APPLICATION_JSON)
@Blocking
public Response testCase4() {
return headersService.dumpHeaders();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.jboss.resteasy.reactive.server.handlers;

import java.io.ByteArrayInputStream;
import java.util.List;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;
Expand All @@ -22,24 +24,33 @@ public void handle(ResteasyReactiveRequestContext requestContext) throws Excepti
boolean mediaTypeAlreadyExists = false;
//we already have a response
//set it explicitly
Response.ResponseBuilder responseBuilder;
ResponseBuilderImpl responseBuilder;
Response existing = (Response) result;
if (existing.getEntity() instanceof GenericEntity) {
GenericEntity<?> genericEntity = (GenericEntity<?>) existing.getEntity();
requestContext.setGenericReturnType(genericEntity.getType());
responseBuilder = Response.fromResponse(existing).entity(genericEntity.getEntity());
responseBuilder = fromResponse(existing);
responseBuilder.entity(genericEntity.getEntity());
} else {
// TCK says to use the entity type as generic type if we return a response
if (existing.hasEntity())
if (existing.hasEntity() && (existing.getEntity() != null))
requestContext.setGenericReturnType(existing.getEntity().getClass());
//TODO: super inefficent
responseBuilder = Response.fromResponse((Response) result);
//TODO: super inefficient
responseBuilder = fromResponse((Response) result);
if ((result instanceof ResponseImpl)) {
// needed in order to preserve entity annotations
ResponseImpl responseImpl = (ResponseImpl) result;
if (responseImpl.getEntityAnnotations() != null) {
requestContext.setAdditionalAnnotations(responseImpl.getEntityAnnotations());
}

// this is a weird case where the response comes from the the rest-client
if (responseBuilder.getEntity() == null) {
if (responseImpl.getEntityStream() instanceof ByteArrayInputStream) {
ByteArrayInputStream byteArrayInputStream = (ByteArrayInputStream) responseImpl.getEntityStream();
responseBuilder.entity(byteArrayInputStream.readAllBytes());
}
}
}
}
if (existing.getMediaType() != null) {
Expand Down Expand Up @@ -100,4 +111,19 @@ public boolean isCreated() {

}
}

// avoid the runtime overhead of looking up the provider
private ResponseBuilderImpl fromResponse(Response response) {
Response.ResponseBuilder b = new ResponseBuilderImpl().status(response.getStatus());
if (response.hasEntity()) {
b.entity(response.getEntity());
}
for (String headerName : response.getHeaders().keySet()) {
List<Object> headerValues = response.getHeaders().get(headerName);
for (Object headerValue : headerValues) {
b.header(headerName, headerValue);
}
}
return (ResponseBuilderImpl) b;
}
}