For the non-DOMSource cases, I think you need to buffer the stream as the 
validate will consume the stream.

Dan



On Aug 13, 2013, at 9:53 PM, ff...@apache.org wrote:

> Author: ffang
> Date: Wed Aug 14 01:53:02 2013
> New Revision: 1513711
> 
> URL: http://svn.apache.org/r1513711
> Log:
> [CXF-5169]move the out message schemavalidate to source databinding
> 
> Modified:
>    
> cxf/trunk/core/src/main/java/org/apache/cxf/databinding/source/XMLStreamDataWriter.java
>    
> cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/interceptors/MessageModeOutInterceptor.java
>    
> cxf/trunk/systests/databinding/src/test/java/org/apache/cxf/systest/source/ClientServerSourceTest.java
>    
> cxf/trunk/systests/databinding/src/test/java/org/apache/cxf/systest/source/GreeterImpl.java
> 
> Modified: 
> cxf/trunk/core/src/main/java/org/apache/cxf/databinding/source/XMLStreamDataWriter.java
> URL: 
> http://svn.apache.org/viewvc/cxf/trunk/core/src/main/java/org/apache/cxf/databinding/source/XMLStreamDataWriter.java?rev=1513711&r1=1513710&r2=1513711&view=diff
> ==============================================================================
> --- 
> cxf/trunk/core/src/main/java/org/apache/cxf/databinding/source/XMLStreamDataWriter.java
>  (original)
> +++ 
> cxf/trunk/core/src/main/java/org/apache/cxf/databinding/source/XMLStreamDataWriter.java
>  Wed Aug 14 01:53:02 2013
> @@ -28,12 +28,15 @@ import javax.xml.stream.XMLStreamReader;
> import javax.xml.stream.XMLStreamWriter;
> import javax.xml.transform.Source;
> import javax.xml.transform.dom.DOMSource;
> +import javax.xml.transform.stream.StreamSource;
> import javax.xml.validation.Schema;
> 
> import org.w3c.dom.Document;
> import org.w3c.dom.DocumentFragment;
> import org.w3c.dom.Node;
> 
> +import org.xml.sax.SAXException;
> +
> import org.apache.cxf.common.i18n.Message;
> import org.apache.cxf.common.logging.LogUtils;
> import org.apache.cxf.databinding.DataWriter;
> @@ -46,6 +49,8 @@ import org.apache.cxf.staxutils.W3CDOMSt
> public class XMLStreamDataWriter implements DataWriter<XMLStreamWriter> {
>     private static final Logger LOG = 
> LogUtils.getL7dLogger(XMLStreamDataWriter.class);
> 
> +    private Schema schema;
> +    
>     public void write(Object obj, MessagePartInfo part, XMLStreamWriter 
> output) {
>         write(obj, output);
>     }
> @@ -55,14 +60,24 @@ public class XMLStreamDataWriter impleme
>             XMLStreamReader reader = null;
>             if (obj instanceof DataSource) {
>                 DataSource ds = (DataSource)obj;
> +                if (schema != null) {
> +                    StreamSource ss = new StreamSource(ds.getInputStream());
> +                    schema.newValidator().validate(ss);
> +                }
>                 reader = StaxUtils.createXMLStreamReader(ds.getInputStream());
>                 StaxUtils.copy(reader, writer);
>                 reader.close();
>             } else if (obj instanceof Node) {
> +                if (schema != null) {
> +                    schema.newValidator().validate(new DOMSource((Node)obj));
> +                }
>                 Node nd = (Node)obj;
>                 writeNode(nd, writer);
>             } else {
>                 Source s = (Source) obj;
> +                if (schema != null) {
> +                    schema.newValidator().validate(s);
> +                }
>                 if (s instanceof DOMSource
>                     && ((DOMSource) s).getNode() == null) {
>                     return;
> @@ -74,6 +89,9 @@ public class XMLStreamDataWriter impleme
>                             e.getClass().getCanonicalName(), e.getMessage());
>         } catch (IOException e) {
>             throw new Fault(new Message("COULD_NOT_WRITE_XML_STREAM", LOG), 
> e);
> +        } catch (SAXException e) {
> +            throw new Fault("COULD_NOT_WRITE_XML_STREAM_CAUSED_BY", LOG, e,
> +                            e.getClass().getCanonicalName(), e.getMessage());
>         }
>     }
> 
> @@ -119,6 +137,7 @@ public class XMLStreamDataWriter impleme
>     }
> 
>     public void setSchema(Schema s) {
> +        this.schema = s;
>     }
> 
>     public void setAttachments(Collection<Attachment> attachments) {
> 
> Modified: 
> cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/interceptors/MessageModeOutInterceptor.java
> URL: 
> http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/interceptors/MessageModeOutInterceptor.java?rev=1513711&r1=1513710&r2=1513711&view=diff
> ==============================================================================
> --- 
> cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/interceptors/MessageModeOutInterceptor.java
>  (original)
> +++ 
> cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/interceptors/MessageModeOutInterceptor.java
>  Wed Aug 14 01:53:02 2013
> @@ -34,15 +34,11 @@ import javax.xml.soap.SOAPPart;
> import javax.xml.stream.XMLStreamException;
> import javax.xml.stream.XMLStreamWriter;
> import javax.xml.transform.Source;
> -import javax.xml.transform.dom.DOMSource;
> -import javax.xml.validation.Schema;
> 
> import org.w3c.dom.DocumentFragment;
> import org.w3c.dom.Node;
> 
> -import org.xml.sax.SAXException;
> 
> -import org.apache.cxf.annotations.SchemaValidation.SchemaValidationType;
> import org.apache.cxf.attachment.AttachmentDeserializer;
> import org.apache.cxf.binding.soap.Soap12;
> import org.apache.cxf.binding.soap.SoapFault;
> @@ -53,7 +49,6 @@ import org.apache.cxf.binding.soap.saaj.
> import org.apache.cxf.binding.soap.saaj.SAAJStreamWriter;
> import org.apache.cxf.binding.soap.saaj.SAAJUtils;
> import org.apache.cxf.helpers.IOUtils;
> -import org.apache.cxf.helpers.ServiceUtils;
> import org.apache.cxf.interceptor.AbstractOutDatabindingInterceptor;
> import org.apache.cxf.interceptor.Fault;
> import org.apache.cxf.interceptor.StaxOutInterceptor;
> @@ -64,13 +59,10 @@ import org.apache.cxf.message.MessageImp
> import org.apache.cxf.message.MessageUtils;
> import org.apache.cxf.phase.AbstractPhaseInterceptor;
> import org.apache.cxf.phase.Phase;
> -import org.apache.cxf.service.Service;
> import org.apache.cxf.service.model.BindingMessageInfo;
> import org.apache.cxf.service.model.BindingOperationInfo;
> -import org.apache.cxf.service.model.ServiceModelUtil;
> import org.apache.cxf.staxutils.StaxUtils;
> import org.apache.cxf.staxutils.W3CDOMStreamWriter;
> -import org.apache.cxf.ws.addressing.EndpointReferenceUtils;
> 
> public class MessageModeOutInterceptor extends 
> AbstractPhaseInterceptor<Message> {
>     MessageModeOutInterceptorInternal internal;
> @@ -90,7 +82,6 @@ public class MessageModeOutInterceptor e
>         this.bindingName = bname;
>     }
>     public void handleMessage(Message message) throws Fault {
> -        checkSchemaValidation(message);
>         BindingOperationInfo bop = 
> message.getExchange().get(BindingOperationInfo.class);
>         if (bop != null && !bindingName.equals(bop.getBinding().getName())) {
>             return;
> @@ -303,50 +294,6 @@ public class MessageModeOutInterceptor e
>         }
>     }
> 
> -    private void checkSchemaValidation(Message message) {
> -        
> -        if (ServiceUtils.isSchemaValidationEnabled(SchemaValidationType.OUT, 
> message)) {
> -            Service service = 
> ServiceModelUtil.getService(message.getExchange());
> -            if (service == null) {
> -                return;
> -            }
> -            Schema schema = 
> EndpointReferenceUtils.getSchema(service.getServiceInfos().get(0),
> -                            message.getExchange().getBus());
> -            if (schema == null) {
> -                return;
> -            }
> -            MessageContentsList list = 
> (MessageContentsList)message.getContent(List.class);
> -            if (list == null || list.isEmpty()) {
> -                return;
> -            }
> -            Object o = list.get(0);
> -            SOAPMessage soapMessage = null;
> -            
> -            if (o instanceof SOAPMessage) {
> -                soapMessage = (SOAPMessage)o;
> -                
> -                try {
> -                    DOMSource source = new 
> DOMSource(soapMessage.getSOAPBody().getFirstChild());
> -                    schema.newValidator().validate(source);
> -                } catch (SAXException e) {
> -                    throw new Fault(e);
> -                } catch (IOException e) {
> -                    throw new Fault(e);
> -                } catch (SOAPException e) {
> -                    throw new Fault(e);
> -                }
> -            } else if (o instanceof Source) {
> -                Source source = (Source)o;
> -                try {
> -                    schema.newValidator().validate(source);
> -                } catch (SAXException e) {
> -                    throw new Fault(e);
> -                } catch (IOException e) {
> -                    throw new Fault(e);
> -                }
> -            }
> -        }
> -        
> -    }
> +    
> 
> }
> 
> Modified: 
> cxf/trunk/systests/databinding/src/test/java/org/apache/cxf/systest/source/ClientServerSourceTest.java
> URL: 
> http://svn.apache.org/viewvc/cxf/trunk/systests/databinding/src/test/java/org/apache/cxf/systest/source/ClientServerSourceTest.java?rev=1513711&r1=1513710&r2=1513711&view=diff
> ==============================================================================
> --- 
> cxf/trunk/systests/databinding/src/test/java/org/apache/cxf/systest/source/ClientServerSourceTest.java
>  (original)
> +++ 
> cxf/trunk/systests/databinding/src/test/java/org/apache/cxf/systest/source/ClientServerSourceTest.java
>  Wed Aug 14 01:53:02 2013
> @@ -87,7 +87,7 @@ public class ClientServerSourceTest exte
>         DOMSource ds = new DOMSource(doc);
>         DOMSource resp = port.sayHi(ds);
>         assertEquals("We should get the right response", "Bonjour", 
> -                     DOMUtils.getContent(getElement(resp.getNode())));
> +                     
> DOMUtils.getContent(getElement(resp.getNode().getFirstChild().getFirstChild())));
> 
>         doc = DOMUtils.newDocument();
>         Element el = 
> doc.createElementNS("http://apache.org/hello_world_soap_http_source/source/types";,
> 
> Modified: 
> cxf/trunk/systests/databinding/src/test/java/org/apache/cxf/systest/source/GreeterImpl.java
> URL: 
> http://svn.apache.org/viewvc/cxf/trunk/systests/databinding/src/test/java/org/apache/cxf/systest/source/GreeterImpl.java?rev=1513711&r1=1513710&r2=1513711&view=diff
> ==============================================================================
> --- 
> cxf/trunk/systests/databinding/src/test/java/org/apache/cxf/systest/source/GreeterImpl.java
>  (original)
> +++ 
> cxf/trunk/systests/databinding/src/test/java/org/apache/cxf/systest/source/GreeterImpl.java
>  Wed Aug 14 01:53:02 2013
> @@ -44,9 +44,11 @@ public class GreeterImpl implements Gree
>         Document doc = DOMUtils.newDocument();
>         Element el = 
> doc.createElementNS("http://apache.org/hello_world_soap_http_source/source/types";,
>             "ns1:sayHiResponse");
> -        el.appendChild(doc.createTextNode("Bonjour"));
> +        Element el2 = 
> doc.createElementNS("http://apache.org/hello_world_soap_http_source/source/types";,
> +            "ns1:responseType");
> +        el2.appendChild(doc.createTextNode("Bonjour"));
> +        el.appendChild(el2);
>         doc.appendChild(el);
> -        
>         return new DOMSource(doc);
>     }
>     private Element getElement(Node nd) {
> 
> 

-- 
Daniel Kulp
dk...@apache.org - http://dankulp.com/blog
Talend Community Coder - http://coders.talend.com

Reply via email to