SPR-5973: Using UriComponents in more places, replaced UriBuilder by UriComponentsBuilder, UriComponents is now immutable.
This commit is contained in:
parent
ce8bc8e7e4
commit
c8add61a72
|
|
@ -22,13 +22,13 @@ import org.springframework.util.Assert;
|
|||
import org.springframework.web.context.request.RequestAttributes;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
import org.springframework.web.util.UriBuilder;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
/** @author Arjen Poutsma */
|
||||
public class ServletUriBuilder extends UriBuilder {
|
||||
public class ServletUriComponentsBuilder extends UriComponentsBuilder {
|
||||
|
||||
|
||||
public static ServletUriBuilder fromCurrentServletRequest() {
|
||||
public static ServletUriComponentsBuilder fromCurrentServletRequest() {
|
||||
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
|
||||
Assert.state(requestAttributes != null, "Could not find current RequestAttributes in RequestContextHolder");
|
||||
Assert.isInstanceOf(ServletRequestAttributes.class, requestAttributes);
|
||||
|
|
@ -38,10 +38,10 @@ public class ServletUriBuilder extends UriBuilder {
|
|||
return fromServletRequest(servletRequest);
|
||||
}
|
||||
|
||||
public static ServletUriBuilder fromServletRequest(HttpServletRequest request) {
|
||||
public static ServletUriComponentsBuilder fromServletRequest(HttpServletRequest request) {
|
||||
Assert.notNull(request, "'request' must not be null");
|
||||
|
||||
ServletUriBuilder builder = new ServletUriBuilder();
|
||||
ServletUriComponentsBuilder builder = new ServletUriComponentsBuilder();
|
||||
builder.scheme(request.getScheme());
|
||||
builder.host(request.getServerName());
|
||||
builder.port(request.getServerPort());
|
||||
|
|
@ -1,515 +0,0 @@
|
|||
/*
|
||||
* Copyright 2002-2011 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.util;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.EnumSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Builder for {@link URI} objects.
|
||||
*
|
||||
* <p>Typical usage involves:
|
||||
* <ol>
|
||||
* <li>Create a {@code UriBuilder} with one of the static factory methods (such as {@link #fromPath(String)} or
|
||||
* {@link #fromUri(URI)})</li>
|
||||
* <li>Set the various URI components through the respective methods ({@link #scheme(String)},
|
||||
* {@link #userInfo(String)}, {@link #host(String)}, {@link #port(int)}, {@link #path(String)},
|
||||
* {@link #pathSegment(String...)}, {@link #queryParam(String, Object...)}, and {@link #fragment(String)}.</li>
|
||||
* <li>Build the URI with one of the {@link #build} method variants.</li>
|
||||
* </ol>
|
||||
*
|
||||
* <p>Most of the URI component methods accept URI template variables (i.e. {@code "{foo}"}), which are expanded by
|
||||
* calling {@code build}.
|
||||
*
|
||||
* <p>Inspired by {@link javax.ws.rs.core.UriBuilder}.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @since 3.1
|
||||
* @see #newInstance()
|
||||
* @see #fromPath(String)
|
||||
* @see #fromUri(URI)
|
||||
*/
|
||||
public class UriBuilder {
|
||||
|
||||
private String scheme;
|
||||
|
||||
private String userInfo;
|
||||
|
||||
private String host;
|
||||
|
||||
private int port = -1;
|
||||
|
||||
private final List<String> pathSegments = new ArrayList<String>();
|
||||
|
||||
private final StringBuilder queryBuilder = new StringBuilder();
|
||||
|
||||
private String fragment;
|
||||
|
||||
/**
|
||||
* Default constructor. Protected to prevent direct instantiation.
|
||||
*
|
||||
* @see #newInstance()
|
||||
* @see #fromPath(String)
|
||||
* @see #fromUri(URI)
|
||||
*/
|
||||
protected UriBuilder() {
|
||||
}
|
||||
|
||||
// Factory methods
|
||||
|
||||
/**
|
||||
* Returns a new, empty URI builder.
|
||||
*
|
||||
* @return the new {@code UriBuilder}
|
||||
*/
|
||||
public static UriBuilder newInstance() {
|
||||
return new UriBuilder();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a URI builder that is initialized with the given path.
|
||||
*
|
||||
* @param path the path to initialize with
|
||||
* @return the new {@code UriBuilder}
|
||||
*/
|
||||
public static UriBuilder fromPath(String path) {
|
||||
UriBuilder builder = new UriBuilder();
|
||||
builder.path(path);
|
||||
return builder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a URI builder that is initialized with the given {@code URI}.
|
||||
*
|
||||
* @param uri the URI to initialize with
|
||||
* @return the new {@code UriBuilder}
|
||||
*/
|
||||
public static UriBuilder fromUri(URI uri) {
|
||||
UriBuilder builder = new UriBuilder();
|
||||
builder.uri(uri);
|
||||
return builder;
|
||||
}
|
||||
|
||||
// build methods
|
||||
|
||||
/**
|
||||
* Builds a URI with no URI template variables. Any template variable definitions found will be encoded (i.e.
|
||||
* {@code "/{foo}"} will result in {@code "/%7Bfoo%7D"}.
|
||||
* @return the resulting URI
|
||||
*/
|
||||
public URI build() {
|
||||
String port = portAsString();
|
||||
String path = null;
|
||||
if (!pathSegments.isEmpty()) {
|
||||
StringBuilder pathBuilder = new StringBuilder();
|
||||
for (String pathSegment : pathSegments) {
|
||||
boolean startsWithSlash = pathSegment.charAt(0) == '/';
|
||||
boolean endsWithSlash = pathBuilder.length() > 0 && pathBuilder.charAt(pathBuilder.length() - 1) == '/';
|
||||
|
||||
if (!endsWithSlash && !startsWithSlash) {
|
||||
pathBuilder.append('/');
|
||||
}
|
||||
else if (endsWithSlash && startsWithSlash) {
|
||||
pathSegment = pathSegment.substring(1);
|
||||
}
|
||||
pathBuilder.append(pathSegment);
|
||||
}
|
||||
path = pathBuilder.toString();
|
||||
}
|
||||
String query = queryAsString();
|
||||
|
||||
String uri = UriUtils.buildUri(scheme, null, userInfo, host, port, path, query, fragment);
|
||||
|
||||
uri = StringUtils.replace(uri, "{", "%7B");
|
||||
uri = StringUtils.replace(uri, "}", "%7D");
|
||||
|
||||
return URI.create(uri);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a URI with the given URI template variables. Any template variable definitions found will be expanded with
|
||||
* the given variables map. All variable values will be encoded in accordance with the encoding rules for the URI
|
||||
* component they occur in.
|
||||
*
|
||||
* @param uriVariables the map of URI variables
|
||||
* @return the resulting URI
|
||||
*/
|
||||
public URI build(Map<String, ?> uriVariables) {
|
||||
return buildFromMap(uriVariables, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a URI with the given URI template variables. Any template variable definitions found will be expanded with the
|
||||
* given variables map. All variable values will not be encoded.
|
||||
*
|
||||
* @param uriVariables the map of URI variables
|
||||
* @return the resulting URI
|
||||
*/
|
||||
public URI buildFromEncoded(Map<String, ?> uriVariables) {
|
||||
return buildFromMap(uriVariables, false);
|
||||
}
|
||||
|
||||
private URI buildFromMap(Map<String, ?> uriVariables, boolean encodeUriVariableValues) {
|
||||
if (CollectionUtils.isEmpty(uriVariables)) {
|
||||
return build();
|
||||
}
|
||||
String scheme = expand(this.scheme, UriComponents.Type.SCHEME, uriVariables, encodeUriVariableValues);
|
||||
String userInfo = expand(this.userInfo, UriComponents.Type.USER_INFO, uriVariables, encodeUriVariableValues);
|
||||
String host = expand(this.host, UriComponents.Type.HOST, uriVariables, encodeUriVariableValues);
|
||||
String port = expand(this.portAsString(), UriComponents.Type.PORT, uriVariables, encodeUriVariableValues);
|
||||
String path = null;
|
||||
if (!this.pathSegments.isEmpty()) {
|
||||
StringBuilder pathBuilder = new StringBuilder();
|
||||
for (String pathSegment : this.pathSegments) {
|
||||
boolean startsWithSlash = pathSegment.charAt(0) == '/';
|
||||
boolean endsWithSlash = pathBuilder.length() > 0 && pathBuilder.charAt(pathBuilder.length() - 1) == '/';
|
||||
|
||||
if (!endsWithSlash && !startsWithSlash) {
|
||||
pathBuilder.append('/');
|
||||
}
|
||||
else if (endsWithSlash && startsWithSlash) {
|
||||
pathSegment = pathSegment.substring(1);
|
||||
}
|
||||
pathSegment = expand(pathSegment, UriComponents.Type.PATH_SEGMENT, uriVariables, encodeUriVariableValues);
|
||||
pathBuilder.append(pathSegment);
|
||||
}
|
||||
path = pathBuilder.toString();
|
||||
}
|
||||
String query = expand(this.queryAsString(), UriComponents.Type.QUERY, uriVariables, encodeUriVariableValues);
|
||||
String fragment = expand(this.fragment, UriComponents.Type.FRAGMENT, uriVariables, encodeUriVariableValues);
|
||||
|
||||
String uri = UriUtils.buildUri(scheme, null, userInfo, host, port, path, query, fragment);
|
||||
return URI.create(uri);
|
||||
}
|
||||
|
||||
private String expand(String source,
|
||||
UriComponents.Type uriComponent,
|
||||
Map<String, ?> uriVariables,
|
||||
boolean encodeUriVariableValues) {
|
||||
if (source == null) {
|
||||
return null;
|
||||
}
|
||||
if (source.indexOf('{') == -1) {
|
||||
return source;
|
||||
}
|
||||
UriTemplate template = new UriComponentTemplate(source, uriComponent, encodeUriVariableValues);
|
||||
return template.expandAsString(uriVariables);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a URI with the given URI template variable values. Any template variable definitions found will be expanded
|
||||
* with the given variables. All variable values will be encoded in accordance with the encoding rules for the URI
|
||||
* component they occur in.
|
||||
*
|
||||
* @param uriVariableValues the array of URI variables
|
||||
* @return the resulting URI
|
||||
*/
|
||||
public URI build(Object... uriVariableValues) {
|
||||
return buildFromVarArg(true, uriVariableValues);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a URI with the given URI template variable values. Any template variable definitions found will be expanded
|
||||
* with the given variables. All variable values will not be encoded.
|
||||
*
|
||||
* @param uriVariableValues the array of URI variables
|
||||
* @return the resulting URI
|
||||
*/
|
||||
public URI buildFromEncoded(Object... uriVariableValues) {
|
||||
return buildFromVarArg(false, uriVariableValues);
|
||||
}
|
||||
|
||||
private URI buildFromVarArg(boolean encodeUriVariableValues, Object... uriVariableValues) {
|
||||
if (ObjectUtils.isEmpty(uriVariableValues)) {
|
||||
return build();
|
||||
}
|
||||
|
||||
StringBuilder uriBuilder = new StringBuilder();
|
||||
|
||||
UriTemplate template;
|
||||
|
||||
if (scheme != null) {
|
||||
template = new UriComponentTemplate(scheme, UriComponents.Type.SCHEME, encodeUriVariableValues);
|
||||
uriBuilder.append(template.expandAsString(uriVariableValues));
|
||||
uriBuilder.append(':');
|
||||
}
|
||||
|
||||
if (userInfo != null || host != null || port != -1) {
|
||||
uriBuilder.append("//");
|
||||
|
||||
if (StringUtils.hasLength(userInfo)) {
|
||||
template = new UriComponentTemplate(userInfo, UriComponents.Type.USER_INFO, encodeUriVariableValues);
|
||||
uriBuilder.append(template.expandAsString(uriVariableValues));
|
||||
uriBuilder.append('@');
|
||||
}
|
||||
|
||||
if (host != null) {
|
||||
template = new UriComponentTemplate(host, UriComponents.Type.HOST, encodeUriVariableValues);
|
||||
uriBuilder.append(template.expandAsString(uriVariableValues));
|
||||
}
|
||||
|
||||
if (port != -1) {
|
||||
uriBuilder.append(':');
|
||||
uriBuilder.append(port);
|
||||
}
|
||||
}
|
||||
|
||||
if (!pathSegments.isEmpty()) {
|
||||
for (String pathSegment : pathSegments) {
|
||||
boolean startsWithSlash = pathSegment.charAt(0) == '/';
|
||||
boolean endsWithSlash = uriBuilder.length() > 0 && uriBuilder.charAt(uriBuilder.length() - 1) == '/';
|
||||
|
||||
if (!endsWithSlash && !startsWithSlash) {
|
||||
uriBuilder.append('/');
|
||||
}
|
||||
else if (endsWithSlash && startsWithSlash) {
|
||||
pathSegment = pathSegment.substring(1);
|
||||
}
|
||||
template = new UriComponentTemplate(pathSegment, UriComponents.Type.PATH_SEGMENT, encodeUriVariableValues);
|
||||
uriBuilder.append(template.expandAsString(uriVariableValues));
|
||||
}
|
||||
}
|
||||
|
||||
if (queryBuilder.length() > 0) {
|
||||
uriBuilder.append('?');
|
||||
template = new UriComponentTemplate(queryBuilder.toString(), UriComponents.Type.QUERY, encodeUriVariableValues);
|
||||
uriBuilder.append(template.expandAsString(uriVariableValues));
|
||||
}
|
||||
|
||||
if (StringUtils.hasLength(fragment)) {
|
||||
uriBuilder.append('#');
|
||||
template = new UriComponentTemplate(fragment, UriComponents.Type.FRAGMENT, encodeUriVariableValues);
|
||||
uriBuilder.append(template.expandAsString(uriVariableValues));
|
||||
}
|
||||
|
||||
return URI.create(uriBuilder.toString());
|
||||
}
|
||||
|
||||
// URI components methods
|
||||
|
||||
/**
|
||||
* Initializes all components of this URI builder with the components of the given URI.
|
||||
*
|
||||
* @param uri the URI
|
||||
* @return this UriBuilder
|
||||
*/
|
||||
public UriBuilder uri(URI uri) {
|
||||
Assert.notNull(uri, "'uri' must not be null");
|
||||
Assert.isTrue(!uri.isOpaque(), "Opaque URI [" + uri + "] not supported");
|
||||
|
||||
this.scheme = uri.getScheme();
|
||||
|
||||
if (uri.getRawUserInfo() != null) {
|
||||
this.userInfo = uri.getRawUserInfo();
|
||||
}
|
||||
if (uri.getHost() != null) {
|
||||
this.host = uri.getHost();
|
||||
}
|
||||
if (uri.getPort() != -1) {
|
||||
this.port = uri.getPort();
|
||||
}
|
||||
if (StringUtils.hasLength(uri.getRawPath())) {
|
||||
String[] pathSegments = StringUtils.tokenizeToStringArray(uri.getRawPath(), "/");
|
||||
|
||||
this.pathSegments.clear();
|
||||
Collections.addAll(this.pathSegments, pathSegments);
|
||||
}
|
||||
if (StringUtils.hasLength(uri.getRawQuery())) {
|
||||
this.queryBuilder.setLength(0);
|
||||
this.queryBuilder.append(uri.getRawQuery());
|
||||
}
|
||||
if (uri.getRawFragment() != null) {
|
||||
this.fragment = uri.getRawFragment();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the URI scheme. The given scheme may contain URI template variables, and may also be {@code null} to clear the
|
||||
* scheme of this builder.
|
||||
*
|
||||
* @param scheme the URI scheme
|
||||
* @return this UriBuilder
|
||||
*/
|
||||
public UriBuilder scheme(String scheme) {
|
||||
if (scheme != null) {
|
||||
Assert.hasLength(scheme, "'scheme' must not be empty");
|
||||
this.scheme = encodeUriComponent(scheme, UriComponents.Type.SCHEME);
|
||||
}
|
||||
else {
|
||||
this.scheme = null;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the URI user info. The given user info may contain URI template variables, and may also be {@code null} to
|
||||
* clear the user info of this builder.
|
||||
*
|
||||
* @param userInfo the URI user info
|
||||
* @return this UriBuilder
|
||||
*/
|
||||
public UriBuilder userInfo(String userInfo) {
|
||||
if (userInfo != null) {
|
||||
Assert.hasLength(userInfo, "'userInfo' must not be empty");
|
||||
this.userInfo = encodeUriComponent(userInfo, UriComponents.Type.USER_INFO);
|
||||
}
|
||||
else {
|
||||
this.userInfo = null;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the URI host. The given host may contain URI template variables, and may also be {@code null} to clear the host
|
||||
* of this builder.
|
||||
*
|
||||
* @param host the URI host
|
||||
* @return this UriBuilder
|
||||
*/
|
||||
public UriBuilder host(String host) {
|
||||
if (host != null) {
|
||||
Assert.hasLength(host, "'host' must not be empty");
|
||||
this.host = encodeUriComponent(host, UriComponents.Type.HOST);
|
||||
}
|
||||
else {
|
||||
this.host = null;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the URI port. Passing {@code -1} will clear the port of this builder.
|
||||
*
|
||||
* @param port the URI port
|
||||
* @return this UriBuilder
|
||||
*/
|
||||
public UriBuilder port(int port) {
|
||||
Assert.isTrue(port >= -1, "'port' must not be < -1");
|
||||
this.port = port;
|
||||
return this;
|
||||
}
|
||||
|
||||
private String portAsString() {
|
||||
return this.port != -1 ? Integer.toString(this.port) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends the given path to the existing path of this builder. The given path may contain URI template variables.
|
||||
*
|
||||
* @param path the URI path
|
||||
* @return this UriBuilder
|
||||
*/
|
||||
public UriBuilder path(String path) {
|
||||
Assert.notNull(path, "path must not be null");
|
||||
|
||||
String[] pathSegments = StringUtils.tokenizeToStringArray(path, "/");
|
||||
return pathSegment(pathSegments);
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends the given path segments to the existing path of this builder. Each given path segments may contain URI
|
||||
* template variables.
|
||||
*
|
||||
* @param segments the URI path segments
|
||||
* @return this UriBuilder
|
||||
*/
|
||||
public UriBuilder pathSegment(String... segments) throws IllegalArgumentException {
|
||||
Assert.notNull(segments, "'segments' must not be null");
|
||||
for (String segment : segments) {
|
||||
this.pathSegments.add(encodeUriComponent(segment, UriComponents.Type.PATH_SEGMENT));
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends the given query parameter to the existing query parameters. The given name or any of the values may contain
|
||||
* URI template variables. If no values are given, the resulting URI will contain the query parameter name only (i.e.
|
||||
* {@code ?foo} instead of {@code ?foo=bar}.
|
||||
*
|
||||
* @param name the query parameter name
|
||||
* @param values the query parameter values
|
||||
* @return this UriBuilder
|
||||
*/
|
||||
public UriBuilder queryParam(String name, Object... values) {
|
||||
Assert.notNull(name, "'name' must not be null");
|
||||
|
||||
String encodedName = encodeUriComponent(name, UriComponents.Type.QUERY_PARAM);
|
||||
|
||||
if (ObjectUtils.isEmpty(values)) {
|
||||
if (queryBuilder.length() != 0) {
|
||||
queryBuilder.append('&');
|
||||
}
|
||||
queryBuilder.append(encodedName);
|
||||
}
|
||||
else {
|
||||
for (Object value : values) {
|
||||
if (queryBuilder.length() != 0) {
|
||||
queryBuilder.append('&');
|
||||
}
|
||||
queryBuilder.append(encodedName);
|
||||
|
||||
String valueAsString = value != null ? value.toString() : "";
|
||||
if (valueAsString.length() != 0) {
|
||||
queryBuilder.append('=');
|
||||
queryBuilder.append(encodeUriComponent(valueAsString, UriComponents.Type.QUERY_PARAM));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
private String queryAsString() {
|
||||
return queryBuilder.length() != 0 ? queryBuilder.toString() : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the URI fragment. The given fragment may contain URI template variables, and may also be {@code null} to clear
|
||||
* the fragment of this builder.
|
||||
*
|
||||
* @param fragment the URI fragment
|
||||
* @return this UriBuilder
|
||||
*/
|
||||
public UriBuilder fragment(String fragment) {
|
||||
if (fragment != null) {
|
||||
Assert.hasLength(fragment, "'fragment' must not be empty");
|
||||
this.fragment = encodeUriComponent(fragment, UriComponents.Type.FRAGMENT);
|
||||
}
|
||||
else {
|
||||
this.fragment = null;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
private String encodeUriComponent(String source, UriComponents.Type uriComponent) {
|
||||
return UriUtils.encodeUriComponent(source, uriComponent, EnumSet.of(UriUtils.EncodingOption.ALLOW_TEMPLATE_VARS));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -1,46 +0,0 @@
|
|||
/*
|
||||
* Copyright 2002-2011 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.util;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Subclass of {@link UriTemplate} that operates on URI components, rather than full URIs.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @since 3.1
|
||||
*/
|
||||
class UriComponentTemplate extends UriTemplate {
|
||||
|
||||
private final UriComponents.Type uriComponent;
|
||||
|
||||
private boolean encodeUriVariableValues;
|
||||
|
||||
UriComponentTemplate(String uriTemplate, UriComponents.Type uriComponent, boolean encodeUriVariableValues) {
|
||||
super(uriTemplate);
|
||||
Assert.notNull(uriComponent, "'uriComponent' must not be null");
|
||||
this.uriComponent = uriComponent;
|
||||
this.encodeUriVariableValues = encodeUriVariableValues;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getVariableValueAsString(Object variableValue) {
|
||||
String variableValueString = super.getVariableValueAsString(variableValue);
|
||||
return encodeUriVariableValues ? UriUtils.encodeUriComponent(variableValueString, uriComponent) :
|
||||
variableValueString;
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
|
|
@ -0,0 +1,323 @@
|
|||
/*
|
||||
* Copyright 2002-2011 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.util;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Builder for {@link UriComponents}.
|
||||
* <p/>
|
||||
* Typical usage involves:
|
||||
* <ol>
|
||||
* <li>Create a {@code UriComponentsBuilder} with one of the static factory methods (such as
|
||||
* {@link #fromPath(String)} or {@link #fromUri(URI)})</li>
|
||||
* <li>Set the various URI components through the respective methods ({@link #scheme(String)},
|
||||
* {@link #userInfo(String)}, {@link #host(String)}, {@link #port(int)}, {@link #path(String)},
|
||||
* {@link #pathSegment(String...)}, {@link #queryParam(String, Object...)}, and
|
||||
* {@link #fragment(String)}.</li>
|
||||
* <li>Build the {@link UriComponents} instance with the {@link #build()} method.</li>
|
||||
* </ol>
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @see #newInstance()
|
||||
* @see #fromPath(String)
|
||||
* @see #fromUri(URI)
|
||||
* @since 3.1
|
||||
*/
|
||||
public class UriComponentsBuilder {
|
||||
|
||||
private static final char PATH_DELIMITER = '/';
|
||||
|
||||
private String scheme;
|
||||
|
||||
private String userInfo;
|
||||
|
||||
private String host;
|
||||
|
||||
private int port = -1;
|
||||
|
||||
private final List<String> pathSegments = new ArrayList<String>();
|
||||
|
||||
private final StringBuilder queryBuilder = new StringBuilder();
|
||||
|
||||
private String fragment;
|
||||
|
||||
/**
|
||||
* Default constructor. Protected to prevent direct instantiation.
|
||||
*
|
||||
* @see #newInstance()
|
||||
* @see #fromPath(String)
|
||||
* @see #fromUri(URI)
|
||||
*/
|
||||
protected UriComponentsBuilder() {
|
||||
}
|
||||
|
||||
// Factory methods
|
||||
|
||||
/**
|
||||
* Returns a new, empty URI builder.
|
||||
*
|
||||
* @return the new {@code UriComponentsBuilder}
|
||||
*/
|
||||
public static UriComponentsBuilder newInstance() {
|
||||
return new UriComponentsBuilder();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a URI builder that is initialized with the given path.
|
||||
*
|
||||
* @param path the path to initialize with
|
||||
* @return the new {@code UriComponentsBuilder}
|
||||
*/
|
||||
public static UriComponentsBuilder fromPath(String path) {
|
||||
UriComponentsBuilder builder = new UriComponentsBuilder();
|
||||
builder.path(path);
|
||||
return builder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a URI builder that is initialized with the given {@code URI}.
|
||||
*
|
||||
* @param uri the URI to initialize with
|
||||
* @return the new {@code UriComponentsBuilder}
|
||||
*/
|
||||
public static UriComponentsBuilder fromUri(URI uri) {
|
||||
UriComponentsBuilder builder = new UriComponentsBuilder();
|
||||
builder.uri(uri);
|
||||
return builder;
|
||||
}
|
||||
|
||||
// build methods
|
||||
|
||||
/**
|
||||
* Builds a {@code UriComponents} instance from the various components contained in this builder.
|
||||
*
|
||||
* @return the URI components
|
||||
*/
|
||||
public UriComponents build() {
|
||||
String port = portAsString();
|
||||
String path = pathAsString();
|
||||
String query = queryAsString();
|
||||
return UriComponents.fromUriComponents(scheme, null, userInfo, host, port, path, query, fragment, false);
|
||||
}
|
||||
|
||||
// URI components methods
|
||||
|
||||
/**
|
||||
* Initializes all components of this URI builder with the components of the given URI.
|
||||
*
|
||||
* @param uri the URI
|
||||
* @return this UriComponentsBuilder
|
||||
*/
|
||||
public UriComponentsBuilder uri(URI uri) {
|
||||
Assert.notNull(uri, "'uri' must not be null");
|
||||
Assert.isTrue(!uri.isOpaque(), "Opaque URI [" + uri + "] not supported");
|
||||
|
||||
this.scheme = uri.getScheme();
|
||||
|
||||
if (uri.getUserInfo() != null) {
|
||||
this.userInfo = uri.getUserInfo();
|
||||
}
|
||||
if (uri.getHost() != null) {
|
||||
this.host = uri.getHost();
|
||||
}
|
||||
if (uri.getPort() != -1) {
|
||||
this.port = uri.getPort();
|
||||
}
|
||||
if (StringUtils.hasLength(uri.getPath())) {
|
||||
String[] pathSegments = StringUtils.tokenizeToStringArray(uri.getPath(), "/");
|
||||
|
||||
this.pathSegments.clear();
|
||||
Collections.addAll(this.pathSegments, pathSegments);
|
||||
}
|
||||
if (StringUtils.hasLength(uri.getQuery())) {
|
||||
this.queryBuilder.setLength(0);
|
||||
this.queryBuilder.append(uri.getQuery());
|
||||
}
|
||||
if (uri.getFragment() != null) {
|
||||
this.fragment = uri.getFragment();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the URI scheme. The given scheme may contain URI template variables, and may also be {@code null} to clear the
|
||||
* scheme of this builder.
|
||||
*
|
||||
* @param scheme the URI scheme
|
||||
* @return this UriComponentsBuilder
|
||||
*/
|
||||
public UriComponentsBuilder scheme(String scheme) {
|
||||
this.scheme = scheme;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the URI user info. The given user info may contain URI template variables, and may also be {@code null} to
|
||||
* clear the user info of this builder.
|
||||
*
|
||||
* @param userInfo the URI user info
|
||||
* @return this UriComponentsBuilder
|
||||
*/
|
||||
public UriComponentsBuilder userInfo(String userInfo) {
|
||||
this.userInfo = userInfo;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the URI host. The given host may contain URI template variables, and may also be {@code null} to clear the host
|
||||
* of this builder.
|
||||
*
|
||||
* @param host the URI host
|
||||
* @return this UriComponentsBuilder
|
||||
*/
|
||||
public UriComponentsBuilder host(String host) {
|
||||
this.host = host;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the URI port. Passing {@code -1} will clear the port of this builder.
|
||||
*
|
||||
* @param port the URI port
|
||||
* @return this UriComponentsBuilder
|
||||
*/
|
||||
public UriComponentsBuilder port(int port) {
|
||||
Assert.isTrue(port >= -1, "'port' must not be < -1");
|
||||
this.port = port;
|
||||
return this;
|
||||
}
|
||||
|
||||
private String portAsString() {
|
||||
return this.port != -1 ? Integer.toString(this.port) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends the given path to the existing path of this builder. The given path may contain URI template variables.
|
||||
*
|
||||
* @param path the URI path
|
||||
* @return this UriComponentsBuilder
|
||||
*/
|
||||
public UriComponentsBuilder path(String path) {
|
||||
Assert.notNull(path, "path must not be null");
|
||||
|
||||
String[] pathSegments = StringUtils.tokenizeToStringArray(path, "/");
|
||||
return pathSegment(pathSegments);
|
||||
}
|
||||
|
||||
private String pathAsString() {
|
||||
if (!pathSegments.isEmpty()) {
|
||||
StringBuilder pathBuilder = new StringBuilder();
|
||||
for (String pathSegment : pathSegments) {
|
||||
boolean startsWithSlash = pathSegment.charAt(0) == PATH_DELIMITER;
|
||||
boolean endsWithSlash =
|
||||
pathBuilder.length() > 0 && pathBuilder.charAt(pathBuilder.length() - 1) == PATH_DELIMITER;
|
||||
|
||||
if (!endsWithSlash && !startsWithSlash) {
|
||||
pathBuilder.append('/');
|
||||
}
|
||||
else if (endsWithSlash && startsWithSlash) {
|
||||
pathSegment = pathSegment.substring(1);
|
||||
}
|
||||
pathBuilder.append(pathSegment);
|
||||
}
|
||||
return pathBuilder.toString();
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Appends the given path segments to the existing path of this builder. Each given path segments may contain URI
|
||||
* template variables.
|
||||
*
|
||||
* @param segments the URI path segments
|
||||
* @return this UriComponentsBuilder
|
||||
*/
|
||||
public UriComponentsBuilder pathSegment(String... segments) throws IllegalArgumentException {
|
||||
Assert.notNull(segments, "'segments' must not be null");
|
||||
Collections.addAll(this.pathSegments, segments);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends the given query parameter to the existing query parameters. The given name or any of the values may contain
|
||||
* URI template variables. If no values are given, the resulting URI will contain the query parameter name only (i.e.
|
||||
* {@code ?foo} instead of {@code ?foo=bar}.
|
||||
*
|
||||
* @param name the query parameter name
|
||||
* @param values the query parameter values
|
||||
* @return this UriComponentsBuilder
|
||||
*/
|
||||
public UriComponentsBuilder queryParam(String name, Object... values) {
|
||||
Assert.notNull(name, "'name' must not be null");
|
||||
|
||||
if (ObjectUtils.isEmpty(values)) {
|
||||
if (queryBuilder.length() != 0) {
|
||||
queryBuilder.append('&');
|
||||
}
|
||||
queryBuilder.append(name);
|
||||
}
|
||||
else {
|
||||
for (Object value : values) {
|
||||
if (queryBuilder.length() != 0) {
|
||||
queryBuilder.append('&');
|
||||
}
|
||||
queryBuilder.append(name);
|
||||
|
||||
if (value != null) {
|
||||
queryBuilder.append('=');
|
||||
queryBuilder.append(value.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
private String queryAsString() {
|
||||
return queryBuilder.length() != 0 ? queryBuilder.toString() : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the URI fragment. The given fragment may contain URI template variables, and may also be {@code null} to clear
|
||||
* the fragment of this builder.
|
||||
*
|
||||
* @param fragment the URI fragment
|
||||
* @return this UriComponentsBuilder
|
||||
*/
|
||||
public UriComponentsBuilder fragment(String fragment) {
|
||||
if (fragment != null) {
|
||||
Assert.hasLength(fragment, "'fragment' must not be empty");
|
||||
this.fragment = fragment;
|
||||
}
|
||||
else {
|
||||
this.fragment = null;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -21,10 +21,13 @@ import java.io.UnsupportedEncodingException;
|
|||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.Collections;
|
||||
import java.util.EnumMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
|
|
@ -49,16 +52,17 @@ public class UriTemplate implements Serializable {
|
|||
/** Replaces template variables in the URI template. */
|
||||
private static final String DEFAULT_VARIABLE_PATTERN = "(.*)";
|
||||
|
||||
|
||||
private final List<String> variableNames;
|
||||
|
||||
private final Pattern matchPattern;
|
||||
|
||||
private final String uriTemplate;
|
||||
|
||||
private final UriComponents uriComponents;
|
||||
|
||||
|
||||
/**
|
||||
* Construct a new {@link UriTemplate} with the given URI String.
|
||||
* Construct a new {@code UriTemplate} with the given URI String.
|
||||
* @param uriTemplate the URI template string
|
||||
*/
|
||||
public UriTemplate(String uriTemplate) {
|
||||
|
|
@ -66,8 +70,18 @@ public class UriTemplate implements Serializable {
|
|||
this.uriTemplate = uriTemplate;
|
||||
this.variableNames = parser.getVariableNames();
|
||||
this.matchPattern = parser.getMatchPattern();
|
||||
this.uriComponents = UriComponents.fromUriString(uriTemplate);
|
||||
}
|
||||
|
||||
public UriTemplate(Map<UriComponents.Type, String> uriComponents) {
|
||||
this.uriComponents = UriComponents.fromUriComponentMap(uriComponents);
|
||||
String uriTemplate = this.uriComponents.toUriString();
|
||||
Parser parser = new Parser(uriTemplate);
|
||||
this.uriTemplate = uriTemplate;
|
||||
this.variableNames = parser.getVariableNames();
|
||||
this.matchPattern = parser.getMatchPattern();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the names of the variables in the template, in order.
|
||||
* @return the template variable names
|
||||
|
|
@ -76,6 +90,7 @@ public class UriTemplate implements Serializable {
|
|||
return this.variableNames;
|
||||
}
|
||||
|
||||
// expanding
|
||||
|
||||
/**
|
||||
* Given the Map of variables, expands this template into a URI. The Map keys represent variable names,
|
||||
|
|
@ -95,7 +110,8 @@ public class UriTemplate implements Serializable {
|
|||
* or if it does not contain values for all the variable names
|
||||
*/
|
||||
public URI expand(Map<String, ?> uriVariables) {
|
||||
return encodeUri(expandAsString(uriVariables));
|
||||
UriComponents expandedComponents = expandAsUriComponents(uriVariables, true);
|
||||
return expandedComponents.toUri();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -116,20 +132,72 @@ public class UriTemplate implements Serializable {
|
|||
* @throws IllegalArgumentException if <code>uriVariables</code> is <code>null</code>;
|
||||
* or if it does not contain values for all the variable names
|
||||
*/
|
||||
public String expandAsString(Map<String, ?> uriVariables) {
|
||||
Assert.notNull(uriVariables, "'uriVariables' must not be null");
|
||||
Object[] values = new Object[this.variableNames.size()];
|
||||
for (int i = 0; i < this.variableNames.size(); i++) {
|
||||
String name = this.variableNames.get(i);
|
||||
if (!uriVariables.containsKey(name)) {
|
||||
throw new IllegalArgumentException("'uriVariables' Map has no value for '" + name + "'");
|
||||
}
|
||||
values[i] = uriVariables.get(name);
|
||||
}
|
||||
return expandAsString(values);
|
||||
public String expandAsString(final Map<String, ?> uriVariables, boolean encode) {
|
||||
UriComponents expandedComponents = expandAsUriComponents(uriVariables, encode);
|
||||
return expandedComponents.toUriString();
|
||||
}
|
||||
|
||||
/**
|
||||
public UriComponents expandAsUriComponents(final Map<String, ?> uriVariables, boolean encode) {
|
||||
Assert.notNull(uriVariables, "'uriVariables' must not be null");
|
||||
Set<String> variablesSet = new HashSet<String>(this.variableNames);
|
||||
variablesSet.removeAll(uriVariables.keySet());
|
||||
Assert.isTrue(variablesSet.isEmpty(),
|
||||
"'uriVariables' does not contain keys for all variables: " + variablesSet);
|
||||
|
||||
Map<UriComponents.Type, String> expandedComponents = new EnumMap<UriComponents.Type, String>(UriComponents.Type.class);
|
||||
|
||||
for (Map.Entry<UriComponents.Type, String> entry : this.uriComponents.entrySet()) {
|
||||
UriComponents.Type key = entry.getKey();
|
||||
String value = entry.getValue();
|
||||
String expandedValue = expandUriComponent(key, value, uriVariables);
|
||||
expandedComponents.put(key, expandedValue);
|
||||
}
|
||||
UriComponents result = UriComponents.fromUriComponentMap(expandedComponents);
|
||||
if (encode) {
|
||||
result = result.encode();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private String expandUriComponent(UriComponents.Type componentType, String value, Map<String, ?> uriVariables) {
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
if (value.indexOf('{') == -1) {
|
||||
return value;
|
||||
}
|
||||
Matcher matcher = NAMES_PATTERN.matcher(value);
|
||||
StringBuffer sb = new StringBuffer();
|
||||
while (matcher.find()) {
|
||||
String match = matcher.group(1);
|
||||
String variableName = getVariableName(match);
|
||||
Object variableValue = uriVariables.get(variableName);
|
||||
String uriVariableValueString = getVariableValueAsString(variableValue);
|
||||
String replacement = Matcher.quoteReplacement(uriVariableValueString);
|
||||
matcher.appendReplacement(sb, replacement);
|
||||
}
|
||||
matcher.appendTail(sb);
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private String getVariableName(String match) {
|
||||
int colonIdx = match.indexOf(':');
|
||||
return colonIdx == -1 ? match : match.substring(0, colonIdx);
|
||||
}
|
||||
|
||||
/**
|
||||
* Template method that returns the string representation of the given URI template value.
|
||||
*
|
||||
* <p>Defaults implementation simply calls {@link Object#toString()}, or returns an empty string for {@code null}.
|
||||
*
|
||||
* @param variableValue the URI template variable value
|
||||
* @return the variable value as string
|
||||
*/
|
||||
protected String getVariableValueAsString(Object variableValue) {
|
||||
return variableValue != null ? variableValue.toString() : "";
|
||||
}
|
||||
|
||||
/**
|
||||
* Given an array of variables, expand this template into a full URI. The array represent variable values.
|
||||
* The order of variables is significant.
|
||||
* <p>Example:
|
||||
|
|
@ -144,7 +212,8 @@ public class UriTemplate implements Serializable {
|
|||
* or if it does not contain sufficient variables
|
||||
*/
|
||||
public URI expand(Object... uriVariableValues) {
|
||||
return encodeUri(expandAsString(uriVariableValues));
|
||||
UriComponents expandedComponents = expandAsUriComponents(uriVariableValues, true);
|
||||
return expandedComponents.toUri();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -157,42 +226,37 @@ public class UriTemplate implements Serializable {
|
|||
* </pre>
|
||||
* will print: <blockquote><code>http://example.com/hotels/1/bookings/42</code></blockquote>
|
||||
*
|
||||
* @param uriVariableValues the array of URI variables
|
||||
* @return the expanded URI
|
||||
*
|
||||
* @param uriVariableValues the array of URI variables
|
||||
* @return the expanded URI
|
||||
* @throws IllegalArgumentException if <code>uriVariables</code> is <code>null</code>
|
||||
* or if it does not contain sufficient variables
|
||||
*/
|
||||
public String expandAsString(Object... uriVariableValues) {
|
||||
Assert.notNull(uriVariableValues, "'uriVariableValues' must not be null");
|
||||
if (uriVariableValues.length < this.variableNames.size()) {
|
||||
throw new IllegalArgumentException(
|
||||
"Not enough of variables values in [" + this.uriTemplate + "]: expected at least " +
|
||||
this.variableNames.size() + "; got " + uriVariableValues.length);
|
||||
}
|
||||
Matcher matcher = NAMES_PATTERN.matcher(this.uriTemplate);
|
||||
StringBuffer uriBuffer = new StringBuffer();
|
||||
int i = 0;
|
||||
while (matcher.find()) {
|
||||
Object uriVariableValue = uriVariableValues[i++];
|
||||
String uriVariableValueString = getVariableValueAsString(uriVariableValue);
|
||||
String replacement = Matcher.quoteReplacement(uriVariableValueString);
|
||||
matcher.appendReplacement(uriBuffer, replacement);
|
||||
}
|
||||
matcher.appendTail(uriBuffer);
|
||||
return uriBuffer.toString();
|
||||
public String expandAsString(boolean encode, Object[] uriVariableValues) {
|
||||
UriComponents expandedComponents = expandAsUriComponents(uriVariableValues, encode);
|
||||
return expandedComponents.toUriString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Template method that returns the string representation of the given URI template value.
|
||||
*
|
||||
* <p>Defaults implementation simply calls {@link Object#toString()}, or returns an empty string for {@code null}.
|
||||
*
|
||||
* @param variableValue the URI template variable value
|
||||
* @return the variable value as string
|
||||
*/
|
||||
protected String getVariableValueAsString(Object variableValue) {
|
||||
return variableValue != null ? variableValue.toString() : "";
|
||||
}
|
||||
public UriComponents expandAsUriComponents(Object[] uriVariableValues, boolean encode) {
|
||||
Assert.notNull(uriVariableValues, "'uriVariableValues' must not be null");
|
||||
if (uriVariableValues.length < this.variableNames.size()) {
|
||||
throw new IllegalArgumentException(
|
||||
"Not enough of variables values in [" + this.uriTemplate + "]: expected at least " +
|
||||
this.variableNames.size() + "; got " + uriVariableValues.length);
|
||||
}
|
||||
Map<String, Object> uriVariables = new LinkedHashMap<String, Object>(this.variableNames.size());
|
||||
|
||||
for (int i = 0, size = variableNames.size(); i < size; i++) {
|
||||
String variableName = variableNames.get(i);
|
||||
Object variableValue = uriVariableValues[i];
|
||||
uriVariables.put(variableName, variableValue);
|
||||
}
|
||||
|
||||
return expandAsUriComponents(uriVariables, encode);
|
||||
}
|
||||
|
||||
|
||||
// matching
|
||||
|
||||
/**
|
||||
* Indicate whether the given URI matches this template.
|
||||
|
|
@ -316,4 +380,5 @@ public class UriTemplate implements Serializable {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,12 +18,6 @@ package org.springframework.web.util;
|
|||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.Collections;
|
||||
import java.util.EnumMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
|
|
@ -44,181 +38,6 @@ import org.springframework.util.Assert;
|
|||
*/
|
||||
public abstract class UriUtils {
|
||||
|
||||
private static final String DEFAULT_ENCODING = "UTF-8";
|
||||
|
||||
private static final String SCHEME_PATTERN = "([^:/?#]+):";
|
||||
|
||||
private static final String HTTP_PATTERN = "(http|https):";
|
||||
|
||||
private static final String USERINFO_PATTERN = "([^@/]*)";
|
||||
|
||||
private static final String HOST_PATTERN = "([^/?#:]*)";
|
||||
|
||||
private static final String PORT_PATTERN = "(\\d*)";
|
||||
|
||||
private static final String PATH_PATTERN = "([^?#]*)";
|
||||
|
||||
private static final String QUERY_PATTERN = "([^#]*)";
|
||||
|
||||
private static final String LAST_PATTERN = "(.*)";
|
||||
|
||||
// Regex patterns that matches URIs. See RFC 3986, appendix B
|
||||
private static final Pattern URI_PATTERN = Pattern.compile(
|
||||
"^(" + SCHEME_PATTERN + ")?" + "(//(" + USERINFO_PATTERN + "@)?" + HOST_PATTERN + "(:" + PORT_PATTERN +
|
||||
")?" + ")?" + PATH_PATTERN + "(\\?" + QUERY_PATTERN + ")?" + "(#" + LAST_PATTERN + ")?");
|
||||
|
||||
private static final Pattern HTTP_URL_PATTERN = Pattern.compile(
|
||||
"^" + HTTP_PATTERN + "(//(" + USERINFO_PATTERN + "@)?" + HOST_PATTERN + "(:" + PORT_PATTERN + ")?" + ")?" +
|
||||
PATH_PATTERN + "(\\?" + LAST_PATTERN + ")?");
|
||||
|
||||
|
||||
// Parsing
|
||||
|
||||
/**
|
||||
* Parses the given source URI into a mapping of URI components to string values.
|
||||
*
|
||||
* @param uri the source URI
|
||||
* @return the URI components of the URI
|
||||
*/
|
||||
public static UriComponents parseUriComponents(String uri) {
|
||||
Assert.notNull(uri, "'uri' must not be null");
|
||||
Matcher m = URI_PATTERN.matcher(uri);
|
||||
if (m.matches()) {
|
||||
Map<UriComponents.Type, String> result = new EnumMap<UriComponents.Type, String>(UriComponents.Type.class);
|
||||
|
||||
result.put(UriComponents.Type.SCHEME, m.group(2));
|
||||
result.put(UriComponents.Type.AUTHORITY, m.group(3));
|
||||
result.put(UriComponents.Type.USER_INFO, m.group(5));
|
||||
result.put(UriComponents.Type.HOST, m.group(6));
|
||||
result.put(UriComponents.Type.PORT, m.group(8));
|
||||
result.put(UriComponents.Type.PATH, m.group(9));
|
||||
result.put(UriComponents.Type.QUERY, m.group(11));
|
||||
result.put(UriComponents.Type.FRAGMENT, m.group(13));
|
||||
|
||||
return new UriComponents(result);
|
||||
}
|
||||
else {
|
||||
throw new IllegalArgumentException("[" + uri + "] is not a valid URI");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the given source HTTP URL into a mapping of URI components to string values.
|
||||
|
||||
* <p><strong>Note</strong> that the returned map will contain a mapping for
|
||||
* {@link org.springframework.web.util.UriComponents.Type#FRAGMENT}, as fragments are not supposed to be sent to the
|
||||
* server, but retained by the client.
|
||||
*
|
||||
* @param httpUrl the source URI
|
||||
* @return the URI components of the URI
|
||||
*/
|
||||
public static UriComponents parseHttpUrlComponents(String httpUrl) {
|
||||
Assert.notNull(httpUrl, "'httpUrl' must not be null");
|
||||
Matcher m = HTTP_URL_PATTERN.matcher(httpUrl);
|
||||
if (m.matches()) {
|
||||
Map<UriComponents.Type, String> result = new EnumMap<UriComponents.Type, String>(UriComponents.Type.class);
|
||||
|
||||
result.put(UriComponents.Type.SCHEME, m.group(1));
|
||||
result.put(UriComponents.Type.AUTHORITY, m.group(2));
|
||||
result.put(UriComponents.Type.USER_INFO, m.group(4));
|
||||
result.put(UriComponents.Type.HOST, m.group(5));
|
||||
result.put(UriComponents.Type.PORT, m.group(7));
|
||||
result.put(UriComponents.Type.PATH, m.group(8));
|
||||
result.put(UriComponents.Type.QUERY, m.group(10));
|
||||
|
||||
return new UriComponents(result);
|
||||
}
|
||||
else {
|
||||
throw new IllegalArgumentException("[" + httpUrl + "] is not a valid HTTP URL");
|
||||
}
|
||||
}
|
||||
|
||||
// building
|
||||
|
||||
/**
|
||||
* Builds a URI from the given URI components. The given map should contain at least one entry.
|
||||
*
|
||||
* <p><strong>Note</strong> that {@link org.springframework.web.util.UriComponents.Type#PATH_SEGMENT} and {@link org.springframework.web.util.UriComponents.Type#QUERY_PARAM} keys (if any)
|
||||
* will not be used to build the URI, in favor of {@link org.springframework.web.util.UriComponents.Type#PATH} and {@link org.springframework.web.util.UriComponents.Type#QUERY}
|
||||
* respectively.
|
||||
*
|
||||
* @param uriComponents the components to build the URI out of
|
||||
* @return the URI created from the given components
|
||||
*/
|
||||
public static String buildUri(Map<UriComponents.Type, String> uriComponents) {
|
||||
Assert.notEmpty(uriComponents, "'uriComponents' must not be empty");
|
||||
|
||||
return buildUri(uriComponents.get(UriComponents.Type.SCHEME), uriComponents.get(
|
||||
UriComponents.Type.AUTHORITY),
|
||||
uriComponents.get(UriComponents.Type.USER_INFO), uriComponents.get(UriComponents.Type.HOST),
|
||||
uriComponents.get(UriComponents.Type.PORT), uriComponents.get(UriComponents.Type.PATH),
|
||||
uriComponents.get(UriComponents.Type.QUERY), uriComponents.get(UriComponents.Type.FRAGMENT));
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a URI from the given URI component parameters. All parameters can be {@code null}.
|
||||
*
|
||||
* @param scheme the scheme
|
||||
* @param authority the authority
|
||||
* @param userinfo the user info
|
||||
* @param host the host
|
||||
* @param port the port
|
||||
* @param path the path
|
||||
* @param query the query
|
||||
* @param fragment the fragment
|
||||
* @return the URI created from the given components
|
||||
*/
|
||||
public static String buildUri(String scheme,
|
||||
String authority,
|
||||
String userinfo,
|
||||
String host,
|
||||
String port,
|
||||
String path,
|
||||
String query,
|
||||
String fragment) {
|
||||
StringBuilder uriBuilder = new StringBuilder();
|
||||
|
||||
if (scheme != null) {
|
||||
uriBuilder.append(scheme);
|
||||
uriBuilder.append(':');
|
||||
}
|
||||
|
||||
if (userinfo != null || host != null || port != null) {
|
||||
uriBuilder.append("//");
|
||||
if (userinfo != null) {
|
||||
uriBuilder.append(userinfo);
|
||||
uriBuilder.append('@');
|
||||
}
|
||||
if (host != null) {
|
||||
uriBuilder.append(host);
|
||||
}
|
||||
if (port != null) {
|
||||
uriBuilder.append(':');
|
||||
uriBuilder.append(port);
|
||||
}
|
||||
}
|
||||
else if (authority != null) {
|
||||
uriBuilder.append("//");
|
||||
uriBuilder.append(authority);
|
||||
}
|
||||
|
||||
if (path != null) {
|
||||
uriBuilder.append(path);
|
||||
}
|
||||
|
||||
if (query != null) {
|
||||
uriBuilder.append('?');
|
||||
uriBuilder.append(query);
|
||||
}
|
||||
|
||||
if (fragment != null) {
|
||||
uriBuilder.append('#');
|
||||
uriBuilder.append(fragment);
|
||||
}
|
||||
|
||||
return uriBuilder.toString();
|
||||
}
|
||||
|
||||
// encoding
|
||||
|
||||
/**
|
||||
|
|
@ -232,9 +51,10 @@ public abstract class UriUtils {
|
|||
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
|
||||
*/
|
||||
public static String encodeUri(String uri, String encoding) throws UnsupportedEncodingException {
|
||||
Map<UriComponents.Type, String> uriComponents = parseUriComponents(uri);
|
||||
return encodeUriComponents(uriComponents, encoding);
|
||||
}
|
||||
UriComponents uriComponents = UriComponents.fromUriString(uri);
|
||||
UriComponents encoded = uriComponents.encode(encoding);
|
||||
return encoded.toUriString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Encodes the given HTTP URI into an encoded String. All various URI components are encoded according to their
|
||||
|
|
@ -248,33 +68,9 @@ public abstract class UriUtils {
|
|||
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
|
||||
*/
|
||||
public static String encodeHttpUrl(String httpUrl, String encoding) throws UnsupportedEncodingException {
|
||||
Map<UriComponents.Type, String> uriComponents = parseHttpUrlComponents(httpUrl);
|
||||
return encodeUriComponents(uriComponents, encoding);
|
||||
}
|
||||
|
||||
/**
|
||||
* Encodes the given source URI components into an encoded String. All various URI components are optional, but encoded
|
||||
* according to their respective valid character sets.
|
||||
*
|
||||
* @param uriComponents the URI components
|
||||
* @param encoding the character encoding to encode to
|
||||
* @return the encoded URI
|
||||
* @throws IllegalArgumentException when the given uri parameter is not a valid URI
|
||||
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
|
||||
*/
|
||||
public static String encodeUriComponents(Map<UriComponents.Type, String> uriComponents,
|
||||
String encoding) throws UnsupportedEncodingException {
|
||||
Assert.notEmpty(uriComponents, "'uriComponents' must not be empty");
|
||||
Assert.hasLength(encoding, "'encoding' must not be empty");
|
||||
|
||||
Map<UriComponents.Type, String> encodedUriComponents = new EnumMap<UriComponents.Type, String>(UriComponents.Type.class);
|
||||
for (Map.Entry<UriComponents.Type, String> entry : uriComponents.entrySet()) {
|
||||
if (entry.getValue() != null) {
|
||||
String encodedValue = encodeUriComponent(entry.getValue(), encoding, entry.getKey(), null);
|
||||
encodedUriComponents.put(entry.getKey(), encodedValue);
|
||||
}
|
||||
}
|
||||
return buildUri(encodedUriComponents);
|
||||
UriComponents uriComponents = UriComponents.fromHttpUrl(httpUrl);
|
||||
UriComponents encoded = uriComponents.encode(encoding);
|
||||
return encoded.toUriString();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -303,141 +99,10 @@ public abstract class UriUtils {
|
|||
String query,
|
||||
String fragment,
|
||||
String encoding) throws UnsupportedEncodingException {
|
||||
Assert.hasLength(encoding, "'encoding' must not be empty");
|
||||
UriComponents uriComponents = UriComponents.fromUriComponents(scheme, authority, userInfo, host, port, path, query, fragment);
|
||||
UriComponents encoded = uriComponents.encode(encoding);
|
||||
|
||||
if (scheme != null) {
|
||||
scheme = encodeScheme(scheme, encoding);
|
||||
}
|
||||
if (authority != null) {
|
||||
authority = encodeAuthority(authority, encoding);
|
||||
}
|
||||
if (userInfo != null) {
|
||||
userInfo = encodeUserInfo(userInfo, encoding);
|
||||
}
|
||||
if (host != null) {
|
||||
host = encodeHost(host, encoding);
|
||||
}
|
||||
if (port != null) {
|
||||
port = encodePort(port, encoding);
|
||||
}
|
||||
if (path != null) {
|
||||
path = encodePath(path, encoding);
|
||||
}
|
||||
if (query != null) {
|
||||
query = encodeQuery(query, encoding);
|
||||
}
|
||||
if (fragment != null) {
|
||||
fragment = encodeFragment(fragment, encoding);
|
||||
}
|
||||
return buildUri(scheme, authority, userInfo, host, port, path, query, fragment);
|
||||
}
|
||||
|
||||
/**
|
||||
* Encodes the given source into an encoded String using the rules specified by the given component.
|
||||
*
|
||||
* @param source the source string
|
||||
* @param uriComponent the URI component for the source
|
||||
* @return the encoded URI
|
||||
* @throws IllegalArgumentException when the given uri parameter is not a valid URI
|
||||
*/
|
||||
public static String encodeUriComponent(String source, UriComponents.Type uriComponent) {
|
||||
return encodeUriComponent(source, uriComponent, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Encodes the given source into an encoded String using the rules specified by the given component and with the
|
||||
* given options.
|
||||
*
|
||||
* @param source the source string
|
||||
* @param encoding the encoding of the source string
|
||||
* @param uriComponent the URI component for the source
|
||||
* @param encodingOptions the options used when encoding. May be {@code null}.
|
||||
* @return the encoded URI
|
||||
* @throws IllegalArgumentException when the given uri parameter is not a valid URI
|
||||
* @see EncodingOption
|
||||
*/
|
||||
public static String encodeUriComponent(String source,
|
||||
UriComponents.Type uriComponent,
|
||||
Set<EncodingOption> encodingOptions) {
|
||||
try {
|
||||
return encodeUriComponent(source, DEFAULT_ENCODING, uriComponent, encodingOptions);
|
||||
}
|
||||
catch (UnsupportedEncodingException ex) {
|
||||
throw new InternalError("\"" + DEFAULT_ENCODING + "\" not supported");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Encodes the given source into an encoded String using the rules specified by the given component.
|
||||
*
|
||||
* @param source the source string
|
||||
* @param encoding the encoding of the source string
|
||||
* @param uriComponent the URI component for the source
|
||||
* @return the encoded URI
|
||||
* @throws IllegalArgumentException when the given uri parameter is not a valid URI
|
||||
*/
|
||||
public static String encodeUriComponent(String source,
|
||||
String encoding,
|
||||
UriComponents.Type uriComponent) throws UnsupportedEncodingException {
|
||||
return encodeUriComponent(source, encoding, uriComponent, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Encodes the given source into an encoded String using the rules specified by the given component and with the
|
||||
* given options.
|
||||
*
|
||||
* @param source the source string
|
||||
* @param encoding the encoding of the source string
|
||||
* @param uriComponent the URI component for the source
|
||||
* @param encodingOptions the options used when encoding. May be {@code null}.
|
||||
* @return the encoded URI
|
||||
* @throws IllegalArgumentException when the given uri parameter is not a valid URI
|
||||
* @see EncodingOption
|
||||
*/
|
||||
public static String encodeUriComponent(String source,
|
||||
String encoding,
|
||||
UriComponents.Type uriComponent,
|
||||
Set<EncodingOption> encodingOptions) throws UnsupportedEncodingException {
|
||||
Assert.hasLength(encoding, "'encoding' must not be empty");
|
||||
|
||||
byte[] bytes = encodeInternal(source.getBytes(encoding), uriComponent, encodingOptions);
|
||||
return new String(bytes, "US-ASCII");
|
||||
}
|
||||
|
||||
private static byte[] encodeInternal(byte[] source,
|
||||
UriComponents.Type uriComponent,
|
||||
Set<EncodingOption> encodingOptions) {
|
||||
Assert.notNull(source, "'source' must not be null");
|
||||
Assert.notNull(uriComponent, "'uriComponent' must not be null");
|
||||
|
||||
if (encodingOptions == null) {
|
||||
encodingOptions = Collections.emptySet();
|
||||
}
|
||||
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream(source.length);
|
||||
for (int i = 0; i < source.length; i++) {
|
||||
int b = source[i];
|
||||
if (b < 0) {
|
||||
b += 256;
|
||||
}
|
||||
if (uriComponent.isAllowed(b)) {
|
||||
bos.write(b);
|
||||
}
|
||||
else if (encodingOptions.contains(EncodingOption.ALLOW_TEMPLATE_VARS) && (b == '{' || b == '}')) {
|
||||
bos.write(b);
|
||||
}
|
||||
else {
|
||||
bos.write('%');
|
||||
|
||||
char hex1 = Character.toUpperCase(Character.forDigit((b >> 4) & 0xF, 16));
|
||||
char hex2 = Character.toUpperCase(Character.forDigit(b & 0xF, 16));
|
||||
|
||||
bos.write(hex1);
|
||||
bos.write(hex2);
|
||||
}
|
||||
}
|
||||
return bos.toByteArray();
|
||||
return encoded.toUriString();
|
||||
}
|
||||
|
||||
// encoding convenience methods
|
||||
|
|
@ -451,7 +116,7 @@ public abstract class UriUtils {
|
|||
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
|
||||
*/
|
||||
public static String encodeScheme(String scheme, String encoding) throws UnsupportedEncodingException {
|
||||
return encodeUriComponent(scheme, encoding, UriComponents.Type.SCHEME, null);
|
||||
return UriComponents.encodeUriComponent(scheme, encoding, UriComponents.Type.SCHEME);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -463,7 +128,7 @@ public abstract class UriUtils {
|
|||
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
|
||||
*/
|
||||
public static String encodeAuthority(String authority, String encoding) throws UnsupportedEncodingException {
|
||||
return encodeUriComponent(authority, encoding, UriComponents.Type.AUTHORITY, null);
|
||||
return UriComponents.encodeUriComponent(authority, encoding, UriComponents.Type.AUTHORITY);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -475,7 +140,7 @@ public abstract class UriUtils {
|
|||
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
|
||||
*/
|
||||
public static String encodeUserInfo(String userInfo, String encoding) throws UnsupportedEncodingException {
|
||||
return encodeUriComponent(userInfo, encoding, UriComponents.Type.USER_INFO, null);
|
||||
return UriComponents.encodeUriComponent(userInfo, encoding, UriComponents.Type.USER_INFO);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -487,7 +152,7 @@ public abstract class UriUtils {
|
|||
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
|
||||
*/
|
||||
public static String encodeHost(String host, String encoding) throws UnsupportedEncodingException {
|
||||
return encodeUriComponent(host, encoding, UriComponents.Type.HOST, null);
|
||||
return UriComponents.encodeUriComponent(host, encoding, UriComponents.Type.HOST);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -499,7 +164,7 @@ public abstract class UriUtils {
|
|||
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
|
||||
*/
|
||||
public static String encodePort(String port, String encoding) throws UnsupportedEncodingException {
|
||||
return encodeUriComponent(port, encoding, UriComponents.Type.PORT, null);
|
||||
return UriComponents.encodeUriComponent(port, encoding, UriComponents.Type.PORT);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -511,7 +176,7 @@ public abstract class UriUtils {
|
|||
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
|
||||
*/
|
||||
public static String encodePath(String path, String encoding) throws UnsupportedEncodingException {
|
||||
return encodeUriComponent(path, encoding, UriComponents.Type.PATH, null);
|
||||
return UriComponents.encodeUriComponent(path, encoding, UriComponents.Type.PATH);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -523,7 +188,7 @@ public abstract class UriUtils {
|
|||
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
|
||||
*/
|
||||
public static String encodePathSegment(String segment, String encoding) throws UnsupportedEncodingException {
|
||||
return encodeUriComponent(segment, encoding, UriComponents.Type.PATH_SEGMENT, null);
|
||||
return UriComponents.encodeUriComponent(segment, encoding, UriComponents.Type.PATH_SEGMENT);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -535,7 +200,7 @@ public abstract class UriUtils {
|
|||
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
|
||||
*/
|
||||
public static String encodeQuery(String query, String encoding) throws UnsupportedEncodingException {
|
||||
return encodeUriComponent(query, encoding, UriComponents.Type.QUERY, null);
|
||||
return UriComponents.encodeUriComponent(query, encoding, UriComponents.Type.QUERY);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -547,7 +212,7 @@ public abstract class UriUtils {
|
|||
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
|
||||
*/
|
||||
public static String encodeQueryParam(String queryParam, String encoding) throws UnsupportedEncodingException {
|
||||
return encodeUriComponent(queryParam, encoding, UriComponents.Type.QUERY_PARAM, null);
|
||||
return UriComponents.encodeUriComponent(queryParam, encoding, UriComponents.Type.QUERY_PARAM);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -559,7 +224,7 @@ public abstract class UriUtils {
|
|||
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
|
||||
*/
|
||||
public static String encodeFragment(String fragment, String encoding) throws UnsupportedEncodingException {
|
||||
return encodeUriComponent(fragment, encoding, UriComponents.Type.FRAGMENT, null);
|
||||
return UriComponents.encodeUriComponent(fragment, encoding, UriComponents.Type.FRAGMENT);
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -609,16 +274,4 @@ public abstract class UriUtils {
|
|||
return changed ? new String(bos.toByteArray(), encoding) : source;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enumeration used to control how URIs are encoded.
|
||||
*/
|
||||
public enum EncodingOption {
|
||||
|
||||
/**
|
||||
* Allow for URI template variables to occur in the URI component (i.e. '{foo}')
|
||||
*/
|
||||
ALLOW_TEMPLATE_VARS
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,131 +0,0 @@
|
|||
/*
|
||||
* Copyright 2002-2011 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.util;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/** @author Arjen Poutsma */
|
||||
public class UriBuilderTests {
|
||||
|
||||
@Test
|
||||
public void plain() throws URISyntaxException {
|
||||
UriBuilder builder = UriBuilder.newInstance();
|
||||
URI result = builder.scheme("http").host("example.com").path("foo").queryParam("bar").fragment("baz").build();
|
||||
|
||||
URI expected = new URI("http://example.com/foo?bar#baz");
|
||||
assertEquals("Invalid result URI", expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fromPath() throws URISyntaxException {
|
||||
URI result = UriBuilder.fromPath("foo").queryParam("bar").fragment("baz").build();
|
||||
|
||||
URI expected = new URI("/foo?bar#baz");
|
||||
assertEquals("Invalid result URI", expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fromUri() throws URISyntaxException {
|
||||
URI uri = new URI("http://example.com/foo?bar#baz");
|
||||
|
||||
URI result = UriBuilder.fromUri(uri).build();
|
||||
assertEquals("Invalid result URI", uri, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore("Working on it")
|
||||
public void templateVarsVarArgs() throws URISyntaxException {
|
||||
URI result = UriBuilder.fromPath("/{foo}/{bar}").build("baz", "qux");
|
||||
|
||||
URI expected = new URI("http://example.com/baz/qux");
|
||||
assertEquals("Invalid result URI", expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void templateVarsEncoded() throws URISyntaxException, UnsupportedEncodingException {
|
||||
URI result = UriBuilder.fromPath("{foo}").build("bar baz");
|
||||
|
||||
URI expected = new URI("/bar%20baz");
|
||||
assertEquals("Invalid result URI", expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void templateVarsNotEncoded() throws URISyntaxException {
|
||||
UriBuilder builder = UriBuilder.newInstance();
|
||||
URI result = builder.scheme("http").host("example.com").path("{foo}").buildFromEncoded("bar%20baz");
|
||||
|
||||
URI expected = new URI("http://example.com/bar%20baz");
|
||||
assertEquals("Invalid result URI", expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void templateVarsMap() throws URISyntaxException {
|
||||
Map<String, String> vars = new HashMap<String, String>(2);
|
||||
vars.put("bar", "qux");
|
||||
vars.put("foo", "baz");
|
||||
URI result = UriBuilder.fromPath("/{foo}/{bar}").build(vars);
|
||||
URI expected = new URI("/baz/qux");
|
||||
assertEquals("Invalid result URI", expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unusedTemplateVars() throws URISyntaxException {
|
||||
UriBuilder builder = UriBuilder.newInstance();
|
||||
URI result = builder.scheme("http").host("example.com").path("{foo}").build();
|
||||
|
||||
URI expected = new URI("http://example.com/%7Bfoo%7D");
|
||||
assertEquals("Invalid result URI", expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pathSegments() throws URISyntaxException {
|
||||
UriBuilder builder = UriBuilder.newInstance();
|
||||
URI result = builder.pathSegment("foo").pathSegment("bar").build();
|
||||
|
||||
URI expected = new URI("/foo/bar");
|
||||
assertEquals("Invalid result URI", expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void queryParam() throws URISyntaxException {
|
||||
UriBuilder builder = UriBuilder.newInstance();
|
||||
URI result = builder.queryParam("baz", "qux", 42).build();
|
||||
|
||||
URI expected = new URI("?baz=qux&baz=42");
|
||||
assertEquals("Invalid result URI", expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void emptyQueryParam() throws URISyntaxException {
|
||||
UriBuilder builder = UriBuilder.newInstance();
|
||||
URI result = builder.queryParam("baz").build();
|
||||
|
||||
URI expected = new URI("?baz");
|
||||
assertEquals("Invalid result URI", expected, result);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,110 @@
|
|||
/*
|
||||
* Copyright 2002-2011 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.util;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/** @author Arjen Poutsma */
|
||||
public class UriComponentsBuilderTests {
|
||||
|
||||
@Test
|
||||
public void plain() throws URISyntaxException {
|
||||
UriComponentsBuilder builder = UriComponentsBuilder.newInstance();
|
||||
UriComponents result = builder.scheme("http").host("example.com").path("foo").queryParam("bar").fragment("baz").build();
|
||||
assertEquals("http", result.getScheme());
|
||||
assertEquals("example.com", result.getHost());
|
||||
assertEquals("/foo", result.getPath());
|
||||
assertEquals("bar", result.getQuery());
|
||||
assertEquals("baz", result.getFragment());
|
||||
|
||||
URI expected = new URI("http://example.com/foo?bar#baz");
|
||||
assertEquals("Invalid result URI", expected, result.toUri());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fromPath() throws URISyntaxException {
|
||||
UriComponents result = UriComponentsBuilder.fromPath("foo").queryParam("bar").fragment("baz").build();
|
||||
assertEquals("/foo", result.getPath());
|
||||
assertEquals("bar", result.getQuery());
|
||||
assertEquals("baz", result.getFragment());
|
||||
|
||||
URI expected = new URI("/foo?bar#baz");
|
||||
assertEquals("Invalid result URI", expected, result.toUri());
|
||||
|
||||
result = UriComponentsBuilder.fromPath("/foo").build();
|
||||
assertEquals("/foo", result.getPath());
|
||||
|
||||
expected = new URI("/foo");
|
||||
assertEquals("Invalid result URI", expected, result.toUri());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fromUri() throws URISyntaxException {
|
||||
URI uri = new URI("http://example.com/foo?bar#baz");
|
||||
UriComponents result = UriComponentsBuilder.fromUri(uri).build();
|
||||
assertEquals("http", result.getScheme());
|
||||
assertEquals("example.com", result.getHost());
|
||||
assertEquals("/foo", result.getPath());
|
||||
assertEquals("bar", result.getQuery());
|
||||
assertEquals("baz", result.getFragment());
|
||||
|
||||
assertEquals("Invalid result URI", uri, result.toUri());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pathSegments() throws URISyntaxException {
|
||||
UriComponentsBuilder builder = UriComponentsBuilder.newInstance();
|
||||
URI result = builder.pathSegment("foo").pathSegment("bar").build().toUri();
|
||||
|
||||
URI expected = new URI("/foo/bar");
|
||||
assertEquals("Invalid result URI", expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void queryParam() throws URISyntaxException {
|
||||
UriComponentsBuilder builder = UriComponentsBuilder.newInstance();
|
||||
URI result = builder.queryParam("baz", "qux", 42).build().toUri();
|
||||
|
||||
URI expected = new URI("?baz=qux&baz=42");
|
||||
assertEquals("Invalid result URI", expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void emptyQueryParam() throws URISyntaxException {
|
||||
UriComponentsBuilder builder = UriComponentsBuilder.newInstance();
|
||||
URI result = builder.queryParam("baz").build().toUri();
|
||||
|
||||
URI expected = new URI("?baz");
|
||||
assertEquals("Invalid result URI", expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void combineWithUriTemplate() throws URISyntaxException {
|
||||
UriComponentsBuilder builder = UriComponentsBuilder.fromPath("/{foo}");
|
||||
UriComponents components = builder.build();
|
||||
UriTemplate template = new UriTemplate(components);
|
||||
URI uri = template.expand("bar baz");
|
||||
assertEquals(new URI("/bar%20baz"), uri);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -16,58 +16,105 @@
|
|||
|
||||
package org.springframework.web.util;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
/** @author Arjen Poutsma */
|
||||
public class UriComponentsTests {
|
||||
|
||||
private UriComponents components;
|
||||
@Test
|
||||
public void fromUri() {
|
||||
Map<UriComponents.Type, String> result = UriComponents.fromUriString("http://www.ietf.org/rfc/rfc3986.txt");
|
||||
assertEquals("http", result.get(UriComponents.Type.SCHEME));
|
||||
assertNull(result.get(UriComponents.Type.USER_INFO));
|
||||
assertEquals("www.ietf.org", result.get(UriComponents.Type.HOST));
|
||||
assertNull(result.get(UriComponents.Type.PORT));
|
||||
assertEquals("/rfc/rfc3986.txt", result.get(UriComponents.Type.PATH));
|
||||
assertNull(result.get(UriComponents.Type.QUERY));
|
||||
assertNull(result.get(UriComponents.Type.FRAGMENT));
|
||||
|
||||
result = UriComponents.fromUriString(
|
||||
"http://arjen:foobar@java.sun.com:80/javase/6/docs/api/java/util/BitSet.html?foo=bar#and(java.util.BitSet)");
|
||||
assertEquals("http", result.get(UriComponents.Type.SCHEME));
|
||||
assertEquals("arjen:foobar", result.get(UriComponents.Type.USER_INFO));
|
||||
assertEquals("java.sun.com", result.get(UriComponents.Type.HOST));
|
||||
assertEquals("80", result.get(UriComponents.Type.PORT));
|
||||
assertEquals("/javase/6/docs/api/java/util/BitSet.html", result.get(UriComponents.Type.PATH));
|
||||
assertEquals("foo=bar", result.get(UriComponents.Type.QUERY));
|
||||
assertEquals("and(java.util.BitSet)", result.get(UriComponents.Type.FRAGMENT));
|
||||
|
||||
result = UriComponents.fromUriString("mailto:java-net@java.sun.com");
|
||||
assertEquals("mailto", result.get(UriComponents.Type.SCHEME));
|
||||
assertNull(result.get(UriComponents.Type.USER_INFO));
|
||||
assertNull(result.get(UriComponents.Type.HOST));
|
||||
assertNull(result.get(UriComponents.Type.PORT));
|
||||
assertEquals("java-net@java.sun.com", result.get(UriComponents.Type.PATH));
|
||||
assertNull(result.get(UriComponents.Type.QUERY));
|
||||
assertNull(result.get(UriComponents.Type.FRAGMENT));
|
||||
|
||||
result = UriComponents.fromUriString("docs/guide/collections/designfaq.html#28");
|
||||
assertNull(result.get(UriComponents.Type.SCHEME));
|
||||
assertNull(result.get(UriComponents.Type.USER_INFO));
|
||||
assertNull(result.get(UriComponents.Type.HOST));
|
||||
assertNull(result.get(UriComponents.Type.PORT));
|
||||
assertEquals("docs/guide/collections/designfaq.html", result.get(UriComponents.Type.PATH));
|
||||
assertNull(result.get(UriComponents.Type.QUERY));
|
||||
assertEquals("28", result.get(UriComponents.Type.FRAGMENT));
|
||||
}
|
||||
|
||||
@Before
|
||||
public void createComponents() {
|
||||
components = new UriComponents();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pathSegments() {
|
||||
String path = "/foo/bar";
|
||||
components.setPath(path);
|
||||
String path = "/foo/bar";
|
||||
UriComponents components = UriComponents.fromUriComponentMap(Collections.singletonMap(UriComponents.Type.PATH, path));
|
||||
List<String> expected = Arrays.asList("foo", "bar");
|
||||
|
||||
List<String> pathSegments = components.getPathSegments();
|
||||
assertEquals(expected, pathSegments);
|
||||
|
||||
components.setPath(null);
|
||||
|
||||
components.setPathSegments(expected);
|
||||
assertEquals(path, components.getPath());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void queryParams() {
|
||||
String query = "foo=bar&foo=baz&qux";
|
||||
components.setQuery(query);
|
||||
UriComponents components = UriComponents.fromUriComponentMap(
|
||||
Collections.singletonMap(UriComponents.Type.QUERY, query));
|
||||
MultiValueMap<String, String> expected = new LinkedMultiValueMap<String, String>(1);
|
||||
expected.put("foo", Arrays.asList("bar", "baz"));
|
||||
expected.set("qux", null);
|
||||
|
||||
MultiValueMap<String, String> result = components.getQueryParams();
|
||||
assertEquals(expected, result);
|
||||
|
||||
components.setQuery(null);
|
||||
|
||||
components.setQueryParams(expected);
|
||||
assertEquals(query, components.getQuery());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encode() {
|
||||
UriComponents uriComponents = UriComponents.fromUriString("http://example.com/hotel list");
|
||||
UriComponents encoded = uriComponents.encode();
|
||||
assertEquals("/hotel%20list", encoded.getPath());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toUriEncoded() throws URISyntaxException {
|
||||
UriComponents uriComponents = UriComponents.fromUriString("http://example.com/hotel list/Z\u00fcrich");
|
||||
UriComponents encoded = uriComponents.encode();
|
||||
assertEquals(new URI("http://example.com/hotel%20list/Z%C3%BCrich"), encoded.toUri());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toUriNotEncoded() throws URISyntaxException {
|
||||
UriComponents uriComponents = UriComponents.fromUriString("http://example.com/hotel list/Z\u00fcrich");
|
||||
assertEquals(new URI("http://example.com/hotel%20list/Z\u00fcrich"), uriComponents.toUri());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -53,6 +53,16 @@ public class UriTemplateTests {
|
|||
template.expand("1");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void expandMap() throws Exception {
|
||||
Map<String, String> uriVariables = new HashMap<String, String>(2);
|
||||
uriVariables.put("booking", "42");
|
||||
uriVariables.put("hotel", "1");
|
||||
UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}");
|
||||
URI result = template.expand(uriVariables);
|
||||
assertEquals("Invalid expanded template", new URI("http://example.com/hotels/1/bookings/42"), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void expandMapDuplicateVariables() throws Exception {
|
||||
UriTemplate template = new UriTemplate("/order/{c}/{c}/{c}");
|
||||
|
|
@ -61,16 +71,6 @@ public class UriTemplateTests {
|
|||
assertEquals("Invalid expanded template", new URI("/order/cheeseburger/cheeseburger/cheeseburger"), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void expandMap() throws Exception {
|
||||
Map<String, String> uriVariables = new HashMap<String, String>(2);
|
||||
uriVariables.put("booking", "42");
|
||||
uriVariables.put("hotel", "1");
|
||||
UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}");
|
||||
URI result = template.expand(uriVariables);
|
||||
assertEquals("Invalid expanded template", new URI("http://example.com/hotels/1/bookings/42"), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void expandMapNonString() throws Exception {
|
||||
Map<String, Integer> uriVariables = new HashMap<String, Integer>(2);
|
||||
|
|
@ -80,6 +80,15 @@ public class UriTemplateTests {
|
|||
URI result = template.expand(uriVariables);
|
||||
assertEquals("Invalid expanded template", new URI("http://example.com/hotels/1/bookings/42"), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void expandMapEncoded() throws Exception {
|
||||
Map<String, String> uriVariables = Collections.singletonMap("hotel", "Z\u00fcrich");
|
||||
UriTemplate template = new UriTemplate("http://example.com/hotel list/{hotel}");
|
||||
URI result = template.expand(uriVariables);
|
||||
assertEquals("Invalid expanded template", new URI("http://example.com/hotel%20list/Z%C3%BCrich"), result);
|
||||
}
|
||||
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void expandMapInvalidAmountVariables() throws Exception {
|
||||
|
|
|
|||
|
|
@ -17,11 +17,10 @@
|
|||
package org.springframework.web.util;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
|
|
@ -31,45 +30,6 @@ public class UriUtilsTests {
|
|||
private static final String ENC = "UTF-8";
|
||||
|
||||
|
||||
@Test
|
||||
public void parseUriComponents() {
|
||||
Map<UriComponents.Type, String> result = UriUtils.parseUriComponents("http://www.ietf.org/rfc/rfc3986.txt");
|
||||
assertEquals("http", result.get(UriComponents.Type.SCHEME));
|
||||
assertNull(result.get(UriComponents.Type.USER_INFO));
|
||||
assertEquals("www.ietf.org", result.get(UriComponents.Type.HOST));
|
||||
assertNull(result.get(UriComponents.Type.PORT));
|
||||
assertEquals("/rfc/rfc3986.txt", result.get(UriComponents.Type.PATH));
|
||||
assertNull(result.get(UriComponents.Type.QUERY));
|
||||
assertNull(result.get(UriComponents.Type.FRAGMENT));
|
||||
|
||||
result = UriUtils.parseUriComponents(
|
||||
"http://arjen:foobar@java.sun.com:80/javase/6/docs/api/java/util/BitSet.html?foo=bar#and(java.util.BitSet)");
|
||||
assertEquals("http", result.get(UriComponents.Type.SCHEME));
|
||||
assertEquals("arjen:foobar", result.get(UriComponents.Type.USER_INFO));
|
||||
assertEquals("java.sun.com", result.get(UriComponents.Type.HOST));
|
||||
assertEquals("80", result.get(UriComponents.Type.PORT));
|
||||
assertEquals("/javase/6/docs/api/java/util/BitSet.html", result.get(UriComponents.Type.PATH));
|
||||
assertEquals("foo=bar", result.get(UriComponents.Type.QUERY));
|
||||
assertEquals("and(java.util.BitSet)", result.get(UriComponents.Type.FRAGMENT));
|
||||
|
||||
result = UriUtils.parseUriComponents("mailto:java-net@java.sun.com");
|
||||
assertEquals("mailto", result.get(UriComponents.Type.SCHEME));
|
||||
assertNull(result.get(UriComponents.Type.USER_INFO));
|
||||
assertNull(result.get(UriComponents.Type.HOST));
|
||||
assertNull(result.get(UriComponents.Type.PORT));
|
||||
assertEquals("java-net@java.sun.com", result.get(UriComponents.Type.PATH));
|
||||
assertNull(result.get(UriComponents.Type.QUERY));
|
||||
assertNull(result.get(UriComponents.Type.FRAGMENT));
|
||||
|
||||
result = UriUtils.parseUriComponents("docs/guide/collections/designfaq.html#28");
|
||||
assertNull(result.get(UriComponents.Type.SCHEME));
|
||||
assertNull(result.get(UriComponents.Type.USER_INFO));
|
||||
assertNull(result.get(UriComponents.Type.HOST));
|
||||
assertNull(result.get(UriComponents.Type.PORT));
|
||||
assertEquals("docs/guide/collections/designfaq.html", result.get(UriComponents.Type.PATH));
|
||||
assertNull(result.get(UriComponents.Type.QUERY));
|
||||
assertEquals("28", result.get(UriComponents.Type.FRAGMENT));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodeScheme() throws UnsupportedEncodingException {
|
||||
|
|
|
|||
Loading…
Reference in New Issue