From d03a796fb7f70b612977bdfe82db01c6784d4125 Mon Sep 17 00:00:00 2001 From: joshua bauer Date: Wed, 18 Jul 2018 15:12:53 -0700 Subject: [PATCH] Add support for redirects. --- .../proteus/server/ServerResponse.java | 20 +++++++++++++++++++ .../sinistral/proteus/controllers/Tests.java | 9 +++++++++ .../server/TestControllerEndpoints.java | 10 ++++++++-- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/sinistral/proteus/server/ServerResponse.java b/src/main/java/io/sinistral/proteus/server/ServerResponse.java index 4919c3d..a56b55f 100644 --- a/src/main/java/io/sinistral/proteus/server/ServerResponse.java +++ b/src/main/java/io/sinistral/proteus/server/ServerResponse.java @@ -15,6 +15,7 @@ import com.google.inject.Inject; import io.sinistral.proteus.server.predicates.ServerPredicates; +import io.undertow.Handlers; import io.undertow.io.IoCallback; import io.undertow.server.DefaultResponseListener; import io.undertow.server.HttpHandler; @@ -55,6 +56,7 @@ public class ServerResponse protected boolean processXml = false; protected boolean processJson = false; protected boolean preprocessed = false; + protected String redirectLocation = null; public ServerResponse() { @@ -265,6 +267,12 @@ public ServerResponse ok() this.status = StatusCodes.OK; return this; } + + public ServerResponse redirect(String location) + { + this.redirectLocation = location; + return this; + } public ServerResponse accepted() { @@ -390,6 +398,18 @@ public void send(final HttpServerExchange exchange) throws RuntimeException public void send(final HttpHandler handler, final HttpServerExchange exchange) throws RuntimeException { + if(this.redirectLocation != null) + { + try + { + Handlers.redirect(redirectLocation).handleRequest(exchange); + } catch (Exception e) + { + throw new RuntimeException(e); + } + return; + } + final boolean hasBody = this.body != null; final boolean hasEntity = this.entity != null; final boolean hasError = this.throwable != null; diff --git a/src/test/java/io/sinistral/proteus/controllers/Tests.java b/src/test/java/io/sinistral/proteus/controllers/Tests.java index fd4b76f..ee21c1a 100644 --- a/src/test/java/io/sinistral/proteus/controllers/Tests.java +++ b/src/test/java/io/sinistral/proteus/controllers/Tests.java @@ -225,6 +225,15 @@ public ServerResponse> genericOptionalSet( ServerRequest request, @Qu } + + @GET + @Path("/redirect") + @ApiOperation(value = "Redirect endpoint", httpMethod = "GET" ) + public ServerResponse testRedirect() + { + return response().redirect("https://google.com"); + } + @POST @Path("/response/parse/ids") @Blocking diff --git a/src/test/java/io/sinistral/proteus/server/TestControllerEndpoints.java b/src/test/java/io/sinistral/proteus/server/TestControllerEndpoints.java index 55532c8..bfb8484 100644 --- a/src/test/java/io/sinistral/proteus/server/TestControllerEndpoints.java +++ b/src/test/java/io/sinistral/proteus/server/TestControllerEndpoints.java @@ -141,7 +141,7 @@ public void responseEchoModel() { User model = new User(101L,UserType.ADMIN); - given().contentType(ContentType.JSON).accept(ContentType.JSON).body(model).log().all().when().post("tests/response/json/echo").then().statusCode(200).and().body(containsString("101")); + given().contentType(ContentType.JSON).accept(ContentType.JSON).body(model).log().uri().when().post("tests/response/json/echo").then().statusCode(200).and().body(containsString("101")); } @@ -151,7 +151,7 @@ public void responseInnerClassModel() User.InnerUserModel model = new User.InnerUserModel(); model.id = 101L; - given().contentType(ContentType.JSON).accept(ContentType.JSON).body(model).log().all().when().post("tests/response/json/innerClass").then().statusCode(200).and().body(containsString("101")); + given().contentType(ContentType.JSON).accept(ContentType.JSON).body(model).log().uri().when().post("tests/response/json/innerClass").then().statusCode(200).and().body(containsString("101")); } @@ -168,6 +168,12 @@ public void responseMap() { given().accept(ContentType.JSON).log().uri().when().get("tests/response/future/map").then().statusCode(200).and().body("message", is("success")); } + + @Test + public void testRedirect() + { + given().log().uri().when().get("tests/redirect").then().statusCode(200).and().header("Server", "gws"); + } @SuppressWarnings("resource") @Test