001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.servicemix.cxfbc.interceptors;
018    
019    import javax.xml.transform.Source;
020    import javax.xml.transform.dom.DOMSource;
021    import javax.xml.validation.Schema;
022    import org.w3c.dom.Element;
023    import org.apache.cxf.binding.soap.SoapMessage;
024    import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor;
025    import org.apache.cxf.interceptor.Fault;
026    import org.apache.cxf.service.Service;
027    import org.apache.cxf.service.model.ServiceModelUtil;
028    import org.apache.cxf.wsdl.EndpointReferenceUtils;
029    import org.apache.servicemix.jbi.jaxp.SourceTransformer;
030    import org.apache.servicemix.soap.util.DomUtil;
031    
032    
033    public abstract class AbstractSchemaValidationInterceptor extends
034            AbstractSoapInterceptor {
035        private boolean useJBIWrapper = true;
036        private boolean useSOAPEnvelope = true;
037        
038        public AbstractSchemaValidationInterceptor(String phase, boolean useJBIWrapper, boolean useSOAPEnvelope) {
039            super(phase);
040            this.useJBIWrapper = useJBIWrapper;
041            this.useSOAPEnvelope = useSOAPEnvelope;
042        }
043    
044        protected void validateMessage(SoapMessage message) throws Fault {
045            Service service = ServiceModelUtil.getService(message.getExchange());
046            if (service != null) {
047                Schema schema = EndpointReferenceUtils.getSchema(service.getServiceInfos().get(0));
048                if (schema != null) {
049                    javax.xml.validation.Validator validator = schema.newValidator();
050                    try {
051                        
052                        
053                        Element sourceMessage = new SourceTransformer().toDOMElement(message.getContent(Source.class));
054                        message.setContent(Source.class, new DOMSource(sourceMessage));
055                        if (!useJBIWrapper && !useSOAPEnvelope) {
056                            validator.validate(new DOMSource(sourceMessage));
057                        } else {
058                            Element partWrapper = DomUtil
059                                    .getFirstChildElement(sourceMessage);
060                            while (partWrapper != null) {
061                                Element partContent = DomUtil
062                                        .getFirstChildElement(partWrapper);
063                                validator.validate(new DOMSource(partContent));
064                                partWrapper = DomUtil
065                                        .getNextSiblingElement(partWrapper);
066                            }
067                        }
068                        
069                    } catch (Exception e) {
070                        throw new Fault(e);
071                    }
072                }
073            }
074        }
075        
076    }