Search in sources :

Example 11 with FallbackConverter

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;
}
Also used : TypeConverter(org.apache.camel.TypeConverter) InputStream(java.io.InputStream) Reader(java.io.Reader) BufferedReader(java.io.BufferedReader) File(java.io.File) FallbackConverter(org.apache.camel.FallbackConverter)

Example 12 with FallbackConverter

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;
}
Also used : TypeConverter(org.apache.camel.TypeConverter) FallbackConverter(org.apache.camel.FallbackConverter)

Aggregations

FallbackConverter (org.apache.camel.FallbackConverter)12 TypeConverter (org.apache.camel.TypeConverter)8 InputStream (java.io.InputStream)3 GenericFile (org.apache.camel.component.file.GenericFile)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Reader (java.io.Reader)2 Node (org.w3c.dom.Node)2 NodeList (org.w3c.dom.NodeList)2 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)1 HttpRequest (io.netty.handler.codec.http.HttpRequest)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 StringReader (java.io.StringReader)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 Response (javax.ws.rs.core.Response)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1 Source (javax.xml.transform.Source)1