cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1087072 - in /cxf/trunk/rt: core/src/main/java/org/apache/cxf/databinding/source/ frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/xml/ frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ frontend/jaxrs/src/test/java/org/apach...
Date Wed, 30 Mar 2011 21:07:00 GMT
Author: sergeyb
Date: Wed Mar 30 21:06:59 2011
New Revision: 1087072

URL: http://svn.apache.org/viewvc?rev=1087072&view=rev
Log:
Updating JAX-RS SourceProvider to work with readers available on the message

Modified:
    cxf/trunk/rt/core/src/main/java/org/apache/cxf/databinding/source/XMLStreamDataReader.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/xml/XMLSource.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/SourceProvider.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/ext/xml/XMLSourceTest.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/SourceProviderTest.java

Modified: cxf/trunk/rt/core/src/main/java/org/apache/cxf/databinding/source/XMLStreamDataReader.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/core/src/main/java/org/apache/cxf/databinding/source/XMLStreamDataReader.java?rev=1087072&r1=1087071&r2=1087072&view=diff
==============================================================================
--- cxf/trunk/rt/core/src/main/java/org/apache/cxf/databinding/source/XMLStreamDataReader.java
(original)
+++ cxf/trunk/rt/core/src/main/java/org/apache/cxf/databinding/source/XMLStreamDataReader.java
Wed Mar 30 21:06:59 2011
@@ -198,9 +198,7 @@ public class XMLStreamDataReader impleme
         
         CachedOutputStream out = new CachedOutputStream();
         try {
-            XMLStreamWriter xsw = StaxUtils.createXMLStreamWriter(out);
-            StaxUtils.copy(input, xsw);
-            xsw.close();
+            StaxUtils.copy(input, out);
             return out.getInputStream();
         } finally {
             out.close();

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/xml/XMLSource.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/xml/XMLSource.java?rev=1087072&r1=1087071&r2=1087072&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/xml/XMLSource.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/xml/XMLSource.java
Wed Mar 30 21:06:59 2011
@@ -33,6 +33,7 @@ import java.util.logging.Logger;
 
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.Unmarshaller;
+import javax.xml.bind.annotation.XmlRootElement;
 import javax.xml.namespace.NamespaceContext;
 import javax.xml.namespace.QName;
 import javax.xml.transform.Source;
@@ -345,13 +346,18 @@ public class XMLSource {
     
     private <T> T readFromSource(Source s, Class<T> cls) {
         try {
+            
             JAXBElementProvider provider = new JAXBElementProvider();
             JAXBContext c = provider.getPackageContext(cls);
             if (c == null) {
                 c = provider.getClassContext(cls);
             }
             Unmarshaller u = c.createUnmarshaller();
-            return cls.cast(u.unmarshal(s));
+            if (cls.getAnnotation(XmlRootElement.class) != null) {
+                return cls.cast(u.unmarshal(s));
+            } else {
+                return u.unmarshal(s, cls).getValue();
+            }
         } catch (Exception ex) {
             throw new RuntimeException(ex);
         }

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java?rev=1087072&r1=1087071&r2=1087072&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
Wed Mar 30 21:06:59 2011
@@ -52,6 +52,7 @@ import javax.xml.stream.XMLOutputFactory
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamReader;
 import javax.xml.stream.XMLStreamWriter;
+import javax.xml.transform.Source;
 
 import org.apache.cxf.helpers.CastUtils;
 import org.apache.cxf.jaxb.NamespaceMapper;
@@ -100,7 +101,8 @@ public class JAXBElementProvider extends
     
     @Override
     protected boolean canBeReadAsJaxbElement(Class<?> type) {
-        return type != XMLSource.class && super.canBeReadAsJaxbElement(type);
+        return super.canBeReadAsJaxbElement(type) 
+            && type != XMLSource.class && !Source.class.isAssignableFrom(type);
     }
     
     @Context

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/SourceProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/SourceProvider.java?rev=1087072&r1=1087071&r2=1087072&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/SourceProvider.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/SourceProvider.java
Wed Mar 30 21:06:59 2011
@@ -28,6 +28,7 @@ import java.lang.reflect.Type;
 import javax.ws.rs.Consumes;
 import javax.ws.rs.Produces;
 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;
@@ -43,10 +44,10 @@ import javax.xml.transform.stream.Stream
 
 import org.w3c.dom.Document;
 
+import org.apache.cxf.io.CachedOutputStream;
+import org.apache.cxf.jaxrs.ext.MessageContext;
 import org.apache.cxf.jaxrs.ext.xml.XMLSource;
 import org.apache.cxf.jaxrs.utils.HttpUtils;
-import org.apache.cxf.message.Message;
-import org.apache.cxf.phase.PhaseInterceptorChain;
 import org.apache.cxf.staxutils.StaxSource;
 import org.apache.cxf.staxutils.StaxUtils;
 
@@ -57,6 +58,9 @@ public class SourceProvider extends Abst
     MessageBodyReader<Object>, MessageBodyWriter<Source> {
 
     private static final String PREFERRED_FORMAT = "source-preferred-format";
+    @Context
+    private MessageContext context;
+    
     
     public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations,
MediaType mt) {
         return Source.class.isAssignableFrom(type);
@@ -83,7 +87,7 @@ public class SourceProvider extends Abst
         if (DOMSource.class.isAssignableFrom(theSource) || Document.class.isAssignableFrom(theSource))
{
             
             boolean docRequired = Document.class.isAssignableFrom(theSource);
-            XMLStreamReader reader = StaxUtils.createXMLStreamReader(is);
+            XMLStreamReader reader = getReader(is);
             try {
                 Document doc = StaxUtils.read(reader);
                 return docRequired ? doc : new DOMSource(doc);
@@ -100,17 +104,50 @@ public class SourceProvider extends Abst
             }
         } else if (SAXSource.class.isAssignableFrom(theSource)
                   || StaxSource.class.isAssignableFrom(theSource)) {
-            return new StaxSource(StaxUtils.createXMLStreamReader(is));
+            return new StaxSource(getReader(is));
         } else if (StreamSource.class.isAssignableFrom(theSource)
                    || Source.class.isAssignableFrom(theSource)) {
-            return new StreamSource(is);
+            return new StreamSource(getRealStream(is));
         } else if (XMLSource.class.isAssignableFrom(theSource)) {
-            return new XMLSource(is);
+            return new XMLSource(getRealStream(is));
         }
         
         throw new IOException("Unrecognized source");
     }
 
+    protected XMLStreamReader getReader(InputStream is) {
+        XMLStreamReader reader = getReaderFromMessage();
+        return reader == null ? StaxUtils.createXMLStreamReader(is) : reader;
+    }
+    
+    protected InputStream getRealStream(InputStream is) throws IOException {
+        XMLStreamReader reader = getReaderFromMessage();
+        return reader == null ? is : getStreamFromReader(reader);
+    }
+    
+    private InputStream getStreamFromReader(XMLStreamReader input) 
+        throws IOException {
+        
+        CachedOutputStream out = new CachedOutputStream();
+        try {
+            StaxUtils.copy(input, out);
+            return out.getInputStream();
+        } catch (XMLStreamException ex) {
+            throw new IOException(ex);
+        } finally {
+            out.close();
+        }
+    }
+    
+    protected XMLStreamReader getReaderFromMessage() {
+        MessageContext mc = getContext();
+        if (mc != null) {
+            return (XMLStreamReader)mc.getContent(XMLStreamReader.class);
+        } else {
+            return null;
+        }
+    }
+    
     public void writeTo(Source source, Class<?> clazz, Type genericType, Annotation[]
annotations,  
         MediaType mt, MultivaluedMap<String, Object> headers, OutputStream os)
         throws IOException {
@@ -143,16 +180,15 @@ public class SourceProvider extends Abst
     }
     
     protected String getPreferredSource() {
-        Message m = getCurrentMessage();
-        if (m != null) {
-            return (String)m.getContextualProperty(PREFERRED_FORMAT);
+        MessageContext mc = getContext();
+        if (mc != null) {
+            return (String)mc.getContextualProperty(PREFERRED_FORMAT);
         } else {
             return "sax";
         }
     }
     
-    protected Message getCurrentMessage() {
-        return PhaseInterceptorChain.getCurrentMessage();
+    protected MessageContext getContext() {
+        return context;    
     }
-    
 }

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/ext/xml/XMLSourceTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/ext/xml/XMLSourceTest.java?rev=1087072&r1=1087071&r2=1087072&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/ext/xml/XMLSourceTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/ext/xml/XMLSourceTest.java
Wed Mar 30 21:06:59 2011
@@ -27,6 +27,7 @@ import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 
+import javax.xml.bind.annotation.XmlAttribute;
 import javax.xml.bind.annotation.XmlRootElement;
 
 import org.apache.cxf.common.xmlschema.XmlSchemaConstants;
@@ -103,6 +104,15 @@ public class XMLSourceTest extends Asser
     }
     
     @Test
+    public void testGetNodeAsJaxbElement() {
+        InputStream is = new ByteArrayInputStream("<foo><bar name=\"foo\"/></foo>".getBytes());
+        XMLSource xp = new XMLSource(is);
+        Bar3 bar = xp.getNode("/foo/bar", Bar3.class);
+        assertNotNull(bar);
+        assertEquals("foo", bar.getName());
+    }
+    
+    @Test
     public void testGetNodeNamespace() {
         String data = "<x:foo xmlns:x=\"http://baz\"><x:bar/></x:foo>";

         InputStream is = new ByteArrayInputStream(data.getBytes());
@@ -219,4 +229,15 @@ public class XMLSourceTest extends Asser
     private static class Bar2 {
         
     }
+    
+    private static class Bar3 {
+        
+        @XmlAttribute
+        private String name;
+
+        public String getName() {
+            return name;
+        }
+        
+    }
 }

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/SourceProviderTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/SourceProviderTest.java?rev=1087072&r1=1087071&r2=1087072&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/SourceProviderTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/SourceProviderTest.java
Wed Mar 30 21:06:59 2011
@@ -21,22 +21,31 @@ package org.apache.cxf.jaxrs.provider;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
+import java.util.Collections;
 
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.ext.MessageBodyReader;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.stream.XMLStreamReader;
 import javax.xml.transform.Source;
+import javax.xml.transform.TransformerFactory;
 import javax.xml.transform.dom.DOMSource;
 import javax.xml.transform.sax.SAXSource;
+import javax.xml.transform.stream.StreamResult;
 import javax.xml.transform.stream.StreamSource;
 
 import org.w3c.dom.Document;
 
+import org.apache.cxf.jaxrs.ext.MessageContext;
+import org.apache.cxf.jaxrs.ext.MessageContextImpl;
 import org.apache.cxf.jaxrs.impl.MetadataMap;
 import org.apache.cxf.message.Message;
 import org.apache.cxf.message.MessageImpl;
 import org.apache.cxf.staxutils.StaxSource;
+import org.apache.cxf.staxutils.StaxUtils;
+import org.apache.cxf.staxutils.transform.InTransformReader;
 
 import org.junit.Assert;
 import org.junit.Test;
@@ -71,9 +80,31 @@ public class SourceProviderTest extends 
         assertTrue(Document.class.isAssignableFrom(verifyRead(p, Document.class).getClass()));
     }
     
+    @SuppressWarnings("unchecked")
+    @Test
+    public void testReadFromStreamReader() throws Exception {
+        TestSourceProvider p = new TestSourceProvider();
+        
+        InputStream is = new ByteArrayInputStream("<test xmlns=\"http://bar\"/>".getBytes());
+        XMLStreamReader reader = StaxUtils.createXMLStreamReader(is);
+        reader = new InTransformReader(reader, 
+                                       Collections.singletonMap("{http://bar}test", "test2"),
+                                       null, false);
+        
+        p.getMessage().setContent(XMLStreamReader.class, reader);
+        
+        Source source = (Source)p.readFrom((Class)Source.class,
+                   null, null, null, null, is);
+        ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
+        TransformerFactory.newInstance().newTransformer()
+            .transform(source, new StreamResult(bos));
+        assertTrue(bos.toString().contains("test2"));
+    }
+    
     @Test
     public void testReadFromWithPreferredFormat() throws Exception {
-        SourceProvider p = new TestSourceProvider("sax");
+        TestSourceProvider p = new TestSourceProvider();
+        p.getMessage().put("source-preferred-format", "sax");        
         assertSame(StaxSource.class, verifyRead(p, Source.class).getClass());
     }
     
@@ -92,8 +123,8 @@ public class SourceProviderTest extends 
     }
     
     @SuppressWarnings("unchecked")
-    private <T> Object verifyRead(MessageBodyReader p, Class<T> type) throws
Exception {
-        return p.readFrom(type,
+    private <T> T verifyRead(MessageBodyReader p, Class<T> type) throws Exception
{
+        return (T)p.readFrom(type,
                    null, null, null, null,
                    new ByteArrayInputStream("<test/>".getBytes()));
     }
@@ -107,22 +138,17 @@ public class SourceProviderTest extends 
     
     private static class TestSourceProvider extends SourceProvider {
         
-        private String format;
+        private Message m = new MessageImpl();
         
         public TestSourceProvider() {
-            
         }
         
-        public TestSourceProvider(String format) {
-            this.format = format;    
-        }
-
-        protected Message getCurrentMessage() {
-            Message m = new MessageImpl();
-            if (format != null) {
-                m.put("source-preferred-format", format);
-            }
+        public Message getMessage() {
             return m;
+        }
+        
+        protected MessageContext getContext() {
+            return new MessageContextImpl(m);
         };
     }
 }



Mime
View raw message