cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1335400 - in /cxf/trunk/rt: frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/json/ rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/json/
Date Tue, 08 May 2012 10:00:23 GMT
Author: sergeyb
Date: Tue May  8 10:00:22 2012
New Revision: 1335400

URL: http://svn.apache.org/viewvc?rev=1335400&view=rev
Log:
[CXF-4296] Updating JSONProvider to use a namespaceMap for setting a Marshaller namespace
mapper

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/JAXBElementProvider.java
    cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/json/JSONProvider.java
    cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/json/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=1335400&r1=1335399&r2=1335400&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
Tue May  8 10:00:22 2012
@@ -48,6 +48,7 @@ import javax.xml.bind.JAXBContext;
 import javax.xml.bind.JAXBElement;
 import javax.xml.bind.JAXBException;
 import javax.xml.bind.Marshaller;
+import javax.xml.bind.PropertyException;
 import javax.xml.bind.Unmarshaller;
 import javax.xml.bind.ValidationEventHandler;
 import javax.xml.bind.annotation.XmlAnyElement;
@@ -64,6 +65,7 @@ import org.xml.sax.helpers.DefaultHandle
 
 import org.apache.cxf.common.i18n.BundleUtils;
 import org.apache.cxf.common.jaxb.JAXBUtils;
+import org.apache.cxf.common.jaxb.NamespaceMapper;
 import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.common.util.PackageUtils;
 import org.apache.cxf.jaxrs.ext.MessageContext;
@@ -85,9 +87,12 @@ public abstract class AbstractJAXBProvid
     protected static final ResourceBundle BUNDLE = BundleUtils.getBundle(AbstractJAXBProvider.class);
 
     protected static final Logger LOG = LogUtils.getL7dLogger(AbstractJAXBProvider.class);
+    protected static final String NS_MAPPER_PROPERTY = "com.sun.xml.bind.namespacePrefixMapper";
+    protected static final String NS_MAPPER_PROPERTY_INT = "com.sun.xml.internal.bind.namespacePrefixMapper";
     private static final String JAXB_DEFAULT_NAMESPACE = "##default";
     private static final String JAXB_DEFAULT_NAME = "##default";
     
+    
     protected Set<Class<?>> collectionContextClasses = new HashSet<Class<?>>();
     
     protected Map<String, String> jaxbElementClassMap = Collections.emptyMap();
@@ -124,6 +129,21 @@ public abstract class AbstractJAXBProvid
     private Marshaller.Listener marshallerListener;
     private DocumentDepthProperties depthProperties;
     
+    protected static void setNamespaceMapper(Marshaller ms, Map<String, String> map)
throws Exception {
+        NamespaceMapper nsMapper = new NamespaceMapper(map);
+        setMarshallerProp(ms, nsMapper, NS_MAPPER_PROPERTY, NS_MAPPER_PROPERTY_INT);
+    }
+    
+    protected static void setMarshallerProp(Marshaller ms, Object value, 
+                                          String name1, String name2) throws Exception {
+        try {
+            ms.setProperty(name1, value);
+        } catch (PropertyException ex) {
+            ms.setProperty(name2, value);
+        }
+        
+    }
+    
     public void setValidationHandler(ValidationEventHandler handler) {
         eventHandler = handler;
     }

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=1335400&r1=1335399&r2=1335400&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
Tue May  8 10:00:22 2012
@@ -46,7 +46,6 @@ import javax.ws.rs.ext.Provider;
 import javax.xml.bind.JAXBElement;
 import javax.xml.bind.JAXBException;
 import javax.xml.bind.Marshaller;
-import javax.xml.bind.PropertyException;
 import javax.xml.bind.Unmarshaller;
 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
 import javax.xml.namespace.QName;
@@ -57,7 +56,6 @@ import javax.xml.stream.XMLStreamReader;
 import javax.xml.stream.XMLStreamWriter;
 import javax.xml.transform.Source;
 
-import org.apache.cxf.common.jaxb.NamespaceMapper;
 import org.apache.cxf.helpers.CastUtils;
 import org.apache.cxf.jaxrs.ext.MessageContext;
 import org.apache.cxf.jaxrs.ext.Nullable;
@@ -80,8 +78,6 @@ import org.apache.cxf.staxutils.transfor
 @Provider
 public class JAXBElementProvider<T> extends AbstractJAXBProvider<T>  {
     private static final String XML_PI_START = "<?xml version=\"1.0\" encoding=\"";
-    private static final String NS_MAPPER_PROPERTY = "com.sun.xml.bind.namespacePrefixMapper";
-    private static final String NS_MAPPER_PROPERTY_INT = "com.sun.xml.internal.bind.namespacePrefixMapper";
     private static final String XML_PI_PROPERTY = "com.sun.xml.bind.xmlHeaders";
     private static final String XML_PI_PROPERTY_INT = "com.sun.xml.internal.bind.xmlHeaders";
     
@@ -383,11 +379,6 @@ public class JAXBElementProvider<T> exte
         marshal(obj, cls, genericType, enc, os, mt, ms);
     }
     
-    protected static void setNamespaceMapper(Marshaller ms, Map<String, String> map)
throws Exception {
-        NamespaceMapper nsMapper = new NamespaceMapper(map);
-        setMarshallerProp(ms, nsMapper, NS_MAPPER_PROPERTY, NS_MAPPER_PROPERTY_INT);
-    }
-    
     protected void marshal(Object obj, Class<?> cls, Type genericType, 
                            String enc, OutputStream os, MediaType mt) throws Exception {
         marshal(obj, cls, genericType, enc, os, mt, new Annotation[]{});
@@ -451,15 +442,7 @@ public class JAXBElementProvider<T> exte
         }
     }
     
