Search in sources :

Example 21 with DbxException

use of com.dropbox.core.DbxException in project orgzly-android by orgzly.

the class DropboxClient method download.

/**
 * Download file from Dropbox and store it to a local file.
 */
public VersionedRook download(Uri repoUri, String fileName, File localFile) throws IOException {
    linkedOrThrow();
    Uri uri = repoUri.buildUpon().appendPath(fileName).build();
    OutputStream out = new BufferedOutputStream(new FileOutputStream(localFile));
    try {
        Metadata pathMetadata = dbxClient.files().getMetadata(uri.getPath());
        if (pathMetadata instanceof FileMetadata) {
            FileMetadata metadata = (FileMetadata) pathMetadata;
            String rev = metadata.getRev();
            long mtime = metadata.getServerModified().getTime();
            dbxClient.files().download(metadata.getPathLower(), rev).download(out);
            return new VersionedRook(repoUri, uri, rev, mtime);
        } else {
            throw new IOException("Failed downloading Dropbox file " + uri + ": Not a file");
        }
    } catch (DbxException e) {
        if (e.getMessage() != null) {
            throw new IOException("Failed downloading Dropbox file " + uri + ": " + e.getMessage());
        } else {
            throw new IOException("Failed downloading Dropbox file " + uri + ": " + e.toString());
        }
    } finally {
        out.close();
    }
}
Also used : BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) FolderMetadata(com.dropbox.core.v2.files.FolderMetadata) FileMetadata(com.dropbox.core.v2.files.FileMetadata) Metadata(com.dropbox.core.v2.files.Metadata) FileMetadata(com.dropbox.core.v2.files.FileMetadata) IOException(java.io.IOException) Uri(android.net.Uri) BufferedOutputStream(java.io.BufferedOutputStream) DbxException(com.dropbox.core.DbxException)

Example 22 with DbxException

use of com.dropbox.core.DbxException in project keepass2android by PhilippC.

the class DropboxV2Storage method uploadFile.

