Search in sources :

Example 1 with FileMetadata

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

the class DbxClientV2Test method testRetrySuccess.

@Test
public void testRetrySuccess() throws DbxException, IOException {
    HttpRequestor mockRequestor = mock(HttpRequestor.class);
    DbxRequestConfig config = createRequestConfig().withAutoRetryEnabled(3).withHttpRequestor(mockRequestor).build();
    DbxClientV2 client = new DbxClientV2(config, "fakeAccessToken");
    FileMetadata expected = new FileMetadata("bar.txt", "id:1HkLjqifwMAAAAAAAAAAAQ", new Date(1456169040985L), new Date(1456169040985L), "2e0c38735597", 2091603);
    // 503 twice, then return result
    HttpRequestor.Uploader mockUploader = mockUploader();
    when(mockUploader.finish()).thenReturn(createEmptyResponse(503)).thenReturn(createEmptyResponse(503)).thenReturn(createSuccessResponse(serialize(expected)));
    when(mockRequestor.startPost(anyString(), anyHeaders())).thenReturn(mockUploader);
    Metadata actual = client.files().getMetadata(expected.getId());
    // should have only been called 3 times: initial call + 2 retries
    verify(mockRequestor, times(3)).startPost(anyString(), anyHeaders());
    assertEquals(actual.getName(), expected.getName());
    assertTrue(actual instanceof FileMetadata, actual.getClass().toString());
    assertEquals(((FileMetadata) actual).getId(), expected.getId());
}
Also used : HttpRequestor(com.dropbox.core.http.HttpRequestor) DbxRequestConfig(com.dropbox.core.DbxRequestConfig) FileMetadata(com.dropbox.core.v2.files.FileMetadata) FileMetadata(com.dropbox.core.v2.files.FileMetadata) Metadata(com.dropbox.core.v2.files.Metadata) Date(java.util.Date) Test(org.testng.annotations.Test)

Example 2 with FileMetadata

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

the class DbxClientV2Test method testRetryDownload.

@Test
public void testRetryDownload() throws DbxException, IOException {
    HttpRequestor mockRequestor = mock(HttpRequestor.class);
    DbxRequestConfig config = createRequestConfig().withAutoRetryEnabled(3).withHttpRequestor(mockRequestor).build();
    DbxClientV2 client = new DbxClientV2(config, "fakeAccessToken");
    FileMetadata expectedMetadata = new FileMetadata("download_me.txt", "id:KLavC4viCDAAAAAAAAAAAQ", new Date(1456169692501L), new Date(1456169692501L), "341438735597", 2626);
    byte[] expectedBytes = new byte[] { 1, 2, 3, 4 };
    // 503 once, then return 200
    HttpRequestor.Uploader mockUploader = mockUploader();
    when(mockUploader.finish()).thenReturn(createEmptyResponse(503)).thenReturn(createDownloaderResponse(expectedBytes, serialize(expectedMetadata)));
    when(mockRequestor.startPost(anyString(), anyHeaders())).thenReturn(mockUploader);
    DbxDownloader<FileMetadata> downloader = client.files().download(expectedMetadata.getId());
    // should have been attempted twice
    verify(mockRequestor, times(2)).startPost(anyString(), anyHeaders());
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    FileMetadata actualMetadata = downloader.download(bout);
    byte[] actualBytes = bout.toByteArray();
    assertEquals(actualBytes, expectedBytes);
    assertEquals(actualMetadata, expectedMetadata);
}
Also used : HttpRequestor(com.dropbox.core.http.HttpRequestor) DbxRequestConfig(com.dropbox.core.DbxRequestConfig) FileMetadata(com.dropbox.core.v2.files.FileMetadata) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Date(java.util.Date) Test(org.testng.annotations.Test)

Example 3 with FileMetadata

use of com.dropbox.core.v2.files.FileMetadata 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));
    assertEquals(metadata.getSize(), 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 4 with FileMetadata

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

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

the class Main method uploadFile.

/**
     * Uploads a file in a single request. This approach is preferred for small files since it
     * eliminates unnecessary round-trips to the servers.
     *
     * @param dbxClient Dropbox user authenticated client
     * @param localFIle local file to upload
     * @param dropboxPath Where to upload the file to within Dropbox
     */
private static void uploadFile(DbxClientV2 dbxClient, File localFile, String dropboxPath) {
    try (InputStream in = new FileInputStream(localFile)) {
        FileMetadata metadata = dbxClient.files().uploadBuilder(dropboxPath).withMode(WriteMode.ADD).withClientModified(new Date(localFile.lastModified())).uploadAndFinish(in);
        System.out.println(metadata.toStringMultiline());
    } catch (UploadErrorException ex) {
        System.err.println("Error uploading to Dropbox: " + ex.getMessage());
        System.exit(1);
    } catch (DbxException ex) {
        System.err.println("Error uploading to Dropbox: " + ex.getMessage());
        System.exit(1);
    } catch (IOException ex) {
        System.err.println("Error reading from file \"" + localFile + "\": " + ex.getMessage());
        System.exit(1);
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) UploadErrorException(com.dropbox.core.v2.files.UploadErrorException) FileMetadata(com.dropbox.core.v2.files.FileMetadata) NetworkIOException(com.dropbox.core.NetworkIOException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Date(java.util.Date) DbxException(com.dropbox.core.DbxException)

Aggregations

FileMetadata (com.dropbox.core.v2.files.FileMetadata)16 Metadata (com.dropbox.core.v2.files.Metadata)7 Date (java.util.Date)6 DbxException (com.dropbox.core.DbxException)5 Test (org.testng.annotations.Test)5 DbxRequestConfig (com.dropbox.core.DbxRequestConfig)4 FolderMetadata (com.dropbox.core.v2.files.FolderMetadata)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 IOException (java.io.IOException)4 HttpRequestor (com.dropbox.core.http.HttpRequestor)3 DbxClientV2 (com.dropbox.core.v2.DbxClientV2)3 DeletedMetadata (com.dropbox.core.v2.files.DeletedMetadata)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 FileInputStream (java.io.FileInputStream)3 InputStream (java.io.InputStream)3 NetworkIOException (com.dropbox.core.NetworkIOException)2 ListFolderResult (com.dropbox.core.v2.files.ListFolderResult)2 ProgressDialog (android.app.ProgressDialog)1 Intent (android.content.Intent)1 FloatingActionButton (android.support.design.widget.FloatingActionButton)1