-    private static void setMarshallerProp(Marshaller ms, Object value, 
-                                          String name1, String name2) throws Exception {
-        try {
-            ms.setProperty(name1, value);
-        } catch (PropertyException ex) {
-            ms.setProperty(name2, value);
-        }
-        
-    }
+    
     
     protected void addAttachmentMarshaller(Marshaller ms) {
         Collection<Attachment> attachments = getAttachments(true);

Modified: cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/json/JSONProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/json/JSONProvider.java?rev=1335400&r1=1335399&r2=1335400&view=diff
==============================================================================
--- cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/json/JSONProvider.java
(original)
+++ cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/json/JSONProvider.java
Tue May  8 10:00:22 2012
@@ -506,6 +506,9 @@ public class JSONProvider<T> extends Abs
         }
         
         Marshaller ms = createMarshaller(actualObject, actualClass, genericType, enc);
+        if (!namespaceMap.isEmpty()) {
+            setNamespaceMapper(ms, namespaceMap);
+        }
         marshal(ms, actualObject, actualClass, genericType, enc, os, false);
     }
     

Modified: cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/json/JSONProviderTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/json/JSONProviderTest.java?rev=1335400&r1=1335399&r2=1335400&view=diff
==============================================================================
--- cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/json/JSONProviderTest.java
(original)
+++ cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/json/JSONProviderTest.java
Tue May  8 10:00:22 2012
@@ -51,6 +51,7 @@ import javax.xml.bind.annotation.XmlElem
 import javax.xml.bind.annotation.XmlElementWrapper;
 import javax.xml.bind.annotation.XmlMixed;
 import javax.xml.bind.annotation.XmlRootElement;
+import javax.xml.bind.annotation.XmlSeeAlso;
 import javax.xml.bind.annotation.XmlType;
 import javax.xml.stream.XMLStreamWriter;
 import javax.xml.transform.stream.StreamSource;
@@ -170,7 +171,6 @@ public class JSONProviderTest extends As
                          new MetadataMap<String, Object>(), bos);
         String expected = "{\"ns1.SuperBooks\":[{\"id\":123,\"name\":\"CXF in Action\","
             + "\"state\":\"\",\"superId\":124}]}";
-        System.out.println(bos.toString());
         assertEquals(expected, bos.toString());
     }
     
@@ -598,7 +598,7 @@ public class JSONProviderTest extends As
                   MediaType.APPLICATION_JSON_TYPE, new MetadataMap<String, Object>(),
os);
         
         String s = os.toString();
-        System.out.println(s);
+        
         assertEquals("{\"ns1.thetag\":{\"group\":\"b\",\"name\":\"a\"}}", s);
         
     }
@@ -1137,6 +1137,86 @@ public class JSONProviderTest extends As
         return new TagVO2(name, group);
     }
     
+    @Test
+    public void testWriteReadDerivedNamespace() throws Exception {
+        JSONProvider<Base1> provider = new JSONProvider<Base1>();
+        provider.setMarshallAsJaxbElement(true);
+        Map<String, String> namespaceMap = new HashMap<String, String>();
+        namespaceMap.put("http://derivedtest", "derivedtestnamespace");
+        provider.setNamespaceMap(namespaceMap);
+
+        Base1 b = new Derived1("base", "derived");
+
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        provider.writeTo(b, Base1.class, Base1.class,
+                        new Annotation[0], MediaType.APPLICATION_JSON_TYPE,
+                        new MetadataMap<String, Object>(), bos);
+        
+        readBase(bos.toString());
+    }
+
+    private void readBase(String data) throws Exception {
+        JSONProvider<Base1> provider = new JSONProvider<Base1>();
+        provider.setUnmarshallAsJaxbElement(true);
+        Map<String, String> namespaceMap = new HashMap<String, String>();
+        namespaceMap.put("http://derivedtest", "derivedtestnamespace");
+        provider.setNamespaceMap(namespaceMap);
+
+        ByteArrayInputStream is = new ByteArrayInputStream(data.getBytes());
+
+        Base1 base = provider.readFrom(
+                        Base1.class, Base1.class,
+                        new Annotation[0], MediaType.APPLICATION_JSON_TYPE,
+                        new MetadataMap<String, String>(), is);
+        assertEquals("base", base.getBase1Field());
+    }
+
+    @XmlAccessorType(XmlAccessType.FIELD)
+    @XmlType(name = "Base1", namespace = "http://derivedtest")
+    @XmlSeeAlso({Derived1.class })
+    public static class Base1 {
+    
+        protected String base1Field;
+    
+        Base1() { }
+    
+        Base1(String base) {
+            base1Field = base;
+        }
+    
+        public String getBase1Field() {
+            return base1Field;
+        }
+    
+        public void setBase1Field(String value) {
+            this.base1Field = value;
+        }
+    }
+
+
+    @XmlAccessorType(XmlAccessType.FIELD)
+    @XmlType(name = "Derived1", namespace = "http://derivedtest")
+    public static class Derived1 extends Base1 {
+    
+        protected String derived1Field;
+    
+        Derived1() { }
+        public Derived1(String base, String derived) {
+            super(base);
+            derived1Field = derived;
+        }
+    
+        public String getDerived1Field() {
+            return derived1Field;
+        }
+    
+        public void setDerived1Field(String value) {
+            this.derived1Field = value;
+        }
+    
+    }
+
+    
     @XmlRootElement()
     public static class Books {
         @XmlMixed



Mime
View raw message