You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by se...@apache.org on 2008/12/04 20:08:21 UTC

svn commit: r723397 - in /cxf/branches/2.1.x-fixes: ./ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/schemas/ rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ rt/...

Author: sergeyb
Date: Thu Dec  4 11:08:20 2008
New Revision: 723397

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

........
  r721221 | sergeyb | 2008-11-27 16:16:12 +0000 (Thu, 27 Nov 2008) | 1 line
  
  JAXRS : JSONProvider fixes, adding an option to share schema resources between JSON and JAXB providers
........

Added:
    cxf/branches/2.1.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/schemas/
      - copied from r721221, cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/schemas/
    cxf/branches/2.1.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/schemas/SchemaHandler.java
      - copied unchanged from r721221, cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/schemas/SchemaHandler.java
    cxf/branches/2.1.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JSONProviderTest.java
      - copied unchanged from r721221, cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JSONProviderTest.java
    cxf/branches/2.1.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/resources/ManyTags.java
      - copied unchanged from r721221, cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/resources/ManyTags.java
    cxf/branches/2.1.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/resources/TagVO.java
      - copied unchanged from r721221, cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/resources/TagVO.java
    cxf/branches/2.1.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/resources/Tags.java
      - copied unchanged from r721221, cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/resources/Tags.java
    cxf/branches/2.1.x-fixes/systests/src/test/java/org/apache/cxf/systest/jaxrs/bookid.xsd
      - copied unchanged from r721221, cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/bookid.xsd
    cxf/branches/2.1.x-fixes/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/add_book2json.txt
      - copied unchanged from r721221, cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/add_book2json.txt
    cxf/branches/2.1.x-fixes/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/add_book2json_invalid.txt
      - copied unchanged from r721221, cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/add_book2json_invalid.txt
Modified:
    cxf/branches/2.1.x-fixes/   (props changed)
    cxf/branches/2.1.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractAegisProvider.java
    cxf/branches/2.1.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
    cxf/branches/2.1.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
    cxf/branches/2.1.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
    cxf/branches/2.1.x-fixes/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
    cxf/branches/2.1.x-fixes/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreSpring.java
    cxf/branches/2.1.x-fixes/systests/src/test/java/org/apache/cxf/systest/jaxrs/Books.java
    cxf/branches/2.1.x-fixes/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
    cxf/branches/2.1.x-fixes/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerSpringBookTest.java
    cxf/branches/2.1.x-fixes/systests/src/test/resources/jaxrs/WEB-INF/beans.xml

Propchange: cxf/branches/2.1.x-fixes/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Thu Dec  4 11:08:20 2008
@@ -1 +1 @@
-/cxf/trunk:686333-686363,686764,686820,687096,687194,687363,687387,687463,687543,687722,687798,687814,687817,687891,687910,687914,688086,688102,688133,688596,688735,688870,689572,689596,689855,689924,690067,690289,691246,691271,691295,691338,691355,691488,691602,691646,691706,691728,692116,692157,692310,692466,692499,693653,693819,694179,694263,694417,694716,694744,694747,694795,694869,694981,694987,694993,695041,695096,695396,695484,695537,695552,695561,695619,695684,695835,695840,695868,695935,695977,696016,696094,696433,696720,697085,697868,698128,699289,700261,700507,700602,700981,701316,701783,701830,701862,702187,702205-702248,702267,702547,702561,702580,702602,702609,702616,702653,702656,702957,703191,703239,703309,703501,703513,703548,704584,704937,704997,705150,705235,705274,705340,705446,705548,705614,705692,705708,706482,706631,706675,706900,706909,707034,707089,707100,707902,708035,708044,708074,708410,708417,708550,708554,709353-709354,709425,710076,710150,71015
 4,711193,711388,711410,711490,711635,711975,712194,712198,712238,712272,712299,712312,712670,712893,713082,713095-713096,713099,713584,713597,713737,713804,713899,714167-714168,714245,714255,717937-717961,718281,718439,718448,718565,718620,718640,718665,718970,719017,719210,719215-719218,719222,719273,719305,719327,719354,719362,719368,719382,719649,719680,720053,720119,720238,720497,723024
