Skip to content

Commit

Permalink
Support CompletionStage<Response>
Browse files Browse the repository at this point in the history
Signed-off-by: jansupol <[email protected]>
  • Loading branch information
jansupol committed Jul 21, 2022
1 parent 82e89ac commit 3e8c2c7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -85,6 +85,7 @@ public class ResourceMethodInvoker implements Endpoint, ResourceInfo {
private final Type invocableResponseType;
private final boolean canUseInvocableResponseType;
private final boolean isCompletionStageResponseType;
private final boolean isCompletionStageResponseResponseType; // CompletionStage<Response>
private final Type completionStageResponseType;
private final ResourceMethodDispatcher dispatcher;
private final Method resourceMethod;
Expand Down Expand Up @@ -313,6 +314,8 @@ protected void configure() {
&& CompletionStage.class.isAssignableFrom((Class<?>) ((ParameterizedType) invocableResponseType).getRawType());
this.completionStageResponseType =
isCompletionStageResponseType ? ((ParameterizedType) invocableResponseType).getActualTypeArguments()[0] : null;
this.isCompletionStageResponseResponseType = Class.class.isInstance(completionStageResponseType)
&& Response.class.isAssignableFrom((Class<?>) completionStageResponseType);
}

private <T> void addNameBoundProviders(
Expand Down Expand Up @@ -465,7 +468,7 @@ private Response invoke(final RequestProcessingContext context, final Object res
if (canUseInvocableResponseType
&& response.hasEntity()
&& !(response.getEntityType() instanceof ParameterizedType)) {
response.setEntityType(unwrapInvocableResponseType(context.request()));
response.setEntityType(unwrapInvocableResponseType(context.request(), response.getEntityType()));
}

return response;
Expand All @@ -484,10 +487,10 @@ private Response invoke(final RequestProcessingContext context, final Object res
return jaxrsResponse;
}

private Type unwrapInvocableResponseType(ContainerRequest request) {
private Type unwrapInvocableResponseType(ContainerRequest request, Type entityType) {
if (isCompletionStageResponseType
&& request.resolveProperty(ServerProperties.UNWRAP_COMPLETION_STAGE_IN_WRITER_ENABLE, Boolean.FALSE)) {
return completionStageResponseType;
&& request.resolveProperty(ServerProperties.UNWRAP_COMPLETION_STAGE_IN_WRITER_ENABLE, Boolean.TRUE)) {
return isCompletionStageResponseResponseType ? entityType : completionStageResponseType;
}
return invocableResponseType;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -104,6 +104,14 @@ public void testGetCompletedAsync() {
assertThat(response.readEntity(String.class), is(ENTITY));
}

@Test
public void testGetCompletedAsyncResponse() {
Response response = target("cs/completedAsyncResponse").request().get();

assertThat(response.getStatus(), is(200));
assertThat(response.readEntity(List.class).get(0), is(ENTITY));
}

@Test
public void testGetException400Async() {
Response response = target("cs/exception400Async").request().get();
Expand Down Expand Up @@ -213,6 +221,14 @@ public CompletionStage<String> getCompletedAsync() {
return cs;
}

@GET
@Path("/completedAsyncResponse")
public CompletionStage<Response> getCompletedAsyncResponse() {
CompletableFuture<Response> cs = new CompletableFuture<>();
delaySubmit(() -> cs.complete(Response.ok().entity(Collections.singletonList(ENTITY)).build()));
return cs;
}

@GET
@Path("/exception400Async")
public CompletionStage<String> getException400Async() {
Expand Down

0 comments on commit 3e8c2c7

Please sign in to comment.