Search in sources :

Example 56 with Converter

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

the class SpringIntegrationConverter method toSpringMessage.

@Converter
public static org.springframework.messaging.Message<?> toSpringMessage(final org.apache.camel.Message camelMessage) throws Exception {
    if (camelMessage instanceof SpringIntegrationMessage) {
        SpringIntegrationMessage siMessage = (SpringIntegrationMessage) camelMessage;
        org.springframework.messaging.Message<?> message = siMessage.getMessage();
        if (message != null) {
            return message;
        }
    }
    // Create a new spring message and copy the attributes and body from the camel message
    MessageHeaders messageHeaders = new MessageHeaders(camelMessage.getHeaders());
    return new GenericMessage<Object>(camelMessage.getBody(), messageHeaders);
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) MessageHeaders(org.springframework.messaging.MessageHeaders) SpringIntegrationMessage(org.apache.camel.component.spring.integration.SpringIntegrationMessage) Converter(org.apache.camel.Converter)

Example 57 with Converter

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

the class ElasticsearchActionRequestConverter method toMultiGetRequest.

@Converter
public static MultiGetRequest toMultiGetRequest(Object document, Exchange exchange) {
    List<Item> items = (List<Item>) document;
    MultiGetRequest multiGetRequest = new MultiGetRequest();
    Iterator<Item> it = items.iterator();
    while (it.hasNext()) {
        MultiGetRequest.Item item = it.next();
        multiGetRequest.add(item);
    }
    return multiGetRequest;
}
Also used : Item(org.elasticsearch.action.get.MultiGetRequest.Item) List(java.util.List) Item(org.elasticsearch.action.get.MultiGetRequest.Item) MultiGetRequest(org.elasticsearch.action.get.MultiGetRequest) Converter(org.apache.camel.Converter)

Example 58 with Converter

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

the class AnnotationTypeConverterLoader method loadConverterMethods.

/**
     * Loads all of the converter methods for the given type
     */
protected void loadConverterMethods(TypeConverterRegistry registry, Class<?> type) {
    if (visitedClasses.contains(type)) {
        return;
    }
    visitedClasses.add(type);
    try {
        Method[] methods = type.getDeclaredMethods();
        CachingInjector<?> injector = null;
        for (Method method : methods) {
            // in two different jars (as is the case sometimes with specs).
            if (ObjectHelper.hasAnnotation(method, Converter.class, true)) {
                boolean allowNull = false;
                if (method.getAnnotation(Converter.class) != null) {
                    allowNull = method.getAnnotation(Converter.class).allowNull();
                }
                injector = handleHasConverterAnnotation(registry, type, injector, method, allowNull);
            } else if (ObjectHelper.hasAnnotation(method, FallbackConverter.class, true)) {
                boolean allowNull = false;
                if (method.getAnnotation(FallbackConverter.class) != null) {
                    allowNull = method.getAnnotation(FallbackConverter.class).allowNull();
                }
                injector = handleHasFallbackConverterAnnotation(registry, type, injector, method, allowNull);
            }
        }
        Class<?> superclass = type.getSuperclass();
        if (superclass != null && !superclass.equals(Object.class)) {
            loadConverterMethods(registry, superclass);
        }
    } catch (NoClassDefFoundError e) {
        boolean ignore = false;
        // does the class allow to ignore the type converter when having load errors
        if (ObjectHelper.hasAnnotation(type, Converter.class, true)) {
            if (type.getAnnotation(Converter.class) != null) {
                ignore = type.getAnnotation(Converter.class).ignoreOnLoadError();
            }
        }
        // if we should ignore then only log at debug level
        if (ignore) {
            LOG.debug("Ignoring converter type: " + type.getCanonicalName() + " as a dependent class could not be found: " + e, e);
        } else {
            LOG.warn("Ignoring converter type: " + type.getCanonicalName() + " as a dependent class could not be found: " + e, e);
        }
    }
}
Also used : FallbackConverter(org.apache.camel.FallbackConverter) FallbackConverter(org.apache.camel.FallbackConverter) Converter(org.apache.camel.Converter) TypeConverter(org.apache.camel.TypeConverter) Method(java.lang.reflect.Method)

Example 59 with Converter

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

the class XmlConverter method toDOMNodeFromSAX.

@Converter
public Node toDOMNodeFromSAX(SAXSource source) throws ParserConfigurationException, IOException, SAXException, TransformerException {
    DOMResult result = new DOMResult();
    toResult(source, result);
    return result.getNode();
}
Also used : DOMResult(javax.xml.transform.dom.DOMResult) Converter(org.apache.camel.Converter)

Example 60 with Converter

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

the class XmlConverter method toByteArray.

/**
     * Converts the given input Source into bytes
     */
@Converter
public byte[] toByteArray(Source source, Exchange exchange) throws TransformerException {
    if (source instanceof BytesSource) {
        return ((BytesSource) source).getData();
    } else {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        if (exchange != null) {
            // check the camelContext properties first
            Properties properties = ObjectHelper.getCamelPropertiesWithPrefix(OUTPUT_PROPERTIES_PREFIX, exchange.getContext());
            if (properties.size() > 0) {
                toResult(source, new StreamResult(buffer), properties);
                return buffer.toByteArray();
            }
        }
        // using the old way to deal with it
        toResult(source, new StreamResult(buffer));
        return buffer.toByteArray();
    }
}
Also used : BytesSource(org.apache.camel.BytesSource) StreamResult(javax.xml.transform.stream.StreamResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Properties(java.util.Properties) Converter(org.apache.camel.Converter)

Aggregations

Converter (org.apache.camel.Converter)71 TypeConverter (org.apache.camel.TypeConverter)11 FallbackConverter (org.apache.camel.FallbackConverter)10 InputStream (java.io.InputStream)8 ByteBuffer (java.nio.ByteBuffer)7 IOConverter (org.apache.camel.converter.IOConverter)7 InputSource (org.xml.sax.InputSource)6 FileNotFoundException (java.io.FileNotFoundException)5 IOException (java.io.IOException)5 List (java.util.List)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 StringReader (java.io.StringReader)4 ArrayList (java.util.ArrayList)4 FileInputStream (java.io.FileInputStream)3 XMLStreamReader (javax.xml.stream.XMLStreamReader)3 SAXSource (javax.xml.transform.sax.SAXSource)3 MultiSearchRequest (org.elasticsearch.action.search.MultiSearchRequest)3 SearchRequest (org.elasticsearch.action.search.SearchRequest)3 Document (org.w3c.dom.Document)3 Element (org.w3c.dom.Element)3