public interface Message
Exchange
.
Headers is represented in Camel using a CaseInsensitiveMap
.
The implementation of the map can be configured by the HeadersMapFactory
which can be set
on the CamelContext
. The default implementation uses the CaseInsensitiveMap
.Modifier and Type | Method and Description |
---|---|
Message |
copy()
Creates a copy of this message so that it can be used and possibly
modified further in another exchange.
|
void |
copyFrom(Message message)
Copies the contents of the other message into this message
If you need to do a copy and then set a new body,
then use
copyFromWithNewBody(Message, Object) method instead. |
void |
copyFromWithNewBody(Message message,
Object newBody)
|
Object |
getBody()
Returns the body of the message as a POJO
The body can be null if no body is set.
|
<T> T |
getBody(Class<T> type)
Returns the body as the specified type
Notice if the message body is stream based then calling this method multiple times may lead to the stream not being able to be re-read again.
|
Exchange |
getExchange()
Returns the exchange this message is related to
|
Object |
getHeader(String name)
Accesses a specific header
|
<T> T |
getHeader(String name,
Class<T> type)
Returns a header associated with this message by name and specifying the
type required
|
Object |
getHeader(String name,
Object defaultValue)
Accesses a specific header
|
<T> T |
getHeader(String name,
Object defaultValue,
Class<T> type)
Returns a header associated with this message by name and specifying the
type required
|
Object |
getHeader(String name,
Supplier<Object> defaultValueSupplier)
Accesses a specific header
|
<T> T |
getHeader(String name,
Supplier<Object> defaultValueSupplier,
Class<T> type)
Returns a header associated with this message by name and specifying the
type required
|
Map<String,Object> |
getHeaders()
Returns all of the headers associated with the message.
|
Object |
getMandatoryBody()
Returns the body of the message as a POJO
Notice if the message body is stream based then calling this method multiple times may lead to the stream not being able to be re-read again.
|
<T> T |
getMandatoryBody(Class<T> type)
Returns the mandatory body as the specified type
Notice if the message body is stream based then calling this method multiple times may lead to the stream not being able to be re-read again.
|
String |
getMessageId()
Returns the id of the message.
|
boolean |
hasHeaders()
Returns whether has any headers has been set.
|
Object |
removeHeader(String name)
Removes the named header from this message
|
boolean |
removeHeaders(String pattern)
Removes the headers from this message
|
boolean |
removeHeaders(String pattern,
String... excludePatterns)
Removes the headers from this message that match the given pattern,
except for the ones matching one or more excludePatterns
|
void |
setBody(Object body)
Sets the body of the message
|
<T> void |
setBody(Object body,
Class<T> type)
Sets the body of the message as a specific type
|
void |
setHeader(String name,
Object value)
Sets a header on the message
|
void |
setHeaders(Map<String,Object> headers)
|
void |
setMessageId(String messageId)
Sets the id of the message
|
String getMessageId()
Exchange.getExchangeId()
as messages are associated with the exchange
and using different IDs does not offer much value. Another reason is to optimize for performance to avoid generating new IDs.
A few Camel components do provide their own message IDs such as the JMS components.void setMessageId(String messageId)
messageId
- id of the messageExchange getExchange()
Object getHeader(String name)
name
- name of headerObject getHeader(String name, Object defaultValue)
name
- name of headerdefaultValue
- the default value to return if header was absentObject getHeader(String name, Supplier<Object> defaultValueSupplier)
name
- name of headerdefaultValueSupplier
- the default value supplier used to generate the value to return if header was absent<T> T getHeader(String name, Class<T> type)
name
- the name of the headertype
- the type of the headerTypeConversionException
- is thrown if error during type conversion<T> T getHeader(String name, Object defaultValue, Class<T> type)
name
- the name of the headerdefaultValue
- the default value to return if header was absenttype
- the type of the header<T> T getHeader(String name, Supplier<Object> defaultValueSupplier, Class<T> type)
name
- the name of the headerdefaultValueSupplier
- the default value supplier used to generate the value to return if header was absenttype
- the type of the headervoid setHeader(String name, Object value)
name
- of the headervalue
- to associate with the nameObject removeHeader(String name)
name
- name of the headerboolean removeHeaders(String pattern)
pattern
- pattern of namesboolean removeHeaders(String pattern, String... excludePatterns)
pattern
- pattern of names that should be removedexcludePatterns
- one or more pattern of header names that should be excluded (= preserved)Map<String,Object> getHeaders()
CaseInsensitiveMap
.
The implementation of the map can be configured by the HeadersMapFactory
which can be set
on the CamelContext
. The default implementation uses the CaseInsensitiveMap
.
Important: If you want to walk the returned Map
and fetch all the keys and values, you should use
the Map.entrySet()
method, which ensure you get the keys in the original case.void setHeaders(Map<String,Object> headers)
Message
to this Message
, then
use getHeaders().putAll(other) to copy the headers, where other is the other headers.headers
- headers to setboolean hasHeaders()
Object getBody()
StreamCache.reset()
method to reset the stream to be able to re-read again (if possible).
See more details about stream caching.Object getMandatoryBody() throws InvalidPayloadException
InvalidPayloadException
- Is thrown if the body being null or wrong class type<T> T getBody(Class<T> type)
StreamCache.reset()
method to reset the stream to be able to re-read again (if possible).
See more details about stream caching.type
- the type that the bodyTypeConversionException
- is thrown if error during type conversion<T> T getMandatoryBody(Class<T> type) throws InvalidPayloadException
StreamCache.reset()
method to reset the stream to be able to re-read again (if possible).
See more details about stream caching.type
- the type that the bodyInvalidPayloadException
- Is thrown if the body being null or wrong class typevoid setBody(Object body)
body
- the body<T> void setBody(Object body, Class<T> type)
body
- the bodytype
- the type of the bodyMessage copy()
Message
copy will have its Exchange
set
to the same Exchange
instance as from the source.void copyFrom(Message message)
copyFromWithNewBody(Message, Object)
method instead.
The returned Message
copy will have its Exchange
set
to the same Exchange
instance as from the source.message
- the other messagecopyFromWithNewBody(Message, Object)
void copyFromWithNewBody(Message message, Object newBody)
Message
copy will have its Exchange
set
to the same Exchange
instance as from the source.message
- the other messagenewBody
- the new body to useApache Camel