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.List;
020    import java.util.Map;
021    
022    import org.apache.camel.spi.Synchronization;
023    import org.apache.camel.spi.UnitOfWork;
024    
025    /**
026     * An Exchange is the message container holding the information during the entire routing of
027     * a {@link  Message} received by a {@link Consumer}. 
028     * <p/>
029     * During processing down the {@link Processor} chain, the {@link Exchange} provides access to the 
030     * current (not the original) request and response {@link Message} messages. The {@link Exchange} 
031     * also holds meta-data during its entire lifetime stored as properties accessible using the 
032     * various {@link #getProperty(String)} methods. The {@link #setProperty(String, Object)} is 
033     * used to store a property. For example you can use this to store security, SLA related 
034     * data or any other information deemed useful throughout processing. If an {@link Exchange} 
035     * failed during routing the {@link Exception} that caused the failure is stored and accessible 
036     * via the {@link #getException()} method.
037     * <p/>
038     * An Exchange is created when a {@link Consumer} receives a request. A new {@link Message} is
039     * created, the request is set as the body of the {@link Message} and depending on the {@link Consumer}
040     * other {@link Endpoint} and protocol related information is added as headers on the {@link Message}.
041     * Then an Exchange is created and the newly created {@link Message} is set as the in on the Exchange.
042     * Therefore an Exchange starts its life in a {@link Consumer}. The Exchange is then sent down the
043     * {@link Route} for processing along a {@link Processor} chain. The {@link Processor} as the name
044     * suggests is what processes the {@link Message} in the Exchange and Camel, in addition to 
045     * providing out-of-the-box a large number of useful processors, it also allows you to create your own. 
046     * The rule Camel uses is to take the out {@link Message} produced by the previous {@link Processor} 
047     * and set it as the in for the next {@link Processor}. If the previous {@link Processor} did not
048     * produce an out, then the in of the previous {@link Processor} is sent as the next in. At the
049     * end of the processing chain, depending on the {@link ExchangePattern Message Exchange Pattern} (or MEP)
050     * the last out (or in of no out available) is sent by the {@link Consumer} back to the original caller.
051     * <p/>
052     * Camel, in addition to providing out-of-the-box a large number of useful processors, it also allows 
053     * you to implement and use your own. When the Exchange is passed to a {@link Processor}, it always 
054     * contains an in {@link Message} and no out {@link Message}. The {@link Processor} <b>may</b> produce 
055     * an out, depending on the nature of the {@link Processor}. The in {@link Message} can be accessed 
056     * using the {@link #getIn()} method. Since the out message is null when entering the {@link Processor}, 
057     * the {@link #getOut()} method is actually a convenient factory method that will lazily instantiate a 
058     * {@link org.apache.camel.impl.DefaultMessage} which you could populate. As an alternative you could 
059     * also instantiate your specialized  {@link Message} and set it on the exchange using the 
060     * {@link #setOut(org.apache.camel.Message)} method. Please note that a {@link Message} contains not only 
061     * the body but also headers and attachments. If you are creating a new {@link Message} the headers and 
062     * attachments of the in {@link Message} are not automatically copied to the out by Camel and you'll have 
063     * to set the headers and attachments you need yourself. If your {@link Processor} is not producing a 
064     * different {@link Message} but only needs to slightly  modify the in, you can simply update the in 
065     * {@link Message} returned by {@link #getIn()}. 
066     * <p/>
067     * See this <a href="http://camel.apache.org/using-getin-or-getout-methods-on-exchange.html">FAQ entry</a> 
068     * for more details.
069     * 
070     */
071    public interface Exchange {
072    
073        String AUTHENTICATION                   = "CamelAuthentication";
074        String AUTHENTICATION_FAILURE_POLICY_ID = "CamelAuthenticationFailurePolicyId";
075        String ACCEPT_CONTENT_TYPE              = "CamelAcceptContentType";
076        String AGGREGATED_SIZE                  = "CamelAggregatedSize";
077        String AGGREGATED_COMPLETED_BY          = "CamelAggregatedCompletedBy";
078        String AGGREGATED_CORRELATION_KEY       = "CamelAggregatedCorrelationKey";
079        String AGGREGATION_STRATEGY             = "CamelAggregationStrategy";
080        String ASYNC_WAIT                       = "CamelAsyncWait";
081    
082        String BATCH_INDEX                = "CamelBatchIndex";
083        String BATCH_SIZE                 = "CamelBatchSize";
084        String BATCH_COMPLETE             = "CamelBatchComplete";
085        String BEAN_METHOD_NAME           = "CamelBeanMethodName";
086        String BEAN_MULTI_PARAMETER_ARRAY = "CamelBeanMultiParameterArray";
087        String BINDING                    = "CamelBinding";
088    
089        String CHARSET_NAME      = "CamelCharsetName";
090        String CREATED_TIMESTAMP = "CamelCreatedTimestamp";
091        String CONTENT_ENCODING  = "Content-Encoding";
092        String CONTENT_TYPE      = "Content-Type";
093        String CORRELATION_ID    = "CamelCorrelationId";
094    
095        String DATASET_INDEX             = "CamelDataSetIndex";
096        String DEFAULT_CHARSET_PROPERTY  = "org.apache.camel.default.charset";
097        String DESTINATION_OVERRIDE_URL = "CamelDestinationOverrideUrl";
098        String DISABLE_HTTP_STREAM_CACHE = "CamelDisableHttpStreamCache";
099    
100        String ERRORHANDLER_HANDLED = "CamelErrorHandlerHandled";
101        String EXCEPTION_CAUGHT     = "CamelExceptionCaught";
102    
103        String FAILURE_HANDLED      = "CamelFailureHandled";
104        String FAILURE_ENDPOINT     = "CamelFailureEndpoint";
105        String FILTER_NON_XML_CHARS = "CamelFilterNonXmlChars";
106        String FILE_LOCAL_WORK_PATH = "CamelFileLocalWorkPath";
107        String FILE_NAME            = "CamelFileName";
108        String FILE_NAME_ONLY       = "CamelFileNameOnly";
109        String FILE_NAME_PRODUCED   = "CamelFileNameProduced";
110        String FILE_PATH            = "CamelFilePath";
111        String FILE_PARENT          = "CamelFileParent";
112        String FILE_LAST_MODIFIED   = "CamelFileLastModified";
113        String FILTER_MATCHED       = "CamelFilterMatched";
114    
115        String GROUPED_EXCHANGE = "CamelGroupedExchange";
116        
117        String HTTP_BASE_URI           = "CamelHttpBaseUri";
118        String HTTP_CHARACTER_ENCODING = "CamelHttpCharacterEncoding";
119        String HTTP_METHOD             = "CamelHttpMethod";
120        String HTTP_PATH               = "CamelHttpPath";
121        String HTTP_PROTOCOL_VERSION   = "CamelHttpProtocolVersion";
122        String HTTP_QUERY              = "CamelHttpQuery";
123        String HTTP_RESPONSE_CODE      = "CamelHttpResponseCode";
124        String HTTP_URI                = "CamelHttpUri";
125        String HTTP_URL                = "CamelHttpUrl";
126        String HTTP_CHUNKED            = "CamelHttpChunked";
127        String HTTP_SERVLET_REQUEST    = "CamelHttpServletRequest";
128        String HTTP_SERVLET_RESPONSE   = "CamelHttpServletResponse";
129    
130        String INTERCEPTED_ENDPOINT = "CamelInterceptedEndpoint";
131    
132        String LANGUAGE_SCRIPT          = "CamelLanguageScript";
133        String LOG_DEBUG_BODY_MAX_CHARS = "CamelLogDebugBodyMaxChars";
134        String LOG_DEBUG_BODY_STREAMS   = "CamelLogDebugStreams";
135        String LOOP_INDEX               = "CamelLoopIndex";
136        String LOOP_SIZE                = "CamelLoopSize";
137    
138        String MAXIMUM_CACHE_POOL_SIZE = "CamelMaximumCachePoolSize";
139        String MULTICAST_INDEX         = "CamelMulticastIndex";
140        String MULTICAST_COMPLETE      = "CamelMulticastComplete";
141    
142        String NOTIFY_EVENT = "CamelNotifyEvent";
143    
144        String ON_COMPLETION = "CamelOnCompletion";
145    
146        String RECEIVED_TIMESTAMP      = "CamelReceivedTimestamp";
147        String REDELIVERED             = "CamelRedelivered";
148        String REDELIVERY_COUNTER      = "CamelRedeliveryCounter";
149        String REDELIVERY_MAX_COUNTER  = "CamelRedeliveryMaxCounter";
150        String REDELIVERY_EXHAUSTED    = "CamelRedeliveryExhausted";
151        String ROLLBACK_ONLY           = "CamelRollbackOnly";
152        String ROLLBACK_ONLY_LAST      = "CamelRollbackOnlyLast";
153        String ROUTE_STOP              = "CamelRouteStop";
154    
155        String SOAP_ACTION        = "CamelSoapAction";
156        String SKIP_GZIP_ENCODING = "CamelSkipGzipEncoding";
157        String SLIP_ENDPOINT      = "CamelSlipEndpoint";
158        String SPLIT_INDEX        = "CamelSplitIndex";
159        String SPLIT_COMPLETE     = "CamelSplitComplete";
160        String SPLIT_SIZE         = "CamelSplitSize";
161    
162        String TIMER_FIRED_TIME      = "CamelTimerFiredTime";
163        String TIMER_NAME            = "CamelTimerName";
164        String TIMER_PERIOD          = "CamelTimerPeriod";
165        String TIMER_TIME            = "CamelTimerTime";
166        String TO_ENDPOINT           = "CamelToEndpoint";
167        String TRACE_EVENT           = "CamelTraceEvent";
168        String TRACE_EVENT_NODE_ID   = "CamelTraceEventNodeId";
169        String TRACE_EVENT_TIMESTAMP = "CamelTraceEventTimestamp";
170        String TRACE_EVENT_EXCHANGE  = "CamelTraceEventExchange";
171        String TRANSFER_ENCODING     = "Transfer-Encoding";
172    
173        String XSLT_FILE_NAME = "CamelXsltFileName";
174    
175        /**
176         * Returns the {@link ExchangePattern} (MEP) of this exchange.
177         *
178         * @return the message exchange pattern of this exchange
179         */
180        ExchangePattern getPattern();
181    
182        /**
183         * Allows the {@link ExchangePattern} (MEP) of this exchange to be customized.
184         *
185         * This typically won't be required as an exchange can be created with a specific MEP
186         * by calling {@link Endpoint#createExchange(ExchangePattern)} but it is here just in case
187         * it is needed.
188         *
189         * @param pattern  the pattern 
190         */
191        void setPattern(ExchangePattern pattern);
192    
193        /**
194         * Returns a property associated with this exchange by name
195         *
196         * @param name the name of the property
197         * @return the value of the given property or <tt>null</tt> if there is no property for
198         *         the given name
199         */
200        Object getProperty(String name);
201    
202        /**
203         * Returns a property associated with this exchange by name
204         *
205         * @param name the name of the property
206         * @param defaultValue the default value to return if property was absent
207         * @return the value of the given property or <tt>defaultValue</tt> if there is no
208         *         property for the given name
209         */
210        Object getProperty(String name, Object defaultValue);
211    
212        /**
213         * Returns a property associated with this exchange by name and specifying
214         * the type required
215         *
216         * @param name the name of the property
217         * @param type the type of the property
218         * @return the value of the given property or <tt>null</tt> if there is no property for
219         *         the given name or <tt>null</tt> if it cannot be converted to the given type
220         */
221        <T> T getProperty(String name, Class<T> type);
222    
223        /**
224         * Returns a property associated with this exchange by name and specifying
225         * the type required
226         *
227         * @param name the name of the property
228         * @param defaultValue the default value to return if property was absent
229         * @param type the type of the property
230         * @return the value of the given property or <tt>defaultValue</tt> if there is no property for
231         *         the given name or <tt>null</tt> if it cannot be converted to the given type
232         */
233        <T> T getProperty(String name, Object defaultValue, Class<T> type);
234    
235        /**
236         * Sets a property on the exchange
237         *
238         * @param name  of the property
239         * @param value to associate with the name
240         */
241        void setProperty(String name, Object value);
242    
243        /**
244         * Removes the given property on the exchange
245         *
246         * @param name of the property
247         * @return the old value of the property
248         */
249        Object removeProperty(String name);
250    
251        /**
252         * Returns all of the properties associated with the exchange
253         *
254         * @return all the headers in a Map
255         */
256        Map<String, Object> getProperties();
257    
258        /**
259         * Returns whether any properties has been set
260         *
261         * @return <tt>true</tt> if any properties has been set
262         */
263        boolean hasProperties();
264    
265        /**
266         * Returns the inbound request message
267         *
268         * @return the message
269         */
270        Message getIn();
271    
272        /**
273         * Returns the inbound request message as the given type
274         *
275         * @param type the given type
276         * @return the message as the given type or <tt>null</tt> if not possible to covert to given type
277         */
278        <T> T getIn(Class<T> type);
279    
280        /**
281         * Sets the inbound message instance
282         *
283         * @param in the inbound message
284         */
285        void setIn(Message in);
286    
287        /**
288         * Returns the outbound message, lazily creating one if one has not already
289         * been associated with this exchange.
290         * <p/>
291         * <br/><b>Important:</b> If you want to change the current message, then use {@link #getIn()} instead as it will
292         * ensure headers etc. is kept and propagated when routing continues. Bottom line end users should rarely use
293         * this method.
294         * <p/>
295         * <br/>If you want to test whether an OUT message have been set or not, use the {@link #hasOut()} method.
296         * <p/>
297         * See also the class java doc for this {@link Exchange} for more details and this
298         * <a href="http://camel.apache.org/using-getin-or-getout-methods-on-exchange.html">FAQ entry</a>.
299         *
300         * @return the response
301         * @see #getIn()
302         */
303        Message getOut();
304    
305        /**
306         * Returns the outbound request message as the given type
307         * <p/>
308         * <br/><b>Important:</b> If you want to change the current message, then use {@link #getIn()} instead as it will
309         * ensure headers etc. is kept and propagated when routing continues. Bottom line end users should rarely use
310         * this method.
311         * <p/>
312         * <br/>If you want to test whether an OUT message have been set or not, use the {@link #hasOut()} method.
313         * <p/>
314         * See also the class java doc for this {@link Exchange} for more details and this
315         * <a href="http://camel.apache.org/using-getin-or-getout-methods-on-exchange.html">FAQ entry</a>.
316         *
317         * @param type the given type
318         * @return the message as the given type or <tt>null</tt> if not possible to covert to given type
319         * @see #getIn(Class)
320         */
321        <T> T getOut(Class<T> type);
322    
323        /**
324         * Returns whether an OUT message has been set or not.
325         *
326         * @return <tt>true</tt> if an OUT message exists, <tt>false</tt> otherwise.
327         */
328        boolean hasOut();
329    
330        /**
331         * Sets the outbound message
332         *
333         * @param out the outbound message
334         */
335        void setOut(Message out);
336    
337        /**
338         * Returns the exception associated with this exchange
339         *
340         * @return the exception (or null if no faults)
341         */
342        Exception getException();
343    
344        /**
345         * Returns the exception associated with this exchange.
346         * <p/>
347         * Is used to get the caused exception that typically have been wrapped in some sort
348         * of Camel wrapper exception
349         * <p/>
350         * The strategy is to look in the exception hierarchy to find the first given cause that matches the type.
351         * Will start from the bottom (the real cause) and walk upwards.
352         *
353         * @param type the exception type
354         * @return the exception (or <tt>null</tt> if no caused exception matched)
355         */
356        <T> T getException(Class<T> type);
357    
358        /**
359         * Sets the exception associated with this exchange
360         * <p/>
361         * Camel will wrap {@link Throwable} into {@link Exception} type to
362         * accommodate for the {@link #getException()} method returning a plain {@link Exception} type.
363         *
364         * @param t  the caused exception
365         */
366        void setException(Throwable t);
367    
368        /**
369         * Returns true if this exchange failed due to either an exception or fault
370         *
371         * @return true if this exchange failed due to either an exception or fault
372         * @see Exchange#getException()
373         * @see Message#setFault(boolean)
374         * @see Message#isFault()
375         */
376        boolean isFailed();
377    
378        /**
379         * Returns true if this exchange is transacted
380         */
381        boolean isTransacted();
382    
383        /**
384         * Returns true if this exchange is marked for rollback
385         */
386        boolean isRollbackOnly();
387    
388        /**
389         * Returns the container so that a processor can resolve endpoints from URIs
390         *
391         * @return the container which owns this exchange
392         */
393        CamelContext getContext();
394    
395        /**
396         * Creates a copy of the current message exchange so that it can be
397         * forwarded to another destination
398         */
399        Exchange copy();
400    
401        /**
402         * Returns the endpoint which originated this message exchange if a consumer on an endpoint
403         * created the message exchange, otherwise this property will be <tt>null</tt>
404         */
405        Endpoint getFromEndpoint();
406    
407        /**
408         * Sets the endpoint which originated this message exchange. This method
409         * should typically only be called by {@link org.apache.camel.Endpoint} implementations
410         *
411         * @param fromEndpoint the endpoint which is originating this message exchange
412         */
413        void setFromEndpoint(Endpoint fromEndpoint);
414        
415        /**
416         * Returns the route id which originated this message exchange if a route consumer on an endpoint
417         * created the message exchange, otherwise this property will be <tt>null</tt>
418         */
419        String getFromRouteId();
420    
421        /**
422         * Sets the route id which originated this message exchange. This method
423         * should typically only be called by the internal framework.
424         *
425         * @param fromRouteId the from route id
426         */
427        void setFromRouteId(String fromRouteId);
428    
429        /**
430         * Returns the unit of work that this exchange belongs to; which may map to
431         * zero, one or more physical transactions
432         */
433        UnitOfWork getUnitOfWork();
434    
435        /**
436         * Sets the unit of work that this exchange belongs to; which may map to
437         * zero, one or more physical transactions
438         */
439        void setUnitOfWork(UnitOfWork unitOfWork);
440    
441        /**
442         * Returns the exchange id (unique)
443         */
444        String getExchangeId();
445    
446        /**
447         * Set the exchange id
448         */
449        void setExchangeId(String id);
450    
451        /**
452         * Adds a {@link org.apache.camel.spi.Synchronization} to be invoked as callback when
453         * this exchange is completed.
454         *
455         * @param onCompletion  the callback to invoke on completion of this exchange
456         */
457        void addOnCompletion(Synchronization onCompletion);
458    
459        /**
460         * Handover all the on completions from this exchange to the target exchange.
461         *
462         * @param target the target exchange
463         */
464        void handoverCompletions(Exchange target);
465    
466        /**
467         * Handover all the on completions from this exchange
468         *
469         * @return the on completions
470         */
471        List<Synchronization> handoverCompletions();
472    
473    }