diff --git a/org.springframework.web/src/main/java/org/springframework/web/util/UriTemplate.java b/org.springframework.web/src/main/java/org/springframework/web/util/UriTemplate.java
index ea3a0c7be72..2bc3521e088 100644
--- a/org.springframework.web/src/main/java/org/springframework/web/util/UriTemplate.java
+++ b/org.springframework.web/src/main/java/org/springframework/web/util/UriTemplate.java
@@ -29,37 +29,31 @@ import java.util.regex.Pattern;
import org.springframework.util.Assert;
/**
- * Represents a URI template. An URI template is a URI-like String that contained variables
- * marked of in braces ({, }), which can be expanded to produce a URI.
- *
See {@link #expand(Map)}, {@link #expand(String[])}, and {@link #match(String)} for example usages.
+ * Represents a URI template. An URI template is a URI-like String that contained variables marked of in braces
+ * ({, }), which can be expanded to produce a URI.
See {@link #expand(Map)}, {@link
+ * #expand(String[])}, and {@link #match(String)} for example usages.
*
* @author Arjen Poutsma
- * @since 3.0
* @see URI Templates
* @since 3.0
*/
public class UriTemplate {
- /**
- * Captures URI template variable names.
- */
+ /** Captures URI template variable names. */
private static final Pattern NAMES_PATTERN = Pattern.compile("\\{([^/]+?)\\}");
- /**
- * Replaces template variables in the URI template.
- */
+ /** Replaces template variables in the URI template. */
private static final String VALUE_REGEX = "(.*)";
-
private final List Example:
+ * Given the Map of variables, expands this template into a URI. The Map keys represent variable names, the Map values
+ * variable values. The order of variables is not significant. Example:
* Example:
- * Example: Example:
- * Example:
* UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}");
* Map<String, String> uriVariables = new HashMap<String, String>();
@@ -91,10 +83,11 @@ public class UriTemplate {
* System.out.println(template.expand(uriVariables));
*
* will print:
+ *
* @param uriVariables the map of URI variables
* @return the expanded URI
- * @throws IllegalArgumentException if http://example.com/hotels/1/bookings/42uriVariables is null;
- * or if it does not contain values for all the variable names
+ * @throws IllegalArgumentException if uriVariables is null; or if it does not contain values
+ * for all the variable names
*/
public URI expand(Map
- * will print:
+ * Given an array of variables, expand this template into a full URI. The array represent variable values. The order of
+ * variables is significant. http://example.com/hotels/1/bookings/42 will print:
+ *
* @param uriVariableValues the array of URI variables
* @return the expanded URI
- * @throws IllegalArgumentException if http://example.com/hotels/1/bookings/42uriVariables is null;
- * or if it does not contain sufficient variables
+ * @throws IllegalArgumentException if uriVariables is null; or if it does not contain
+ * sufficient variables
*/
public URI expand(String... uriVariableValues) {
Assert.notNull(uriVariableValues, "'uriVariableValues' must not be null");
if (uriVariableValues.length != this.variableNames.size()) {
- throw new IllegalArgumentException("Invalid amount of variables values in [" +
- this.uriTemplate + "]: expected " + this.variableNames.size() +
- "; got " + uriVariableValues.length);
+ throw new IllegalArgumentException(
+ "Invalid amount of variables values in [" + this.uriTemplate + "]: expected " +
+ this.variableNames.size() + "; got " + uriVariableValues.length);
}
Matcher matcher = NAMES_PATTERN.matcher(this.uriTemplate);
StringBuffer buffer = new StringBuffer();
@@ -143,6 +133,7 @@ public class UriTemplate {
/**
* Indicate whether the given URI matches this template.
+ *
* @param uri the URI to match to
* @return true if it matches; false otherwise
*/
@@ -155,14 +146,11 @@ public class UriTemplate {
}
/**
- * Match the given URI to a map of variable values. Keys in the returned map are
- * variable names, values are variable values, as occurred in the given URI.
- *
- * UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}");
- * System.out.println(template.match("http://example.com/hotels/1/bookings/42"));
- *
- * will print:
+ * Match the given URI to a map of variable values. Keys in the returned map are variable names, values are variable
+ * values, as occurred in the given URI. {hotel=1, booking=42} UriTemplate template = new
+ * UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}"); System.out.println(template.match("http://example.com/hotels/1/bookings/42"));
+ * will print:
+ *
* @param uri the URI to match to
* @return a map of variable values
*/
@@ -185,30 +173,57 @@ public class UriTemplate {
return this.uriTemplate;
}
-
private static URI encodeUri(String uri) {
try {
- int idx = uri.indexOf(':');
- URI result;
- if (idx != -1) {
- String scheme = uri.substring(0, idx);
- String ssp = uri.substring(idx + 1);
- result = new URI(scheme, ssp, null);
+ int schemeIdx = uri.indexOf(':');
+ if (schemeIdx == -1) {
+ int queryIdx = uri.indexOf('?');
+ if (queryIdx == -1) {
+ int fragmentIdx = uri.indexOf('#');
+ if (fragmentIdx == -1) {
+ return new URI(null, null, uri, null);
+ }
+ else {
+ String path = uri.substring(0, fragmentIdx);
+ String fragment = uri.substring(fragmentIdx + 1);
+ return new URI(null, null, path, fragment);
+ }
+ }
+ else {
+ int fragmentIdx = uri.indexOf('#', queryIdx + 1);
+ if (fragmentIdx == -1) {
+ String path = uri.substring(0, queryIdx);
+ String query = uri.substring(queryIdx + 1);
+ return new URI(null, null, path, query, null);
+ }
+ else {
+ String path = uri.substring(0, queryIdx);
+ String query = uri.substring(queryIdx + 1, fragmentIdx);
+ String fragment = uri.substring(fragmentIdx + 1);
+ return new URI(null, null, path, query, fragment);
+ }
+ }
}
else {
- result = new URI(null, null, uri, null);
+ int fragmentIdx = uri.indexOf('#', schemeIdx + 1);
+ String scheme = uri.substring(0, schemeIdx);
+ if (fragmentIdx == -1) {
+ String ssp = uri.substring(schemeIdx + 1);
+ return new URI(scheme, ssp, null);
+ }
+ else {
+ String ssp = uri.substring(schemeIdx + 1, fragmentIdx);
+ String fragment = uri.substring(fragmentIdx + 1);
+ return new URI(scheme, ssp, fragment);
+ }
}
- return result;
}
catch (URISyntaxException ex) {
throw new IllegalArgumentException("Could not create URI from [" + uri + "]: " + ex);
}
}
-
- /**
- * Static inner class to parse uri template strings into a matching regular expression.
- */
+ /** Static inner class to parse uri template strings into a matching regular expression. */
private static class Parser {
private final List{hotel=1, booking=42}