Support {/var} syntax in UriComponentsBuilder

Issue: SPR-12750
This commit is contained in:
Rossen Stoyanchev
2015-03-20 15:33:56 -04:00
parent 6a96c26dd4
commit a57d42829c
3 changed files with 52 additions and 12 deletions

View File

@@ -716,18 +716,35 @@ public class UriComponentsBuilder implements Cloneable {
}
public void addPath(String path) {
if (StringUtils.hasText(path)) {
PathSegmentComponentBuilder psBuilder = getLastBuilder(PathSegmentComponentBuilder.class);
FullPathComponentBuilder fpBuilder = getLastBuilder(FullPathComponentBuilder.class);
if (psBuilder != null) {
path = path.startsWith("/") ? path : "/" + path;
}
if (fpBuilder == null) {
fpBuilder = new FullPathComponentBuilder();
this.builders.add(fpBuilder);
}
fpBuilder.append(path);
if (!StringUtils.hasText(path)) {
return;
}
int startIndex = path.indexOf("{/");
while (startIndex != -1) {
String pathToAdd = path.substring(0, startIndex);
addPathInternal(pathToAdd);
int endIndex = path.indexOf("}", startIndex);
String pathSegmentToAdd = "{" + path.substring(startIndex + 2, endIndex) + "}";
addPathSegments(pathSegmentToAdd);
path = (endIndex >= path.length()) ? "" : path.substring(endIndex + 1);
startIndex = path.indexOf("{/");
}
addPathInternal(path);
}
private void addPathInternal(String path) {
PathSegmentComponentBuilder psBuilder = getLastBuilder(PathSegmentComponentBuilder.class);
FullPathComponentBuilder fpBuilder = getLastBuilder(FullPathComponentBuilder.class);
if (psBuilder != null) {
path = path.startsWith("/") ? path : "/" + path;
}
if (fpBuilder == null) {
fpBuilder = new FullPathComponentBuilder();
this.builders.add(fpBuilder);
}
fpBuilder.append(path);
}
@SuppressWarnings("unchecked")