Search in sources :

Example 1 with GetMetadataErrorException

use of com.dropbox.core.v2.files.GetMetadataErrorException in project orgzly-android by orgzly.

the class DropboxClient method getBooks.

public List<VersionedRook> getBooks(Uri repoUri) throws IOException {
    linkedOrThrow();
    List<VersionedRook> list = new ArrayList<>();
    String path = repoUri.getPath();
    /* Fix root path. */
    if (path == null || path.equals("/")) {
        path = ROOT_PATH;
    }
    /* Strip trailing slashes. */
    path = path.replaceAll("/+$", "");
    try {
        if (ROOT_PATH.equals(path) || dbxClient.files().getMetadata(path) instanceof FolderMetadata) {
            /* Get folder content. */
            ListFolderResult result = dbxClient.files().listFolder(path);
            while (true) {
                for (Metadata metadata : result.getEntries()) {
                    if (metadata instanceof FileMetadata) {
                        FileMetadata file = (FileMetadata) metadata;
                        if (BookName.isSupportedFormatFileName(file.getName())) {
                            Uri uri = repoUri.buildUpon().appendPath(file.getName()).build();
                            VersionedRook book = new VersionedRook(repoUri, uri, file.getRev(), file.getServerModified().getTime());
                            list.add(book);
                        }
                    }
                }
                if (!result.getHasMore()) {
                    break;
                }
                result = dbxClient.files().listFolderContinue(result.getCursor());
            }
        } else {
            throw new IOException("Not a directory: " + repoUri);
        }
    } catch (DbxException e) {
        e.printStackTrace();
        /* If we get NOT_FOUND from Dropbox, just return the empty list. */
        if (e instanceof GetMetadataErrorException) {
            if (((GetMetadataErrorException) e).errorValue.getPathValue() == LookupError.NOT_FOUND) {
                return list;
            }
        }
        throw new IOException("Failed getting the list of files in " + repoUri + " listing " + path + ": " + (e.getMessage() != null ? e.getMessage() : e.toString()));
    }
    return list;
}
Also used : 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) FileMetadata(com.dropbox.core.v2.files.FileMetadata) ListFolderResult(com.dropbox.core.v2.files.ListFolderResult) FolderMetadata(com.dropbox.core.v2.files.FolderMetadata) IOException(java.io.IOException) Uri(android.net.Uri) DbxException(com.dropbox.core.DbxException) GetMetadataErrorException(com.dropbox.core.v2.files.GetMetadataErrorException)

Example 2 with GetMetadataErrorException

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

the class DropboxV2Storage method convertException.

private Exception convertException(DbxException e) {
    Log.d(TAG, "Exception of type " + e.getClass().getName() + ":" + e.getMessage());
    if (InvalidAccessTokenException.class.isAssignableFrom(e.getClass())) {
        Log.d(TAG, "LoggedIn=false (due to InvalidAccessTokenException)");
        setLoggedIn(false);
        clearKeys();
        return new UserInteractionRequiredException("Unlinked from Dropbox! User must re-link.", e);
    }
    if (ListFolderErrorException.class.isAssignableFrom(e.getClass())) {
        ListFolderErrorException listFolderErrorException = (ListFolderErrorException) e;
        if (listFolderErrorException.errorValue.getPathValue().isNotFound())
            return new FileNotFoundException(e.toString());
    }
    if (DownloadErrorException.class.isAssignableFrom(e.getClass())) {
        DownloadErrorException downloadErrorException = (DownloadErrorException) e;
        if (downloadErrorException.errorValue.getPathValue().isNotFound())
            return new FileNotFoundException(e.toString());
    }
    if (GetMetadataErrorException.class.isAssignableFrom(e.getClass())) {
        GetMetadataErrorException getMetadataErrorException = (GetMetadataErrorException) e;
        if (getMetadataErrorException.errorValue.getPathValue().isNotFound())
            return new FileNotFoundException(e.toString());
    }
    if (DeleteErrorException.class.isAssignableFrom(e.getClass())) {
        DeleteErrorException deleteErrorException = (DeleteErrorException) e;
        if (deleteErrorException.errorValue.getPathLookupValue().isNotFound())
            return new FileNotFoundException(e.toString());
    }
    return e;
}
Also used : ListFolderErrorException(com.dropbox.core.v2.files.ListFolderErrorException) FileNotFoundException(java.io.FileNotFoundException) DeleteErrorException(com.dropbox.core.v2.files.DeleteErrorException) DownloadErrorException(com.dropbox.core.v2.files.DownloadErrorException) GetMetadataErrorException(com.dropbox.core.v2.files.GetMetadataErrorException)

Example 3 with GetMetadataErrorException

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

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

the class DbxClientV2IT method testError409.

@Test(expectedExceptions = { GetMetadataErrorException.class })
public void testError409() throws Exception {
    DbxClientV2 client = ITUtil.newClientV2();
    String path = ITUtil.path(getClass(), "/testError409/" + ITUtil.format(new Date()));
    try {
        client.files().getMetadata(path);
    } catch (GetMetadataErrorException ex) {
        assertThat(ex.getRequestId()).isNotNull();
        if (ex.getUserMessage() != null) {
            assertThat(ex.getUserMessage().getLocale()).isNotNull();
            assertThat(ex.getUserMessage().getText()).isNotNull();
            assertThat(ex.getUserMessage().toString()).isNotNull();
        }
        GetMetadataError err = ex.errorValue;
        assertThat(err).isNotNull();
        assertThat(err.tag()).isEqualTo(GetMetadataError.Tag.PATH);
        assertThat(err.isPath()).isTrue();
        LookupError lookup = err.getPathValue();
        assertThat(lookup).isNotNull();
        assertThat(lookup.tag()).isEqualTo(LookupError.Tag.NOT_FOUND);
        assertThat(lookup.isNotFound()).isTrue();
        assertThat(lookup.isNotFile()).isFalse();
        assertThat(lookup.isOther()).isFalse();
        assertThat(lookup.isMalformedPath()).isFalse();
        // raise so test can confirm an exception was thrown
        throw ex;
    }
}
Also used : GetMetadataError(com.dropbox.core.v2.files.GetMetadataError) Date(java.util.Date) LookupError(com.dropbox.core.v2.files.LookupError) GetMetadataErrorException(com.dropbox.core.v2.files.GetMetadataErrorException) Test(org.testng.annotations.Test)

Aggregations

GetMetadataErrorException (com.dropbox.core.v2.files.GetMetadataErrorException)4 DbxException (com.dropbox.core.DbxException)2 FileMetadata (com.dropbox.core.v2.files.FileMetadata)2 FolderMetadata (com.dropbox.core.v2.files.FolderMetadata)2 LookupError (com.dropbox.core.v2.files.LookupError)2 Metadata (com.dropbox.core.v2.files.Metadata)2 Uri (android.net.Uri)1 DbxClientV2 (com.dropbox.core.v2.DbxClientV2)1 DeleteErrorException (com.dropbox.core.v2.files.DeleteErrorException)1 DeletedMetadata (com.dropbox.core.v2.files.DeletedMetadata)1 DownloadErrorException (com.dropbox.core.v2.files.DownloadErrorException)1 GetMetadataError (com.dropbox.core.v2.files.GetMetadataError)1 ListFolderErrorException (com.dropbox.core.v2.files.ListFolderErrorException)1 ListFolderResult (com.dropbox.core.v2.files.ListFolderResult)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Test (org.testng.annotations.Test)1