Search in sources :

Example 41 with TypeConverter

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

Example 42 with TypeConverter

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

Example 43 with TypeConverter

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

Example 44 with TypeConverter

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

Example 45 with TypeConverter

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

Aggregations

TypeConverter (org.apache.camel.TypeConverter)48 Map (java.util.Map)13 FallbackConverter (org.apache.camel.FallbackConverter)9 InputStream (java.io.InputStream)6 HashMap (java.util.HashMap)6 Exchange (org.apache.camel.Exchange)6 RuntimeCamelException (org.apache.camel.RuntimeCamelException)6 ArrayList (java.util.ArrayList)5 NoTypeConversionAvailableException (org.apache.camel.NoTypeConversionAvailableException)5 HttpString (io.undertow.util.HttpString)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 ObjectOutputStream (java.io.ObjectOutputStream)4 PrintWriter (java.io.PrintWriter)4 StringWriter (java.io.StringWriter)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 List (java.util.List)4 HeaderMap (io.undertow.util.HeaderMap)3 IOException (java.io.IOException)3 URI (java.net.URI)3 Message (org.apache.camel.Message)3