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
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 import org.w3c.dom.Element;
032
033
034 public abstract class AbstractSchemaValidationInterceptor extends
035 AbstractSoapInterceptor {
036 private boolean useJBIWrapper = true;
037 private boolean useSOAPEnvelope = true;
038
039 public AbstractSchemaValidationInterceptor(String phase, boolean useJBIWrapper, boolean useSOAPEnvelope) {
040 super(phase);
041 this.useJBIWrapper = useJBIWrapper;
042 this.useSOAPEnvelope = useSOAPEnvelope;
043 }
044
045 protected void validateMessage(SoapMessage message) throws Fault {
046 Service service = ServiceModelUtil.getService(message.getExchange());
047 if (service != null) {
048 Schema schema = EndpointReferenceUtils.getSchema(service.getServiceInfos().get(0));
049 if (schema != null) {
050 javax.xml.validation.Validator validator = schema.newValidator();
051 try {
052 Element sourceMessage = new SourceTransformer().toDOMElement(message.getContent(Source.class));
053 message.setContent(Source.class, new DOMSource(sourceMessage));
054 if (!useJBIWrapper && !useSOAPEnvelope) {
055 validator.validate(new DOMSource(sourceMessage));
056 } else {
057 Element partWrapper = DomUtil
058 .getFirstChildElement(sourceMessage);
059 while (partWrapper != null) {
060 Element partContent = DomUtil
061 .getFirstChildElement(partWrapper);
062 validator.validate(new DOMSource(partContent));
063 partWrapper = DomUtil
064 .getNextSiblingElement(partWrapper);
065 }
066 }
067
068 } catch (Exception e) {
069 throw new Fault(e);
070 }
071 }
072 }
073 }
074
075 }