cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j...@apache.org
Subject svn commit: r575303 - in /incubator/cxf/branches/jliu: rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ systests/src/test/java/org/apache/cxf/systest/jaxrs/ systests/src/te...
Date Thu, 13 Sep 2007 13:16:26 GMT
Author: jliu
Date: Thu Sep 13 06:16:25 2007
New Revision: 575303

URL: http://svn.apache.org/viewvc?rev=575303&view=rev
Log:
Looks like the JAXB databinding in JAX-RS can be very simple. Different from WSDL-based services
in CXF, the JAXB databinding in JAX-RS does not need to handle Array, List etc as a pure plain-old-xml
binding does not allow multiple root elements anyway, you always have to use a wrapper class,
like Books. 

Added:
    incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/CDs.java
  (with props)
    incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_get_cds.txt
  (with props)
Modified:
    incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSOutInterceptor.java
    incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
    incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
    incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java

Modified: incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSOutInterceptor.java
URL: http://svn.apache.org/viewvc/incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSOutInterceptor.java?rev=575303&r1=575302&r2=575303&view=diff
==============================================================================
--- incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSOutInterceptor.java
(original)
+++ incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSOutInterceptor.java
Thu Sep 13 06:16:25 2007
@@ -21,7 +21,6 @@
 
 import java.io.IOException;
 import java.io.OutputStream;
-import java.util.List;
 
 import javax.ws.rs.core.Response;
 import javax.ws.rs.ext.EntityProvider;
