Exchange e = getExchange();
if (e != null) {
CamelContext camelContext = e.getContext();
if (camelContext != null) {
boolean tryConvert = true;
TypeConverter converter = camelContext.getTypeConverter();
// if its the default type converter then use a performance shortcut to check if it can convert it
// this is faster than getting throwing and catching NoTypeConversionAvailableException
// the StreamCachingInterceptor will attempt to convert the payload to a StremCache for caching purpose
// so we get invoked on each node the exchange passes. So this is a little performance optimization
// to avoid the excessive exception handling
if (body != null && converter instanceof DefaultTypeConverter) {
DefaultTypeConverter defaultTypeConverter = (DefaultTypeConverter) converter;
// we can only check if there is no converter meaning we have tried to convert it beforehand
// and then knows for sure there is no converter possible
tryConvert = !defaultTypeConverter.hasNoConverterFor(type, body.getClass());
}
if (tryConvert) {
try {
// lets first try converting the body itself first
// as for some types like InputStream v Reader its more efficient to do the transformation
// from the body itself as its got efficient implementations of them, before trying the
// message
return converter.convertTo(type, e, body);
} catch (NoTypeConversionAvailableException ex) {
// ignore
}
}
// fallback to the message itself
return converter.convertTo(type, this);
}
}
return (T)getBody();
}