org.apache.camel.util
Class ExchangeHelper

java.lang.Object
  extended by org.apache.camel.util.ExchangeHelper

public final class ExchangeHelper
extends Object

Some helper methods for working with Exchange objects

Version:

Method Summary
static
<T> T
convertToMandatoryType(Exchange exchange, Class<T> type, Object value)
          Converts the value to the given expected type or throws an exception
static
<T> T
convertToType(Exchange exchange, Class<T> type, Object value)
          Converts the value to the given expected type
static Exchange copyExchangeAndSetCamelContext(Exchange exchange, CamelContext context)
          Copies the exchange but the copy will be tied to the given context
static Exchange copyExchangeAndSetCamelContext(Exchange exchange, CamelContext context, boolean handover)
          Copies the exchange but the copy will be tied to the given context
static void copyResults(Exchange result, Exchange source)
          Copies the results of a message exchange from the source exchange to the result exchange which will copy the out and fault message contents and the exception
static void copyResultsPreservePattern(Exchange result, Exchange source)
          Copies the source exchange to target exchange preserving the ExchangePattern of target.
static Exchange createCopy(Exchange exchange, boolean preserveExchangeId)
          Creates a new instance and copies from the current message exchange so that it can be forwarded to another destination as a new instance.
static Exchange createCorrelatedCopy(Exchange exchange, boolean handover)
          Creates a new instance and copies from the current message exchange so that it can be forwarded to another destination as a new instance.
static String createExceptionMessage(String message, Exchange exchange, Throwable cause)
          Deprecated. use org.apache.camel.CamelExchangeException.createExceptionMessage instead
static Map<String,Object> createVariableMap(Exchange exchange)
          Creates a Map of the variables which are made available to a script or template
static
<T> T
extractFutureBody(CamelContext context, Future<Object> future, Class<T> type)
          Extracts the body from the given future, that represents a handle to an asynchronous exchange.
static
<T> T
extractFutureBody(CamelContext context, Future<Object> future, long timeout, TimeUnit unit, Class<T> type)
          Extracts the body from the given future, that represents a handle to an asynchronous exchange.
static Object extractResultBody(Exchange exchange, ExchangePattern pattern)
          Extracts the body from the given exchange.
static
<T> T
getBinding(Exchange exchange, Class<T> type)
          Extracts the Exchange.BINDING of the given type or null if not present
static String getContentEncoding(Exchange exchange)
          Returns the MIME content encoding on the input message or null if one is not defined
static String getContentType(Exchange exchange)
          Returns the MIME content type on the input message or null if one is not defined
static Exchange getExchangeById(Iterable<Exchange> exchanges, String exchangeId)
          Returns the first exchange in the given collection of exchanges which has the same exchange ID as the one given or null if none could be found
static
<T> T
getMandatoryHeader(Exchange exchange, String headerName, Class<T> type)
          Gets the mandatory inbound header of the correct type
static Object getMandatoryInBody(Exchange exchange)
          Deprecated. use Message.getMandatoryBody()
static
<T> T
getMandatoryInBody(Exchange exchange, Class<T> type)
          Deprecated. use Message.getMandatoryBody(Class)
static Object getMandatoryOutBody(Exchange exchange)
          Deprecated. use Message.getMandatoryBody()
static
<T> T
getMandatoryOutBody(Exchange exchange, Class<T> type)
          Deprecated. use Message.getMandatoryBody(Class)
static
<T> T
getMandatoryProperty(Exchange exchange, String propertyName, Class<T> type)
          Gets the mandatory property of the exchange of the correct type
static Message getResultMessage(Exchange exchange)
          Returns the message where to write results in an exchange-pattern-sensitive way.
static boolean hasExceptionBeenHandledByErrorHandler(Exchange exchange)
          Tests whether the exchange has already been handled by the error handler
static boolean hasFaultMessage(Exchange exchange)
          Tests whether the exchange has a fault message set and that its not null.
static boolean isFailureHandled(Exchange exchange)
          Checks whether the exchange has been failure handed
static boolean isInterrupted(Exchange exchange)
          Checks whether the exchange UnitOfWork has been interrupted during processing
static boolean isOutCapable(Exchange exchange)
          Returns true if the given exchange pattern (if defined) can support OUT messages
static boolean isRedelivered(Exchange exchange)
          Checks whether the exchange UnitOfWork is redelivered
static boolean isRedeliveryExhausted(Exchange exchange)
          Checks whether the exchange is redelivery exhausted
static boolean isUnitOfWorkExhausted(Exchange exchange)
          Checks whether the exchange UnitOfWork is exhausted
static String logIds(Exchange exchange)
          Gets both the messageId and exchangeId to be used for logging purposes.
static Object lookupBean(Exchange exchange, String name)
          Performs a lookup in the registry of the bean name
