[ https://issues.apache.org/jira/browse/CXF-7527?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16248643#comment-16248643 ]
ASF GitHub Bot commented on CXF-7527: ------------------------------------- andymc12 closed pull request #337: [CXF-7527] getMatchedURIs to avoid duplicate URIs URL: https://github.com/apache/cxf/pull/337 This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriInfoImpl.java b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriInfoImpl.java index b0909271b9c..effdc68b74e 100644 --- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriInfoImpl.java +++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriInfoImpl.java @@ -176,7 +176,7 @@ public UriBuilder getRequestUriBuilder() { List<String> uris = new LinkedList<String>(); StringBuilder sumPath = new StringBuilder(""); for (MethodInvocationInfo invocation : stack) { - List<String> templateObjects = invocation.getTemplateValues(); + List<String> templateObjects = invocation.getTemplateValues(); OperationResourceInfo ori = invocation.getMethodInfo(); URITemplate[] paths = { ori.getClassResourceInfo().getURITemplate(), @@ -190,13 +190,15 @@ public UriBuilder getRequestUriBuilder() { } uris.add(0, createMatchedPath(paths[0].getValue(), rootObjects, decode)); } - for (URITemplate t : paths) { - if (t != null) { - sumPath.append("/").append(t.getValue()); + if (paths[1] != null && paths[1].getValue().length() > 1) { + for (URITemplate t : paths) { + if (t != null) { + sumPath.append("/").append(t.getValue()); + } } + objects.addAll(templateObjects); + uris.add(0, createMatchedPath(sumPath.toString(), objects, decode)); } - objects.addAll(templateObjects); - uris.add(0, createMatchedPath(sumPath.toString(), objects, decode)); } return uris; } @@ -206,7 +208,11 @@ public UriBuilder getRequestUriBuilder() { private static String createMatchedPath(String uri, List<? extends Object> vars, boolean decode) { String uriPath = UriBuilder.fromPath(uri).buildFromEncoded(vars.toArray()).getRawPath(); - return decode ? HttpUtils.pathDecode(uriPath) : uriPath; + uriPath = decode ? HttpUtils.pathDecode(uriPath) : uriPath; + if (uriPath.startsWith("/")) { + uriPath = uriPath.substring(1); + } + return uriPath; } private String doGetPath(boolean decode, boolean addSlash) { String path = HttpUtils.getPathToMatch(message, addSlash); diff --git a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriInfoImplTest.java b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriInfoImplTest.java index b2ddc1a5373..60f9df917cd 100644 --- a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriInfoImplTest.java +++ b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriInfoImplTest.java @@ -19,14 +19,24 @@ package org.apache.cxf.jaxrs.impl; +import java.lang.reflect.Method; import java.net.URI; +import java.util.ArrayList; import java.util.List; +import javax.ws.rs.GET; +import javax.ws.rs.Path; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.PathSegment; +import javax.ws.rs.core.Response; import javax.ws.rs.core.UriInfo; +import org.apache.cxf.jaxrs.model.ClassResourceInfo; +import org.apache.cxf.jaxrs.model.MethodInvocationInfo; +import org.apache.cxf.jaxrs.model.OperationResourceInfo; +import org.apache.cxf.jaxrs.model.OperationResourceInfoStack; import org.apache.cxf.jaxrs.model.URITemplate; +import org.apache.cxf.jaxrs.utils.AnnotationUtils; import org.apache.cxf.message.Exchange; import org.apache.cxf.message.ExchangeImpl; import org.apache.cxf.message.Message; @@ -408,6 +418,151 @@ public void testGetPath() { } + @Path("foo") + public static class RootResource { + + @GET + public Response get() { + return null; + } + + @GET + @Path("bar") + public Response getSubMethod() { + return null; + } + + @Path("sub") + public SubResource getSubResourceLocator() { + return new SubResource(); + } + } + + public static class SubResource { + @GET + public Response getFromSub() { + return null; + } + + @GET + @Path("subSub") + public Response getFromSubSub() { + return null; + } + } + + private static ClassResourceInfo getCri(Class<?> clazz, boolean setUriTemplate) { + ClassResourceInfo cri = new ClassResourceInfo(clazz); + Path path = AnnotationUtils.getClassAnnotation(clazz, Path.class); + if (setUriTemplate) { + cri.setURITemplate(URITemplate.createTemplate(path)); + } + return cri; + } + + private static OperationResourceInfo getOri(ClassResourceInfo cri, String methodName) throws Exception { + Method method = cri.getResourceClass().getMethod(methodName); + OperationResourceInfo ori = new OperationResourceInfo(method, cri); + ori.setURITemplate(URITemplate.createTemplate(AnnotationUtils.getMethodAnnotation(method, Path.class))); + return ori; + } + + private static List<String> getMatchedURIs(UriInfo u) { + List<String> matchedUris = u.getMatchedURIs(); +// for (String s : matchedUris) { +// System.out.println(s); +// } + return matchedUris; + } + + @Test + public void testGetMatchedURIsRoot() throws Exception { + System.out.println("testGetMatchedURIsRoot"); + Message m = mockMessage("http://localhost:8080/app", "/foo"); + OperationResourceInfoStack oriStack = new OperationResourceInfoStack(); + ClassResourceInfo cri = getCri(RootResource.class, true); + OperationResourceInfo ori = getOri(cri, "get"); + + MethodInvocationInfo miInfo = new MethodInvocationInfo(ori, RootResource.class, new ArrayList<String>()); + oriStack.push(miInfo); + m.put(OperationResourceInfoStack.class, oriStack); + + UriInfoImpl u = new UriInfoImpl(m); + List<String> matchedUris = getMatchedURIs(u); + assertEquals(1, matchedUris.size()); + assertTrue(matchedUris.contains("foo")); + } + + @Test + public void testGetMatchedURIsRootSub() throws Exception { + System.out.println("testGetMatchedURIsRootSub"); + Message m = mockMessage("http://localhost:8080/app", "/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); + List<String> matchedUris = getMatchedURIs(u); + assertEquals(2, matchedUris.size()); + assertEquals("foo/bar", matchedUris.get(0)); + assertEquals("foo", matchedUris.get(1)); + } + + @Test + public void testGetMatchedURIsSubResourceLocator() throws Exception { + System.out.println("testGetMatchedURIsSubResourceLocator"); + 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); + List<String> matchedUris = getMatchedURIs(u); + assertEquals(2, matchedUris.size()); + assertEquals("foo/sub", matchedUris.get(0)); + assertEquals("foo", matchedUris.get(1)); + } + + @Test + public void testGetMatchedURIsSubResourceLocatorSubPath() throws Exception { + System.out.println("testGetMatchedURIsSubResourceLocatorSubPath"); + Message m = mockMessage("http://localhost:8080/app", "/foo/sub/subSub"); + 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, "getFromSubSub"); + + miInfo = new MethodInvocationInfo(subOri, SubResource.class, new ArrayList<String>()); + oriStack.push(miInfo); + m.put(OperationResourceInfoStack.class, oriStack); + + UriInfoImpl u = new UriInfoImpl(m); + List<String> matchedUris = getMatchedURIs(u); + assertEquals(3, matchedUris.size()); + assertEquals("foo/sub/subSub", matchedUris.get(0)); + assertEquals("foo/sub", matchedUris.get(1)); + assertEquals("foo", matchedUris.get(2)); + } + private Message mockMessage(String baseAddress, String pathInfo) { return mockMessage(baseAddress, pathInfo, null, null); } diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java index d505e0902d6..b271ea977da 100644 --- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java +++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java @@ -2598,10 +2598,10 @@ public void testUriInfoMatchedResourcesWithObject() throws Exception { @Test public void testUriInfoMatchedUrisDecode() throws Exception { - String expected = "[/bookstore/booksubresource/123/chapters/sub/1/matched!uris, " - + "/bookstore/booksubresource/123/chapters/sub/1/, " - + "/bookstore/booksubresource/123/, " - + "/bookstore]"; + String expected = "[bookstore/booksubresource/123/chapters/sub/1/matched!uris, " + + "bookstore/booksubresource/123/chapters/sub/1/, " + + "bookstore/booksubresource/123/, " + + "bookstore]"; getAndCompare("http://localhost:" + PORT + "/bookstore/" + "booksubresource/123/chapters/sub/1/matched%21uris?decode=true", expected, "text/plain", "text/plain", 200); @@ -2610,10 +2610,10 @@ public void testUriInfoMatchedUrisDecode() throws Exception { @Test public void testUriInfoMatchedUrisNoDecode() throws Exception { //note '%21' instead of '!' - String expected = "[/bookstore/booksubresource/123/chapters/sub/1/matched%21uris, " - + "/bookstore/booksubresource/123/chapters/sub/1/, " - + "/bookstore/booksubresource/123/, " - + "/bookstore]"; + String expected = "[bookstore/booksubresource/123/chapters/sub/1/matched%21uris, " + + "bookstore/booksubresource/123/chapters/sub/1/, " + + "bookstore/booksubresource/123/, " + + "bookstore]"; getAndCompare("http://localhost:" + PORT + "/bookstore/" + "booksubresource/123/chapters/sub/1/matched%21uris?decode=false", expected, diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSUriInfoMatchTest.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSUriInfoMatchTest.java index 5b21a5d456b..92c24004400 100644 --- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSUriInfoMatchTest.java +++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSUriInfoMatchTest.java @@ -60,7 +60,7 @@ public void testMatchedUris() throws Exception { WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(100000000L); wc.accept("text/plain"); String data = wc.get(String.class); - assertEquals("/my/resource/1/matched/uris,/my/resource/1", data); + assertEquals("my/resource/1/matched/uris,my/resource/1", data); } @Test public void testMatchedUrisParam() throws Exception { @@ -69,7 +69,7 @@ public void testMatchedUrisParam() throws Exception { WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(100000000L); wc.accept("text/plain"); String data = wc.get(String.class); - assertEquals("/my/resource/1/matched/uris/param,/my/resource/1", data); + assertEquals("my/resource/1/matched/uris/param,my/resource/1", data); } @Test public void testMatchedUrisParam2() throws Exception { @@ -78,7 +78,7 @@ public void testMatchedUrisParam2() throws Exception { WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(100000000L); wc.accept("text/plain"); String data = wc.get(String.class); - assertEquals("/my/resource/1/matched/uris/param/2,/my/resource/1", data); + assertEquals("my/resource/1/matched/uris/param/2,my/resource/1", data); } @Test public void testMatchedResources() throws Exception { ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org > JAXRS UriInfo.getMatchedUris does return matched URIs twice for sub resources > ----------------------------------------------------------------------------- > > Key: CXF-7527 > URL: https://issues.apache.org/jira/browse/CXF-7527 > Project: CXF > Issue Type: Bug > Components: JAX-RS > Affects Versions: 3.1.12 > Reporter: Lenoire > Attachments: uriinfo-issues.jar > > > Invoking method {{UriInfo.getMatchedURIs()}} return matched resource URI > twice when invoked from SubResource method. > See attachment for a junit test reproducing the issue (=> > {{testMatchedUrisFromSubResource()}}) -- This message was sent by Atlassian JIRA (v6.4.14#64029)