From 99c0171a880efc464e9f81a830e46bc96db9cdf1 Mon Sep 17 00:00:00 2001 From: joshua bauer Date: Wed, 25 Jul 2018 08:50:39 -0700 Subject: [PATCH] Improve ServerResponse functionality. --- .../proteus/server/ServerResponse.java | 78 ++++++++++++++++--- 1 file changed, 69 insertions(+), 9 deletions(-) diff --git a/src/main/java/io/sinistral/proteus/server/ServerResponse.java b/src/main/java/io/sinistral/proteus/server/ServerResponse.java index 9e4e759..af573e8 100644 --- a/src/main/java/io/sinistral/proteus/server/ServerResponse.java +++ b/src/main/java/io/sinistral/proteus/server/ServerResponse.java @@ -3,10 +3,15 @@ */ package io.sinistral.proteus.server; +import java.net.URI; import java.nio.ByteBuffer; +import java.util.Date; import java.util.HashMap; +import java.util.Locale; import java.util.Map; +import javax.ws.rs.core.Response; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -15,7 +20,6 @@ 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; @@ -29,7 +33,10 @@ /** * @author jbauer + * Base server response. Friendlier interface to underlying exchange. + * @TODO extend javax.ws.rs.core.Response */ + public class ServerResponse { private static Logger log = LoggerFactory.getLogger(ServerResponse.class.getCanonicalName()); @@ -56,7 +63,7 @@ public class ServerResponse protected boolean processXml = false; protected boolean processJson = false; protected boolean preprocessed = false; - protected String redirectLocation = null; + protected String location = null; public ServerResponse() { @@ -125,6 +132,15 @@ public void setStatus(int status) { this.status = status; } + + /** + * @param status + * the status to set + */ + public void setStatus(Response.Status status) + { + this.status = status.getStatusCode(); + } /** * @param contentType @@ -169,6 +185,24 @@ public ServerResponse entity(T entity) return this; } + + public ServerResponse lastModified(Date date) + { + this.headers.add(Headers.LAST_MODIFIED, date.getTime()); + return this; + } + + public ServerResponse contentLanguage(Locale locale) + { + this.headers.add(Headers.CONTENT_LANGUAGE, locale.toLanguageTag()); + return this; + } + + public ServerResponse contentLanguage(String language) + { + this.headers.add(Headers.CONTENT_LANGUAGE, language); + return this; + } public ServerResponse throwable(Throwable throwable) { @@ -270,14 +304,14 @@ public ServerResponse ok() public ServerResponse redirect(String location) { - this.redirectLocation = location; + this.location = location; this.status = StatusCodes.FOUND; return this; } public ServerResponse redirectPermanently(String location) { - this.redirectLocation = location; + this.location = location; this.status = StatusCodes.MOVED_PERMANENTLY;; return this; } @@ -333,6 +367,27 @@ public ServerResponse created() this.status = StatusCodes.CREATED; return this; } + + public ServerResponse created(String location) + { + this.status = StatusCodes.CREATED; + this.location = location; + return this; + } + + public ServerResponse created(URI uri) + { + this.status = StatusCodes.CREATED; + this.location = uri.toString(); + return this; + } + + public ServerResponse notModified() + { + this.status = StatusCodes.NOT_MODIFIED; + return this; + } + public ServerResponse notFound() { @@ -443,10 +498,16 @@ public void send(final HttpServerExchange exchange) throws RuntimeException public void send(final HttpHandler handler, final HttpServerExchange exchange) throws RuntimeException { - if(this.redirectLocation != null) - { - exchange.setStatusCode(this.status); - exchange.getResponseHeaders().put(Headers.LOCATION, this.redirectLocation); + + exchange.setStatusCode(this.status); + + if(this.location != null) + { + exchange.getResponseHeaders().put(Headers.LOCATION, this.location); + } + + if(this.status == StatusCodes.TEMPORARY_REDIRECT || this.status == StatusCodes.PERMANENT_REDIRECT) + { exchange.endExchange(); return; } @@ -474,7 +535,6 @@ public void send(final HttpHandler handler, final HttpServerExchange exchange) t exchange.getResponseCookies().putAll(this.cookies); } - exchange.setStatusCode(this.status); if (this.contentType != null) {