Search in sources :

Example 21 with DbxClientV2

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

the class DbxClientV2IT method testRangeDownload.

@Test
public void testRangeDownload() throws Exception {
    DbxClientV2 client = ITUtil.newClientV2();
    byte[] contents = ITUtil.randomBytes(500);
    String path = ITUtil.path(getClass(), "/testRangeDownload.dat");
    FileMetadata metadata = client.files().uploadBuilder(path).withAutorename(false).withMode(WriteMode.OVERWRITE).withMute(true).uploadAndFinish(new ByteArrayInputStream(contents));
    assertThat(metadata.getSize()).isEqualTo(contents.length);
    assertRangeDownload(client, path, contents, 0, contents.length);
    assertRangeDownload(client, path, contents, 0, 200);
    assertRangeDownload(client, path, contents, 300, 200);
    assertRangeDownload(client, path, contents, 499, 1);
    assertRangeDownload(client, path, contents, 0, 600);
    assertRangeDownload(client, path, contents, 250, null);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) FileMetadata(com.dropbox.core.v2.files.FileMetadata) Test(org.testng.annotations.Test)

Example 22 with DbxClientV2

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

the class DbxClientV2IT method testUploadAndDownload.

private void testUploadAndDownload(DbxClientV2 client, boolean trackProgress) throws Exception {
    final byte[] contents = ITUtil.randomBytes(1024 << 8);
    String filename = "testUploadAndDownload.dat";
    String path = ITUtil.path(getClass(), "/" + filename);
    ProgressListener progressListener = null;
    if (trackProgress) {
        progressListener = createTestListener(contents.length);
    }
    FileMetadata metadata = client.files().uploadBuilder(path).withAutorename(false).withMode(WriteMode.ADD).withMute(true).uploadAndFinish(new ByteArrayInputStream(contents), progressListener);
    assertThat(metadata.getName()).isEqualTo(filename);
    assertThat(metadata.getPathLower()).isEqualTo(path.toLowerCase());
    assertThat(metadata.getSize()).isEqualTo(contents.length);
    Metadata actual = client.files().getMetadata(path);
    assertWithMessage(actual.getClass().getCanonicalName()).that(actual instanceof FileMetadata).isTrue();
    try {
        assertThat(actual).isEqualTo(metadata);
        if (trackProgress) {
            progressListener = createTestListener(contents.length);
        }
        DbxDownloader<FileMetadata> downloader = client.files().download(path);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        downloader.download(out, progressListener);
        byte[] actualContents = out.toByteArray();
        FileMetadata actualResult = downloader.getResult();
        assertThat(actualResult).isEqualTo(metadata);
        assertThat(actualContents).isEqualTo(contents);
        assertThat(downloader.getContentType()).isEqualTo("application/octet-stream");
    } catch (AssertionError e) {
        // so subsequent tests don't fail due to file not being cleaned up
        client.files().deleteV2(path).getMetadata();
        throw e;
    }
    Metadata deleted = client.files().deleteV2(path).getMetadata();
    assertThat(deleted).isEqualTo(metadata);
}
Also used : ProgressListener(com.dropbox.core.util.IOUtil.ProgressListener) ByteArrayInputStream(java.io.ByteArrayInputStream) FileMetadata(com.dropbox.core.v2.files.FileMetadata) FileMetadata(com.dropbox.core.v2.files.FileMetadata) Metadata(com.dropbox.core.v2.files.Metadata) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 23 with DbxClientV2

use of com.dropbox.core.v2.DbxClientV2 in project syndesis-qe by syndesisio.

the class DropBoxUtils method getClient.

