Encapsulate choice of URI encoding within RequestPath

Currently the URI path is always treated as UTF-8 so we can eliminate
the encoding parameter from RequestPath#parse.
This commit is contained in:
Rossen Stoyanchev
2017-07-10 14:55:01 +02:00
parent 122ee3096c
commit 9f7d57f933
8 changed files with 17 additions and 22 deletions

View File

@@ -19,7 +19,6 @@ package org.springframework.mock.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;
@@ -92,7 +91,7 @@ public class MockServerRequest implements ServerRequest {
this.method = method;
this.uri = uri;
this.pathContainer = RequestPath.create(uri, contextPath, StandardCharsets.UTF_8);
this.pathContainer = RequestPath.parse(uri, contextPath);
this.headers = headers;
this.cookies = cookies;
this.body = body;

View File

@@ -61,7 +61,7 @@ public abstract class AbstractServerHttpRequest implements ServerHttpRequest {
*/
public AbstractServerHttpRequest(URI uri, String contextPath, HttpHeaders headers) {
this.uri = uri;
this.path = new DefaultRequestPath(uri, contextPath, StandardCharsets.UTF_8);
this.path = RequestPath.parse(uri, contextPath);
this.headers = HttpHeaders.readOnlyHttpHeaders(headers);
}

View File

@@ -17,7 +17,6 @@
package org.springframework.http.server.reactive;
import java.net.URI;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;
@@ -40,8 +39,8 @@ class DefaultRequestPath implements RequestPath {
private final PathContainer pathWithinApplication;
DefaultRequestPath(URI uri, @Nullable String contextPath, Charset charset) {
this.fullPath = PathContainer.parse(uri.getRawPath(), charset);
DefaultRequestPath(URI uri, @Nullable String contextPath) {
this.fullPath = PathContainer.parse(uri.getRawPath(), StandardCharsets.UTF_8);
this.contextPath = initContextPath(this.fullPath, contextPath);
this.pathWithinApplication = extractPathWithinApplication(this.fullPath, this.contextPath);
}
@@ -86,7 +85,6 @@ class DefaultRequestPath implements RequestPath {
// PathContainer methods..
@Override
public String value() {
return this.fullPath.value();

View File

@@ -18,7 +18,6 @@ package org.springframework.http.server.reactive;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
@@ -107,14 +106,14 @@ class DefaultServerHttpRequestBuilder implements ServerHttpRequest.Builder {
@Nullable
private RequestPath getRequestPathToUse(@Nullable URI uriToUse) {
if (uriToUse == null) {
if (this.contextPath == null) {
return null;
}
if (uriToUse == null && this.contextPath == null) {
return null;
}
else if (uriToUse == null) {
return new DefaultRequestPath(this.delegate.getPath(), this.contextPath);
}
else {
return new DefaultRequestPath(uriToUse, this.contextPath, StandardCharsets.UTF_8);
return RequestPath.parse(uriToUse, this.contextPath);
}
}

View File

@@ -16,7 +16,8 @@
package org.springframework.http.server.reactive;
import java.net.URI;
import java.nio.charset.Charset;
import org.springframework.lang.Nullable;
/**
* Represents the complete path for a request.
@@ -41,11 +42,12 @@ public interface RequestPath extends PathContainer {
*/
PathContainer pathWithinApplication();
/**
* Create a new {@code RequestPath} with the given parameters.
*/
static RequestPath create(URI uri, String contextPath, Charset charset) {
return new DefaultRequestPath(uri, contextPath, charset);
static RequestPath parse(URI uri, @Nullable String contextPath) {
return new DefaultRequestPath(uri, contextPath);
}
}

View File

@@ -261,12 +261,11 @@ public class PathPattern implements Comparable<PathPattern> {
* @return the subset of the path that is matched by pattern or "" if none of it is matched by pattern elements
*/
public PathContainer extractPathWithinPattern(PathContainer path) {
// TODO: implement extractPathWithinPattern for PathContainer
String result = extractPathWithinPattern(path.value());
return PathContainer.parse(result, StandardCharsets.UTF_8);
}
// TODO: implement extractPathWithinPattern natively for PathContainer
private String extractPathWithinPattern(String path) {
// assert this.matches(path)
PathElement elem = head;

View File

@@ -19,7 +19,6 @@ import java.net.URI;
import org.junit.Test;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertEquals;
/**
@@ -54,7 +53,7 @@ public class DefaultRequestPathTests {
private void testRequestPath(String fullPath, String contextPath, String pathWithinApplication) {
URI uri = URI.create("http://localhost:8080" + fullPath);
RequestPath requestPath = new DefaultRequestPath(uri, contextPath, UTF_8);
RequestPath requestPath = RequestPath.parse(uri, contextPath);
assertEquals(contextPath.equals("/") ? "" : contextPath, requestPath.contextPath().value());
assertEquals(pathWithinApplication, requestPath.pathWithinApplication().value());

View File

@@ -19,7 +19,6 @@ 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;
@@ -91,7 +90,7 @@ public class MockServerRequest implements ServerRequest {
this.method = method;
this.uri = uri;
this.pathContainer = RequestPath.create(uri, contextPath, StandardCharsets.UTF_8);
this.pathContainer = RequestPath.parse(uri, contextPath);
this.headers = headers;
this.cookies = cookies;
this.body = body;