cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject cxf git commit: [CXF-6565] Updating DOM4JProvider to support suppressing XML declarations and, optionally, use StaxUtils to write XML
Date Thu, 27 Aug 2015 11:40:19 GMT
Repository: cxf
Updated Branches:
  refs/heads/master 1df8753ff -> 3dca45675


[CXF-6565] Updating DOM4JProvider to support suppressing XML declarations and, optionally,
use StaxUtils to write XML


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/3dca4567
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/3dca4567
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/3dca4567

Branch: refs/heads/master
Commit: 3dca456756b176e072dd5bf37a30887600bf3aca
Parents: 1df8753
Author: Sergey Beryozkin <sberyozkin@gmail.com>
Authored: Thu Aug 27 12:39:49 2015 +0100
Committer: Sergey Beryozkin <sberyozkin@gmail.com>
Committed: Thu Aug 27 12:39:49 2015 +0100

----------------------------------------------------------------------
 .../cxf/jaxrs/provider/dom4j/DOM4JProvider.java | 29 +++++++++-
 .../jaxrs/provider/dom4j/DOM4JProviderTest.java | 60 ++++++++++++++++----
 2 files changed, 75 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/3dca4567/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/dom4j/DOM4JProvider.java
----------------------------------------------------------------------
diff --git a/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/dom4j/DOM4JProvider.java
b/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/dom4j/DOM4JProvider.java
index f79af23..1a797be 100644
--- a/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/dom4j/DOM4JProvider.java
+++ b/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/dom4j/DOM4JProvider.java
@@ -37,18 +37,25 @@ import javax.ws.rs.ext.Providers;
 
 import org.apache.cxf.jaxrs.provider.AbstractConfigurableProvider;
 import org.apache.cxf.jaxrs.utils.ExceptionUtils;
+import org.apache.cxf.jaxrs.utils.JAXRSUtils;
+import org.apache.cxf.message.Message;
+import org.apache.cxf.message.MessageUtils;
 import org.apache.cxf.staxutils.StaxUtils;
+import org.dom4j.io.OutputFormat;
+import org.dom4j.io.XMLWriter;
 
 @Produces({"application/xml", "application/*+xml", "text/xml", "application/json", "application/*+json"
})
 @Consumes({"application/xml", "application/*+xml", "text/xml", "application/json", "application/*+json"
})
 public class DOM4JProvider extends AbstractConfigurableProvider 
     implements MessageBodyReader<org.dom4j.Document>, MessageBodyWriter<org.dom4j.Document>
{
-
+    public static final String SUPPRESS_XML_DECLARATION = "suppress.xml.declaration";
     private static final Class<org.w3c.dom.Document> DOM_DOC_CLS = 
         org.w3c.dom.Document.class;
 
     private Providers providers;
 
+    private boolean convertAlways;
+
     @Context
     public void setProviders(Providers providers) {
         this.providers = providers;
@@ -87,8 +94,16 @@ public class DOM4JProvider extends AbstractConfigurableProvider
                         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);
+        if (!convertAlways && mt.getSubtype().contains("xml")) {
+            
+            XMLWriter writer;
+            if (MessageUtils.getContextualBoolean(getCurrentMessage(), SUPPRESS_XML_DECLARATION,
false)) {
+                OutputFormat format = new org.dom4j.io.OutputFormat();
+                format.setSuppressDeclaration(true);
+                writer = new org.dom4j.io.XMLWriter(os, format);
+            } else {
+                writer = new org.dom4j.io.XMLWriter(os);
+            }
             writer.write(doc);
             writer.flush();
         } else {
@@ -103,6 +118,10 @@ public class DOM4JProvider extends AbstractConfigurableProvider
         }
     }
 
+    protected Message getCurrentMessage() {
+        return JAXRSUtils.getCurrentMessage();
+    }
+
     private org.w3c.dom.Document convertToDOM(org.dom4j.Document doc) {
         String xml = doc.asXML();
         try {
@@ -111,4 +130,8 @@ public class DOM4JProvider extends AbstractConfigurableProvider
             throw ExceptionUtils.toInternalServerErrorException(ex, null);
         }
     }
+
+    public void convertToDOMAlways(boolean convert) {
+        convertAlways = convert;
+    }
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/3dca4567/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/dom4j/DOM4JProviderTest.java
----------------------------------------------------------------------
diff --git a/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/dom4j/DOM4JProviderTest.java
b/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/dom4j/DOM4JProviderTest.java
index c6553ea..f4fa478 100644
--- a/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/dom4j/DOM4JProviderTest.java
+++ b/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/dom4j/DOM4JProviderTest.java
@@ -24,6 +24,7 @@ import java.lang.annotation.Annotation;
 
 import javax.ws.rs.core.Application;
 import javax.ws.rs.core.MediaType;
+import javax.ws.rs.ext.Providers;
 
 import org.apache.cxf.endpoint.Endpoint;
 import org.apache.cxf.jaxrs.impl.MetadataMap;
