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