#337 - Fixed potential double-encoding in Traverson.
We now avoid calling URI.toString() to early to prevent the need to re-parse it and thus accidentally cause a double-encoding. Original pull request: #382.
This commit is contained in:
committed by
Oliver Gierke
parent
a019edae82
commit
dd4ec341b5
@@ -59,6 +59,7 @@ import com.jayway.jsonpath.JsonPath;
|
||||
* @author Oliver Gierke
|
||||
* @author Dietrich Schulten
|
||||
* @author Greg Turnquist
|
||||
* @author Tom Bunting
|
||||
* @since 0.11
|
||||
*/
|
||||
public class Traverson {
|
||||
@@ -305,7 +306,7 @@ public class Traverson {
|
||||
public <T> T toObject(Class<T> type) {
|
||||
|
||||
Assert.notNull(type, "Target type must not be null!");
|
||||
return operations.exchange(traverseToFinalUrl(true), GET, prepareRequest(headers), type).getBody();
|
||||
return operations.exchange(traverseToExpandedFinalUrl(), GET, prepareRequest(headers), type).getBody();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -318,7 +319,7 @@ public class Traverson {
|
||||
public <T> T toObject(ParameterizedTypeReference<T> type) {
|
||||
|
||||
Assert.notNull(type, "Target type must not be null!");
|
||||
return operations.exchange(traverseToFinalUrl(true), GET, prepareRequest(headers), type).getBody();
|
||||
return operations.exchange(traverseToExpandedFinalUrl(), GET, prepareRequest(headers), type).getBody();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -332,7 +333,7 @@ public class Traverson {
|
||||
|
||||
Assert.hasText(jsonPath, "JSON path must not be null or empty!");
|
||||
|
||||
String forObject = operations.exchange(traverseToFinalUrl(true), GET, prepareRequest(headers), String.class)
|
||||
String forObject = operations.exchange(traverseToExpandedFinalUrl(), GET, prepareRequest(headers), String.class)
|
||||
.getBody();
|
||||
return JsonPath.read(forObject, jsonPath);
|
||||
}
|
||||
@@ -346,7 +347,7 @@ public class Traverson {
|
||||
public <T> ResponseEntity<T> toEntity(Class<T> type) {
|
||||
|
||||
Assert.notNull(type, "Target type must not be null!");
|
||||
return operations.exchange(traverseToFinalUrl(true), GET, prepareRequest(headers), type);
|
||||
return operations.exchange(traverseToExpandedFinalUrl(), GET, prepareRequest(headers), type);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -374,14 +375,20 @@ public class Traverson {
|
||||
private Link traverseToLink(boolean expandFinalUrl) {
|
||||
|
||||
Assert.isTrue(rels.size() > 0, "At least one rel needs to be provided!");
|
||||
return new Link(traverseToFinalUrl(expandFinalUrl), rels.get(rels.size() - 1).getRel());
|
||||
return new Link(expandFinalUrl ? traverseToExpandedFinalUrl().toString() : traverseToFinalUrl(),
|
||||
rels.get(rels.size() - 1).getRel());
|
||||
}
|
||||
|
||||
private String traverseToFinalUrl(boolean expandFinalUrl) {
|
||||
private String traverseToFinalUrl() {
|
||||
|
||||
String uri = getAndFindLinkWithRel(baseUri.toString(), rels.iterator());
|
||||
UriTemplate uriTemplate = new UriTemplate(uri);
|
||||
return expandFinalUrl ? uriTemplate.expand(templateParameters).toString() : uriTemplate.toString();
|
||||
return new UriTemplate(uri).toString();
|
||||
}
|
||||
|
||||
private URI traverseToExpandedFinalUrl() {
|
||||
|
||||
String uri = getAndFindLinkWithRel(baseUri.toString(), rels.iterator());
|
||||
return new UriTemplate(uri).expand(templateParameters);
|
||||
}
|
||||
|
||||
private String getAndFindLinkWithRel(String uri, Iterator<Hop> rels) {
|
||||
|
||||
@@ -147,6 +147,16 @@ public class Server implements Closeable {
|
||||
respond(). //
|
||||
withBody(springagramItemWithoutImageHalDocument). //
|
||||
withContentType(MediaTypes.HAL_JSON.toString());
|
||||
|
||||
// For Traverson URI double encoding test
|
||||
|
||||
onRequest(). //
|
||||
havingPathEqualTo("/springagram/items"). //
|
||||
havingQueryString(equalTo("projection=no%20images")). //
|
||||
respond(). //
|
||||
withBody(springagramItemsHalDocument). //
|
||||
withContentType(MediaTypes.HAL_JSON.toString());
|
||||
|
||||
}
|
||||
|
||||
public String rootResource() {
|
||||
|
||||
@@ -355,6 +355,23 @@ public class TraversonTest {
|
||||
assertThat(item.image, equalTo(server.rootResource() + "/springagram/file/cat"));
|
||||
assertThat(item.description, equalTo("cat"));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see #337
|
||||
*/
|
||||
@Test
|
||||
public void doesNotDoubleEncodeURI() {
|
||||
|
||||
this.traverson = new Traverson(URI.create(server.rootResource() + "/springagram"), MediaTypes.HAL_JSON);
|
||||
|
||||
Resource<?> itemResource = traverson.//
|
||||
follow(rel("items").withParameters(Collections.singletonMap("projection", "no images"))).//
|
||||
toObject(Resource.class);
|
||||
|
||||
assertThat(itemResource.hasLink("self"), is(true));
|
||||
assertThat(itemResource.getLink("self").expand().getHref(),
|
||||
equalTo(server.rootResource() + "/springagram/items"));
|
||||
}
|
||||
|
||||
private void setUpActors() {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user