Polish + minor HttpHandler refactoring
CompositeHttpHandler is public and called ContextPathCompositeHandler. Also an overhaul of the Javadoc on HttpHandler, WebHttpHandlerAdapter, and ContextPathCompositeHandler.
This commit is contained in:
@@ -1,73 +0,0 @@
|
||||
|
||||
package org.springframework.http.server.reactive;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
/**
|
||||
* Composite HttpHandler that selects the handler to use by context path.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
class CompositeHttpHandler implements HttpHandler {
|
||||
|
||||
private final Map<String, HttpHandler> handlerMap;
|
||||
|
||||
public CompositeHttpHandler(Map<String, ? extends HttpHandler> handlerMap) {
|
||||
Assert.notEmpty(handlerMap, "Handler map must not be empty");
|
||||
this.handlerMap = initHandlerMap(handlerMap);
|
||||
}
|
||||
|
||||
private static Map<String, HttpHandler> initHandlerMap(
|
||||
Map<String, ? extends HttpHandler> inputMap) {
|
||||
inputMap.keySet().stream().forEach(CompositeHttpHandler::validateContextPath);
|
||||
return new LinkedHashMap<>(inputMap);
|
||||
}
|
||||
|
||||
private static void validateContextPath(String contextPath) {
|
||||
Assert.hasText(contextPath, "Context path must not be empty");
|
||||
if (!contextPath.equals("/")) {
|
||||
Assert.isTrue(contextPath.startsWith("/"),
|
||||
"Context path must begin with '/'");
|
||||
Assert.isTrue(!contextPath.endsWith("/"),
|
||||
"Context path must not end with '/'");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Void> handle(ServerHttpRequest request, ServerHttpResponse response) {
|
||||
String path = getPathToUse(request);
|
||||
return this.handlerMap.entrySet().stream().filter(
|
||||
entry -> path.startsWith(entry.getKey())).findFirst().map(entry -> {
|
||||
// Preserve "native" contextPath from underlying request..
|
||||
String contextPath = request.getContextPath() + entry.getKey();
|
||||
ServerHttpRequest mutatedRequest = request.mutate().contextPath(
|
||||
contextPath).build();
|
||||
HttpHandler handler = entry.getValue();
|
||||
return handler.handle(mutatedRequest, response);
|
||||
}).orElseGet(() -> {
|
||||
response.setStatusCode(HttpStatus.NOT_FOUND);
|
||||
response.setComplete();
|
||||
return Mono.empty();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Strip the context path from the native request, if any.
|
||||
*/
|
||||
private String getPathToUse(ServerHttpRequest request) {
|
||||
String path = request.getURI().getRawPath();
|
||||
String contextPath = request.getContextPath();
|
||||
if (!StringUtils.hasText(contextPath)) {
|
||||
return path;
|
||||
}
|
||||
int contextLength = contextPath.length();
|
||||
return (path.length() > contextLength ? path.substring(contextLength) : "");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
|
||||
package org.springframework.http.server.reactive;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
/**
|
||||
* {@code HttpHandler} delegating requests to one of several {@code HttpHandler}'s
|
||||
* based on simple, prefix-based mappings.
|
||||
*
|
||||
* <p>This is intended as a coarse-grained mechanism for delegating requests to
|
||||
* one of several applications -- each represented by an {@code HttpHandler}, with
|
||||
* the application "context path" (the prefix-based mapping) exposed via
|
||||
* {@link ServerHttpRequest#getContextPath()}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 5.0
|
||||
*/
|
||||
public class ContextPathCompositeHandler implements HttpHandler {
|
||||
|
||||
private final Map<String, HttpHandler> handlerMap;
|
||||
|
||||
|
||||
public ContextPathCompositeHandler(Map<String, ? extends HttpHandler> handlerMap) {
|
||||
Assert.notEmpty(handlerMap, "Handler map must not be empty");
|
||||
this.handlerMap = initHandlers(handlerMap);
|
||||
}
|
||||
|
||||
private static Map<String, HttpHandler> initHandlers(Map<String, ? extends HttpHandler> map) {
|
||||
map.keySet().forEach(ContextPathCompositeHandler::assertValidContextPath);
|
||||
return new LinkedHashMap<>(map);
|
||||
}
|
||||
|
||||
private static void assertValidContextPath(String contextPath) {
|
||||
Assert.hasText(contextPath, "Context path must not be empty");
|
||||
if (contextPath.equals("/")) {
|
||||
return;
|
||||
}
|
||||
Assert.isTrue(contextPath.startsWith("/"), "Context path must begin with '/'");
|
||||
Assert.isTrue(!contextPath.endsWith("/"), "Context path must not end with '/'");
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Mono<Void> handle(ServerHttpRequest request, ServerHttpResponse response) {
|
||||
String path = getPathWithinApplication(request);
|
||||
return this.handlerMap.entrySet().stream()
|
||||
.filter(entry -> path.startsWith(entry.getKey()))
|
||||
.findFirst()
|
||||
.map(entry -> {
|
||||
String contextPath = request.getContextPath() + entry.getKey();
|
||||
ServerHttpRequest newRequest = request.mutate().contextPath(contextPath).build();
|
||||
return entry.getValue().handle(newRequest, response);
|
||||
})
|
||||
.orElseGet(() -> {
|
||||
response.setStatusCode(HttpStatus.NOT_FOUND);
|
||||
response.setComplete();
|
||||
return Mono.empty();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the path within the "native" context path of the underlying server,
|
||||
* for example when running on a Servlet container.
|
||||
*/
|
||||
private String getPathWithinApplication(ServerHttpRequest request) {
|
||||
String path = request.getURI().getRawPath();
|
||||
String contextPath = request.getContextPath();
|
||||
if (!StringUtils.hasText(contextPath)) {
|
||||
return path;
|
||||
}
|
||||
int length = contextPath.length();
|
||||
return (path.length() > length ? path.substring(length) : "");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,35 +16,38 @@
|
||||
|
||||
package org.springframework.http.server.reactive;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
/**
|
||||
* Contract for handling HTTP requests in a non-blocking way.
|
||||
* Lowest level contract for reactive HTTP request handling that serves as a
|
||||
* common denominator across different runtimes.
|
||||
*
|
||||
* <p>Higher-level, but still generic, building blocks for applications such as
|
||||
* {@code WebFilter}, {@code WebSession}, {@code ServerWebExchange}, and others
|
||||
* are available in the {@link org.springframework.web.server} package.
|
||||
*
|
||||
* <p>Application level programming models such as annotated controllers and
|
||||
* functional handlers are available in the {@code spring-webflux} module.
|
||||
*
|
||||
* <p>Typically an {@link HttpHandler} represents an entire application with
|
||||
* higher-level programming models bridged via
|
||||
* {@link org.springframework.web.server.adapter.WebHttpHandlerBuilder
|
||||
* WebHttpHandlerBuilder}. Multiple applications at unique context paths can be
|
||||
* plugged in with the help of the {@link ContextPathCompositeHandler}.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 5.0
|
||||
* @see ContextPathCompositeHandler
|
||||
*/
|
||||
public interface HttpHandler {
|
||||
|
||||
/**
|
||||
* Handle the given request and generate a response.
|
||||
* @param request current HTTP request
|
||||
* @param response current HTTP response
|
||||
* @return {@code Mono<Void>} to indicate when request handling is complete
|
||||
* Handle the given request and write to the response.
|
||||
* @param request current request
|
||||
* @param response current response
|
||||
* @return indicates completion of request handling
|
||||
*/
|
||||
Mono<Void> handle(ServerHttpRequest request, ServerHttpResponse response);
|
||||
|
||||
/**
|
||||
* Return a composite {@link HttpHandler} that maps multiple
|
||||
* {@link HttpHandler}s each mapped to a distinct context path.
|
||||
* @param handlerMap the source handler map
|
||||
* @return a composite {@link HttpHandler}
|
||||
*/
|
||||
static HttpHandler of(Map<String, ? extends HttpHandler> handlerMap) {
|
||||
return new CompositeHttpHandler(handlerMap);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -48,6 +48,7 @@ public class ReactorHttpHandlerAdapter
|
||||
this.httpHandler = httpHandler;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Mono<Void> apply(HttpServerRequest request, HttpServerResponse response) {
|
||||
|
||||
|
||||
@@ -42,7 +42,7 @@ import rx.RxReactiveStreams;
|
||||
*/
|
||||
public class RxNettyHttpHandlerAdapter implements RequestHandler<ByteBuf, ByteBuf> {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(ReactorHttpHandlerAdapter.class);
|
||||
private static final Log logger = LogFactory.getLog(RxNettyHttpHandlerAdapter.class);
|
||||
|
||||
|
||||
private final HttpHandler httpHandler;
|
||||
|
||||
@@ -48,7 +48,7 @@ import org.springframework.util.Assert;
|
||||
@SuppressWarnings("serial")
|
||||
public class ServletHttpHandlerAdapter implements Servlet {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(ReactorHttpHandlerAdapter.class);
|
||||
private static final Log logger = LogFactory.getLog(ServletHttpHandlerAdapter.class);
|
||||
|
||||
|
||||
private static final int DEFAULT_BUFFER_SIZE = 8192;
|
||||
@@ -110,12 +110,18 @@ public class ServletHttpHandlerAdapter implements Servlet {
|
||||
this.httpHandler.handle(httpRequest, httpResponse).subscribe(subscriber);
|
||||
}
|
||||
|
||||
protected ServerHttpRequest createRequest(HttpServletRequest request, AsyncContext context) throws IOException {
|
||||
return new ServletServerHttpRequest(request, context, getDataBufferFactory(), getBufferSize());
|
||||
protected ServerHttpRequest createRequest(HttpServletRequest request,
|
||||
AsyncContext context) throws IOException {
|
||||
|
||||
return new ServletServerHttpRequest(
|
||||
request, context, getDataBufferFactory(), getBufferSize());
|
||||
}
|
||||
|
||||
protected ServerHttpResponse createResponse(HttpServletResponse response, AsyncContext context) throws IOException {
|
||||
return new ServletServerHttpResponse(response, context, getDataBufferFactory(), getBufferSize());
|
||||
protected ServerHttpResponse createResponse(HttpServletResponse response,
|
||||
AsyncContext context) throws IOException {
|
||||
|
||||
return new ServletServerHttpResponse(
|
||||
response, context, getDataBufferFactory(), getBufferSize());
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -37,7 +37,7 @@ import org.springframework.util.Assert;
|
||||
*/
|
||||
public class UndertowHttpHandlerAdapter implements io.undertow.server.HttpHandler {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(ReactorHttpHandlerAdapter.class);
|
||||
private static final Log logger = LogFactory.getLog(UndertowHttpHandlerAdapter.class);
|
||||
|
||||
|
||||
private final HttpHandler httpHandler;
|
||||
|
||||
@@ -35,24 +35,24 @@ import org.springframework.web.server.session.DefaultWebSessionManager;
|
||||
import org.springframework.web.server.session.WebSessionManager;
|
||||
|
||||
/**
|
||||
* Builder for an {@link HttpHandler} that adapts to a target {@link WebHandler}
|
||||
* along with a chain of {@link WebFilter}s and a set of
|
||||
* {@link WebExceptionHandler}s.
|
||||
* This builder has two purposes.
|
||||
*
|
||||
* <p>Example usage:
|
||||
* <pre>
|
||||
* WebFilter filter = ... ;
|
||||
* WebHandler webHandler = ... ;
|
||||
* WebExceptionHandler exceptionHandler = ...;
|
||||
* <p>One is to assemble a processing chain that consists of a target
|
||||
* {@link WebHandler}, then decorated with a set of {@link WebFilter}'s, then
|
||||
* further decorated with a set of {@link WebExceptionHandler}'s.
|
||||
*
|
||||
* HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler(webHandler)
|
||||
* .filters(filter)
|
||||
* .exceptionHandlers(exceptionHandler)
|
||||
* .build();
|
||||
* </pre>
|
||||
* <p>The second purpose is to adapt the resulting processing chain to an
|
||||
* {@link HttpHandler} -- the lowest level reactive HTTP handling abstraction,
|
||||
* which can then be used with any of the supported runtimes. The adaptation
|
||||
* is done with the help of {@link HttpWebHandlerAdapter}.
|
||||
*
|
||||
* <p>The processing chain can be assembled manually via builder methods, or
|
||||
* detected from Spring configuration via
|
||||
* {@link #applicationContext(ApplicationContext)}, or a mix of both.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 5.0
|
||||
* @see HttpWebHandlerAdapter
|
||||
*/
|
||||
public class WebHttpHandlerBuilder {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user