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