cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1350305 - in /cxf/trunk/rt/rs/extensions/providers: ./ src/main/java/org/apache/cxf/jaxrs/provider/dom4j/ src/test/java/org/apache/cxf/jaxrs/provider/dom4j/
Date Thu, 14 Jun 2012 15:53:47 GMT
Author: sergeyb
Date: Thu Jun 14 15:53:47 2012
New Revision: 1350305

URL: http://svn.apache.org/viewvc?rev=1350305&view=rev
Log:
[CXF-4381] Adding an optional DOM4J provider

Added:
    cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/dom4j/
    cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/dom4j/DOM4JProvider.java
  (with props)
    cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/dom4j/
    cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/dom4j/DOM4JProviderTest.java
  (with props)
Modified:
    cxf/trunk/rt/rs/extensions/providers/pom.xml

Modified: cxf/trunk/rt/rs/extensions/providers/pom.xml
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/providers/pom.xml?rev=1350305&r1=1350304&r2=1350305&view=diff
==============================================================================
--- cxf/trunk/rt/rs/extensions/providers/pom.xml (original)
+++ cxf/trunk/rt/rs/extensions/providers/pom.xml Thu Jun 14 15:53:47 2012
@@ -37,6 +37,7 @@
         <cxf.osgi.import>
             javax.servlet*;version="${cxf.osgi.javax.servlet.version}",
         </cxf.osgi.import>
+        <dom4j.version>1.6.1</dom4j.version>
     </properties>
 
     <dependencies>
@@ -87,6 +88,13 @@
             <optional>true</optional>
         </dependency>
         <dependency>
+			<groupId>dom4j</groupId>
+			<artifactId>dom4j</artifactId>
+			<version>${dom4j.version}</version>
+			<scope>provided</scope>
+            <optional>true</optional>
+		</dependency>
+        <dependency>
             <groupId>org.apache.abdera</groupId>
             <artifactId>abdera-core</artifactId>
             <scope>provided</scope>

