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.UnitOfWork;
022    
023    /**
024     * The base message exchange interface providing access to the request, response
025     * and fault {@link Message} instances. Different providers such as JMS, JBI,
026     * CXF and HTTP can provide their own derived API to expose the underlying
027     * transport semantics to avoid the leaky abstractions of generic APIs.
028     *
029     * @version $Revision: 703643 $
030     */
031    public interface Exchange {
032    
033        String CHARSET_NAME = "org.apache.camel.Exchange.CharsetName";
034    
035        String AGGREGATED_COUNT = "org.apache.camel.Exchange.AggregatedCount";
036    
037        String EXCEPTION_HANDLED_PROPERTY = "CamelExceptionHandled";
038    
039        /**
040         * Returns the {@link ExchangePattern} (MEP) of this exchange.
041         *
042         * @return the message exchange pattern of this exchange
043         */
044        ExchangePattern getPattern();
045    
046        /**
047         * Allows the {@link ExchangePattern} (MEP) of this exchange to be customized.
048         *
049         * This typically won't be required as an exchange can be created with a specific MEP
050         * by calling {@link Endpoint#createExchange(ExchangePattern)} but it is here just in case
051         * it is needed.
052         *
053         * @param pattern  the pattern 
054         */
055        void setPattern(ExchangePattern pattern);
056    
057        /**
058         * Returns a property associated with this exchange by name
059         *
060         * @param name the name of the property
061         * @return the value of the given header or null if there is no property for
062         *         the given name
063         */
064        Object getProperty(String name);
065    
066        /**
067         * Returns a property associated with this exchange by name and specifying
068         * the type required
069         *
070         * @param name the name of the property
071         * @param type the type of the property
072         * @return the value of the given header or null if there is no property for
073         *         the given name or null if it cannot be converted to the given
074         *         type
075         */
076        <T> T getProperty(String name, Class<T> type);
077    
078        /**
079         * Sets a property on the exchange
080         *
081         * @param name  of the property
082         * @param value to associate with the name
083         */
084        void setProperty(String name, Object value);
085    
086        /**
087         * Removes the given property on the exchange
088         *
089         * @param name of the property
090         * @return the old value of the property
091         */
092        Object removeProperty(String name);
093    
094        /**
095         * Returns all of the properties associated with the exchange
096         *
097         * @return all the headers in a Map
098         */
099        Map<String, Object> getProperties();
100    
101        /**
102         * Returns the inbound request message
103         *
104         * @return the message
105         */
106        Message getIn();
107    
108        /**
109         * Sets the inbound message instance
110         *
111         * @param in the inbound message
112         */
113        void setIn(Message in);
114    
115        /**
116         * Returns the outbound message, lazily creating one if one has not already
117         * been associated with this exchange. If you want to inspect this property
118         * but not force lazy creation then invoke the {@link #getOut(boolean)}
119         * method passing in <tt>false</tt>
120         *
121         * @return the response
122         */
123        Message getOut();
124    
125        /**
126         * Returns the outbound message; optionally lazily creating one if one has
127         * not been associated with this exchange
128         *
129         * @param lazyCreate <tt>true</tt> will lazy create the out message
130         * @return the response
131         */
132        Message getOut(boolean lazyCreate);
133    
134        /**
135         * Sets the outbound message
136         *
137         * @param out the outbound message
138         */
139        void setOut(Message out);
140    
141        /**
142         * Returns the fault message
143         *
144         * @return the fault
145         */
146        Message getFault();
147    
148        /**
149         * Returns the fault message; optionally lazily creating one if one has
150         * not been associated with this exchange
151         *
152         * @param lazyCreate <tt>true</tt> will lazy create the fault message
153         * @return the fault
154         */
155        Message getFault(boolean lazyCreate);
156    
157        /**
158         * Returns the exception associated with this exchange
159         *
160         * @return the exception (or null if no faults)
161         */
162        Throwable getException();
163    
164        /**
165         * Sets the exception associated with this exchange
166         *
167         * @param e  the caused exception
168         */
169        void setException(Throwable e);
170    
171        /**
172         * Returns true if this exchange failed due to either an exception or fault
173         *
174         * @return true if this exchange failed due to either an exception or fault
175         * @see Exchange#getException()
176         * @see Exchange#getFault()
177         */
178        boolean isFailed();
179    
180        /**
181         * Returns true if this exchange is transacted
182         */
183        boolean isTransacted();
184    
185        /**
186         * Returns the container so that a processor can resolve endpoints from URIs
187         *
188         * @return the container which owns this exchange
189         */
190        CamelContext getContext();
191    
192        /**
193         * Creates a new exchange instance with empty messages, headers and properties
194         */
195        Exchange newInstance();
196    
197        /**
198         * Creates a copy of the current message exchange so that it can be
199         * forwarded to another destination
200         */
201        Exchange copy();
202    
203        /**
204         * Copies the data into this exchange from the given exchange
205         *
206         * @param source is the source from which headers and messages will be copied
207         */
208        void copyFrom(Exchange source);
209    
210        /**
211         * Returns the unit of work that this exchange belongs to; which may map to
212         * zero, one or more physical transactions
213         */
214        UnitOfWork getUnitOfWork();
215    
216        /**
217         * Sets the unit of work that this exchange belongs to; which may map to
218         * zero, one or more physical transactions
219         */
220        void setUnitOfWork(UnitOfWork unitOfWork);
221    
222        /**
223         * Returns the exchange id (unique)
224         */
225        String getExchangeId();
226    
227        /**
228         * Set the exchange id
229         */
230        void setExchangeId(String id);
231    
232    }