Search in sources :

Example 1 with NetworkIOException

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

the class Main method longpoll.

/**
 * Will perform longpoll request for changes in the user's Dropbox
 * account and display those changes. This is more efficient that
 * periodic polling the endpoint.
 */
public static void longpoll(DbxAuthInfo auth, String path) throws IOException {
    long longpollTimeoutSecs = TimeUnit.MINUTES.toSeconds(2);
    // need 2 Dropbox clients for making calls:
    // 
    // (1) One for longpoll requests, with its read timeout set longer than our polling timeout
    // (2) One for all other requests, with its read timeout set to the default, shorter timeout
    // 
    StandardHttpRequestor.Config config = StandardHttpRequestor.Config.DEFAULT_INSTANCE;
    StandardHttpRequestor.Config longpollConfig = config.copy().withReadTimeout(5, TimeUnit.MINUTES).build();
    DbxClientV2 dbxClient = createClient(auth, config);
    DbxClientV2 dbxLongpollClient = createClient(auth, longpollConfig);
    try {
        // We only care about file changes, not existing files, so grab latest cursor for this
        // path and then longpoll for changes.
        String cursor = getLatestCursor(dbxClient, path);
        System.out.println("Longpolling for changes... press CTRL-C to exit.");
        while (true) {
            // will block for longpollTimeoutSecs or until a change is made in the folder
            ListFolderLongpollResult result = dbxLongpollClient.files().listFolderLongpoll(cursor, longpollTimeoutSecs);
            // we have changes, list them
            if (result.getChanges()) {
                cursor = printChanges(dbxClient, cursor);
            }
            // we were asked to back off from our polling, wait the requested amount of seconds
            // before issuing another longpoll request.
            Long backoff = result.getBackoff();
            if (backoff != null) {
                try {
                    System.out.printf("backing off for %d secs...\n", backoff.longValue());
                    Thread.sleep(TimeUnit.SECONDS.toMillis(backoff));
                } catch (InterruptedException ex) {
                    System.exit(0);
                }
            }
        }
    } catch (DbxApiException ex) {
        // if a user message is available, try using that instead
        String message = ex.getUserMessage() != null ? ex.getUserMessage().getText() : ex.getMessage();
        System.err.println("Error making API call: " + message);
        System.exit(1);
    } catch (NetworkIOException ex) {
        System.err.println("Error making API call: " + ex.getMessage());
        if (ex.getCause() instanceof SocketTimeoutException) {
            System.err.println("Consider increasing socket read timeout or decreasing longpoll timeout.");
        }
        System.exit(1);
    } catch (DbxException ex) {
        System.err.println("Error making API call: " + ex.getMessage());
        System.exit(1);
    }
}
Also used : StandardHttpRequestor(com.dropbox.core.http.StandardHttpRequestor) DbxClientV2(com.dropbox.core.v2.DbxClientV2) SocketTimeoutException(java.net.SocketTimeoutException) DbxApiException(com.dropbox.core.DbxApiException) ListFolderLongpollResult(com.dropbox.core.v2.files.ListFolderLongpollResult) DbxException(com.dropbox.core.DbxException) NetworkIOException(com.dropbox.core.NetworkIOException)

Example 2 with NetworkIOException

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

the class Main method chunkedUploadFile.

