Skip to content
Open
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 @@ -249,7 +249,10 @@ public String getMatchedResourceTemplate() {
final List<URITemplate> templates = new LinkedList<>();
for (MethodInvocationInfo invocation : stack) {
OperationResourceInfo ori = invocation.getMethodInfo();
templates.add(ori.getClassResourceInfo().getURITemplate());
final URITemplate classUriTemplate = ori.getClassResourceInfo().getURITemplate();
if (classUriTemplate != null) {
templates.add(classUriTemplate);
}
templates.add(ori.getURITemplate());
}

Expand All @@ -258,7 +261,7 @@ public String getMatchedResourceTemplate() {
for (int i = 1; i < templates.size(); ++i) {
builder = builder.path(templates.get(i).getValue());
}
return builder.build().toString();
return builder.toTemplate();
}
}
LOG.fine("No resource stack information, returning empty template");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,12 @@ public Response get() {
return null;
}

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

@GET
@Path("bar")
public Response getSubMethod() {
Expand Down Expand Up @@ -563,6 +569,58 @@ public void testGetMatchedURIsSubResourceLocatorSubPath() throws Exception {
assertEquals("foo", matchedUris.get(2));
}

@Test
public void testGetMatchedResourceTemplateIncludesApplicationPathAndTemplateVariables() 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, "getTemplate");

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");

OperationResourceInfoStack oriStack = new OperationResourceInfoStack();
ClassResourceInfo cri = getCri(RootResource.class, true);
OperationResourceInfo ori = getOri(cri, "getSubMethod");

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/bar", u.getMatchedResourceTemplate());
}

@Test
public void testGetMatchedResourceTemplateSubResourceWithoutClassPath() throws Exception {
Message m = mockMessage("http://localhost:8080/app", "/foo/sub");
OperationResourceInfoStack oriStack = new OperationResourceInfoStack();
ClassResourceInfo rootCri = getCri(RootResource.class, true);
OperationResourceInfo rootOri = getOri(rootCri, "getSubResourceLocator");

MethodInvocationInfo miInfo = new MethodInvocationInfo(rootOri, RootResource.class, new ArrayList<String>());
oriStack.push(miInfo);

ClassResourceInfo subCri = getCri(SubResource.class, false);
OperationResourceInfo subOri = getOri(subCri, "getFromSub");

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

UriInfoImpl u = new UriInfoImpl(m);
assertEquals("/foo/sub/", u.getMatchedResourceTemplate());
}

private Message mockMessage(String baseAddress, String pathInfo) {
return mockMessage(baseAddress, pathInfo, null, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.UriInfo;

@Path("/bookstore2")
public class BookStorePerRequest {
Expand Down Expand Up @@ -86,6 +87,12 @@ public void setHttpHeaders(HttpHeaders headers) {
public Book getBookByHeader2() throws Exception {
return getBookByHeader();
}

@GET
@Path("/book%20template/{name:[a-zA-Z][a-zA-Z_0-9]*}")
public String getBookTemplate(@Context final UriInfo info) throws Exception {
return info.getMatchedResourceTemplate();
}

@GET
@Path("/bookheaders/")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
import org.junit.BeforeClass;
import org.junit.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -202,6 +204,14 @@ public void testGetBook123TwoApplications() throws Exception {
doTestPerRequest("http://localhost:" + PORT + "/application6/thebooks/bookstore2/bookheaders");
doTestPerRequest("http://localhost:" + PORT + "/application6/the%20books2/bookstore2/book%20headers");
}

@Test
public void testGetMatchedResourceTemplate() throws Exception {
WebClient wc = WebClient.create("http://localhost:" + PORT
+ "/application6/the%20books2/bookstore2/book%20template/abc");
assertThat(wc.accept("*/*").get(String.class),
equalTo("/bookstore2/book%20template/{name:[a-zA-Z][a-zA-Z_0-9]*}"));
}

@SafeVarargs
private final Response doTestPerRequest(String address, Map.Entry<String, String> ... params) throws Exception {
Expand Down