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