cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1091061 - in /cxf/trunk/rt/frontend/jaxrs/src: main/java/org/apache/cxf/jaxrs/provider/ test/java/org/apache/cxf/jaxrs/provider/
Date Mon, 11 Apr 2011 13:15:14 GMT
Author: sergeyb
Date: Mon Apr 11 13:15:14 2011
New Revision: 1091061

URL: http://svn.apache.org/viewvc?rev=1091061&view=rev
Log:
[JAX-RS] Minor updates to JAXB provides to enable the output validation

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/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.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=1091061&r1=1091060&r2=1091061&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
Mon Apr 11 13:15:14 2011
@@ -48,6 +48,7 @@ import javax.xml.bind.JAXBElement;
 import javax.xml.bind.JAXBException;
 import javax.xml.bind.Marshaller;
 import javax.xml.bind.Unmarshaller;
+import javax.xml.bind.ValidationEventHandler;
 import javax.xml.bind.annotation.XmlAnyElement;
 import javax.xml.bind.annotation.XmlRootElement;
 import javax.xml.bind.annotation.XmlType;
@@ -57,6 +58,8 @@ import javax.xml.stream.XMLStreamReader;
 import javax.xml.stream.XMLStreamWriter;
 import javax.xml.validation.Schema;
 
+import org.xml.sax.helpers.DefaultHandler;
+
 import org.apache.cxf.common.i18n.BundleUtils;
 import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.common.util.PackageUtils;
@@ -111,6 +114,14 @@ public abstract class AbstractJAXBProvid
     private boolean singleJaxbContext;
     private Class[] extraClass;
     
+    private boolean validateOutput;
+    private boolean validateBeforeWrite;
+    private ValidationEventHandler eventHandler;
+    
+    public void setValidationHandler(ValidationEventHandler handler) {
+        eventHandler = handler;
+    }
+    
     public void setSingleJaxbContext(boolean useSingleContext) {
         singleJaxbContext = useSingleContext;
     }
@@ -446,6 +457,9 @@ public abstract class AbstractJAXBProvid
         if (schema != null) {
             unmarshaller.setSchema(schema);
         }
+        if (eventHandler != null) {
+            unmarshaller.setEventHandler(eventHandler);
+        }
         if (uProperties != null) {
             for (Map.Entry<String, Object> entry : uProperties.entrySet()) {
                 unmarshaller.setProperty(entry.getKey(), entry.getValue());
@@ -465,9 +479,21 @@ public abstract class AbstractJAXBProvid
         if (enc != null) {
             marshaller.setProperty(Marshaller.JAXB_ENCODING, enc);
         }
+        validateObjectIfNeeded(marshaller, obj);
         return marshaller;
     }
     
+    protected void validateObjectIfNeeded(Marshaller marshaller, Object obj) 
+        throws JAXBException {
+        if (validateOutput && schema != null) {
+            marshaller.setEventHandler(eventHandler);
+            marshaller.setSchema(schema);
+            if (validateBeforeWrite) {
+                marshaller.marshal(obj, new DefaultHandler());
+                marshaller.setSchema(null);
+            }
+        }
+    }
         
     protected Class<?> getActualType(Class<?> type, Type genericType, Annotation[]
anns) {
         Class<?> theType = null;
@@ -599,6 +625,14 @@ public abstract class AbstractJAXBProvid
                                                             true);
     }
     
+    public void setValidateBeforeWrite(boolean validateBeforeWrite) {
+        this.validateBeforeWrite = validateBeforeWrite;
+    }
+
+    public void setValidateOutput(boolean validateOutput) {
+        this.validateOutput = validateOutput;
+    }
+
     @XmlRootElement
     protected static class CollectionWrapper {
         

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=1091061&r1=1091060&r2=1091061&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
Mon Apr 11 13:15:14 2011
@@ -44,7 +44,6 @@ 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.adapters.XmlJavaTypeAdapter;
 import javax.xml.namespace.QName;
 import javax.xml.stream.XMLInputFactory;
@@ -80,7 +79,6 @@ public class JAXBElementProvider extends
                                     Marshaller.JAXB_SCHEMA_LOCATION});
     
     private Map<String, Object> mProperties = Collections.emptyMap();
-    private ValidationEventHandler eventHandler;
     
     public JAXBElementProvider() {
         
@@ -98,10 +96,6 @@ public class JAXBElementProvider extends
         super.setContext(mc);
     }
     
-    public void setValidationHandler(ValidationEventHandler handler) {
-        eventHandler = handler;
-    }
-    
     public void setEnableBuffering(boolean enableBuf) {
         super.setEnableBuffering(enableBuf);
     }
@@ -142,9 +136,6 @@ public class JAXBElementProvider extends
             theType = getActualType(theType, genericType, anns);
 
             Unmarshaller unmarshaller = createUnmarshaller(theType, genericType, isCollection);
-            if (eventHandler != null) {
-                unmarshaller.setEventHandler(eventHandler);
-            }
             addAttachmentUnmarshaller(unmarshaller);
             Object response = null;
             if (JAXBElement.class.isAssignableFrom(type) 

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=1091061&r1=1091060&r2=1091061&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
Mon Apr 11 13:15:14 2011
@@ -1028,6 +1028,53 @@ public class JAXBElementProviderTest ext
         assertNotNull("schema can not be read from disk", s);
     }
     
+    @Test
+    public void testWriteWithValidation() throws Exception {
+        JAXBElementProvider provider = new JAXBElementProvider();
+        List<String> locations = new ArrayList<String>();
+        String loc = getClass().getClassLoader().getResource("book1.xsd").toURI().getPath();
+        locations.add(loc);
+        provider.setSchemas(locations);
+        Schema s = provider.getSchema();
+        assertNotNull("schema can not be read from disk", s);
+        
+        provider.setValidateOutput(true);
+        provider.setValidateBeforeWrite(true);
+        
+        Book2 book2 = new Book2();
+        
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        provider.writeTo(book2, Book2.class, Book2.class, new Annotation[]{}, MediaType.TEXT_XML_TYPE,
+                         new MetadataMap<String, Object>(), bos);
+        
+        assertTrue(bos.toString().contains("http://superbooks"));
+    }
+    
+    @Test
+    public void testWriteWithFailedValidation() throws Exception {
+        JAXBElementProvider provider = new JAXBElementProvider();
+        List<String> locations = new ArrayList<String>();
+        String loc = getClass().getClassLoader().getResource("test.xsd").toURI().getPath();
+        locations.add(loc);
+        provider.setSchemas(locations);
+        Schema s = provider.getSchema();
+        assertNotNull("schema can not be read from disk", s);
+        
+        provider.setValidateOutput(true);
+        provider.setValidateBeforeWrite(true);
+
+        try {
+            provider.writeTo(new Book2(), Book2.class, Book2.class, new Annotation[]{}, 
+                             MediaType.TEXT_XML_TYPE,
+                             new MetadataMap<String, Object>(), new ByteArrayOutputStream());
+            fail("Validation exception expected");
+        } catch (Exception ex) {
+            Throwable cause = ex.getCause();
+            assertTrue(cause.getMessage().contains("Cannot find the declaration of element"));
+        }
+        
+    }
+    
     @Test 
     public void testIsReadableWithJaxbIndex() {
         JAXBElementProvider p = new JAXBElementProvider();



Mime
View raw message