Search in sources :

Example 1 with BoxException

use of com.box.androidsdk.content.BoxException in project box-android-sdk by box.

the class BoxHttpResponse method readStream.

private String readStream(InputStream inputStream) throws IOException, BoxException {
    if (inputStream == null) {
        return null;
    }
    // Wrap in gzip stream for gzip content encoding
    InputStream stream = mContentEncoding != null && mContentEncoding.equalsIgnoreCase("gzip") ? new GZIPInputStream(inputStream) : inputStream;
    StringBuilder builder = new StringBuilder();
    char[] buffer = new char[BUFFER_SIZE];
    try {
        InputStreamReader reader = new InputStreamReader(stream, "UTF-8");
        int read = reader.read(buffer, 0, BUFFER_SIZE);
        while (read != -1) {
            builder.append(buffer, 0, read);
            read = reader.read(buffer, 0, BUFFER_SIZE);
        }
        reader.close();
    } catch (IOException e) {
        throw new BoxException("Unable to read stream", e);
    }
    return builder.toString();
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) BoxException(com.box.androidsdk.content.BoxException) InputStreamReader(java.io.InputStreamReader) GZIPInputStream(java.util.zip.GZIPInputStream) ProgressInputStream(com.box.androidsdk.content.utils.ProgressInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException)

Example 2 with BoxException

use of com.box.androidsdk.content.BoxException in project box-android-sdk by box.

the class BoxHttpResponse method getStringBody.

/**
     * Returns a string representation of this response's body. This method is used when logging this response's body.
     * By default, it returns an empty string (to avoid accidentally logging binary data) unless the response contained
     * an error message.
     * @return a string representation of this response's body.
     * @throws BoxException thrown if there was an issue getting the body.
     */
public String getStringBody() throws BoxException {
    if (mBodyString != null) {
        return mBodyString;
    }
    InputStream stream = null;
    try {
        stream = isErrorCode(this.mResponseCode) ? mConnection.getErrorStream() : mConnection.getInputStream();
        mBodyString = readStream(stream);
    } catch (IOException e) {
        throw new BoxException("Unable to get string body", e);
    }
    return mBodyString;
}
Also used : BoxException(com.box.androidsdk.content.BoxException) GZIPInputStream(java.util.zip.GZIPInputStream) ProgressInputStream(com.box.androidsdk.content.utils.ProgressInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException)

Example 3 with BoxException

use of com.box.androidsdk.content.BoxException in project box-android-sdk by box.

the class BoxHttpResponse method disconnect.

/**
     * Disconnects this response from the server and frees up any network resources. The body of this response can no
     * longer be read after it has been disconnected.
     * @throws BoxException thrown if there was an issue closing the connection.
     */
public void disconnect() throws BoxException {
    try {
        if (this.rawInputStream == null) {
            this.rawInputStream = mConnection.getInputStream();
        }
        // We need to manually read from the raw input stream in case there are any remaining bytes. There's a bug
        // where a wrapping GZIPInputStream may not read to the end of a chunked response, causing Java to not
        // return the connection to the connection pool.
        byte[] buffer = new byte[BUFFER_SIZE];
        int n = this.rawInputStream.read(buffer);
        while (n != -1) {
            n = this.rawInputStream.read(buffer);
        }
        this.rawInputStream.close();
        if (this.mInputStream != null) {
            this.mInputStream.close();
        }
    } catch (IOException e) {
        throw new BoxException("Couldn't finish closing the connection to the Box API due to a network error or " + "because the stream was already closed.", e);
    }
}
Also used : BoxException(com.box.androidsdk.content.BoxException) IOException(java.io.IOException)

Example 4 with BoxException

use of com.box.androidsdk.content.BoxException in project box-android-sdk by box.

the class BoxRequest method handleSendException.

