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/06 06:44:33 UTC

svn commit: r601622 - in /incubator/cxf/trunk: rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ systests/src/test/java/org/apache/cxf/systest/jaxrs/

Author: jliu
Date: Wed Dec  5 21:44:29 2007
New Revision: 601622

URL: http://svn.apache.org/viewvc?rev=601622&view=rev
Log:
CXF-1255. Support using DOMSource and input and output parameter types in JAX-RS. 

Added:
    incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/DOMSourceProvider.java   (with props)
Modified:
    incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactoryImpl.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

Added: incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/DOMSourceProvider.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/DOMSourceProvider.java?rev=601622&view=auto
==============================================================================
--- incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/DOMSourceProvider.java (added)
+++ incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/DOMSourceProvider.java Wed Dec  5 21:44:29 2007
@@ -0,0 +1,78 @@
+/**
+ * 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.jaxrs.provider;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.ext.EntityProvider;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+
+import org.w3c.dom.Document;
+
+
+import org.xml.sax.SAXException;
+
+public class DOMSourceProvider implements  EntityProvider<DOMSource> {
+
+    public boolean supports(Class<?> type) {
+        return DOMSource.class.isAssignableFrom(type);
+    }
+    
+    public DOMSource readFrom(Class<DOMSource> source, MediaType media,
+                              MultivaluedMap<String, String> httpHeaders, InputStream is) throws IOException {
+        Document doc = null;
+        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+        DocumentBuilder builder;
+        try {
+            builder = factory.newDocumentBuilder();
+            doc = builder.parse(is);
+        } catch (SAXException e) {
+            e.printStackTrace();
+
+        } catch (ParserConfigurationException e1) {
+            e1.printStackTrace();
+        }
+
+        return new DOMSource(doc);
+    }
+
+    public void writeTo(DOMSource source, MediaType media, MultivaluedMap<String, Object> httpHeaders,
+                        OutputStream os) throws IOException {
+        StreamResult result = new StreamResult(os);
+        TransformerFactory tf = TransformerFactory.newInstance();
+        try {
+            Transformer t = tf.newTransformer();
+            t.transform(source, result);
+        } catch (TransformerException te) {
+            te.printStackTrace();
+        }
+    }
+}

Propchange: incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/DOMSourceProvider.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/DOMSourceProvider.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactoryImpl.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactoryImpl.java?rev=601622&r1=601621&r2=601622&view=diff
==============================================================================
--- incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactoryImpl.java (original)
+++ incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactoryImpl.java Wed Dec  5 21:44:29 2007
@@ -43,7 +43,7 @@
         entityProviders.add(new JAXBElementProvider());
         entityProviders.add(new JSONProvider());
         entityProviders.add(new StringProvider());
-
+        entityProviders.add(new DOMSourceProvider());
         sort();
     }
     

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=601622&r1=601621&r2=601622&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 Wed Dec  5 21:44:29 2007
@@ -29,9 +29,11 @@
 import javax.ws.rs.UriParam;
 import javax.ws.rs.UriTemplate;
 import javax.ws.rs.core.Response;
+import javax.xml.transform.dom.DOMSource;
 
 import org.apache.cxf.customer.book.BookNotFoundDetails;
 import org.apache.cxf.customer.book.BookNotFoundFault;
+import org.apache.cxf.helpers.XMLUtils;
 
 @UriTemplate("/bookstore/")
 public class BookStore {
@@ -109,7 +111,14 @@
 
         return r;
     }
-
+    
+    @HttpMethod("PUT")
+    @UriTemplate("/bookswithdom/")
+    public DOMSource updateBook(DOMSource ds) {
+        System.out.println("----invoking updateBook with DOMSource");
+        XMLUtils.printDOM(ds.getNode());
+        return ds;
+    }
 
     @HttpMethod("DELETE")
     @UriTemplate("/books/{bookId}/")

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=601622&r1=601621&r2=601622&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 Wed Dec  5 21:44:29 2007
@@ -165,6 +165,32 @@
     }  
     
     @Test
+    public void testUpdateBookWithDom() throws Exception {
+        String endpointAddress = "http://localhost:9080/bookstore/bookswithdom";
+
+        String inputFile = getClass().getResource("resources/update_book.txt").getFile();
+        File input = new File(inputFile);
+        PutMethod put = new PutMethod(endpointAddress);
+        RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");
+        put.setRequestEntity(entity);
+        HttpClient httpclient = new HttpClient();
+
+        try {
+            int result = httpclient.executeMethod(put);
+            assertEquals(200, result);
+            System.out.println(put.getResponseBodyAsString());
+        } finally {
+            // Release current connection to the connection pool once you are
+            // done
+            put.releaseConnection();
+        }
+        
+        InputStream expected = getClass().getResourceAsStream("resources/update_book.txt");
+
+        assertTrue(put.getResponseBodyAsString().indexOf(getStringFromInputStream(expected)) >= 0);
+    }
+    
+    @Test
     public void testUpdateBookFailed() throws Exception {
         String endpointAddress =
             "http://localhost:9080/bookstore/books";