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;
018    
019    import java.util.Map;
020    
021    import org.apache.camel.spi.Synchronization;
022    import org.apache.camel.spi.UnitOfWork;
023    
024    /**
025     * The base message exchange interface providing access to the request, response
026     * and fault {@link Message} instances. Different providers such as JMS, JBI,
027     * CXF and HTTP can provide their own derived API to expose the underlying
028     * transport semantics to avoid the leaky abstractions of generic APIs.
029     *
030     * @version $Revision: 905594 $
031     */
032    public interface Exchange {
033    
034        String ACCEPT_CONTENT_TYPE = "CamelAcceptContentType";
035        
036        String AGGREGATED_INDEX = "CamelAggregatedIndex";
037        String AGGREGATED_SIZE  = "CamelAggregatedSize";
038    
039        String ASYNC_WAIT = "CamelAsyncWait";
040    
041        String BATCH_INDEX    = "CamelBatchIndex";
042        String BATCH_SIZE     = "CamelBatchSize";
043        String BATCH_COMPLETE = "CamelBatchComplete";
044    
045        String BEAN_METHOD_NAME           = "CamelBeanMethodName";
046        String BEAN_MULTI_PARAMETER_ARRAY = "CamelBeanMultiParameterArray";
047    
048        String BINDING = "CamelBinding";
049    
050        String CHARSET_NAME     = "CamelCharsetName";
051        String CONTENT_ENCODING = "Content-Encoding";
052        String CONTENT_TYPE     = "Content-Type";
053        String CORRELATION_ID   = "CamelCorrelationId";
054    
055        String DATASET_INDEX = "CamelDataSetIndex";
056    
057        String EXCEPTION_CAUGHT     = "CamelExceptionCaught";
058        String ERRORHANDLER_HANDLED = "CamelErrorHandlerHandled";
059        String FAILURE_HANDLED      = "CamelFailureHandled";
060        String FAILURE_ENDPOINT     = "CamelFailureEndpoint";
061        
062        String FILTER_NON_XML_CHARS = "CamelFilterNonXmlChars";
063    
064        String FILE_LOCAL_WORK_PATH = "CamelFileLocalWorkPath";
065        String FILE_NAME            = "CamelFileName";
066        String FILE_NAME_ONLY       = "CamelFileNameOnly";
067        String FILE_NAME_PRODUCED   = "CamelFileNameProduced";
068        String FILE_PATH            = "CamelFilePath";
069        String FILE_PARENT          = "CamelFileParent";
070        String FILE_LAST_MODIFIED   = "CamelFileLastModified";
071    
072        String GROUPED_EXCHANGE = "CamelGroupedExchange";
073        
074        String HTTP_BASE_URI           = "CamelHttpBaseUri";
075        String HTTP_CHARACTER_ENCODING = "CamelHttpCharacterEncoding";
076        String HTTP_METHOD             = "CamelHttpMethod";
077        String HTTP_PATH               = "CamelHttpPath";
078        String HTTP_QUERY              = "CamelHttpQuery";
079        String HTTP_RESPONSE_CODE      = "CamelHttpResponseCode";
080        String HTTP_URI                = "CamelHttpUri";
081        String HTTP_URL                = "CamelHttpUrl";
082        String HTTP_CHUNKED            = "CamelHttpChunked";
083    
084        String INTERCEPTED_ENDPOINT = "CamelInterceptedEndpoint";
085        String TO_ENDPOINT          = "CamelToEndpoint";
086    
087        String LOG_DEBUG_BODY_MAX_CHARS = "CamelLogDebugBodyMaxChars";
088        String LOG_DEBUG_BODY_STREAMS   = "CamelLogDebugStreams";
089        String LOOP_INDEX               = "CamelLoopIndex";
090        String LOOP_SIZE                = "CamelLoopSize";
091    
092        String MULTICAST_INDEX = "CamelMulticastIndex";
093    
094        String ON_COMPLETION = "CamelOnCompletion";
095    
096        String ROUTE_STOP         = "CamelRouteStop";
097        String REDELIVERED        = "CamelRedelivered";
098        String REDELIVERY_COUNTER = "CamelRedeliveryCounter";
099        String REDELIVERY_EXHAUSTED = "CamelRedeliveryExhausted";
100        String ROLLBACK_ONLY      = "CamelRollbackOnly";
101        String ROLLBACK_ONLY_LAST = "CamelRollbackOnlyLast";
102    
103        String SPLIT_INDEX = "CamelSplitIndex";
104        String SPLIT_SIZE  = "CamelSplitSize";
105    
106        String TIMER_FIRED_TIME = "CamelTimerFiredTime";
107        String TIMER_NAME       = "CamelTimerName";
108        String TIMER_PERIOD     = "CamelTimerPeriod";
109        String TIMER_TIME       = "CamelTimerTime";
110    
111        String TRANSACTED = "CamelTransacted";
112    
113        String TRACE_EVENT           = "CamelTraceEvent";
114        String TRACE_EVENT_NODE_ID   = "CamelTraceEventNodeId";
115        String TRACE_EVENT_TIMESTAMP = "CamelTraceEventTimestamp";
116        String TRACE_EVENT_EXCHANGE  = "CamelTraceEventExchange";
117    
118        /**
119         * Returns the {@link ExchangePattern} (MEP) of this exchange.
120         *
121         * @return the message exchange pattern of this exchange
122         */
123        ExchangePattern getPattern();
124    
125        /**
126         * Allows the {@link ExchangePattern} (MEP) of this exchange to be customized.
127         *
128         * This typically won't be required as an exchange can be created with a specific MEP
129         * by calling {@link Endpoint#createExchange(ExchangePattern)} but it is here just in case
130         * it is needed.
131         *
132         * @param pattern  the pattern 
133         */
134        void setPattern(ExchangePattern pattern);
135    
136        /**
137         * Returns a property associated with this exchange by name
138         *
139         * @param name the name of the property
140         * @return the value of the given property or <tt>null</tt> if there is no property for
141         *         the given name
142         */
143        Object getProperty(String name);
144    
145        /**
146         * Returns a property associated with this exchange by name
147         *
148         * @param name the name of the property
149         * @param defaultValue the default value to return if property was absent
150         * @return the value of the given property or <tt>defaultValue</tt> if there is no
151         *         property for the given name
152         */
153        Object getProperty(String name, Object defaultValue);
154    
155        /**
156         * Returns a property associated with this exchange by name and specifying
157         * the type required
158         *
159         * @param name the name of the property
160         * @param type the type of the property
161         * @return the value of the given property or <tt>null</tt> if there is no property for
162         *         the given name or <tt>null</tt> if it cannot be converted to the given type
163         */
164        <T> T getProperty(String name, Class<T> type);
165    
166        /**
167         * Returns a property associated with this exchange by name and specifying
168         * the type required
169         *
170         * @param name the name of the property
171         * @param defaultValue the default value to return if property was absent
172         * @param type the type of the property
173         * @return the value of the given property or <tt>defaultValue</tt> if there is no property for
174         *         the given name or <tt>null</tt> if it cannot be converted to the given type
175         */
176        <T> T getProperty(String name, Object defaultValue, Class<T> type);
177    
178        /**
179         * Sets a property on the exchange
180         *
181         * @param name  of the property
182         * @param value to associate with the name
183         */
184        void setProperty(String name, Object value);
185    
186        /**
187         * Removes the given property on the exchange
188         *
189         * @param name of the property
190         * @return the old value of the property
191         */
192        Object removeProperty(String name);
193    
194        /**
195         * Returns all of the properties associated with the exchange
196         *
197         * @return all the headers in a Map
198         */
199        Map<String, Object> getProperties();
200    
201        /**
202         * Returns whether any properties has been set
203         *
204         * @return <tt>true</tt> if any properties has been set
205         */
206        boolean hasProperties();
207    
208        /**
209         * Returns the inbound request message
210         *
211         * @return the message
212         */
213        Message getIn();
214    
215        /**
216         * Returns the inbound request message as the given type
217         *
218         * @param type the given type
219         * @return the message as the given type or <tt>null</tt> if not possible to covert to given type
220         */
221        <T> T getIn(Class<T> type);
222    
223        /**
224         * Sets the inbound message instance
225         *
226         * @param in the inbound message
227         */
228        void setIn(Message in);
229    
230        /**
231         * Returns the outbound message, lazily creating one if one has not already
232         * been associated with this exchange.
233         * <p/>
234         * If you want to test whether an OUT message have been set or not, use the {@link #hasOut()} method.
235         *
236         * @return the response
237         */
238        Message getOut();
239    
240        /**
241         * Returns the outbound request message as the given type
242         *
243         * @param type the given type
244         * @return the message as the given type or <tt>null</tt> if not possible to covert to given type
245         */
246        <T> T getOut(Class<T> type);
247    
248        /**
249         * Returns whether an OUT message has been set or not.
250         *
251         * @return <tt>true</tt> if an OUT message exists, <tt>false</tt> otherwise.
252         */
253        boolean hasOut();
254    
255        /**
256         * Sets the outbound message
257         *
258         * @param out the outbound message
259         */
260        void setOut(Message out);
261    
262        /**
263         * Returns the exception associated with this exchange
264         *
265         * @return the exception (or null if no faults)
266         */
267        Exception getException();
268    
269        /**
270         * Returns the exception associated with this exchange.
271         * <p/>
272         * Is used to get the caused exception that typically have been wrapped in some sort
273         * of Camel wrapper exception
274         * <p/>
275         * The strategy is to look in the exception hierarchy to find the first given cause that matches the type.
276         * Will start from the bottom (the real cause) and walk upwards.
277         *
278         * @param type the exception type
279         * @return the exception (or <tt>null</tt> if no caused exception matched)
280         */
281        <T> T getException(Class<T> type);
282    
283        /**
284         * Sets the exception associated with this exchange
285         *
286         * @param e  the caused exception
287         */
288        void setException(Exception e);
289    
290        /**
291         * Returns true if this exchange failed due to either an exception or fault
292         *
293         * @return true if this exchange failed due to either an exception or fault
294         * @see Exchange#getException()
295         * @see Message#setFault(boolean)
296         * @see Message#isFault()
297         */
298        boolean isFailed();
299    
300        /**
301         * Returns true if this exchange is transacted
302         */
303        boolean isTransacted();
304    
305        /**
306         * Returns true if this exchange is marked for rollback
307         */
308        boolean isRollbackOnly();
309    
310        /**
311         * Returns the container so that a processor can resolve endpoints from URIs
312         *
313         * @return the container which owns this exchange
314         */
315        CamelContext getContext();
316    
317        /**
318         * Creates a copy of the current message exchange so that it can be
319         * forwarded to another destination
320         */
321        Exchange copy();
322    
323        /**
324         * Returns the endpoint which originated this message exchange if a consumer on an endpoint created the message exchange
325         * otherwise this property will be null
326         */
327        Endpoint getFromEndpoint();
328    
329        /**
330         * Sets the endpoint which originated this message exchange. This method
331         * should typically only be called by {@link org.apache.camel.Endpoint} implementations
332         *
333         * @param fromEndpoint the endpoint which is originating this message exchange
334         */
335        void setFromEndpoint(Endpoint fromEndpoint);
336        
337        /**
338         * Returns the unit of work that this exchange belongs to; which may map to
339         * zero, one or more physical transactions
340         */
341        UnitOfWork getUnitOfWork();
342    
343        /**
344         * Sets the unit of work that this exchange belongs to; which may map to
345         * zero, one or more physical transactions
346         */
347        void setUnitOfWork(UnitOfWork unitOfWork);
348    
349        /**
350         * Returns the exchange id (unique)
351         */
352        String getExchangeId();
353    
354        /**
355         * Set the exchange id
356         */
357        void setExchangeId(String id);
358    
359        /**
360         * Adds a {@link org.apache.camel.spi.Synchronization} to be invoked as callback when
361         * this exchange is completed.
362         *
363         * @param onCompletion  the callback to invoke on completion of this exchange
364         */
365        void addOnCompletion(Synchronization onCompletion);
366    
367    }