Browse Source
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-14726pull/1215/head
17 changed files with 714 additions and 71 deletions
@ -0,0 +1,126 @@
@@ -0,0 +1,126 @@
|
||||
/* |
||||
* 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.web.reactive.result.method.annotation; |
||||
|
||||
import org.junit.After; |
||||
import org.junit.Before; |
||||
import org.junit.Test; |
||||
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.http.server.reactive.HttpHandler; |
||||
import org.springframework.http.server.reactive.ServerHttpRequest; |
||||
import org.springframework.http.server.reactive.bootstrap.ReactorHttpServer; |
||||
import org.springframework.util.SocketUtils; |
||||
import org.springframework.web.bind.annotation.GetMapping; |
||||
import org.springframework.web.bind.annotation.RestController; |
||||
import org.springframework.web.client.RestTemplate; |
||||
import org.springframework.web.reactive.DispatcherHandler; |
||||
import org.springframework.web.reactive.config.EnableWebReactive; |
||||
|
||||
import static org.junit.Assert.assertEquals; |
||||
|
||||
/** |
||||
* Integration tests that demonstrate running multiple applications under |
||||
* different context paths. |
||||
* |
||||
* @author Rossen Stoyanchev |
||||
*/ |
||||
@SuppressWarnings({"unused", "WeakerAccess"}) |
||||
public class ContextPathIntegrationTests { |
||||
|
||||
private ReactorHttpServer server; |
||||
|
||||
|
||||
@Before |
||||
public void setUp() throws Exception { |
||||
|
||||
AnnotationConfigApplicationContext context1 = new AnnotationConfigApplicationContext(); |
||||
context1.register(WebApp1Config.class); |
||||
context1.refresh(); |
||||
|
||||
AnnotationConfigApplicationContext context2 = new AnnotationConfigApplicationContext(); |
||||
context2.register(WebApp2Config.class); |
||||
context2.refresh(); |
||||
|
||||
HttpHandler webApp1Handler = DispatcherHandler.toHttpHandler(context1); |
||||
HttpHandler webApp2Handler = DispatcherHandler.toHttpHandler(context2); |
||||
|
||||
this.server = new ReactorHttpServer(); |
||||
this.server.setPort(SocketUtils.findAvailableTcpPort()); |
||||
|
||||
this.server.registerHttpHandler("/webApp1", webApp1Handler); |
||||
this.server.registerHttpHandler("/webApp2", webApp2Handler); |
||||
|
||||
this.server.afterPropertiesSet(); |
||||
this.server.start(); |
||||
} |
||||
|
||||
@After |
||||
public void tearDown() throws Exception { |
||||
this.server.stop(); |
||||
} |
||||
|
||||
|
||||
@Test |
||||
public void basic() throws Exception { |
||||
|
||||
RestTemplate restTemplate = new RestTemplate(); |
||||
String actual; |
||||
|
||||
actual = restTemplate.getForObject(createUrl("/webApp1/test"), String.class); |
||||
assertEquals("Tested in /webApp1", actual); |
||||
|
||||
actual = restTemplate.getForObject(createUrl("/webApp2/test"), String.class); |
||||
assertEquals("Tested in /webApp2", actual); |
||||
} |
||||
|
||||
private String createUrl(String path) { |
||||
return "http://localhost:" + this.server.getPort() + path; |
||||
} |
||||
|
||||
|
||||
@EnableWebReactive |
||||
@Configuration |
||||
static class WebApp1Config { |
||||
|
||||
@Bean |
||||
public TestController testController() { |
||||
return new TestController(); |
||||
} |
||||
} |
||||
|
||||
@EnableWebReactive |
||||
@Configuration |
||||
static class WebApp2Config { |
||||
|
||||
@Bean |
||||
public TestController testController() { |
||||
return new TestController(); |
||||
} |
||||
} |
||||
|
||||
@RestController |
||||
static class TestController { |
||||
|
||||
@GetMapping("/test") |
||||
public String handle(ServerHttpRequest request) { |
||||
return "Tested in " + request.getContextPath(); |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,152 @@
@@ -0,0 +1,152 @@
|
||||
/* |
||||
* 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.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); |
||||
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, "contextPath must not be empty"); |
||||
if (!contextPath.equals("/")) { |
||||
Assert.isTrue(contextPath.startsWith("/"), "contextPath must begin with '/'"); |
||||
Assert.isTrue(!contextPath.endsWith("/"), "contextPath 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 -> { |
||||
HttpHandler handler = entry.getValue(); |
||||
ServerHttpRequest req = new ContextPathRequestDecorator(request, entry.getKey()); |
||||
return handler.handle(req, response); |
||||
}) |
||||
.orElseGet(() -> { |
||||
response.setStatusCode(HttpStatus.NOT_FOUND); |
||||
response.setComplete(); |
||||
return Mono.empty(); |
||||
}); |
||||
} |
||||
|
||||
/** Strip the context path from 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) : ""); |
||||
} |
||||
} |
||||
|
||||
private static class ContextPathRequestDecorator extends ServerHttpRequestDecorator { |
||||
|
||||
private final String contextPath; |
||||
|
||||
public ContextPathRequestDecorator(ServerHttpRequest delegate, String contextPath) { |
||||
super(delegate); |
||||
this.contextPath = delegate.getContextPath() + contextPath; |
||||
} |
||||
|
||||
@Override |
||||
public String getContextPath() { |
||||
return this.contextPath; |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,90 @@
@@ -0,0 +1,90 @@
|
||||
/* |
||||
* 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.net.URI; |
||||
|
||||
import reactor.core.publisher.Flux; |
||||
|
||||
import org.springframework.core.io.buffer.DataBuffer; |
||||
import org.springframework.http.HttpCookie; |
||||
import org.springframework.http.HttpHeaders; |
||||
import org.springframework.http.HttpMethod; |
||||
import org.springframework.util.Assert; |
||||
import org.springframework.util.MultiValueMap; |
||||
|
||||
/** |
||||
* Wraps another {@link ServerHttpRequest} and delegates all methods to it. |
||||
* Sub-classes can override specific methods selectively. |
||||
* |
||||
* @author Rossen Stoyanchev |
||||
* @since 5.0 |
||||
*/ |
||||
public class ServerHttpRequestDecorator implements ServerHttpRequest { |
||||
|
||||
private final ServerHttpRequest delegate; |
||||
|
||||
|
||||
public ServerHttpRequestDecorator(ServerHttpRequest delegate) { |
||||
Assert.notNull(delegate, "'delegate' is required."); |
||||
this.delegate = delegate; |
||||
} |
||||
|
||||
|
||||
public ServerHttpRequest getDelegate() { |
||||
return this.delegate; |
||||
} |
||||
|
||||
|
||||
// ServerHttpRequest delegation methods...
|
||||
|
||||
@Override |
||||
public HttpMethod getMethod() { |
||||
return getDelegate().getMethod(); |
||||
} |
||||
|
||||
@Override |
||||
public URI getURI() { |
||||
return getDelegate().getURI(); |
||||
} |
||||
|
||||
@Override |
||||
public MultiValueMap<String, String> getQueryParams() { |
||||
return getDelegate().getQueryParams(); |
||||
} |
||||
|
||||
@Override |
||||
public HttpHeaders getHeaders() { |
||||
return getDelegate().getHeaders(); |
||||
} |
||||
|
||||
@Override |
||||
public MultiValueMap<String, HttpCookie> getCookies() { |
||||
return getDelegate().getCookies(); |
||||
} |
||||
|
||||
@Override |
||||
public Flux<DataBuffer> getBody() { |
||||
return getDelegate().getBody(); |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public String toString() { |
||||
return getClass().getSimpleName() + " [delegate=" + getDelegate() + "]"; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,181 @@
@@ -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(); |
||||
} |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue