Skip to content

Commit eed5de6

Browse files
jungmreta
andauthored
[CXF-9209] UriInfoImpl#getMatchedResourceTemplate support variable Paths (#2983)
* [CXF-9209] UriInfoImpl#getMatchedResourceTemplate support variable Paths * Read ApplicationPath instead of baseUri substring * read effective applicationPath from new property in Exchange * Use builder.toTemplate() for UriInfo#getMatchedResourceTemplate() implementantion Signed-off-by: Andriy Redko <drreta@gmail.com> --------- Signed-off-by: Andriy Redko <drreta@gmail.com> Co-authored-by: Andriy Redko <drreta@gmail.com>
1 parent fafb3e1 commit eed5de6

File tree

4 files changed

+80
-2
lines changed

4 files changed

+80
-2
lines changed

rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriInfoImpl.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,10 @@ public String getMatchedResourceTemplate() {
249249
final List<URITemplate> templates = new LinkedList<>();
250250
for (MethodInvocationInfo invocation : stack) {
251251
OperationResourceInfo ori = invocation.getMethodInfo();
252-
templates.add(ori.getClassResourceInfo().getURITemplate());
252+
final URITemplate classUriTemplate = ori.getClassResourceInfo().getURITemplate();
253+
if (classUriTemplate != null) {
254+
templates.add(classUriTemplate);
255+
}
253256
templates.add(ori.getURITemplate());
254257
}
255258

@@ -258,7 +261,7 @@ public String getMatchedResourceTemplate() {
258261
for (int i = 1; i < templates.size(); ++i) {
259262
builder = builder.path(templates.get(i).getValue());
260263
}
261-
return builder.build().toString();
264+
return builder.toTemplate();
262265
}
263266
}
264267
LOG.fine("No resource stack information, returning empty template");

rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriInfoImplTest.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,12 @@ public Response get() {
430430
return null;
431431
}
432432

433+
@GET
434+
@Path("one/{name:[a-zA-Z][a-zA-Z_0-9]*}")
435+
public Response getTemplate() {
436+
return null;
437+
}
438+
433439
@GET
434440
@Path("bar")
435441
public Response getSubMethod() {
@@ -563,6 +569,58 @@ public void testGetMatchedURIsSubResourceLocatorSubPath() throws Exception {
563569
assertEquals("foo", matchedUris.get(2));
564570
}
565571

572+
@Test
573+
public void testGetMatchedResourceTemplateIncludesApplicationPathAndTemplateVariables() throws Exception {
574+
Message m = mockMessage("http://localhost:8080/app", "/foo/one/abc");
575+
OperationResourceInfoStack oriStack = new OperationResourceInfoStack();
576+
ClassResourceInfo cri = getCri(RootResource.class, true);
577+
OperationResourceInfo ori = getOri(cri, "getTemplate");
578+
579+
MethodInvocationInfo miInfo = new MethodInvocationInfo(ori, RootResource.class, new ArrayList<String>());
580+
oriStack.push(miInfo);
581+
m.put(OperationResourceInfoStack.class, oriStack);
582+
583+
UriInfoImpl u = new UriInfoImpl(m);
584+
assertEquals("/foo/one/{name:[a-zA-Z][a-zA-Z_0-9]*}", u.getMatchedResourceTemplate());
585+
}
586+
587+
@Test
588+
public void testGetMatchedResourceTemplateIgnoresPathBeforeApplicationPath() throws Exception {
589+
Message m = mockMessage("http://localhost:8080/context/service", "/foo/bar");
590+
591+
OperationResourceInfoStack oriStack = new OperationResourceInfoStack();
592+
ClassResourceInfo cri = getCri(RootResource.class, true);
593+
OperationResourceInfo ori = getOri(cri, "getSubMethod");
594+
595+
MethodInvocationInfo miInfo = new MethodInvocationInfo(ori, RootResource.class, new ArrayList<String>());
596+
oriStack.push(miInfo);
597+
m.put(OperationResourceInfoStack.class, oriStack);
598+
599+
UriInfoImpl u = new UriInfoImpl(m);
600+
assertEquals("/foo/bar", u.getMatchedResourceTemplate());
601+
}
602+
603+
@Test
604+
public void testGetMatchedResourceTemplateSubResourceWithoutClassPath() throws Exception {
605+
Message m = mockMessage("http://localhost:8080/app", "/foo/sub");
606+
OperationResourceInfoStack oriStack = new OperationResourceInfoStack();
607+
ClassResourceInfo rootCri = getCri(RootResource.class, true);
608+
OperationResourceInfo rootOri = getOri(rootCri, "getSubResourceLocator");
609+
610+
MethodInvocationInfo miInfo = new MethodInvocationInfo(rootOri, RootResource.class, new ArrayList<String>());
611+
oriStack.push(miInfo);
612+
613+
ClassResourceInfo subCri = getCri(SubResource.class, false);
614+
OperationResourceInfo subOri = getOri(subCri, "getFromSub");
615+
616+
miInfo = new MethodInvocationInfo(subOri, SubResource.class, new ArrayList<String>());
617+
oriStack.push(miInfo);
618+
m.put(OperationResourceInfoStack.class, oriStack);
619+
620+
UriInfoImpl u = new UriInfoImpl(m);
621+
assertEquals("/foo/sub/", u.getMatchedResourceTemplate());
622+
}
623+
566624
private Message mockMessage(String baseAddress, String pathInfo) {
567625
return mockMessage(baseAddress, pathInfo, null, null);
568626
}

systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStorePerRequest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import jakarta.ws.rs.core.Context;
3232
import jakarta.ws.rs.core.HttpHeaders;
3333
import jakarta.ws.rs.core.Response;
34+
import jakarta.ws.rs.core.UriInfo;
3435

3536
@Path("/bookstore2")
3637
public class BookStorePerRequest {
@@ -86,6 +87,12 @@ public void setHttpHeaders(HttpHeaders headers) {
8687
public Book getBookByHeader2() throws Exception {
8788
return getBookByHeader();
8889
}
90+
91+
@GET
92+
@Path("/book%20template/{name:[a-zA-Z][a-zA-Z_0-9]*}")
93+
public String getBookTemplate(@Context final UriInfo info) throws Exception {
94+
return info.getMatchedResourceTemplate();
95+
}
8996

9097
@GET
9198
@Path("/bookheaders/")

systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerNonSpringBookTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
import org.junit.BeforeClass;
4545
import org.junit.Test;
4646

47+
import static org.hamcrest.MatcherAssert.assertThat;
48+
import static org.hamcrest.Matchers.equalTo;
4749
import static org.junit.Assert.assertEquals;
4850
import static org.junit.Assert.assertNotSame;
4951
import static org.junit.Assert.assertTrue;
@@ -202,6 +204,14 @@ public void testGetBook123TwoApplications() throws Exception {
202204
doTestPerRequest("http://localhost:" + PORT + "/application6/thebooks/bookstore2/bookheaders");
203205
doTestPerRequest("http://localhost:" + PORT + "/application6/the%20books2/bookstore2/book%20headers");
204206
}
207+
208+
@Test
209+
public void testGetMatchedResourceTemplate() throws Exception {
210+
WebClient wc = WebClient.create("http://localhost:" + PORT
211+
+ "/application6/the%20books2/bookstore2/book%20template/abc");
212+
assertThat(wc.accept("*/*").get(String.class),
213+
equalTo("/bookstore2/book%20template/{name:[a-zA-Z][a-zA-Z_0-9]*}"));
214+
}
205215

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

0 commit comments

Comments
 (0)