cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r796748 - in /cxf/branches/2.2.x-fixes: ./ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/
Date Wed, 22 Jul 2009 14:45:17 GMT
Author: sergeyb
Date: Wed Jul 22 14:45:16 2009
New Revision: 796748

URL: http://svn.apache.org/viewvc?rev=796748&view=rev
Log:
Merged revisions 796741 via svnmerge from 
https://svn.apache.org/repos/asf/cxf/trunk

........
  r796741 | sergeyb | 2009-07-22 15:29:21 +0100 (Wed, 22 Jul 2009) | 1 line
  
  JAXRS : updating JSONProvider to optionally ignore mixed content characters
........

Added:
    cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/book.xml
      - copied unchanged from r796741, cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/book.xml
Modified:
    cxf/branches/2.2.x-fixes/   (props changed)
    cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
    cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
    cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JSONProviderTest.java

Propchange: cxf/branches/2.2.x-fixes/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Wed Jul 22 14:45:16 2009
@@ -1 +1 @@
-/cxf/trunk:782728-782730,783097,783294,783396,784059,784181-784184,784893,784895,785279-785282,785468,785621,785624,785651,785734,785866,786142,786271-786272,786395,786512,786514,786582-786583,786638,786647,786850,787200,787269,787277-787279,787290-787291,787305,787323,787366,787849,788030,788060,788187,788444,788451,788703,788752,788774,788819-788820,789013,789371,789387,789420,789527-789530,789704-789705,789788,789811,789896-789901,790074,790094,790134,790188,790294,790553,790637-790644,790868,791301,791354,791538,791753,791947,792007,792096,792183,792261-792265,792271,792604,792683-792685,792975,792985,793059,793570,794297,794396,794680,794728,794771,794778-794780,794892,795044,795104,795160,795583,795907,796022-796023,796352,796593
+/cxf/trunk:782728-782730,783097,783294,783396,784059,784181-784184,784893,784895,785279-785282,785468,785621,785624,785651,785734,785866,786142,786271-786272,786395,786512,786514,786582-786583,786638,786647,786850,787200,787269,787277-787279,787290-787291,787305,787323,787366,787849,788030,788060,788187,788444,788451,788703,788752,788774,788819-788820,789013,789371,789387,789420,789527-789530,789704-789705,789788,789811,789896-789901,790074,790094,790134,790188,790294,790553,790637-790644,790868,791301,791354,791538,791753,791947,792007,792096,792183,792261-792265,792271,792604,792683-792685,792975,792985,793059,793570,794297,794396,794680,794728,794771,794778-794780,794892,795044,795104,795160,795583,795907,796022-796023,796352,796593,796741

Propchange: cxf/branches/2.2.x-fixes/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java?rev=796748&r1=796747&r2=796748&view=diff
==============================================================================
--- cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
(original)
+++ cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
Wed Jul 22 14:45:16 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/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java?rev=796748&r1=796747&r2=796748&view=diff
==============================================================================
--- cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
(original)
+++ cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
Wed Jul 22 14:45:16 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/branches/2.2.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JSONProviderTest.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JSONProviderTest.java?rev=796748&r1=796747&r2=796748&view=diff
==============================================================================
--- cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JSONProviderTest.java
(original)
+++ cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JSONProviderTest.java
Wed Jul 22 14:45:16 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;
+    }
 }



Mime
View raw message