static
<T> T
lookupBean(Exchange exchange, String name, Class<T> type)
          Performs a lookup in the registry of the bean name and type
static Object lookupMandatoryBean(Exchange exchange, String name)
          Performs a lookup in the registry of the mandatory bean name and throws an exception if it could not be found
static
<T> T
lookupMandatoryBean(Exchange exchange, String name, Class<T> type)
          Performs a lookup in the registry of the mandatory bean name and throws an exception if it could not be found
static
<T> T
newInstance(Exchange exchange, Class<T> type)
          Creates a new instance of the given type from the injector
static void populateVariableMap(Exchange exchange, Map<String,Object> map)
          Populates the Map with the variables which are made available to a script or template
static void prepareAggregation(Exchange oldExchange, Exchange newExchange)
          Prepares the exchanges for aggregation.
static void prepareOutToIn(Exchange exchange)
          Strategy to prepare results before next iterator or when we are complete, which is done by copying OUT to IN, so there is only an IN as input for the next iteration.
static Endpoint resolveEndpoint(Exchange exchange, Object value)
          Attempts to resolve the endpoint for the given value
static void setFailureHandled(Exchange exchange)
          Sets the exchange to be failure handled.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getBinding

public static <T> T getBinding(Exchange exchange,
                               Class<T> type)
Extracts the Exchange.BINDING of the given type or null if not present

Parameters:
exchange - the message exchange
type - the expected binding type
Returns:
the binding object of the given type or null if it could not be found or converted

resolveEndpoint

public static Endpoint resolveEndpoint(Exchange exchange,
                                       Object value)
                                throws NoSuchEndpointException
Attempts to resolve the endpoint for the given value

Parameters:
exchange - the message exchange being processed
value - the value which can be an Endpoint or an object which provides a String representation of an endpoint via Object.toString()
Returns:
the endpoint
Throws:
NoSuchEndpointException - if the endpoint cannot be resolved

getMandatoryProperty

public static <T> T getMandatoryProperty(Exchange exchange,
                                         String propertyName,
                                         Class<T> type)
                              throws NoSuchPropertyException
Gets the mandatory property of the exchange of the correct type

Parameters:
exchange - the exchange
propertyName - the property name
type - the type
Returns:
the property value
Throws:
TypeConversionException - is thrown if error during type conversion
NoSuchPropertyException - is thrown if no property exists

getMandatoryHeader

public static <T> T getMandatoryHeader(Exchange exchange,
                                       String headerName,
                                       Class<T> type)
                            throws TypeConversionException,
                                   NoSuchHeaderException
Gets the mandatory inbound header of the correct type

Parameters:
exchange - the exchange
headerName - the header name
type - the type
Returns:
the header value
Throws:
TypeConversionException - is thrown if error during type conversion
NoSuchHeaderException - is thrown if no headers exists

getMandatoryInBody

@Deprecated
public static Object getMandatoryInBody(Exchange exchange)
                                 throws InvalidPayloadException
Deprecated. use Message.getMandatoryBody()

Returns the mandatory inbound message body of the correct type or throws an exception if it is not present

Parameters:
exchange - the exchange
Returns:
the body, is never null
Throws:
InvalidPayloadException - Is thrown if the body being null or wrong class type

getMandatoryInBody

@Deprecated
public static <T> T getMandatoryInBody(Exchange exchange,
                                                  Class<T> type)
                            throws InvalidPayloadException
Deprecated. use Message.getMandatoryBody(Class)

Returns the mandatory inbound message body of the correct type or throws an exception if it is not present

Throws:
InvalidPayloadException

getMandatoryOutBody

@Deprecated
public static Object getMandatoryOutBody(Exchange exchange)
                                  throws InvalidPayloadException
Deprecated. use Message.getMandatoryBody()

Returns the mandatory outbound message body of the correct type or throws an exception if it is not present

Throws:
InvalidPayloadException

getMandatoryOutBody

@Deprecated
public static <T> T getMandatoryOutBody(Exchange exchange,
                                                   Class<T> type)
                             throws InvalidPayloadException
Deprecated. use Message.getMandatoryBody(Class)

Returns the mandatory outbound message body of the correct type or throws an exception if it is not present

Throws:
InvalidPayloadException

convertToMandatoryType

public static <T> T convertToMandatoryType(Exchange exchange,
                                           Class<T> type,
                                           Object value)
                                throws TypeConversionException,
                                       NoTypeConversionAvailableException
Converts the value to the given expected type or throws an exception

Returns:
the converted value
Throws:
TypeConversionException - is thrown if error during type conversion
NoTypeConversionAvailableException} - if no type converters exists to convert to the given type
NoTypeConversionAvailableException

convertToType

public static <T> T convertToType(Exchange exchange,
                                  Class<T> type,
                                  Object value)
                       throws TypeConversionException