private T handleSendException(BoxRequestHandler requestHandler, BoxHttpResponse response, Exception ex) throws BoxException {
    if (ex instanceof BoxException) {
        if (requestHandler.onException(this, response, (BoxException) ex)) {
            return send();
        } else {
            throw (BoxException) ex;
        }
    } else {
        BoxException e = new BoxException("Couldn't connect to the Box API due to a network error.", ex);
        requestHandler.onException(this, response, e);
        throw e;
    }
}
Also used : BoxException(com.box.androidsdk.content.BoxException)

Example 5 with BoxException

use of com.box.androidsdk.content.BoxException in project box-android-sdk by box.

the class BoxRequest method onSend.

/**
     *
     * Synchronously make the request to Box and handle the response appropriately.
     * @return the expected BoxObject if the request is successful.
     * @throws BoxException thrown if there was a problem with handling the request.
     */
protected T onSend() throws BoxException {
    BoxRequest.BoxRequestHandler requestHandler = getRequestHandler();
    BoxHttpResponse response = null;
    HttpURLConnection connection = null;
    try {
        // Create the HTTP request and send it
        BoxHttpRequest request = createHttpRequest();
        connection = request.getUrlConnection();
        if (mRequiresSocket && connection instanceof HttpsURLConnection) {
            final SSLSocketFactory factory = ((HttpsURLConnection) connection).getSSLSocketFactory();
            SSLSocketFactoryWrapper wrappedFactory = new SSLSocketFactoryWrapper(factory);
            mSocketFactoryRef = new WeakReference<SSLSocketFactoryWrapper>(wrappedFactory);
            ((HttpsURLConnection) connection).setSSLSocketFactory(wrappedFactory);
        }
        if (mTimeout > 0) {
            connection.setConnectTimeout(mTimeout);
            connection.setReadTimeout(mTimeout);
        }
        response = sendRequest(request, connection);
        logDebug(response);
        // Process the response through the provided handler
        if (requestHandler.isResponseSuccess(response)) {
            T result = (T) requestHandler.onResponse(mClazz, response);
            return result;
        }
        throw new BoxException("An error occurred while sending the request", response);
    } catch (IOException e) {
        return handleSendException(requestHandler, response, e);
    } catch (InstantiationException e) {
        return handleSendException(requestHandler, response, e);
    } catch (IllegalAccessException e) {
        return handleSendException(requestHandler, response, e);
    } catch (BoxException e) {
        return handleSendException(requestHandler, response, e);
    } finally {
        if (connection != null) {
            connection.disconnect();
        }
    }
}
Also used : BoxException(com.box.androidsdk.content.BoxException) IOException(java.io.IOException) HttpURLConnection(java.net.HttpURLConnection) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Aggregations

BoxException (com.box.androidsdk.content.BoxException)14 IOException (java.io.IOException)8 BoxFutureTask (com.box.androidsdk.content.BoxFutureTask)4 ExecutionException (java.util.concurrent.ExecutionException)4 InputStream (java.io.InputStream)3 BoxError (com.box.androidsdk.content.models.BoxError)2 ProgressInputStream (com.box.androidsdk.content.utils.ProgressInputStream)2 WeakReference (java.lang.ref.WeakReference)2 FutureTask (java.util.concurrent.FutureTask)2 GZIPInputStream (java.util.zip.GZIPInputStream)2 BoxApiUser (com.box.androidsdk.content.BoxApiUser)1 AuthFailure (com.box.androidsdk.content.auth.OAuthWebView.AuthFailure)1 BoxDownload (com.box.androidsdk.content.models.BoxDownload)1 BoxEntity (com.box.androidsdk.content.models.BoxEntity)1 BoxFile (com.box.androidsdk.content.models.BoxFile)1 BoxIteratorRealTimeServers (com.box.androidsdk.content.models.BoxIteratorRealTimeServers)1 BoxObject (com.box.androidsdk.content.models.BoxObject)1 BoxSimpleMessage (com.box.androidsdk.content.models.BoxSimpleMessage)1 BoxUser (com.box.androidsdk.content.models.BoxUser)1 BoxRequestsEvent (com.box.androidsdk.content.requests.BoxRequestsEvent)1