cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1389791 - in /cxf/trunk: 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/ systests/jaxrs/src/t...
Date Tue, 25 Sep 2012 10:58:09 GMT
Author: sergeyb
Date: Tue Sep 25 10:58:08 2012
New Revision: 1389791

URL: http://svn.apache.org/viewvc?rev=1389791&view=rev
Log:
[CXF-4519] Adding an option to set type-specific schemas on JAXB providers

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/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/schemas/SchemaHandler.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java
    cxf/trunk/systests/jaxrs/src/test/resources/jaxrs/WEB-INF/beans.xml

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=1389791&r1=1389790&r2=1389791&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 Sep 25 10:58:08 2012
@@ -111,8 +111,11 @@ public abstract class AbstractJAXBProvid
     private Map<Class<?>, JAXBContext> classContexts = new HashMap<Class<?>,
JAXBContext>();
     
     private MessageContext mc;
+    
     private Schema schema;
     private String catalogLocation;
+    private Map<String, SchemaHandler> schemaHandlers;
+    
     private String collectionWrapperName;
     private Map<String, String> collectionWrapperMap;
     private List<String> jaxbElementClassNames = Collections.emptyList();
@@ -370,6 +373,11 @@ public abstract class AbstractJAXBProvid
         return unmarshalAsJaxbElement && type != Response.class;
     }
     
+    @Deprecated
+    public void setSchemas(List<String> locations) {
+        setSchemaLocations(locations);
+    }
+    
     public void setSchemaLocations(List<String> locations) {
         schema = SchemaHandler.createSchema(locations, catalogLocation, getBus());    
     }
@@ -378,7 +386,15 @@ public abstract class AbstractJAXBProvid
         this.catalogLocation = name;
     }
     
-    public void setSchema(Schema s) {
+    public void setSchemaHandler(SchemaHandler handler) {
+        setSchema(handler.getSchema());
+    }
+    
+    public void setSchemaHandlers(Map<String, SchemaHandler> handlers) {
+        schemaHandlers = handlers;
+    }
+    
+    protected void setSchema(Schema s) {
         schema = s;    
     }
     
@@ -490,8 +506,9 @@ public abstract class AbstractJAXBProvid
         JAXBContext context = isCollection ? getCollectionContext(cls) 
                                            : getJAXBContext(cls, genericType);
         Unmarshaller unmarshaller = context.createUnmarshaller();
-        if (schema != null) {
-            unmarshaller.setSchema(schema);
+        Schema theSchema = getSchema(cls);
+        if (theSchema != null) {
+            unmarshaller.setSchema(theSchema);
         }
         if (eventHandler != null) {
             unmarshaller.setEventHandler(eventHandler);
@@ -521,18 +538,21 @@ public abstract class AbstractJAXBProvid
         if (marshallerListener != null) {
             marshaller.setListener(marshallerListener);
         }
-        validateObjectIfNeeded(marshaller, obj);
+        validateObjectIfNeeded(marshaller, cls, obj);
         return marshaller;
     }
     
-    protected void validateObjectIfNeeded(Marshaller marshaller, Object obj) 
+    protected void validateObjectIfNeeded(Marshaller marshaller, Class<?> cls, Object
obj) 
         throws JAXBException {
-        if (validateOutput && schema != null) {
-            marshaller.setEventHandler(eventHandler);
-            marshaller.setSchema(schema);
-            if (validateBeforeWrite) {
-                marshaller.marshal(obj, new DefaultHandler());
-                marshaller.setSchema(null);
+        if (validateOutput) {
+            Schema theSchema = getSchema(cls);
+            if (theSchema != null) {
+                marshaller.setEventHandler(eventHandler);
+                marshaller.setSchema(theSchema);
+                if (validateBeforeWrite) {
+                    marshaller.marshal(obj, new DefaultHandler());
+                    marshaller.setSchema(null);
+                }
             }
         }
     }
@@ -556,7 +576,21 @@ public abstract class AbstractJAXBProvid
     }
     
     protected Schema getSchema() {
-        return schema;
+        return getSchema(null);
+    }
+    
+    protected Schema getSchema(Class<?> cls) {
+        // deal with the typical default case first
+        if (schema == null && schemaHandlers == null) {
+            return null;
+        }
+        
+        if (schema != null) {
+            return schema;
+        } else {
+            SchemaHandler handler = schemaHandlers.get(cls.getName());
+            return handler != null ? handler.getSchema() : null; 
+        }
     }
 
     

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=1389791&r1=1389790&r2=1389791&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 Sep 25 10:58:08 2012
@@ -66,7 +66,6 @@ import org.apache.cxf.jaxrs.utils.Annota
 import org.apache.cxf.jaxrs.utils.HttpUtils;
 import org.apache.cxf.jaxrs.utils.InjectionUtils;
 import org.apache.cxf.jaxrs.utils.JAXBUtils;
-import org.apache.cxf.jaxrs.utils.schemas.SchemaHandler;
 import org.apache.cxf.message.Attachment;
 import org.apache.cxf.message.Message;
 import org.apache.cxf.staxutils.DepthExceededStaxException;
@@ -131,14 +130,6 @@ public class JAXBElementProvider<T> exte
         super.setProduceMediaTypes(types);
     }
     
-    public void setSchemas(List<String> locations) {
-        super.setSchemaLocations(locations);
-    }
-    
-    public void setSchemaHandler(SchemaHandler handler) {
-        super.setSchema(handler.getSchema());
-    }
-    
     public void setMarshallerProperties(Map<String, Object> marshallProperties) {
         mProperties = marshallProperties;
     }

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java?rev=1389791&r1=1389790&r2=1389791&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
Tue Sep 25 10:58:08 2012
@@ -1110,11 +1110,11 @@ public final class ProviderFactory {
         boolean schemasMethodAvailable = false;
         for (ProviderInfo<MessageBodyReader<?>> r : messageReaders) {
             schemasMethodAvailable = 
-                injectProviderProperty(r.getProvider(), "setSchemas", List.class, schemas);
+                injectProviderProperty(r.getProvider(), "setSchemaLocations", List.class,
schemas);
         }
         if (!schemasMethodAvailable) {
             for (ProviderInfo<MessageBodyReader<?>> r : jaxbReaders) {
-                injectProviderProperty(r.getProvider(), "setSchemas", List.class, schemas);
+                injectProviderProperty(r.getProvider(), "setSchemaLocations", List.class,
schemas);
             }
         }
     }

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/schemas/SchemaHandler.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/schemas/SchemaHandler.java?rev=1389791&r1=1389790&r2=1389791&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/schemas/SchemaHandler.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/schemas/SchemaHandler.java
Tue Sep 25 10:58:08 2012
@@ -63,7 +63,12 @@ public class SchemaHandler {
         bus = b;
     }
     
+    @Deprecated
     public void setSchemas(List<String> locations) {
+        setSchemaLocations(locations);
+    }
+    
+    public void setSchemaLocations(List<String> locations) {
         schema = createSchema(locations, catalogLocation, 
                               bus == null ? BusFactory.getThreadDefaultBus() : bus);
     }

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=1389791&r1=1389790&r2=1389791&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
Tue Sep 25 10:58:08 2012
@@ -1169,7 +1169,7 @@ public class JAXBElementProviderTest ext
         JAXBElementProvider<?> provider = new JAXBElementProvider<Object>();
         List<String> locations = new ArrayList<String>();
         locations.add("classpath:/test.xsd");
-        provider.setSchemas(locations);
+        provider.setSchemaLocations(locations);
         Schema s = provider.getSchema();
         assertNotNull("schema can not be read from classpath", s);
     }
@@ -1181,7 +1181,7 @@ public class JAXBElementProviderTest ext
         String loc = getClass().getClassLoader().getResource("test.xsd").toURI().getPath();
         
         locations.add("file:" + loc);
-        provider.setSchemas(locations);
+        provider.setSchemaLocations(locations);
         Schema s = provider.getSchema();
         assertNotNull("schema can not be read from disk", s);
     }
