Use PathContainer in web.reactive.function.server

This commit uses the newly introduced `PathContainer` and `RequestPath`
support in the functional web framework. It exposes the path container
as property in `ServerRequest`, and uses that in the path-based
`RequestPredicates`.
This commit is contained in:
Arjen Poutsma
2017-07-03 12:33:41 +02:00
parent 727ca4514a
commit 2ccbc55ffd
9 changed files with 191 additions and 75 deletions

View File

@@ -19,6 +19,7 @@ package org.springframework.web.reactive.function.server;
import java.net.InetSocketAddress;
import java.net.URI;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.Principal;
import java.util.Arrays;
import java.util.Collections;
@@ -38,6 +39,8 @@ import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpRange;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.PathContainer;
import org.springframework.http.server.reactive.RequestPath;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
@@ -48,6 +51,7 @@ import org.springframework.web.server.WebSession;
/**
* Mock implementation of {@link ServerRequest}.
*
* @author Arjen Poutsma
* @since 5.0
*/
@@ -57,6 +61,8 @@ public class MockServerRequest implements ServerRequest {
private final URI uri;
private final RequestPath pathContainer;
private final MockHeaders headers;
private final MultiValueMap<String, HttpCookie> cookies;
@@ -76,13 +82,15 @@ public class MockServerRequest implements ServerRequest {
@Nullable
private Principal principal;
private MockServerRequest(HttpMethod method, URI uri,
MockHeaders headers, MultiValueMap<String, HttpCookie> cookies, @Nullable Object body,
private MockServerRequest(HttpMethod method, URI uri, String contextPath, MockHeaders headers,
MultiValueMap<String, HttpCookie> cookies, @Nullable Object body,
Map<String, Object> attributes, MultiValueMap<String, String> queryParams,
Map<String, String> pathVariables, @Nullable WebSession session, @Nullable Principal principal) {
this.method = method;
this.uri = uri;
this.pathContainer = RequestPath.create(uri, contextPath, StandardCharsets.UTF_8);
this.headers = headers;
this.cookies = cookies;
this.body = body;
@@ -104,6 +112,11 @@ public class MockServerRequest implements ServerRequest {
return this.uri;
}
@Override
public PathContainer pathContainer() {
return this.pathContainer;
}
@Override
public Headers headers() {
return this.headers;
@@ -116,7 +129,7 @@ public class MockServerRequest implements ServerRequest {
@Override
@SuppressWarnings("unchecked")
public <S> S body(BodyExtractor<S, ? super ServerHttpRequest> extractor){
public <S> S body(BodyExtractor<S, ? super ServerHttpRequest> extractor) {
Assert.state(this.body != null, "No body");
return (S) this.body;
}
@@ -186,6 +199,8 @@ public class MockServerRequest implements ServerRequest {
Builder uri(URI uri);
Builder contextPath(String contextPath);
Builder header(String key, String value);
Builder headers(HttpHeaders headers);
@@ -222,6 +237,8 @@ public class MockServerRequest implements ServerRequest {
private URI uri = URI.create("http://localhost");
private String contextPath = "";
private MockHeaders headers = new MockHeaders(new HttpHeaders());
private MultiValueMap<String, HttpCookie> cookies = new LinkedMultiValueMap<>();
@@ -256,18 +273,11 @@ public class MockServerRequest implements ServerRequest {
}
@Override
public Builder header(String key, String value) {
Assert.notNull(key, "'key' must not be null");
Assert.notNull(value, "'value' must not be null");
this.headers.header(key, value);
public Builder contextPath(String contextPath) {
Assert.notNull(contextPath, "'contextPath' must not be null");
this.contextPath = contextPath;
return this;
}
@Override
public Builder headers(HttpHeaders headers) {
Assert.notNull(headers, "'headers' must not be null");
this.headers = new MockHeaders(headers);
return this;
}
@Override
@@ -283,6 +293,21 @@ public class MockServerRequest implements ServerRequest {
return this;
}
@Override
public Builder header(String key, String value) {
Assert.notNull(key, "'key' must not be null");
Assert.notNull(value, "'value' must not be null");
this.headers.header(key, value);
return this;
}
@Override
public Builder headers(HttpHeaders headers) {
Assert.notNull(headers, "'headers' must not be null");
this.headers = new MockHeaders(headers);
return this;
}
@Override
public Builder attribute(String name, Object value) {
Assert.notNull(name, "'name' must not be null");
@@ -345,16 +370,16 @@ public class MockServerRequest implements ServerRequest {
@Override
public MockServerRequest body(Object body) {
this.body = body;
return new MockServerRequest(this.method, this.uri, this.headers, this.cookies,
this.body, this.attributes, this.queryParams, this.pathVariables, this.session,
this.principal);
return new MockServerRequest(this.method, this.uri, this.contextPath, this.headers,
this.cookies, this.body, this.attributes, this.queryParams, this.pathVariables,
this.session, this.principal);
}
@Override
public MockServerRequest build() {
return new MockServerRequest(this.method, this.uri, this.headers, this.cookies, null,
this.attributes, this.queryParams, this.pathVariables, this.session,
this.principal);
return new MockServerRequest(this.method, this.uri, this.contextPath, this.headers,
this.cookies, null, this.attributes, this.queryParams, this.pathVariables,
this.session, this.principal);
}
}