+/cxf/trunk:686333-686363,686764,686820,687096,687194,687363,687387,687463,687543,687722,687798,687814,687817,687891,687910,687914,688086,688102,688133,688596,688735,688870,689572,689596,689855,689924,690067,690289,691246,691271,691295,691338,691355,691488,691602,691646,691706,691728,692116,692157,692310,692466,692499,693653,693819,694179,694263,694417,694716,694744,694747,694795,694869,694981,694987,694993,695041,695096,695396,695484,695537,695552,695561,695619,695684,695835,695840,695868,695935,695977,696016,696094,696433,696720,697085,697868,698128,699289,700261,700507,700602,700981,701316,701783,701830,701862,702187,702205-702248,702267,702547,702561,702580,702602,702609,702616,702653,702656,702957,703191,703239,703309,703501,703513,703548,704584,704937,704997,705150,705235,705274,705340,705446,705548,705614,705692,705708,706482,706631,706675,706900,706909,707034,707089,707100,707902,708035,708044,708074,708410,708417,708550,708554,709353-709354,709425,710076,710150,71015
 4,711193,711388,711410,711490,711635,711975,712194,712198,712238,712272,712299,712312,712670,712893,713082,713095-713096,713099,713584,713597,713737,713804,713899,714167-714168,714245,714255,717937-717961,718281,718439,718448,718565,718620,718640,718665,718970,719017,719210,719215-719218,719222,719273,719305,719327,719354,719362,719368,719382,719649,719680,720053,720119,720238,720497,721221,723024

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

Modified: cxf/branches/2.1.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractAegisProvider.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.1.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractAegisProvider.java?rev=723397&r1=723396&r2=723397&view=diff
==============================================================================
--- cxf/branches/2.1.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractAegisProvider.java (original)
+++ cxf/branches/2.1.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractAegisProvider.java Thu Dec  4 11:08:20 2008
@@ -19,46 +19,26 @@
 
 package org.apache.cxf.jaxrs.provider;
 
-import java.io.BufferedReader;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.Reader;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Type;
-import java.util.ArrayList;
 import java.util.HashSet;
-import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.WeakHashMap;
-import java.util.logging.Logger;
 
 import javax.ws.rs.core.Context;
 import javax.ws.rs.ext.ContextResolver;
 import javax.ws.rs.ext.MessageBodyReader;
 import javax.ws.rs.ext.MessageBodyWriter;
-import javax.xml.XMLConstants;
-import javax.xml.transform.Source;
-import javax.xml.transform.stream.StreamSource;
-import javax.xml.validation.Schema;
-import javax.xml.validation.SchemaFactory;
 
 import org.apache.cxf.aegis.AegisContext;
-import org.apache.cxf.common.logging.LogUtils;
 
 public abstract class AbstractAegisProvider 
     implements MessageBodyReader<Object>, MessageBodyWriter<Object> {
     
-    private static final Logger LOG = LogUtils.getL7dLogger(AbstractAegisProvider.class);
-
-    private static final String CLASSPATH_PREFIX = "classpath:";
-        
     private static Map<Class<?>, AegisContext> classContexts = new WeakHashMap<Class<?>, AegisContext>();
     
     @Context protected ContextResolver<AegisContext> resolver;
-    private Schema schema;
     
     public boolean isWriteable(Class<?> type, Type genericType, Annotation[] anns) {
         return isSupported(type, genericType, anns);
@@ -68,10 +48,6 @@
         return isSupported(type, genericType, annotations);
     }
 
-    public void setSchemas(List<String> locations) {
-        schema = createSchema(locations);    
-    }
-    
     public long getSize(Object o) {
         return -1;
     }
@@ -116,44 +92,4 @@
     protected boolean isSupported(Class<?> type, Type genericType, Annotation[] annotations) {
         return true;
     }
-    
-    private Schema createSchema(List<String> locations) {
-        
-        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
-        Schema s = null;
-        try {
-            List<Source> sources = new ArrayList<Source>();
-            for (String loc : locations) {
-                InputStream is = null;
-                if (loc.startsWith(CLASSPATH_PREFIX)) {
-                    String path = loc.substring(CLASSPATH_PREFIX.length() + 1);
-                    is = getClass().getClassLoader().getResourceAsStream(path);
-                    if (is == null) {
-                        LOG.warning("No schema resource " + loc + " is available on classpath");
-                        return null;
-                    }
-                } else {
-                    File f = new File(loc);
-                    if (!f.exists()) {
-                        LOG.warning("No schema resource " + loc + " is available on local disk");
-                        return null;
-                    }
-                    is = new FileInputStream(f);
-                }
-                                
-                Reader r = new BufferedReader(
-                               new InputStreamReader(is, "UTF-8"));
-                sources.add(new StreamSource(r));
-            }
-            s = factory.newSchema(sources.toArray(new Source[]{}));
-        } catch (Exception ex) {
-            LOG.warning("Validation will be disabled, failed to create schema : " + ex.getMessage());
-        }
-        return s;
-        
-    }
-    
-    protected Schema getSchema() {
-        return schema;
-    }
 }

Modified: cxf/branches/2.1.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.1.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java?rev=723397&r1=723396&r2=723397&view=diff
==============================================================================
--- cxf/branches/2.1.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java (original)
+++ cxf/branches/2.1.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java Thu Dec  4 11:08:20 2008
@@ -19,27 +19,21 @@
 
 package org.apache.cxf.jaxrs.provider;
 
