Author: ningjiang
Date: Wed Dec 5 09:56:16 2012
New Revision: 1417334
URL: http://svn.apache.org/viewvc?rev=1417334&view=rev
Log:
CAMEL-5847 Added a converter for files or inputstreams with thanks to Arnout
Merged revisions 1417318,1417330 via svnmerge from
https://svn.apache.org/repos/asf/camel/trunk
........
r1417318 | ningjiang | 2012-12-05 16:59:12 +0800 (Wed, 05 Dec 2012) | 1 line
CAMEL-5847 Added a converter for files or inputstreams with thanks to Arnout
........
r1417330 | ningjiang | 2012-12-05 17:29:56 +0800 (Wed, 05 Dec 2012) | 1 line
CAMEL-5847 make sure the FileInputStream is closed at the end.
........
Modified:
camel/branches/camel-2.10.x/ (props changed)
camel/branches/camel-2.10.x/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/converters/MongoDbBasicConverters.java
camel/branches/camel-2.10.x/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbConversionsTest.java
Propchange: camel/branches/camel-2.10.x/
------------------------------------------------------------------------------
Merged /camel/trunk:r1417318-1417330
Propchange: camel/branches/camel-2.10.x/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.
Modified:
camel/branches/camel-2.10.x/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/converters/MongoDbBasicConverters.java
URL:
http://svn.apache.org/viewvc/camel/branches/camel-2.10.x/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/converters/MongoDbBasicConverters.java?rev=1417334&r1=1417333&r2=1417334&view=diff
==============================================================================
---
camel/branches/camel-2.10.x/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/converters/MongoDbBasicConverters.java
(original)
+++
camel/branches/camel-2.10.x/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/converters/MongoDbBasicConverters.java
Wed Dec 5 09:56:16 2012
@@ -16,13 +16,24 @@
*/
package org.apache.camel.component.mongodb.converters;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+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.apache.camel.util.IOHelper;
+import org.bson.BSONCallback;
+import org.bson.BasicBSONDecoder;
import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -61,6 +72,49 @@ 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);
+ } finally {
+ // we need to make sure to close the input stream
+ IOHelper.close(is, "InputStream", LOG);
+ }
+ 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/branches/camel-2.10.x/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbConversionsTest.java
URL:
http://svn.apache.org/viewvc/camel/branches/camel-2.10.x/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbConversionsTest.java?rev=1417334&r1=1417333&r2=1417334&view=diff
==============================================================================
---
camel/branches/camel-2.10.x/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbConversionsTest.java
(original)
+++
camel/branches/camel-2.10.x/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbConversionsTest.java
Wed Dec 5 09:56:16 2012
@@ -16,16 +16,19 @@
*/
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.CamelContext;
import org.apache.camel.builder.RouteBuilder;
-import org.apache.camel.component.properties.PropertiesComponent;
-import org.apache.camel.spring.SpringCamelContext;
+import org.apache.camel.converter.IOConverter;
+import org.bson.BSONObject;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@@ -71,6 +74,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() {