private DbxClientV2 getClient() throws DbxException {
    if (this.client == null) {
        System.setProperty("https.protocols", "TLSv1.2");
        Account account = AccountsDirectory.getInstance().get(Account.Name.DROPBOX);
        DbxRequestConfig config = new DbxRequestConfig(account.getProperty("clientIdentifier"));
        this.client = new DbxClientV2(config, account.getProperty("accessToken"));
        log.debug("DropBox client created, logged as: " + client.users().getCurrentAccount());
    } else {
        log.debug("DropBox client was already created, returning existing instance");
    }
    return this.client;
}
Also used : Account(io.syndesis.qe.account.Account) DbxClientV2(com.dropbox.core.v2.DbxClientV2) DbxRequestConfig(com.dropbox.core.DbxRequestConfig)

Example 24 with DbxClientV2

use of com.dropbox.core.v2.DbxClientV2 in project dropbox-webhooks-spring-boot-example by zeldan.

the class DropboxService method logChangedFiles.

public void logChangedFiles(final String userId) throws Exception {
    final String accessToken = getTokenAndCheckIsTokenExists(userId);
    final DbxClientV2 client = new DbxClientV2(requestConfig, accessToken);
    final String cursor = userTokenRepository.getValue(CURSORS_HASH_KEY, userId);
    final ListFolderResult listFolderContinue = client.files().listFolderContinue(cursor);
    logChangedFilesOfUser(userId, listFolderContinue);
}
Also used : DbxClientV2(com.dropbox.core.v2.DbxClientV2) ListFolderResult(com.dropbox.core.v2.files.ListFolderResult)

Example 25 with DbxClientV2

use of com.dropbox.core.v2.DbxClientV2 in project dropbox-webhooks-spring-boot-example by zeldan.

the class DropboxService method finishAuthAndSaveUserDetails.

public void finishAuthAndSaveUserDetails(final HttpSession session, final Map<String, String[]> parameterMap) throws Exception {
    final DbxSessionStore csrfTokenStore = new DbxStandardSessionStore(session, dropboxConfigProp.getSessionStore().getKey());
    final DbxAuthFinish authFinish = auth.finishFromRedirect(dropboxConfigProp.getRedirectUri(), csrfTokenStore, parameterMap);
    final String accessToken = authFinish.getAccessToken();
    final DbxClientV2 client = new DbxClientV2(requestConfig, accessToken);
    final String userId = authFinish.getUserId();
    final ListFolderResult listFolderResult = client.files().listFolderBuilder("").withRecursive(true).start();
    saveAccessTokenAndActualCursor(userId, accessToken, listFolderResult);
}
Also used : DbxSessionStore(com.dropbox.core.DbxSessionStore) DbxClientV2(com.dropbox.core.v2.DbxClientV2) ListFolderResult(com.dropbox.core.v2.files.ListFolderResult) DbxAuthFinish(com.dropbox.core.DbxAuthFinish) DbxStandardSessionStore(com.dropbox.core.DbxStandardSessionStore)

Aggregations

DbxClientV2 (com.dropbox.core.v2.DbxClientV2)25 FileMetadata (com.dropbox.core.v2.files.FileMetadata)24 Test (org.testng.annotations.Test)19 DbxRequestConfig (com.dropbox.core.DbxRequestConfig)18 HttpRequestor (com.dropbox.core.http.HttpRequestor)15 Metadata (com.dropbox.core.v2.files.Metadata)15 DbxException (com.dropbox.core.DbxException)10 DbxCredential (com.dropbox.core.oauth.DbxCredential)10 ByteArrayInputStream (java.io.ByteArrayInputStream)9 JsonReader (com.dropbox.core.json.JsonReader)5 ListFolderResult (com.dropbox.core.v2.files.ListFolderResult)5 FullAccount (com.dropbox.core.v2.users.FullAccount)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 DbxAuthInfo (com.dropbox.core.DbxAuthInfo)4 DeletedMetadata (com.dropbox.core.v2.files.DeletedMetadata)4 FolderMetadata (com.dropbox.core.v2.files.FolderMetadata)4 IOException (java.io.IOException)4 Date (java.util.Date)4 NetworkIOException (com.dropbox.core.NetworkIOException)3 ProgressListener (com.dropbox.core.util.IOUtil.ProgressListener)3