Extract CompositeHttpHandler functionality
Extract CompositeHttpHandler to a package private class and add direct support via `HttpHandler.of(...)`. This removes the need for the `HttpHandlerAdapterSupport` class.
This commit is contained in:
parent
170057005e
commit
57d2fcef55
|
|
@ -0,0 +1,73 @@
|
|||
|
||||
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) : "");
|
||||
}
|
||||
}
|
||||
|
|
@ -16,12 +16,15 @@
|
|||
|
||||
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.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 5.0
|
||||
*/
|
||||
public interface HttpHandler {
|
||||
|
|
@ -34,4 +37,14 @@ public interface HttpHandler {
|
|||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,139 +0,0 @@
|
|||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.http.server.reactive;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Base class for adapters from native runtime HTTP request handlers to a
|
||||
* reactive {@link HttpHandler} contract.
|
||||
*
|
||||
* <p>Provides support for delegating incoming requests to a single or multiple
|
||||
* {@link HttpHandler}s each mapped to a distinct context path. In either case
|
||||
* sub-classes simply use {@link #getHttpHandler()} to access the handler to
|
||||
* delegate incoming requests to.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 5.0
|
||||
*/
|
||||
public abstract class HttpHandlerAdapterSupport {
|
||||
|
||||
protected final Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
private final HttpHandler httpHandler;
|
||||
|
||||
|
||||
/**
|
||||
* Constructor with a single {@code HttpHandler} to use for all requests.
|
||||
* @param httpHandler the handler to use
|
||||
*/
|
||||
public HttpHandlerAdapterSupport(HttpHandler httpHandler) {
|
||||
Assert.notNull(httpHandler, "'httpHandler' is required");
|
||||
this.httpHandler = httpHandler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor with {@code HttpHandler}s mapped to distinct context paths.
|
||||
* Context paths must start but not end with "/" and must be encoded.
|
||||
* <p>At request time context paths are compared against the "raw" path of
|
||||
* the request URI in the order in which they are provided. The first one
|
||||
* to match is chosen. If none match the response status is set to 404.
|
||||
* @param handlerMap map with context paths and {@code HttpHandler}s.
|
||||
* @see ServerHttpRequest#getContextPath()
|
||||
*/
|
||||
public HttpHandlerAdapterSupport(Map<String, HttpHandler> handlerMap) {
|
||||
this.httpHandler = new CompositeHttpHandler(handlerMap);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the {@link HttpHandler} to delegate incoming requests to.
|
||||
*/
|
||||
public HttpHandler getHttpHandler() {
|
||||
return this.httpHandler;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Composite HttpHandler that selects the handler to use by context path.
|
||||
*/
|
||||
private static class CompositeHttpHandler implements HttpHandler {
|
||||
|
||||
private final Map<String, HttpHandler> handlerMap;
|
||||
|
||||
public CompositeHttpHandler(Map<String, HttpHandler> handlerMap) {
|
||||
Assert.notEmpty(handlerMap, "Handler map must not be empty");
|
||||
this.handlerMap = initHandlerMap(handlerMap);
|
||||
}
|
||||
|
||||
private static Map<String, HttpHandler> initHandlerMap(Map<String, 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) : "");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -18,7 +18,6 @@ package org.springframework.http.server.reactive;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.Map;
|
||||
import javax.servlet.AsyncContext;
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
|
@ -31,7 +30,7 @@ import org.springframework.core.io.buffer.DataBufferFactory;
|
|||
/**
|
||||
* {@link ServletHttpHandlerAdapter} extension that uses Jetty APIs for writing
|
||||
* to the response with {@link ByteBuffer}.
|
||||
*
|
||||
*
|
||||
* @author Violeta Georgieva
|
||||
* @since 5.0
|
||||
*/
|
||||
|
|
@ -43,10 +42,6 @@ public class JettyHttpHandlerAdapter extends ServletHttpHandlerAdapter {
|
|||
super(httpHandler);
|
||||
}
|
||||
|
||||
public JettyHttpHandlerAdapter(Map<String, HttpHandler> handlerMap) {
|
||||
super(handlerMap);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected ServerHttpResponse createResponse(HttpServletResponse response,
|
||||
|
|
@ -73,4 +68,4 @@ public class JettyHttpHandlerAdapter extends ServletHttpHandlerAdapter {
|
|||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,7 +16,6 @@
|
|||
|
||||
package org.springframework.http.server.reactive;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
import io.netty.handler.codec.http.HttpResponseStatus;
|
||||
|
|
@ -24,7 +23,10 @@ import reactor.core.publisher.Mono;
|
|||
import reactor.ipc.netty.http.server.HttpServerRequest;
|
||||
import reactor.ipc.netty.http.server.HttpServerResponse;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.core.io.buffer.NettyDataBufferFactory;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Adapt {@link HttpHandler} to the Reactor Netty channel handling function.
|
||||
|
|
@ -32,19 +34,20 @@ import org.springframework.core.io.buffer.NettyDataBufferFactory;
|
|||
* @author Stephane Maldini
|
||||
* @since 5.0
|
||||
*/
|
||||
public class ReactorHttpHandlerAdapter extends HttpHandlerAdapterSupport
|
||||
public class ReactorHttpHandlerAdapter
|
||||
implements BiFunction<HttpServerRequest, HttpServerResponse, Mono<Void>> {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(ReactorHttpHandlerAdapter.class);
|
||||
|
||||
|
||||
private final HttpHandler httpHandler;
|
||||
|
||||
|
||||
public ReactorHttpHandlerAdapter(HttpHandler httpHandler) {
|
||||
super(httpHandler);
|
||||
Assert.notNull(httpHandler, "HttpHandler must not be null");
|
||||
this.httpHandler = httpHandler;
|
||||
}
|
||||
|
||||
public ReactorHttpHandlerAdapter(Map<String, HttpHandler> handlerMap) {
|
||||
super(handlerMap);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Mono<Void> apply(HttpServerRequest request, HttpServerResponse response) {
|
||||
|
||||
|
|
@ -52,7 +55,7 @@ public class ReactorHttpHandlerAdapter extends HttpHandlerAdapterSupport
|
|||
ReactorServerHttpRequest req = new ReactorServerHttpRequest(request, bufferFactory);
|
||||
ReactorServerHttpResponse resp = new ReactorServerHttpResponse(response, bufferFactory);
|
||||
|
||||
return getHttpHandler().handle(req, resp)
|
||||
return this.httpHandler.handle(req, resp)
|
||||
.otherwise(ex -> {
|
||||
logger.error("Could not complete request", ex);
|
||||
response.status(HttpResponseStatus.INTERNAL_SERVER_ERROR);
|
||||
|
|
|
|||
|
|
@ -17,39 +17,40 @@
|
|||
package org.springframework.http.server.reactive;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.SocketAddress;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.reactivestreams.Publisher;
|
||||
import org.springframework.core.io.buffer.NettyDataBufferFactory;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.ByteBufAllocator;
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.handler.codec.http.HttpResponseStatus;
|
||||
import io.reactivex.netty.protocol.http.server.HttpServerRequest;
|
||||
import io.reactivex.netty.protocol.http.server.HttpServerResponse;
|
||||
import io.reactivex.netty.protocol.http.server.RequestHandler;
|
||||
import org.reactivestreams.Publisher;
|
||||
import reactor.core.publisher.Mono;
|
||||
import rx.Observable;
|
||||
import rx.RxReactiveStreams;
|
||||
|
||||
import org.springframework.core.io.buffer.NettyDataBufferFactory;
|
||||
|
||||
/**
|
||||
* Adapt {@link HttpHandler} to the RxNetty {@link RequestHandler}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 5.0
|
||||
*/
|
||||
public class RxNettyHttpHandlerAdapter extends HttpHandlerAdapterSupport
|
||||
implements RequestHandler<ByteBuf, ByteBuf> {
|
||||
public class RxNettyHttpHandlerAdapter implements RequestHandler<ByteBuf, ByteBuf> {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(ReactorHttpHandlerAdapter.class);
|
||||
|
||||
|
||||
private final HttpHandler httpHandler;
|
||||
|
||||
|
||||
public RxNettyHttpHandlerAdapter(HttpHandler httpHandler) {
|
||||
super(httpHandler);
|
||||
}
|
||||
|
||||
public RxNettyHttpHandlerAdapter(Map<String, HttpHandler> handlerMap) {
|
||||
super(handlerMap);
|
||||
Assert.notNull(httpHandler, "HttpHandler must not be null");
|
||||
this.httpHandler = httpHandler;
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -64,7 +65,7 @@ public class RxNettyHttpHandlerAdapter extends HttpHandlerAdapterSupport
|
|||
RxNettyServerHttpRequest request = new RxNettyServerHttpRequest(nativeRequest, bufferFactory, remoteAddress);
|
||||
RxNettyServerHttpResponse response = new RxNettyServerHttpResponse(nativeResponse, bufferFactory);
|
||||
|
||||
Publisher<Void> result = getHttpHandler().handle(request, response)
|
||||
Publisher<Void> result = this.httpHandler.handle(request, response)
|
||||
.otherwise(ex -> {
|
||||
logger.error("Could not complete request", ex);
|
||||
nativeResponse.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
|
||||
|
|
|
|||
|
|
@ -17,7 +17,6 @@
|
|||
package org.springframework.http.server.reactive;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
import javax.servlet.AsyncContext;
|
||||
import javax.servlet.Servlet;
|
||||
import javax.servlet.ServletConfig;
|
||||
|
|
@ -28,6 +27,8 @@ import javax.servlet.http.HttpServlet;
|
|||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.reactivestreams.Subscriber;
|
||||
import org.reactivestreams.Subscription;
|
||||
|
||||
|
|
@ -45,24 +46,27 @@ import org.springframework.util.Assert;
|
|||
*/
|
||||
@WebServlet(asyncSupported = true)
|
||||
@SuppressWarnings("serial")
|
||||
public class ServletHttpHandlerAdapter extends HttpHandlerAdapterSupport implements Servlet {
|
||||
public class ServletHttpHandlerAdapter implements Servlet {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(ReactorHttpHandlerAdapter.class);
|
||||
|
||||
|
||||
private static final int DEFAULT_BUFFER_SIZE = 8192;
|
||||
|
||||
|
||||
private final HttpHandler httpHandler;
|
||||
|
||||
private int bufferSize = DEFAULT_BUFFER_SIZE;
|
||||
|
||||
|
||||
// Servlet is based on blocking I/O, hence the usage of non-direct, heap-based buffers
|
||||
// (i.e. 'false' as constructor argument)
|
||||
private DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory(false);
|
||||
|
||||
|
||||
public ServletHttpHandlerAdapter(HttpHandler httpHandler) {
|
||||
super(httpHandler);
|
||||
}
|
||||
|
||||
public ServletHttpHandlerAdapter(Map<String, HttpHandler> handlerMap) {
|
||||
super(handlerMap);
|
||||
Assert.notNull(httpHandler, "HttpHandler must not be null");
|
||||
this.httpHandler = httpHandler;
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -103,7 +107,7 @@ public class ServletHttpHandlerAdapter extends HttpHandlerAdapterSupport impleme
|
|||
ServerHttpResponse httpResponse = createResponse(((HttpServletResponse) response), asyncContext);
|
||||
|
||||
HandlerResultSubscriber subscriber = new HandlerResultSubscriber(asyncContext);
|
||||
getHttpHandler().handle(httpRequest, httpResponse).subscribe(subscriber);
|
||||
this.httpHandler.handle(httpRequest, httpResponse).subscribe(subscriber);
|
||||
}
|
||||
|
||||
protected ServerHttpRequest createRequest(HttpServletRequest request, AsyncContext context) throws IOException {
|
||||
|
|
|
|||
|
|
@ -18,7 +18,6 @@ package org.springframework.http.server.reactive;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.Map;
|
||||
import javax.servlet.AsyncContext;
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
|
@ -45,10 +44,6 @@ public class TomcatHttpHandlerAdapter extends ServletHttpHandlerAdapter {
|
|||
super(httpHandler);
|
||||
}
|
||||
|
||||
public TomcatHttpHandlerAdapter(Map<String, HttpHandler> handlerMap) {
|
||||
super(handlerMap);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected ServerHttpRequest createRequest(HttpServletRequest request, AsyncContext cxt) throws IOException {
|
||||
|
|
@ -106,4 +101,4 @@ public class TomcatHttpHandlerAdapter extends ServletHttpHandlerAdapter {
|
|||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,9 +16,10 @@
|
|||
|
||||
package org.springframework.http.server.reactive;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import io.undertow.server.HttpServerExchange;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.reactivestreams.Subscriber;
|
||||
import org.reactivestreams.Subscription;
|
||||
|
||||
|
|
@ -34,18 +35,19 @@ import org.springframework.util.Assert;
|
|||
* @author Arjen Poutsma
|
||||
* @since 5.0
|
||||
*/
|
||||
public class UndertowHttpHandlerAdapter extends HttpHandlerAdapterSupport
|
||||
implements io.undertow.server.HttpHandler {
|
||||
public class UndertowHttpHandlerAdapter implements io.undertow.server.HttpHandler {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(ReactorHttpHandlerAdapter.class);
|
||||
|
||||
|
||||
private final HttpHandler httpHandler;
|
||||
|
||||
private DataBufferFactory bufferFactory = new DefaultDataBufferFactory(false);
|
||||
|
||||
|
||||
public UndertowHttpHandlerAdapter(HttpHandler httpHandler) {
|
||||
super(httpHandler);
|
||||
}
|
||||
|
||||
public UndertowHttpHandlerAdapter(Map<String, HttpHandler> handlerMap) {
|
||||
super(handlerMap);
|
||||
Assert.notNull(httpHandler, "HttpHandler must not be null");
|
||||
this.httpHandler = httpHandler;
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -66,7 +68,7 @@ public class UndertowHttpHandlerAdapter extends HttpHandlerAdapterSupport
|
|||
ServerHttpResponse response = new UndertowServerHttpResponse(exchange, getDataBufferFactory());
|
||||
|
||||
HandlerResultSubscriber resultSubscriber = new HandlerResultSubscriber(exchange);
|
||||
getHttpHandler().handle(request, response).subscribe(resultSubscriber);
|
||||
this.httpHandler.handle(request, response).subscribe(resultSubscriber);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -34,11 +34,11 @@ import static org.junit.Assert.assertTrue;
|
|||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link HttpHandlerAdapterSupport}.
|
||||
* Unit tests for {@link HttpHandler}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class HttpHandlerAdapterSupportTests {
|
||||
public class HttpHandlerTests {
|
||||
|
||||
@Test
|
||||
public void invalidContextPath() {
|
||||
|
|
@ -139,10 +139,14 @@ public class HttpHandlerAdapterSupportTests {
|
|||
|
||||
|
||||
@SuppressWarnings("WeakerAccess")
|
||||
private static class TestHttpHandlerAdapter extends HttpHandlerAdapterSupport {
|
||||
private static class TestHttpHandlerAdapter {
|
||||
|
||||
|
||||
private final HttpHandler httpHandler;
|
||||
|
||||
|
||||
public TestHttpHandlerAdapter(Map<String, HttpHandler> handlerMap) {
|
||||
super(handlerMap);
|
||||
this.httpHandler = HttpHandler.of(handlerMap);
|
||||
}
|
||||
|
||||
public ServerHttpResponse handle(String path) {
|
||||
|
|
@ -152,7 +156,7 @@ public class HttpHandlerAdapterSupportTests {
|
|||
|
||||
public ServerHttpResponse handle(ServerHttpRequest request) {
|
||||
ServerHttpResponse response = new MockServerHttpResponse();
|
||||
getHttpHandler().handle(request, response);
|
||||
this.httpHandler.handle(request, response);
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
|
@ -20,7 +20,7 @@ import org.eclipse.jetty.server.Server;
|
|||
import org.eclipse.jetty.server.ServerConnector;
|
||||
import org.eclipse.jetty.servlet.ServletContextHandler;
|
||||
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.ServletHttpHandlerAdapter;
|
||||
|
||||
|
|
@ -53,8 +53,8 @@ public class JettyHttpServer extends AbstractHttpServer {
|
|||
}
|
||||
|
||||
private ServletHttpHandlerAdapter createServletAdapter() {
|
||||
return getHttpHandlerMap() != null ? new JettyHttpHandlerAdapter(getHttpHandlerMap()) :
|
||||
new JettyHttpHandlerAdapter(getHttpHandler());
|
||||
return new JettyHttpHandlerAdapter(getHttpHandlerMap() != null
|
||||
? HttpHandler.of(getHttpHandlerMap()) : getHttpHandler());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ import java.util.concurrent.atomic.AtomicReference;
|
|||
import reactor.core.Loopback;
|
||||
import reactor.ipc.netty.NettyContext;
|
||||
|
||||
import org.springframework.http.server.reactive.HttpHandler;
|
||||
import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter;
|
||||
|
||||
/**
|
||||
|
|
@ -42,9 +43,8 @@ public class ReactorHttpServer extends AbstractHttpServer implements Loopback {
|
|||
}
|
||||
|
||||
private ReactorHttpHandlerAdapter createHttpHandlerAdapter() {
|
||||
return (getHttpHandlerMap() != null ?
|
||||
new ReactorHttpHandlerAdapter(getHttpHandlerMap()) :
|
||||
new ReactorHttpHandlerAdapter(getHttpHandler()));
|
||||
return new ReactorHttpHandlerAdapter(getHttpHandlerMap() != null
|
||||
? HttpHandler.of(getHttpHandlerMap()) : getHttpHandler());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ package org.springframework.http.server.reactive.bootstrap;
|
|||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
|
||||
import org.springframework.http.server.reactive.HttpHandler;
|
||||
import org.springframework.http.server.reactive.RxNettyHttpHandlerAdapter;
|
||||
|
||||
/**
|
||||
|
|
@ -37,9 +38,8 @@ public class RxNettyHttpServer extends AbstractHttpServer {
|
|||
}
|
||||
|
||||
private RxNettyHttpHandlerAdapter createHttpHandlerAdapter() {
|
||||
return (getHttpHandlerMap() != null ?
|
||||
new RxNettyHttpHandlerAdapter(getHttpHandlerMap()) :
|
||||
new RxNettyHttpHandlerAdapter(getHttpHandler()));
|
||||
return new RxNettyHttpHandlerAdapter(getHttpHandlerMap() != null
|
||||
? HttpHandler.of(getHttpHandlerMap()) : getHttpHandler());
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ import java.io.File;
|
|||
import org.apache.catalina.Context;
|
||||
import org.apache.catalina.LifecycleException;
|
||||
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.TomcatHttpHandlerAdapter;
|
||||
import org.springframework.util.Assert;
|
||||
|
|
@ -68,9 +68,8 @@ public class TomcatHttpServer extends AbstractHttpServer {
|
|||
}
|
||||
|
||||
private ServletHttpHandlerAdapter initServletAdapter() {
|
||||
return getHttpHandlerMap() != null ?
|
||||
new TomcatHttpHandlerAdapter(getHttpHandlerMap()) :
|
||||
new TomcatHttpHandlerAdapter(getHttpHandler());
|
||||
return new TomcatHttpHandlerAdapter(getHttpHandlerMap() != null
|
||||
? HttpHandler.of(getHttpHandlerMap()) : getHttpHandler());
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ import java.net.InetSocketAddress;
|
|||
|
||||
import io.undertow.Undertow;
|
||||
|
||||
import org.springframework.http.server.reactive.HttpHandler;
|
||||
import org.springframework.http.server.reactive.UndertowHttpHandlerAdapter;
|
||||
|
||||
/**
|
||||
|
|
@ -38,9 +39,8 @@ public class UndertowHttpServer extends AbstractHttpServer {
|
|||
}
|
||||
|
||||
private UndertowHttpHandlerAdapter initHttpHandlerAdapter() {
|
||||
return getHttpHandlerMap() != null ?
|
||||
new UndertowHttpHandlerAdapter(getHttpHandlerMap()) :
|
||||
new UndertowHttpHandlerAdapter(getHttpHandler());
|
||||
return new UndertowHttpHandlerAdapter(getHttpHandlerMap() != null
|
||||
? HttpHandler.of(getHttpHandlerMap()) : getHttpHandler());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
Loading…
Reference in New Issue