@@ -1192,7 +1192,7 @@ public class JAXBElementProviderTest ext
         List<String> locations = new ArrayList<String>();
         String loc = getClass().getClassLoader().getResource("book1.xsd").toURI().getPath();
         locations.add(loc);
-        provider.setSchemas(locations);
+        provider.setSchemaLocations(locations);
         Schema s = provider.getSchema();
         assertNotNull("schema can not be read from disk", s);
         
@@ -1214,7 +1214,7 @@ public class JAXBElementProviderTest ext
         List<String> locations = new ArrayList<String>();
         String loc = getClass().getClassLoader().getResource("test.xsd").toURI().getPath();
         locations.add("file:" + loc);
-        provider.setSchemas(locations);
+        provider.setSchemaLocations(locations);
         Schema s = provider.getSchema();
         assertNotNull("schema can not be read from disk", s);
         

Modified: cxf/trunk/systests/jaxrs/src/test/resources/jaxrs/WEB-INF/beans.xml
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/resources/jaxrs/WEB-INF/beans.xml?rev=1389791&r1=1389790&r2=1389791&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/resources/jaxrs/WEB-INF/beans.xml (original)
+++ cxf/trunk/systests/jaxrs/src/test/resources/jaxrs/WEB-INF/beans.xml Tue Sep 25 10:58:08
2012
@@ -265,7 +265,7 @@ http://cxf.apache.org/schemas/core.xsd">
   </bean>
   
   <bean id="schemaHolder" class="org.apache.cxf.jaxrs.utils.schemas.SchemaHandler">
-      <property name="schemas" ref="theSchemas"/>
+      <property name="schemaLocations" ref="theSchemas"/>
   </bean>
   
   <util:list id="theSchemas">



Mime
View raw message