Skip to content

Commit

Permalink
Improve ServerResponse functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
noboomu committed Jul 25, 2018
1 parent dab0494 commit 99c0171
Showing 1 changed file with 69 additions and 9 deletions.
78 changes: 69 additions & 9 deletions src/main/java/io/sinistral/proteus/server/ServerResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -29,7 +33,10 @@

/**
* @author jbauer
* Base server response. Friendlier interface to underlying exchange.
* @TODO extend javax.ws.rs.core.Response
*/

public class ServerResponse<T>
{
private static Logger log = LoggerFactory.getLogger(ServerResponse.class.getCanonicalName());
Expand All @@ -56,7 +63,7 @@ public class ServerResponse<T>
protected boolean processXml = false;
protected boolean processJson = false;
protected boolean preprocessed = false;
protected String redirectLocation = null;
protected String location = null;

public ServerResponse()
{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -169,6 +185,24 @@ public ServerResponse<T> entity(T entity)

return this;
}

public ServerResponse<T> lastModified(Date date)
{
this.headers.add(Headers.LAST_MODIFIED, date.getTime());
return this;
}

public ServerResponse<T> contentLanguage(Locale locale)
{
this.headers.add(Headers.CONTENT_LANGUAGE, locale.toLanguageTag());
return this;
}

public ServerResponse<T> contentLanguage(String language)
{
this.headers.add(Headers.CONTENT_LANGUAGE, language);
return this;
}

public ServerResponse<T> throwable(Throwable throwable)
{
Expand Down Expand Up @@ -270,14 +304,14 @@ public ServerResponse<T> ok()

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

public ServerResponse<T> redirectPermanently(String location)
{
this.redirectLocation = location;
this.location = location;
this.status = StatusCodes.MOVED_PERMANENTLY;;
return this;
}
Expand Down Expand Up @@ -333,6 +367,27 @@ public ServerResponse<T> created()
this.status = StatusCodes.CREATED;
return this;
}

public ServerResponse<T> created(String location)
{
this.status = StatusCodes.CREATED;
this.location = location;
return this;
}

public ServerResponse<T> created(URI uri)
{
this.status = StatusCodes.CREATED;
this.location = uri.toString();
return this;
}

public ServerResponse<T> notModified()
{
this.status = StatusCodes.NOT_MODIFIED;
return this;
}


public ServerResponse<T> notFound()
{
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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)
{
Expand Down

0 comments on commit 99c0171

Please sign in to comment.