Repository: camel Updated Branches: refs/heads/master 2d5000ea0 -> 39b79b97b
http://git-wip-us.apache.org/repos/asf/camel/blob/b4845575/components/camel-mongodb-gridfs/src/main/java/org/apache/camel/component/gridfs/GridFsEndpoint.java ---------------------------------------------------------------------- diff --git a/components/camel-mongodb-gridfs/src/main/java/org/apache/camel/component/gridfs/GridFsEndpoint.java b/components/camel-mongodb-gridfs/src/main/java/org/apache/camel/component/gridfs/GridFsEndpoint.java new file mode 100644 index 0000000..4856a7f --- /dev/null +++ b/components/camel-mongodb-gridfs/src/main/java/org/apache/camel/component/gridfs/GridFsEndpoint.java @@ -0,0 +1,360 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.gridfs; + +import com.mongodb.DB; +import com.mongodb.DBCollection; +import com.mongodb.Mongo; +import com.mongodb.ReadPreference; +import com.mongodb.WriteConcern; +import com.mongodb.gridfs.GridFS; +import org.apache.camel.Consumer; +import org.apache.camel.Processor; +import org.apache.camel.Producer; +import org.apache.camel.impl.DefaultEndpoint; +import org.apache.camel.spi.Metadata; +import org.apache.camel.spi.UriEndpoint; +import org.apache.camel.spi.UriParam; +import org.apache.camel.spi.UriPath; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@UriEndpoint(scheme = "gridfs", title = "MongoDBGridFS", syntax = "gridfs:connectionBean", + label = "database,nosql") +public class GridFsEndpoint extends DefaultEndpoint { + + public enum QueryStrategy { + TimeStamp, + PersistentTimestamp, + FileAttribute, + TimeStampAndFileAttribute, + PersistentTimestampAndFileAttribute + }; + public static final String GRIDFS_OPERATION = "gridfs.operation"; + public static final String GRIDFS_METADATA = "gridfs.metadata"; + public static final String GRIDFS_CHUNKSIZE = "gridfs.chunksize"; + + private static final Logger LOG = LoggerFactory.getLogger(GridFsEndpoint.class); + + @UriPath @Metadata(required = "true") + private String connectionBean; + @UriParam @Metadata(required = "true") + private String database; + @UriParam(defaultValue = GridFS.DEFAULT_BUCKET) + private String bucket; + @UriParam(enums = "ACKNOWLEDGED,W1,W2,W3,UNACKNOWLEDGED,JOURNALED,MAJORITY,SAFE") + private WriteConcern writeConcern; + @UriParam + private WriteConcern writeConcernRef; + @UriParam + private ReadPreference readPreference; + + @UriParam(label = "producer") + private String operation; + + @UriParam(label = "consumer") + private String query; + @UriParam(label = "consumer", defaultValue = "1000") + private long initialDelay = 1000; + @UriParam(label = "consumer", defaultValue = "500") + private long delay = 500; + + @UriParam(label = "consumer", defaultValue = "TimeStamp") + private QueryStrategy queryStrategy = QueryStrategy.TimeStamp; + @UriParam(label = "consumer", defaultValue = "camel-timestamps") + private String persistentTSCollection = "camel-timestamps"; + @UriParam(label = "consumer", defaultValue = "camel-timestamp") + private String persistentTSObject = "camel-timestamp"; + @UriParam(label = "consumer", defaultValue = "camel-processed") + private String fileAttributeName = "camel-processed"; + + + private Mongo mongoConnection; + private DB db; + private GridFS gridFs; + private DBCollection filesCollection; + + public GridFsEndpoint(String uri, GridFsComponent component) { + super(uri, component); + } + + @Override + public Producer createProducer() throws Exception { + initializeConnection(); + return new GridFsProducer(this); + } + + @Override + public Consumer createConsumer(Processor processor) throws Exception { + initializeConnection(); + return new GridFsConsumer(this, processor); + } + + public boolean isSingleton() { + return true; + } + + @SuppressWarnings("deprecation") + public void initializeConnection() throws Exception { + LOG.info("Initialize GridFS endpoint: {}", this.toString()); + if (database == null) { + throw new IllegalStateException("Missing required endpoint configuration: database"); + } + db = mongoConnection.getDB(database); + if (db == null) { + throw new IllegalStateException("Could not initialize GridFsComponent. Database " + database + " does not exist."); + } + gridFs = new GridFS(db, bucket == null ? GridFS.DEFAULT_BUCKET : bucket) { + { + filesCollection = getFilesCollection(); + } + }; + } + + + @Override + protected void doStart() throws Exception { + if (writeConcern != null && writeConcernRef != null) { + String msg = "Cannot set both writeConcern and writeConcernRef at the same time. Respective values: " + writeConcern + + ", " + writeConcernRef + ". Aborting initialization."; + throw new IllegalArgumentException(msg); + } + + setWriteReadOptionsOnConnection(); + super.doStart(); + } + private void setWriteReadOptionsOnConnection() { + // Set the WriteConcern + if (writeConcern != null) { + mongoConnection.setWriteConcern(writeConcern); + } else if (writeConcernRef != null) { + mongoConnection.setWriteConcern(writeConcernRef); + } + + // Set the ReadPreference + if (readPreference != null) { + mongoConnection.setReadPreference(readPreference); + } + } + + + + + // ======= Getters and setters =============================================== + public String getConnectionBean() { + return connectionBean; + } + /** + * Name of {@link com.mongodb.Mongo} to use. + */ + public void setConnectionBean(String connectionBean) { + this.connectionBean = connectionBean; + } + + public Mongo getMongoConnection() { + return mongoConnection; + } + /** + * Sets the Mongo instance that represents the backing connection + * + * @param mongoConnection the connection to the database + */ + public void setMongoConnection(Mongo mongoConnection) { + this.mongoConnection = mongoConnection; + } + + public DB getDB() { + return db; + } + + public String getDatabase() { + return database; + } + /** + * Sets the name of the MongoDB database to target + * + * @param database name of the MongoDB database + */ + public void setDatabase(String database) { + this.database = database; + } + /** + * Sets the name of the GridFS bucket within the database. Default is "fs". + * + * @param database name of the MongoDB database + */ + public String getBucket() { + return bucket; + } + public void setBucket(String bucket) { + this.bucket = bucket; + } + + public String getQuery() { + return query; + } + /** + * Additional query parameters (in JSON) that are used to configure the query used for finding + * files in the GridFsConsumer + * @param query + */ + public void setQuery(String query) { + this.query = query; + } + public long getDelay() { + return delay; + } + /** + * Sets the delay between polls within the Consumer. Default is 500ms + * @param delay + */ + public void setDelay(long delay) { + this.delay = delay; + } + public long getInitialDelay() { + return initialDelay; + } + /** + * Sets the initialDelay before the consumer will start polling. Default is 1000ms + * @param initialDelay + */ + public void setInitialDelay(long initialDelay) { + this.initialDelay = delay; + } + + /** + * Sets the QueryStrategy that is used for polling for new files. Default is Timestamp + * @see QueryStrategy + * @param s + */ + public void setQueryStrategy(String s) { + queryStrategy = QueryStrategy.valueOf(s); + } + public QueryStrategy getQueryStrategy() { + return queryStrategy; + } + /** + * If the QueryType uses a persistent timestamp, this sets the name of the collection within + * the DB to store the timestamp. + * @param s + */ + public void setPersistentTSCollection(String s) { + persistentTSCollection = s; + } + public String getPersistentTSCollection() { + return persistentTSCollection; + } + /** + * If the QueryType uses a persistent timestamp, this is the ID of the object in the collection + * to store the timestamp. + * @param s + */ + public void setPersistentTSObject(String id) { + persistentTSObject = id; + } + public String getPersistentTSObject() { + return persistentTSObject; + } + + /** + * If the QueryType uses a FileAttribute, this sets the name of the attribute that is used. Default is "camel-processed". + * @param f + */ + public void setFileAttributeName(String f) { + fileAttributeName = f; + } + public String getFileAttributeName() { + return fileAttributeName; + } + + /** + * Set the {@link WriteConcern} for write operations on MongoDB using the standard ones. + * Resolved from the fields of the WriteConcern class by calling the {@link WriteConcern#valueOf(String)} method. + * + * @param writeConcern the standard name of the WriteConcern + * @see <a href="http://api.mongodb.org/java/current/com/mongodb/WriteConcern.html#valueOf(java.lang.String)">possible options</a> + */ + public void setWriteConcern(String writeConcern) { + this.writeConcern = WriteConcern.valueOf(writeConcern); + } + + public WriteConcern getWriteConcern() { + return writeConcern; + } + + /** + * Set the {@link WriteConcern} for write operations on MongoDB, passing in the bean ref to a custom WriteConcern which exists in the Registry. + * You can also use standard WriteConcerns by passing in their key. See the {@link #setWriteConcern(String) setWriteConcern} method. + * + * @param writeConcernRef the name of the bean in the registry that represents the WriteConcern to use + */ + public void setWriteConcernRef(String writeConcernRef) { + WriteConcern wc = this.getCamelContext().getRegistry().lookupByNameAndType(writeConcernRef, WriteConcern.class); + if (wc == null) { + String msg = "Camel MongoDB component could not find the WriteConcern in the Registry. Verify that the " + + "provided bean name (" + writeConcernRef + ") is correct. Aborting initialization."; + throw new IllegalArgumentException(msg); + } + + this.writeConcernRef = wc; + } + + public WriteConcern getWriteConcernRef() { + return writeConcernRef; + } + + /** + * Sets a MongoDB {@link ReadPreference} on the Mongo connection. Read preferences set directly on the connection will be + * overridden by this setting. + * <p/> + * The {@link com.mongodb.ReadPreference#valueOf(String)} utility method is used to resolve the passed {@code readPreference} + * value. Some examples for the possible values are {@code nearest}, {@code primary} or {@code secondary} etc. + * + * @param readPreference the name of the read preference to set + */ + public void setReadPreference(String readPreference) { + this.readPreference = ReadPreference.valueOf(readPreference); + } + + public ReadPreference getReadPreference() { + return readPreference; + } + + + /** + * Sets the operation this endpoint will execute against GridRS. + */ + public void setOperation(String operation) { + this.operation = operation; + } + + public String getOperation() { + return operation; + } + + public GridFS getGridFs() { + return gridFs; + } + + public void setGridFs(GridFS gridFs) { + this.gridFs = gridFs; + } + public DBCollection getFilesCollection() { + return filesCollection; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/b4845575/components/camel-mongodb-gridfs/src/main/java/org/apache/camel/component/gridfs/GridFsProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-mongodb-gridfs/src/main/java/org/apache/camel/component/gridfs/GridFsProducer.java b/components/camel-mongodb-gridfs/src/main/java/org/apache/camel/component/gridfs/GridFsProducer.java new file mode 100644 index 0000000..dccb499 --- /dev/null +++ b/components/camel-mongodb-gridfs/src/main/java/org/apache/camel/component/gridfs/GridFsProducer.java @@ -0,0 +1,145 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.gridfs; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.io.Reader; + +import com.mongodb.BasicDBObject; +import com.mongodb.DBCursor; +import com.mongodb.DBObject; +import com.mongodb.gridfs.GridFSDBFile; +import com.mongodb.gridfs.GridFSInputFile; +import com.mongodb.util.JSON; +import org.apache.camel.Exchange; +import org.apache.camel.impl.DefaultProducer; + + +public class GridFsProducer extends DefaultProducer { + private GridFsEndpoint endpoint; + + public GridFsProducer(GridFsEndpoint endpoint) { + super(endpoint); + this.endpoint = endpoint; + } + + public void process(Exchange exchange) throws Exception { + String operation = endpoint.getOperation(); + if (operation == null) { + operation = exchange.getIn().getHeader(GridFsEndpoint.GRIDFS_OPERATION, String.class); + } + if (operation == null || "create".equals(operation)) { + final String filename = exchange.getIn().getHeader(Exchange.FILE_NAME, String.class); + Long chunkSize = exchange.getIn().getHeader(GridFsEndpoint.GRIDFS_CHUNKSIZE, Long.class); + + InputStream ins = exchange.getIn().getMandatoryBody(InputStream.class); + GridFSInputFile gfsFile = endpoint.getGridFs().createFile(ins, filename, true); + if (chunkSize != null && chunkSize > 0) { + gfsFile.setChunkSize(chunkSize); + } + final String ct = exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class); + if (ct != null) { + gfsFile.setContentType(ct); + } + String metaData = exchange.getIn().getHeader(GridFsEndpoint.GRIDFS_METADATA, String.class); + DBObject dbObject = (DBObject) JSON.parse(metaData); + gfsFile.setMetaData(dbObject); + gfsFile.save(); + exchange.getIn().setHeader(Exchange.FILE_NAME_PRODUCED, gfsFile.getFilename()); + } else if ("remove".equals(operation)) { + final String filename = exchange.getIn().getHeader(Exchange.FILE_NAME, String.class); + endpoint.getGridFs().remove(filename); + } else if ("findOne".equals(operation)) { + final String filename = exchange.getIn().getHeader(Exchange.FILE_NAME, String.class); + GridFSDBFile file = endpoint.getGridFs().findOne(filename); + if (file != null) { + exchange.getIn().setHeader(GridFsEndpoint.GRIDFS_METADATA, JSON.serialize(file.getMetaData())); + exchange.getIn().setHeader(Exchange.FILE_CONTENT_TYPE, file.getContentType()); + exchange.getIn().setHeader(Exchange.FILE_LENGTH, file.getLength()); + exchange.getIn().setHeader(Exchange.FILE_LAST_MODIFIED, file.getUploadDate()); + exchange.getIn().setBody(file.getInputStream(), InputStream.class); + } else { + throw new FileNotFoundException("No GridFS file for " + filename); + } + } else if ("listAll".equals(operation)) { + final String filename = exchange.getIn().getHeader(Exchange.FILE_NAME, String.class); + DBCursor cursor; + if (filename == null) { + cursor = endpoint.getGridFs().getFileList(); + } else { + cursor = endpoint.getGridFs().getFileList(new BasicDBObject("filename", filename)); + } + exchange.getIn().setBody(new DBCursorFilenameReader(cursor), Reader.class); + } else if ("count".equals(operation)) { + final String filename = exchange.getIn().getHeader(Exchange.FILE_NAME, String.class); + DBCursor cursor; + if (filename == null) { + cursor = endpoint.getGridFs().getFileList(); + } else { + cursor = endpoint.getGridFs().getFileList(new BasicDBObject("filename", filename)); + } + exchange.getIn().setBody(cursor.count(), Integer.class); + } + + } + + + private class DBCursorFilenameReader extends Reader { + DBCursor cursor; + StringBuilder current; + int pos; + + DBCursorFilenameReader(DBCursor c) { + cursor = c; + current = new StringBuilder(4096); + pos = 0; + fill(); + } + void fill() { + if (pos > 0) { + current.delete(0, pos); + pos = 0; + } + while (cursor.hasNext() && current.length() < 4000) { + DBObject o = cursor.next(); + current.append(o.get("filename")).append("\t").append(o.get("_id")).append("\n"); + } + } + @Override + public int read(char[] cbuf, int off, int len) throws IOException { + if (pos == current.length()) { + fill(); + } + if (pos == current.length()) { + return -1; + } + if (len > (current.length() - pos)) { + len = current.length() - pos; + } + current.getChars(pos, pos + len, cbuf, off); + pos += len; + return len; + } + + @Override + public void close() throws IOException { + cursor.close(); + } + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/b4845575/components/camel-mongodb-gridfs/src/main/resources/META-INF/LICENSE.txt ---------------------------------------------------------------------- diff --git a/components/camel-mongodb-gridfs/src/main/resources/META-INF/LICENSE.txt b/components/camel-mongodb-gridfs/src/main/resources/META-INF/LICENSE.txt new file mode 100644 index 0000000..6b0b127 --- /dev/null +++ b/components/camel-mongodb-gridfs/src/main/resources/META-INF/LICENSE.txt @@ -0,0 +1,203 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + http://git-wip-us.apache.org/repos/asf/camel/blob/b4845575/components/camel-mongodb-gridfs/src/main/resources/META-INF/NOTICE.txt ---------------------------------------------------------------------- diff --git a/components/camel-mongodb-gridfs/src/main/resources/META-INF/NOTICE.txt b/components/camel-mongodb-gridfs/src/main/resources/META-INF/NOTICE.txt new file mode 100644 index 0000000..2e215bf --- /dev/null +++ b/components/camel-mongodb-gridfs/src/main/resources/META-INF/NOTICE.txt @@ -0,0 +1,11 @@ + ========================================================================= + == NOTICE file corresponding to the section 4 d of == + == the Apache License, Version 2.0, == + == in this case for the Apache Camel distribution. == + ========================================================================= + + This product includes software developed by + The Apache Software Foundation (http://www.apache.org/). + + Please read the different LICENSE files present in the licenses directory of + this distribution. http://git-wip-us.apache.org/repos/asf/camel/blob/b4845575/components/camel-mongodb-gridfs/src/main/resources/META-INF/services/org/apache/camel/component/gridfs ---------------------------------------------------------------------- diff --git a/components/camel-mongodb-gridfs/src/main/resources/META-INF/services/org/apache/camel/component/gridfs b/components/camel-mongodb-gridfs/src/main/resources/META-INF/services/org/apache/camel/component/gridfs new file mode 100644 index 0000000..50df682 --- /dev/null +++ b/components/camel-mongodb-gridfs/src/main/resources/META-INF/services/org/apache/camel/component/gridfs @@ -0,0 +1,18 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +class=org.apache.camel.component.gridfs.GridFsComponent http://git-wip-us.apache.org/repos/asf/camel/blob/b4845575/components/camel-mongodb-gridfs/src/test/java/org/apache/camel/component/gridfs/AbstractMongoDbTest.java ---------------------------------------------------------------------- diff --git a/components/camel-mongodb-gridfs/src/test/java/org/apache/camel/component/gridfs/AbstractMongoDbTest.java b/components/camel-mongodb-gridfs/src/test/java/org/apache/camel/component/gridfs/AbstractMongoDbTest.java new file mode 100644 index 0000000..f4c2bff --- /dev/null +++ b/components/camel-mongodb-gridfs/src/test/java/org/apache/camel/component/gridfs/AbstractMongoDbTest.java @@ -0,0 +1,62 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.gridfs; + + +import com.mongodb.MongoClient; +import com.mongodb.gridfs.GridFS; + +import org.apache.camel.CamelContext; +import org.apache.camel.component.properties.PropertiesComponent; +import org.apache.camel.spring.SpringCamelContext; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +public abstract class AbstractMongoDbTest extends CamelTestSupport { + + protected MongoClient mongo; + protected GridFS gridfs; + + protected ApplicationContext applicationContext; + + @SuppressWarnings("deprecation") + @Override + public void doPostSetup() { + mongo = applicationContext.getBean(MongoClient.class); + gridfs = new GridFS(mongo.getDB("test"), getBucket()); + } + + public String getBucket() { + return this.getClass().getSimpleName(); + } + + @Override + public void tearDown() throws Exception { + super.tearDown(); + mongo.close(); + } + + @Override + protected CamelContext createCamelContext() throws Exception { + applicationContext = new AnnotationConfigApplicationContext(EmbedMongoConfiguration.class); + CamelContext ctx = new SpringCamelContext(applicationContext); + PropertiesComponent pc = new PropertiesComponent("classpath:mongodb.test.properties"); + ctx.addComponent("properties", pc); + return ctx; + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/b4845575/components/camel-mongodb-gridfs/src/test/java/org/apache/camel/component/gridfs/EmbedMongoConfiguration.java ---------------------------------------------------------------------- diff --git a/components/camel-mongodb-gridfs/src/test/java/org/apache/camel/component/gridfs/EmbedMongoConfiguration.java b/components/camel-mongodb-gridfs/src/test/java/org/apache/camel/component/gridfs/EmbedMongoConfiguration.java new file mode 100644 index 0000000..d755a45 --- /dev/null +++ b/components/camel-mongodb-gridfs/src/test/java/org/apache/camel/component/gridfs/EmbedMongoConfiguration.java @@ -0,0 +1,58 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.gridfs; + +import java.io.IOException; +import java.net.UnknownHostException; + +import com.mongodb.MongoClient; +import de.flapdoodle.embed.mongo.MongodExecutable; +import de.flapdoodle.embed.mongo.MongodStarter; +import de.flapdoodle.embed.mongo.config.IMongodConfig; +import de.flapdoodle.embed.mongo.config.MongodConfigBuilder; +import de.flapdoodle.embed.mongo.config.Net; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import static de.flapdoodle.embed.mongo.distribution.Version.Main.PRODUCTION; +import static de.flapdoodle.embed.process.runtime.Network.localhostIsIPv6; +import static org.springframework.util.SocketUtils.findAvailableTcpPort; + +@Configuration +public class EmbedMongoConfiguration { + + private static final int PORT = findAvailableTcpPort(); + + static { + try { + IMongodConfig mongodConfig = new MongodConfigBuilder() + .version(PRODUCTION) + .net(new Net(PORT, localhostIsIPv6())) + .build(); + MongodExecutable mongodExecutable = MongodStarter.getDefaultInstance().prepare(mongodConfig); + mongodExecutable.start(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + @Bean + public MongoClient myDb() throws UnknownHostException { + return new MongoClient("0.0.0.0", PORT); + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/b4845575/components/camel-mongodb-gridfs/src/test/java/org/apache/camel/component/gridfs/GridFsConsumerTest.java ---------------------------------------------------------------------- diff --git a/components/camel-mongodb-gridfs/src/test/java/org/apache/camel/component/gridfs/GridFsConsumerTest.java b/components/camel-mongodb-gridfs/src/test/java/org/apache/camel/component/gridfs/GridFsConsumerTest.java new file mode 100644 index 0000000..64aa1d9 --- /dev/null +++ b/components/camel-mongodb-gridfs/src/test/java/org/apache/camel/component/gridfs/GridFsConsumerTest.java @@ -0,0 +1,99 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.camel.component.gridfs; + +import java.util.HashMap; +import java.util.Map; + +import com.mongodb.gridfs.GridFS; + +import org.apache.camel.Exchange; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; + +import org.junit.Test; + +/** + * + */ +public class GridFsConsumerTest extends AbstractMongoDbTest { + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() { + from("direct:create").to("gridfs:myDb?database={{mongodb.testDb}}&operation=create&bucket=" + getBucket()); + from("direct:create-a").to("gridfs:myDb?database={{mongodb.testDb}}&operation=create&bucket=" + getBucket() + "-a"); + from("direct:create-pts").to("gridfs:myDb?database={{mongodb.testDb}}&operation=create&bucket=" + getBucket() + "-pts"); + + from("gridfs:myDb?database={{mongodb.testDb}}&bucket=" + getBucket()).convertBodyTo(String.class).to("mock:test"); + from("gridfs:myDb?database={{mongodb.testDb}}&bucket=" + getBucket() + "-a&queryStrategy=FileAttribute") + .convertBodyTo(String.class).to("mock:test"); + from("gridfs:myDb?database={{mongodb.testDb}}&bucket=" + getBucket() + "-pts&queryStrategy=PersistentTimestamp") + .convertBodyTo(String.class).to("mock:test"); + } + }; + } + + + @Test + public void testTimestamp() throws Exception { + runTest("direct:create", gridfs); + } + @Test + @SuppressWarnings("deprecation") + public void testAttribute() throws Exception { + runTest("direct:create-a", new GridFS(mongo.getDB("test"), getBucket() + "-a")); + } + + @Test + @SuppressWarnings("deprecation") + public void testPersistentTS() throws Exception { + runTest("direct:create-pts", new GridFS(mongo.getDB("test"), getBucket() + "-pts")); + } + + public void runTest(String target, GridFS gridfs) throws Exception { + MockEndpoint mock = getMockEndpoint("mock:test"); + String data = "This is some stuff to go into the db"; + mock.expectedMessageCount(1); + mock.expectedBodiesReceived(data); + + Map<String, Object> headers = new HashMap<String, Object>(); + String fn = "filename.for.db.txt"; + assertEquals(0, gridfs.find(fn).size()); + + headers.put(Exchange.FILE_NAME, fn); + template.requestBodyAndHeaders(target, data, headers); + + mock.assertIsSatisfied(); + mock.reset(); + + mock.expectedMessageCount(3); + mock.expectedBodiesReceived(data, data, data); + + headers.put(Exchange.FILE_NAME, fn + "_1"); + template.requestBodyAndHeaders(target, data, headers); + headers.put(Exchange.FILE_NAME, fn + "_2"); + template.requestBodyAndHeaders(target, data, headers); + headers.put(Exchange.FILE_NAME, fn + "_3"); + template.requestBodyAndHeaders(target, data, headers); + mock.assertIsSatisfied(); + Thread.sleep(1000); + mock.assertIsSatisfied(); + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/b4845575/components/camel-mongodb-gridfs/src/test/java/org/apache/camel/component/gridfs/GridFsProducerOperationsTest.java ---------------------------------------------------------------------- diff --git a/components/camel-mongodb-gridfs/src/test/java/org/apache/camel/component/gridfs/GridFsProducerOperationsTest.java b/components/camel-mongodb-gridfs/src/test/java/org/apache/camel/component/gridfs/GridFsProducerOperationsTest.java new file mode 100644 index 0000000..df7882d --- /dev/null +++ b/components/camel-mongodb-gridfs/src/test/java/org/apache/camel/component/gridfs/GridFsProducerOperationsTest.java @@ -0,0 +1,74 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.gridfs; + +import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; + +import org.apache.camel.Exchange; +import org.apache.camel.builder.RouteBuilder; +import org.junit.Test; + +public class GridFsProducerOperationsTest extends AbstractMongoDbTest { + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() { + from("direct:create").to("gridfs:myDb?database={{mongodb.testDb}}&operation=create&bucket=" + getBucket()); + from("direct:remove").to("gridfs:myDb?database={{mongodb.testDb}}&operation=remove&bucket=" + getBucket()); + from("direct:findOne").to("gridfs:myDb?database={{mongodb.testDb}}&operation=findOne&bucket=" + getBucket()); + from("direct:listAll").to("gridfs:myDb?database={{mongodb.testDb}}&operation=listAll&bucket=" + getBucket()); + from("direct:count").to("gridfs:myDb?database={{mongodb.testDb}}&operation=count&bucket=" + getBucket()); + from("direct:headerOp").to("gridfs:myDb?database={{mongodb.testDb}}&bucket=" + getBucket()); + } + }; + } + + @Test + public void testOperations() throws Exception { + Map<String, Object> headers = new HashMap<String, Object>(); + String fn = "filename.for.db.txt"; + assertEquals(0, gridfs.find(fn).size()); + + headers.put(Exchange.FILE_NAME, fn); + String data = "This is some stuff to go into the db"; + template.requestBodyAndHeaders("direct:create", data, headers); + assertEquals(1, gridfs.find(fn).size()); + assertEquals(1, template.requestBodyAndHeaders("direct:count", null, headers)); + InputStream ins = template.requestBodyAndHeaders("direct:findOne", null, headers, InputStream.class); + assertNotNull(ins); + byte b[] = new byte[2048]; + int i = ins.read(b); + assertEquals(data, new String(b, 0, i, "utf-8")); + + headers.put(Exchange.FILE_NAME, "2-" + fn); + + template.requestBodyAndHeaders("direct:create", data + "data2", headers); + assertEquals(1, template.requestBodyAndHeaders("direct:count", null, headers)); + assertEquals(2, template.requestBody("direct:count", null, Integer.class).intValue()); + + String s = template.requestBody("direct:listAll", null, String.class); + assertTrue(s.contains("2-" + fn)); + template.requestBodyAndHeaders("direct:remove", null, headers); + assertEquals(1, template.requestBody("direct:count", null, Integer.class).intValue()); + s = template.requestBody("direct:listAll", null, String.class); + assertFalse(s.contains("2-" + fn)); + } +} + http://git-wip-us.apache.org/repos/asf/camel/blob/b4845575/components/camel-mongodb-gridfs/src/test/resources/log4j.properties ---------------------------------------------------------------------- diff --git a/components/camel-mongodb-gridfs/src/test/resources/log4j.properties b/components/camel-mongodb-gridfs/src/test/resources/log4j.properties new file mode 100644 index 0000000..cb64298 --- /dev/null +++ b/components/camel-mongodb-gridfs/src/test/resources/log4j.properties @@ -0,0 +1,37 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +log4j.rootLogger=INFO, file +# change the logging level of this category to increase verbosity of the MongoDB component +log4j.category.org.apache.camel.component.mongodb=INFO, file +log4j.additivity.org.apache.camel.component.mongodb=false + +# uncomment the following line to turn on Camel debugging +#log4j.logger.org.apache.camel=DEBUG + +# CONSOLE appender not used by default +log4j.appender.out=org.apache.log4j.ConsoleAppender +log4j.appender.out.layout=org.apache.log4j.PatternLayout +log4j.appender.out.layout.ConversionPattern=[%30.30t] %-30.30c{1} %-5p %m%n +#log4j.appender.out.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n + + +# File appender +log4j.appender.file=org.apache.log4j.FileAppender +log4j.appender.file.layout=org.apache.log4j.PatternLayout +log4j.appender.file.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n +log4j.appender.file.file=target/camel-mongodb-test.log http://git-wip-us.apache.org/repos/asf/camel/blob/b4845575/components/camel-mongodb-gridfs/src/test/resources/mongodb.test.properties ---------------------------------------------------------------------- diff --git a/components/camel-mongodb-gridfs/src/test/resources/mongodb.test.properties b/components/camel-mongodb-gridfs/src/test/resources/mongodb.test.properties new file mode 100644 index 0000000..20c529d --- /dev/null +++ b/components/camel-mongodb-gridfs/src/test/resources/mongodb.test.properties @@ -0,0 +1,21 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +mongodb.connectionURI=mongodb://localhost:27017 +mongodb.testDb=test +mongodb.testCollection=camelTest +mongodb.cappedTestCollection=camelTestCapped \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/b4845575/components/pom.xml ---------------------------------------------------------------------- diff --git a/components/pom.xml b/components/pom.xml index 80c11ca..a71de32 100644 --- a/components/pom.xml +++ b/components/pom.xml @@ -114,7 +114,6 @@ <module>camel-google-mail</module> <module>camel-gora</module> <module>camel-grape</module> - <module>camel-gridfs</module> <module>camel-gson</module> <module>camel-guava-eventbus</module> <module>camel-guice</module> @@ -171,6 +170,7 @@ <module>camel-mina2</module> <module>camel-mllp</module> <module>camel-mongodb</module> + <module>camel-mongodb-gridfs</module> <module>camel-mqtt</module> <module>camel-msv</module> <module>camel-mustache</module> http://git-wip-us.apache.org/repos/asf/camel/blob/b4845575/platforms/karaf/features/src/main/resources/features.xml ---------------------------------------------------------------------- diff --git a/platforms/karaf/features/src/main/resources/features.xml b/platforms/karaf/features/src/main/resources/features.xml index 64651f2..3d6f777 100644 --- a/platforms/karaf/features/src/main/resources/features.xml +++ b/platforms/karaf/features/src/main/resources/features.xml @@ -1090,9 +1090,9 @@ <feature version='${project.version}'>camel-core</feature> <feature version='${project.version}'>camel-jackson</feature> </feature> - <feature name='camel-gridfs' version='${project.version}' resolver='(obr)' start-level='50'> + <feature name='camel-mongodb-gridfs' version='${project.version}' resolver='(obr)' start-level='50'> <bundle dependency='true'>mvn:org.mongodb/mongo-java-driver/${mongo-java-driver-version}</bundle> - <bundle>mvn:org.apache.camel/camel-gridfs/${project.version}</bundle> + <bundle>mvn:org.apache.camel/camel-mongodb-gridfs/${project.version}</bundle> <feature version='${project.version}'>camel-core</feature> <feature version='${project.version}'>camel-jackson</feature> </feature>