Search in sources :

Example 1 with DbxDownloader

use of com.dropbox.core.DbxDownloader 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) {
        addAuthHeaders(headers);
    }
    addUserLocaleHeader(headers, requestConfig);
    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 executeRetriable(requestConfig.getMaxRetries(), new RetriableExecution<DbxDownloader<ResT>>() {

        private String userIdAnon;

        @Override
        public DbxDownloader<ResT> execute() throws DbxWrappedException, DbxException {
            HttpRequestor.Response response = DbxRequestUtil.startPostRaw(requestConfig, USER_AGENT_ID, host, path, body, headers);
            String requestId = DbxRequestUtil.getRequestId(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());
                    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.addUserLocaleHeader(com.dropbox.core.DbxRequestUtil.addUserLocaleHeader) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

BadResponseException (com.dropbox.core.BadResponseException)1 DbxDownloader (com.dropbox.core.DbxDownloader)1 DbxRequestUtil.addUserLocaleHeader (com.dropbox.core.DbxRequestUtil.addUserLocaleHeader)1 NetworkIOException (com.dropbox.core.NetworkIOException)1 HttpRequestor (com.dropbox.core.http.HttpRequestor)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1