Repository: camel
Updated Branches:
  refs/heads/master 3c385c32d -> 4552d4491


CAMEL-11680: Camel-dropbox should support Put not only from localPath


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/4552d449
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/4552d449
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/4552d449

Branch: refs/heads/master
Commit: 4552d44917ba0cd472c7e28fc539354e215d6793
Parents: 3c385c3
Author: Claus Ibsen <davscl...@apache.org>
Authored: Tue Sep 5 19:10:28 2017 +0200
Committer: Claus Ibsen <davscl...@apache.org>
Committed: Tue Sep 5 19:10:28 2017 +0200

----------------------------------------------------------------------
 .../src/main/docs/dropbox-component.adoc        | 20 ++++-
 .../component/dropbox/DropboxConfiguration.java |  3 +-
 .../dropbox/core/DropboxAPIFacade.java          | 81 +++++++++++++++++++-
 .../dropbox/util/DropboxConstants.java          |  1 +
 4 files changed, 100 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/4552d449/components/camel-dropbox/src/main/docs/dropbox-component.adoc
----------------------------------------------------------------------
diff --git a/components/camel-dropbox/src/main/docs/dropbox-component.adoc 
b/components/camel-dropbox/src/main/docs/dropbox-component.adoc
index 3281c55..015620f 100644
--- a/components/camel-dropbox/src/main/docs/dropbox-component.adoc
+++ b/components/camel-dropbox/src/main/docs/dropbox-component.adoc
@@ -91,7 +91,7 @@ with the following path and query parameters:
 | **accessToken** (common) | *Required* The access token to make API requests 
for a specific Dropbox user |  | String
 | **client** (common) | To use an existing DbxClient instance as DropBox 
client. |  | DbxClient
 | **clientIdentifier** (common) | *Required* Name of the app registered to 
make API requests |  | String
-| **localPath** (common) | Folder or file to upload on Dropbox from the local 
filesystem. |  | String
+| **localPath** (common) | Optional folder or file to upload on Dropbox from 
the local filesystem. If this option has not been configured then the message 
body is used as the content to upload. |  | String
 | **newRemotePath** (common) | Destination file or folder |  | String
 | **query** (common) | A space-separated list of sub-strings to search for. A 
file matches only if it contains all the sub-strings. If this option is not set 
all files will be matched. |  | String
 | **remotePath** (common) | Original file or folder to move |  | String
@@ -266,7 +266,9 @@ dropbox: in case of "add" the new file will be renamed if a 
file with the same
 name already exists on dropbox. In case of "force" if a file with the same 
name already exists on
 dropbox, this will be overwritten.
 
-|`localPath` |`true` |Folder or file to upload on Dropbox from the local 
filesystem .
+|`localPath` |`false` |Folder or file to upload on Dropbox from the local 
filesystem.
+If this option has been configured then it takes precedence over uploading as 
a single
+file with content from the Camel message body (message body is converted into 
a byte array).
 
 |`remotePath` |`false` |Folder destination on Dropbox. If the property is not 
set, the component
 will upload the file on a remote path equal to the local path.