-import java.io.BufferedReader;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.Reader;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Type;
-import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import java.util.ResourceBundle;
 import java.util.WeakHashMap;
 import java.util.logging.Logger;
 
+import javax.ws.rs.WebApplicationException;
 import javax.ws.rs.core.Context;
 import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
 import javax.ws.rs.ext.ContextResolver;
 import javax.ws.rs.ext.MessageBodyReader;
 import javax.ws.rs.ext.MessageBodyWriter;
-import javax.xml.XMLConstants;
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.JAXBElement;
 import javax.xml.bind.JAXBException;
@@ -48,16 +42,14 @@
 import javax.xml.bind.annotation.XmlRootElement;
 import javax.xml.bind.annotation.adapters.XmlAdapter;
 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
-import javax.xml.transform.Source;
-import javax.xml.transform.stream.StreamSource;
 import javax.xml.validation.Schema;
-import javax.xml.validation.SchemaFactory;
 
 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.jaxrs.utils.AnnotationUtils;
 import org.apache.cxf.jaxrs.utils.InjectionUtils;
+import org.apache.cxf.jaxrs.utils.schemas.SchemaHandler;
 
 public abstract class AbstractJAXBProvider 
     implements MessageBodyReader<Object>, MessageBodyWriter<Object> {
@@ -67,7 +59,6 @@
     private static final Logger LOG = LogUtils.getL7dLogger(AbstractJAXBProvider.class);
 
     private static final String CHARSET_PARAMETER = "charset"; 
-    private static final String CLASSPATH_PREFIX = "classpath:";
         
     private static Map<String, JAXBContext> packageContexts = new WeakHashMap<String, JAXBContext>();
     private static Map<Class<?>, JAXBContext> classContexts = new WeakHashMap<Class<?>, JAXBContext>();
@@ -84,14 +75,19 @@
         return isSupported(type, genericType, annotations);
     }
 
-    public void setSchemas(List<String> locations) {
-        schema = createSchema(locations);    
+    public void setSchemaLocations(List<String> locations) {
+        schema = SchemaHandler.createSchema(locations);    
     }
     
     public long getSize(Object o) {
         return -1;
     }
 
+    public void setSchema(Schema s) {
+        schema = s;    
+    }
+ 
+
     protected JAXBContext getJAXBContext(Class<?> type, Type genericType) throws JAXBException {
         
         if (resolver != null) {
@@ -222,43 +218,17 @@
         return obj;
     }
     
-    private Schema createSchema(List<String> locations) {
-        
-        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
-        Schema s = null;
-        try {
-            List<Source> sources = new ArrayList<Source>();
-            for (String loc : locations) {
-                InputStream is = null;
-                if (loc.startsWith(CLASSPATH_PREFIX)) {
-                    String path = loc.substring(CLASSPATH_PREFIX.length() + 1);
-                    is = getClass().getClassLoader().getResourceAsStream(path);
-                    if (is == null) {
-                        LOG.warning("No schema resource " + loc + " is available on classpath");
-                        return null;
-                    }
-                } else {
-                    File f = new File(loc);
-                    if (!f.exists()) {
-                        LOG.warning("No schema resource " + loc + " is available on local disk");
-                        return null;
-                    }
-                    is = new FileInputStream(f);
-                }
-                                
-                Reader r = new BufferedReader(
-                               new InputStreamReader(is, "UTF-8"));
-                sources.add(new StreamSource(r));
-            }
-            s = factory.newSchema(sources.toArray(new Source[]{}));
-        } catch (Exception ex) {
-            LOG.warning("Validation will be disabled, failed to create schema : " + ex.getMessage());
-        }
-        return s;
-        
-    }
-    
     protected Schema getSchema() {
         return schema;
     }
+    
+    protected static void handleJAXBException(JAXBException e) {
+        Throwable t = e.getLinkedException() != null 
+            ? e.getLinkedException() : e.getCause() != null ? e.getCause() : e;
+        String message = new org.apache.cxf.common.i18n.Message("JAXB_EXCEPTION", 
+                             BUNDLE, t.getMessage()).toString();
+        Response r = Response.status(Response.Status.INTERNAL_SERVER_ERROR)
+            .type(MediaType.TEXT_PLAIN).entity(message).build();
+        throw new WebApplicationException(t, r);
+    }
 }

Modified: cxf/branches/2.1.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.1.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java?rev=723397&r1=723396&r2=723397&view=diff
==============================================================================
--- cxf/branches/2.1.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java (original)
+++ cxf/branches/2.1.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java Thu Dec  4 11:08:20 2008
@@ -33,7 +33,6 @@
 import javax.ws.rs.WebApplicationException;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.MultivaluedMap;
-import javax.ws.rs.core.Response;
 import javax.ws.rs.ext.Provider;
 import javax.xml.bind.JAXBElement;
 import javax.xml.bind.JAXBException;
@@ -41,6 +40,8 @@
 import javax.xml.bind.Unmarshaller;
 import javax.xml.transform.stream.StreamSource;
 
+import org.apache.cxf.jaxrs.utils.schemas.SchemaHandler;
+
 @ProduceMime({"application/xml", "text/xml" })
 @ConsumeMime({"application/xml", "text/xml" })
 @Provider
@@ -49,7 +50,11 @@
     private Map<String, Object> mProperties = new HashMap<String, Object>();
     
     public void setSchemas(List<String> locations) {
-        super.setSchemas(locations);
+        super.setSchemaLocations(locations);
+    }
+    
+    public void setSchemaHandler(SchemaHandler handler) {
+        super.setSchema(handler.getSchema());
     }
     
     public void setMarshallerProperties(Map<String, Object> marshallProperties) {
@@ -74,16 +79,12 @@
             }
             
         } catch (JAXBException e) {
-            Throwable t = e.getLinkedException() != null 
-                ? e.getLinkedException() : e.getCause() != null ? e.getCause() : e;
-            String message = new org.apache.cxf.common.i18n.Message("JAXB_EXCEPTION", 
-                                 BUNDLE, t.getMessage()).toString();
-            Response r = Response.status(Response.Status.INTERNAL_SERVER_ERROR)
-                .type(MediaType.TEXT_PLAIN).entity(message).build();
-            throw new WebApplicationException(t, r);
+            handleJAXBException(e);
         } catch (Exception e) {
             throw new WebApplicationException(e);        
         }
