Search in sources :

Example 6 with FallbackConverter

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

the class NettyHttpConverter method convertToHttpRequest.

/**
     * A fallback converter that allows us to easily call Java beans and use the raw Netty {@link HttpRequest} as parameter types.
     */
@FallbackConverter
public static Object convertToHttpRequest(Class<?> type, Exchange exchange, Object value, TypeConverterRegistry registry) {
    // if we want to covert to HttpRequest
    if (value != null && HttpRequest.class.isAssignableFrom(type)) {
        // okay we may need to cheat a bit when we want to grab the HttpRequest as its stored on the NettyHttpMessage
        // so if the message instance is a NettyHttpMessage and its body is the value, then we can grab the
        // HttpRequest from the NettyHttpMessage
        NettyHttpMessage msg;
        if (exchange.hasOut()) {
            msg = exchange.getOut(NettyHttpMessage.class);
        } else {
            msg = exchange.getIn(NettyHttpMessage.class);
        }
        if (msg != null && msg.getBody() == value) {
            // ensure the http request content is reset so we can read all the content out-of-the-box
            FullHttpRequest request = msg.getHttpRequest();
            request.content().resetReaderIndex();
            return request;
        }
    }
    return null;
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) FallbackConverter(org.apache.camel.FallbackConverter)

Example 7 with FallbackConverter

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

the class JacksonTypeConverters method convertTo.

@FallbackConverter
public <T> T convertTo(Class<T> type, Exchange exchange, Object value, TypeConverterRegistry registry) throws Exception {
    // only do this if enabled (disabled by default)
    if (!init && exchange != null) {
        // init to see if this is enabled
        String text = exchange.getContext().getProperties().get(JacksonConstants.ENABLE_TYPE_CONVERTER);
        if (text != null) {
            text = exchange.getContext().resolvePropertyPlaceholders(text);
            enabled = "true".equalsIgnoreCase(text);
        }
        // pojoOnly is enabled by default
        text = exchange.getContext().getProperties().get(JacksonConstants.TYPE_CONVERTER_TO_POJO);
        if (text != null) {
            text = exchange.getContext().resolvePropertyPlaceholders(text);
            toPojo = "true".equalsIgnoreCase(text);
        }
        init = true;
    }
    if (!enabled) {
        return null;
    }
    if (!toPojo && isNotPojoType(type)) {
        return null;
    }
    if (exchange != null) {
        ObjectMapper mapper = resolveObjectMapper(exchange.getContext().getRegistry());
        // if we want to convert to a String or byte[] then use write operation
        if (String.class.isAssignableFrom(type)) {
            String out = mapper.writeValueAsString(value);
            return type.cast(out);
        } else if (byte[].class.isAssignableFrom(type)) {
            byte[] out = mapper.writeValueAsBytes(value);
            return type.cast(out);
        } else if (mapper.canSerialize(type)) {
            // if the source value type is readable by the mapper then use its read operation
            if (String.class.isAssignableFrom(value.getClass())) {
                return mapper.readValue((String) value, type);
            } else if (byte[].class.isAssignableFrom(value.getClass())) {
                return mapper.readValue((byte[]) value, type);
            } else if (File.class.isAssignableFrom(value.getClass())) {
                return mapper.readValue((File) value, type);
            } else if (InputStream.class.isAssignableFrom(value.getClass())) {
                return mapper.readValue((InputStream) value, type);
            } else if (Reader.class.isAssignableFrom(value.getClass())) {
                return mapper.readValue((Reader) value, type);
            } else {
                // fallback to generic convert value
                return mapper.convertValue(value, type);
            }
        }
    }
    // Just return null to let other fallback converter to do the job
    return null;
}
Also used : InputStream(java.io.InputStream) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) FallbackConverter(org.apache.camel.FallbackConverter)

Example 8 with FallbackConverter

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

the class JcloudsPayloadConverter method convertTo.

@FallbackConverter
@SuppressWarnings("unchecked")
public static <T extends Payload> T convertTo(Class<T> type, Exchange exchange, Object value, TypeConverterRegistry registry) throws IOException {
    Class<?> sourceType = value.getClass();
    if (GenericFile.class.isAssignableFrom(sourceType)) {
        GenericFile<?> genericFile = (GenericFile<?>) value;
        if (genericFile.getFile() != null) {
            Class<?> genericFileType = genericFile.getFile().getClass();
            TypeConverter converter = registry.lookup(Payload.class, genericFileType);
            if (converter != null) {
                return (T) converter.convertTo(Payload.class, genericFile.getFile());
            }
        }
    }
    return null;
}
Also used : TypeConverter(org.apache.camel.TypeConverter) InputStreamPayload(org.jclouds.io.payloads.InputStreamPayload) ByteSourcePayload(org.jclouds.io.payloads.ByteSourcePayload) Payload(org.jclouds.io.Payload) GenericFile(org.apache.camel.component.file.GenericFile) FallbackConverter(org.apache.camel.FallbackConverter)

