Skip to content

Commit

Permalink
Add support for redirects.
Browse files Browse the repository at this point in the history
  • Loading branch information
noboomu committed Jul 18, 2018
1 parent c8ac7ad commit d03a796
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
20 changes: 20 additions & 0 deletions src/main/java/io/sinistral/proteus/server/ServerResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -55,6 +56,7 @@ public class ServerResponse<T>
protected boolean processXml = false;
protected boolean processJson = false;
protected boolean preprocessed = false;
protected String redirectLocation = null;

public ServerResponse()
{
Expand Down Expand Up @@ -265,6 +267,12 @@ public ServerResponse<T> ok()
this.status = StatusCodes.OK;
return this;
}

public ServerResponse<T> redirect(String location)
{
this.redirectLocation = location;
return this;
}

public ServerResponse<T> accepted()
{
Expand Down Expand Up @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions src/test/java/io/sinistral/proteus/controllers/Tests.java
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,15 @@ public ServerResponse<Set<Long>> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));

}

Expand All @@ -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"));

}

Expand All @@ -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
Expand Down

0 comments on commit d03a796

Please sign in to comment.