You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by jl...@apache.org on 2007/12/18 17:58:18 UTC

svn commit: r605250 - in /incubator/cxf/trunk: rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/ rt/frontend/jaxrs/src/tes...

Author: jliu
Date: Tue Dec 18 08:58:03 2007
New Revision: 605250

URL: http://svn.apache.org/viewvc?rev=605250&view=rev
Log:
JSR-311 sub-resource locator support. Added system test etc.

Added:
    incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_get_chapter1.txt   (with props)
Modified:
    incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSInvoker.java
    incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSUtils.java
    incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSInInterceptor.java
    incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/URITemplate.java
    incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/URITemplateTest.java
    incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/Book.java
    incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
    incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java

Modified: incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSInvoker.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSInvoker.java?rev=605250&r1=605249&r2=605250&view=diff
==============================================================================
--- incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSInvoker.java (original)
+++ incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSInvoker.java Tue Dec 18 08:58:03 2007
@@ -64,14 +64,24 @@
         
         if (ori.isSubResourceLocator()) {
             //the result becomes the object that will handle the request
+            if (result != null) {
+                if (result instanceof MessageContentsList) {
+                    result = ((MessageContentsList)result).get(0);
+                } else if (result instanceof List) {
+                    result = ((List)result).get(0);
+                } else if (result.getClass().isArray()) {
+                    result = ((Object[])result)[0];
+                } 
+            }
             resourceObjects = new ArrayList<Object>();
             resourceObjects.add(result);
             
             Map<String, String> values = new HashMap<String, String>();                 
             Message msg = exchange.getInMessage();
-            String path = (String)msg.get(JAXRSInInterceptor.RELATIVE_PATH);
+            String subResourcePath = (String)msg.get(JAXRSInInterceptor.SUBRESOURCE_PATH);
             String httpMethod = (String)msg.get(Message.HTTP_REQUEST_METHOD); 
-            OperationResourceInfo subOri = JAXRSUtils.findTargetMethod(classResourceInfo, path,
+            ClassResourceInfo subCri = JAXRSUtils.findSubResourceClass(classResourceInfo, result.getClass());
+            OperationResourceInfo subOri = JAXRSUtils.findTargetMethod(subCri, subResourcePath,
                                                                                      httpMethod, values);
             exchange.put(OperationResourceInfo.class, subOri);
 
@@ -80,11 +90,11 @@
             //I.e., only one place either in the root resource or sub-resouce class can
             //have a parameter that read from entitybody.
             InputStream is = msg.getContent(InputStream.class);
-            List<Object> newParams = JAXRSUtils.processParameters(ori.getMethod(), path,
+            List<Object> newParams = JAXRSUtils.processParameters(subOri.getMethod(), subResourcePath,
                                                                              httpMethod, values, is);
             msg.setContent(List.class, newParams);
             
-            this.invoke(exchange, request);
+            return this.invoke(exchange, newParams);
         }
         
         return result;

Modified: incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSUtils.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSUtils.java?rev=605250&r1=605249&r2=605250&view=diff
==============================================================================
--- incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSUtils.java (original)
+++ incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSUtils.java Tue Dec 18 08:58:03 2007
@@ -45,14 +45,24 @@
     
     private JAXRSUtils() {        
     }
-
+    
+    public static ClassResourceInfo findSubResourceClass(ClassResourceInfo resource,
+                                                             Class subResourceClassType) {
+        for (ClassResourceInfo subCri : resource.getSubClassResourceInfo()) {
+            if (subCri.getResourceClass() == subResourceClassType) {
+                return subCri;
+            }
+        }
+        return null;
+    }
+    
     public static OperationResourceInfo findTargetResourceClass(List<ClassResourceInfo> resources,
                                                                 String path, String httpMethod,
                                                                 Map<String, String> values) {
         for (ClassResourceInfo resource : resources) {
             URITemplate uriTemplate = resource.getURITemplate();
             if (uriTemplate.match(path, values)) {
-                String subResourcePath = values.values().iterator().next();
+                String subResourcePath = values.get(URITemplate.RIGHT_HAND_VALUE);
                 OperationResourceInfo ori = findTargetMethod(resource, subResourcePath, httpMethod, values);
                 if (ori != null) {
                     return ori;

Modified: incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSInInterceptor.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSInInterceptor.java?rev=605250&r1=605249&r2=605250&view=diff
==============================================================================
--- incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSInInterceptor.java (original)
+++ incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSInInterceptor.java Tue Dec 18 08:58:03 2007
@@ -28,6 +28,7 @@
 import org.apache.cxf.jaxrs.JAXRSUtils;
 import org.apache.cxf.jaxrs.model.ClassResourceInfo;
 import org.apache.cxf.jaxrs.model.OperationResourceInfo;
+import org.apache.cxf.jaxrs.model.URITemplate;
 import org.apache.cxf.message.Message;
 import org.apache.cxf.phase.AbstractPhaseInterceptor;
 import org.apache.cxf.phase.Phase;
@@ -36,6 +37,7 @@
 public class JAXRSInInterceptor extends AbstractPhaseInterceptor<Message> {
 
     public static final String RELATIVE_PATH = "relative.path";
+    public static final String SUBRESOURCE_PATH = "subresource.path";
 
     //private static final Logger LOG = Logger.getLogger(RESTDispatchInterceptor.class.getName());
     //private static final ResourceBundle BUNDLE = BundleUtils.getBundle(RESTDispatchInterceptor.class);
@@ -80,7 +82,8 @@
             //throw new Fault(new org.apache.cxf.common.i18n.Message("NO_OP", BUNDLE, method, path));
         }
         message.getExchange().put(OperationResourceInfo.class, ori);
-
+        message.put(SUBRESOURCE_PATH, values.get(URITemplate.RIGHT_HAND_VALUE));
+        
         //2. Process parameters
         InputStream is = message.getContent(InputStream.class);
         List<Object> params = JAXRSUtils.processParameters(ori.getMethod(), path, httpMethod, values, is);

Modified: incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/URITemplate.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/URITemplate.java?rev=605250&r1=605249&r2=605250&view=diff
==============================================================================
--- incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/URITemplate.java (original)
+++ incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/URITemplate.java Tue Dec 18 08:58:03 2007
@@ -30,6 +30,7 @@
 public final class URITemplate {
     public static final String LIMITED_REGEX_SUFFIX = "(/.*)?";
     public static final String UNLIMITED_REGEX_SUFFIX = "(/)?";
+    public static final String RIGHT_HAND_VALUE = "RIGHT_HAND_VALUE";
     
     /**
      * The regular expression for matching URI templates and names.
@@ -128,9 +129,9 @@
             templateVariableToValue.put(name, currentValue);
         }
 
-        // Assign the right hand side value to the null key
+        // The right hand side value, might be used to further resolve sub-resources.
         if (regexSuffix != null) {
-            templateVariableToValue.put(null, m.group(i));
+            templateVariableToValue.put(RIGHT_HAND_VALUE, m.group(i));
         }
 
         return true;

Modified: incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/URITemplateTest.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/URITemplateTest.java?rev=605250&r1=605249&r2=605250&view=diff
==============================================================================
--- incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/URITemplateTest.java (original)
+++ incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/URITemplateTest.java Tue Dec 18 08:58:03 2007
@@ -81,11 +81,10 @@
         
         boolean match = uriTemplate.match("/customers/123", values);
         assertTrue(match);
-        String subResourcePath = values.values().iterator().next();
+        String subResourcePath = values.get(URITemplate.RIGHT_HAND_VALUE);
         assertEquals("/123", subResourcePath);
     }
-    
-    
+        
     @Test
     public void testURITemplateWithSubResourceVariation2() throws Exception {
         //So "/customers" is the URITemplate for the root resource class
@@ -94,7 +93,22 @@
         
         boolean match = uriTemplate.match("/customers/name/john/dep/CS", values);
         assertTrue(match);
-        String subResourcePath = values.values().iterator().next();
+        String subResourcePath = values.get(URITemplate.RIGHT_HAND_VALUE);
         assertEquals("/name/john/dep/CS", subResourcePath);
+    }
+        
+    @Test
+    /* Test a sub-resource locator method like this
+     * @HttpMethod("GET") @UriTemplate("/books/{bookId}/") 
+     * public Book getBook(@UriParam("bookId") String id)
+     */
+    public void testURITemplateWithSubResourceVariation3() throws Exception {
+        URITemplate uriTemplate = new URITemplate("/books/{bookId}/", URITemplate.LIMITED_REGEX_SUFFIX);
+        Map<String, String> values = new HashMap<String, String>();
+
+        boolean match = uriTemplate.match("/books/123/chapter/1", values);
+        assertTrue(match);
+        String subResourcePath = values.get(URITemplate.RIGHT_HAND_VALUE);
+        assertEquals("/chapter/1", subResourcePath);
     }
 }

Modified: incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/Book.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/Book.java?rev=605250&r1=605249&r2=605250&view=diff
==============================================================================
--- incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/Book.java (original)
+++ incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/Book.java Tue Dec 18 08:58:03 2007
@@ -22,6 +22,7 @@
 import java.util.HashMap;
 import java.util.Map;
 
+import javax.ws.rs.HttpMethod;
 import javax.ws.rs.UriParam;
 import javax.ws.rs.UriTemplate;
 import javax.xml.bind.annotation.XmlRootElement;
@@ -53,8 +54,11 @@
         return id;
     }
     
-    @UriTemplate("chapters/{chapterid}/")
-    public Chapter getChapter(@UriParam("id")int chapterid) {
+    @HttpMethod("GET")
+    @UriTemplate("chapters/{chapterid}/")    
+    public Chapter getChapter(@UriParam("chapterid")int chapterid) {
+        System.out.println("----invoking getChapter with chapterid: " + chapterid);
+
         return chapters.get(new Long(chapterid));
     }   
     

Modified: incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java?rev=605250&r1=605249&r2=605250&view=diff
==============================================================================
--- incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java (original)
+++ incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java Tue Dec 18 08:58:03 2007
@@ -62,7 +62,7 @@
         }
     }
     
-    @UriTemplate("booksubresource/{bookId}/")
+    @UriTemplate("/booksubresource/{bookId}/")
     public Book getBookSubResource(@UriParam("bookId") String id) throws BookNotFoundFault {
         System.out.println("----invoking getBookSubResource with id: " + id);
         Book book = books.get(Long.parseLong(id));

Modified: incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java?rev=605250&r1=605249&r2=605250&view=diff
==============================================================================
--- incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java (original)
+++ incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java Tue Dec 18 08:58:03 2007
@@ -57,6 +57,20 @@
     }
     
     @Test
+    public void testGetChapter() throws Exception {
+        String endpointAddress =
+            "http://localhost:9080/bookstore/booksubresource/123/chapters/1"; 
+        URL url = new URL(endpointAddress);
+        InputStream in = url.openStream();
+        assertNotNull(in);           
+
+        InputStream expected = getClass()
+            .getResourceAsStream("resources/expected_get_chapter1.txt");
+
+        assertEquals(getStringFromInputStream(expected), getStringFromInputStream(in)); 
+    }
+    
+    @Test
     public void testGetBook123ReturnString() throws Exception {
         String endpointAddress =
             "http://localhost:9080/bookstore/booknames/123"; 

Added: incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_get_chapter1.txt
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_get_chapter1.txt?rev=605250&view=auto
==============================================================================
--- incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_get_chapter1.txt (added)
+++ incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_get_chapter1.txt Tue Dec 18 08:58:03 2007
@@ -0,0 +1 @@
+<Chapter><id>1</id><title>chapter 1</title></Chapter>
\ No newline at end of file

Propchange: incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_get_chapter1.txt
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_get_chapter1.txt
------------------------------------------------------------------------------
    svn:mime-type = text/plain