use of org.apache.camel.FallbackConverter in project camel by apache.
the class GenericFileConverter method convertTo.
@FallbackConverter
public static Object convertTo(Class<?> type, Exchange exchange, Object value, TypeConverterRegistry registry) throws IOException, NoTypeConversionAvailableException {
// use a fallback type converter so we can convert the embedded body if the value is GenericFile
if (GenericFile.class.isAssignableFrom(value.getClass())) {
GenericFile<?> file = (GenericFile<?>) value;
Class<?> from = file.getBody().getClass();
// maybe from is already the type we want
if (from.isAssignableFrom(type)) {
return file.getBody();
}
// no then try to lookup a type converter
TypeConverter tc = registry.lookup(type, from);
if (tc != null) {
Object body = file.getBody();
// if the desired type is InputStream or Reader we can use the optimized methods
if (Reader.class.isAssignableFrom(type)) {
Reader reader = genericFileToReader(file, exchange);
if (reader != null) {
return reader;
}
}
if (InputStream.class.isAssignableFrom(type)) {
InputStream is = genericFileToInputStream(file, exchange);
if (is != null) {
return is;
}
}
// which mean we have to use the Reader first, and then convert from there
if (body instanceof File && file.getCharset() != null) {
Reader reader = genericFileToReader(file, exchange);
// we dont want a reader back, so use the type converter registry to find a suitable converter
TypeConverter readerTc = registry.lookup(type, Reader.class);
if (readerTc != null) {
// use the reader based type converter
return readerTc.convertTo(type, exchange, reader);
}
}
// fallback and use the type suitable type converter
return tc.convertTo(type, exchange, body);
}
}
return null;
}
use of org.apache.camel.FallbackConverter 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;
}
Aggregations