Search in sources :

Example 46 with TypeConverter

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

the class XsltBuilder method getSource.

/**
     * Converts the inbound body to a {@link Source}, if the body is <b>not</b> already a {@link Source}.
     * <p/>
     * This implementation will prefer to source in the following order:
     * <ul>
     *   <li>StAX - If StAX is allowed</li>
     *   <li>SAX - SAX as 2nd choice</li>
     *   <li>Stream - Stream as 3rd choice</li>
     *   <li>DOM - DOM as 4th choice</li>
     * </ul>
     */
protected Source getSource(Exchange exchange, Object body) {
    // body may already be a source
    if (body instanceof Source) {
        return (Source) body;
    }
    Source source = null;
    if (body != null) {
        if (isAllowStAX()) {
            // try StAX if enabled
            source = exchange.getContext().getTypeConverter().tryConvertTo(StAXSource.class, exchange, body);
        }
        if (source == null) {
            // then try SAX
            source = exchange.getContext().getTypeConverter().tryConvertTo(SAXSource.class, exchange, body);
            tryAddEntityResolver((SAXSource) source);
        }
        if (source == null) {
            // then try stream
            source = exchange.getContext().getTypeConverter().tryConvertTo(StreamSource.class, exchange, body);
        }
        if (source == null) {
            // and fallback to DOM
            source = exchange.getContext().getTypeConverter().tryConvertTo(DOMSource.class, exchange, body);
        }
        // now we just put the call of source converter at last
        if (source == null) {
            TypeConverter tc = exchange.getContext().getTypeConverterRegistry().lookup(Source.class, body.getClass());
            if (tc != null) {
                source = tc.convertTo(Source.class, exchange, body);
            }
        }
    }
    if (source == null) {
        if (isFailOnNullBody()) {
            throw new ExpectedBodyTypeException(exchange, Source.class);
        } else {
            try {
                source = converter.toDOMSource(converter.createDocument());
            } catch (ParserConfigurationException e) {
                throw new RuntimeTransformException(e);
            }
        }
    }
    return source;
}
Also used : TypeConverter(org.apache.camel.TypeConverter) DOMSource(javax.xml.transform.dom.DOMSource) ExpectedBodyTypeException(org.apache.camel.ExpectedBodyTypeException) StAX2SAXSource(org.apache.camel.converter.jaxp.StAX2SAXSource) SAXSource(javax.xml.transform.sax.SAXSource) StreamSource(javax.xml.transform.stream.StreamSource) StAXSource(javax.xml.transform.stax.StAXSource) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) RuntimeTransformException(org.apache.camel.RuntimeTransformException) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) StAX2SAXSource(org.apache.camel.converter.jaxp.StAX2SAXSource) Source(javax.xml.transform.Source) SAXSource(javax.xml.transform.sax.SAXSource) StAXSource(javax.xml.transform.stax.StAXSource)

Example 47 with TypeConverter

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

the class BaseTypeConverterRegistry method doLookup.

protected TypeConverter doLookup(Class<?> toType, Class<?> fromType, boolean isSuper) {
    if (fromType != null) {
        // lets try if there is a direct match
        TypeConverter converter = getTypeConverter(toType, fromType);
        if (converter != null) {
            return converter;
        }
        // try the interfaces
        for (Class<?> type : fromType.getInterfaces()) {
            converter = getTypeConverter(toType, type);
            if (converter != null) {
                return converter;
            }
        }
        // try super then
        Class<?> fromSuperClass = fromType.getSuperclass();
        if (fromSuperClass != null && !fromSuperClass.equals(Object.class)) {
            converter = doLookup(toType, fromSuperClass, true);
            if (converter != null) {
                return converter;
            }
        }
    }
    // only do these tests as fallback and only on the target type (eg not on its super)
    if (!isSuper) {
        if (fromType != null && !fromType.equals(Object.class)) {
            // lets try classes derived from this toType
            Set<Map.Entry<TypeMapping, TypeConverter>> entries = typeMappings.entrySet();
            for (Map.Entry<TypeMapping, TypeConverter> entry : entries) {
                TypeMapping key = entry.getKey();
                Class<?> aToType = key.getToType();
                if (toType.isAssignableFrom(aToType)) {
                    Class<?> aFromType = key.getFromType();
                    // skip Object based we do them last
                    if (!aFromType.equals(Object.class) && aFromType.isAssignableFrom(fromType)) {
                        return entry.getValue();
                    }
                }
            }
            // lets test for Object based converters as last resort
            TypeConverter converter = getTypeConverter(toType, Object.class);
            if (converter != null) {
                return converter;
            }
        }
    }
    // none found
    return null;
}
Also used : TypeConverter(org.apache.camel.TypeConverter) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 48 with TypeConverter

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

the class LazyLoadingTypeConverter method getTypeConverter.

@Override
public TypeConverter getTypeConverter(Class<?> toType, Class<?> fromType) {
    TypeConverter answer = super.getTypeConverter(toType, fromType);
    if (answer == null && !loaded.get()) {
        // okay we could not convert, so try again, but load the converters up front
        ensureLoaded();
        answer = super.getTypeConverter(toType, fromType);
    }
    return answer;
}
Also used : TypeConverter(org.apache.camel.TypeConverter)

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