@@ -54,7 +55,7 @@ public class DOM4JProviderTest extends Assert {
     }
     private org.dom4j.Document readXML(MediaType ct, final String xml) throws Exception {
         DOM4JProvider p = new DOM4JProvider();
-        p.setProviders(new ProvidersImpl(createMessage()));
+        p.setProviders(new ProvidersImpl(createMessage(false)));
         org.dom4j.Document dom = p.readFrom(org.dom4j.Document.class, org.dom4j.Document.class,

             new Annotation[] {}, ct, new MetadataMap<String, String>(),
             new ByteArrayInputStream(xml.getBytes("UTF-8")));
@@ -65,7 +66,7 @@ public class DOM4JProviderTest extends Assert {
     public void testReadJSONConvertToXML() throws Exception {
         final String xml = "{\"a\":{\"b\":2}}";
         DOM4JProvider p = new DOM4JProvider();
-        p.setProviders(new ProvidersImpl(createMessage()));
+        p.setProviders(new ProvidersImpl(createMessage(false)));
         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")));
@@ -76,24 +77,58 @@ public class DOM4JProviderTest extends Assert {
 
     @Test
     public void testWriteXML() throws Exception {
-        doTestWriteXML(MediaType.APPLICATION_XML_TYPE);
+        doTestWriteXML(MediaType.APPLICATION_XML_TYPE, false);
     }
     
     @Test
     public void testWriteXMLCustomCt() throws Exception {
-        doTestWriteXML(MediaType.valueOf("application/custom+xml"));
+        doTestWriteXML(MediaType.valueOf("application/custom+xml"), false);
     }
     
-    private void doTestWriteXML(MediaType ct) throws Exception {
+    @Test
+    public void testWriteXMLAsDOMW3C() throws Exception {
+        doTestWriteXML(MediaType.APPLICATION_XML_TYPE, true);
+    }
+    
+    @Test
+    public void testWriteXMLSuppressDeclaration() throws Exception {
+        org.dom4j.Document dom = readXML(MediaType.APPLICATION_XML_TYPE, "<a/>");
+        final Message message = createMessage(true);
+        Providers providers = new ProvidersImpl(message);
+        DOM4JProvider p = new DOM4JProvider() {
+            protected Message getCurrentMessage() {
+                return message;
+            }
+        };
+        p.setProviders(providers);
+        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();
+        assertFalse(str.startsWith("<?xml"));
+        assertTrue(str.contains("<a/>") || str.contains("<a></a>"));
+    }
+    
+    private void doTestWriteXML(MediaType ct, boolean convert) throws Exception {
         org.dom4j.Document dom = readXML(ct, "<a/>");
-        DOM4JProvider p = new DOM4JProvider();
-        p.setProviders(new ProvidersImpl(createMessage()));
-        
+        final Message message = createMessage(false);
+        Providers providers = new ProvidersImpl(message);
+        DOM4JProvider p = new DOM4JProvider() {
+            protected Message getCurrentMessage() {
+                return message;
+            }
+        };
+        p.setProviders(providers);
+        p.convertToDOMAlways(convert);
         ByteArrayOutputStream bos = new ByteArrayOutputStream();
         p.writeTo(dom, org.dom4j.Document.class, org.dom4j.Document.class, 
             new Annotation[]{}, ct, new MetadataMap<String, Object>(), bos);
         String str = bos.toString();
-        // starts with the xml PI
+        if (convert) {
+            assertFalse(str.startsWith("<?xml"));
+        } else {
+            assertTrue(str.startsWith("<?xml"));
+        }
         assertTrue(str.contains("<a/>") || str.contains("<a></a>"));
     }
     
@@ -101,7 +136,7 @@ public class DOM4JProviderTest extends Assert {
     public void testWriteJSON() throws Exception {
         org.dom4j.Document dom = readXML();
         DOM4JProvider p = new DOM4JProvider();
-        p.setProviders(new ProvidersImpl(createMessage()));
+        p.setProviders(new ProvidersImpl(createMessage(false)));
         
         ByteArrayOutputStream bos = new ByteArrayOutputStream();
         p.writeTo(dom, org.dom4j.Document.class, org.dom4j.Document.class, 
@@ -147,11 +182,12 @@ public class DOM4JProviderTest extends Assert {
         assertEquals("[{\"a\":1}]", str);
     }
     
-    private Message createMessage() {
+    private Message createMessage(boolean suppress) {
         ProviderFactory factory = ServerProviderFactory.getInstance();
         Message m = new MessageImpl();
         m.put("org.apache.cxf.http.case_insensitive_queries", false);
         Exchange e = new ExchangeImpl();
+        e.put(DOM4JProvider.SUPPRESS_XML_DECLARATION, suppress);
         m.setExchange(e);
         e.setInMessage(m);
         Endpoint endpoint = EasyMock.createMock(Endpoint.class);
@@ -199,4 +235,6 @@ public class DOM4JProviderTest extends Assert {
         e.put(Endpoint.class, endpoint);
         return m;
     }
+            
+    
 }


Mime
View raw message