@@ -281,6 +283,20 @@ 
from("direct:start").to("dropbox://put?accessToken=XXX&clientIdentifier=XXX&uplo
 
from("direct:start").to("dropbox://put?accessToken=XXX&clientIdentifier=XXX&uploadMode=add&localPath=/root/folder1&remotePath=/root/folder2").to("mock:result");
 -------------------------------
 
+And to upload a single file with content from the message body
+
+[source,java]
+-------------------------------
+from("direct:start")
+   .setHeader(DropboxConstants.HEADER_PUT_FILE_NAME, constant("myfile.txt"))
+   
.to("dropbox://put?accessToken=XXX&clientIdentifier=XXX&uploadMode=add&remotePath=/root/folder2")
+   .to("mock:result");
+-------------------------------
+
+The name of the file can be provided in the header 
`DropboxConstants.HEADER_PUT_FILE_NAME`
+or `Exchange.FILE_NAME` in that order of precedence. If no header has been 
provided then the message id (uuid) is
+used as the file name.
+
 #### Result Message Headers
 
 The following headers are set on message result:

http://git-wip-us.apache.org/repos/asf/camel/blob/4552d449/components/camel-dropbox/src/main/java/org/apache/camel/component/dropbox/DropboxConfiguration.java
----------------------------------------------------------------------
diff --git 
a/components/camel-dropbox/src/main/java/org/apache/camel/component/dropbox/DropboxConfiguration.java
 
b/components/camel-dropbox/src/main/java/org/apache/camel/component/dropbox/DropboxConfiguration.java
index 753e4d8..e02c82d 100755
--- 
a/components/camel-dropbox/src/main/java/org/apache/camel/component/dropbox/DropboxConfiguration.java
+++ 
b/components/camel-dropbox/src/main/java/org/apache/camel/component/dropbox/DropboxConfiguration.java
@@ -93,7 +93,8 @@ public class DropboxConfiguration {
     }
 
     /**
-     * Folder or file to upload on Dropbox from the local filesystem.
+     * Optional folder or file to upload on Dropbox from the local filesystem.
+     * If this option has not been configured then the message body is used as 
the content to upload.
      */
     public void setLocalPath(String localPath) {
         this.localPath = localPath;

http://git-wip-us.apache.org/repos/asf/camel/blob/4552d449/components/camel-dropbox/src/main/java/org/apache/camel/component/dropbox/core/DropboxAPIFacade.java
----------------------------------------------------------------------
diff --git 
a/components/camel-dropbox/src/main/java/org/apache/camel/component/dropbox/core/DropboxAPIFacade.java
 
b/components/camel-dropbox/src/main/java/org/apache/camel/component/dropbox/core/DropboxAPIFacade.java
index 832cac90..4daf08f 100755
--- 
a/components/camel-dropbox/src/main/java/org/apache/camel/component/dropbox/core/DropboxAPIFacade.java
+++ 
b/components/camel-dropbox/src/main/java/org/apache/camel/component/dropbox/core/DropboxAPIFacade.java
@@ -16,9 +16,11 @@
  */
 package org.apache.camel.component.dropbox.core;
 
+import java.io.ByteArrayInputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.util.AbstractMap;
 import java.util.Collection;
 import java.util.Collections;
@@ -45,6 +47,8 @@ import org.apache.commons.io.FileUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import static 
org.apache.camel.component.dropbox.util.DropboxConstants.HEADER_PUT_FILE_NAME;
+
 public final class DropboxAPIFacade {
 
     private static final transient Logger LOG = 
LoggerFactory.getLogger(DropboxAPIFacade.class);
@@ -84,6 +88,15 @@ public final class DropboxAPIFacade {
         } catch (DbxException e) {
             throw new DropboxException(dropboxPath + " does not exist or can't 
obtain metadata");
         }
+
+        if (localPath != null) {
+            return putFile(localPath, mode, dropboxPath, entry);
+        } else {
+            return putBody(exchange, mode, dropboxPath, entry);
+        }
+    }
+
+    private DropboxFileUploadResult putFile(String localPath, 
DropboxUploadMode mode, String dropboxPath, DbxEntry entry) throws 
DropboxException {
         File fileLocalPath = new File(localPath);
         //verify uploading of a single file
         if (fileLocalPath.isFile()) {
@@ -98,6 +111,7 @@ public final class DropboxAPIFacade {
                 }
             }
 
+            LOG.debug("Uploading: {},{}", fileLocalPath, dropboxPath);
             DropboxFileUploadResult result;
             try {
                 DbxEntry.File uploadedFile = putSingleFile(fileLocalPath, 
dropboxPath, mode);
@@ -110,7 +124,8 @@ public final class DropboxAPIFacade {
                 result = new DropboxFileUploadResult(dropboxPath, 
DropboxResultCode.KO);
             }
             return result;
-        } else {       //verify uploading of a list of files inside a dir
+        } else if (fileLocalPath.isDirectory()) {
+            //verify uploading of a list of files inside a dir
             LOG.debug("Uploading a dir...");
             //check if dropbox folder exists
             if (entry != null && !entry.isFolder()) {
@@ -150,7 +165,48 @@ public final class DropboxAPIFacade {
                 dropboxPath = oldDropboxPath;
             }
             return new DropboxFileUploadResult(resultMap);
+        } else {
+            return null;
+        }
+    }
+
+    private DropboxFileUploadResult putBody(Exchange exchange, 
DropboxUploadMode mode, String dropboxPath, DbxEntry entry) throws 
DropboxException {
+        String name = exchange.getIn().getHeader(HEADER_PUT_FILE_NAME, 
String.class);
+        if (name == null) {
+            // fallback to use CamelFileName
+            name = exchange.getIn().getHeader(Exchange.FILE_NAME, 
String.class);
+        }
+        if (name == null) {
+            // use message id as file name
+            name = exchange.getIn().getMessageId();
+        }
+
+        //check if dropbox file exists
+        if (entry != null && !entry.isFile()) {
+            throw new DropboxException(dropboxPath + " exists on dropbox and 
is not a file!");
+        }
+        //in case the entry not exists on dropbox check if the filename should 
be appended
+        if (entry == null) {
+            if (dropboxPath.endsWith(DropboxConstants.DROPBOX_FILE_SEPARATOR)) 
{
+                dropboxPath = dropboxPath + name;
+            }
+        }
+
+        LOG.debug("Uploading message body: {}", dropboxPath);
+
+        DropboxFileUploadResult result;
+        try {
+            DbxEntry.File uploadedFile = putSingleBody(exchange, dropboxPath, 
mode);
+            if (uploadedFile == null) {
+                result = new DropboxFileUploadResult(dropboxPath, 
DropboxResultCode.KO);
+            } else {
+                result = new DropboxFileUploadResult(dropboxPath, 
DropboxResultCode.OK);
+            }
+        } catch (Exception ex) {
+            result = new DropboxFileUploadResult(dropboxPath, 
DropboxResultCode.KO);
         }
+        return result;
+
     }
 
     private DbxEntry.File putSingleFile(File inputFile, String dropboxPath, 
DropboxUploadMode mode) throws Exception {
@@ -170,6 +226,24 @@ public final class DropboxAPIFacade {
         }
     }
 
+    private DbxEntry.File putSingleBody(Exchange exchange, String dropboxPath, 
DropboxUploadMode mode) throws Exception {
+        byte[] data = exchange.getIn().getMandatoryBody(byte[].class);
+        InputStream is = new ByteArrayInputStream(data);
+        try {
+            DbxEntry.File uploadedFile;
+            DbxWriteMode uploadMode;
+            if (mode == DropboxUploadMode.force) {
+                uploadMode = DbxWriteMode.force();
+            } else {
+                uploadMode = DbxWriteMode.add();
+            }
+            uploadedFile = client.uploadFile(dropboxPath, uploadMode, 
data.length, is);
+            return uploadedFile;
+        } finally {
+            is.close();
+        }
+    }
+
     /**
      * Search inside a remote path including its sub directories.
      * The query param can be null.
@@ -241,7 +315,10 @@ public final class DropboxAPIFacade {
         return new 
DropboxFileDownloadResult(downloadFilesInFolder(remotePath));
     }
 
-
+    /**
+     * @deprecated not in use
+     */
+    @Deprecated
     public boolean isDirectory(String path) throws DropboxException {
         try {
             DbxEntry.WithChildren listing = 
client.getMetadataWithChildren(path);

http://git-wip-us.apache.org/repos/asf/camel/blob/4552d449/components/camel-dropbox/src/main/java/org/apache/camel/component/dropbox/util/DropboxConstants.java
----------------------------------------------------------------------
diff --git 
a/components/camel-dropbox/src/main/java/org/apache/camel/component/dropbox/util/DropboxConstants.java
 
b/components/camel-dropbox/src/main/java/org/apache/camel/component/dropbox/util/DropboxConstants.java
index b713b33..536c139 100755
--- 
a/components/camel-dropbox/src/main/java/org/apache/camel/component/dropbox/util/DropboxConstants.java
+++ 
b/components/camel-dropbox/src/main/java/org/apache/camel/component/dropbox/util/DropboxConstants.java
@@ -26,4 +26,5 @@ public interface DropboxConstants {
     String HEADER_LOCAL_PATH = "CamelDropboxLocalPath";
     String HEADER_UPLOAD_MODE = "CamelDropboxUploadMode";
     String HEADER_QUERY = "CamelDropboxQuery";
+    String HEADER_PUT_FILE_NAME = "CamelDropboxPutFileName";
 }

Reply via email to