Skip to content

Commit

Permalink
Added redirect mechanism on ServerRequest.
Browse files Browse the repository at this point in the history
  • Loading branch information
noboomu committed Feb 13, 2020
1 parent 3b70ce6 commit d7fcc26
Show file tree
Hide file tree
Showing 4 changed files with 242 additions and 18 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ Proteus Changelog.
## Unreleased
### No issue

**Cleanup pom.**


[3b70ce6a2f6d81d](https://github.com/noboomu/proteus/commit/3b70ce6a2f6d81d) Joshua Bauer *2020-01-31 23:55:10*

**Update readme.**

* Improvements to JsonViewWrapper.
Expand Down
5 changes: 1 addition & 4 deletions proteus-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@
<artifactId>proteus-project</artifactId>
<groupId>io.sinistral</groupId>
<version>0.4.4-SNAPSHOT</version>

</parent>
<modelVersion>4.0.0</modelVersion>

<modelVersion>4.0.0</modelVersion>
<artifactId>proteus-core</artifactId>

<name>Proteus Core</name>

<packaging>jar</packaging>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,30 @@

import io.sinistral.proteus.server.predicates.ServerPredicates;
import io.undertow.io.Receiver;
import io.undertow.io.Sender;
import io.undertow.security.api.SecurityContext;
import io.undertow.server.DefaultResponseListener;
import io.undertow.server.HttpServerExchange;
import io.undertow.server.*;
import io.undertow.server.handlers.Cookie;
import io.undertow.server.handlers.ExceptionHandler;
import io.undertow.server.handlers.form.FormData;
import io.undertow.server.handlers.form.FormDataParser;
import io.undertow.server.handlers.form.FormEncodedDataDefinition;
import io.undertow.server.handlers.form.MultiPartParserDefinition;
import io.undertow.util.AttachmentKey;
import io.undertow.util.FastConcurrentDirectDeque;
import io.undertow.util.Headers;
import io.undertow.util.*;
import org.xnio.XnioIoThread;
import org.xnio.channels.StreamSinkChannel;
import org.xnio.channels.StreamSourceChannel;
import org.xnio.conduits.StreamSinkConduit;
import org.xnio.conduits.StreamSourceConduit;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.util.Deque;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;

Expand All @@ -32,22 +39,20 @@
*/
public class ServerRequest
{
public static final AttachmentKey<ByteBuffer> BYTE_BUFFER_KEY = AttachmentKey.create(ByteBuffer.class);
protected static final Receiver.ErrorCallback ERROR_CALLBACK = (exchange, e) -> {
exchange.putAttachment(ExceptionHandler.THROWABLE, e);
exchange.endExchange();
};

public static final AttachmentKey<ByteBuffer> BYTE_BUFFER_KEY = AttachmentKey.create(ByteBuffer.class);

protected static final String CHARSET = "UTF-8";
protected static final String TMP_DIR = System.getProperty("java.io.tmpdir");

public final HttpServerExchange exchange;
protected final String path;
protected FormData form;
protected final String contentType;
protected final String method;
protected final String accept;
protected FormData form;

public ServerRequest()
{
Expand Down Expand Up @@ -176,6 +181,15 @@ public void startAsync(final Executor executor, final Runnable runnable)
exchange.dispatch(executor, runnable);
}

public void redirect(String location, boolean includeParameters)
{

exchange.getResponseHeaders().put(Headers.LOCATION, RedirectBuilder.redirect(exchange, location, includeParameters));
exchange.setStatusCode(302);
exchange.endExchange();

}

/**
* @param key
* @return the attachment
Expand All @@ -190,10 +204,7 @@ public <T> T getAttachment(AttachmentKey<T> key)
* @return the inetSocketAddress
* @see io.undertow.server.HttpServerExchange#getDestinationAddress()
*/
public InetSocketAddress getDestinationAddress()
{
return exchange.getDestinationAddress();
}


/**
* @return the path parameters
Expand Down Expand Up @@ -261,6 +272,197 @@ public String getAccept()
{
return accept;
}

public HttpString getProtocol() {return exchange.getProtocol();}

public HttpServerExchange setProtocol(HttpString protocol) {return exchange.setProtocol(protocol);}

public boolean isHttp09() {return exchange.isHttp09();}

public boolean isHttp10() {return exchange.isHttp10();}

public boolean isHttp11() {return exchange.isHttp11();}

public HttpString getRequestMethod() {return exchange.getRequestMethod();}

public HttpServerExchange setRequestMethod(HttpString requestMethod) {return exchange.setRequestMethod(requestMethod);}

public String getRequestScheme() {return exchange.getRequestScheme();}

public HttpServerExchange setRequestScheme(String requestScheme) {return exchange.setRequestScheme(requestScheme);}

public String getRequestURI() {return exchange.getRequestURI();}

public HttpServerExchange setRequestURI(String requestURI) {return exchange.setRequestURI(requestURI);}

public HttpServerExchange setRequestURI(String requestURI, boolean containsHost) {return exchange.setRequestURI(requestURI, containsHost);}

public boolean isHostIncludedInRequestURI() {return exchange.isHostIncludedInRequestURI();}

public String getRequestPath() {return exchange.getRequestPath();}

public HttpServerExchange setRequestPath(String requestPath) {return exchange.setRequestPath(requestPath);}

public String getRelativePath() {return exchange.getRelativePath();}

public HttpServerExchange setRelativePath(String relativePath) {return exchange.setRelativePath(relativePath);}

public String getResolvedPath() {return exchange.getResolvedPath();}

public HttpServerExchange setResolvedPath(String resolvedPath) {return exchange.setResolvedPath(resolvedPath);}

public String getQueryString() {return exchange.getQueryString();}

public HttpServerExchange setQueryString(String queryString) {return exchange.setQueryString(queryString);}

public String getRequestURL() {return exchange.getRequestURL();}

public String getRequestCharset() {return exchange.getRequestCharset();}

public String getResponseCharset() {return exchange.getResponseCharset();}

public String getHostName() {return exchange.getHostName();}

public String getHostAndPort() {return exchange.getHostAndPort();}

public int getHostPort() {return exchange.getHostPort();}

public ServerConnection getConnection() {return exchange.getConnection();}

public boolean isPersistent() {return exchange.isPersistent();}

public boolean isInIoThread() {return exchange.isInIoThread();}

public long getResponseBytesSent() {return exchange.getResponseBytesSent();}

public HttpServerExchange setPersistent(boolean persistent) {return exchange.setPersistent(persistent);}

public boolean isDispatched() {return exchange.isDispatched();}

public HttpServerExchange unDispatch() {return exchange.unDispatch();}

@Deprecated
public HttpServerExchange dispatch() {return exchange.dispatch();}

public HttpServerExchange dispatch(Runnable runnable) {return exchange.dispatch(runnable);}

public HttpServerExchange dispatch(Executor executor, Runnable runnable) {return exchange.dispatch(executor, runnable);}

public HttpServerExchange dispatch(HttpHandler handler) {return exchange.dispatch(handler);}

public HttpServerExchange dispatch(Executor executor, HttpHandler handler) {return exchange.dispatch(executor, handler);}

public HttpServerExchange setDispatchExecutor(Executor executor) {return exchange.setDispatchExecutor(executor);}

public Executor getDispatchExecutor() {return exchange.getDispatchExecutor();}

public HttpServerExchange upgradeChannel(HttpUpgradeListener listener) {return exchange.upgradeChannel(listener);}

public HttpServerExchange upgradeChannel(String productName, HttpUpgradeListener listener) {return exchange.upgradeChannel(productName, listener);}

public HttpServerExchange acceptConnectRequest(HttpUpgradeListener connectListener) {return exchange.acceptConnectRequest(connectListener);}

public HttpServerExchange addExchangeCompleteListener(ExchangeCompletionListener listener) {return exchange.addExchangeCompleteListener(listener);}

public HttpServerExchange addDefaultResponseListener(DefaultResponseListener listener) {return exchange.addDefaultResponseListener(listener);}

public InetSocketAddress getSourceAddress() {return exchange.getSourceAddress();}

public HttpServerExchange setSourceAddress(InetSocketAddress sourceAddress) {return exchange.setSourceAddress(sourceAddress);}

public InetSocketAddress getDestinationAddress() {return exchange.getDestinationAddress();}

public HttpServerExchange setDestinationAddress(InetSocketAddress destinationAddress) {return exchange.setDestinationAddress(destinationAddress);}

public HeaderMap getRequestHeaders() {return exchange.getRequestHeaders();}

public long getRequestContentLength() {return exchange.getRequestContentLength();}

public HeaderMap getResponseHeaders() {return exchange.getResponseHeaders();}

public long getResponseContentLength() {return exchange.getResponseContentLength();}

public HttpServerExchange setResponseContentLength(long length) {return exchange.setResponseContentLength(length);}

public HttpServerExchange addQueryParam(String name, String param) {return exchange.addQueryParam(name, param);}

public HttpServerExchange addPathParam(String name, String param) {return exchange.addPathParam(name, param);}

public Map<String, Cookie> getRequestCookies() {return exchange.getRequestCookies();}

public HttpServerExchange setResponseCookie(Cookie cookie) {return exchange.setResponseCookie(cookie);}

public Map<String, Cookie> getResponseCookies() {return exchange.getResponseCookies();}

public boolean isResponseStarted() {return exchange.isResponseStarted();}

public StreamSourceChannel getRequestChannel() {return exchange.getRequestChannel();}

public boolean isRequestChannelAvailable() {return exchange.isRequestChannelAvailable();}

public boolean isComplete() {return exchange.isComplete();}

public boolean isRequestComplete() {return exchange.isRequestComplete();}

public boolean isResponseComplete() {return exchange.isResponseComplete();}

public StreamSinkChannel getResponseChannel() {return exchange.getResponseChannel();}

public Sender getResponseSender() {return exchange.getResponseSender();}

public Receiver getRequestReceiver() {return exchange.getRequestReceiver();}

public boolean isResponseChannelAvailable() {return exchange.isResponseChannelAvailable();}

@Deprecated
public int getResponseCode() {return exchange.getResponseCode();}

@Deprecated
public HttpServerExchange setResponseCode(int statusCode) {return exchange.setResponseCode(statusCode);}

public int getStatusCode() {return exchange.getStatusCode();}

public HttpServerExchange setStatusCode(int statusCode) {return exchange.setStatusCode(statusCode);}

public HttpServerExchange setReasonPhrase(String message) {return exchange.setReasonPhrase(message);}

public String getReasonPhrase() {return exchange.getReasonPhrase();}

public HttpServerExchange addRequestWrapper(ConduitWrapper<StreamSourceConduit> wrapper) {return exchange.addRequestWrapper(wrapper);}

public HttpServerExchange addResponseWrapper(ConduitWrapper<StreamSinkConduit> wrapper) {return exchange.addResponseWrapper(wrapper);}

public BlockingHttpExchange startBlocking() {return exchange.startBlocking();}

public BlockingHttpExchange startBlocking(BlockingHttpExchange httpExchange) {return exchange.startBlocking(httpExchange);}

public boolean isBlocking() {return exchange.isBlocking();}

public InputStream getInputStream() {return exchange.getInputStream();}

public OutputStream getOutputStream() {return exchange.getOutputStream();}

public long getRequestStartTime() {return exchange.getRequestStartTime();}

public HttpServerExchange endExchange() {return exchange.endExchange();}

public XnioIoThread getIoThread() {return exchange.getIoThread();}

public long getMaxEntitySize() {return exchange.getMaxEntitySize();}

public HttpServerExchange setMaxEntitySize(long maxEntitySize) {return exchange.setMaxEntitySize(maxEntitySize);}

public void setSecurityContext(SecurityContext securityContext) {exchange.setSecurityContext(securityContext);}

public void addResponseCommitListener(ResponseCommitListener listener) {exchange.addResponseCommitListener(listener);}

public <T> List<T> getAttachmentList(AttachmentKey<? extends List<T>> key) {return exchange.getAttachmentList(key);}

public <T> T putAttachment(AttachmentKey<T> key, T value) {return exchange.putAttachment(key, value);}

public <T> T removeAttachment(AttachmentKey<T> key) {return exchange.removeAttachment(key);}

public <T> void addToAttachmentList(AttachmentKey<AttachmentList<T>> key, T value) {exchange.addToAttachmentList(key, value);}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,7 @@ public ServerResponse<T> withIoCallback(IoCallback ioCallback)
return this;
}


public void send(final HttpServerExchange exchange) throws RuntimeException
{
send(null, exchange);
Expand All @@ -567,6 +568,13 @@ public void send(final HttpHandler handler, final HttpServerExchange exchange) t
final boolean hasEntity = this.entity != null;
final boolean hasError = this.throwable != null;

if(exchange.isResponseStarted())
{
return;
}



exchange.setStatusCode(this.status);


Expand Down Expand Up @@ -660,7 +668,19 @@ public void send(final HttpHandler handler, final HttpServerExchange exchange) t
}

} else {
exchange.endExchange();

if(handler != null)
{
try {
handler.handleRequest(exchange);
} catch (Exception e) {
log.error("Error handling request",e);
exchange.endExchange();
}
}
else {
exchange.endExchange();
}
}

}
Expand Down

0 comments on commit d7fcc26

Please sign in to comment.