Browse Source

Polish DefaultUriTemplateHandler

pull/1043/head
Rossen Stoyanchev 10 years ago
parent
commit
0d007a328b
  1. 18
      spring-web/src/main/java/org/springframework/web/util/DefaultUriTemplateHandler.java
  2. 48
      spring-web/src/test/java/org/springframework/web/util/DefaultUriTemplateHandlerTests.java

18
spring-web/src/main/java/org/springframework/web/util/DefaultUriTemplateHandler.java

@ -26,9 +26,14 @@ import java.util.Map;
import org.springframework.util.Assert; 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. * {@link UriComponentsBuilder} to expand and encode variables.
* *
* <p>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 * @author Rossen Stoyanchev
* @since 4.2 * @since 4.2
*/ */
@ -36,6 +41,7 @@ public class DefaultUriTemplateHandler implements UriTemplateHandler {
private String baseUrl; private String baseUrl;
private boolean parsePath; private boolean parsePath;
private boolean strictEncoding; private boolean strictEncoding;
@ -152,7 +158,7 @@ public class DefaultUriTemplateHandler implements UriTemplateHandler {
else { else {
Map<String, Object> encodedUriVars = new HashMap<String, Object>(uriVariables.size()); Map<String, Object> encodedUriVars = new HashMap<String, Object>(uriVariables.size());
for (Map.Entry<String, ?> entry : uriVariables.entrySet()) { for (Map.Entry<String, ?> entry : uriVariables.entrySet()) {
encodedUriVars.put(entry.getKey(), encodeValue(entry.getValue())); encodedUriVars.put(entry.getKey(), applyStrictEncoding(entry.getValue()));
} }
return builder.build().expand(encodedUriVars); return builder.build().expand(encodedUriVars);
} }
@ -165,13 +171,13 @@ public class DefaultUriTemplateHandler implements UriTemplateHandler {
else { else {
Object[] encodedUriVars = new Object[uriVariables.length]; Object[] encodedUriVars = new Object[uriVariables.length];
for (int i = 0; i < uriVariables.length; i++) { for (int i = 0; i < uriVariables.length; i++) {
encodedUriVars[i] = encodeValue(uriVariables[i]); encodedUriVars[i] = applyStrictEncoding(uriVariables[i]);
} }
return builder.build().expand(encodedUriVars); return builder.build().expand(encodedUriVars);
} }
} }
private String encodeValue(Object value) { private String applyStrictEncoding(Object value) {
String stringValue = (value != null ? value.toString() : ""); String stringValue = (value != null ? value.toString() : "");
try { try {
return UriUtils.encode(stringValue, "UTF-8"); 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 * Invoked after the URI template has been expanded and encoded to prefix it
* the configured {@link #setBaseUrl(String) baseUrl} if any. * with the configured {@link #setBaseUrl(String) baseUrl}, if any.
* @param uriComponents the expanded and encoded URI * @param uriComponents the expanded and encoded URI
* @return the final URI * @return the final URI
*/ */

48
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}. * Unit tests for {@link DefaultUriTemplateHandler}.
*
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
*/ */
public class DefaultUriTemplateHandlerTests { public class DefaultUriTemplateHandlerTests {
@ -33,35 +34,35 @@ public class DefaultUriTemplateHandlerTests {
@Test @Test
public void baseUrl() throws Exception { public void baseUrlWithoutPath() throws Exception {
this.handler.setBaseUrl("http://localhost:8080"); this.handler.setBaseUrl("http://localhost:8080");
URI actual = this.handler.expand("/myapiresource"); 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 @Test
public void baseUrlWithPartialPath() throws Exception { public void baseUrlWithPath() throws Exception {
this.handler.setBaseUrl("http://localhost:8080/context"); this.handler.setBaseUrl("http://localhost:8080/context");
URI actual = this.handler.expand("/myapiresource"); 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 @Test
public void parsePathOff() throws Exception { public void parsePathIsOff() throws Exception {
this.handler.setParsePath(false); this.handler.setParsePath(false);
Map<String, String> vars = new HashMap<>(2); Map<String, String> vars = new HashMap<>(2);
vars.put("hotel", "1"); vars.put("hotel", "1");
vars.put("publicpath", "pics/logo.png"); vars.put("publicpath", "pics/logo.png");
String template = "http://example.com/hotels/{hotel}/pic/{publicpath}"; String template = "http://example.com/hotels/{hotel}/pic/{publicpath}";
URI actual = this.handler.expand(template, vars); 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 @Test
public void parsePathOn() throws Exception { public void parsePathIsOn() throws Exception {
this.handler.setParsePath(true); this.handler.setParsePath(true);
Map<String, String> vars = new HashMap<>(2); Map<String, String> vars = new HashMap<>(2);
vars.put("hotel", "1"); vars.put("hotel", "1");
@ -69,19 +70,28 @@ public class DefaultUriTemplateHandlerTests {
vars.put("scale", "150x150"); vars.put("scale", "150x150");
String template = "http://example.com/hotels/{hotel}/pic/{publicpath}/size/{scale}"; String template = "http://example.com/hotels/{hotel}/pic/{publicpath}/size/{scale}";
URI actual = this.handler.expand(template, vars); 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 @Test
public void strictEncodingOff() throws Exception { public void strictEncodingIsOffWithMap() throws Exception {
this.handler.setStrictEncoding(false); this.handler.setStrictEncoding(false);
Map<String, String> vars = new HashMap<>(2); Map<String, String> vars = new HashMap<>(2);
vars.put("userId", "john;doe"); vars.put("userId", "john;doe");
String template = "http://www.example.com/user/{userId}/dashboard"; String template = "http://www.example.com/user/{userId}/dashboard";
URI actual = this.handler.expand(template, vars); 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 @Test
@ -91,8 +101,8 @@ public class DefaultUriTemplateHandlerTests {
vars.put("userId", "john;doe"); vars.put("userId", "john;doe");
String template = "http://www.example.com/user/{userId}/dashboard"; String template = "http://www.example.com/user/{userId}/dashboard";
URI actual = this.handler.expand(template, vars); 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 @Test
@ -100,8 +110,8 @@ public class DefaultUriTemplateHandlerTests {
this.handler.setStrictEncoding(true); this.handler.setStrictEncoding(true);
String template = "http://www.example.com/user/{userId}/dashboard"; String template = "http://www.example.com/user/{userId}/dashboard";
URI actual = this.handler.expand(template, "john;doe"); 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());
} }
} }

Loading…
Cancel
Save