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.impl;
018    
019    
020    import org.apache.camel.CamelContext;
021    import org.apache.camel.ConsumerTemplate;
022    import org.apache.camel.Endpoint;
023    import org.apache.camel.Exchange;
024    import org.apache.camel.util.CamelContextHelper;
025    
026    import static org.apache.camel.util.ObjectHelper.wrapRuntimeCamelException;
027    
028    /**
029     * @version $Revision: 816068 $
030     */
031    public class DefaultConsumerTemplate implements ConsumerTemplate {
032    
033        private final CamelContext context;
034        private final ConsumerCache consumerCache = new ConsumerCache();
035    
036        public DefaultConsumerTemplate(CamelContext context) {
037            this.context = context;
038        }
039    
040        public void start() throws Exception {
041            consumerCache.start();
042        }
043    
044        public void stop() throws Exception {
045            consumerCache.stop();
046        }
047        
048        public CamelContext getCamelContext() {
049            return context;
050        }
051    
052        public Exchange receive(String endpointUri) {
053            Endpoint endpoint = resolveMandatoryEndpoint(endpointUri);
054            return consumerCache.receive(endpoint);
055        }
056    
057        public Exchange receive(Endpoint endpoinit) {
058            return receive(endpoinit.getEndpointUri());
059        }
060    
061        public Exchange receive(String endpointUri, long timeout) {
062            Endpoint endpoint = resolveMandatoryEndpoint(endpointUri);
063            return consumerCache.receive(endpoint, timeout);
064        }
065    
066        public Exchange receive(Endpoint endpoint, long timeout) {
067            return receive(endpoint.getEndpointUri(), timeout);
068        }
069    
070        public Exchange receiveNoWait(String endpointUri) {
071            Endpoint endpoint = resolveMandatoryEndpoint(endpointUri);
072            return consumerCache.receiveNoWait(endpoint);
073        }
074    
075        public Exchange receiveNoWait(Endpoint endpoint) {
076            return receiveNoWait(endpoint.getEndpointUri());
077        }
078    
079        public Object receiveBody(String endpointUri) {
080            Exchange exchange = receive(endpointUri);
081            return extractResultBody(exchange);
082        }
083    
084        public Object receiveBody(Endpoint endpoint) {
085            return receiveBody(endpoint.getEndpointUri());
086        }
087    
088        public Object receiveBody(String endpointUri, long timeout) {
089            Exchange exchange = receive(endpointUri, timeout);
090            return extractResultBody(exchange);
091        }
092    
093        public Object receiveBody(Endpoint endpoint, long timeout) {
094            return receiveBody(endpoint.getEndpointUri(), timeout);
095        }
096    
097        public Object receiveBodyNoWait(String endpointUri) {
098            Exchange exchange = receiveNoWait(endpointUri);
099            return extractResultBody(exchange);
100        }
101    
102        public Object receiveBodyNoWait(Endpoint endpoint) {
103            return receiveBodyNoWait(endpoint.getEndpointUri());
104        }
105    
106        public <T> T receiveBody(String endpointUri, Class<T> type) {
107            Object body = receiveBody(endpointUri);
108            return context.getTypeConverter().convertTo(type, body);
109        }
110    
111        public <T> T receiveBody(Endpoint endpoint, Class<T> type) {
112            return receiveBody(endpoint.getEndpointUri(), type);
113        }
114    
115        public <T> T receiveBody(String endpointUri, long timeout, Class<T> type) {
116            Object body = receiveBody(endpointUri, timeout);
117            return context.getTypeConverter().convertTo(type, body);
118        }
119    
120        public <T> T receiveBody(Endpoint endpoint, long timeout, Class<T> type) {
121            return receiveBody(endpoint.getEndpointUri(), timeout, type);
122        }
123    
124        public <T> T receiveBodyNoWait(String endpointUri, Class<T> type) {
125            Object body = receiveBodyNoWait(endpointUri);
126            return context.getTypeConverter().convertTo(type, body);
127        }
128    
129        public <T> T receiveBodyNoWait(Endpoint endpoint, Class<T> type) {
130            return receiveBodyNoWait(endpoint.getEndpointUri(), type);
131        }
132    
133        protected Endpoint resolveMandatoryEndpoint(String endpointUri) {
134            return CamelContextHelper.getMandatoryEndpoint(context, endpointUri);
135        }
136    
137        /**
138         * Extracts the body from the given result.
139         * <p/>
140         * If the exchange pattern is provided it will try to honor it and retrive the body
141         * from either IN or OUT according to the pattern.
142         *
143         * @param result   the result
144         * @return  the result, can be <tt>null</tt>.
145         */
146        protected Object extractResultBody(Exchange result) {
147            Object answer = null;
148            if (result != null) {
149                // rethrow if there was an exception
150                if (result.getException() != null) {
151                    throw wrapRuntimeCamelException(result.getException());
152                }
153    
154                // okay no fault then return the response
155                if (result.hasOut()) {
156                    // use OUT as the response
157                    answer = result.getOut().getBody();
158                } else {
159                    // use IN as the response
160                    answer = result.getIn().getBody();
161                }
162            }
163            return answer;
164        }
165    }