cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r797534 - in /cxf/trunk/rt/frontend/jaxrs/src: main/java/org/apache/cxf/jaxrs/provider/AegisJSONProvider.java main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java test/java/org/apache/cxf/jaxrs/provider/AegisJSONProviderTest.java
Date Fri, 24 Jul 2009 15:33:57 GMT
Author: sergeyb
Date: Fri Jul 24 15:33:57 2009
New Revision: 797534

URL: http://svn.apache.org/viewvc?rev=797534&view=rev
Log:
JAXRS : fixing Aegis JSON reads

Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AegisJSONProvider.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/AegisJSONProviderTest.java

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AegisJSONProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AegisJSONProvider.java?rev=797534&r1=797533&r2=797534&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AegisJSONProvider.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AegisJSONProvider.java
Fri Jul 24 15:33:57 2009
@@ -25,6 +25,8 @@
 import java.io.OutputStreamWriter;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Type;
+import java.util.Collections;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
@@ -33,11 +35,13 @@
 import javax.ws.rs.Produces;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.ext.Provider;
+import javax.xml.namespace.NamespaceContext;
 import javax.xml.namespace.QName;
 import javax.xml.stream.XMLStreamReader;
 import javax.xml.stream.XMLStreamWriter;
 
-import org.apache.cxf.common.util.PackageUtils;
+import org.apache.cxf.jaxrs.utils.JAXRSUtils;
+import org.apache.cxf.staxutils.DepthXMLStreamReader;
 import org.codehaus.jettison.AbstractXMLStreamWriter;
 import org.codehaus.jettison.mapped.Configuration;
 import org.codehaus.jettison.mapped.MappedNamespaceConvention;
@@ -99,17 +103,17 @@
     
     @Override
     protected XMLStreamReader createStreamReader(Class<?> type, InputStream is) throws
Exception {
+        if (readXsiType) {
+            namespaceMap.putIfAbsent("http://www.w3.org/2001/XMLSchema-instance", "xsins");
+        }
+        getQName(type);
         MappedXMLInputFactory factory = new MappedXMLInputFactory(namespaceMap);
-        return factory.createXMLStreamReader(is);
+        return new NamespaceContextReader(factory.createXMLStreamReader(is));
     }
     
     private QName getQName(Class<?> type) {
-        String nsURI = PackageUtils.getNamespace(PackageUtils.getPackageName(type));
-        if (nsURI.endsWith("/")) {
-            nsURI = nsURI.substring(0, nsURI.length() - 1);
-        }
-        QName qname = new QName(nsURI, type.getSimpleName(), "ns1"); 
-        namespaceMap.putIfAbsent(nsURI, "ns1");
+        QName qname = JAXRSUtils.getClassQName(type); 
+        namespaceMap.putIfAbsent(qname.getNamespaceURI(), "ns1");
         return qname;
     }
     
@@ -118,4 +122,36 @@
                                     qname.getNamespaceURI(),
                                     qname.getLocalPart());
     }
+    
+    private class NamespaceContextReader extends DepthXMLStreamReader {
+        public NamespaceContextReader(XMLStreamReader reader) {
+            super(reader);
+        }
+        
+        @Override
+        public NamespaceContext getNamespaceContext() {
+            return new NamespaceContext() {
+
+                public String getNamespaceURI(String prefix) {
+                    for (Map.Entry<String, String> entry : namespaceMap.entrySet())
{
+                        if (entry.getValue().equals(prefix)) {
+                            return entry.getKey();
+                        }
+                    }
+                    return null;
+                }
+
+                public String getPrefix(String ns) {
+                    return namespaceMap.get(ns);
+                }
+
+                public Iterator getPrefixes(String ns) {
+                    String prefix = getPrefix(ns);
+                    return prefix == null ? null : Collections.singletonList(prefix).iterator();
+                }
+                
+            };
+        }
+    }
+    
 }

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java?rev=797534&r1=797533&r2=797534&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java Fri
Jul 24 15:33:57 2009
@@ -63,9 +63,11 @@
 import javax.ws.rs.ext.MessageBodyReader;
 import javax.ws.rs.ext.MessageBodyWriter;
 import javax.ws.rs.ext.Providers;
+import javax.xml.namespace.QName;
 
 import org.apache.cxf.common.i18n.BundleUtils;
 import org.apache.cxf.common.logging.LogUtils;
+import org.apache.cxf.common.util.PackageUtils;
 import org.apache.cxf.common.util.StringUtils;
 import org.apache.cxf.jaxrs.ext.MessageContext;
 import org.apache.cxf.jaxrs.ext.MessageContextImpl;
@@ -1041,4 +1043,12 @@
         return false;
     }
     
+    public static QName getClassQName(Class<?> type) {
+        String nsURI = PackageUtils.getNamespace(PackageUtils.getPackageName(type));
+        if (nsURI.endsWith("/")) {
+            nsURI = nsURI.substring(0, nsURI.length() - 1);
+        }
+        return new QName(nsURI, type.getSimpleName(), "ns1"); 
+    }
+    
 }

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/AegisJSONProviderTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/AegisJSONProviderTest.java?rev=797534&r1=797533&r2=797534&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/AegisJSONProviderTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/AegisJSONProviderTest.java
Fri Jul 24 15:33:57 2009
@@ -35,7 +35,6 @@
 import org.apache.cxf.jaxrs.resources.Tags;
 
 import org.junit.Assert;
-import org.junit.Ignore;
 import org.junit.Test;
 
 public class AegisJSONProviderTest extends Assert {
@@ -52,14 +51,27 @@
         assertFalse(p.isReadable(AegisTestBean.class, null, null, null));
     }
     
-    @SuppressWarnings("unchecked")
+    
     @Test
-    @Ignore
     public void testReadFrom() throws Exception {
+        doTestRead(true);
+    }
+    
+    @Test
+    public void testReadFromNoMap() throws Exception {
+        doTestRead(false);
+    }
+    
+    @SuppressWarnings("unchecked")
+    private void doTestRead(boolean setNsMap) throws Exception {
         AegisJSONProvider p = new AegisJSONProvider();
-        Map<String, String> namespaceMap = new HashMap<String, String>();
-        namespaceMap.put("http://fortest.jaxrs.cxf.apache.org", "ns1");
-        namespaceMap.put("http://www.w3.org/2001/XMLSchema-instance", "ns2");
+        p.clearContexts();
+        if (setNsMap) {
+            Map<String, String> namespaceMap = new HashMap<String, String>();
+            namespaceMap.put("http://fortest.jaxrs.cxf.apache.org", "ns1");
+            namespaceMap.put("http://www.w3.org/2001/XMLSchema-instance", "xsins");
+            p.setNamespaceMap(namespaceMap);
+        }
         String data = "{\"ns1.AegisTestBean\":{\"@xsins.type\":\"ns1:AegisTestBean\","
             + "\"ns1.boolValue\":true,\"ns1.strValue\":\"hovercraft\"}}";
         



Mime
View raw message