Added: cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/dom4j/DOM4JProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/dom4j/DOM4JProvider.java?rev=1350305&view=auto
==============================================================================
--- cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/dom4j/DOM4JProvider.java
(added)
+++ cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/dom4j/DOM4JProvider.java
Thu Jun 14 15:53:47 2012
@@ -0,0 +1,110 @@
+/**
+ * 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.dom4j;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.StringReader;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.ext.MessageBodyReader;
+import javax.ws.rs.ext.MessageBodyWriter;
+import javax.ws.rs.ext.Providers;
+
+import org.apache.cxf.helpers.DOMUtils;
+
+@javax.ws.rs.Produces({"application/xml", "application/json" })
+@javax.ws.rs.Consumes({"application/xml", "application/json" })
+public class DOM4JProvider implements MessageBodyReader<org.dom4j.Document>,
+                MessageBodyWriter<org.dom4j.Document> {
+
+    private static final Class<org.w3c.dom.Document> DOM_DOC_CLS = 
+        org.w3c.dom.Document.class;
+
+    private Providers providers;
+
+    @Context
+    public void setProviders(Providers providers) {
+        this.providers = providers;
+    }
+
+    public boolean isReadable(Class<?> cls, Type type, Annotation[] anns,
+                    MediaType mt) {
+        return org.dom4j.Document.class.isAssignableFrom(cls);
+    }
+
+    public org.dom4j.Document readFrom(Class<org.dom4j.Document> cls, Type type,
+                    Annotation[] anns, MediaType mt,
+                    MultivaluedMap<String, String> headers, InputStream is)
+        throws IOException, WebApplicationException {
+        MessageBodyReader<org.w3c.dom.Document> reader =
+            providers.getMessageBodyReader(DOM_DOC_CLS, DOM_DOC_CLS, anns, mt);
+        if (reader == null) {
+            throw new WebApplicationException(415);
+        }
+        org.w3c.dom.Document domDoc =
+            reader.readFrom(DOM_DOC_CLS, DOM_DOC_CLS, anns, mt, headers, is);
+        return new org.dom4j.io.DOMReader().read(domDoc);
+    }
+
+    public long getSize(org.dom4j.Document doc, Class<?> cls, Type type,
+        Annotation[] anns, MediaType mt) {
+        return -1;
+    }
+
+    public boolean isWriteable(Class<?> cls, Type type,
+                               Annotation[] anns, MediaType mt) {
+        return org.dom4j.Document.class.isAssignableFrom(cls);
+    }
+
+    public void writeTo(org.dom4j.Document doc, Class<?> cls, 
+                        Type type, Annotation[] anns, MediaType mt,
+                        MultivaluedMap<String, Object> headers, OutputStream os)
+        throws IOException, WebApplicationException {
+        if (mt.getSubtype().contains("xml")) {
+            org.dom4j.io.XMLWriter writer = new org.dom4j.io.XMLWriter(os);
+            writer.write(doc);
+            writer.flush();
+        } else {
+            org.w3c.dom.Document domDoc = convertToDOM(doc);
+
+            MessageBodyWriter<org.w3c.dom.Document> writer =
+                providers.getMessageBodyWriter(DOM_DOC_CLS, DOM_DOC_CLS, anns, mt);
+            if (writer == null) {
+                throw new WebApplicationException(406);
+            }
+            writer.writeTo(domDoc, DOM_DOC_CLS, DOM_DOC_CLS, anns, mt, headers, os);
+        }
+    }
+
+    private org.w3c.dom.Document convertToDOM(org.dom4j.Document doc) {
+        String xml = doc.asXML();
+        try {
+            return DOMUtils.readXml(new StringReader(xml));
+        } catch (Exception ex) {
+            throw new javax.ws.rs.WebApplicationException(ex);
+        }
+    }
+}

Propchange: cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/dom4j/DOM4JProvider.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/dom4j/DOM4JProvider.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/dom4j/DOM4JProviderTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/dom4j/DOM4JProviderTest.java?rev=1350305&view=auto
==============================================================================
--- cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/dom4j/DOM4JProviderTest.java
(added)
+++ cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/dom4j/DOM4JProviderTest.java
Thu Jun 14 15:53:47 2012
@@ -0,0 +1,126 @@
+/**
+ * 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.dom4j;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.lang.annotation.Annotation;
+
+import javax.ws.rs.core.Application;
+import javax.ws.rs.core.MediaType;
+
+import org.apache.cxf.endpoint.Endpoint;
+import org.apache.cxf.jaxrs.impl.MetadataMap;
+import org.apache.cxf.jaxrs.impl.ProvidersImpl;
+import org.apache.cxf.jaxrs.provider.ProviderFactory;
+import org.apache.cxf.message.Exchange;
+import org.apache.cxf.message.ExchangeImpl;
+import org.apache.cxf.message.Message;
+import org.apache.cxf.message.MessageImpl;
+import org.easymock.EasyMock;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class DOM4JProviderTest extends Assert {
+
+    
+    @Test
+    public void testReadXML() throws Exception {
+        String str = readXML().asXML();
+        // starts with the xml PI
+        assertTrue(str.contains("<a/>") || str.contains("<a></a>"));
+    }
+    
+    private org.dom4j.Document readXML() throws Exception {
+        final String xml = "<a/>";
+        DOM4JProvider p = new DOM4JProvider();
+        p.setProviders(new ProvidersImpl(createMessage()));
+        org.dom4j.Document dom = p.readFrom(org.dom4j.Document.class, org.dom4j.Document.class,

+            new Annotation[] {}, MediaType.APPLICATION_XML_TYPE, new MetadataMap<String,
String>(),
+            new ByteArrayInputStream(xml.getBytes("UTF-8")));
+        return dom;
+    }
+    
+    @Test
+    public void testReadJSONConvertToXML() throws Exception {
+        final String xml = "{\"a\":{\"b\":2}}";
+        DOM4JProvider p = new DOM4JProvider();
+        p.setProviders(new ProvidersImpl(createMessage()));
+        org.dom4j.Document dom = p.readFrom(org.dom4j.Document.class, org.dom4j.Document.class,

+                   new Annotation[]{}, MediaType.APPLICATION_JSON_TYPE, new MetadataMap<String,
String>(),
+                   new ByteArrayInputStream(xml.getBytes("UTF-8")));
+        String str = dom.asXML();
+        // starts with the xml PI
+        assertTrue(str.contains("<a><b>2</b></a>"));
+    }
+
+    @Test
+    public void testWriteXML() throws Exception {
+        org.dom4j.Document dom = readXML();
+        DOM4JProvider p = new DOM4JProvider();
+        p.setProviders(new ProvidersImpl(createMessage()));
+        
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        p.writeTo(dom, org.dom4j.Document.class, org.dom4j.Document.class, 
+            new Annotation[]{}, MediaType.APPLICATION_XML_TYPE, new MetadataMap<String,
Object>(),
+            bos);
+        String str = bos.toString();
+        // starts with the xml PI
+        assertTrue(str.contains("<a/>") || str.contains("<a></a>"));
+    }
+    
+    @Test
+    public void testWriteJSON() throws Exception {
+        org.dom4j.Document dom = readXML();
+        DOM4JProvider p = new DOM4JProvider();
+        p.setProviders(new ProvidersImpl(createMessage()));
+        
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        p.writeTo(dom, org.dom4j.Document.class, org.dom4j.Document.class, 
+                   new Annotation[]{}, MediaType.APPLICATION_JSON_TYPE, new MetadataMap<String,
Object>(),
+                   bos);
+        String str = bos.toString();
+        // starts with the xml PI
+        assertEquals("{\"a\":\"\"}", str);
+    }
+    
+    private Message createMessage() {
+        ProviderFactory factory = ProviderFactory.getInstance();
+        Message m = new MessageImpl();
+        m.put("org.apache.cxf.http.case_insensitive_queries", false);
+        Exchange e = new ExchangeImpl();
+        m.setExchange(e);
+        e.setInMessage(m);
+        Endpoint endpoint = EasyMock.createMock(Endpoint.class);
+        endpoint.getEndpointInfo();
+        EasyMock.expectLastCall().andReturn(null).anyTimes();
+        endpoint.get(Application.class.getName());
+        EasyMock.expectLastCall().andReturn(null);
+        endpoint.size();
+        EasyMock.expectLastCall().andReturn(0).anyTimes();
+        endpoint.isEmpty();
+        EasyMock.expectLastCall().andReturn(true).anyTimes();
+        endpoint.get(ProviderFactory.class.getName());
+        EasyMock.expectLastCall().andReturn(factory).anyTimes();
+        EasyMock.replay(endpoint);
+        e.put(Endpoint.class, endpoint);
+        return m;
+    }
+}

Propchange: cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/dom4j/DOM4JProviderTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/dom4j/DOM4JProviderTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date



Mime
View raw message