Search in sources :

Example 1 with DbxEntry

use of com.dropbox.core.DbxEntry in project camel by apache.

the class DropboxScheduledPollSearchConsumer method poll.

/**
     * Poll from a dropbox remote path and put the result in the message exchange
     * @return number of messages polled
     * @throws Exception
     */
@Override
protected int poll() throws Exception {
    Exchange exchange = endpoint.createExchange();
    DropboxSearchResult result = new DropboxAPIFacade(configuration.getClient(), exchange).search(configuration.getRemotePath(), configuration.getQuery());
    StringBuilder fileExtracted = new StringBuilder();
    for (DbxEntry entry : result.getFound()) {
        fileExtracted.append(entry.name).append("-").append(entry.path).append("\n");
    }
    exchange.getIn().setHeader(DropboxResultHeader.FOUND_FILES.name(), fileExtracted.toString());
    exchange.getIn().setBody(result.getFound());
    if (LOG.isDebugEnabled()) {
        LOG.debug("Downloaded: {}", result.toString());
    }
    try {
        // send message to next processor in the route
        getProcessor().process(exchange);
        // number of messages polled
        return 1;
    } finally {
        // log exception if an exception occurred and was not handled
        if (exchange.getException() != null) {
            getExceptionHandler().handleException("Error processing exchange", exchange, exchange.getException());
        }
    }
}
Also used : Exchange(org.apache.camel.Exchange) DbxEntry(com.dropbox.core.DbxEntry) DropboxSearchResult(org.apache.camel.component.dropbox.dto.DropboxSearchResult) DropboxAPIFacade(org.apache.camel.component.dropbox.core.DropboxAPIFacade)

Example 2 with DbxEntry

use of com.dropbox.core.DbxEntry in project openhab1-addons by openhab.

the class DropboxSynchronizer method syncLocalToDropbox.

/**
     * Synchronizes all changes from the local filesystem into Dropbox. Changes
     * are identified by the files' <code>lastModified</code> attribute. If there
     * are less files locally the additional files will be deleted from the
     * Dropbox. New files will be uploaded or overwritten if they exist already.
     * 
     * @throws DbxException if there are technical or application level
     *             errors in the Dropbox communication
     * @throws IOException
     */
public void syncLocalToDropbox(DbxClient client) throws DbxException, IOException {
    logger.debug("Started synchronization from local to Dropbox ...");
    Map<String, Long> dropboxEntries = new HashMap<String, Long>();
    WithChildren metadata = client.getMetadataWithChildren("/");
    File dropboxEntryFile = new File(DBX_FOLDER + DROPBOX_ENTRIES_FILE_NAME);
    if (!dropboxEntryFile.exists() || !metadata.hash.equals(lastHash)) {
        collectDropboxEntries(client, dropboxEntries, "/");
        serializeDropboxEntries(dropboxEntryFile, dropboxEntries);
        lastHash = metadata.hash;
    // TODO: TEE: we could think about writing the 'lastHash' to a file?
    // let's see what daily use brings whether this a necessary feature!
    } else {
        logger.trace("Dropbox entry file '{}' exists -> extract content", dropboxEntryFile.getPath());
        dropboxEntries = extractDropboxEntries(dropboxEntryFile);
    }
    Map<String, Long> localEntries = new HashMap<String, Long>();
    collectLocalEntries(localEntries, contentDir);
    logger.debug("There are '{}' local entries that met the upload filters ...", localEntries.size());
    boolean isChanged = false;
    for (java.util.Map.Entry<String, Long> entry : localEntries.entrySet()) {
        if (dropboxEntries.containsKey(entry.getKey())) {
            if (entry.getValue().compareTo(dropboxEntries.get(entry.getKey())) > 0) {
                logger.trace("Local file '{}' is newer - upload to Dropbox!", entry.getKey());
                if (!fakeMode) {
                    uploadFile(client, entry.getKey(), true);
                }
                isChanged = true;
            }
        } else {
            logger.trace("Local file '{}' doesn't exist in Dropbox - upload to Dropbox!", entry.getKey());
            if (!fakeMode) {
                uploadFile(client, entry.getKey(), false);
            }
            isChanged = true;
        }
        dropboxEntries.remove(entry.getKey());
    }
    // so delete them from Dropbox!
    for (String path : dropboxEntries.keySet()) {
        for (String filter : uploadFilterElements) {
            if (path.matches(filter)) {
                if (!fakeMode) {
                    client.delete(path);
                }
                isChanged = true;
                logger.debug("Successfully deleted file '{}' from Dropbox", path);
            } else {
                logger.trace("Skipped file '{}' since it doesn't match the given filter arguments.", path);
            }
        }
    }
    // which causes a new generation during the next sync
    if (isChanged) {
        boolean success = FileUtils.deleteQuietly(dropboxEntryFile);
        if (!success) {
            logger.warn("Couldn't delete file '{}'", dropboxEntryFile.getPath());
        } else {
            logger.debug("Deleted cache file '{}' since there are changes. It will be recreated on the next synchronization loop.", dropboxEntryFile.getPath());
        }
        // since there are changes we have to update the lastCursor (and
        // the corresponding file) to have the right starting point for the
        // next synchronization loop
        DbxDelta<DbxEntry> delta = client.getDelta(lastCursor);
        writeDeltaCursor(delta.cursor);
    } else {
        logger.debug("No files changed locally. No deltas to upload to Dropbox ...");
    }
}
Also used : WithChildren(com.dropbox.core.DbxEntry.WithChildren) DbxEntry(com.dropbox.core.DbxEntry) HashMap(java.util.HashMap) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with DbxEntry

use of com.dropbox.core.DbxEntry 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 DbxEntry

use of com.dropbox.core.DbxEntry 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 5 with DbxEntry

use of com.dropbox.core.DbxEntry in project camel by apache.

the class DropboxSearchProducer method process.

@Override
public void process(Exchange exchange) throws Exception {
    DropboxSearchResult result = new DropboxAPIFacade(configuration.getClient(), exchange).search(configuration.getRemotePath(), configuration.getQuery());
    StringBuilder fileExtracted = new StringBuilder();
    for (DbxEntry entry : result.getFound()) {
        fileExtracted.append(entry.name).append("-").append(entry.path).append("\n");
    }
    exchange.getIn().setHeader(DropboxResultHeader.FOUND_FILES.name(), fileExtracted.toString());
    exchange.getIn().setBody(result.getFound());
}
Also used : DbxEntry(com.dropbox.core.DbxEntry) DropboxSearchResult(org.apache.camel.component.dropbox.dto.DropboxSearchResult) DropboxAPIFacade(org.apache.camel.component.dropbox.core.DropboxAPIFacade)

Aggregations

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