Search in sources :

Example 1 with DeletedMetadata

use of com.dropbox.core.v2.files.DeletedMetadata in project keepass2android by PhilippC.

the class DropboxV2Storage method convertToFileEntry.

private FileEntry convertToFileEntry(Metadata e) throws Exception {
    // Log.d("JFS","e="+e);
    FileEntry fileEntry = new FileEntry();
    fileEntry.canRead = true;
    fileEntry.canWrite = true;
    if (e instanceof FolderMetadata) {
        FolderMetadata fm = (FolderMetadata) e;
        fileEntry.isDirectory = true;
        fileEntry.sizeInBytes = 0;
        fileEntry.lastModifiedTime = 0;
    } else if (e instanceof FileMetadata) {
        FileMetadata fm = (FileMetadata) e;
        fileEntry.sizeInBytes = fm.getSize();
        fileEntry.isDirectory = false;
        fileEntry.lastModifiedTime = fm.getServerModified().getTime();
    } else if (e instanceof DeletedMetadata) {
        throw new FileNotFoundException();
    } else {
        throw new Exception("unexpected metadata " + e.getClass().getName());
    }
    fileEntry.path = getProtocolId() + "://" + e.getPathLower();
    fileEntry.displayName = e.getName();
    // Log.d("JFS","Ok. Dir="+fileEntry.isDirectory);
    return fileEntry;
}
Also used : FileMetadata(com.dropbox.core.v2.files.FileMetadata) FileNotFoundException(java.io.FileNotFoundException) FolderMetadata(com.dropbox.core.v2.files.FolderMetadata) DeletedMetadata(com.dropbox.core.v2.files.DeletedMetadata) GetMetadataErrorException(com.dropbox.core.v2.files.GetMetadataErrorException) DownloadErrorException(com.dropbox.core.v2.files.DownloadErrorException) FileNotFoundException(java.io.FileNotFoundException) InvalidAccessTokenException(com.dropbox.core.InvalidAccessTokenException) DbxException(com.dropbox.core.DbxException) ListFolderErrorException(com.dropbox.core.v2.files.ListFolderErrorException) DeleteErrorException(com.dropbox.core.v2.files.DeleteErrorException)

Example 2 with DeletedMetadata

use of com.dropbox.core.v2.files.DeletedMetadata in project dropbox-sdk-java by dropbox.

the class Main method testEnumeratedSubtypeSerialization.

private static void testEnumeratedSubtypeSerialization(DbxClientV2 client) throws DbxException, IOException {
    String rootPath = "/test/proguard-tests";
    try {
        FolderMetadata root = client.files().createFolderV2(rootPath).getMetadata();
        assertNotNull(root);
        assertEquals(root.getPathLower(), rootPath);
        assertEquals(root.getPathDisplay(), rootPath);
    } catch (CreateFolderErrorException ex) {
        if (ex.errorValue.isPath() && ex.errorValue.getPathValue().isConflict() && ex.errorValue.getPathValue().getConflictValue() == WriteConflictError.FOLDER) {
        // ignore duplicate folder exception
        } else {
            throw ex;
        }
    }
    Map<String, byte[]> files = new LinkedHashMap<String, byte[]>();
    files.put(rootPath + "/foo.blob", bytes(1024));
    files.put(rootPath + "/bar.blob", bytes(512));
    files.put(rootPath + "/sub/a.dat", bytes(4096));
    files.put(rootPath + "/sub/b/c.dat", bytes(64));
    files.put(rootPath + "/pics/cat.rawb", bytes(8196));
    try {
        for (Map.Entry<String, byte[]> entry : files.entrySet()) {
            String path = entry.getKey();
            byte[] data = entry.getValue();
            FileMetadata file = client.files().uploadBuilder(path).withMode(WriteMode.OVERWRITE).withAutorename(false).withMute(true).uploadAndFinish(new ByteArrayInputStream(data));
            assertNotNull(file);
            assertEquals(file.getPathLower(), path);
            assertEquals(file.getSize(), data.length);
            Metadata metadata = client.files().getMetadata(path);
            assertEquals(metadata, file);
        }
        for (String path : files.keySet()) {
            Metadata file = client.files().deleteV2(path).getMetadata();
            assertNotNull(file);
            assertEquals(file.getPathLower(), path);
            assertTrue(file instanceof FileMetadata);
            Metadata deleted = client.files().getMetadataBuilder(path).withIncludeDeleted(true).start();
            assertNotNull(deleted);
            assertTrue(deleted instanceof DeletedMetadata, deleted.getClass().toString());
            assertEquals(deleted.getPathLower(), path);
        }
    } finally {
        client.files().deleteV2(rootPath);
    }
}
Also used : CreateFolderErrorException(com.dropbox.core.v2.files.CreateFolderErrorException) ByteArrayInputStream(java.io.ByteArrayInputStream) FileMetadata(com.dropbox.core.v2.files.FileMetadata) 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) DeletedMetadata(com.dropbox.core.v2.files.DeletedMetadata) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with DeletedMetadata

