From e9ea1af37128516d0c4ef64acfb740004a44b629 Mon Sep 17 00:00:00 2001 From: Joshua Bauer Date: Wed, 16 Oct 2019 16:49:35 -0700 Subject: [PATCH] Support wrapping ServerResponse in a CompletableFuture --- CHANGELOG.md | 10 ++++ .../server/handlers/HandlerGenerator.java | 59 +++++++++++++------ .../proteus/test/controllers/Tests.java | 12 +++- .../test/server/TestControllerEndpoints.java | 9 ++- 4 files changed, 70 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b3cb85..e26456d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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.** diff --git a/core/src/main/java/io/sinistral/proteus/server/handlers/HandlerGenerator.java b/core/src/main/java/io/sinistral/proteus/server/handlers/HandlerGenerator.java index 1d24e2d..44e7ab1 100644 --- a/core/src/main/java/io/sinistral/proteus/server/handlers/HandlerGenerator.java +++ b/core/src/main/java/io/sinistral/proteus/server/handlers/HandlerGenerator.java @@ -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 { diff --git a/core/src/test/java/io/sinistral/proteus/test/controllers/Tests.java b/core/src/test/java/io/sinistral/proteus/test/controllers/Tests.java index 5fadf8d..e335af9 100644 --- a/core/src/test/java/io/sinistral/proteus/test/controllers/Tests.java +++ b/core/src/test/java/io/sinistral/proteus/test/controllers/Tests.java @@ -175,7 +175,7 @@ public CompletableFuture> responseFutureMap( ServerRequest re Map map = ImmutableMap.of("message", "success"); return CompletableFuture.completedFuture(map); } - + @GET @Path("response/map") public ServerResponse> futureMap( ServerRequest request ) @@ -409,6 +409,16 @@ public ServerResponse minValue(ServerRequest request, @QueryParam("p } + + @GET + @Path("response/future/response") + public CompletableFuture>> responseFutureResponseMap( ServerRequest request ) + { + Map map = ImmutableMap.of("message", "success"); + return CompletableFuture.completedFuture(response(map).applicationJson().ok()); + } + + @GET @Path("response/error/401") public ServerResponse unauthorizedError(ServerRequest request, @QueryParam("test") Optional param ) throws Exception diff --git a/core/src/test/java/io/sinistral/proteus/test/server/TestControllerEndpoints.java b/core/src/test/java/io/sinistral/proteus/test/server/TestControllerEndpoints.java index 0efeb4d..15be39a 100644 --- a/core/src/test/java/io/sinistral/proteus/test/server/TestControllerEndpoints.java +++ b/core/src/test/java/io/sinistral/proteus/test/server/TestControllerEndpoints.java @@ -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() {