Search in sources :

Example 1 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) throws Exception {
    byte[] contents = ITUtil.randomBytes(1024);
    String filename = "testUploadAndDownload.dat";
    String path = ITUtil.path(getClass(), "/" + filename);
    FileMetadata metadata = client.files().uploadBuilder(path).withAutorename(false).withMode(WriteMode.ADD).withMute(true).uploadAndFinish(new ByteArrayInputStream(contents));
    assertEquals(metadata.getName(), filename);
    assertEquals(metadata.getPathLower(), path.toLowerCase());
    assertEquals(metadata.getSize(), contents.length);
    Metadata actual = client.files().getMetadata(path);
    assertTrue(actual instanceof FileMetadata, actual.getClass().getCanonicalName());
    assertEquals(actual, metadata);
    DbxDownloader<FileMetadata> downloader = client.files().download(path);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    downloader.download(out);
    byte[] actualContents = out.toByteArray();
    FileMetadata actualResult = downloader.getResult();
    assertEquals(actualResult, metadata);
    assertEquals(actualContents, contents);
    Metadata deleted = client.files().delete(path);
    assertEquals(deleted, metadata);
}
Also used : 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 2 with DbxClientV2

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

the class DropboxClientFactory method init.

public static void init(String accessToken) {
    if (sDbxClient == null) {
        DbxRequestConfig requestConfig = DbxRequestConfig.newBuilder("examples-v2-demo").withHttpRequestor(new OkHttp3Requestor(OkHttp3Requestor.defaultOkHttpClient())).build();
        sDbxClient = new DbxClientV2(requestConfig, accessToken);
    }
}
Also used : DbxClientV2(com.dropbox.core.v2.DbxClientV2) DbxRequestConfig(com.dropbox.core.DbxRequestConfig) OkHttp3Requestor(com.dropbox.core.http.OkHttp3Requestor)

Example 3 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) {
        Optional<Account> optional = AccountsDirectory.getInstance().getAccount("QE Dropbox");
        if (optional.isPresent()) {
            DbxRequestConfig config = new DbxRequestConfig(optional.get().getProperty("clientIdentifier"));
            this.client = new DbxClientV2(config, optional.get().getProperty("accessToken"));
        } else {
            log.error("Unable to create DropBox client - credentials not found.");
            throw new IllegalArgumentException("DropBox credentials were not found.");
        }
        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.accounts.Account) DbxClientV2(com.dropbox.core.v2.DbxClientV2) DbxRequestConfig(com.dropbox.core.DbxRequestConfig)

Example 4 with DbxClientV2

use of com.dropbox.core.v2.DbxClientV2 in project drill by apache.

the class DropboxFileSystem method getClient.

private DbxClientV2 getClient() {
    if (this.client != null) {
        return client;
    }
    // read preferred client identifier from config or use "Apache/Drill"
    String clientIdentifier = this.getConf().get("clientIdentifier", "Apache/Drill");
    logger.info("Creating dropbox client with client identifier: {}", clientIdentifier);
    DbxRequestConfig config = DbxRequestConfig.newBuilder(clientIdentifier).build();
    // read access token from config or credentials provider
    logger.info("Reading dropbox access token from configuration or credentials provider");
    String accessToken = this.getConf().get("dropboxAccessToken", "");
    this.client = new DbxClientV2(config, accessToken);
    return this.client;
}
Also used : DbxClientV2(com.dropbox.core.v2.DbxClientV2) DbxRequestConfig(com.dropbox.core.DbxRequestConfig)

Example 5 with DbxClientV2

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

the class Main method createClient.

/**
 * Create a new Dropbox client using the given authentication
 * information and HTTP client config.
 *
 * @param auth Authentication information
 * @param config HTTP request configuration
 *
 * @return new Dropbox V2 client
 */
private static DbxClientV2 createClient(DbxAuthInfo auth, StandardHttpRequestor.Config config) {
    String clientUserAgentId = "examples-longpoll";
    StandardHttpRequestor requestor = new StandardHttpRequestor(config);
    DbxRequestConfig requestConfig = DbxRequestConfig.newBuilder(clientUserAgentId).withHttpRequestor(requestor).build();
    return new DbxClientV2(requestConfig, auth.getAccessToken(), auth.getHost());
}
Also used : StandardHttpRequestor(com.dropbox.core.http.StandardHttpRequestor) DbxClientV2(com.dropbox.core.v2.DbxClientV2) DbxRequestConfig(com.dropbox.core.DbxRequestConfig)

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