Search in sources :

Example 1 with DropboxException

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

the class DropboxEndpoint method createProducer.

/**
     * Create one of the camel producer available based on the configuration
     * @return the camel producer
     * @throws Exception
     */
public Producer createProducer() throws Exception {
    LOG.trace("Resolve producer dropbox endpoint {" + configuration.getOperation().toString() + "}");
    LOG.trace("Resolve producer dropbox attached client: " + configuration.getClient());
    if (configuration.getOperation() == DropboxOperation.put) {
        return new DropboxPutProducer(this, configuration);
    } else if (this.configuration.getOperation() == DropboxOperation.search) {
        return new DropboxSearchProducer(this, configuration);
    } else if (this.configuration.getOperation() == DropboxOperation.del) {
        return new DropboxDelProducer(this, configuration);
    } else if (this.configuration.getOperation() == DropboxOperation.get) {
        return new DropboxGetProducer(this, configuration);
    } else if (this.configuration.getOperation() == DropboxOperation.move) {
        return new DropboxMoveProducer(this, configuration);
    } else {
        throw new DropboxException("Operation specified is not valid for producer!");
    }
}
Also used : DropboxGetProducer(org.apache.camel.component.dropbox.integration.producer.DropboxGetProducer) DropboxMoveProducer(org.apache.camel.component.dropbox.integration.producer.DropboxMoveProducer) DropboxPutProducer(org.apache.camel.component.dropbox.integration.producer.DropboxPutProducer) DropboxSearchProducer(org.apache.camel.component.dropbox.integration.producer.DropboxSearchProducer) DropboxException(org.apache.camel.component.dropbox.util.DropboxException) DropboxDelProducer(org.apache.camel.component.dropbox.integration.producer.DropboxDelProducer)

Example 2 with DropboxException

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

the class DropboxEndpoint method createConsumer.

/**
     * Create one of the camel consumer available based on the configuration
     * @param processor  the given processor
     * @return the camel consumer
     * @throws Exception
     */
public Consumer createConsumer(Processor processor) throws Exception {
    LOG.trace("Resolve consumer dropbox endpoint {" + configuration.getOperation().toString() + "}");
    LOG.trace("Resolve consumer dropbox attached client:" + configuration.getClient());
    DropboxScheduledPollConsumer consumer;
    if (this.configuration.getOperation() == DropboxOperation.search) {
        consumer = new DropboxScheduledPollSearchConsumer(this, processor, configuration);
        consumer.setDelay(POLL_CONSUMER_DELAY);
        return consumer;
    } else if (this.configuration.getOperation() == DropboxOperation.get) {
        consumer = new DropboxScheduledPollGetConsumer(this, processor, configuration);
        consumer.setDelay(POLL_CONSUMER_DELAY);
        return consumer;
    } else {
        throw new DropboxException("Operation specified is not valid for consumer!");
    }
}
Also used : DropboxScheduledPollSearchConsumer(org.apache.camel.component.dropbox.integration.consumer.DropboxScheduledPollSearchConsumer) DropboxException(org.apache.camel.component.dropbox.util.DropboxException) DropboxScheduledPollGetConsumer(org.apache.camel.component.dropbox.integration.consumer.DropboxScheduledPollGetConsumer) DropboxScheduledPollConsumer(org.apache.camel.component.dropbox.integration.consumer.DropboxScheduledPollConsumer)

Example 3 with DropboxException

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

the class DropboxAPIFacade method downloadFilesInFolder.

private Map<String, Object> downloadFilesInFolder(String path) throws DropboxException {
    try {
        DbxEntry.WithChildren listing = client.getMetadataWithChildren(path);
        if (listing == null) {
            return Collections.emptyMap();
        } else if (listing.children == null) {
            LOG.debug("downloading a single file...");
            Map.Entry<String, Object> entry = downloadSingleFile(path);
            return Collections.singletonMap(entry.getKey(), entry.getValue());
        }
        Map<String, Object> result = new HashMap<>();
        for (DbxEntry entry : listing.children) {
            if (entry.isFile()) {
                try {
                    Map.Entry<String, Object> singleFile = downloadSingleFile(entry.path);
                    result.put(singleFile.getKey(), singleFile.getValue());
                } catch (DropboxException e) {
                    LOG.warn("Cannot download from path={}, reason={}. This exception is ignored.", entry.path, e.getMessage());
                }
            } else {
                Map<String, Object> filesInFolder = downloadFilesInFolder(entry.path);
                result.putAll(filesInFolder);
            }
        }
        return result;
    } catch (DbxException e) {
        throw new DropboxException(e);
    }
}
Also used : DbxEntry(com.dropbox.core.DbxEntry) DbxEntry(com.dropbox.core.DbxEntry) DropboxException(org.apache.camel.component.dropbox.util.DropboxException) DbxException(com.dropbox.core.DbxException)

Example 4 with DropboxException

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

the class DropboxAPIFacade method downloadSingleFile.

private Map.Entry<String, Object> downloadSingleFile(String path) throws DropboxException {
    try {
        OutputStreamBuilder target = OutputStreamBuilder.withExchange(exchange);
        DbxEntry.File downloadedFile = client.getFile(path, null, target);
        if (downloadedFile != null) {
            LOG.debug("downloaded path={}", path);
            return new AbstractMap.SimpleEntry<>(path, target.build());
        } else {
            return null;
        }
    } catch (DbxException e) {
        throw new DropboxException(path + " does not exist or can't obtain metadata");
    } catch (IOException e) {
        throw new DropboxException(path + " can't obtain a stream");
    }
}
Also used : DbxEntry(com.dropbox.core.DbxEntry) DropboxException(org.apache.camel.component.dropbox.util.DropboxException) IOException(java.io.IOException) OutputStreamBuilder(org.apache.camel.converter.stream.OutputStreamBuilder) DbxException(com.dropbox.core.DbxException)

Example 5 with DropboxException

use of org.apache.camel.component.dropbox.util.DropboxException 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)

Aggregations

DropboxException (org.apache.camel.component.dropbox.util.DropboxException)5 DbxEntry (com.dropbox.core.DbxEntry)3 DbxException (com.dropbox.core.DbxException)3 IOException (java.io.IOException)2 File (java.io.File)1 DropboxFileUploadResult (org.apache.camel.component.dropbox.dto.DropboxFileUploadResult)1 DropboxScheduledPollConsumer (org.apache.camel.component.dropbox.integration.consumer.DropboxScheduledPollConsumer)1 DropboxScheduledPollGetConsumer (org.apache.camel.component.dropbox.integration.consumer.DropboxScheduledPollGetConsumer)1 DropboxScheduledPollSearchConsumer (org.apache.camel.component.dropbox.integration.consumer.DropboxScheduledPollSearchConsumer)1 DropboxDelProducer (org.apache.camel.component.dropbox.integration.producer.DropboxDelProducer)1 DropboxGetProducer (org.apache.camel.component.dropbox.integration.producer.DropboxGetProducer)1 DropboxMoveProducer (org.apache.camel.component.dropbox.integration.producer.DropboxMoveProducer)1 DropboxPutProducer (org.apache.camel.component.dropbox.integration.producer.DropboxPutProducer)1 DropboxSearchProducer (org.apache.camel.component.dropbox.integration.producer.DropboxSearchProducer)1 DropboxResultCode (org.apache.camel.component.dropbox.util.DropboxResultCode)1 OutputStreamBuilder (org.apache.camel.converter.stream.OutputStreamBuilder)1