cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r796741 - in /cxf/trunk/rt/frontend/jaxrs/src: main/java/org/apache/cxf/jaxrs/provider/ test/java/org/apache/cxf/jaxrs/provider/
Date Wed, 22 Jul 2009 14:29:22 GMT
Author: sergeyb
Date: Wed Jul 22 14:29:21 2009
New Revision: 796741

URL: http://svn.apache.org/viewvc?rev=796741&view=rev
Log:
JAXRS : updating JSONProvider to optionally ignore mixed content characters

Added:
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/book.xml   (with
props)
Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JSONProviderTest.java

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java?rev=796741&r1=796740&r2=796741&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
Wed Jul 22 14:29:21 2009
@@ -19,6 +19,8 @@
 
 package org.apache.cxf.jaxrs.provider;
 
+import java.io.PrintWriter;
+import java.io.StringWriter;
 import java.io.UnsupportedEncodingException;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Array;
@@ -155,7 +157,11 @@
     
     protected QName getJaxbQName(Class<?> cls, Type type, Object object, boolean pluralName)

         throws Exception {
-        //try the easy way first
+        
+        if (cls == JAXBElement.class) {
+            return object != null ? ((JAXBElement)object).getName() : null;
+        }
+        
         XmlRootElement root = cls.getAnnotation(XmlRootElement.class);
         QName qname = null;
         if (root != null) {
@@ -431,6 +437,9 @@
     }
     
     protected static void handleJAXBException(JAXBException e) {
+        StringWriter sw = new StringWriter();
+        e.printStackTrace(new PrintWriter(sw));
+        LOG.warning(sw.toString());
         StringBuilder sb = new StringBuilder();
         if (e.getMessage() != null) {
             sb.append(e.getMessage()).append(". ");
@@ -445,7 +454,6 @@
             ? e.getLinkedException() : e.getCause() != null ? e.getCause() : e;
         String message = new org.apache.cxf.common.i18n.Message("JAXB_EXCEPTION", 
                              BUNDLE, sb.toString()).toString();
-        LOG.warning(message);
         Response r = Response.status(Response.Status.INTERNAL_SERVER_ERROR)
             .type(MediaType.TEXT_PLAIN).entity(message).build();
         throw new WebApplicationException(t, r);

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java?rev=796741&r1=796740&r2=796741&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
Wed Jul 22 14:29:21 2009
@@ -74,7 +74,7 @@
     private Map<String, String> wrapperMap;
     private boolean dropRootElement;
     private boolean dropCollectionWrapperElement;
-    
+    private boolean ignoreMixedContent; 
     @Context
     public void setMessageContext(MessageContext mc) {
         super.setContext(mc);
@@ -88,6 +88,10 @@
         this.dropCollectionWrapperElement = drop;
     }
     
+    public void setIgnoreMixedContent(boolean ignore) {
+        this.ignoreMixedContent = ignore;
+    }
+    
     public void setSupportUnwrapped(boolean unwrap) {
         this.unwrapped = unwrap;
     }
@@ -293,6 +297,9 @@
         
         XMLStreamWriter writer = createWriter(actualObject, actualClass, genericType, enc,

                                               os, isCollection);
+        if (ignoreMixedContent) {
+            writer = new IgnoreMixedContentWriter(writer);
+        }
         ms.marshal(actualObject, writer);
         writer.close();
     }
@@ -373,5 +380,24 @@
         }
     }
     
-    
+    protected static class IgnoreMixedContentWriter extends DelegatingXMLStreamWriter {
+        boolean lastWriteChars;
+        
+        public IgnoreMixedContentWriter(XMLStreamWriter writer) {
+            super(writer);
+        }
+
+        public void writeCharacters(String text) throws XMLStreamException {
+            if (!lastWriteChars) {
+                super.writeCharacters(text);
+                lastWriteChars = true;
+            }
+        }
+
+        public void writeStartElement(String prefix, String localName, String namespaceURI)

+            throws XMLStreamException {
+            super.writeStartElement(prefix, localName, namespaceURI);
+            lastWriteChars = false;
+        }
+    }
 }

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JSONProviderTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JSONProviderTest.java?rev=796741&r1=796740&r2=796741&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JSONProviderTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JSONProviderTest.java
Wed Jul 22 14:29:21 2009
@@ -21,6 +21,7 @@
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Method;
 import java.util.ArrayList;
@@ -31,8 +32,16 @@
 
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.ext.MessageBodyReader;
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBElement;
+import javax.xml.bind.Unmarshaller;
+import javax.xml.bind.annotation.XmlAnyElement;
+import javax.xml.bind.annotation.XmlMixed;
+import javax.xml.bind.annotation.XmlRootElement;
+import javax.xml.transform.stream.StreamSource;
 
 import org.apache.cxf.jaxrs.impl.MetadataMap;
+import org.apache.cxf.jaxrs.resources.Book;
 import org.apache.cxf.jaxrs.resources.CollectionsResource;
 import org.apache.cxf.jaxrs.resources.ManyTags;
 import org.apache.cxf.jaxrs.resources.TagVO;
@@ -288,6 +297,31 @@
     }
     
     @Test
+    public void testWriteIgnoreMixedContent() throws Exception {
+        doTestMixedContent("{\"Book\":{\"name\":\"CXF\",\"id\":125}}", true);
+    }
+    
+    @Test
+    public void testWriteMixedContent() throws Exception {
+        doTestMixedContent("{\"Book\":{\"name\":\"CXF\",\"id\":125,\"$\":\"\\n     \\n\"}}",
false);
+    }
+    
+    private void doTestMixedContent(String data, boolean ignore) throws Exception {
+        InputStream is = getClass().getResourceAsStream("book.xml");
+        JAXBContext context = JAXBContext.newInstance(new Class[]{Books.class, Book.class});
+        Unmarshaller um = context.createUnmarshaller();
+        JAXBElement jaxbEl = um.unmarshal(new StreamSource(is), Books.class);
+        JSONProvider p = new JSONProvider();
+        p.setIgnoreMixedContent(ignore);
+        ByteArrayOutputStream os = new ByteArrayOutputStream();
+        
+        p.writeTo(jaxbEl, (Class)JAXBElement.class, JAXBElement.class, JAXBElement.class.getAnnotations(),

+                  MediaType.APPLICATION_JSON_TYPE, new MetadataMap<String, Object>(),
os);
+        String s = os.toString();
+        assertEquals(data, s);
+    }
+    
+    @Test
     public void testWriteToListWithManyValues() throws Exception {
         JSONProvider p = new JSONProvider();
         Tags tags = new Tags();
@@ -341,7 +375,6 @@
                   MediaType.APPLICATION_JSON_TYPE, new MetadataMap<String, Object>(),
os);
         
         String s = os.toString();
-        System.out.println(s);
         assertEquals(
             "{\"ManyTags\":{\"tags\":{\"list\":[{\"group\":\"b\",\"name\":\"a\"}]}}}",
             s);
@@ -354,4 +387,11 @@
     private TagVO2 createTag2(String name, String group) {
         return new TagVO2(name, group);
     }
+    
+    @XmlRootElement()
+    public static class Books {
+        @XmlMixed
+        @XmlAnyElement(lax = true)
+        protected List<Object> books;
+    }
 }

Added: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/book.xml
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/book.xml?rev=796741&view=auto
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/book.xml (added)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/book.xml Wed Jul
22 14:29:21 2009
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
+<Book>
+ 
+     <name>CXF</name>
+     <id>125</id>
+     
+</Book>

Propchange: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/book.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/book.xml
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/book.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml



Mime
View raw message