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;
}
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;
}
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);
}
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()]);
}
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;
}
Aggregations