Skip to content

Commit

Permalink
Support wrapping ServerResponse in a CompletableFuture
Browse files Browse the repository at this point in the history
  • Loading branch information
noboomu committed Oct 16, 2019
1 parent 23d28b1 commit e9ea1af
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 20 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ Proteus Changelog.
## Unreleased
### No issue

**Added max min annotation support.**


[23d28b1509aaaf7](https://github.com/noboomu/proteus/commit/23d28b1509aaaf7) Joshua Bauer *2019-10-16 20:45:51*

**refactor(readme): Typo in maven dependency tag for OpenAPI**


[b1f9def9a259cb7](https://github.com/noboomu/proteus/commit/b1f9def9a259cb7) Dheeraj Khardwal *2019-10-13 10:45:08*

**Added travis config.**


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -675,38 +675,61 @@ protected void addClassMethodHandlers(TypeSpec.Builder typeBuilder, Class<?> cla

methodBuilder.addCode("$L", "\n");


if (m.getReturnType().equals(ServerResponse.class)) {
methodBuilder.addStatement("$L.send(this,$L)", "response", "exchange");

} else if (m.getReturnType().getTypeName().contains("java.util.concurrent.CompletionStage")
|| m.getReturnType().getTypeName().contains("java.util.concurrent.CompletableFuture"))
} else if ((m.getGenericReturnType().toString().contains("java.util.concurrent.CompletionStage") && m.getGenericReturnType().toString().contains("ServerResponse"))
|| (m.getGenericReturnType().toString().contains("java.util.concurrent.CompletableFuture") && m.getGenericReturnType().toString().contains("ServerResponse")))

{
String postProcess = ".";

if (!producesContentType.contains(",")) {
if (producesContentType.contains(MediaType.APPLICATION_JSON)) {
postProcess = ".applicationJson().";
} else if (producesContentType.contains(MediaType.APPLICATION_XML)) {
postProcess = ".applicationXml().";
} else if (producesContentType.contains(MediaType.TEXT_HTML)) {
postProcess = ".textHtml().";
}
else {
postProcess = String.format(".contentType(%s).",producesContentType);
}
}
methodBuilder.addCode("exchange.dispatch( () -> ");
methodBuilder.beginControlFlow("", "");


methodBuilder.addCode(
"$L.thenAccept( r -> io.sinistral.proteus.server.ServerResponse.response(r)" + postProcess + "send(this,$L) )\n\t.exceptionally( ex -> ",
"$L.thenAccept( r -> r.send(this,$L) )\n\t.exceptionally( ex -> ",
"response", "exchange");
methodBuilder.beginControlFlow("", "");
methodBuilder.addCode("\t\tthrow new java.util.concurrent.CompletionException(ex);\n\t");
methodBuilder.endControlFlow(")", "");

methodBuilder.endControlFlow(")", "");
}
else if (m.getReturnType().getTypeName().contains("java.util.concurrent.CompletionStage")
|| m.getReturnType().getTypeName().contains("java.util.concurrent.CompletableFuture"))
{


String postProcess = ".";

if (!producesContentType.contains(",")) {
if (producesContentType.contains(MediaType.APPLICATION_JSON)) {
postProcess = ".applicationJson().";
} else if (producesContentType.contains(MediaType.APPLICATION_XML)) {
postProcess = ".applicationXml().";
} else if (producesContentType.contains(MediaType.TEXT_HTML)) {
postProcess = ".textHtml().";
} else if (producesContentType != null) {
postProcess = String.format(".contentType(\"%s\").", producesContentType);
} else {
postProcess = ".";
}
}
methodBuilder.addCode("exchange.dispatch( () -> ");
methodBuilder.beginControlFlow("", "");


methodBuilder.addCode(
"$L.thenAccept( r -> io.sinistral.proteus.server.ServerResponse.response(r)" + postProcess + "send(this,$L) )\n\t.exceptionally( ex -> ",
"response", "exchange");
methodBuilder.beginControlFlow("", "");
methodBuilder.addCode("\t\tthrow new java.util.concurrent.CompletionException(ex);\n\t");
methodBuilder.endControlFlow(")", "");

methodBuilder.endControlFlow(")", "");




} else {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public CompletableFuture<Map<String,String>> responseFutureMap( ServerRequest re
Map<String,String> map = ImmutableMap.of("message", "success");
return CompletableFuture.completedFuture(map);
}

@GET
@Path("response/map")
public ServerResponse<Map<String,String>> futureMap( ServerRequest request )
Expand Down Expand Up @@ -409,6 +409,16 @@ public ServerResponse<ByteBuffer> minValue(ServerRequest request, @QueryParam("p

}


@GET
@Path("response/future/response")
public CompletableFuture<ServerResponse<Map<String,String>>> responseFutureResponseMap( ServerRequest request )
{
Map<String,String> map = ImmutableMap.of("message", "success");
return CompletableFuture.completedFuture(response(map).applicationJson().ok());
}


@GET
@Path("response/error/401")
public ServerResponse<Void> unauthorizedError(ServerRequest request, @QueryParam("test") Optional<String> param ) throws Exception
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,14 @@ public void responseFutureMap()
{
given().accept(ContentType.JSON).when().get("tests/response/future/map").then().statusCode(200).and().body("message", is("success"));
}


@Test
public void responseFutureResponseMap()
{
given().accept(ContentType.JSON).when().get("tests/response/future/response").then().statusCode(200).and().body("message", is("success"));
}


@Test
public void testRedirect()
{
Expand Down

0 comments on commit e9ea1af

Please sign in to comment.