Search in sources :

Example 1 with BadResponseException

use of com.dropbox.core.BadResponseException 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) {
        addAuthHeaders(headers);
    }
    if (!this.host.getNotify().equals(host)) {
        // TODO(krieb): fix this ugliness
        addUserLocaleHeader(headers, requestConfig);
    }
    headers.add(new HttpRequestor.Header("Content-Type", "application/json; charset=utf-8"));
    return executeRetriable(requestConfig.getMaxRetries(), new RetriableExecution<ResT>() {

        private String userIdAnon;

        @Override
        public ResT execute() throws DbxWrappedException, DbxException {
            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.addUserLocaleHeader(com.dropbox.core.DbxRequestUtil.addUserLocaleHeader) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 2 with BadResponseException

use of com.dropbox.core.BadResponseException 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)2 DbxRequestUtil.addUserLocaleHeader (com.dropbox.core.DbxRequestUtil.addUserLocaleHeader)2 NetworkIOException (com.dropbox.core.NetworkIOException)2 HttpRequestor (com.dropbox.core.http.HttpRequestor)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 DbxDownloader (com.dropbox.core.DbxDownloader)1