diff --git a/spring-test/src/main/java/org/springframework/mock/web/reactive/function/server/MockServerRequest.java b/spring-test/src/main/java/org/springframework/mock/web/reactive/function/server/MockServerRequest.java index e21b795d530..4adca3e3084 100644 --- a/spring-test/src/main/java/org/springframework/mock/web/reactive/function/server/MockServerRequest.java +++ b/spring-test/src/main/java/org/springframework/mock/web/reactive/function/server/MockServerRequest.java @@ -38,6 +38,7 @@ import org.springframework.http.HttpCookie; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.HttpRange; +import org.springframework.http.HttpRequest; import org.springframework.http.MediaType; import org.springframework.http.server.PathContainer; import org.springframework.http.server.RequestPath; @@ -50,6 +51,8 @@ import org.springframework.util.MultiValueMap; import org.springframework.web.reactive.function.BodyExtractor; import org.springframework.web.reactive.function.server.ServerRequest; import org.springframework.web.server.WebSession; +import org.springframework.web.util.UriBuilder; +import org.springframework.web.util.UriComponentsBuilder; /** * Mock implementation of {@link ServerRequest}. @@ -119,6 +122,11 @@ public class MockServerRequest implements ServerRequest { return this.uri; } + @Override + public UriBuilder uriBuilder() { + return UriComponentsBuilder.fromHttpRequest(new ServerRequestAdapter()); + } + @Override public PathContainer pathContainer() { return this.pathContainer; @@ -467,4 +475,23 @@ public class MockServerRequest implements ServerRequest { } + private final class ServerRequestAdapter implements HttpRequest { + + @Override + public String getMethodValue() { + return methodName(); + } + + @Override + public URI getURI() { + return MockServerRequest.this.uri; + } + + @Override + public HttpHeaders getHeaders() { + return MockServerRequest.this.headers.headers; + } + } + + } diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerRequest.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerRequest.java index 18acc501061..1f770e9b4a5 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerRequest.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerRequest.java @@ -36,6 +36,7 @@ import org.springframework.core.ParameterizedTypeReference; import org.springframework.http.HttpCookie; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpRange; +import org.springframework.http.HttpRequest; import org.springframework.http.MediaType; import org.springframework.http.codec.HttpMessageReader; import org.springframework.http.server.PathContainer; @@ -49,6 +50,8 @@ import org.springframework.web.reactive.function.UnsupportedMediaTypeException; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.UnsupportedMediaTypeStatusException; import org.springframework.web.server.WebSession; +import org.springframework.web.util.UriBuilder; +import org.springframework.web.util.UriComponentsBuilder; /** * {@code ServerRequest} implementation based on a {@link ServerWebExchange}. @@ -92,6 +95,11 @@ class DefaultServerRequest implements ServerRequest { return request().getURI(); } + @Override + public UriBuilder uriBuilder() { + return UriComponentsBuilder.fromHttpRequest(new ServerRequestAdapter()); + } + @Override public PathContainer pathContainer() { return request().getPath(); @@ -255,4 +263,23 @@ class DefaultServerRequest implements ServerRequest { } } + private final class ServerRequestAdapter implements HttpRequest { + + @Override + public String getMethodValue() { + return methodName(); + } + + @Override + public URI getURI() { + return uri(); + } + + @Override + public HttpHeaders getHeaders() { + return request().getHeaders(); + } + } + + } diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RequestPredicates.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RequestPredicates.java index b320eba6c79..1da5a793ec0 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RequestPredicates.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RequestPredicates.java @@ -46,6 +46,7 @@ import org.springframework.util.Assert; import org.springframework.util.MultiValueMap; import org.springframework.web.reactive.function.BodyExtractor; import org.springframework.web.server.WebSession; +import org.springframework.web.util.UriBuilder; import org.springframework.web.util.UriUtils; import org.springframework.web.util.pattern.PathPattern; import org.springframework.web.util.pattern.PathPatternParser; @@ -486,6 +487,11 @@ public abstract class RequestPredicates { return this.request.uri(); } + @Override + public UriBuilder uriBuilder() { + return this.request.uriBuilder(); + } + @Override public String path() { return this.subPathContainer.value(); diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/ServerRequest.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/ServerRequest.java index c7f61c1fdba..594dc845282 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/ServerRequest.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/ServerRequest.java @@ -45,6 +45,7 @@ import org.springframework.util.MultiValueMap; import org.springframework.web.reactive.function.BodyExtractor; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.WebSession; +import org.springframework.web.util.UriBuilder; /** * Represents a server-side HTTP request, as handled by a {@code HandlerFunction}. @@ -78,6 +79,16 @@ public interface ServerRequest { */ URI uri(); + /** + * Return a {@code UriBuilderComponents} from the URI associated with this + * {@code ServerRequest}, while also overlaying with values from the headers + * "Forwarded" (RFC 7239), + * or "X-Forwarded-Host", "X-Forwarded-Port", and "X-Forwarded-Proto" if + * "Forwarded" is not found. + * @return a URI builder + */ + UriBuilder uriBuilder(); + /** * Return the request path. */ diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/support/ServerRequestWrapper.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/support/ServerRequestWrapper.java index 07c59e052cd..c4b100819fb 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/support/ServerRequestWrapper.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/support/ServerRequestWrapper.java @@ -43,6 +43,7 @@ import org.springframework.web.reactive.function.BodyExtractor; import org.springframework.web.reactive.function.server.HandlerFunction; import org.springframework.web.reactive.function.server.ServerRequest; import org.springframework.web.server.WebSession; +import org.springframework.web.util.UriBuilder; /** * Implementation of the {@link ServerRequest} interface that can be subclassed @@ -89,6 +90,11 @@ public class ServerRequestWrapper implements ServerRequest { return this.delegate.uri(); } + @Override + public UriBuilder uriBuilder() { + return this.delegate.uriBuilder(); + } + @Override public String path() { return this.delegate.path(); diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/support/WebFluxUriComponentsBuilder.java b/spring-webflux/src/main/java/org/springframework/web/reactive/support/WebFluxUriComponentsBuilder.java deleted file mode 100644 index b2634c4df38..00000000000 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/support/WebFluxUriComponentsBuilder.java +++ /dev/null @@ -1,73 +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.web.reactive.support; - -import java.net.URI; - -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpRequest; -import org.springframework.util.Assert; -import org.springframework.web.reactive.function.server.ServerRequest; -import org.springframework.web.util.UriComponentsBuilder; - -/** - * @author Arjen Poutsma - * @since 5.0 - */ -public class WebFluxUriComponentsBuilder { - - /** - * Create a new {@code UriComponents} object from the URI associated with - * the given {@code ServerRequest} while also overlaying with values from the headers - * "Forwarded" (RFC 7239), - * or "X-Forwarded-Host", "X-Forwarded-Port", and "X-Forwarded-Proto" if - * "Forwarded" is not found. - * @param request the source request - * @return the URI components of the URI - */ - public static UriComponentsBuilder fromServerRequest(ServerRequest request) { - Assert.notNull(request, "'request' must not be null"); - - return UriComponentsBuilder.fromHttpRequest(new ServerRequestAdapter(request)); - } - - - private static final class ServerRequestAdapter implements HttpRequest { - - private final ServerRequest adaptee; - - public ServerRequestAdapter(ServerRequest adaptee) { - this.adaptee = adaptee; - } - - @Override - public String getMethodValue() { - return this.adaptee.methodName(); - } - - @Override - public URI getURI() { - return this.adaptee.uri(); - } - - @Override - public HttpHeaders getHeaders() { - return this.adaptee.headers().asHttpHeaders(); - } - } - -} diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/DefaultServerRequestTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/DefaultServerRequestTests.java index ab95851c074..1b90a717d10 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/DefaultServerRequestTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/DefaultServerRequestTests.java @@ -87,6 +87,21 @@ public class DefaultServerRequestTests { assertEquals(uri, request.uri()); } + @Test + public void uriBuilder() throws Exception { + URI uri = new URI("http", "localhost", "/path", "a=1", null); + MockServerHttpRequest mockRequest = MockServerHttpRequest.method(HttpMethod.GET, uri).build(); + DefaultServerRequest request = new DefaultServerRequest(mockRequest.toExchange(), messageReaders); + + + URI result = request.uriBuilder().build(); + assertEquals("http", result.getScheme()); + assertEquals("localhost", result.getHost()); + assertEquals(-1, result.getPort()); + assertEquals("/path", result.getPath()); + assertEquals("a=1", result.getQuery()); + } + @Test public void attribute() throws Exception { MockServerHttpRequest mockRequest = MockServerHttpRequest.method(HttpMethod.GET, "http://example.com").build(); diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/MockServerRequest.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/MockServerRequest.java index b536b392e20..f97257ba888 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/MockServerRequest.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/MockServerRequest.java @@ -38,6 +38,7 @@ import org.springframework.http.HttpCookie; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.HttpRange; +import org.springframework.http.HttpRequest; import org.springframework.http.MediaType; import org.springframework.http.server.PathContainer; import org.springframework.http.server.RequestPath; @@ -49,6 +50,8 @@ import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.reactive.function.BodyExtractor; import org.springframework.web.server.WebSession; +import org.springframework.web.util.UriBuilder; +import org.springframework.web.util.UriComponentsBuilder; /** * Mock implementation of {@link ServerRequest}. @@ -118,6 +121,11 @@ public class MockServerRequest implements ServerRequest { return this.uri; } + @Override + public UriBuilder uriBuilder() { + return UriComponentsBuilder.fromHttpRequest(new ServerRequestAdapter()); + } + @Override public PathContainer pathContainer() { return this.pathContainer; @@ -466,4 +474,23 @@ public class MockServerRequest implements ServerRequest { } + private final class ServerRequestAdapter implements HttpRequest { + + @Override + public String getMethodValue() { + return methodName(); + } + + @Override + public URI getURI() { + return MockServerRequest.this.uri; + } + + @Override + public HttpHeaders getHeaders() { + return MockServerRequest.this.headers.headers; + } + } + + } diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/support/WebFluxUriComponentsBuilderTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/support/WebFluxUriComponentsBuilderTests.java deleted file mode 100644 index 6c4178bc5ce..00000000000 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/support/WebFluxUriComponentsBuilderTests.java +++ /dev/null @@ -1,46 +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.web.reactive.support; - -import java.net.URI; - -import org.junit.Test; - -import org.springframework.web.reactive.function.server.MockServerRequest; -import org.springframework.web.util.UriComponents; - -import static org.junit.Assert.*; - -/** - * @author Arjen Poutsma - */ -public class WebFluxUriComponentsBuilderTests { - - @Test - public void fromServerRequest() throws Exception { - URI uri = new URI("http", "localhost", "/path", "a=1", null); - MockServerRequest request = MockServerRequest.builder().uri(uri).build(); - - UriComponents result = WebFluxUriComponentsBuilder.fromServerRequest(request).build(); - assertEquals("http", result.getScheme()); - assertEquals("localhost", result.getHost()); - assertEquals(-1, result.getPort()); - assertEquals("/path", result.getPath()); - assertEquals("a=1", result.getQuery()); - } - -} \ No newline at end of file