+        // unreachable
+        return null;
     }
 
     

Modified: cxf/branches/2.1.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.1.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java?rev=723397&r1=723396&r2=723397&view=diff
==============================================================================
--- cxf/branches/2.1.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java (original)
+++ cxf/branches/2.1.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java Thu Dec  4 11:08:20 2008
@@ -23,6 +23,7 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.io.OutputStreamWriter;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Type;
 import java.util.HashMap;
@@ -35,28 +36,47 @@
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.ext.Provider;
-import javax.xml.bind.JAXBContext;
 import javax.xml.bind.JAXBElement;
 import javax.xml.bind.JAXBException;
 import javax.xml.bind.Marshaller;
 import javax.xml.bind.Unmarshaller;
-import javax.xml.stream.XMLOutputFactory;
+import javax.xml.bind.annotation.XmlRootElement;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamReader;
-import javax.xml.stream.XMLStreamWriter;
 
+import org.apache.cxf.jaxrs.utils.schemas.SchemaHandler;
+import org.codehaus.jettison.AbstractXMLStreamWriter;
+import org.codehaus.jettison.mapped.Configuration;
+import org.codehaus.jettison.mapped.MappedNamespaceConvention;
 import org.codehaus.jettison.mapped.MappedXMLInputFactory;
-import org.codehaus.jettison.mapped.MappedXMLOutputFactory;
+import org.codehaus.jettison.mapped.MappedXMLStreamWriter;
 
 @ProduceMime("application/json")
 @ConsumeMime("application/json")
 @Provider
 public final class JSONProvider extends AbstractJAXBProvider  {
     
+    private static final String JAXB_DEFAULT_NAMESPACE = "##default";
+    private static final String JAXB_DEFAULT_NAME = "##default";
+    
     private Map<String, String> namespaceMap = new HashMap<String, String>();
+    private boolean serializeAsArray;
+    private List<String> arrayKeys;
     
     public void setSchemas(List<String> locations) {
-        super.setSchemas(locations);
+        super.setSchemaLocations(locations);
+    }
+    
+    public void setSchemaHandler(SchemaHandler handler) {
+        super.setSchema(handler.getSchema());
+    }
+    
+    public void setSerializeAsArray(boolean asArray) {
+        this.serializeAsArray = asArray;
+    }
+    
+    public void setArrayKeys(List<String> keys) {
+        this.arrayKeys = keys;
     }
     
     public void setNamespaceMap(Map<String, String> namespaceMap) {
@@ -69,8 +89,7 @@
         
         try {
             Class<?> theType = getActualType(type, genericType);
-            JAXBContext context = getJAXBContext(theType, genericType);
-            Unmarshaller unmarshaller = context.createUnmarshaller();
+            Unmarshaller unmarshaller = createUnmarshaller(theType, genericType);
             
             MappedXMLInputFactory factory = new MappedXMLInputFactory(namespaceMap);
             XMLStreamReader xsw = factory.createXMLStreamReader(is);
@@ -83,12 +102,14 @@
             return response;
             
         } catch (JAXBException e) {
-            throw new WebApplicationException(e);         
+            handleJAXBException(e);
         } catch (XMLStreamException e) {
             throw new WebApplicationException(e);
         } catch (Exception e) {
             throw new WebApplicationException(e);
-        } 
+        }
+        // unreachable
+        return null;
     }
 
     public void writeTo(Object obj, Class<?> cls, Type genericType, Annotation[] anns,  
@@ -103,8 +124,22 @@
             }
             Marshaller ms = createMarshaller(actualObject, actualClass, genericType, m);
 
