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.cxfbean;
018    
019    import java.io.IOException;
020    
021    import org.apache.camel.Exchange;
022    import org.apache.camel.Processor;
023    import org.apache.camel.component.cxf.CxfConstants;
024    import org.apache.camel.component.cxf.transport.CamelDestination;
025    import org.apache.commons.logging.Log;
026    import org.apache.commons.logging.LogFactory;
027    import org.apache.cxf.Bus;
028    import org.apache.cxf.message.Message;
029    import org.apache.cxf.message.MessageImpl;
030    import org.apache.cxf.service.model.EndpointInfo;
031    import org.apache.cxf.transport.ConduitInitiator;
032    
033    /**
034     * A CXF transport {@link org.apache.cxf.transport.Destination} that listens 
035     * Camel {@link Exchange} from an associated {@link CxfBeanEndpoint}.
036     *  
037     * @version $Revision: 13809 $
038     */
039    public class CxfBeanDestination extends CamelDestination implements Processor {
040        private static final Log LOG = LogFactory.getLog(CxfBeanDestination.class);
041        private CxfBeanComponent cxfBeanComponent;
042        private CxfBeanEndpoint endpoint;
043    
044        public CxfBeanDestination(CxfBeanComponent cxfBeanComponent, Bus bus,
045                ConduitInitiator conduitInitiator,
046                EndpointInfo endpointInfo) throws IOException {
047            super(null, bus, conduitInitiator, endpointInfo);
048            this.cxfBeanComponent = cxfBeanComponent;
049        }
050    
051        @Override
052        public void activate() {
053            if (LOG.isDebugEnabled()) {
054                LOG.debug("Activating CxfBeanDestination " + getCamelDestinationUri());
055            }
056    
057            endpoint = cxfBeanComponent.getEndpoint(getCamelDestinationUri());
058            
059            if (endpoint == null) {
060                LOG.error("Failed to find endpoint " + getCamelDestinationUri());
061                return;
062            }
063                
064            endpoint.setProcessor(this);
065        }
066    
067        @Override
068        public void deactivate() {
069        }
070    
071        public void process(Exchange camelExchange) throws Exception {
072            if (LOG.isTraceEnabled()) {
073                LOG.trace("Received request : " + camelExchange);
074            }
075            
076            org.apache.cxf.message.Message cxfMessage = 
077                endpoint.getCxfBeanBinding().createCxfMessageFromCamelExchange(camelExchange,
078                        endpoint.getHeaderFilterStrategy());
079                          
080            cxfMessage.put(CxfConstants.CAMEL_EXCHANGE, camelExchange);
081            ((MessageImpl)cxfMessage).setDestination(this);
082    
083            // Handling the incoming message
084            // The response message will be send back by the outgoing chain
085            incomingObserver.onMessage(cxfMessage);
086        }
087        
088        @Override
089        protected void propagateResponseHeadersToCamel(Message outMessage, Exchange camelExchange) {
090            endpoint.getCxfBeanBinding().propagateResponseHeadersToCamel(outMessage, camelExchange,
091                                                                         endpoint.getHeaderFilterStrategy());
092        }
093    
094    }