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.transport;
018    
019    import java.io.IOException;
020    import java.util.Collection;
021    import java.util.HashSet;
022    import java.util.Set;
023    
024    import javax.annotation.PostConstruct;
025    import javax.annotation.Resource;
026    
027    import org.apache.camel.CamelContext;
028    import org.apache.camel.component.cxf.CxfHeaderFilterStrategy;
029    import org.apache.camel.spi.HeaderFilterStrategy;
030    import org.apache.cxf.Bus;
031    import org.apache.cxf.service.model.EndpointInfo;
032    import org.apache.cxf.transport.AbstractTransportFactory;
033    import org.apache.cxf.transport.Conduit;
034    import org.apache.cxf.transport.ConduitInitiator;
035    import org.apache.cxf.transport.ConduitInitiatorManager;
036    import org.apache.cxf.transport.Destination;
037    import org.apache.cxf.transport.DestinationFactory;
038    import org.apache.cxf.transport.DestinationFactoryManager;
039    import org.apache.cxf.ws.addressing.EndpointReferenceType;
040    
041    /**
042     * @version $Revision: 16195 $
043     */
044    public class CamelTransportFactory extends AbstractTransportFactory implements ConduitInitiator, DestinationFactory {
045    
046        public static final String TRANSPORT_ID = "http://cxf.apache.org/transports/camel";
047    
048        private static final Set<String> URI_PREFIXES = new HashSet<String>();
049    
050        private Collection<String> activationNamespaces;
051        
052        private HeaderFilterStrategy headerFilterStrategy;
053        
054        private boolean checkException;
055    
056        static {
057            URI_PREFIXES.add("camel://");        
058        }
059    
060        private Bus bus;
061        private CamelContext camelContext;
062        
063        public CamelTransportFactory() {
064            CxfHeaderFilterStrategy defaultHeaderFilterStrategy = new CxfHeaderFilterStrategy();
065            // Doesn't filter the camel relates headers by default
066            defaultHeaderFilterStrategy.setOutFilterPattern(null);
067            headerFilterStrategy = defaultHeaderFilterStrategy;
068        }
069    
070        @Resource(name = "bus")
071        public void setBus(Bus b) {
072            bus = b;
073        }
074    
075        public Bus getBus() {
076            return bus;
077        }
078    
079        public void setActivationNamespaces(Collection<String> ans) {
080            activationNamespaces = ans;
081        }
082    
083        public CamelContext getCamelContext() {
084            return camelContext;
085        }
086    
087        @Resource(name = "camelContext")
088        public void setCamelContext(CamelContext camelContext) {
089            this.camelContext = camelContext;
090        }
091        
092        public void setCheckException(boolean check) {
093            checkException = check;
094        }
095        
096        public boolean isCheckException() {
097            return checkException;
098        }
099    
100        public Conduit getConduit(EndpointInfo targetInfo) throws IOException {
101            return getConduit(targetInfo, null);
102        }
103    
104        public Conduit getConduit(EndpointInfo endpointInfo, EndpointReferenceType target) throws IOException {
105            return new CamelConduit(camelContext, bus, endpointInfo, target, headerFilterStrategy);
106        }
107    
108        public Destination getDestination(EndpointInfo endpointInfo) throws IOException {
109            return new CamelDestination(camelContext, bus, this, endpointInfo, headerFilterStrategy, checkException);
110        }
111    
112        public Set<String> getUriPrefixes() {
113            return URI_PREFIXES;
114        }
115    
116        @PostConstruct
117        void registerWithBindingManager() {
118            if (null == bus) {
119                return;
120            }
121            ConduitInitiatorManager cim = bus.getExtension(ConduitInitiatorManager.class);
122            if (null != cim && null != activationNamespaces) {
123                for (String ns : activationNamespaces) {
124                    cim.registerConduitInitiator(ns, this);
125                }
126            }
127            DestinationFactoryManager dfm = bus.getExtension(DestinationFactoryManager.class);
128            if (null != dfm && null != activationNamespaces) {
129                for (String ns : activationNamespaces) {
130                    dfm.registerDestinationFactory(ns, this);
131                }
132            }
133        }
134    
135        public HeaderFilterStrategy getHeaderFilterStrategy() {
136            return headerFilterStrategy;
137        }
138    
139        public void setHeaderFilterStrategy(HeaderFilterStrategy headerFilterStrategy) {
140            this.headerFilterStrategy = headerFilterStrategy;
141        }
142        
143        
144    }
145    
146