Hi Willem

Is the input stream closed? That is needed as otherwise the file is
locked. Especially on windows there is this problem.
And it needs to be done in a try .. finally, so its always closed,
even if there is some exception.


On Wed, Dec 5, 2012 at 9:59 AM,  <ningji...@apache.org> wrote:
> Author: ningjiang
> Date: Wed Dec  5 08:59:12 2012
> New Revision: 1417318
>
> URL: http://svn.apache.org/viewvc?rev=1417318&view=rev
> Log:
> CAMEL-5847 Added a converter for files or inputstreams with thanks to Arnout
>
> Modified:
>     
> camel/trunk/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/converters/MongoDbBasicConverters.java
>     
> camel/trunk/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbConversionsTest.java
>
> Modified: 
> camel/trunk/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/converters/MongoDbBasicConverters.java
> URL: 
> http://svn.apache.org/viewvc/camel/trunk/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/converters/MongoDbBasicConverters.java?rev=1417318&r1=1417317&r2=1417318&view=diff
> ==============================================================================
> --- 
> camel/trunk/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/converters/MongoDbBasicConverters.java
>  (original)
> +++ 
> camel/trunk/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/converters/MongoDbBasicConverters.java
>  Wed Dec  5 08:59:12 2012
> @@ -16,13 +16,22 @@
>   */
>  package org.apache.camel.component.mongodb.converters;
>
> +import java.io.File;
> +import java.io.FileInputStream;
> +import java.io.FileNotFoundException;
> +import java.io.InputStream;
>  import java.util.Map;
>
>  import com.mongodb.BasicDBObject;
>  import com.mongodb.DBObject;
>  import com.mongodb.util.JSON;
> +import com.mongodb.util.JSONCallback;
>
>  import org.apache.camel.Converter;
> +import org.apache.camel.Exchange;
> +import org.apache.camel.converter.IOConverter;
> +import org.bson.BSONCallback;
> +import org.bson.BasicBSONDecoder;
>  import org.codehaus.jackson.map.ObjectMapper;
>  import org.slf4j.Logger;
>  import org.slf4j.LoggerFactory;
> @@ -61,6 +70,47 @@ public final class MongoDbBasicConverter
>      }
>
>      @Converter
> +    public static DBObject fromFileToDBObject(File f, Exchange exchange) 
> throws FileNotFoundException {
> +        return fromInputStreamToDBObject(new FileInputStream(f), exchange);
> +    }
> +
> +    @Converter
> +    public static DBObject fromInputStreamToDBObject(InputStream is, 
> Exchange exchange) {
> +        DBObject answer = null;
> +        try {
> +            byte[] input = IOConverter.toBytes(is);
> +
> +            if (isBson(input)) {
> +                BSONCallback callback = new JSONCallback();
> +                new BasicBSONDecoder().decode(input, callback);
> +                answer = (DBObject) callback.get();
> +            } else {
> +                answer = (DBObject) JSON.parse(IOConverter.toString(input, 
> exchange));
> +            }
> +        } catch (Exception e) {
> +            LOG.warn("String -> DBObject conversion selected, but the 
> following exception occurred. Returning null.", e);
> +        }
> +
> +        return answer;
> +    }
> +
> +    /**
> +     * If the input starts with any number of whitespace characters and then 
> a '{' character, we
> +     * assume it is JSON rather than BSON. There are probably no useful BSON 
> blobs that fit this pattern
> +     */
> +    private static boolean isBson(byte[] input) {
> +        int i = 0;
> +        while (i < input.length) {
> +            if (input[i] == '{') {
> +                return false;
> +            } else if (!Character.isWhitespace(input[i])) {
> +                return true;
> +            }
> +        }
> +        return true;
> +    }
> +
> +    @Converter
>      public static DBObject fromAnyObjectToDBObject(Object value) {
>          BasicDBObject answer;
>          try {
>
> Modified: 
> camel/trunk/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbConversionsTest.java
> URL: 
> http://svn.apache.org/viewvc/camel/trunk/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbConversionsTest.java?rev=1417318&r1=1417317&r2=1417318&view=diff
> ==============================================================================
> --- 
> camel/trunk/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbConversionsTest.java
>  (original)
> +++ 
> camel/trunk/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbConversionsTest.java
>  Wed Dec  5 08:59:12 2012
> @@ -16,14 +16,18 @@
>   */
>  package org.apache.camel.component.mongodb;
>
> +import java.io.ByteArrayInputStream;
>  import java.util.HashMap;
>  import java.util.Map;
>
> +import com.mongodb.BasicDBObject;
>  import com.mongodb.DBObject;
> +import com.mongodb.DefaultDBEncoder;
>  import com.mongodb.WriteResult;
>
>  import org.apache.camel.builder.RouteBuilder;
> -
> +import org.apache.camel.converter.IOConverter;
> +import org.bson.BSONObject;
>  import org.junit.Test;
>
>  public class MongoDbConversionsTest extends AbstractMongoDbTest {
> @@ -68,6 +72,30 @@ public class MongoDbConversionsTest exte
>          assertNotNull("No record with 'testInsertJsonString' _id", b);
>      }
>
> +    @Test
> +    public void testInsertJsonInputStream() throws Exception {
> +        assertEquals(0, testCollection.count());
> +        Object result = template.requestBody("direct:insertJsonString",
> +                        IOConverter.toInputStream("{\"fruits\": [\"apple\", 
> \"banana\"], \"veggie\": \"broccoli\", \"_id\": \"testInsertJsonString\"}\n", 
> null));
> +        assertTrue(result instanceof WriteResult);
> +        DBObject b = testCollection.findOne("testInsertJsonString");
> +        assertNotNull("No record with 'testInsertJsonString' _id", b);
> +    }
> +
> +    @Test
> +    public void testInsertBsonInputStream() {
> +        assertEquals(0, testCollection.count());
> +
> +        DefaultDBEncoder encoder = new DefaultDBEncoder();
> +        BSONObject bsonObject = new BasicDBObject();
> +        bsonObject.put("_id", "testInsertBsonString");
> +
> +        Object result = template.requestBody("direct:insertJsonString", new 
> ByteArrayInputStream(encoder.encode(bsonObject)));
> +        assertTrue(result instanceof WriteResult);
> +        DBObject b = testCollection.findOne("testInsertBsonString");
> +        assertNotNull("No record with 'testInsertBsonString' _id", b);
> +    }
> +
>      @Override
>      protected RouteBuilder createRouteBuilder() throws Exception {
>          return new RouteBuilder() {
>
>



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
FuseSource is now part of Red Hat
Email: cib...@redhat.com
Web: http://fusesource.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen

Reply via email to