Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ private static final class Variable extends UriChunk {
private static final Pattern VARIABLE_PATTERN = Pattern.compile("(\\w[-\\w\\.]*[ ]*)(\\:(.+))?");
private String name;
private Pattern pattern;
private String template;

private Variable() {
// empty constructor
Expand All @@ -488,14 +489,16 @@ public static Variable create(String uriChunk) {
return null;
}
if (CurlyBraceTokenizer.insideBraces(uriChunk)) {
uriChunk = CurlyBraceTokenizer.stripBraces(uriChunk).trim();
Matcher matcher = VARIABLE_PATTERN.matcher(uriChunk);
String trimmedUriChunk = CurlyBraceTokenizer.stripBraces(uriChunk).trim();
Matcher matcher = VARIABLE_PATTERN.matcher(trimmedUriChunk);
if (matcher.matches()) {
newVariable.name = matcher.group(1).trim();
if (matcher.group(2) != null && matcher.group(3) != null) {
String patternExpression = matcher.group(3).trim();
newVariable.pattern = Pattern.compile(patternExpression);
}
// Store the exact variable template
newVariable.template = uriChunk.trim();
return newVariable;
}
}
Expand Down Expand Up @@ -526,10 +529,7 @@ public boolean matches(String value) {

@Override
public String getValue() {
if (pattern != null) {
return "{" + name + ":" + pattern + "}";
}
return "{" + name + "}";
return template;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,12 @@ public Response getTemplate() {
return null;
}

@GET
@Path("one/{name: [a-zA-Z][a-zA-Z_0-9]*}")
public Response getTemplateSpaces() {
return null;
}

@GET
@Path("bar")
public Response getSubMethod() {
Expand Down Expand Up @@ -584,6 +590,21 @@ public void testGetMatchedResourceTemplateIncludesApplicationPathAndTemplateVari
assertEquals("/foo/one/{name:[a-zA-Z][a-zA-Z_0-9]*}", u.getMatchedResourceTemplate());
}

@Test
public void testGetMatchedResourceTemplatePreserveSpacesInTemplateVariables() throws Exception {
Message m = mockMessage("http://localhost:8080/app", "/foo/one/abc");
OperationResourceInfoStack oriStack = new OperationResourceInfoStack();
ClassResourceInfo cri = getCri(RootResource.class, true);
OperationResourceInfo ori = getOri(cri, "getTemplateSpaces");

MethodInvocationInfo miInfo = new MethodInvocationInfo(ori, RootResource.class, new ArrayList<String>());
oriStack.push(miInfo);
m.put(OperationResourceInfoStack.class, oriStack);

UriInfoImpl u = new UriInfoImpl(m);
assertEquals("/foo/one/{name: [a-zA-Z][a-zA-Z_0-9]*}", u.getMatchedResourceTemplate());
}

@Test
public void testGetMatchedResourceTemplateIgnoresPathBeforeApplicationPath() throws Exception {
Message m = mockMessage("http://localhost:8080/context/service", "/foo/bar");
Expand Down