com.dropbox.core.DbxDownloader Java Examples

The following examples show how to use com.dropbox.core.DbxDownloader. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DropboxReadFeature.java    From cyberduck with GNU General Public License v3.0 5 votes vote down vote up
@Override
public InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException {
    try {
        final DownloadBuilder builder = new DbxUserFilesRequests(session.getClient(file)).downloadBuilder(containerService.getKey(file));
        if(status.isAppend()) {
            final HttpRange range = HttpRange.withStatus(status);
            builder.range(range.getStart());
        }
        final DbxDownloader<FileMetadata> downloader = builder.start();
        return downloader.getInputStream();
    }
    catch(DbxException e) {
        throw new DropboxExceptionMappingService().map("Download {0} failed", e, file);
    }
}
 
Example #2
Source File: FileThumbnailRequestHandler.java    From dropbox-sdk-java with MIT License 5 votes vote down vote up
@Override
public Result load(Request request, int networkPolicy) throws IOException {

    try {
        DbxDownloader<FileMetadata> downloader =
                mDbxClient.files().getThumbnailBuilder(request.uri.getPath())
                        .withFormat(ThumbnailFormat.JPEG)
                        .withSize(ThumbnailSize.W1024H768)
                        .start();

        return new Result(Okio.source(downloader.getInputStream()), Picasso.LoadedFrom.NETWORK);
    } catch (DbxException e) {
        throw new IOException(e);
    }
}
 
Example #3
Source File: DbxClientV2Test.java    From dropbox-sdk-java with MIT License 5 votes vote down vote up
@Test
public void testRetryDownload() throws Exception {
    HttpRequestor mockRequestor = mock(HttpRequestor.class);
    DbxRequestConfig config = createRequestConfig()
        .withAutoRetryEnabled(3)
        .withHttpRequestor(mockRequestor)
        .build();

    DbxClientV2 client = new DbxClientV2(config, "fakeAccessToken");
    FileMetadata expectedMetadata = constructFileMetadate();
    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);
}
 
Example #4
Source File: DbxClientV2Test.java    From dropbox-sdk-java with MIT License 4 votes vote down vote up
@Test
public void testRefreshAndRetryAfterTokenExpiredForDownload() throws Exception {
    HttpRequestor mockRequestor = mock(HttpRequestor.class);
    DbxRequestConfig config = createRequestConfig()
        .withHttpRequestor(mockRequestor)
        .build();

    long now = System.currentTimeMillis();

    DbxCredential credential = new DbxCredential("accesstoken", now + 2*DbxCredential
        .EXPIRE_MARGIN,
        "refresh_token",
        "appkey", "app_secret");

    DbxClientV2 client = new DbxClientV2(config, credential);
    FileMetadata expected = constructFileMetadate();

    HttpRequestor.Uploader mockUploader = mockUploader();
    when(mockUploader.finish())
        .thenReturn(createTokenExpiredResponse())
        .thenReturn(createSuccessRefreshResponse("new_token", 14400L))
        .thenReturn(createDownloadSuccessResponse("data".getBytes(), new String(serialize(expected))));

    when(mockRequestor.startPost(anyString(), anyHeaders()))
        .thenReturn(mockUploader);

    ByteArrayOutputStream out = new ByteArrayOutputStream();

    DbxDownloader<FileMetadata> downloader = client.files().download("/path");

    try {
        downloader.download(out);
    } finally {
        downloader.close();
    }

    assertEquals(out.toString(), "data");

    Metadata actual = downloader.getResult();

    verify(mockRequestor, times(3)).startPost(anyString(), anyHeaders());
    assertEquals(credential.getAccessToken(), "new_token");

    assertEquals(actual.getName(), expected.getName());
    assertEquals(((FileMetadata) actual).getId(), expected.getId());
}
 
Example #5
Source File: DbxClientV2IT.java    From dropbox-sdk-java with MIT License 3 votes vote down vote up
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);

    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);

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

    assertEquals(actualResult, metadata);
    assertEquals(actualContents, contents);
    assertEquals(downloader.getContentType(), "application/octet-stream");

    Metadata deleted = client.files().delete(path);
    assertEquals(deleted, metadata);
}
 
Example #6
Source File: DbxDownloadStyleBuilder.java    From dropbox-sdk-java with MIT License 2 votes vote down vote up
/**
 * Issues the download request using this builder's request parameters and returns a {@link
 * DbxDownloader} for reading the response body.
 *
 * Callers should fully read the response body using {@link DbxDownloader#getInputStream} and
 * close the downloader afterwards (see {@link DbxDownloader#close}). This will allow for proper
 * resource deallocation and connection re-use..
 *
 * See {@link #download} convenience method for a simpler way to complete the request.
 *
 * @return {@link DbxDownloader} used to download data and read the response.
 *
 * @throws DbxException if an error occursing issuing the request
 */
public abstract DbxDownloader<R> start() throws DbxException;