Example 9 with FallbackConverter

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

the class JacksonXMLTypeConverters method convertTo.

@FallbackConverter
public <T> T convertTo(Class<T> type, Exchange exchange, Object value, TypeConverterRegistry registry) {
    // only do this if enabled
    if (!init && exchange != null) {
        // init to see if this is enabled
        String text = exchange.getContext().getProperties().get(JacksonXMLConstants.ENABLE_XML_TYPE_CONVERTER);
        enabled = "true".equalsIgnoreCase(text);
        init = true;
    }
    if (enabled == null || !enabled) {
        return null;
    }
    if (isNotPojoType(type)) {
        return null;
    }
    if (exchange != null && value instanceof Map) {
        ObjectMapper mapper = resolveObjectMapper(exchange.getContext().getRegistry());
        if (mapper.canSerialize(type)) {
            return mapper.convertValue(value, type);
        }
    }
    // Just return null to let other fallback converter to do the job
    return null;
}
Also used : Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) FallbackConverter(org.apache.camel.FallbackConverter)

Example 10 with FallbackConverter

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

the class SaxonConverter method convertTo.

@FallbackConverter
public static <T> T convertTo(Class<T> type, Exchange exchange, Object value, TypeConverterRegistry registry) {
    if (NodeInfo.class.isAssignableFrom(value.getClass())) {
        // use a fallback type converter so we can convert the embedded body if the value is NodeInfo
        NodeInfo ni = (NodeInfo) value;
        // first try to find a Converter for Node
        TypeConverter tc = registry.lookup(type, Node.class);
        if (tc != null) {
            Node node = NodeOverNodeInfo.wrap(ni);
            return tc.convertTo(type, exchange, node);
        }
        // if this does not exist we can also try NodeList (there are some type converters for that) as
        // the default Xerces Node implementation also implements NodeList.
        tc = registry.lookup(type, NodeList.class);
        if (tc != null) {
            List<NodeInfo> nil = new LinkedList<NodeInfo>();
            nil.add((NodeInfo) value);
            return tc.convertTo(type, exchange, toDOMNodeList(nil));
        }
    } else if (List.class.isAssignableFrom(value.getClass())) {
        TypeConverter tc = registry.lookup(type, NodeList.class);
        if (tc != null) {
            List<NodeInfo> lion = new LinkedList<NodeInfo>();
            for (Object o : (List<?>) value) {
                if (o instanceof NodeInfo) {
                    lion.add((NodeInfo) o);
                }
            }
            if (lion.size() > 0) {
                NodeList nl = toDOMNodeList(lion);
                return tc.convertTo(type, exchange, nl);
            }
        }
    } else if (NodeOverNodeInfo.class.isAssignableFrom(value.getClass())) {
        // NodeOverNode info is a read-only Node implementation from Saxon. In contrast to the JDK
        // com.sun.org.apache.xerces.internal.dom.NodeImpl class it does not implement NodeList, but
        // many Camel type converters are based on that interface. Therefore we convert to NodeList and
        // try type conversion in the fallback type converter.
        TypeConverter tc = registry.lookup(type, NodeList.class);
        if (tc != null) {
            List<Node> domNodeList = new LinkedList<Node>();
            domNodeList.add((NodeOverNodeInfo) value);
            return tc.convertTo(type, exchange, new DOMNodeList(domNodeList));
        }
    }
    return null;
}
Also used : TypeConverter(org.apache.camel.TypeConverter) DOMNodeList(net.sf.saxon.dom.DOMNodeList) NodeOverNodeInfo(net.sf.saxon.dom.NodeOverNodeInfo) NodeInfo(net.sf.saxon.om.NodeInfo) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) DOMNodeList(net.sf.saxon.dom.DOMNodeList) NodeList(org.w3c.dom.NodeList) DOMNodeList(net.sf.saxon.dom.DOMNodeList) List(java.util.List) LinkedList(java.util.LinkedList) LinkedList(java.util.LinkedList) 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