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());
}
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);
}
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);
}
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);
}
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);
}
}
Aggregations