Search in sources :

Example 11 with BoxException

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

the class MainActivity method uploadSampleFile.

/**
     * Method demonstrates a sample file being uploaded using the file api
     */
private void uploadSampleFile() {
    mDialog = ProgressDialog.show(MainActivity.this, getText(R.string.boxsdk_Please_wait), getText(R.string.boxsdk_Please_wait));
    new Thread() {

        @Override
        public void run() {
            try {
                String uploadFileName = "box_logo.png";
                InputStream uploadStream = getResources().getAssets().open(uploadFileName);
                String destinationFolderId = "0";
                String uploadName = "BoxSDKUpload.png";
                BoxRequestsFile.UploadFile request = mFileApi.getUploadRequest(uploadStream, uploadName, destinationFolderId);
                final BoxFile uploadFileInfo = request.send();
                showToast("Uploaded " + uploadFileInfo.getName());
                loadRootFolder();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (BoxException e) {
                e.printStackTrace();
                BoxError error = e.getAsBoxError();
                if (error != null && error.getStatus() == HttpStatus.SC_CONFLICT) {
                    ArrayList<BoxEntity> conflicts = error.getContextInfo().getConflicts();
                    if (conflicts != null && conflicts.size() == 1 && conflicts.get(0) instanceof BoxFile) {
                        uploadNewVersion((BoxFile) conflicts.get(0));
                        return;
                    }
                }
                showToast("Upload failed");
            } finally {
                mDialog.dismiss();
            }
        }
    }.start();
}
Also used : BoxException(com.box.androidsdk.content.BoxException) BoxError(com.box.androidsdk.content.models.BoxError) InputStream(java.io.InputStream) BoxFile(com.box.androidsdk.content.models.BoxFile) IOException(java.io.IOException) BoxEntity(com.box.androidsdk.content.models.BoxEntity)

Example 12 with BoxException

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

the class OAuthActivity method getAuthFailure.

/**
     * Takes an auth exception and converts it to an AuthFailure so it can be properly handled
     *
     * @param e The auth exception
     * @return The typed AuthFailure
     */
private OAuthWebView.AuthFailure getAuthFailure(Exception e) {
    String error = getString(R.string.boxsdk_Authentication_fail);
    if (e != null) {
        // Get the proper exception
        Throwable ex = e instanceof ExecutionException ? ((ExecutionException) e).getCause() : e;
        if (ex instanceof BoxException) {
            BoxError boxError = ((BoxException) ex).getAsBoxError();
            if (boxError != null) {
                if (((BoxException) ex).getResponseCode() == HttpURLConnection.HTTP_FORBIDDEN || ((BoxException) ex).getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED || boxError.getError().equals("unauthorized_device")) {
                    error += ":" + getResources().getText(R.string.boxsdk_Authentication_fail_forbidden) + "\n";
                } else {
                    error += ":";
                }
                error += boxError.getErrorDescription();
                return new OAuthWebView.AuthFailure(OAuthWebView.AuthFailure.TYPE_AUTHENTICATION_UNAUTHORIZED, error);
            }
        }
        error += ":" + ex;
    }
    return new OAuthWebView.AuthFailure(OAuthWebView.AuthFailure.TYPE_GENERIC, error);
}
Also used : BoxException(com.box.androidsdk.content.BoxException) AuthFailure(com.box.androidsdk.content.auth.OAuthWebView.AuthFailure) BoxError(com.box.androidsdk.content.models.BoxError) ExecutionException(java.util.concurrent.ExecutionException)

Example 13 with BoxException

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

the class BoxRequest method send.

/**
     * 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.
     */
public final T send() throws BoxException {
    Exception ex = null;
    T result = null;
    try {
        result = onSend();
    } catch (Exception e) {
        ex = e;
    }
    // We catch the exception so that onSendCompleted can be called in case additional actions need to be taken
    onSendCompleted(new BoxResponse(result, ex, this));
    if (ex != null) {
        if (ex instanceof BoxException) {
            throw (BoxException) ex;
        } else {
            throw new BoxException("unexpected exception ", ex);
        }
    }
    return result;
}
Also used : BoxException(com.box.androidsdk.content.BoxException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) ExecutionException(java.util.concurrent.ExecutionException) BoxException(com.box.androidsdk.content.BoxException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 14 with BoxException

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

the class DefaultAvatarController method executeAvatarDownloadRequest.

@Override
public BoxFutureTask<BoxDownload> executeAvatarDownloadRequest(final String userId, BoxAvatarView avatarView) {
    final WeakReference<BoxAvatarView> avatarViewWeakReference = new WeakReference<BoxAvatarView>(avatarView);
    try {
        final File avatarFile = getAvatarFile(userId);
        if (mUnavailableAvatars.contains(avatarFile.getAbsolutePath())) {
            // no point trying if we tried before and it was unavailable.
            return null;
        }
        final BoxFutureTask<BoxDownload> avatarDownloadTask = getApiUser().getDownloadAvatarRequest(getAvatarDir(userId), userId).toTask();
        avatarDownloadTask.addOnCompletedListener(new BoxFutureTask.OnCompletedListener<BoxDownload>() {

            @Override
            public void onCompleted(BoxResponse<BoxDownload> response) {
                if (response.isSuccess()) {
                    BoxAvatarView avatarView = avatarViewWeakReference.get();
                    if (avatarView != null) {
                        avatarView.updateAvatar();
                    }
                } else {
                    if (response.getException() instanceof BoxException) {
                        if (((BoxException) response.getException()).getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
                            mUnavailableAvatars.add(getAvatarFile(userId).getAbsolutePath());
                        }
                    }
                    if (avatarFile != null) {
                        avatarFile.delete();
                    }
                }
            }
        });
        executeTask(avatarDownloadTask);
        return avatarDownloadTask;
    } catch (IOException e) {
        BoxLogUtils.e("unable to createFile ", e);
    }
    return null;
}
Also used : BoxException(com.box.androidsdk.content.BoxException) BoxAvatarView(com.box.androidsdk.content.views.BoxAvatarView) BoxDownload(com.box.androidsdk.content.models.BoxDownload) WeakReference(java.lang.ref.WeakReference) IOException(java.io.IOException) File(java.io.File) BoxRequestsFile(com.box.androidsdk.content.requests.BoxRequestsFile) BoxFutureTask(com.box.androidsdk.content.BoxFutureTask)

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