Converts the value to the given expected type

Returns:
the converted value
Throws:
TypeConversionException - is thrown if error during type conversion

createCorrelatedCopy

public static Exchange createCorrelatedCopy(Exchange exchange,
                                            boolean handover)
Creates a new instance and copies from the current message exchange so that it can be forwarded to another destination as a new instance. Unlike regular copy this operation will not share the same UnitOfWork so its should be used for async messaging, where the original and copied exchange are independent.

Parameters:
exchange - original copy of the exchange
handover - whether the on completion callbacks should be handed over to the new copy.

createCopy

public static Exchange createCopy(Exchange exchange,
                                  boolean preserveExchangeId)
Creates a new instance and copies from the current message exchange so that it can be forwarded to another destination as a new instance.

Parameters:
exchange - original copy of the exchange
preserveExchangeId - whether or not the exchange id should be preserved
Returns:
the copy

copyResults

public static void copyResults(Exchange result,
                               Exchange source)
Copies the results of a message exchange from the source exchange to the result exchange which will copy the out and fault message contents and the exception

Parameters:
result - the result exchange which will have the output and error state added
source - the source exchange which is not modified

copyResultsPreservePattern

public static void copyResultsPreservePattern(Exchange result,
                                              Exchange source)
Copies the source exchange to target exchange preserving the ExchangePattern of target.

Parameters:
source - source exchange.
result - target exchange.

getResultMessage

public static Message getResultMessage(Exchange exchange)
Returns the message where to write results in an exchange-pattern-sensitive way.

Parameters:
exchange - message exchange.
Returns:
result message.

isOutCapable

public static boolean isOutCapable(Exchange exchange)
Returns true if the given exchange pattern (if defined) can support OUT messages

Parameters:
exchange - the exchange to interrogate
Returns:
true if the exchange is defined as an ExchangePattern which supports OUT messages

newInstance

public static <T> T newInstance(Exchange exchange,
                                Class<T> type)
Creates a new instance of the given type from the injector

Parameters:
exchange - the exchange
type - the given type
Returns:
the created instance of the given type

createVariableMap

public static Map<String,Object> createVariableMap(Exchange exchange)
Creates a Map of the variables which are made available to a script or template

Parameters:
exchange - the exchange to make available
Returns:
a Map populated with the require variables

populateVariableMap

public static void populateVariableMap(Exchange exchange,
                                       Map<String,Object> map)
Populates the Map with the variables which are made available to a script or template

Parameters:
exchange - the exchange to make available
map - the map to populate

getContentType

public static String getContentType(Exchange exchange)
Returns the MIME content type on the input message or null if one is not defined

Parameters:
exchange - the exchange
Returns:
the MIME content type

getContentEncoding

public static String getContentEncoding(Exchange exchange)
Returns the MIME content encoding on the input message or null if one is not defined

Parameters:
exchange - the exchange
Returns:
the MIME content encoding

lookupMandatoryBean

public static Object lookupMandatoryBean(Exchange exchange,
                                         String name)
                                  throws NoSuchBeanException
Performs a lookup in the registry of the mandatory bean name and throws an exception if it could not be found

Parameters:
exchange - the exchange
name - the bean name
Returns:
the bean
Throws:
NoSuchBeanException - if no bean could be found in the registry

lookupMandatoryBean

public static <T> T lookupMandatoryBean(Exchange exchange,
                                        String name,
                                        Class<T> type)
Performs a lookup in the registry of the mandatory bean name and throws an exception if it could not be found

Parameters:
exchange - the exchange
name - the bean name
type - the expected bean type
Returns:
the bean
Throws:
NoSuchBeanException - if no bean could be found in the registry

lookupBean

public static Object lookupBean(Exchange exchange,
                                String name)
Performs a lookup in the registry of the bean name

Parameters:
exchange - the exchange
name - the bean name
Returns:
the bean, or null if no bean could be found

lookupBean

public static <T> T lookupBean(Exchange exchange,
                               String name,
                               Class<T> type)
Performs a lookup in the registry of the bean name and type

Parameters:
exchange - the exchange
name - the bean name
type - the expected bean type
Returns:
the bean, or null if no bean could be found

getExchangeById

public static Exchange getExchangeById(Iterable<Exchange> exchanges,
                                       String exchangeId)
Returns the first exchange in the given collection of exchanges which has the same exchange ID as the one given or null if none could be found

Parameters:
exchanges - the exchanges
exchangeId - the exchangeId to find
Returns:
matching exchange, or null if none found

prepareAggregation

public static void prepareAggregation(Exchange oldExchange,
                                      Exchange newExchange)
Prepares the exchanges for aggregation.

This implementation will copy the OUT body to the IN body so when you do aggregation the body is only in the IN body to avoid confusing end users.

