use of org.apache.camel.TypeConverter in project camel by apache.
the class BeanConverter method convertTo.
@FallbackConverter
public static Object convertTo(Class<?> type, Exchange exchange, Object value, TypeConverterRegistry registry) {
// use a fallback type converter so we can convert the embedded body if the value is BeanInvocation
if (BeanInvocation.class.isAssignableFrom(value.getClass())) {
BeanInvocation bi = (BeanInvocation) value;
if (bi.getArgs() == null || bi.getArgs().length != 1) {
// not possible to convert at this time as we try to convert the data passed in at first argument
return Void.TYPE;
}
Class<?> from = bi.getArgs()[0].getClass();
Object body = bi.getArgs()[0];
// maybe from is already the type we want
if (type.isAssignableFrom(from)) {
return body;
}
// no then try to lookup a type converter
TypeConverter tc = registry.lookup(type, from);
if (tc != null) {
return tc.convertTo(type, exchange, body);
}
}
return null;
}
use of org.apache.camel.TypeConverter in project camel by apache.
the class ExchangeHelper method convertToType.
/**
* Converts the value to the given expected type
*
* @return the converted value
* @throws org.apache.camel.TypeConversionException is thrown if error during type conversion
*/
public static <T> T convertToType(Exchange exchange, Class<T> type, Object value) throws TypeConversionException {
CamelContext camelContext = exchange.getContext();
ObjectHelper.notNull(camelContext, "CamelContext of Exchange");
TypeConverter converter = camelContext.getTypeConverter();
if (converter != null) {
return converter.convertTo(type, exchange, value);
}
return null;
}
use of org.apache.camel.TypeConverter in project camel by apache.
the class TypeConverterRegistryTest method testDefaultTypeConverterRegistry.
public void testDefaultTypeConverterRegistry() {
DefaultCamelContext ctx = new DefaultCamelContext();
assertNotNull(ctx.getTypeConverterRegistry());
// file to input stream is a default converter in Camel
TypeConverter tc = ctx.getTypeConverterRegistry().lookup(InputStream.class, File.class);
assertNotNull(tc);
}
use of org.apache.camel.TypeConverter in project camel by apache.
the class CxfConverter method toInputStream.
@Converter
public static InputStream toInputStream(Response response, Exchange exchange) {
Object obj = response.getEntity();
if (obj == null) {
return null;
}
if (obj instanceof InputStream) {
// short circuit the lookup
return (InputStream) obj;
}
TypeConverterRegistry registry = exchange.getContext().getTypeConverterRegistry();
TypeConverter tc = registry.lookup(InputStream.class, obj.getClass());
if (tc != null) {
return tc.convertTo(InputStream.class, exchange, obj);
}
return null;
}
use of org.apache.camel.TypeConverter in project camel by apache.
the class CxfPayloadConverter method convertVia.
private static <T, V> CxfPayload<T> convertVia(Class<V> via, Exchange exchange, Object value, TypeConverterRegistry registry) {
TypeConverter tc = registry.lookup(via, value.getClass());
if (tc != null) {
TypeConverter tc1 = registry.lookup(Document.class, via);
if (tc1 != null) {
V is = tc.convertTo(via, exchange, value);
Document document = tc1.convertTo(Document.class, exchange, is);
return documentToCxfPayload(document, exchange);
}
}
return null;
}
Aggregations