From d7fcc26ab5ff4a73c24b877ed4c41de266bb2048 Mon Sep 17 00:00:00 2001 From: Joshua Bauer Date: Thu, 13 Feb 2020 14:06:02 -0800 Subject: [PATCH] Added redirect mechanism on ServerRequest. --- CHANGELOG.md | 5 + proteus-core/pom.xml | 5 +- .../proteus/server/ServerRequest.java | 228 +++++++++++++++++- .../proteus/server/ServerResponse.java | 22 +- 4 files changed, 242 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c999a9f..d107593 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/proteus-core/pom.xml b/proteus-core/pom.xml index 953d38c..e3a33f1 100644 --- a/proteus-core/pom.xml +++ b/proteus-core/pom.xml @@ -4,14 +4,11 @@ proteus-project io.sinistral 0.4.4-SNAPSHOT - - 4.0.0 + 4.0.0 proteus-core - Proteus Core - jar diff --git a/proteus-core/src/main/java/io/sinistral/proteus/server/ServerRequest.java b/proteus-core/src/main/java/io/sinistral/proteus/server/ServerRequest.java index fa2d021..a7c3179 100644 --- a/proteus-core/src/main/java/io/sinistral/proteus/server/ServerRequest.java +++ b/proteus-core/src/main/java/io/sinistral/proteus/server/ServerRequest.java @@ -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; @@ -32,22 +39,20 @@ */ public class ServerRequest { + public static final AttachmentKey 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 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() { @@ -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 @@ -190,10 +204,7 @@ public T getAttachment(AttachmentKey key) * @return the inetSocketAddress * @see io.undertow.server.HttpServerExchange#getDestinationAddress() */ - public InetSocketAddress getDestinationAddress() - { - return exchange.getDestinationAddress(); - } + /** * @return the path parameters @@ -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 getRequestCookies() {return exchange.getRequestCookies();} + + public HttpServerExchange setResponseCookie(Cookie cookie) {return exchange.setResponseCookie(cookie);} + + public Map 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 wrapper) {return exchange.addRequestWrapper(wrapper);} + + public HttpServerExchange addResponseWrapper(ConduitWrapper 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 List getAttachmentList(AttachmentKey> key) {return exchange.getAttachmentList(key);} + + public T putAttachment(AttachmentKey key, T value) {return exchange.putAttachment(key, value);} + + public T removeAttachment(AttachmentKey key) {return exchange.removeAttachment(key);} + + public void addToAttachmentList(AttachmentKey> key, T value) {exchange.addToAttachmentList(key, value);} } diff --git a/proteus-core/src/main/java/io/sinistral/proteus/server/ServerResponse.java b/proteus-core/src/main/java/io/sinistral/proteus/server/ServerResponse.java index be160af..8366f49 100644 --- a/proteus-core/src/main/java/io/sinistral/proteus/server/ServerResponse.java +++ b/proteus-core/src/main/java/io/sinistral/proteus/server/ServerResponse.java @@ -555,6 +555,7 @@ public ServerResponse withIoCallback(IoCallback ioCallback) return this; } + public void send(final HttpServerExchange exchange) throws RuntimeException { send(null, exchange); @@ -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); @@ -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(); + } } }