Search in sources :

Example 1 with TypeConverter

use of org.apache.camel.TypeConverter in project camel by apache.

the class MyOtherTypeConverter 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 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();
            return tc.convertTo(type, exchange, body);
        }
    }
    return null;
}
Also used : TypeConverter(org.apache.camel.TypeConverter) GenericFile(org.apache.camel.component.file.GenericFile) FallbackConverter(org.apache.camel.FallbackConverter)

Example 2 with TypeConverter

use of org.apache.camel.TypeConverter in project camel by apache.

the class MyTypeConverter 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 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();
            return tc.convertTo(type, exchange, body);
        }
    }
    return null;
}
Also used : TypeConverter(org.apache.camel.TypeConverter) GenericFile(org.apache.camel.component.file.GenericFile) FallbackConverter(org.apache.camel.FallbackConverter)

Example 3 with TypeConverter

use of org.apache.camel.TypeConverter in project camel by apache.

the class CxfConverter method convertTo.

/**
     * Use a fallback type converter so we can convert the embedded list element 
     * if the value is MessageContentsList.  The algorithm of this converter
     * finds the first non-null list element from the list and applies conversion
     * to the list element.
     * 
     * @param type the desired type to be converted to
     * @param exchange optional exchange which can be null
     * @param value the object to be converted
     * @param registry type converter registry
     * @return the converted value of the desired type or null if no suitable converter found
     */
@SuppressWarnings("unchecked")
@FallbackConverter
public static <T> T convertTo(Class<T> type, Exchange exchange, Object value, TypeConverterRegistry registry) {
    // CXF-WS MessageContentsList class
    if (MessageContentsList.class.isAssignableFrom(value.getClass())) {
        MessageContentsList list = (MessageContentsList) value;
        // try to turn the first array element into the object that we want
        for (Object embedded : list) {
            if (embedded != null) {
                if (type.isInstance(embedded)) {
                    return type.cast(embedded);
                } else {
                    TypeConverter tc = registry.lookup(type, embedded.getClass());
                    if (tc != null) {
                        Object result = tc.convertTo(type, exchange, embedded);
                        if (result != null) {
                            return (T) result;
                        }
                        // there is no suitable result will be return
                        break;
                    }
                }
            }
        }
        // return void to indicate its not possible to convert at this time
        return (T) Void.TYPE;
    }
    // CXF-RS Response class
    if (Response.class.isAssignableFrom(value.getClass())) {
        Response response = (Response) value;
        Object entity = response.getEntity();
        TypeConverter tc = registry.lookup(type, entity.getClass());
        if (tc != null) {
            return tc.convertTo(type, exchange, entity);
        }
        // return void to indicate its not possible to convert at this time
        return (T) Void.TYPE;
    }
    return null;
}
Also used : TypeConverter(org.apache.camel.TypeConverter) Response(javax.ws.rs.core.Response) MessageContentsList(org.apache.cxf.message.MessageContentsList) FallbackConverter(org.apache.camel.FallbackConverter)

Example 4 with TypeConverter

use of org.apache.camel.TypeConverter in project camel by apache.

the class CxfPayloadConverter method convertTo.