use of com.dropbox.core.v2.files.DeletedMetadata in project dropbox-sdk-java by dropbox.

the class Main method printChanges.

/**
 * Prints changes made to a folder in Dropbox since the given
 * cursor was retrieved.
 *
 * @param dbxClient Dropbox client to use for fetching folder changes
 * @param cursor lastest cursor received since last set of changes
 *
 * @return latest cursor after changes
 */
private static String printChanges(DbxClientV2 client, String cursor) throws DbxApiException, DbxException {
    while (true) {
        ListFolderResult result = client.files().listFolderContinue(cursor);
        for (Metadata metadata : result.getEntries()) {
            String type;
            String details;
            if (metadata instanceof FileMetadata) {
                FileMetadata fileMetadata = (FileMetadata) metadata;
                type = "file";
                details = "(rev=" + fileMetadata.getRev() + ")";
            } else if (metadata instanceof FolderMetadata) {
                FolderMetadata folderMetadata = (FolderMetadata) metadata;
                type = "folder";
                details = folderMetadata.getSharingInfo() != null ? "(shared)" : "";
            } else if (metadata instanceof DeletedMetadata) {
                type = "deleted";
                details = "";
            } else {
                throw new IllegalStateException("Unrecognized metadata type: " + metadata.getClass());
            }
            System.out.printf("\t%10s %24s \"%s\"\n", type, details, metadata.getPathLower());
        }
        // update cursor to fetch remaining results
        cursor = result.getCursor();
        if (!result.getHasMore()) {
            break;
        }
    }
    return cursor;
}
Also used : FileMetadata(com.dropbox.core.v2.files.FileMetadata) FolderMetadata(com.dropbox.core.v2.files.FolderMetadata) Metadata(com.dropbox.core.v2.files.Metadata) DeletedMetadata(com.dropbox.core.v2.files.DeletedMetadata) FileMetadata(com.dropbox.core.v2.files.FileMetadata) ListFolderResult(com.dropbox.core.v2.files.ListFolderResult) FolderMetadata(com.dropbox.core.v2.files.FolderMetadata) DeletedMetadata(com.dropbox.core.v2.files.DeletedMetadata)

Example 4 with DeletedMetadata

use of com.dropbox.core.v2.files.DeletedMetadata 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

DeletedMetadata (com.dropbox.core.v2.files.DeletedMetadata)4 FileMetadata (com.dropbox.core.v2.files.FileMetadata)4 FolderMetadata (com.dropbox.core.v2.files.FolderMetadata)4 Metadata (com.dropbox.core.v2.files.Metadata)3 DbxException (com.dropbox.core.DbxException)2 ListFolderErrorException (com.dropbox.core.v2.files.ListFolderErrorException)2 ListFolderResult (com.dropbox.core.v2.files.ListFolderResult)2 InvalidAccessTokenException (com.dropbox.core.InvalidAccessTokenException)1 CreateFolderErrorException (com.dropbox.core.v2.files.CreateFolderErrorException)1 DeleteErrorException (com.dropbox.core.v2.files.DeleteErrorException)1 DownloadErrorException (com.dropbox.core.v2.files.DownloadErrorException)1 GetMetadataErrorException (com.dropbox.core.v2.files.GetMetadataErrorException)1 ListFolderContinueErrorException (com.dropbox.core.v2.files.ListFolderContinueErrorException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 PrintWriter (java.io.PrintWriter)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1