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