Search in sources :

Example 6 with Converter

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

the class ElasticsearchActionRequestConverter method toMultiSearchRequest.

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

Example 7 with Converter

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

the class DomConverter method toString.

@Converter
public String toString(NodeList nodeList, Exchange exchange) throws TransformerException {
    // converting NodeList to String is more tricky
    // sometimes the NodeList is a Node which we can then leverage
    // the XML converter to turn into XML incl. tags
    StringBuilder buffer = new StringBuilder();
    // use XML converter at first since it preserves tag names
    boolean found = false;
    if (nodeList instanceof Node) {
        Node node = (Node) nodeList;
        String s = toString(node, exchange);
        if (ObjectHelper.isNotEmpty(s)) {
            found = true;
            buffer.append(s);
        }
    } else {
        // use XML converter at first since it preserves tag names
        int size = nodeList.getLength();
        for (int i = 0; i < size; i++) {
            Node node = nodeList.item(i);
            String s = toString(node, exchange);
            if (ObjectHelper.isNotEmpty(s)) {
                found = true;
                buffer.append(s);
            }
        }
    }
    // used an xpath to select an attribute or text() or something
    if (!found) {
        append(buffer, nodeList);
    }
    return buffer.toString();
}
Also used : Node(org.w3c.dom.Node) Converter(org.apache.camel.Converter)

Example 8 with Converter

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

the class XmlConverter method toString.

/**
     * Converts the given input Source into text
     */
@Converter
public String toString(Source source, Exchange exchange) throws TransformerException {
    if (source == null) {
        return null;
    } else if (source instanceof StringSource) {
        return ((StringSource) source).getText();
    } else if (source instanceof BytesSource) {
        return new String(((BytesSource) source).getData());
    } else {
        StringWriter buffer = new StringWriter();
        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.toString();
            }
        }
        // using the old way to deal with it
        toResult(source, new StreamResult(buffer));
        return buffer.toString();
    }
}
Also used : BytesSource(org.apache.camel.BytesSource) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) StringSource(org.apache.camel.StringSource) Properties(java.util.Properties) Converter(org.apache.camel.Converter)

Example 9 with Converter

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

the class XmlConverter method toDOMNodeFromStAX.

@Converter
public Node toDOMNodeFromStAX(StAXSource 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 10 with Converter

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

the class XmlConverter method toSAXSourceFromStAX.

@Converter
public SAXSource toSAXSourceFromStAX(StAXSource source, Exchange exchange) throws TransformerException {
    String str = toString(source, exchange);
    StringReader reader = new StringReader(str);
    return new SAXSource(new InputSource(reader));
}
Also used : InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) StringReader(java.io.StringReader) 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