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    
018    package org.apache.camel.component.cxf;
019    
020    import java.util.Arrays;
021    import java.util.Iterator;
022    import java.util.List;
023    
024    import org.apache.camel.spi.HeaderFilterStrategy.Direction;
025    import org.apache.commons.logging.Log;
026    import org.apache.commons.logging.LogFactory;
027    import org.apache.cxf.binding.soap.SoapBindingConstants;
028    import org.apache.cxf.binding.soap.SoapBindingFactory;
029    import org.apache.cxf.binding.soap.SoapHeader;
030    import org.apache.cxf.binding.soap.SoapVersion;
031    import org.apache.cxf.binding.soap.SoapVersionFactory;
032    import org.apache.cxf.headers.Header;
033    
034    /**
035     * A {@link MessageHeaderFilter} to drop all SOAP headers.
036     *
037     * @version $Revision: 3094 $
038     */
039    public class SoapMessageHeaderFilter implements MessageHeaderFilter {
040        private static final Log LOG = LogFactory.getLog(SoapMessageHeaderFilter.class);
041    
042        private static final List<String> ACTIVATION_NS = 
043            Arrays.asList(SoapBindingConstants.SOAP11_BINDING_ID, 
044                          SoapBindingFactory.SOAP_11_BINDING, 
045                          SoapBindingFactory.SOAP_12_BINDING);
046        
047        public List<String> getActivationNamespaces() {
048            return ACTIVATION_NS;
049        }
050    
051        public void filter(Direction direction, List<Header> headers) {
052            // Treat both in and out direction the same
053            if (headers == null) {
054                return;
055            }
056            
057            Iterator<Header> iterator = headers.iterator();
058            while (iterator.hasNext()) {
059                Header header = iterator.next();
060                if (LOG.isTraceEnabled()) {
061                    LOG.trace("Processing header: " + header);
062                }
063                
064                if (!(header instanceof SoapHeader)) {
065                    if (LOG.isTraceEnabled()) {
066                        LOG.trace("Skipped header: " + header + " since it is not a SoapHeader");
067                    }
068                    continue;
069                }
070                
071                SoapHeader soapHeader = SoapHeader.class.cast(header);
072                for (Iterator<SoapVersion> itv = SoapVersionFactory.getInstance().getVersions(); itv.hasNext();) {
073                    SoapVersion version = itv.next();
074    
075                    if (soapHeader.getActor() != null 
076                        && soapHeader.getActor().equals(version.getNextRole())) {
077                        // dropping headers if actor/role equals to {ns}/role|actor/next
078                        // cxf SoapHeader needs to have soap:header@relay attribute, 
079                        // then we can check for it here as well
080                        if (LOG.isTraceEnabled()) {
081                            LOG.trace("Filtered header: " + header);
082                        }
083                        iterator.remove();
084                        break;
085                    }
086                }
087            }
088        }
089    
090    }