Parameters:
oldExchange - the old exchange
newExchange - the new exchange

isFailureHandled

public static boolean isFailureHandled(Exchange exchange)
Checks whether the exchange has been failure handed

Parameters:
exchange - the exchange
Returns:
true if failure handled, false otherwise

isUnitOfWorkExhausted

public static boolean isUnitOfWorkExhausted(Exchange exchange)
Checks whether the exchange UnitOfWork is exhausted

Parameters:
exchange - the exchange
Returns:
true if exhausted, false otherwise

setFailureHandled

public static void setFailureHandled(Exchange exchange)
Sets the exchange to be failure handled.

Parameters:
exchange - the exchange

isRedeliveryExhausted

public static boolean isRedeliveryExhausted(Exchange exchange)
Checks whether the exchange is redelivery exhausted

Parameters:
exchange - the exchange
Returns:
true if exhausted, false otherwise

isRedelivered

public static boolean isRedelivered(Exchange exchange)
Checks whether the exchange UnitOfWork is redelivered

Parameters:
exchange - the exchange
Returns:
true if redelivered, false otherwise

isInterrupted

public static boolean isInterrupted(Exchange exchange)
Checks whether the exchange UnitOfWork has been interrupted during processing

Parameters:
exchange - the exchange
Returns:
true if interrupted, false otherwise

extractResultBody

public static Object extractResultBody(Exchange exchange,
                                       ExchangePattern pattern)
Extracts the body from the given exchange.

If the exchange pattern is provided it will try to honor it and retrieve the body from either IN or OUT according to the pattern.

Parameters:
exchange - the exchange
pattern - exchange pattern if given, can be null
Returns:
the result body, can be null.
Throws:
CamelExecutionException - is thrown if the processing of the exchange failed

hasFaultMessage

public static boolean hasFaultMessage(Exchange exchange)
Tests whether the exchange has a fault message set and that its not null.

Parameters:
exchange - the exchange
Returns:
true if fault message exists

hasExceptionBeenHandledByErrorHandler

public static boolean hasExceptionBeenHandledByErrorHandler(Exchange exchange)
Tests whether the exchange has already been handled by the error handler

Parameters:
exchange - the exchange
Returns:
true if handled already by error handler, false otherwise

extractFutureBody

public static <T> T extractFutureBody(CamelContext context,
                                      Future<Object> future,
                                      Class<T> type)
Extracts the body from the given future, that represents a handle to an asynchronous exchange.

Will wait until the future task is complete.

Parameters:
context - the camel context
future - the future handle
type - the expected body response type
Returns:
the result body, can be null.
Throws:
CamelExecutionException - is thrown if the processing of the exchange failed

extractFutureBody

public static <T> T extractFutureBody(CamelContext context,
                                      Future<Object> future,
                                      long timeout,
                                      TimeUnit unit,
                                      Class<T> type)
                           throws TimeoutException
Extracts the body from the given future, that represents a handle to an asynchronous exchange.

Will wait for the future task to complete, but waiting at most the timeout value.

Parameters:
context - the camel context
future - the future handle
timeout - timeout value
unit - timeout unit
type - the expected body response type
Returns:
the result body, can be null.
Throws:
CamelExecutionException - is thrown if the processing of the exchange failed
TimeoutException - is thrown if a timeout triggered

createExceptionMessage

@Deprecated
public static String createExceptionMessage(String message,
                                                       Exchange exchange,
                                                       Throwable cause)
Deprecated. use org.apache.camel.CamelExchangeException.createExceptionMessage instead


prepareOutToIn

public static void prepareOutToIn(Exchange exchange)
Strategy to prepare results before next iterator or when we are complete, which is done by copying OUT to IN, so there is only an IN as input for the next iteration.

Parameters:
exchange - the exchange to prepare

logIds

public static String logIds(Exchange exchange)
Gets both the messageId and exchangeId to be used for logging purposes.

Logging both ids, can help to correlate exchanges which may be redelivered messages from for example a JMS broker.

Parameters:
exchange - the exchange
Returns:
a log message with both the messageId and exchangeId

copyExchangeAndSetCamelContext

public static Exchange copyExchangeAndSetCamelContext(Exchange exchange,
                                                      CamelContext context)
Copies the exchange but the copy will be tied to the given context

Parameters:
exchange - the source exchange
context - the camel context
Returns:
a copy with the given camel context

copyExchangeAndSetCamelContext

public static Exchange copyExchangeAndSetCamelContext(Exchange exchange,
                                                      CamelContext context,
                                                      boolean handover)
Copies the exchange but the copy will be tied to the given context

Parameters:
exchange - the source exchange
context - the camel context
handover - whether to handover on completions from the source to the copy
Returns:
a copy with the given camel context


Apache CAMEL