Search in sources :

Example 1 with JSONCallback

use of com.mongodb.util.JSONCallback 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)

Aggregations

BasicDBObject (com.mongodb.BasicDBObject)1 JSONCallback (com.mongodb.util.JSONCallback)1 FileNotFoundException (java.io.FileNotFoundException)1 Converter (org.apache.camel.Converter)1 IOConverter (org.apache.camel.converter.IOConverter)1 BSONCallback (org.bson.BSONCallback)1 BasicBSONDecoder (org.bson.BasicBSONDecoder)1