-            XMLOutputFactory factory = new MappedXMLOutputFactory(namespaceMap);
-            XMLStreamWriter xsw = factory.createXMLStreamWriter(os);            
+            Configuration c = new Configuration(namespaceMap);
+            MappedNamespaceConvention convention = new MappedNamespaceConvention(c);
+            AbstractXMLStreamWriter xsw = new MappedXMLStreamWriter(
+                                               convention, 
+                                               new OutputStreamWriter(os, "UTF-8"));
+            if (serializeAsArray) {
+                if (arrayKeys != null) {
+                    for (String key : arrayKeys) {
+                        xsw.seriliazeAsArray(key);
+                    }
+                } else {
+                    String key = getKey(convention, cls);
+                    xsw.seriliazeAsArray(key);
+                }
+            }
+                        
             ms.marshal(actualObject, xsw);
             xsw.close();
             
@@ -115,7 +150,33 @@
         }
     }
 
-    
+    private String getKey(MappedNamespaceConvention convention, Class<?> cls) {
+        String key = null;
+        
+        XmlRootElement root = cls.getAnnotation(XmlRootElement.class);
+        if (root != null) {
+            
+            String namespace = root.namespace();
+            if (JAXB_DEFAULT_NAMESPACE.equals(namespace)) {
+                namespace = "";
+            }
+            
+            String prefix = namespaceMap.get(namespace);
+            if (prefix == null) {
+                prefix = "";
+            }
+            
+            String name = root.name();
+            if (JAXB_DEFAULT_NAME.equals(name)) {
+                name = cls.getSimpleName();
+            }
+            key = convention.createKey(prefix, namespace, name);
+            
+        } else {
+            key = convention.createKey("", "", cls.getSimpleName());
+        }
+        return key;
+    }
     
     
 }

Modified: cxf/branches/2.1.x-fixes/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.1.x-fixes/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java?rev=723397&r1=723396&r2=723397&view=diff
==============================================================================
--- cxf/branches/2.1.x-fixes/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java (original)
+++ cxf/branches/2.1.x-fixes/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java Thu Dec  4 11:08:20 2008
@@ -289,6 +289,24 @@
 
         return r;
     }
+    
+    @DELETE
+    @Path("/books/id")
+    public Response deleteWithQuery(@QueryParam("value") @DefaultValue("-1") int id) {
+        if (id != 123) {
+            throw new WebApplicationException();
+        }
+        Book b = books.get(new Long(id));
+
+        Response r;
+        if (b != null) {
+            r = Response.ok().build();
+        } else {
+            r = Response.notModified().build();
+        }
+
+        return r;
+    }
 
     @POST
     @Path("/booksplain")

Modified: cxf/branches/2.1.x-fixes/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreSpring.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.1.x-fixes/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreSpring.java?rev=723397&r1=723396&r2=723397&view=diff
==============================================================================
--- cxf/branches/2.1.x-fixes/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreSpring.java (original)
+++ cxf/branches/2.1.x-fixes/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreSpring.java Thu Dec  4 11:08:20 2008
@@ -23,6 +23,7 @@
 import java.util.HashMap;
 import java.util.Map;
 
+import javax.ws.rs.ConsumeMime;
 import javax.ws.rs.GET;
 import javax.ws.rs.POST;
 import javax.ws.rs.Path;
@@ -48,6 +49,12 @@
     }
     
     @GET
