Search in sources :

Example 1 with DropboxResultCode

use of org.apache.camel.component.dropbox.util.DropboxResultCode in project camel by apache.

the class DropboxAPIFacade method put.

/**
     * Put or upload a new file or an entire directory to dropbox
     * @param localPath  the file path or the dir path on the local filesystem
     * @param remotePath the remote path destination on dropbox
     * @param mode how a file should be saved on dropbox;
     *             in case of "add" the new file will be renamed in case
     *             a file with the same name already exists on dropbox.
     *             in case of "force" the file already existing with the same name will be overridden.
     * @return a result object reporting for each remote path the result of the operation.
     * @throws DropboxException
     */
public DropboxFileUploadResult put(String localPath, String remotePath, DropboxUploadMode mode) throws DropboxException {
    //in case the remote path is not specified, the remotePath = localPath
    String dropboxPath = remotePath == null ? localPath : remotePath;
    DbxEntry entry;
    try {
        entry = client.getMetadata(dropboxPath);
    } catch (DbxException e) {
        throw new DropboxException(dropboxPath + " does not exist or can't obtain metadata");
    }
    File fileLocalPath = new File(localPath);
    //verify uploading of a single file
    if (fileLocalPath.isFile()) {
        //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(DROPBOX_FILE_SEPARATOR)) {
                dropboxPath = dropboxPath + fileLocalPath.getName();
            }
        }
        DropboxFileUploadResult result;
        try {
            DbxEntry.File uploadedFile = putSingleFile(fileLocalPath, 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;
    } else {
        //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()) {
            throw new DropboxException(dropboxPath + " exists on dropbox and is not a folder!");
        }
        if (!dropboxPath.endsWith(DROPBOX_FILE_SEPARATOR)) {
            dropboxPath = dropboxPath + DROPBOX_FILE_SEPARATOR;
        }
        //revert to old path
        String oldDropboxPath = dropboxPath;
        //list all files in a dir
        Collection<File> listFiles = FileUtils.listFiles(fileLocalPath, null, true);
        if (listFiles.isEmpty()) {
            throw new DropboxException(localPath + " doesn't contain any files");
        }
        HashMap<String, DropboxResultCode> resultMap = new HashMap<>(listFiles.size());
        for (File file : listFiles) {
            String absPath = file.getAbsolutePath();
            int indexRemainingPath = localPath.length();
            if (!localPath.endsWith("/")) {
                indexRemainingPath += 1;
            }
            String remainingPath = absPath.substring(indexRemainingPath);
            dropboxPath = dropboxPath + remainingPath;
            try {
                LOG.debug("Uploading: {},{}", fileLocalPath, dropboxPath);
                DbxEntry.File uploadedFile = putSingleFile(file, dropboxPath, mode);
                if (uploadedFile == null) {
                    resultMap.put(dropboxPath, DropboxResultCode.KO);
                } else {
                    resultMap.put(dropboxPath, DropboxResultCode.OK);
                }
            } catch (Exception ex) {
                resultMap.put(dropboxPath, DropboxResultCode.KO);
            }
            dropboxPath = oldDropboxPath;
        }
        return new DropboxFileUploadResult(resultMap);
    }
}
Also used : DbxEntry(com.dropbox.core.DbxEntry) DropboxFileUploadResult(org.apache.camel.component.dropbox.dto.DropboxFileUploadResult) IOException(java.io.IOException) DropboxException(org.apache.camel.component.dropbox.util.DropboxException) DbxException(com.dropbox.core.DbxException) DropboxResultCode(org.apache.camel.component.dropbox.util.DropboxResultCode) DropboxException(org.apache.camel.component.dropbox.util.DropboxException) File(java.io.File) DbxException(com.dropbox.core.DbxException)

Example 2 with DropboxResultCode

use of org.apache.camel.component.dropbox.util.DropboxResultCode in project camel by apache.

the class DropboxPutProducer method process.

@Override
public void process(Exchange exchange) throws Exception {
    DropboxFileUploadResult result = new DropboxAPIFacade(configuration.getClient(), exchange).put(configuration.getLocalPath(), configuration.getRemotePath(), configuration.getUploadMode());
    Map<String, DropboxResultCode> map = result.getResults();
    if (map.size() == 1) {
        for (Map.Entry<String, DropboxResultCode> entry : map.entrySet()) {
            exchange.getIn().setHeader(DropboxResultHeader.UPLOADED_FILE.name(), entry.getKey());
            exchange.getIn().setBody(entry.getValue());
        }
    } else {
        StringBuilder pathsExtracted = new StringBuilder();
        for (Map.Entry<String, DropboxResultCode> entry : map.entrySet()) {
            pathsExtracted.append(entry.getKey()).append("\n");
        }
        exchange.getIn().setHeader(DropboxResultHeader.UPLOADED_FILES.name(), pathsExtracted.toString());
        exchange.getIn().setBody(map);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Uploaded: {}", result.toString());
    }
}
Also used : DropboxResultCode(org.apache.camel.component.dropbox.util.DropboxResultCode) DropboxFileUploadResult(org.apache.camel.component.dropbox.dto.DropboxFileUploadResult) Map(java.util.Map) DropboxAPIFacade(org.apache.camel.component.dropbox.core.DropboxAPIFacade)

Aggregations

DropboxFileUploadResult (org.apache.camel.component.dropbox.dto.DropboxFileUploadResult)2 DropboxResultCode (org.apache.camel.component.dropbox.util.DropboxResultCode)2 DbxEntry (com.dropbox.core.DbxEntry)1 DbxException (com.dropbox.core.DbxException)1 File (java.io.File)1 IOException (java.io.IOException)1 Map (java.util.Map)1 DropboxAPIFacade (org.apache.camel.component.dropbox.core.DropboxAPIFacade)1 DropboxException (org.apache.camel.component.dropbox.util.DropboxException)1