Search in sources :

Example 41 with Converter

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

the class FlatpackConverter method toDocument.

@Converter
public static Document toDocument(DataSet dataSet) throws ParserConfigurationException {
    Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    if (dataSet.getIndex() == -1) {
        Element list = doc.createElement("Dataset");
        dataSet.goTop();
        while (dataSet.next()) {
            list.appendChild(createDatasetRecord(dataSet, doc));
        }
        doc.appendChild(list);
    } else {
        doc.appendChild(createDatasetRecord(dataSet, doc));
    }
    return doc;
}
Also used : Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) Converter(org.apache.camel.Converter)

Example 42 with Converter

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

the class JcloudsPayloadConverter method toPayload.

@Converter
public static Payload toPayload(final StreamSourceCache cache, Exchange exchange) throws IOException {
    long contentLength = ByteStreams.length(new InputSupplier<InputStream>() {

        @Override
        public InputStream getInput() throws IOException {
            return cache.getInputStream();
        }
    });
    cache.reset();
    InputStreamPayload payload = new InputStreamPayload(cache.getInputStream());
    payload.getContentMetadata().setContentLength(contentLength);
    setContentMetadata(payload, exchange);
    return payload;
}
Also used : InputStream(java.io.InputStream) InputStreamPayload(org.jclouds.io.payloads.InputStreamPayload) IOException(java.io.IOException) FallbackConverter(org.apache.camel.FallbackConverter) Converter(org.apache.camel.Converter) TypeConverter(org.apache.camel.TypeConverter)

Example 43 with Converter

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

the class SnmpConverters method toString.

/**
     * Converts the given snmp pdu to a String body.
     *
     * @param pdu       the snmp pdu
     * @return  the text content
     */
@Converter
public static String toString(PDU pdu) {
    // the output buffer
    StringBuilder sb = new StringBuilder();
    // prepare the header
    if (pdu.getType() == PDU.V1TRAP) {
        sb.append("<" + SNMP_TAG + " messageType=\"v1\">");
    } else {
        sb.append(SNMP_TAG_OPEN);
    }
    // Extract SNMPv1 specific variables
    if (pdu.getType() == PDU.V1TRAP) {
        PDUv1 v1pdu = (PDUv1) pdu;
        entryAppend(sb, "enterprise", v1pdu.getEnterprise().toString());
        entryAppend(sb, "agent-addr", v1pdu.getAgentAddress().toString());
        entryAppend(sb, "generic-trap", Integer.toString(v1pdu.getGenericTrap()));
        entryAppend(sb, "specific-trap", Integer.toString(v1pdu.getSpecificTrap()));
        entryAppend(sb, "time-stamp", Long.toString(v1pdu.getTimestamp()));
    }
    // now loop all variables of the response
    for (Object o : pdu.getVariableBindings()) {
        VariableBinding b = (VariableBinding) o;
        sb.append(ENTRY_TAG_OPEN);
        sb.append(OID_TAG_OPEN);
        sb.append(b.getOid().toString());
        sb.append(OID_TAG_CLOSE);
        sb.append(VALUE_TAG_OPEN);
        sb.append(StringHelper.xmlEncode(b.getVariable().toString()));
        sb.append(VALUE_TAG_CLOSE);
        sb.append(ENTRY_TAG_CLOSE);
    }
    // prepare the footer
    sb.append(SNMP_TAG_CLOSE);
    return sb.toString();
}
Also used : PDUv1(org.snmp4j.PDUv1) VariableBinding(org.snmp4j.smi.VariableBinding) Converter(org.apache.camel.Converter)

Example 44 with Converter

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

the class MyCxfCustomerConverter method cxfPayloadToString.

@Converter
public static String cxfPayloadToString(final CxfPayload<?> payload) {
    XmlConverter converter = new XmlConverter();
    StringBuilder buf = new StringBuilder();
    for (Object element : payload.getBody()) {
        String elementString = "";
        try {
            elementString = converter.toString((Element) element, null);
        } catch (TransformerException e) {
            elementString = element.toString();
        }
        buf.append(elementString);
    }
    return buf.toString();
}
Also used : Element(org.w3c.dom.Element) TransformerException(javax.xml.transform.TransformerException) XmlConverter(org.apache.camel.converter.jaxp.XmlConverter) XmlConverter(org.apache.camel.converter.jaxp.XmlConverter) Converter(org.apache.camel.Converter)

Example 45 with Converter

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

the class MailConverters method toString.

/**
     * Converts the given JavaMail multipart to a String body, where the content-type of the multipart
     * must be text based (ie start with text). Can return null.
     */
@Converter
public static String toString(Multipart multipart) throws MessagingException, IOException {
    int size = multipart.getCount();
    for (int i = 0; i < size; i++) {
        BodyPart part = multipart.getBodyPart(i);
        Object content = part.getContent();
        while (content instanceof MimeMultipart) {
            if (multipart.getCount() < 1) {
                break;
            }
            part = ((MimeMultipart) content).getBodyPart(0);
            content = part.getContent();
        }
        if (part.getContentType().toLowerCase().startsWith("text")) {
            return part.getContent().toString();
        }
    }
    return null;
}
Also used : BodyPart(javax.mail.BodyPart) MimeMultipart(javax.mail.internet.MimeMultipart) FallbackConverter(org.apache.camel.FallbackConverter) Converter(org.apache.camel.Converter) TypeConverter(org.apache.camel.TypeConverter) IOConverter(org.apache.camel.converter.IOConverter)

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