Search in sources :

Example 1 with Metadata

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

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

the class DbxClientV2Test method serialize.

private static byte[] serialize(Metadata metadata) {
    assertNotNull(metadata);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        serializer(Metadata.class).serialize(metadata, out);
    } catch (Exception ex) {
        fail("unserializable type: " + metadata.getClass(), ex);
        return null;
    }
    return out.toByteArray();
}
Also used : FileMetadata(com.dropbox.core.v2.files.FileMetadata) Metadata(com.dropbox.core.v2.files.Metadata) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RetryException(com.dropbox.core.RetryException) BadRequestException(com.dropbox.core.BadRequestException) IOException(java.io.IOException) DbxException(com.dropbox.core.DbxException)

Example 3 with Metadata

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

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

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

the class Main method main.

public static void main(String[] args) throws DbxException, IOException {
    // Create Dropbox client
    DbxRequestConfig config = new DbxRequestConfig("dropbox/java-tutorial");
    DbxClientV2 client = new DbxClientV2(config, ACCESS_TOKEN);
    // Instantiate factory and set shared factory
    DbxRequestUtil.sharedCallbackFactory = new DbxExampleGlobalCallbackFactory();
    try {
        // Get files and folder metadata from Dropbox root directory
        client.files().listFolder("/does/not/exist/folder/");
    } catch (ListFolderErrorException ex) {
        System.err.println("STANDARD ROUTE ERROR HANDLER: " + ex.errorValue + "\n");
    } catch (DbxException ex) {
        System.err.println("STANDARD NETWORK ERROR HANDLER: " + ex + "\n");
    }
    try {
        // Get files and folder metadata from Dropbox root directory
        client.auth().tokenRevoke();
        client.files().listFolder("/does/not/exist");
    } catch (ListFolderErrorException ex) {
        System.err.println("STANDARD ROUTE ERROR HANDLER2: " + ex.errorValue + "\n");
    } catch (DbxException ex) {
        System.err.println("STANDARD NETWORK ERROR HANDLER2: " + ex + "\n");
    }
}
Also used : DbxClientV2(com.dropbox.core.v2.DbxClientV2) DbxRequestConfig(com.dropbox.core.DbxRequestConfig) ListFolderErrorException(com.dropbox.core.v2.files.ListFolderErrorException) DbxException(com.dropbox.core.DbxException)

Aggregations

FileMetadata (com.dropbox.core.v2.files.FileMetadata)14 Metadata (com.dropbox.core.v2.files.Metadata)9 DbxException (com.dropbox.core.DbxException)8 IOException (java.io.IOException)5 DbxRequestConfig (com.dropbox.core.DbxRequestConfig)4 DbxClientV2 (com.dropbox.core.v2.DbxClientV2)4 DeletedMetadata (com.dropbox.core.v2.files.DeletedMetadata)4 FolderMetadata (com.dropbox.core.v2.files.FolderMetadata)4 Date (java.util.Date)4 ListFolderResult (com.dropbox.core.v2.files.ListFolderResult)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 FileInputStream (java.io.FileInputStream)3 InputStream (java.io.InputStream)3 Test (org.testng.annotations.Test)3 NetworkIOException (com.dropbox.core.NetworkIOException)2 RetryException (com.dropbox.core.RetryException)2 HttpRequestor (com.dropbox.core.http.HttpRequestor)2 ListFolderErrorException (com.dropbox.core.v2.files.ListFolderErrorException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 PrintWriter (java.io.PrintWriter)2