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:
parent
57d2fcef55
commit
d39da1d06d
|
@ -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;
|
package org.springframework.http.server.reactive;
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import reactor.core.publisher.Mono;
|
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 Arjen Poutsma
|
||||||
* @author Rossen Stoyanchev
|
* @author Rossen Stoyanchev
|
||||||
* @since 5.0
|
* @since 5.0
|
||||||
|
* @see ContextPathCompositeHandler
|
||||||
*/
|
*/
|
||||||
public interface HttpHandler {
|
public interface HttpHandler {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle the given request and generate a response.
|
* Handle the given request and write to the response.
|
||||||
* @param request current HTTP request
|
* @param request current request
|
||||||
* @param response current HTTP response
|
* @param response current response
|
||||||
* @return {@code Mono<Void>} to indicate when request handling is complete
|
* @return indicates completion of request handling
|
||||||
*/
|
*/
|
||||||
Mono<Void> handle(ServerHttpRequest request, ServerHttpResponse response);
|
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;
|
this.httpHandler = httpHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Mono<Void> apply(HttpServerRequest request, HttpServerResponse response) {
|
public Mono<Void> apply(HttpServerRequest request, HttpServerResponse response) {
|
||||||
|
|
||||||
|
|
|
@ -42,7 +42,7 @@ import rx.RxReactiveStreams;
|
||||||
*/
|
*/
|
||||||
public class RxNettyHttpHandlerAdapter implements RequestHandler<ByteBuf, ByteBuf> {
|
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;
|
private final HttpHandler httpHandler;
|
||||||
|
|
|
@ -48,7 +48,7 @@ import org.springframework.util.Assert;
|
||||||
@SuppressWarnings("serial")
|
@SuppressWarnings("serial")
|
||||||
public class ServletHttpHandlerAdapter implements Servlet {
|
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;
|
private static final int DEFAULT_BUFFER_SIZE = 8192;
|
||||||
|
@ -110,12 +110,18 @@ public class ServletHttpHandlerAdapter implements Servlet {
|
||||||
this.httpHandler.handle(httpRequest, httpResponse).subscribe(subscriber);
|
this.httpHandler.handle(httpRequest, httpResponse).subscribe(subscriber);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected ServerHttpRequest createRequest(HttpServletRequest request, AsyncContext context) throws IOException {
|
protected ServerHttpRequest createRequest(HttpServletRequest request,
|
||||||
return new ServletServerHttpRequest(request, context, getDataBufferFactory(), getBufferSize());
|
AsyncContext context) throws IOException {
|
||||||
|
|
||||||
|
return new ServletServerHttpRequest(
|
||||||
|
request, context, getDataBufferFactory(), getBufferSize());
|
||||||
}
|
}
|
||||||
|
|
||||||
protected ServerHttpResponse createResponse(HttpServletResponse response, AsyncContext context) throws IOException {
|
protected ServerHttpResponse createResponse(HttpServletResponse response,
|
||||||
return new ServletServerHttpResponse(response, context, getDataBufferFactory(), getBufferSize());
|
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 {
|
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;
|
private final HttpHandler httpHandler;
|
||||||
|
|
|
@ -35,24 +35,24 @@ import org.springframework.web.server.session.DefaultWebSessionManager;
|
||||||
import org.springframework.web.server.session.WebSessionManager;
|
import org.springframework.web.server.session.WebSessionManager;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Builder for an {@link HttpHandler} that adapts to a target {@link WebHandler}
|
* This builder has two purposes.
|
||||||
* along with a chain of {@link WebFilter}s and a set of
|
|
||||||
* {@link WebExceptionHandler}s.
|
|
||||||
*
|
*
|
||||||
* <p>Example usage:
|
* <p>One is to assemble a processing chain that consists of a target
|
||||||
* <pre>
|
* {@link WebHandler}, then decorated with a set of {@link WebFilter}'s, then
|
||||||
* WebFilter filter = ... ;
|
* further decorated with a set of {@link WebExceptionHandler}'s.
|
||||||
* WebHandler webHandler = ... ;
|
|
||||||
* WebExceptionHandler exceptionHandler = ...;
|
|
||||||
*
|
*
|
||||||
* HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler(webHandler)
|
* <p>The second purpose is to adapt the resulting processing chain to an
|
||||||
* .filters(filter)
|
* {@link HttpHandler} -- the lowest level reactive HTTP handling abstraction,
|
||||||
* .exceptionHandlers(exceptionHandler)
|
* which can then be used with any of the supported runtimes. The adaptation
|
||||||
* .build();
|
* is done with the help of {@link HttpWebHandlerAdapter}.
|
||||||
* </pre>
|
*
|
||||||
|
* <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
|
* @author Rossen Stoyanchev
|
||||||
* @since 5.0
|
* @since 5.0
|
||||||
|
* @see HttpWebHandlerAdapter
|
||||||
*/
|
*/
|
||||||
public class WebHttpHandlerBuilder {
|
public class WebHttpHandlerBuilder {
|
||||||
|
|
||||||
|
|
|
@ -34,26 +34,27 @@ import static org.junit.Assert.assertTrue;
|
||||||
import static org.junit.Assert.fail;
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unit tests for {@link HttpHandler}.
|
* Unit tests for {@link ContextPathCompositeHandler}.
|
||||||
*
|
*
|
||||||
* @author Rossen Stoyanchev
|
* @author Rossen Stoyanchev
|
||||||
*/
|
*/
|
||||||
public class HttpHandlerTests {
|
public class ContextPathCompositeHandlerTests {
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void invalidContextPath() {
|
public void invalidContextPath() {
|
||||||
testInvalidContextPath(" ", "Context path must not be empty");
|
testInvalid(" ", "Context path must not be empty");
|
||||||
testInvalidContextPath("path", "Context path must begin with '/'");
|
testInvalid("path", "Context path must begin with '/'");
|
||||||
testInvalidContextPath("/path/", "Context path must not end with '/'");
|
testInvalid("/path/", "Context path must not end with '/'");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void testInvalidContextPath(String contextPath, String errorMessage) {
|
private void testInvalid(String contextPath, String expectedError) {
|
||||||
try {
|
try {
|
||||||
new TestHttpHandlerAdapter(Collections.singletonMap(contextPath, new TestHttpHandler()));
|
new ContextPathCompositeHandler(Collections.singletonMap(contextPath, new TestHttpHandler()));
|
||||||
fail();
|
fail();
|
||||||
}
|
}
|
||||||
catch (IllegalArgumentException ex) {
|
catch (IllegalArgumentException ex) {
|
||||||
assertEquals(errorMessage, ex.getMessage());
|
assertEquals(expectedError, ex.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -68,7 +69,7 @@ public class HttpHandlerTests {
|
||||||
map.put("/another/path", handler2);
|
map.put("/another/path", handler2);
|
||||||
map.put("/yet/another/path", handler3);
|
map.put("/yet/another/path", handler3);
|
||||||
|
|
||||||
testPath("/another/path/and/more", map);
|
testHandle("/another/path/and/more", map);
|
||||||
|
|
||||||
assertInvoked(handler2, "/another/path");
|
assertInvoked(handler2, "/another/path");
|
||||||
assertNotInvoked(handler1, handler3);
|
assertNotInvoked(handler1, handler3);
|
||||||
|
@ -85,7 +86,7 @@ public class HttpHandlerTests {
|
||||||
map.put("/another/path", handler2);
|
map.put("/another/path", handler2);
|
||||||
map.put("/yet/another/path", handler3);
|
map.put("/yet/another/path", handler3);
|
||||||
|
|
||||||
testPath("/path", map);
|
testHandle("/path", map);
|
||||||
|
|
||||||
assertInvoked(handler1, "/path");
|
assertInvoked(handler1, "/path");
|
||||||
assertNotInvoked(handler2, handler3);
|
assertNotInvoked(handler2, handler3);
|
||||||
|
@ -101,7 +102,7 @@ public class HttpHandlerTests {
|
||||||
TestHttpHandler handler = new TestHttpHandler();
|
TestHttpHandler handler = new TestHttpHandler();
|
||||||
Map<String, HttpHandler> map = Collections.singletonMap("/another/path", handler);
|
Map<String, HttpHandler> map = Collections.singletonMap("/another/path", handler);
|
||||||
|
|
||||||
new TestHttpHandlerAdapter(map).handle(request);
|
new ContextPathCompositeHandler(map).handle(request, new MockServerHttpResponse());
|
||||||
|
|
||||||
assertTrue(handler.wasInvoked());
|
assertTrue(handler.wasInvoked());
|
||||||
assertEquals("/yet/another/path", handler.getRequest().getContextPath());
|
assertEquals("/yet/another/path", handler.getRequest().getContextPath());
|
||||||
|
@ -116,16 +117,18 @@ public class HttpHandlerTests {
|
||||||
map.put("/path", handler1);
|
map.put("/path", handler1);
|
||||||
map.put("/another/path", handler2);
|
map.put("/another/path", handler2);
|
||||||
|
|
||||||
ServerHttpResponse response = testPath("/yet/another/path", map);
|
ServerHttpResponse response = testHandle("/yet/another/path", map);
|
||||||
|
|
||||||
assertNotInvoked(handler1, handler2);
|
assertNotInvoked(handler1, handler2);
|
||||||
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
|
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private ServerHttpResponse testPath(String path, Map<String, HttpHandler> handlerMap) {
|
private ServerHttpResponse testHandle(String pathToHandle, Map<String, HttpHandler> handlerMap) {
|
||||||
TestHttpHandlerAdapter adapter = new TestHttpHandlerAdapter(handlerMap);
|
ServerHttpRequest request = MockServerHttpRequest.get(pathToHandle).build();
|
||||||
return adapter.handle(path);
|
ServerHttpResponse response = new MockServerHttpResponse();
|
||||||
|
new ContextPathCompositeHandler(handlerMap).handle(request, response);
|
||||||
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void assertInvoked(TestHttpHandler handler, String contextPath) {
|
private void assertInvoked(TestHttpHandler handler, String contextPath) {
|
||||||
|
@ -138,30 +141,6 @@ public class HttpHandlerTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@SuppressWarnings("WeakerAccess")
|
|
||||||
private static class TestHttpHandlerAdapter {
|
|
||||||
|
|
||||||
|
|
||||||
private final HttpHandler httpHandler;
|
|
||||||
|
|
||||||
|
|
||||||
public TestHttpHandlerAdapter(Map<String, HttpHandler> handlerMap) {
|
|
||||||
this.httpHandler = HttpHandler.of(handlerMap);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ServerHttpResponse handle(String path) {
|
|
||||||
ServerHttpRequest request = MockServerHttpRequest.get(path).build();
|
|
||||||
return handle(request);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ServerHttpResponse handle(ServerHttpRequest request) {
|
|
||||||
ServerHttpResponse response = new MockServerHttpResponse();
|
|
||||||
this.httpHandler.handle(request, response);
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@SuppressWarnings("WeakerAccess")
|
@SuppressWarnings("WeakerAccess")
|
||||||
private static class TestHttpHandler implements HttpHandler {
|
private static class TestHttpHandler implements HttpHandler {
|
||||||
|
|
|
@ -19,6 +19,7 @@ package org.springframework.http.server.reactive.bootstrap;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.springframework.http.server.reactive.ContextPathCompositeHandler;
|
||||||
import org.springframework.http.server.reactive.HttpHandler;
|
import org.springframework.http.server.reactive.HttpHandler;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
|
@ -79,6 +80,11 @@ public abstract class AbstractHttpServer implements HttpServer {
|
||||||
return this.handlerMap;
|
return this.handlerMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected HttpHandler resolveHttpHandler() {
|
||||||
|
return getHttpHandlerMap() != null ?
|
||||||
|
new ContextPathCompositeHandler(getHttpHandlerMap()) : getHttpHandler();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// InitializingBean
|
// InitializingBean
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,7 @@ import org.eclipse.jetty.server.Server;
|
||||||
import org.eclipse.jetty.server.ServerConnector;
|
import org.eclipse.jetty.server.ServerConnector;
|
||||||
import org.eclipse.jetty.servlet.ServletContextHandler;
|
import org.eclipse.jetty.servlet.ServletContextHandler;
|
||||||
import org.eclipse.jetty.servlet.ServletHolder;
|
import org.eclipse.jetty.servlet.ServletHolder;
|
||||||
import org.springframework.http.server.reactive.HttpHandler;
|
|
||||||
import org.springframework.http.server.reactive.JettyHttpHandlerAdapter;
|
import org.springframework.http.server.reactive.JettyHttpHandlerAdapter;
|
||||||
import org.springframework.http.server.reactive.ServletHttpHandlerAdapter;
|
import org.springframework.http.server.reactive.ServletHttpHandlerAdapter;
|
||||||
|
|
||||||
|
@ -53,8 +53,7 @@ public class JettyHttpServer extends AbstractHttpServer {
|
||||||
}
|
}
|
||||||
|
|
||||||
private ServletHttpHandlerAdapter createServletAdapter() {
|
private ServletHttpHandlerAdapter createServletAdapter() {
|
||||||
return new JettyHttpHandlerAdapter(getHttpHandlerMap() != null
|
return new JettyHttpHandlerAdapter(resolveHttpHandler());
|
||||||
? HttpHandler.of(getHttpHandlerMap()) : getHttpHandler());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -21,7 +21,6 @@ import java.util.concurrent.atomic.AtomicReference;
|
||||||
import reactor.core.Loopback;
|
import reactor.core.Loopback;
|
||||||
import reactor.ipc.netty.NettyContext;
|
import reactor.ipc.netty.NettyContext;
|
||||||
|
|
||||||
import org.springframework.http.server.reactive.HttpHandler;
|
|
||||||
import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter;
|
import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -43,8 +42,7 @@ public class ReactorHttpServer extends AbstractHttpServer implements Loopback {
|
||||||
}
|
}
|
||||||
|
|
||||||
private ReactorHttpHandlerAdapter createHttpHandlerAdapter() {
|
private ReactorHttpHandlerAdapter createHttpHandlerAdapter() {
|
||||||
return new ReactorHttpHandlerAdapter(getHttpHandlerMap() != null
|
return new ReactorHttpHandlerAdapter(resolveHttpHandler());
|
||||||
? HttpHandler.of(getHttpHandlerMap()) : getHttpHandler());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -18,7 +18,6 @@ package org.springframework.http.server.reactive.bootstrap;
|
||||||
|
|
||||||
import io.netty.buffer.ByteBuf;
|
import io.netty.buffer.ByteBuf;
|
||||||
|
|
||||||
import org.springframework.http.server.reactive.HttpHandler;
|
|
||||||
import org.springframework.http.server.reactive.RxNettyHttpHandlerAdapter;
|
import org.springframework.http.server.reactive.RxNettyHttpHandlerAdapter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -38,8 +37,7 @@ public class RxNettyHttpServer extends AbstractHttpServer {
|
||||||
}
|
}
|
||||||
|
|
||||||
private RxNettyHttpHandlerAdapter createHttpHandlerAdapter() {
|
private RxNettyHttpHandlerAdapter createHttpHandlerAdapter() {
|
||||||
return new RxNettyHttpHandlerAdapter(getHttpHandlerMap() != null
|
return new RxNettyHttpHandlerAdapter(resolveHttpHandler());
|
||||||
? HttpHandler.of(getHttpHandlerMap()) : getHttpHandler());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -21,7 +21,7 @@ import java.io.File;
|
||||||
import org.apache.catalina.Context;
|
import org.apache.catalina.Context;
|
||||||
import org.apache.catalina.LifecycleException;
|
import org.apache.catalina.LifecycleException;
|
||||||
import org.apache.catalina.startup.Tomcat;
|
import org.apache.catalina.startup.Tomcat;
|
||||||
import org.springframework.http.server.reactive.HttpHandler;
|
|
||||||
import org.springframework.http.server.reactive.ServletHttpHandlerAdapter;
|
import org.springframework.http.server.reactive.ServletHttpHandlerAdapter;
|
||||||
import org.springframework.http.server.reactive.TomcatHttpHandlerAdapter;
|
import org.springframework.http.server.reactive.TomcatHttpHandlerAdapter;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
@ -68,8 +68,7 @@ public class TomcatHttpServer extends AbstractHttpServer {
|
||||||
}
|
}
|
||||||
|
|
||||||
private ServletHttpHandlerAdapter initServletAdapter() {
|
private ServletHttpHandlerAdapter initServletAdapter() {
|
||||||
return new TomcatHttpHandlerAdapter(getHttpHandlerMap() != null
|
return new TomcatHttpHandlerAdapter(resolveHttpHandler());
|
||||||
? HttpHandler.of(getHttpHandlerMap()) : getHttpHandler());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,6 @@ import java.net.InetSocketAddress;
|
||||||
|
|
||||||
import io.undertow.Undertow;
|
import io.undertow.Undertow;
|
||||||
|
|
||||||
import org.springframework.http.server.reactive.HttpHandler;
|
|
||||||
import org.springframework.http.server.reactive.UndertowHttpHandlerAdapter;
|
import org.springframework.http.server.reactive.UndertowHttpHandlerAdapter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -39,8 +38,7 @@ public class UndertowHttpServer extends AbstractHttpServer {
|
||||||
}
|
}
|
||||||
|
|
||||||
private UndertowHttpHandlerAdapter initHttpHandlerAdapter() {
|
private UndertowHttpHandlerAdapter initHttpHandlerAdapter() {
|
||||||
return new UndertowHttpHandlerAdapter(getHttpHandlerMap() != null
|
return new UndertowHttpHandlerAdapter(resolveHttpHandler());
|
||||||
? HttpHandler.of(getHttpHandlerMap()) : getHttpHandler());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -1,4 +1,12 @@
|
||||||
/**
|
/**
|
||||||
* Core interfaces and classes for Spring Web Reactive.
|
* Top-level package for the {@code spring-webflux} module that contains
|
||||||
|
* {@link org.springframework.web.reactive.DispatcherHandler}, the main entry
|
||||||
|
* point for WebFlux server endpoint processing including key contracts used to
|
||||||
|
* map requests to handlers, invoke them, and process the result.
|
||||||
|
*
|
||||||
|
* <p>The module provides two programming models for reactive server endpoints.
|
||||||
|
* One based on annotated {@code @Controller}'s and another based on functional
|
||||||
|
* routing and handling. The module also contains a functional, reactive
|
||||||
|
* {@code WebClient} as well as client and server, reactive WebSocket support.
|
||||||
*/
|
*/
|
||||||
package org.springframework.web.reactive;
|
package org.springframework.web.reactive;
|
||||||
|
|
Loading…
Reference in New Issue