Moved fromServerRequest to ServerRequest itself

This commit moves WebFluxUriComponentsBuilder.fromServerRequest to the
ServerRequest interface itself.

Consequently, the WebFluxUriComponentsBuilder is removes itself, as it
contained no other methods.

Issue: SPR-15953
This commit is contained in:
Arjen Poutsma 2017-09-25 11:29:07 +02:00
parent 0d8031d9b4
commit 1a3cc3df94
9 changed files with 119 additions and 119 deletions

View File

@ -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;
}
}
}

View File

@ -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();
}
}
}

View File

@ -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();

View File

@ -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" (<a href="http://tools.ietf.org/html/rfc7239">RFC 7239</a>),
* 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.
*/

View File

@ -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();

View File

@ -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" (<a href="http://tools.ietf.org/html/rfc7239">RFC 7239</a>),
* 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();
}
}
}

View File

@ -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();

View File

@ -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;
}
}
}

View File

@ -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());
}
}