+    @Path("/books/list/{id}")
+    public Books getBookAsJsonList(@PathParam("id") Long id) {
+        return new Books(books.get(id));
+    }
+    
+    @GET
     @Path("/books/{id}")
     public Book getBookById(@PathParam("id") Long id) {
         return books.get(id);
@@ -89,6 +96,7 @@
     @POST
     @Path("books/convert")
     @ProduceMime("application/xml")
+    @ConsumeMime({"application/xml", "application/json" })
     public Book convertBook(Book2 book) {
         // how to have Book2 populated ?
         Book b = new Book();
@@ -97,6 +105,8 @@
         return b;
     }
     
+    
+    
     final void init() {
         Book book = new Book();
         book.setId(mainId);

Modified: cxf/branches/2.1.x-fixes/systests/src/test/java/org/apache/cxf/systest/jaxrs/Books.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.1.x-fixes/systests/src/test/java/org/apache/cxf/systest/jaxrs/Books.java?rev=723397&r1=723396&r2=723397&view=diff
==============================================================================
--- cxf/branches/2.1.x-fixes/systests/src/test/java/org/apache/cxf/systest/jaxrs/Books.java (original)
+++ cxf/branches/2.1.x-fixes/systests/src/test/java/org/apache/cxf/systest/jaxrs/Books.java Thu Dec  4 11:08:20 2008
@@ -19,20 +19,27 @@
 
 package org.apache.cxf.systest.jaxrs;
 
-import javax.xml.bind.annotation.XmlElement;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlRootElement;
 
 @XmlRootElement(name = "Books")
+@XmlAccessorType(XmlAccessType.FIELD)
 public class Books {
 
-    @XmlElement
-    private Book b;
+    private List<Book> books = new ArrayList<Book>();
+    
+    public Books() {
+    }
     
     public Books(Book b) {
-        this.b = b;
+        books.add(b);
     }
 
     public Book getBook() {
-        return b;
+        return books.size() == 0 ? null : books.get(0);
     }
 }

Modified: cxf/branches/2.1.x-fixes/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.1.x-fixes/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java?rev=723397&r1=723396&r2=723397&view=diff
==============================================================================
--- cxf/branches/2.1.x-fixes/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java (original)
+++ cxf/branches/2.1.x-fixes/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java Thu Dec  4 11:08:20 2008
@@ -25,6 +25,7 @@
 import java.net.URLConnection;
 
 import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.methods.DeleteMethod;
 import org.apache.commons.httpclient.methods.FileRequestEntity;
 import org.apache.commons.httpclient.methods.GetMethod;
 import org.apache.commons.httpclient.methods.PostMethod;
@@ -498,6 +499,40 @@
     }
     
     @Test
+    public void testDeleteBook() throws Exception {
+        String endpointAddress =
+            "http://localhost:9080/bookstore/books/123"; 
+
+        DeleteMethod post = new DeleteMethod(endpointAddress);
+        HttpClient httpclient = new HttpClient();
+        
+        try {
+            int result = httpclient.executeMethod(post);
+            assertEquals(200, result);
+        } finally {
+            // Release current connection to the connection pool once you are done
+            post.releaseConnection();
+        }  
+    }
+    
+    @Test
+    public void testDeleteBookByQuery() throws Exception {
+        String endpointAddress =
+            "http://localhost:9080/bookstore/books/id?value=123"; 
+
+        DeleteMethod post = new DeleteMethod(endpointAddress);
+        HttpClient httpclient = new HttpClient();
+        
+        try {
+            int result = httpclient.executeMethod(post);
+            assertEquals(200, result);
+        } finally {
+            // Release current connection to the connection pool once you are done
+            post.releaseConnection();
+        }  
+    }
+    
+    @Test
     public void testGetCDsJSON() throws Exception {
         String endpointAddress =
             "http://localhost:9080/bookstore/cds"; 

Modified: cxf/branches/2.1.x-fixes/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerSpringBookTest.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.1.x-fixes/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerSpringBookTest.java?rev=723397&r1=723396&r2=723397&view=diff
==============================================================================
--- cxf/branches/2.1.x-fixes/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerSpringBookTest.java (original)
+++ cxf/branches/2.1.x-fixes/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerSpringBookTest.java Thu Dec  4 11:08:20 2008
@@ -47,98 +47,125 @@
         String endpointAddress =
             "http://localhost:9080/bookstore/bookinfo?"
                                + "param1=12&param2=3"; 
-        getBook(endpointAddress);
+        getBook(endpointAddress, "resources/expected_get_book123json.txt");
     }
     
     @Test
     public void testGetBook123() throws Exception {
         String endpointAddress =
             "http://localhost:9080/bookstore/books/123"; 
-        getBook(endpointAddress); 
+        getBook(endpointAddress, "resources/expected_get_book123json.txt"); 
+    }
+    
+    @Test
+    public void testGetBookAsArray() throws Exception {
+        URL url = new URL("http://localhost:9080/bookstore/books/list/123");
+        URLConnection connect = url.openConnection();
+        connect.addRequestProperty("Accept", "application/json");
+        InputStream in = connect.getInputStream();           
+
+        assertEquals("{\"Books\":{\"books\":[{\"id\":123,\"name\":\"CXF in Action\"}]}}", 
+                     getStringFromInputStream(in)); 
+        
     }
     
     @Test
     public void testGetBookWithEncodedQueryValue() throws Exception {
         String endpointAddress =
             "http://localhost:9080/bookstore/booksquery?id=12%2B3"; 
-        getBook(endpointAddress); 
+        getBook(endpointAddress, "resources/expected_get_book123json.txt"); 
     }
     
     @Test
     public void testGetBookWithEncodedPathValue() throws Exception {
         String endpointAddress =
             "http://localhost:9080/bookstore/id=12%2B3"; 
-        getBook(endpointAddress); 
+        getBook(endpointAddress, "resources/expected_get_book123json.txt"); 
     }
     
     @Test
     public void testGetDefaultBook() throws Exception {
         String endpointAddress =
             "http://localhost:9080/bookstore"; 
-        getBook(endpointAddress); 
+        getBook(endpointAddress, "resources/expected_get_book123json.txt"); 
     }
 
-    private void getBook(String endpointAddress) throws Exception {
+    private void getBook(String endpointAddress, String resource) throws Exception {
         URL url = new URL(endpointAddress);
         URLConnection connect = url.openConnection();
         connect.addRequestProperty("Accept", "application/json");
         InputStream in = connect.getInputStream();           
 
-        InputStream expected = getClass()
-            .getResourceAsStream("resources/expected_get_book123json.txt");
-
+        InputStream expected = getClass().getResourceAsStream(resource);
         assertEquals(getStringFromInputStream(expected), getStringFromInputStream(in));
     }
     
     @Test
-    public void testAddInvalidBook() throws Exception {
+    public void testAddInvalidXmlBook() throws Exception {
         
-        String endpointAddress =
-            "http://localhost:9080/bookstore/books/convert";
-        
-        File input = new File(getClass().getResource("resources/add_book.txt").toURI());         
-        PostMethod post = new PostMethod(endpointAddress);
-        post.setRequestHeader("Content-Type", "application/xml");
-        RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");
-        post.setRequestEntity(entity);
-        HttpClient httpclient = new HttpClient();
+        doPost("http://localhost:9080/bookstore/books/convert",
+               500,
+               "application/xml",
+               "resources/add_book.txt",
+               null);
+                
+    }
+    
+    @Test
+    public void testAddInvalidJsonBook() throws Exception {
         
-        try {
-            int result = httpclient.executeMethod(post);
-            assertEquals(500, result);
-            assertTrue(post.getResponseBodyAsString().contains("JAXBException"));
-        } finally {
-            // Release current connection to the connection pool once you are done
-            post.releaseConnection();
-        }
+        doPost("http://localhost:9080/bookstore/books/convert",
+               500,
+               "application/json",
+               "resources/add_book2json_invalid.txt",
+               null);
                 
     }
     
     @Test
-    public void testAddValidBook() throws Exception {
+    public void testAddValidXmlBook() throws Exception {
         
-        String endpointAddress =
-            "http://localhost:9080/bookstore/books/convert";
+        doPost("http://localhost:9080/bookstore/books/convert",
+               200,
+               "application/xml",
+               "resources/add_book2.txt",
+               "resources/expected_get_book123.txt");
+                
+    }
+    
+    @Test
+    public void testAddValidBookJson() throws Exception {
+        doPost("http://localhost:9080/bookstore/books/convert",
+               200,
+               "application/json",
+               "resources/add_book2json.txt",
+               "resources/expected_get_book123.txt");
+    }
+    
+    private void doPost(String endpointAddress, int expectedStatus, String contentType,
+                        String inResource, String expectedResource) throws Exception {
         
-        File input = new File(getClass().getResource("resources/add_book2.txt").toURI());         
+        File input = new File(getClass().getResource(inResource).toURI());         
         PostMethod post = new PostMethod(endpointAddress);
-        post.setRequestHeader("Content-Type", "application/xml");
+        post.setRequestHeader("Content-Type", contentType);
         RequestEntity entity = new FileRequestEntity(input, "text/xml");
         post.setRequestEntity(entity);
         HttpClient httpclient = new HttpClient();
         
         try {
             int result = httpclient.executeMethod(post);
-            assertEquals(200, result);
+            assertEquals(expectedStatus, result);
             
-            InputStream expected = getClass().getResourceAsStream("resources/expected_get_book123.txt");
-            
-            assertEquals(getStringFromInputStream(expected), post.getResponseBodyAsString());
+            if (expectedStatus != 500) {
+                InputStream expected = getClass().getResourceAsStream(expectedResource);
+                assertEquals(getStringFromInputStream(expected), post.getResponseBodyAsString());
+            } else {
+                assertTrue(post.getResponseBodyAsString().contains("JAXBException"));
+            }
         } finally {
             // Release current connection to the connection pool once you are done
             post.releaseConnection();
         }
-                
     }
     
     private String getStringFromInputStream(InputStream in) throws Exception {        

Modified: cxf/branches/2.1.x-fixes/systests/src/test/resources/jaxrs/WEB-INF/beans.xml
URL: http://svn.apache.org/viewvc/cxf/branches/2.1.x-fixes/systests/src/test/resources/jaxrs/WEB-INF/beans.xml?rev=723397&r1=723396&r2=723397&view=diff
==============================================================================
--- cxf/branches/2.1.x-fixes/systests/src/test/resources/jaxrs/WEB-INF/beans.xml (original)
+++ cxf/branches/2.1.x-fixes/systests/src/test/resources/jaxrs/WEB-INF/beans.xml Thu Dec  4 11:08:20 2008
@@ -26,10 +26,13 @@
   http://cxf.apache.org/simple http://cxf.apache.org/schemas/simple.xsd"-->
 <beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xmlns:util="http://www.springframework.org/schema/util"
   xmlns:jaxrs="http://cxf.apache.org/jaxrs"
   xsi:schemaLocation="
 http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans.xsd
+http://www.springframework.org/schema/util 
+http://www.springframework.org/schema/util/spring-util-2.0.xsd
 http://cxf.apache.org/jaxrs
 http://cxf.apache.org/schemas/jaxrs.xsd">
 
@@ -44,17 +47,38 @@
     </jaxrs:serviceBeans>		   
     <jaxrs:providers>
        <ref bean="jaxbProvider"/>
-       
+       <ref bean="jsonProvider"/>
     </jaxrs:providers>
     
   </jaxrs:server>
 
   <bean id="jaxbProvider" class="org.apache.cxf.jaxrs.provider.JAXBElementProvider">
-      <property name="schemas">
-        <list>
-           <value>classpath:/org/apache/cxf/systest/jaxrs/book.xsd</value>
-        </list>
-      </property>
+      <property name="schemaHandler" ref="schemaHolder"/>
+  </bean>
+  
+  <bean id="schemaHolder" class="org.apache.cxf.jaxrs.utils.schemas.SchemaHandler">
+      <property name="schemas" ref="theSchemas"/>
+  </bean>
+  
+  <util:list id="theSchemas">
+    <value>classpath:/org/apache/cxf/systest/jaxrs/bookid.xsd</value>
+    <value>classpath:/org/apache/cxf/systest/jaxrs/book.xsd</value>
+  </util:list>
+  
+  <util:map id="jsonNamespaceMap" map-class="java.util.Hashtable">
+       <entry key="http://www.example.org/books" value="b"/>
+  </util:map> 
+  
+  <util:list id="jsonKeys">
+    <value>Books</value>
+  </util:list>
+  
+  <bean id="jsonProvider" class="org.apache.cxf.jaxrs.provider.JSONProvider">
+      <property name="namespaceMap" ref="jsonNamespaceMap"/> 
+      <property name="schemaHandler" ref="schemaHolder"/>
+
+      <property name="serializeAsArray" value="true"/> 
+      <property name="arrayKeys" ref="jsonKeys"/>
   </bean>
 </beans>
 <!-- END SNIPPET: beans -->



Re: svn commit: r723397 - in /cxf/branches/2.1.x-fixes: ./ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/schemas/ rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ rt/...

Posted by Glen Mazza <gl...@verizon.net>.
On Thu, 2008-12-04 at 19:08 +0000, sergeyb@apache.org wrote:
> Author: sergeyb
> Date: Thu Dec  4 11:08:20 2008
> New Revision: 723397
>      
> 
> Modified: cxf/branches/2.1.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
> URL: http://svn.apache.org/viewvc/cxf/branches/2.1.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java?rev=723397&r1=723396&r2=723397&view=diff
> ==============================================================================
> --- cxf/branches/2.1.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java (original)
> +++ cxf/branches/2.1.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java Thu Dec  4 11:08:20 2008
> @@ -23,6 +23,7 @@
>  
>      public void writeTo(Object obj, Class<?> cls, Type genericType, Annotation[] anns,  
> @@ -103,8 +124,22 @@
>              }
>              Marshaller ms = createMarshaller(actualObject, actualClass, genericType, m);
>  
> -            XMLOutputFactory factory = new MappedXMLOutputFactory(namespaceMap);
> -            XMLStreamWriter xsw = factory.createXMLStreamWriter(os);            
> +            Configuration c = new Configuration(namespaceMap);
> +            MappedNamespaceConvention convention = new MappedNamespaceConvention(c);
> +            AbstractXMLStreamWriter xsw = new MappedXMLStreamWriter(
> +                                               convention, 
> +                                               new OutputStreamWriter(os, "UTF-8"));
> +            if (serializeAsArray) {
> +                if (arrayKeys != null) {
> +                    for (String key : arrayKeys) {
> +                        xsw.seriliazeAsArray(key);

serializeAsArray(key);