Add contextPath support for reactive web applications
This commit introduces support for running multiple HttpHandler's under distinct context paths which effectively allows running multiple applications on the same server. ContextPathIntegrationTests contains an example of two applications with different context paths. In order to support this the HttpHandler adapters for all supported runtimes now have a common base class HttpHandlerAdapterSupport which has two constructor choices -- one with a single HttpHandler and another with a Map<String, HttpHandler>. Note that in addition to the contextPath under which an HttpHandler is configured there may also be a "native" contextPath under which the native runtime adapter is configured (e.g. Servlet containers). In such cases the contextPath is a combination of the native contextPath and the contextPath assigned to the HttpHandler. See for example HttpHandlerAdapterSupportTests. Issue: SPR-14726
This commit is contained in:
@@ -0,0 +1,181 @@
|
||||
/*
|
||||
* Copyright 2002-2016 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.Arrays;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
|
||||
import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse;
|
||||
|
||||
import static junit.framework.TestCase.assertFalse;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link HttpHandlerAdapterSupport}.
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class HttpHandlerAdapterSupportTests {
|
||||
|
||||
|
||||
@Test
|
||||
public void invalidContextPath() throws Exception {
|
||||
testInvalidContextPath(" ", "contextPath must not be empty");
|
||||
testInvalidContextPath("path", "contextPath must begin with '/'");
|
||||
testInvalidContextPath("/path/", "contextPath must not end with '/'");
|
||||
}
|
||||
|
||||
private void testInvalidContextPath(String contextPath, String errorMessage) {
|
||||
try {
|
||||
new TestHttpHandlerAdapter(new TestHttpHandler(contextPath));
|
||||
fail();
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
assertEquals(errorMessage, ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void match() throws Exception {
|
||||
TestHttpHandler handler1 = new TestHttpHandler("/path");
|
||||
TestHttpHandler handler2 = new TestHttpHandler("/another/path");
|
||||
TestHttpHandler handler3 = new TestHttpHandler("/yet/another/path");
|
||||
|
||||
testPath("/another/path/and/more", handler1, handler2, handler3);
|
||||
|
||||
assertInvoked(handler2);
|
||||
assertNotInvoked(handler1, handler3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void matchWithContextPathEqualToPath() throws Exception {
|
||||
TestHttpHandler handler1 = new TestHttpHandler("/path");
|
||||
TestHttpHandler handler2 = new TestHttpHandler("/another/path");
|
||||
TestHttpHandler handler3 = new TestHttpHandler("/yet/another/path");
|
||||
|
||||
testPath("/path", handler1, handler2, handler3);
|
||||
|
||||
assertInvoked(handler1);
|
||||
assertNotInvoked(handler2, handler3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void matchWithNativeContextPath() throws Exception {
|
||||
MockServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, "/yet/another/path");
|
||||
request.setContextPath("/yet");
|
||||
|
||||
TestHttpHandler handler = new TestHttpHandler("/another/path");
|
||||
new TestHttpHandlerAdapter(handler).handle(request);
|
||||
|
||||
assertTrue(handler.wasInvoked());
|
||||
assertEquals("/yet/another/path", handler.getRequest().getContextPath());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void notFound() throws Exception {
|
||||
TestHttpHandler handler1 = new TestHttpHandler("/path");
|
||||
TestHttpHandler handler2 = new TestHttpHandler("/another/path");
|
||||
|
||||
ServerHttpResponse response = testPath("/yet/another/path", handler1, handler2);
|
||||
|
||||
assertNotInvoked(handler1, handler2);
|
||||
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
|
||||
}
|
||||
|
||||
|
||||
private ServerHttpResponse testPath(String path, TestHttpHandler... handlers) {
|
||||
TestHttpHandlerAdapter adapter = new TestHttpHandlerAdapter(handlers);
|
||||
return adapter.handle(path);
|
||||
}
|
||||
|
||||
private void assertInvoked(TestHttpHandler handler) {
|
||||
assertTrue(handler.wasInvoked());
|
||||
assertEquals(handler.getContextPath(), handler.getRequest().getContextPath());
|
||||
}
|
||||
|
||||
private void assertNotInvoked(TestHttpHandler... handlers) {
|
||||
Arrays.stream(handlers).forEach(handler -> assertFalse(handler.wasInvoked()));
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("WeakerAccess")
|
||||
private static class TestHttpHandlerAdapter extends HttpHandlerAdapterSupport {
|
||||
|
||||
|
||||
public TestHttpHandlerAdapter(TestHttpHandler... handlers) {
|
||||
super(initHandlerMap(handlers));
|
||||
}
|
||||
|
||||
|
||||
private static Map<String, HttpHandler> initHandlerMap(TestHttpHandler... testHandlers) {
|
||||
Map<String, HttpHandler> result = new LinkedHashMap<>();
|
||||
Arrays.stream(testHandlers).forEachOrdered(h -> result.put(h.getContextPath(), h));
|
||||
return result;
|
||||
}
|
||||
|
||||
public ServerHttpResponse handle(String path) {
|
||||
ServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, path);
|
||||
return handle(request);
|
||||
}
|
||||
|
||||
public ServerHttpResponse handle(ServerHttpRequest request) {
|
||||
ServerHttpResponse response = new MockServerHttpResponse();
|
||||
getHttpHandler().handle(request, response);
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("WeakerAccess")
|
||||
private static class TestHttpHandler implements HttpHandler {
|
||||
|
||||
private final String contextPath;
|
||||
|
||||
private ServerHttpRequest request;
|
||||
|
||||
|
||||
public TestHttpHandler(String contextPath) {
|
||||
this.contextPath = contextPath;
|
||||
}
|
||||
|
||||
|
||||
public String getContextPath() {
|
||||
return this.contextPath;
|
||||
}
|
||||
|
||||
public boolean wasInvoked() {
|
||||
return this.request != null;
|
||||
}
|
||||
|
||||
public ServerHttpRequest getRequest() {
|
||||
return this.request;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Void> handle(ServerHttpRequest request, ServerHttpResponse response) {
|
||||
this.request = request;
|
||||
return Mono.empty();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,6 +16,9 @@
|
||||
|
||||
package org.springframework.http.server.reactive.bootstrap;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.http.server.reactive.HttpHandler;
|
||||
import org.springframework.util.SocketUtils;
|
||||
|
||||
@@ -30,6 +33,9 @@ public class HttpServerSupport {
|
||||
|
||||
private HttpHandler httpHandler;
|
||||
|
||||
private Map<String, HttpHandler> handlerMap;
|
||||
|
||||
|
||||
public void setHost(String host) {
|
||||
this.host = host;
|
||||
}
|
||||
@@ -57,4 +63,15 @@ public class HttpServerSupport {
|
||||
return this.httpHandler;
|
||||
}
|
||||
|
||||
public void registerHttpHandler(String contextPath, HttpHandler handler) {
|
||||
if (this.handlerMap == null) {
|
||||
this.handlerMap = new LinkedHashMap<>();
|
||||
}
|
||||
this.handlerMap.put(contextPath, handler);
|
||||
}
|
||||
|
||||
public Map<String, HttpHandler> getHttpHandlerMap() {
|
||||
return this.handlerMap;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -20,6 +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.jetbrains.annotations.NotNull;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.http.server.reactive.ServletHttpHandlerAdapter;
|
||||
@@ -39,7 +40,7 @@ public class JettyHttpServer extends HttpServerSupport implements HttpServer, In
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
this.jettyServer = new Server();
|
||||
|
||||
ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(getHttpHandler());
|
||||
ServletHttpHandlerAdapter servlet = initServletHttpHandlerAdapter();
|
||||
ServletHolder servletHolder = new ServletHolder(servlet);
|
||||
|
||||
ServletContextHandler contextHandler = new ServletContextHandler(this.jettyServer, "", false, false);
|
||||
@@ -51,6 +52,17 @@ public class JettyHttpServer extends HttpServerSupport implements HttpServer, In
|
||||
this.jettyServer.addConnector(connector);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private ServletHttpHandlerAdapter initServletHttpHandlerAdapter() {
|
||||
if (getHttpHandlerMap() != null) {
|
||||
return new ServletHttpHandlerAdapter(getHttpHandlerMap());
|
||||
}
|
||||
else {
|
||||
Assert.notNull(getHttpHandler());
|
||||
return new ServletHttpHandlerAdapter(getHttpHandler());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
if (!this.running) {
|
||||
|
||||
@@ -35,9 +35,13 @@ public class ReactorHttpServer extends HttpServerSupport implements HttpServer,
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
|
||||
Assert.notNull(getHttpHandler());
|
||||
this.reactorHandler = new ReactorHttpHandlerAdapter(getHttpHandler());
|
||||
if (getHttpHandlerMap() != null) {
|
||||
this.reactorHandler = new ReactorHttpHandlerAdapter(getHttpHandlerMap());
|
||||
}
|
||||
else {
|
||||
Assert.notNull(getHttpHandler());
|
||||
this.reactorHandler = new ReactorHttpHandlerAdapter(getHttpHandler());
|
||||
}
|
||||
this.reactorServer = reactor.ipc.netty.http.HttpServer.create(getHost(), getPort());
|
||||
}
|
||||
|
||||
|
||||
@@ -37,8 +37,14 @@ public class RxNettyHttpServer extends HttpServerSupport implements HttpServer {
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
Assert.notNull(getHttpHandler());
|
||||
this.rxNettyHandler = new RxNettyHttpHandlerAdapter(getHttpHandler());
|
||||
|
||||
if (getHttpHandlerMap() != null) {
|
||||
this.rxNettyHandler = new RxNettyHttpHandlerAdapter(getHttpHandlerMap());
|
||||
}
|
||||
else {
|
||||
Assert.notNull(getHttpHandler());
|
||||
this.rxNettyHandler = new RxNettyHttpHandlerAdapter(getHttpHandler());
|
||||
}
|
||||
|
||||
this.rxNettyServer = io.reactivex.netty.protocol.http.server.HttpServer
|
||||
.newServer(new InetSocketAddress(getHost(), getPort()));
|
||||
|
||||
@@ -21,9 +21,11 @@ import java.io.File;
|
||||
import org.apache.catalina.Context;
|
||||
import org.apache.catalina.LifecycleException;
|
||||
import org.apache.catalina.startup.Tomcat;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.http.server.reactive.ServletHttpHandlerAdapter;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* @author Rossen Stoyanchev
|
||||
@@ -54,7 +56,7 @@ public class TomcatHttpServer extends HttpServerSupport implements HttpServer, I
|
||||
this.tomcatServer.setHostname(getHost());
|
||||
this.tomcatServer.setPort(getPort());
|
||||
|
||||
ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(getHttpHandler());
|
||||
ServletHttpHandlerAdapter servlet = initServletHttpHandlerAdapter();
|
||||
|
||||
File base = new File(System.getProperty("java.io.tmpdir"));
|
||||
Context rootContext = tomcatServer.addContext("", base.getAbsolutePath());
|
||||
@@ -62,6 +64,17 @@ public class TomcatHttpServer extends HttpServerSupport implements HttpServer, I
|
||||
rootContext.addServletMappingDecoded("/", "httpHandlerServlet");
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private ServletHttpHandlerAdapter initServletHttpHandlerAdapter() {
|
||||
if (getHttpHandlerMap() != null) {
|
||||
return new ServletHttpHandlerAdapter(getHttpHandlerMap());
|
||||
}
|
||||
else {
|
||||
Assert.notNull(getHttpHandler());
|
||||
return new ServletHttpHandlerAdapter(getHttpHandler());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
|
||||
@@ -42,6 +42,8 @@ public class MockServerHttpRequest implements ServerHttpRequest {
|
||||
|
||||
private URI url;
|
||||
|
||||
private String contextPath = "";
|
||||
|
||||
private final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>();
|
||||
|
||||
private final HttpHeaders headers = new HttpHeaders();
|
||||
@@ -99,6 +101,15 @@ public class MockServerHttpRequest implements ServerHttpRequest {
|
||||
return this.url;
|
||||
}
|
||||
|
||||
public void setContextPath(String contextPath) {
|
||||
this.contextPath = contextPath;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getContextPath() {
|
||||
return this.contextPath;
|
||||
}
|
||||
|
||||
public MockServerHttpRequest addHeader(String name, String value) {
|
||||
getHeaders().add(name, value);
|
||||
return this;
|
||||
|
||||
Reference in New Issue
Block a user