@SuppressWarnings("unchecked")
@FallbackConverter
public static <T> T convertTo(Class<T> type, Exchange exchange, Object value, TypeConverterRegistry registry) {
    // CxfPayloads from other types
    if (type.isAssignableFrom(CxfPayload.class)) {
        try {
            if (!value.getClass().isArray()) {
                Source src = null;
                // directly
                if (value instanceof InputStream) {
                    src = new StreamSource((InputStream) value);
                } else if (value instanceof Reader) {
                    src = new StreamSource((Reader) value);
                } else if (value instanceof String) {
                    src = new StreamSource(new StringReader((String) value));
                } else if (value instanceof Node) {
                    src = new DOMSource((Node) value);
                } else if (value instanceof Source) {
                    src = (Source) value;
                }
                if (src == null) {
                    // assuming staxsource is preferred, otherwise use the
                    // one preferred
                    TypeConverter tc = registry.lookup(javax.xml.transform.stax.StAXSource.class, value.getClass());
                    if (tc == null) {
                        tc = registry.lookup(Source.class, value.getClass());
                    }
                    if (tc != null) {
                        src = tc.convertTo(Source.class, exchange, value);
                    }
                }
                if (src != null) {
                    return (T) sourceToCxfPayload(src, exchange);
                }
            }
            TypeConverter tc = registry.lookup(NodeList.class, value.getClass());
            if (tc != null) {
                NodeList nodeList = tc.convertTo(NodeList.class, exchange, value);
                return (T) nodeListToCxfPayload(nodeList, exchange);
            }
            tc = registry.lookup(Document.class, value.getClass());
            if (tc != null) {
                Document document = tc.convertTo(Document.class, exchange, value);
                return (T) documentToCxfPayload(document, exchange);
            }
            // maybe we can convert via an InputStream
            CxfPayload<?> p;
            p = convertVia(InputStream.class, exchange, value, registry);
            if (p != null) {
                return (T) p;
            }
            // String is the converter of last resort
            p = convertVia(String.class, exchange, value, registry);
            if (p != null) {
                return (T) p;
            }
        } catch (RuntimeCamelException e) {
        // the internal conversion to XML can throw an exception if the content is not XML
        // ignore this and return Void.TYPE to indicate that we cannot convert this
        }
        // no we could not do it currently
        return (T) Void.TYPE;
    }
    // Convert a CxfPayload into something else
    if (CxfPayload.class.isAssignableFrom(value.getClass())) {
        CxfPayload<?> payload = (CxfPayload<?>) value;
        int size = payload.getBodySources().size();
        if (size == 1) {
            if (type.isAssignableFrom(Document.class)) {
                Source s = payload.getBodySources().get(0);
                Document d;
                try {
                    d = StaxUtils.read(s);
                } catch (XMLStreamException e) {
                    throw new RuntimeException(e);
                }
                return type.cast(d);
            }
            // CAMEL-8410 Just make sure we get the Source object directly from the payload body source
            Source s = payload.getBodySources().get(0);
            if (type.isInstance(s)) {
                return type.cast(s);
            }
            TypeConverter tc = registry.lookup(type, XMLStreamReader.class);
            if (tc != null && (s instanceof StaxSource || s instanceof StAXSource)) {
                XMLStreamReader r = (s instanceof StAXSource) ? ((StAXSource) s).getXMLStreamReader() : ((StaxSource) s).getXMLStreamReader();
                if (payload.getNsMap() != null) {
                    r = new DelegatingXMLStreamReader(r, payload.getNsMap());
                }
                return tc.convertTo(type, exchange, r);
            }
            tc = registry.lookup(type, Source.class);
            if (tc != null) {
                XMLStreamReader r = null;
                if (payload.getNsMap() != null) {
                    if (s instanceof StaxSource) {
                        r = ((StaxSource) s).getXMLStreamReader();
                    } else if (s instanceof StAXSource) {
                        r = ((StAXSource) s).getXMLStreamReader();
                    }
                    if (r != null) {
                        s = new StAXSource(new DelegatingXMLStreamReader(r, payload.getNsMap()));
                    }
                }
                return tc.convertTo(type, exchange, s);
            }
        }
        TypeConverter tc = registry.lookup(type, NodeList.class);
        if (tc != null) {
            Object result = tc.convertTo(type, exchange, cxfPayloadToNodeList((CxfPayload<?>) value, exchange));
            if (result == null) {
                // no we could not do it currently, and we just abort the convert here
                return (T) Void.TYPE;
            } else {
                return (T) result;
            }
        }
        // we cannot convert a node list, so we try the first item from the
        // node list
        tc = registry.lookup(type, Node.class);
        if (tc != null) {
            NodeList nodeList = cxfPayloadToNodeList((CxfPayload<?>) value, exchange);
            if (nodeList.getLength() > 0) {
                return tc.convertTo(type, exchange, nodeList.item(0));
            } else {
                // no we could not do it currently
                return (T) Void.TYPE;
            }
        } else {
            if (size == 0) {
                // empty size so we cannot convert
                return (T) Void.TYPE;
            }
        }
    }
    return null;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) XMLStreamReader(javax.xml.stream.XMLStreamReader) CxfPayload(org.apache.camel.component.cxf.CxfPayload) Node(org.w3c.dom.Node) XMLStreamReader(javax.xml.stream.XMLStreamReader) Reader(java.io.Reader) StringReader(java.io.StringReader) Document(org.w3c.dom.Document) DOMSource(javax.xml.transform.dom.DOMSource) StaxSource(org.apache.cxf.staxutils.StaxSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) StAXSource(javax.xml.transform.stax.StAXSource) StringReader(java.io.StringReader) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) NodeList(org.w3c.dom.NodeList) StAXSource(javax.xml.transform.stax.StAXSource) TypeConverter(org.apache.camel.TypeConverter) XMLStreamException(javax.xml.stream.XMLStreamException) StaxSource(org.apache.cxf.staxutils.StaxSource) RuntimeCamelException(org.apache.camel.RuntimeCamelException) FallbackConverter(org.apache.camel.FallbackConverter)

Example 5 with TypeConverter

use of org.apache.camel.TypeConverter in project camel by apache.

the class DefaultComponentVerifier method setProperties.

protected <T> T setProperties(T instance, Map<String, Object> properties) throws Exception {
    if (camelContext == null) {
        throw new IllegalStateException("Camel context is null");
    }
    if (!properties.isEmpty()) {
        final TypeConverter converter = camelContext.getTypeConverter();
        IntrospectionSupport.setProperties(converter, instance, properties);
        for (Map.Entry<String, Object> entry : properties.entrySet()) {
            if (entry.getValue() instanceof String) {
                String value = (String) entry.getValue();
                if (EndpointHelper.isReferenceParameter(value)) {
                    IntrospectionSupport.setProperty(camelContext, converter, instance, entry.getKey(), null, value, true);
                }
            }
        }
    }
    return instance;
}
Also used : TypeConverter(org.apache.camel.TypeConverter) Map(java.util.Map)

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