diff --git a/spring-web/src/main/java/org/springframework/web/util/DefaultUriTemplateHandler.java b/spring-web/src/main/java/org/springframework/web/util/DefaultUriTemplateHandler.java
index 3eb27c3009a..3a1555d448c 100644
--- a/spring-web/src/main/java/org/springframework/web/util/DefaultUriTemplateHandler.java
+++ b/spring-web/src/main/java/org/springframework/web/util/DefaultUriTemplateHandler.java
@@ -26,9 +26,14 @@ import java.util.Map;
import org.springframework.util.Assert;
/**
- * Default implementation of {@link UriTemplateHandler} that uses
+ * Default implementation of {@link UriTemplateHandler} based on the use of
* {@link UriComponentsBuilder} to expand and encode variables.
*
+ *
There are also several properties to customize how URI template handling
+ * that include a {@link #setBaseUrl baseUrl} to be used as a prefix for all URI
+ * templates and a couple of encoding related options -- {@link #setParsePath
+ * parsePath} and {@link #setStrictEncoding strictEncoding} respectively.
+ *
* @author Rossen Stoyanchev
* @since 4.2
*/
@@ -36,6 +41,7 @@ public class DefaultUriTemplateHandler implements UriTemplateHandler {
private String baseUrl;
+
private boolean parsePath;
private boolean strictEncoding;
@@ -152,7 +158,7 @@ public class DefaultUriTemplateHandler implements UriTemplateHandler {
else {
Map encodedUriVars = new HashMap(uriVariables.size());
for (Map.Entry entry : uriVariables.entrySet()) {
- encodedUriVars.put(entry.getKey(), encodeValue(entry.getValue()));
+ encodedUriVars.put(entry.getKey(), applyStrictEncoding(entry.getValue()));
}
return builder.build().expand(encodedUriVars);
}
@@ -165,13 +171,13 @@ public class DefaultUriTemplateHandler implements UriTemplateHandler {
else {
Object[] encodedUriVars = new Object[uriVariables.length];
for (int i = 0; i < uriVariables.length; i++) {
- encodedUriVars[i] = encodeValue(uriVariables[i]);
+ encodedUriVars[i] = applyStrictEncoding(uriVariables[i]);
}
return builder.build().expand(encodedUriVars);
}
}
- private String encodeValue(Object value) {
+ private String applyStrictEncoding(Object value) {
String stringValue = (value != null ? value.toString() : "");
try {
return UriUtils.encode(stringValue, "UTF-8");
@@ -183,8 +189,8 @@ public class DefaultUriTemplateHandler implements UriTemplateHandler {
}
/**
- * Invoked after the URI template has been expanded and encoded to prepend
- * the configured {@link #setBaseUrl(String) baseUrl} if any.
+ * Invoked after the URI template has been expanded and encoded to prefix it
+ * with the configured {@link #setBaseUrl(String) baseUrl}, if any.
* @param uriComponents the expanded and encoded URI
* @return the final URI
*/
diff --git a/spring-web/src/test/java/org/springframework/web/util/DefaultUriTemplateHandlerTests.java b/spring-web/src/test/java/org/springframework/web/util/DefaultUriTemplateHandlerTests.java
index 691d6da285f..f650bcd2959 100644
--- a/spring-web/src/test/java/org/springframework/web/util/DefaultUriTemplateHandlerTests.java
+++ b/spring-web/src/test/java/org/springframework/web/util/DefaultUriTemplateHandlerTests.java
@@ -25,6 +25,7 @@ import static org.junit.Assert.assertEquals;
/**
* Unit tests for {@link DefaultUriTemplateHandler}.
+ *
* @author Rossen Stoyanchev
*/
public class DefaultUriTemplateHandlerTests {
@@ -33,35 +34,35 @@ public class DefaultUriTemplateHandlerTests {
@Test
- public void baseUrl() throws Exception {
+ public void baseUrlWithoutPath() throws Exception {
this.handler.setBaseUrl("http://localhost:8080");
URI actual = this.handler.expand("/myapiresource");
- URI expected = new URI("http://localhost:8080/myapiresource");
- assertEquals(expected, actual);
+
+ assertEquals("http://localhost:8080/myapiresource", actual.toString());
}
@Test
- public void baseUrlWithPartialPath() throws Exception {
+ public void baseUrlWithPath() throws Exception {
this.handler.setBaseUrl("http://localhost:8080/context");
URI actual = this.handler.expand("/myapiresource");
- URI expected = new URI("http://localhost:8080/context/myapiresource");
- assertEquals(expected, actual);
+
+ assertEquals("http://localhost:8080/context/myapiresource", actual.toString());
}
@Test
- public void parsePathOff() throws Exception {
+ public void parsePathIsOff() throws Exception {
this.handler.setParsePath(false);
Map vars = new HashMap<>(2);
vars.put("hotel", "1");
vars.put("publicpath", "pics/logo.png");
String template = "http://example.com/hotels/{hotel}/pic/{publicpath}";
URI actual = this.handler.expand(template, vars);
- URI expected = new URI("http://example.com/hotels/1/pic/pics/logo.png");
- assertEquals(expected, actual);
+
+ assertEquals("http://example.com/hotels/1/pic/pics/logo.png", actual.toString());
}
@Test
- public void parsePathOn() throws Exception {
+ public void parsePathIsOn() throws Exception {
this.handler.setParsePath(true);
Map vars = new HashMap<>(2);
vars.put("hotel", "1");
@@ -69,19 +70,28 @@ public class DefaultUriTemplateHandlerTests {
vars.put("scale", "150x150");
String template = "http://example.com/hotels/{hotel}/pic/{publicpath}/size/{scale}";
URI actual = this.handler.expand(template, vars);
- URI expected = new URI("http://example.com/hotels/1/pic/pics%2Flogo.png/size/150x150");
- assertEquals(expected, actual);
+
+ assertEquals("http://example.com/hotels/1/pic/pics%2Flogo.png/size/150x150", actual.toString());
}
@Test
- public void strictEncodingOff() throws Exception {
+ public void strictEncodingIsOffWithMap() throws Exception {
this.handler.setStrictEncoding(false);
Map vars = new HashMap<>(2);
vars.put("userId", "john;doe");
String template = "http://www.example.com/user/{userId}/dashboard";
URI actual = this.handler.expand(template, vars);
- URI expected = new URI("http://www.example.com/user/john;doe/dashboard");
- assertEquals(expected, actual);
+
+ assertEquals("http://www.example.com/user/john;doe/dashboard", actual.toString());
+ }
+
+ @Test
+ public void strictEncodingOffWithArray() throws Exception {
+ this.handler.setStrictEncoding(false);
+ String template = "http://www.example.com/user/{userId}/dashboard";
+ URI actual = this.handler.expand(template, "john;doe");
+
+ assertEquals("http://www.example.com/user/john;doe/dashboard", actual.toString());
}
@Test
@@ -91,8 +101,8 @@ public class DefaultUriTemplateHandlerTests {
vars.put("userId", "john;doe");
String template = "http://www.example.com/user/{userId}/dashboard";
URI actual = this.handler.expand(template, vars);
- URI expected = new URI("http://www.example.com/user/john%3Bdoe/dashboard");
- assertEquals(expected, actual);
+
+ assertEquals("http://www.example.com/user/john%3Bdoe/dashboard", actual.toString());
}
@Test
@@ -100,8 +110,8 @@ public class DefaultUriTemplateHandlerTests {
this.handler.setStrictEncoding(true);
String template = "http://www.example.com/user/{userId}/dashboard";
URI actual = this.handler.expand(template, "john;doe");
- URI expected = new URI("http://www.example.com/user/john%3Bdoe/dashboard");
- assertEquals(expected, actual);
+
+ assertEquals("http://www.example.com/user/john%3Bdoe/dashboard", actual.toString());
}
}