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.camel.component.cxf.interceptors;
018    
019    import org.apache.cxf.binding.soap.interceptor.SoapHeaderInterceptor;
020    import org.apache.cxf.interceptor.Fault;
021    import org.apache.cxf.message.Exchange;
022    import org.apache.cxf.message.Message;
023    import org.apache.cxf.phase.AbstractPhaseInterceptor;
024    import org.apache.cxf.phase.Phase;
025    import org.apache.cxf.service.model.BindingMessageInfo;
026    import org.apache.cxf.service.model.BindingOperationInfo;
027    import org.apache.cxf.service.model.MessageInfo;
028    import org.apache.cxf.service.model.MessagePartInfo;
029    
030    /**
031     * This interceptor traverses the {@link BindingOperationInfo} and  
032     * invokes the {@link MessagePartInfo#setTypeQName(javax.xml.namespace.QName)} method to set
033     * the service class to null.  The reason we may want to set the service class to null is 
034     * because CXF will try to use JAXB if the service class is present.  It affects DomSource
035     * payload to be processed correctly.
036     *  
037     * @version @Revision: 789534 $
038     */
039    public class RemoveClassTypeInterceptor extends AbstractPhaseInterceptor<Message> {
040    
041        public RemoveClassTypeInterceptor() {
042            super(Phase.UNMARSHAL);
043            addBefore(SoapHeaderInterceptor.class.getName());
044    
045        }
046    
047        public void handleMessage(Message message) throws Fault {
048            Exchange exchange = message.getExchange();
049            BindingOperationInfo bop = exchange.getBindingOperationInfo();
050            
051            if (bop == null) {
052                return;    
053            }
054            
055            if (bop.isUnwrapped()) {
056                bop = bop.getWrappedOperation();
057            }
058    
059            if (bop.isUnwrappedCapable()) {
060                removePartTypeClass(bop.getWrappedOperation().getOperationInfo().getInput());
061                removePartTypeClass(bop.getWrappedOperation().getOperationInfo().getOutput());
062                removePartTypeClass(bop.getWrappedOperation().getInput());
063                removePartTypeClass(bop.getWrappedOperation().getOutput());
064            } else {
065                removePartTypeClass(bop.getOperationInfo().getInput());
066                removePartTypeClass(bop.getOperationInfo().getOutput());
067                removePartTypeClass(bop.getInput());
068                removePartTypeClass(bop.getOutput());
069            }
070            
071        }
072    
073        protected void removePartTypeClass(BindingMessageInfo bmi) {
074            if (bmi != null) {
075                for (MessagePartInfo part : bmi.getMessageParts()) {
076                    part.setTypeClass(null);
077                }     
078            }
079        }
080    
081        protected void removePartTypeClass(MessageInfo msgInfo) {
082            if (msgInfo != null) {
083                for (MessagePartInfo part : msgInfo.getMessageParts()) {
084                    part.setTypeClass(null);
085                }     
086            }
087        }
088    
089    }