You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by re...@apache.org on 2022/03/26 16:24:19 UTC

[cxf] 01/02: CXF-8522: jaxrs.spec.resource.requestmatching consumesOnResourceLocatorTest & other subresource test cases (#925)

This is an automated email from the ASF dual-hosted git repository.

reta pushed a commit to branch 3.4.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git

commit 06a18887b054a5833e0618eb95af06c80e13355b
Author: Andriy Redko <dr...@gmail.com>
AuthorDate: Sat Mar 26 12:03:56 2022 -0400

    CXF-8522: jaxrs.spec.resource.requestmatching consumesOnResourceLocatorTest & other subresource test cases (#925)
    
    (cherry picked from commit a0f912b7a54297f4e405a5c8e09df682ece07c9a)
    (cherry picked from commit f2c3963682e6ff13ebe75bb21f725aaeac4d33d5)
    
    # Conflicts:
    #	systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServerSub.java
---
 .../org/apache/cxf/jaxrs/utils/JAXRSUtils.java     |  9 +++++--
 ...toreSubObject.java => BookResourceLocator.java} | 24 ++++++++++-------
 .../apache/cxf/systest/jaxrs/BookServerSub.java    | 31 +++++++++++++++++++++-
 .../cxf/systest/jaxrs/BookStoreSubObject.java      | 14 +++++++++-
 .../jaxrs/JAXRSClientServerSubBookTest.java        | 24 ++++++++++++++++-
 5 files changed, 88 insertions(+), 14 deletions(-)

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 908fa9d..acce616 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
@@ -510,7 +510,7 @@ public final class JAXRSUtils {
         int pathMatched = 0;
         int methodMatched = 0;
         int consumeMatched = 0;
-
+        
         List<OperationResourceInfo> finalPathSubresources = null;
         for (Map.Entry<ClassResourceInfo, MultivaluedMap<String, String>> rEntry : matchedResources.entrySet()) {
             ClassResourceInfo resource = rEntry.getKey();
@@ -561,7 +561,12 @@ public final class JAXRSUtils {
                 LOG.fine(matchMessageLogSupplier(ori, path, httpMethod, requestType, acceptContentTypes, added));
             }
         }
-        if (finalPathSubresources != null && pathMatched > 0
+        
+        // We may get several matching candidates with different HTTP methods which match subresources
+        // and resources. Before excluding subresources, let us make sure we have at least one matching
+        // HTTP method candidate.
+        boolean isOptions = HttpMethod.OPTIONS.equalsIgnoreCase(httpMethod);
+        if (finalPathSubresources != null && (methodMatched > 0 || isOptions)
             && !MessageUtils.getContextualBoolean(message, KEEP_SUBRESOURCE_CANDIDATES, false)) {
             for (OperationResourceInfo key : finalPathSubresources) {
                 candidateList.remove(key);
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreSubObject.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookResourceLocator.java
similarity index 71%
copy from systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreSubObject.java
copy to systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookResourceLocator.java
index 34b8101..76fed9a 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreSubObject.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookResourceLocator.java
@@ -19,16 +19,22 @@
 
 package org.apache.cxf.systest.jaxrs;
 
+import javax.ws.rs.Consumes;
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
 
-import javax.ws.rs.Path;
-
-@Path("/bookstore")
-public class BookStoreSubObject {
-
-    @Path("/booksubresourceobject")
-    public Object getBookSubResourceObject() throws BookNotFoundFault {
+public class BookResourceLocator {
+    @GET
+    public Book get() {
         return new Book();
     }
-}
-
 
+    @POST
+    @Consumes(MediaType.TEXT_PLAIN)
+    @Produces(MediaType.TEXT_PLAIN)
+    public Book post() {
+        return get();
+    }
+}
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServerSub.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServerSub.java
index 0156cd2..2df764e 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServerSub.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServerSub.java
@@ -19,6 +19,12 @@
 
 package org.apache.cxf.systest.jaxrs;
 
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.Response.Status;
+import javax.ws.rs.ext.ExceptionMapper;
+import javax.ws.rs.ext.Provider;
+
 import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
 import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider;
 import org.apache.cxf.testutil.common.AbstractBusTestServerBase;
@@ -27,10 +33,33 @@ public class BookServerSub extends AbstractBusTestServerBase {
     public static final String PORT = allocatePort(BookServerSub.class);
 
     org.apache.cxf.endpoint.Server server;
-    
+
+    @Provider
+    public static class ExceptionMatcher implements ExceptionMapper<WebApplicationException> {
+        @Override
+        public Response toResponse(WebApplicationException exception) {
+            Response response = exception.getResponse();
+            int status = response == null ? Status.INTERNAL_SERVER_ERROR.getStatusCode() : response.getStatus();
+            if (response != null && response.getEntity() != null) {
+                return response;
+            }
+        
+            switch (status) {
+            case 404:
+            case 405:
+            case 406:
+            case 415:
+                return Response.status(status).entity(String.valueOf(status)).build();
+            default:
+                return response;
+            }
+        }
+    }
+
     protected void run() {
         JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
         sf.setStaticSubresourceResolution(true);
+        sf.setProvider(new ExceptionMatcher());
         sf.setResourceClasses(BookStoreSubObject.class);
         sf.setResourceProvider(BookStoreSubObject.class,
                                new SingletonResourceProvider(new BookStoreSubObject(), true));
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreSubObject.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreSubObject.java
index 34b8101..33cf177 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreSubObject.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreSubObject.java
@@ -20,15 +20,27 @@
 package org.apache.cxf.systest.jaxrs;
 
 
+import javax.ws.rs.GET;
 import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
 
 @Path("/bookstore")
 public class BookStoreSubObject {
-
     @Path("/booksubresourceobject")
     public Object getBookSubResourceObject() throws BookNotFoundFault {
         return new Book();
     }
+    
+    @Path("consumeslocator")
+    public BookResourceLocator consumeslocator() {
+        return new BookResourceLocator();
+    }
+    
+    @GET
+    @Path("{id}")
+    public Book book(@PathParam("id") Long id) {
+        return new Book("Book", id);
+    }
 }
 
 
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerSubBookTest.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerSubBookTest.java
index 8b55de4..0ccbfca 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerSubBookTest.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerSubBookTest.java
@@ -19,6 +19,12 @@
 
 package org.apache.cxf.systest.jaxrs;
 
+import javax.ws.rs.client.Client;
+import javax.ws.rs.client.ClientBuilder;
+import javax.ws.rs.client.WebTarget;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+
 import org.apache.cxf.jaxrs.client.WebClient;
 import org.apache.cxf.jaxrs.model.AbstractResourceInfo;
 import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
@@ -26,7 +32,9 @@ import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
 import org.junit.BeforeClass;
 import org.junit.Test;
 
+import static org.hamcrest.CoreMatchers.equalTo;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
 
 public class JAXRSClientServerSubBookTest extends AbstractBusClientServerTestBase {
@@ -44,9 +52,23 @@ public class JAXRSClientServerSubBookTest extends AbstractBusClientServerTestBas
     public void testGetChapterFromBookSubObject() throws Exception {
         WebClient wc =
             WebClient.create("http://localhost:" + PORT + "/bookstore/booksubresourceobject/chaptersobject/sub/1");
-        WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(100000000L);
         Chapter c = wc.accept("application/xml").get(Chapter.class);
         assertNotNull(c);
     }
 
+    @Test
+    public void testSubresourceLocatorFromBookSubObject() throws Exception {
+        final Client c = ClientBuilder.newClient();
+
+        // There are two matching endpoints with different HTTP methods:
+        //  - POST for BookSubObject /consumeslocator"
+        //  - GET for BookStore /{id}
+        // The test verifies that in this case for the POST method the correct subresource
+        // locator is picked.
+        final WebTarget wc = c.target("http://localhost:" + PORT + "/bookstore/consumeslocator");
+        try (Response r = wc.request().header("Content-Type", MediaType.APPLICATION_ATOM_XML).post(null)) {
+            assertThat(r.getStatus(), equalTo(Response.Status.UNSUPPORTED_MEDIA_TYPE.getStatusCode()));
+            assertThat(Integer.toString(r.getStatus()), equalTo(r.readEntity(String.class)));
+        }
+    }
 }