/**
 * Uploads a file in chunks using multiple requests. This approach is preferred for larger files
 * since it allows for more efficient processing of the file contents on the server side and
 * also allows partial uploads to be retried (e.g. network connection problem will not cause you
 * to re-upload all the bytes).
 *
 * @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 chunkedUploadFile(DbxClientV2 dbxClient, File localFile, String dropboxPath) {
    long size = localFile.length();
    // below to simplify the logic.
    if (size < CHUNKED_UPLOAD_CHUNK_SIZE) {
        System.err.println("File too small, use upload() instead.");
        System.exit(1);
        return;
    }
    long uploaded = 0L;
    DbxException thrown = null;
    ProgressListener progressListener = new ProgressListener() {

        long uploadedBytes = 0;

        @Override
        public void onProgress(long l) {
            printProgress(l + uploadedBytes, size);
            if (l == CHUNKED_UPLOAD_CHUNK_SIZE)
                uploadedBytes += CHUNKED_UPLOAD_CHUNK_SIZE;
        }
    };
    // Chunked uploads have 3 phases, each of which can accept uploaded bytes:
    // 
    // (1)  Start: initiate the upload and get an upload session ID
    // (2) Append: upload chunks of the file to append to our session
    // (3) Finish: commit the upload and close the session
    // 
    // We track how many bytes we uploaded to determine which phase we should be in.
    String sessionId = null;
    for (int i = 0; i < CHUNKED_UPLOAD_MAX_ATTEMPTS; ++i) {
        if (i > 0) {
            System.out.printf("Retrying chunked upload (%d / %d attempts)\n", i + 1, CHUNKED_UPLOAD_MAX_ATTEMPTS);
        }
        try (InputStream in = new FileInputStream(localFile)) {
            // if this is a retry, make sure seek to the correct offset
            in.skip(uploaded);
            // (1) Start
            if (sessionId == null) {
                sessionId = dbxClient.files().uploadSessionStart().uploadAndFinish(in, CHUNKED_UPLOAD_CHUNK_SIZE, progressListener).getSessionId();
                uploaded += CHUNKED_UPLOAD_CHUNK_SIZE;
                printProgress(uploaded, size);
            }
            UploadSessionCursor cursor = new UploadSessionCursor(sessionId, uploaded);
            // (2) Append
            while ((size - uploaded) > CHUNKED_UPLOAD_CHUNK_SIZE) {
                dbxClient.files().uploadSessionAppendV2(cursor).uploadAndFinish(in, CHUNKED_UPLOAD_CHUNK_SIZE, progressListener);
                uploaded += CHUNKED_UPLOAD_CHUNK_SIZE;
                printProgress(uploaded, size);
                cursor = new UploadSessionCursor(sessionId, uploaded);
            }
            // (3) Finish
            long remaining = size - uploaded;
            CommitInfo commitInfo = CommitInfo.newBuilder(dropboxPath).withMode(WriteMode.ADD).withClientModified(new Date(localFile.lastModified())).build();
            FileMetadata metadata = dbxClient.files().uploadSessionFinish(cursor, commitInfo).uploadAndFinish(in, remaining, progressListener);
            System.out.println(metadata.toStringMultiline());
            return;
        } catch (RetryException ex) {
            thrown = ex;
            // RetryExceptions are never automatically retried by the client for uploads. Must
            // catch this exception even if DbxRequestConfig.getMaxRetries() > 0.
            sleepQuietly(ex.getBackoffMillis());
            continue;
        } catch (NetworkIOException ex) {
            thrown = ex;
            // network issue with Dropbox (maybe a timeout?) try again
            continue;
        } catch (UploadSessionLookupErrorException ex) {
            if (ex.errorValue.isIncorrectOffset()) {
                thrown = ex;
                // server offset into the stream doesn't match our offset (uploaded). Seek to
                // the expected offset according to the server and try again.
                uploaded = ex.errorValue.getIncorrectOffsetValue().getCorrectOffset();
                continue;
            } else {
                // Some other error occurred, give up.
                System.err.println("Error uploading to Dropbox: " + ex.getMessage());
                System.exit(1);
                return;
            }
        } catch (UploadSessionFinishErrorException ex) {
            if (ex.errorValue.isLookupFailed() && ex.errorValue.getLookupFailedValue().isIncorrectOffset()) {
                thrown = ex;
                // server offset into the stream doesn't match our offset (uploaded). Seek to
                // the expected offset according to the server and try again.
                uploaded = ex.errorValue.getLookupFailedValue().getIncorrectOffsetValue().getCorrectOffset();
                continue;
            } else {
                // some other error occurred, give up.
                System.err.println("Error uploading to Dropbox: " + ex.getMessage());
                System.exit(1);
                return;
            }
        } catch (DbxException ex) {
            System.err.println("Error uploading to Dropbox: " + ex.getMessage());
            System.exit(1);
            return;
        } catch (IOException ex) {
            System.err.println("Error reading from file \"" + localFile + "\": " + ex.getMessage());
            System.exit(1);
            return;
        }
    }
    // if we made it here, then we must have run out of attempts
    System.err.println("Maxed out upload attempts to Dropbox. Most recent error: " + thrown.getMessage());
    System.exit(1);
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileMetadata(com.dropbox.core.v2.files.FileMetadata) NetworkIOException(com.dropbox.core.NetworkIOException) IOException(java.io.IOException) RetryException(com.dropbox.core.RetryException) FileInputStream(java.io.FileInputStream) Date(java.util.Date) NetworkIOException(com.dropbox.core.NetworkIOException) UploadSessionLookupErrorException(com.dropbox.core.v2.files.UploadSessionLookupErrorException) ProgressListener(com.dropbox.core.util.IOUtil.ProgressListener) UploadSessionFinishErrorException(com.dropbox.core.v2.files.UploadSessionFinishErrorException) UploadSessionCursor(com.dropbox.core.v2.files.UploadSessionCursor) CommitInfo(com.dropbox.core.v2.files.CommitInfo) DbxException(com.dropbox.core.DbxException)

Example 3 with NetworkIOException

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

the class DbxRawClientV2 method uploadStyle.

public <ArgT> HttpRequestor.Uploader uploadStyle(String host, String path, ArgT arg, boolean noAuth, StoneSerializer<ArgT> argSerializer) throws DbxException {
    String uri = DbxRequestUtil.buildUri(host, path);
    List<HttpRequestor.Header> headers = new ArrayList<HttpRequestor.Header>();
    if (!noAuth) {
        refreshAccessTokenIfNeeded();
        addAuthHeaders(headers);
    }
    addUserLocaleHeader(headers, requestConfig);
    addPathRootHeader(headers, this.pathRoot);
    headers.add(new HttpRequestor.Header("Content-Type", "application/octet-stream"));
    headers = DbxRequestUtil.addUserAgentHeader(headers, requestConfig, USER_AGENT_ID);
    headers.add(new HttpRequestor.Header("Dropbox-API-Arg", headerSafeJson(argSerializer, arg)));
    try {
        return requestConfig.getHttpRequestor().startPostInStreamingMode(uri, headers);
    } catch (IOException ex) {
        throw new NetworkIOException(ex);
    }
}
Also used : HttpRequestor(com.dropbox.core.http.HttpRequestor) DbxRequestUtil.addPathRootHeader(com.dropbox.core.DbxRequestUtil.addPathRootHeader) DbxRequestUtil.addUserLocaleHeader(com.dropbox.core.DbxRequestUtil.addUserLocaleHeader) ArrayList(java.util.ArrayList) NetworkIOException(com.dropbox.core.NetworkIOException) IOException(java.io.IOException) NetworkIOException(com.dropbox.core.NetworkIOException)

Example 4 with NetworkIOException

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

the class DbxRawClientV2 method rpcStyle.

public <ArgT, ResT, ErrT> ResT rpcStyle(final String host, final String path, final ArgT arg, final boolean noAuth, final StoneSerializer<ArgT> argSerializer, final StoneSerializer<ResT> responseSerializer, final StoneSerializer<ErrT> errorSerializer) throws DbxWrappedException, DbxException {
    final byte[] body = writeAsBytes(argSerializer, arg);
    final List<HttpRequestor.Header> headers = new ArrayList<HttpRequestor.Header>();
    if (!noAuth) {
        refreshAccessTokenIfNeeded();
    }
    if (!this.host.getNotify().equals(host)) {
        // TODO(krieb): fix this ugliness
        addUserLocaleHeader(headers, requestConfig);
        addPathRootHeader(headers, this.pathRoot);
    }
    headers.add(new HttpRequestor.Header("Content-Type", "application/json; charset=utf-8"));
    return executeRetriableWithRefresh(requestConfig.getMaxRetries(), new RetriableExecution<ResT>() {

        private String userIdAnon;

        @Override
        public ResT execute() throws DbxWrappedException, DbxException {
            if (!noAuth) {
                addAuthHeaders(headers);
            }
            HttpRequestor.Response response = DbxRequestUtil.startPostRaw(requestConfig, USER_AGENT_ID, host, path, body, headers);
            try {
                switch(response.getStatusCode()) {
                    case 200:
                        return responseSerializer.deserialize(response.getBody());
                    case 409:
                        throw DbxWrappedException.fromResponse(errorSerializer, response, userIdAnon);
                    default:
                        throw DbxRequestUtil.unexpectedStatus(response, userIdAnon);
                }
            } catch (JsonProcessingException ex) {
                String requestId = DbxRequestUtil.getRequestId(response);
                throw new BadResponseException(requestId, "Bad JSON: " + ex.getMessage(), ex);
            } catch (IOException ex) {
                throw new NetworkIOException(ex);
            }
        }

        private RetriableExecution<ResT> init(String userId) {
            this.userIdAnon = userId;
            return this;
        }
    }.init(this.userId));
}
Also used : HttpRequestor(com.dropbox.core.http.HttpRequestor) BadResponseException(com.dropbox.core.BadResponseException) ArrayList(java.util.ArrayList) NetworkIOException(com.dropbox.core.NetworkIOException) IOException(java.io.IOException) NetworkIOException(com.dropbox.core.NetworkIOException) DbxRequestUtil.addPathRootHeader(com.dropbox.core.DbxRequestUtil.addPathRootHeader) DbxRequestUtil.addUserLocaleHeader(com.dropbox.core.DbxRequestUtil.addUserLocaleHeader) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 5 with NetworkIOException

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

the class DbxRawClientV2 method downloadStyle.

public <ArgT, ResT, ErrT> DbxDownloader<ResT> downloadStyle(final String host, final String path, final ArgT arg, final boolean noAuth, final List<HttpRequestor.Header> extraHeaders, final StoneSerializer<ArgT> argSerializer, final StoneSerializer<ResT> responseSerializer, final StoneSerializer<ErrT> errorSerializer) throws DbxWrappedException, DbxException {
    final List<HttpRequestor.Header> headers = new ArrayList<HttpRequestor.Header>(extraHeaders);
    if (!noAuth) {
        refreshAccessTokenIfNeeded();
    }
    addUserLocaleHeader(headers, requestConfig);
    addPathRootHeader(headers, this.pathRoot);
    headers.add(new HttpRequestor.Header("Dropbox-API-Arg", headerSafeJson(argSerializer, arg)));
    headers.add(new HttpRequestor.Header("Content-Type", ""));
    final byte[] body = new byte[0];
    return executeRetriableWithRefresh(requestConfig.getMaxRetries(), new RetriableExecution<DbxDownloader<ResT>>() {

        private String userIdAnon;

        @Override
        public DbxDownloader<ResT> execute() throws DbxWrappedException, DbxException {
            if (!noAuth) {
                addAuthHeaders(headers);
            }
            HttpRequestor.Response response = DbxRequestUtil.startPostRaw(requestConfig, USER_AGENT_ID, host, path, body, headers);
            String requestId = DbxRequestUtil.getRequestId(response);
            String contentType = DbxRequestUtil.getContentType(response);
            try {
                switch(response.getStatusCode()) {
                    case 200:
                    // fall-through
                    case 206:
                        List<String> resultHeaders = response.getHeaders().get("dropbox-api-result");
                        if (resultHeaders == null) {
                            throw new BadResponseException(requestId, "Missing Dropbox-API-Result header; " + response.getHeaders());
                        }
                        if (resultHeaders.size() == 0) {
                            throw new BadResponseException(requestId, "No Dropbox-API-Result header; " + response.getHeaders());
                        }
                        String resultHeader = resultHeaders.get(0);
                        if (resultHeader == null) {
                            throw new BadResponseException(requestId, "Null Dropbox-API-Result header; " + response.getHeaders());
                        }
                        ResT result = responseSerializer.deserialize(resultHeader);
                        return new DbxDownloader<ResT>(result, response.getBody(), contentType);
                    case 409:
                        throw DbxWrappedException.fromResponse(errorSerializer, response, userIdAnon);
                    default:
                        throw DbxRequestUtil.unexpectedStatus(response, userIdAnon);
                }
            } catch (JsonProcessingException ex) {
                throw new BadResponseException(requestId, "Bad JSON: " + ex.getMessage(), ex);
            } catch (IOException ex) {
                throw new NetworkIOException(ex);
            }
        }

        private RetriableExecution<DbxDownloader<ResT>> init(String userId) {
            this.userIdAnon = userId;
            return this;
        }
    }.init(this.userId));
}
Also used : HttpRequestor(com.dropbox.core.http.HttpRequestor) BadResponseException(com.dropbox.core.BadResponseException) ArrayList(java.util.ArrayList) NetworkIOException(com.dropbox.core.NetworkIOException) IOException(java.io.IOException) NetworkIOException(com.dropbox.core.NetworkIOException) DbxDownloader(com.dropbox.core.DbxDownloader) DbxRequestUtil.addPathRootHeader(com.dropbox.core.DbxRequestUtil.addPathRootHeader) DbxRequestUtil.addUserLocaleHeader(com.dropbox.core.DbxRequestUtil.addUserLocaleHeader) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

NetworkIOException (com.dropbox.core.NetworkIOException)5 IOException (java.io.IOException)4 DbxRequestUtil.addPathRootHeader (com.dropbox.core.DbxRequestUtil.addPathRootHeader)3 DbxRequestUtil.addUserLocaleHeader (com.dropbox.core.DbxRequestUtil.addUserLocaleHeader)3 HttpRequestor (com.dropbox.core.http.HttpRequestor)3 ArrayList (java.util.ArrayList)3 BadResponseException (com.dropbox.core.BadResponseException)2 DbxException (com.dropbox.core.DbxException)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 DbxApiException (com.dropbox.core.DbxApiException)1 DbxDownloader (com.dropbox.core.DbxDownloader)1 RetryException (com.dropbox.core.RetryException)1 StandardHttpRequestor (com.dropbox.core.http.StandardHttpRequestor)1 ProgressListener (com.dropbox.core.util.IOUtil.ProgressListener)1 DbxClientV2 (com.dropbox.core.v2.DbxClientV2)1 CommitInfo (com.dropbox.core.v2.files.CommitInfo)1 FileMetadata (com.dropbox.core.v2.files.FileMetadata)1 ListFolderLongpollResult (com.dropbox.core.v2.files.ListFolderLongpollResult)1 UploadSessionCursor (com.dropbox.core.v2.files.UploadSessionCursor)1 UploadSessionFinishErrorException (com.dropbox.core.v2.files.UploadSessionFinishErrorException)1