Search in sources :

Example 51 with Converter

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

the class MongoDbBasicConverters method fromInputStreamToDBObject.

@Converter
public static BasicDBObject fromInputStreamToDBObject(InputStream is, Exchange exchange) {
    BasicDBObject answer = null;
    try {
        byte[] input = IOConverter.toBytes(is);
        if (isBson(input)) {
            BSONCallback callback = new JSONCallback();
            new BasicBSONDecoder().decode(input, callback);
            answer = (BasicDBObject) callback.get();
        } else {
            answer = (BasicDBObject) JSON.parse(IOConverter.toString(input, exchange));
        }
    } catch (Exception e) {
        LOG.warn("String -> DBObject conversion selected, but the following exception occurred. Returning null.", e);
    } finally {
        // we need to make sure to close the input stream
        IOHelper.close(is, "InputStream", LOG);
    }
    return answer;
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) JSONCallback(com.mongodb.util.JSONCallback) BSONCallback(org.bson.BSONCallback) BasicBSONDecoder(org.bson.BasicBSONDecoder) FileNotFoundException(java.io.FileNotFoundException) Converter(org.apache.camel.Converter) IOConverter(org.apache.camel.converter.IOConverter)

Example 52 with Converter

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

the class MongoDbBasicConverters method fromAnyObjectToDBObject.

@Converter
public static DBObject fromAnyObjectToDBObject(Object value) {
    BasicDBObject answer;
    try {
        Map<?, ?> m = OBJECT_MAPPER.convertValue(value, Map.class);
        answer = new BasicDBObject(m);
    } catch (Exception e) {
        LOG.warn("Conversion has fallen back to generic Object -> DBObject, but unable to convert type {}. Returning null. {}", value.getClass().getCanonicalName(), e.getClass().getCanonicalName() + ": " + e.getMessage());
        return null;
    }
    return answer;
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) FileNotFoundException(java.io.FileNotFoundException) Converter(org.apache.camel.Converter) IOConverter(org.apache.camel.converter.IOConverter)

Example 53 with Converter

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

the class QuickfixjConverters method toMessage.

@Converter
public static Message toMessage(byte[] value, Exchange exchange) throws InvalidMessage, ConfigError, UnsupportedEncodingException {
    DataDictionary dataDictionary = getDataDictionary(exchange);
    String charsetName = IOHelper.getCharsetName(exchange);
    String message;
    if (charsetName != null) {
        message = new String(value, charsetName);
    } else {
        message = new String(value);
    }
    // if message ends with any sort of newline trim it so QuickfixJ's doesn't fail while parsing the string
    if (message.endsWith("\r\n")) {
        message = message.substring(0, message.length() - 2);
    } else if (message.endsWith("\r") || message.endsWith("\n")) {
        message = message.substring(0, message.length() - 1);
    }
    return new Message(message, dataDictionary, false);
}
Also used : InvalidMessage(quickfix.InvalidMessage) Message(quickfix.Message) DataDictionary(quickfix.DataDictionary) Converter(org.apache.camel.Converter)

Example 54 with Converter

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

the class RestletConverter method toMediaTypes.

@Converter
public static MediaType[] toMediaTypes(final String name) {
    final String[] strings = name.split(",");
    final List<MediaType> answer = new ArrayList<>(strings.length);
    for (int i = 0; i < strings.length; i++) {
        final MediaType mediaType = toMediaType(strings[i]);
        if (mediaType != null) {
            answer.add(mediaType);
        }
    }
    return answer.toArray(new MediaType[answer.size()]);
}
Also used : ArrayList(java.util.ArrayList) MediaType(org.restlet.data.MediaType) Converter(org.apache.camel.Converter)

Example 55 with Converter

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

the class SalesforceReportResultsToListConverter method convertToList.

@Converter
public static List<List<String>> convertToList(final AbstractReportResultsBase reportResults, final Exchange exchange) {
    List<List<String>> results = null;
    if (reportResults instanceof AsyncReportResults) {
        AsyncReportResults asyncReportResults = (AsyncReportResults) reportResults;
        final ReportStatusEnum status = asyncReportResults.getAttributes().getStatus();
        // only successfully completed async report results have data rows
        if (status != ReportStatusEnum.Success) {
            throw new IllegalArgumentException("Invalid asynchronous report results status " + status);
        }
    }
    switch(reportResults.getReportMetadata().getReportFormat()) {
        case TABULAR:
            results = convertTabularResults(reportResults, exchange);
            break;
        case SUMMARY:
            results = convertSummaryResults(reportResults, exchange);
            break;
        case MATRIX:
            results = convertMatrixResults(reportResults, exchange);
            break;
        default:
    }
    return results;
}
Also used : ReportStatusEnum(org.apache.camel.component.salesforce.api.dto.analytics.reports.ReportStatusEnum) AsyncReportResults(org.apache.camel.component.salesforce.api.dto.analytics.reports.AsyncReportResults) ArrayList(java.util.ArrayList) List(java.util.List) 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