Repository: cxf Updated Branches: refs/heads/master fad0c99f7 -> af229e25f
[CXF-5650] Optionally dropping final path subresource candidates Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/af229e25 Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/af229e25 Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/af229e25 Branch: refs/heads/master Commit: af229e25f6a793853bffd91b59857b0223ae8b93 Parents: fad0c99 Author: Sergey Beryozkin <[email protected]> Authored: Mon Mar 31 10:35:02 2014 +0100 Committer: Sergey Beryozkin <[email protected]> Committed: Mon Mar 31 10:35:02 2014 +0100 ---------------------------------------------------------------------- .../org/apache/cxf/jaxrs/utils/JAXRSUtils.java | 50 +++++++++++------ .../cxf/jaxrs/SelectMethodCandidatesTest.java | 59 +++++++++++++++----- 2 files changed, 78 insertions(+), 31 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/af229e25/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java ---------------------------------------------------------------------- diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java index 678d691..7f4d1f8 100644 --- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java +++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java @@ -155,7 +155,7 @@ public final class JAXRSUtils { public static final String MEDIA_TYPE_QS_PARAM = "qs"; private static final String MEDIA_TYPE_DISTANCE_PARAM = "d"; private static final String DEFAULT_CONTENT_TYPE = "default.content.type"; - + private static final String KEEP_SUBRESOURCE_CANDIDATES = "keep.subresource.candidates"; private static final Logger LOG = LogUtils.getL7dLogger(JAXRSUtils.class); private static final ResourceBundle BUNDLE = BundleUtils.getBundle(JAXRSUtils.class); private static final String PATH_SEGMENT_SEP = "/"; @@ -390,6 +390,8 @@ public final class JAXRSUtils { int methodMatched = 0; int consumeMatched = 0; + boolean resourceMethodsAdded = false; + List<OperationResourceInfo> finalPathSubresources = null; for (Map.Entry<ClassResourceInfo, MultivaluedMap<String, String>> rEntry : matchedResources.entrySet()) { ClassResourceInfo resource = rEntry.getKey(); MultivaluedMap<String, String> values = rEntry.getValue(); @@ -410,28 +412,35 @@ public final class JAXRSUtils { URITemplate uriTemplate = ori.getURITemplate(); MultivaluedMap<String, String> map = new MetadataMap<String, String>(values); if (uriTemplate != null && uriTemplate.match(path, map)) { + String finalGroup = map.getFirst(URITemplate.FINAL_MATCH_GROUP); + boolean finalPath = StringUtils.isEmpty(finalGroup) || PATH_SEGMENT_SEP.equals(finalGroup); + if (ori.isSubResourceLocator()) { candidateList.put(ori, map); + if (finalPath) { + if (finalPathSubresources == null) { + finalPathSubresources = new LinkedList<OperationResourceInfo>(); + } + finalPathSubresources.add(ori); + } added = true; - } else { - String finalGroup = map.getFirst(URITemplate.FINAL_MATCH_GROUP); - if (StringUtils.isEmpty(finalGroup) || PATH_SEGMENT_SEP.equals(finalGroup)) { - pathMatched++; - if (matchHttpMethod(ori.getHttpMethod(), httpMethod)) { - methodMatched++; - //CHECKSTYLE:OFF - if (getMethod || matchConsumeTypes(requestType, ori)) { - consumeMatched++; - for (MediaType acceptType : acceptContentTypes) { - if (matchProduceTypes(acceptType, ori)) { - candidateList.put(ori, map); - added = true; - break; - } + } else if (finalPath) { + pathMatched++; + if (matchHttpMethod(ori.getHttpMethod(), httpMethod)) { + methodMatched++; + //CHECKSTYLE:OFF + if (getMethod || matchConsumeTypes(requestType, ori)) { + consumeMatched++; + for (MediaType acceptType : acceptContentTypes) { + if (matchProduceTypes(acceptType, ori)) { + candidateList.put(ori, map); + added = true; + resourceMethodsAdded = true; + break; } } - //CHECKSTYLE:ON } + //CHECKSTYLE:ON } } } @@ -446,7 +455,12 @@ public final class JAXRSUtils { } } } - + if (finalPathSubresources != null && resourceMethodsAdded + && !MessageUtils.getContextualBoolean(message, KEEP_SUBRESOURCE_CANDIDATES, false)) { + for (OperationResourceInfo key : finalPathSubresources) { + candidateList.remove(key); + } + } if (!candidateList.isEmpty()) { Map.Entry<OperationResourceInfo, MultivaluedMap<String, String>> firstEntry = candidateList.entrySet().iterator().next(); http://git-wip-us.apache.org/repos/asf/cxf/blob/af229e25/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/SelectMethodCandidatesTest.java ---------------------------------------------------------------------- diff --git a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/SelectMethodCandidatesTest.java b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/SelectMethodCandidatesTest.java index 990288b..d51cd83 100644 --- a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/SelectMethodCandidatesTest.java +++ b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/SelectMethodCandidatesTest.java @@ -571,10 +571,19 @@ public class SelectMethodCandidatesTest extends Assert { @Test public void testFindTargetSubResource4() throws Exception { - doTestFindTargetSubResource("/1/2/3/d/resource2/1/2", "subresource2"); + doTestFindTargetSubResource("/1/2/3/d/resource2/1/2", "subresource2", true); + } + @Test + public void testFindTargetSubResource5() throws Exception { + doTestFindTargetSubResource("/1/2/3/d/resource2/1/2", "resourceMethod2"); } public void doTestFindTargetSubResource(String path, String method) throws Exception { + doTestFindTargetSubResource(path, method, false); + } + + public void doTestFindTargetSubResource(String path, String method, boolean setKeepSubProp) + throws Exception { JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean(); sf.setResourceClasses(org.apache.cxf.jaxrs.resources.TestResource.class); sf.create(); @@ -583,11 +592,13 @@ public class SelectMethodCandidatesTest extends Assert { String acceptContentTypes = "text/xml,*/*"; MetadataMap<String, String> values = new MetadataMap<String, String>(); - OperationResourceInfo ori = findTargetResourceClass(resources, createMessage(), + OperationResourceInfo ori = findTargetResourceClass(resources, + createMessage(), path, "GET", values, contentTypes, - sortMediaTypes(acceptContentTypes)); + sortMediaTypes(acceptContentTypes), + setKeepSubProp); assertNotNull(ori); assertEquals("resourceMethod needs to be selected", method, ori.getMethodToInvoke().getName()); @@ -741,11 +752,16 @@ public class SelectMethodCandidatesTest extends Assert { assertEquals("readFoo", ori.getMethodToInvoke().getName()); } - private Message createMessage() { + return createMessage(false); + } + private Message createMessage(boolean setKeepSubProp) { ProviderFactory factory = ServerProviderFactory.getInstance(); Message m = new MessageImpl(); m.put("org.apache.cxf.http.case_insensitive_queries", false); + if (setKeepSubProp) { + m.put("keep.subresource.candidates", true); + } Exchange e = new ExchangeImpl(); m.setExchange(e); e.setInMessage(m); @@ -764,21 +780,38 @@ public class SelectMethodCandidatesTest extends Assert { e.put(Endpoint.class, endpoint); return m; } - private OperationResourceInfo findTargetResourceClass(List<ClassResourceInfo> resources, - Message message, - String path, - String httpMethod, - MultivaluedMap<String, String> values, - String requestContentType, - List<MediaType> acceptContentTypes) { + Message message, + String path, + String httpMethod, + MultivaluedMap<String, String> values, + String requestContentType, + List<MediaType> acceptContentTypes) { + return findTargetResourceClass(resources, message, path, httpMethod, values, requestContentType, + acceptContentTypes, false); + + } + //CHECKSTYLE:OFF + private OperationResourceInfo findTargetResourceClass(List<ClassResourceInfo> resources, + Message message, + String path, + String httpMethod, + MultivaluedMap<String, String> values, + String requestContentType, + List<MediaType> acceptContentTypes, + boolean setKeepSubProp) { + //CHECKSTYLE:ON message = message == null ? new MessageImpl() : message; Map<ClassResourceInfo, MultivaluedMap<String, String>> mResources = JAXRSUtils.selectResourceClass(resources, path, message); if (mResources != null) { - OperationResourceInfo ori = JAXRSUtils.findTargetMethod(mResources, createMessage(), httpMethod, - values, requestContentType, acceptContentTypes); + OperationResourceInfo ori = JAXRSUtils.findTargetMethod(mResources, + createMessage(setKeepSubProp), + httpMethod, + values, + requestContentType, + acceptContentTypes); if (ori != null) { return ori; }
