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    import java.util.Set;
021    
022    import javax.activation.DataHandler;
023    
024    /**
025     * Implements the <a
026     * href="http://camel.apache.org/message.html">Message</a> pattern and
027     * represents an inbound or outbound message as part of an {@link Exchange}
028     * <p/>
029     * See {@link org.apache.camel.impl.DefaultMessage DefaultMessage} for how headers is represented in Camel using a
030     * {@link org.apache.camel.util.CaseInsensitiveMap CaseInsensitiveMap}.
031     *
032     * @version $Revision: 802994 $
033     */
034    public interface Message {
035    
036        /**
037         * Returns the id of the message
038         *
039         * @return the message id
040         */
041        String getMessageId();
042    
043        /**
044         * Sets the id of the message
045         *
046         * @param messageId id of the message
047         */
048        void setMessageId(String messageId);
049    
050        /**
051         * Returns the exchange this message is related to
052         *
053         * @return the exchange
054         */
055        Exchange getExchange();
056    
057        /**
058         * Returns true if this message represents a fault
059         *
060         * @return <tt>true</tt> if this is a fault message, <tt>false</tt> for regular messages.
061         */
062        boolean isFault();
063    
064        /**
065         * Sets the fault flag on this message
066         *
067         * @param fault the fault flag
068         */
069        void setFault(boolean fault);
070    
071        /**
072         * Accesses a specific header
073         *
074         * @param name  name of header
075         * @return object header associated with the name
076         */
077        Object getHeader(String name);
078    
079        /**
080         * Returns a header associated with this message by name and specifying the
081         * type required
082         *
083         * @param name the name of the header
084         * @param type the type of the header
085         * @return the value of the given header or null if there is no property for
086         *         the given name or it cannot be converted to the given type
087         */
088        <T> T getHeader(String name, Class<T> type);
089    
090        /**
091         * Sets a header on the message
092         *
093         * @param name of the header
094         * @param value to associate with the name
095         */
096        void setHeader(String name, Object value);
097    
098        /**
099         * Removes the named header from this message
100         *
101         * @param name name of the header
102         * @return the old value of the header
103         */
104        Object removeHeader(String name);
105    
106        /**
107         * Returns all of the headers associated with the message
108         *
109         * @return all the headers in a Map
110         */
111        Map<String, Object> getHeaders();
112    
113        /**
114         * Set all the headers associated with this message
115         *
116         * @param headers headers to set
117         */
118        void setHeaders(Map<String, Object> headers);
119    
120        /**
121         * Returns whether has any headers has been set.
122         *
123         * @return <tt>true</tt> if any headers has been set
124         */
125        boolean hasHeaders();
126    
127        /**
128         * Returns the body of the message as a POJO
129         * <p/>
130         * The body can be <tt>null</tt> if no body is set
131         *
132         * @return the body, can be <tt>null</tt>
133         */
134        Object getBody();
135    
136        /**
137         * Returns the body of the message as a POJO
138         *
139         * @return the body, is never <tt>null</tt>
140         * @throws InvalidPayloadException Is thrown if the body being <tt>null</tt> or wrong class type
141         */
142        Object getMandatoryBody() throws InvalidPayloadException;
143    
144        /**
145         * Returns the body as the specified type
146         *
147         * @param type the type that the body
148         * @return the body of the message as the specified type, or <tt>null</tt> if not possible to convert
149         */
150        <T> T getBody(Class<T> type);
151    
152        /**
153         * Returns the mandatory body as the specified type
154         *
155         * @param type the type that the body
156         * @return the body of the message as the specified type, is never <tt>null</tt>.
157         * @throws InvalidPayloadException Is thrown if the body being <tt>null</tt> or wrong class type
158         */
159        <T> T getMandatoryBody(Class<T> type) throws InvalidPayloadException;
160    
161        /**
162         * Sets the body of the message
163         *
164         * @param body the body
165         */
166        void setBody(Object body);
167    
168        /**
169         * Sets the body of the message as a specific type
170         *
171         * @param body the body
172         * @param type the type of the body
173         */
174        <T> void setBody(Object body, Class<T> type);
175    
176        /**
177         * Creates a copy of this message so that it can be used and possibly
178         * modified further in another exchange
179         *
180         * @return a new message instance copied from this message
181         */
182        Message copy();
183    
184        /**
185         * Copies the contents of the other message into this message
186         *
187         * @param message the other message
188         */
189        void copyFrom(Message message);
190    
191        /**
192         * Returns the attachment specified by the id
193         *
194         * @param id        the id under which the attachment is stored
195         * @return          the data handler for this attachment or null
196         */
197        DataHandler getAttachment(String id);
198    
199        /**
200         * Returns a set of attachment names of the message
201         *
202         * @return  a set of attachment names
203         */
204        Set<String> getAttachmentNames();
205    
206        /**
207         * Removes the attachment specified by the id
208         *
209         * @param id        the id of the attachment to remove
210         */
211        void removeAttachment(String id);
212    
213        /**
214         * Adds an attachment to the message using the id
215         *
216         * @param id        the id to store the attachment under
217         * @param content   the data handler for the attachment
218         */
219        void addAttachment(String id, DataHandler content);
220    
221        /**
222         * Returns all attachments of the message
223         *
224         * @return  the attachments in a map or null
225         */
226        Map<String, DataHandler> getAttachments();
227    
228        /**
229         * Set all the attachments associated with this message
230         *
231         * @param attachments attachements
232         */
233        void setAttachments(Map<String, DataHandler> attachments);
234    
235        /**
236         * Returns whether this message has attachments.
237         *
238         * @return <tt>true</tt> if this message has any attachments.
239         */
240        boolean hasAttachments();
241    
242        /**
243         * Returns the unique ID for a message exchange if this message is capable of creating one or null if not
244         *
245         * @return the created exchange id, or <tt>null</tt> if not capable of creating
246         */
247        String createExchangeId();
248    }