@@ -61,9 +60,7 @@
             if (objs.get(0) instanceof Response) {
                 Response response = (Response)responseObj;
                 responseObj = response.getEntity();   
-                
-                //HttpServletResponse hsr = (HttpServletResponse)message.get("HTTP.RESPONSE");
-                //hsr.setStatus(response.getStatus());
+
                 message.put(Message.RESPONSE_CODE, response.getStatus());
                 
                 if (responseObj == null) {
@@ -72,12 +69,15 @@
             } 
             
             Class targetType = responseObj.getClass();
-            if (responseObj.getClass().isArray()) {
+/*            if (responseObj.getClass().isArray()) {
                 targetType = responseObj.getClass().getComponentType();
             } else if (responseObj instanceof List && ((List)responseObj).get(0)
!= null) {
+                //NOTE: if its a List, the provider should try to determine if it can support
+                //every object inside the List instead of the first one only.
                 targetType = ((List)responseObj).get(0).getClass();
                 
-            }
+            }*/
+            
             EntityProvider provider = ProviderFactory.getInstance().createEntityProvider(targetType);
 
             try {

Modified: incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
URL: http://svn.apache.org/viewvc/incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java?rev=575303&r1=575302&r2=575303&view=diff
==============================================================================
--- incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
(original)
+++ incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
Thu Sep 13 06:16:25 2007
@@ -22,19 +22,15 @@
 
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.lang.reflect.Array;
-import java.util.List;
 import java.util.Map;
 import java.util.WeakHashMap;
 import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.ext.EntityProvider;
 import javax.xml.bind.JAXBContext;
-import javax.xml.bind.JAXBElement;
 import javax.xml.bind.JAXBException;
 import javax.xml.bind.Marshaller;
 import javax.xml.bind.Unmarshaller;
 import javax.xml.bind.annotation.XmlRootElement;
-import javax.xml.namespace.QName;
 
 public final class JAXBElementProvider implements EntityProvider<Object>  {
 
@@ -59,7 +55,9 @@
 
     public void writeTo(Object obj, MultivaluedMap<String, Object> headers, OutputStream
os) {
         try {
-            if (obj.getClass().isArray() || obj instanceof List) {
+            //Looks like we do not need to deal with Array and List as multiple root elements

+            //is not allowed in a plain-old-xml binding anyway.
+/*            if (obj.getClass().isArray() || obj instanceof List) {
                 Class<?> cls = null;
                 Object objArray;
                 if (obj instanceof List) {
@@ -79,12 +77,14 @@
                     marshaller.marshal(new JAXBElement(new QName(null, o.getClass().getSimpleName()),
                                                        cls == null ? o.getClass() : cls,
o), os);
                 }
-            } else {
-                JAXBContext context = getJAXBContext(obj.getClass());
-                Marshaller marshaller = context.createMarshaller();
-                marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
-                marshaller.marshal(obj, os);
-            }
+            } else {*/
+            JAXBContext context = getJAXBContext(obj.getClass());
+            Marshaller marshaller = context.createMarshaller();
+            marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
+            marshaller.marshal(obj, os);
+            
+            //TODO: support Calendar type
+            //}
         } catch (JAXBException e) {
             e.printStackTrace();
         }

Modified: incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
URL: http://svn.apache.org/viewvc/incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java?rev=575303&r1=575302&r2=575303&view=diff
==============================================================================
--- incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
(original)
+++ incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
Thu Sep 13 06:16:25 2007
@@ -36,7 +36,9 @@
 public class BookStore {
     
     private static List<Book> books = new ArrayList<Book>();
+    private static List<CD> cds = new ArrayList<CD>();
     private static long bookId = 123;
+    private static long cdId = 123;
     
     @HttpContext UriInfo uriInfo;
 
@@ -45,17 +47,26 @@
         book.setId(bookId);
         book.setName("CXF in Action");
         books.add(book);
+        
+        CD cd = new CD();
+        cd.setId(cdId);
+        cd.setName("BOHEMIAN RHAYSODY");
+        cds.add(cd);
+        CD cd1 = new CD();
+        cd1.setId(++cdId);
+        cd1.setName("BICYCLE RACE");
+        cds.add(cd1);
     }
     
     public BookStore() {
     }
-
+/*
     @HttpMethod("GET")
     public List<Book> getAllItems() {
         System.out.println("----invoking getBooks");
        
         return books;
-    }    
+    }    */
    
     @HttpMethod("GET")
     @UriTemplate("/books/{bookId}/")
@@ -79,8 +90,7 @@
         
         books.add(book);
 
-        Response r = Response.Builder.ok(book).build();
-        return r;
+        return Response.Builder.ok(book).build();
     }
     
     @HttpMethod("PUT")
@@ -132,16 +142,27 @@
         
         return r;        
     }
-
     
-    @UriTemplate("/cd/{CDId}/")
-    public CD getCD(@UriParam("CDId") String cdId) {
-        System.out.println("----invoking getCD with cdId: " + cdId);
-        CD cd = new CD();
-        cd.setId(223);
-        cd.setName("BOHEMIAN RHAYSODY");
+    @UriTemplate("/cds/{CDId}/")
+    public CD getCD(@UriParam("CDId") String id) {
+        System.out.println("----invoking getCD with cdId: " + id);
+        long idNumber = Long.parseLong(id);
+        for (CD b : cds) {
+            if (idNumber == b.getId()) {
+                return b;
+            }
+        }
         
-        return cd;
+        return null;
+    }
+    
+    @HttpMethod("GET")
+    @UriTemplate("/cds/")    
+    public CDs getCDs() {
+        System.out.println("----invoking getCDs");
+        CDs c = new CDs();       
+        c.setCD(cds);
+        return c;
     }
 }
 

Added: incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/CDs.java
URL: http://svn.apache.org/viewvc/incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/CDs.java?rev=575303&view=auto
==============================================================================
--- incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/CDs.java
(added)
+++ incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/CDs.java
Thu Sep 13 06:16:25 2007
@@ -0,0 +1,37 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.cxf.systest.jaxrs;
+
+import java.util.Collection;
+
+import javax.xml.bind.annotation.XmlRootElement;
+
+@XmlRootElement(name = "CDs")
+public class CDs {
+    private Collection<CD> cds;
+
+    public Collection<CD> getCD() {
+        return cds;
+    }
+
+    public void setCD(Collection<CD> c) {
+        this.cds = c;
+    }
+}

Propchange: incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/CDs.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/CDs.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
URL: http://svn.apache.org/viewvc/incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java?rev=575303&r1=575302&r2=575303&view=diff
==============================================================================
--- incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
(original)
+++ incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
Thu Sep 13 06:16:25 2007
@@ -42,21 +42,6 @@
     public static void startServers() throws Exception {
         assertTrue("server did not launch correctly", launchServer(BookServer.class));
     }
-   
-    @Test
-    public void testGetBooks() throws Exception {
-        String endpointAddress =
-            "http://localhost:9080/xml/bookstore"; 
-        URL url = new URL(endpointAddress);
-        InputStream in = url.openStream();
-        assertNotNull(in);           
-
-        InputStream expected = getClass()
-            .getResourceAsStream("resources/expected_get_books.txt");
-
-        //System.out.println("---" + getStringFromInputStream(in));
-        assertEquals(getStringFromInputStream(expected), getStringFromInputStream(in)); 
-    }
     
     @Test
     public void testGetBook123() throws Exception {
@@ -173,6 +158,22 @@
             post.releaseConnection();
         }               
     } 
+    
+    
+    @Test
+    public void testGetCDs() throws Exception {
+        String endpointAddress =
+            "http://localhost:9080/xml/bookstore/cds"; 
+        URL url = new URL(endpointAddress);
+        InputStream in = url.openStream();
+        assertNotNull(in);           
+
+        InputStream expected = getClass()
+            .getResourceAsStream("resources/expected_get_cds.txt");
+
+        //System.out.println("---" + getStringFromInputStream(in));
+        assertEquals(getStringFromInputStream(expected), getStringFromInputStream(in)); 
+    }
     
     private String getStringFromInputStream(InputStream in) throws Exception {        
         CachedOutputStream bos = new CachedOutputStream();

Added: incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_get_cds.txt
URL: http://svn.apache.org/viewvc/incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_get_cds.txt?rev=575303&view=auto
==============================================================================
--- incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_get_cds.txt
(added)
+++ incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_get_cds.txt
Thu Sep 13 06:16:25 2007
@@ -0,0 +1 @@
+<CDs><CD><id>123</id><name>BOHEMIAN RHAYSODY</name></CD><CD><id>124</id><name>BICYCLE
RACE</name></CD></CDs>
\ No newline at end of file

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

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



Mime
View raw message