cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r705640 - in /cxf/trunk/rt/frontend/jaxrs/src: main/java/org/apache/cxf/jaxrs/provider/ test/java/org/apache/cxf/jaxrs/provider/
Date Fri, 17 Oct 2008 15:16:59 GMT
Author: sergeyb
Date: Fri Oct 17 08:16:58 2008
New Revision: 705640

URL: http://svn.apache.org/viewvc?rev=705640&view=rev
Log:
JAX-RS : setting Marshaller properties on JAXB provider

Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/PrimitiveTextProviderTest.java

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=705640&r1=705639&r2=705640&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
Fri Oct 17 08:16:58 2008
@@ -25,7 +25,9 @@
 import java.io.OutputStream;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Type;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 import javax.ws.rs.WebApplicationException;
 import javax.ws.rs.core.MediaType;
@@ -38,16 +40,20 @@
 import javax.xml.transform.stream.StreamSource;
 
 @Provider
-public final class JAXBElementProvider extends AbstractJAXBProvider  {
+public class JAXBElementProvider extends AbstractJAXBProvider  {
     
-    private String jaxbSchemaLocation;
+    private Map<String, Object> mProperties = new HashMap<String, Object>();
     
     public void setSchemas(List<String> locations) {
         super.setSchemas(locations);
     }
     
+    public void setMarshallerProperties(Map<String, Object> marshallProperties) {
+        mProperties = marshallProperties;
+    }
+    
     public void setSchemaLocation(String schemaLocation) {
-        jaxbSchemaLocation = schemaLocation;
+        mProperties.put(Marshaller.JAXB_SCHEMA_LOCATION, schemaLocation);
     }
     
     public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations,
MediaType m, 
@@ -79,8 +85,8 @@
                 genericType = actualClass;
             }
             Marshaller ms = createMarshaller(actualObject, actualClass, genericType, m);
-            if (jaxbSchemaLocation != null) {
-                ms.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, jaxbSchemaLocation);
+            for (Map.Entry<String, Object> entry : mProperties.entrySet()) {
+                ms.setProperty(entry.getKey(), entry.getValue());
             }
             ms.marshal(actualObject, os);
             

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java?rev=705640&r1=705639&r2=705640&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java
Fri Oct 17 08:16:58 2008
@@ -19,15 +19,35 @@
 
 package org.apache.cxf.jaxrs.provider;
 
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.OutputStream;
+import java.io.Writer;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
+import javax.ws.rs.core.MediaType;
+import javax.xml.bind.JAXBException;
+import javax.xml.bind.Marshaller;
+import javax.xml.bind.PropertyException;
+import javax.xml.bind.ValidationEventHandler;
+import javax.xml.bind.annotation.adapters.XmlAdapter;
+import javax.xml.bind.attachment.AttachmentMarshaller;
+import javax.xml.stream.XMLEventWriter;
+import javax.xml.stream.XMLStreamWriter;
+import javax.xml.transform.Result;
 import javax.xml.validation.Schema;
 
+import org.w3c.dom.Node;
+import org.xml.sax.ContentHandler;
+
 import org.junit.Assert;
 import org.junit.Test;
 
-
 public class JAXBElementProviderTest extends Assert {
 
     @Test
@@ -51,4 +71,150 @@
         assertNotNull("schema can not be read from disk", s);
     }
     
+    @Test
+    public void testSetMarshallProperties() throws Exception {
+        
+        Map<String, Object> props = new HashMap<String, Object>();
+        props.put(Marshaller.JAXB_FORMATTED_OUTPUT, true);
+        props.put(Marshaller.JAXB_SCHEMA_LOCATION, "foo.xsd");
+        
+        final TestMarshaller m = new TestMarshaller();
+        
+        JAXBElementProvider provider = new JAXBElementProvider() {
+            @Override
+            protected Marshaller createMarshaller(Object obj, Class<?> cls, Type genericType,
MediaType mt)
+                throws JAXBException {
+                return m;    
+            }
+        };
+        
+        provider.setMarshallerProperties(props);
+        provider.writeTo("123", String.class, (Type)String.class, new Annotation[]{}, null,
null,
+                         new ByteArrayOutputStream());
+        
+        assertEquals("Marshall properties have not been set", props, m.getProperties());
+    }
+    
+    private static class TestMarshaller implements Marshaller {
+
+        private Map<String, Object> props = new HashMap<String, Object>();
+        
+        public TestMarshaller() {
+            
+        }
+        
+        public Map getProperties() {
+            return props;
+        }
+        
+        public <A extends XmlAdapter> A getAdapter(Class<A> type) {
+            // TODO Auto-generated method stub
+            return null;
+        }
+
+        public AttachmentMarshaller getAttachmentMarshaller() {
+            // TODO Auto-generated method stub
+            return null;
+        }
+
+        public ValidationEventHandler getEventHandler() throws JAXBException {
+            // TODO Auto-generated method stub
+            return null;
+        }
+
+        public Listener getListener() {
+            // TODO Auto-generated method stub
+            return null;
+        }
+
+        public Node getNode(Object contentTree) throws JAXBException {
+            // TODO Auto-generated method stub
+            return null;
+        }
+
+        public Object getProperty(String name) throws PropertyException {
+            // TODO Auto-generated method stub
+            return null;
+        }
+
+        public Schema getSchema() {
+            // TODO Auto-generated method stub
+            return null;
+        }
+
+        public void marshal(Object jaxbElement, Result result) throws JAXBException {
+            // TODO Auto-generated method stub
+            
+        }
+
+        public void marshal(Object jaxbElement, OutputStream os) throws JAXBException {
+        }
+
+        public void marshal(Object jaxbElement, File output) throws JAXBException {
+            // TODO Auto-generated method stub
+            
+        }
+
+        public void marshal(Object jaxbElement, Writer writer) throws JAXBException {
+            // TODO Auto-generated method stub
+            
+        }
+
+        public void marshal(Object jaxbElement, ContentHandler handler) throws JAXBException
{
+            // TODO Auto-generated method stub
+            
+        }
+
+        public void marshal(Object jaxbElement, Node node) throws JAXBException {
+            // TODO Auto-generated method stub
+            
+        }
+
+        public void marshal(Object jaxbElement, XMLStreamWriter writer) throws JAXBException
{
+            // TODO Auto-generated method stub
+            
+        }
+
+        public void marshal(Object jaxbElement, XMLEventWriter writer) throws JAXBException
{
+            // TODO Auto-generated method stub
+            
+        }
+
+        public void setAdapter(XmlAdapter adapter) {
+            // TODO Auto-generated method stub
+            
+        }
+
+        public <A extends XmlAdapter> void setAdapter(Class<A> type, A adapter)
{
+            // TODO Auto-generated method stub
+            
+        }
+
+        public void setAttachmentMarshaller(AttachmentMarshaller am) {
+            // TODO Auto-generated method stub
+            
+        }
+
+        public void setEventHandler(ValidationEventHandler handler) throws JAXBException
{
+            // TODO Auto-generated method stub
+            
+        }
+
+        public void setListener(Listener listener) {
+            // TODO Auto-generated method stub
+            
+        }
+
+        public void setProperty(String name, Object value) throws PropertyException {
+            props.put(name, value);
+            
+        }
+
+        public void setSchema(Schema schema) {
+            // TODO Auto-generated method stub
+            
+        }
+        
+    }
+    
 }

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/PrimitiveTextProviderTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/PrimitiveTextProviderTest.java?rev=705640&r1=705639&r2=705640&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/PrimitiveTextProviderTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/PrimitiveTextProviderTest.java
Fri Oct 17 08:16:58 2008
@@ -54,13 +54,13 @@
     public void testReadByte() throws Exception {
         MessageBodyReader p = new PrimitiveTextProvider();
         
-        byte valueRead = (Byte)p.readFrom((Class)byte.class, 
+        Byte valueRead = (Byte)p.readFrom(byte.class, 
                                           null, 
                                           null, 
                                           null, 
                                           null, 
                                           new ByteArrayInputStream("1".getBytes()));
-        assertEquals(1, valueRead);
+        assertEquals(1, valueRead.byteValue());
         
     }
     
@@ -69,7 +69,7 @@
     public void testReadBoolean() throws Exception {
         MessageBodyReader p = new PrimitiveTextProvider();
         
-        boolean valueRead = (Boolean)p.readFrom((Class)boolean.class, 
+        boolean valueRead = (Boolean)p.readFrom(boolean.class, 
                                           null, 
                                           null, 
                                           null, 



Mime
View raw message