public void uploadFile(String path, byte[] data, boolean writeTransactional) throws Exception {
    ByteArrayInputStream bis = new ByteArrayInputStream(data);
    try {
        path = removeProtocol(path);
        dbxClient.files().uploadBuilder(path).withMode(WriteMode.OVERWRITE).uploadAndFinish(bis);
    } catch (DbxException e) {
        throw convertException(e);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DbxException(com.dropbox.core.DbxException)

Example 23 with DbxException

use of com.dropbox.core.DbxException in project drill by apache.

the class DropboxFileSystem method listStatus.

@Override
public FileStatus[] listStatus(Path path) throws IOException {
    client = getClient();
    List<FileStatus> fileStatusList = new ArrayList<>();
    // Get files and folder metadata from Dropbox root directory
    try {
        ListFolderResult result = client.files().listFolder("");
        while (true) {
            for (Metadata metadata : result.getEntries()) {
                fileStatusList.add(getFileInformation(metadata));
            }
            if (!result.getHasMore()) {
                break;
            }
            result = client.files().listFolderContinue(result.getCursor());
        }
    } catch (DbxException e) {
        throw new IOException(e.getMessage());
    }
    // Convert to Array
    fileStatuses = new FileStatus[fileStatusList.size()];
    for (int i = 0; i < fileStatusList.size(); i++) {
        fileStatuses[i] = fileStatusList.get(i);
    }
    return fileStatuses;
}
Also used : FileStatus(org.apache.hadoop.fs.FileStatus) ArrayList(java.util.ArrayList) FolderMetadata(com.dropbox.core.v2.files.FolderMetadata) FileMetadata(com.dropbox.core.v2.files.FileMetadata) Metadata(com.dropbox.core.v2.files.Metadata) ListFolderResult(com.dropbox.core.v2.files.ListFolderResult) IOException(java.io.IOException) DbxException(com.dropbox.core.DbxException)

Example 24 with DbxException

use of com.dropbox.core.DbxException in project dropbox-sdk-java by dropbox.

the class DropboxBrowse method doBrowse.

// -------------------------------------------------------------------------------------------
// GET /browse?path=...
// -------------------------------------------------------------------------------------------
// The page that lets you browse your Dropbox account.  Makes Dropbox API calls to figure out
// the contents of your files and folders and display them to you.
public void doBrowse(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    if (!common.checkGet(request, response))
        return;
    User user = common.getLoggedInUser(request);
    if (user == null) {
        common.pageSoftError(response, "Can't do /browse.  Nobody is logged in.");
        return;
    }
    DbxClientV2 dbxClient = requireDbxClient(request, response, user);
    if (dbxClient == null)
        return;
    String path = request.getParameter("path");
    if (path == null || path.length() == 0) {
        renderFolder(response, user, dbxClient, "");
    } else {
        String pathError = DbxPathV2.findError(path);
        if (pathError != null) {
            response.sendError(400, "Invalid path: " + jq(path) + ": " + pathError);
            return;
        }
        Metadata metadata;
        try {
            metadata = dbxClient.files().getMetadata(path);
        } catch (GetMetadataErrorException ex) {
            if (ex.errorValue.isPath()) {
                LookupError le = ex.errorValue.getPathValue();
                if (le.isNotFound()) {
                    response.sendError(400, "Path doesn't exist on Dropbox: " + jq(path));
                    return;
                }
            }
            common.handleException(response, ex, "getMetadata(" + jq(path) + ")");
            return;
        } catch (DbxException ex) {
            common.handleDbxException(response, user, ex, "getMetadata(" + jq(path) + ")");
            return;
        }
        path = DbxPathV2.getParent(path) + "/" + metadata.getName();
        if (metadata instanceof FolderMetadata) {
            renderFolder(response, user, dbxClient, path);
        } else {
            renderFile(response, path, (FileMetadata) metadata);
        }
    }
}
Also used : DbxClientV2(com.dropbox.core.v2.DbxClientV2) FolderMetadata(com.dropbox.core.v2.files.FolderMetadata) DeletedMetadata(com.dropbox.core.v2.files.DeletedMetadata) FileMetadata(com.dropbox.core.v2.files.FileMetadata) Metadata(com.dropbox.core.v2.files.Metadata) FolderMetadata(com.dropbox.core.v2.files.FolderMetadata) LookupError(com.dropbox.core.v2.files.LookupError) DbxException(com.dropbox.core.DbxException) GetMetadataErrorException(com.dropbox.core.v2.files.GetMetadataErrorException)

Example 25 with DbxException

use of com.dropbox.core.DbxException in project dropbox-sdk-java by dropbox.

the class DropboxBrowse method renderFolder.

private void renderFolder(HttpServletResponse response, User user, DbxClientV2 dbxClient, String path) throws IOException {
    // Get the folder listing from Dropbox.
    TreeMap<String, Metadata> children = new TreeMap<String, Metadata>();
    ListFolderResult result;
    try {
        try {
            result = dbxClient.files().listFolder(path);
        } catch (ListFolderErrorException ex) {
            if (ex.errorValue.isPath()) {
                if (checkPathError(response, path, ex.errorValue.getPathValue()))
                    return;
            }
            throw ex;
        }
        while (true) {
            for (Metadata md : result.getEntries()) {
                if (md instanceof DeletedMetadata) {
                    children.remove(md.getPathLower());
                } else {
                    children.put(md.getPathLower(), md);
                }
            }
            if (!result.getHasMore())
                break;
            try {
                result = dbxClient.files().listFolderContinue(result.getCursor());
            } catch (ListFolderContinueErrorException ex) {
                if (ex.errorValue.isPath()) {
                    if (checkPathError(response, path, ex.errorValue.getPathValue()))
                        return;
                }
                throw ex;
            }
        }
    } catch (DbxException ex) {
        common.handleDbxException(response, user, ex, "listFolder(" + jq(path) + ")");
        return;
    }
    FormProtection fp = FormProtection.start(response);
    response.setContentType("text/html");
    response.setCharacterEncoding("utf-8");
    PrintWriter out = new PrintWriter(IOUtil.utf8Writer(response.getOutputStream()));
    out.println("<html>");
    out.println("<head><title>" + escapeHtml4(path) + "- Web File Browser</title></head>");
    out.println("<body>");
    fp.insertAntiRedressHtml(out);
    out.println("<h2>Path: " + escapeHtml4(path) + "</h2>");
    // Upload form
    out.println("<form action='/upload' method='post' enctype='multipart/form-data'>");
    fp.insertAntiCsrfFormField(out);
    out.println("<label for='file'>Upload file:</label> <input name='file' type='file'/>");
    out.println("<input type='submit' value='Upload'/>");
    out.println("<input name='targetFolder' type='hidden' value='" + escapeHtml4(path) + "'/>");
    out.println("</form>");
    // Listing of folder contents.
    out.println("<ul>");
    for (Metadata child : children.values()) {
        String href = "/browse?path=" + DbxRequestUtil.encodeUrlParam(child.getPathLower());
        out.println("  <li><a href='" + escapeHtml4(href) + "'>" + escapeHtml4(child.getName()) + "</a></li>");
    }
    out.println("</ul>");
    out.println("</body>");
    out.println("</html>");
    out.flush();
}
Also used : ListFolderContinueErrorException(com.dropbox.core.v2.files.ListFolderContinueErrorException) ListFolderErrorException(com.dropbox.core.v2.files.ListFolderErrorException) FolderMetadata(com.dropbox.core.v2.files.FolderMetadata) DeletedMetadata(com.dropbox.core.v2.files.DeletedMetadata) FileMetadata(com.dropbox.core.v2.files.FileMetadata) Metadata(com.dropbox.core.v2.files.Metadata) ListFolderResult(com.dropbox.core.v2.files.ListFolderResult) DeletedMetadata(com.dropbox.core.v2.files.DeletedMetadata) TreeMap(java.util.TreeMap) DbxException(com.dropbox.core.DbxException) PrintWriter(java.io.PrintWriter)

Aggregations

DbxException (com.dropbox.core.DbxException)31 FileMetadata (com.dropbox.core.v2.files.FileMetadata)14 IOException (java.io.IOException)14 DbxRequestConfig (com.dropbox.core.DbxRequestConfig)10 Metadata (com.dropbox.core.v2.files.Metadata)9 DbxClientV2 (com.dropbox.core.v2.DbxClientV2)8 FolderMetadata (com.dropbox.core.v2.files.FolderMetadata)8 DbxWebAuth (com.dropbox.core.DbxWebAuth)7 File (java.io.File)6 JsonReader (com.dropbox.core.json.JsonReader)5 DeletedMetadata (com.dropbox.core.v2.files.DeletedMetadata)5 ListFolderResult (com.dropbox.core.v2.files.ListFolderResult)5 FileInputStream (java.io.FileInputStream)5 InputStream (java.io.InputStream)5 DbxAuthInfo (com.dropbox.core.DbxAuthInfo)4 BufferedReader (java.io.BufferedReader)4 FileOutputStream (java.io.FileOutputStream)4 InputStreamReader (java.io.InputStreamReader)4 Uri (android.net.Uri)3 DbxAppInfo (com.dropbox.core.DbxAppInfo)3