cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1335406 - in /cxf/branches/2.5.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 Tue, 08 May 2012 10:29:39 GMT
Author: sergeyb
Date: Tue May  8 10:29:39 2012
New Revision: 1335406

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

........
  r1335400 | sergeyb | 2012-05-08 11:00:22 +0100 (Tue, 08 May 2012) | 1 line
  
  [CXF-4296] Updating JSONProvider to use a namespaceMap for setting a Marshaller namespace
mapper
........

Modified:
    cxf/branches/2.5.x-fixes/   (props changed)
    cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
    cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
    cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
    cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JSONProviderTest.java

Propchange: cxf/branches/2.5.x-fixes/
------------------------------------------------------------------------------
    svn:mergeinfo = /cxf/trunk:1335400

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

Modified: cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java?rev=1335406&r1=1335405&r2=1335406&view=diff
==============================================================================
--- cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
(original)
+++ cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
Tue May  8 10:29:39 2012
@@ -46,6 +46,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.apache.cxf.common.i18n.Bundle
 import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.common.util.PackageUtils;
 import org.apache.cxf.jaxb.JAXBUtils;
+import org.apache.cxf.jaxb.NamespaceMapper;
 import org.apache.cxf.jaxrs.ext.MessageContext;
 import org.apache.cxf.jaxrs.model.ClassResourceInfo;
 import org.apache.cxf.jaxrs.utils.InjectionUtils;
@@ -83,9 +85,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();
@@ -122,6 +127,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/branches/2.5.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java?rev=1335406&r1=1335405&r2=1335406&view=diff
==============================================================================
--- cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
(original)
+++ cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
Tue May  8 10:29:39 2012
@@ -44,7 +44,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;
@@ -56,7 +55,6 @@ import javax.xml.stream.XMLStreamWriter;
 import javax.xml.transform.Source;
 
 import org.apache.cxf.helpers.CastUtils;
-import org.apache.cxf.jaxb.NamespaceMapper;
 import org.apache.cxf.jaxrs.ext.MessageContext;
 import org.apache.cxf.jaxrs.ext.Nullable;
 import org.apache.cxf.jaxrs.ext.xml.XMLInstruction;
@@ -78,8 +76,6 @@ import org.apache.cxf.staxutils.transfor
 @Provider
 public class JAXBElementProvider extends AbstractJAXBProvider  {
     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";
     
@@ -372,11 +368,6 @@ public class JAXBElementProvider extends
         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[]{});
@@ -440,15 +431,7 @@ public class JAXBElementProvider extends
         }
     }
     
-    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/branches/2.5.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java?rev=1335406&r1=1335405&r2=1335406&view=diff
==============================================================================
--- cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
(original)
+++ cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
Tue May  8 10:29:39 2012
@@ -504,6 +504,9 @@ public class JSONProvider extends Abstra
         }
         
         Marshaller ms = createMarshaller(actualObject, actualClass, genericType, enc);
+        if (!namespaceMap.isEmpty()) {
+            setNamespaceMapper(ms, namespaceMap);
+        }
         marshal(ms, actualObject, actualClass, genericType, enc, os, false);
     }
     

Modified: cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JSONProviderTest.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JSONProviderTest.java?rev=1335406&r1=1335405&r2=1335406&view=diff
==============================================================================
--- cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JSONProviderTest.java
(original)
+++ cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JSONProviderTest.java
Tue May  8 10:29:39 2012
@@ -50,6 +50,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;
@@ -602,7 +603,6 @@ 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);
         
     }
@@ -1147,7 +1147,87 @@ public class JSONProviderTest extends As
     private TagVO2 createTag2(String name, String group) {
         return new TagVO2(name, group);
     }
+ 
+    @Test
+    public void testWriteReadDerivedNamespace() throws Exception {
+        JSONProvider provider = new JSONProvider();
+        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());
+    }
+
+    @SuppressWarnings("unchecked")
+    private void readBase(String data) throws Exception {
+        JSONProvider provider = new JSONProvider();
+        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 = (Base1)provider.readFrom(
+                        (Class)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