Example usage for com.squareup.okhttp Response isSuccessful

List of usage examples for com.squareup.okhttp Response isSuccessful

Introduction

In this page you can find the example usage for com.squareup.okhttp Response isSuccessful.

Prototype

public boolean isSuccessful() 

Source Link

Document

Returns true if the code is in [200..300), which means the request was successfully received, understood, and accepted.

Usage

From source file:com.he5ed.lib.cloudprovider.apis.BoxApi.java

License:Apache License

@Override
public synchronized CFolder moveFolder(@NonNull CFolder folder, @Nullable CFolder parent)
        throws RequestFailException {
    if (TextUtils.isEmpty(mAccessToken)) {
        throw new RequestFailException("Access token not available");
    }/* w  w  w  .  j  a v a  2 s  . c o m*/

    // exit if root or same name
    if (folder.isRoot() || parent != null && folder.getId().equals(parent.getId()))
        return folder;

    // create parameter as json
    final JSONObject params = new JSONObject();
    try {
        params.put("parent", new JSONObject().put("id", parent != null ? parent.getId() : getRoot().getId()));
    } catch (JSONException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }

    RequestBody body = new RequestBody() {
        @Override
        public MediaType contentType() {
            return JSON;
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            sink.writeUtf8(params.toString());
        }
    };

    Request request = new Request.Builder().url(API_BASE_URL + "/folders/" + folder.getId())
            .header("Authorization", String.format("Bearer %s", mAccessToken)).put(body).build();

    try {
        Response response = mHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            // return folder object
            return buildFolder(new JSONObject(response.body().string()));
        } else {
            throw new RequestFailException(response.message(), response.code());
        }
    } catch (JSONException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    } catch (IOException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }
}

From source file:com.he5ed.lib.cloudprovider.apis.BoxApi.java

License:Apache License

@Override
public synchronized void deleteFolder(@NonNull CFolder folder) throws RequestFailException {
    if (TextUtils.isEmpty(mAccessToken)) {
        throw new RequestFailException("Access token not available");
    }/*from   w  w  w  .  java 2  s  .  c  om*/

    String folderId = folder.getId();
    Uri uri = Uri.parse(API_BASE_URL);
    String url = uri.buildUpon().appendEncodedPath("folders/" + folderId)
            .appendQueryParameter("recursive", "true").build().toString();

    Request request = new Request.Builder().url(url)
            .header("Authorization", String.format("Bearer %s", mAccessToken)).delete().build();

    try {
        Response response = mHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            Log.d(TAG, "CFolder with the id: " + folderId + " deleted");
        } else {
            throw new RequestFailException(response.message(), response.code());
        }
    } catch (IOException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }
}

From source file:com.he5ed.lib.cloudprovider.apis.BoxApi.java

License:Apache License

@Override
public synchronized CFile getFileInfo(@NonNull String fileId) throws RequestFailException {
    if (TextUtils.isEmpty(mAccessToken)) {
        throw new RequestFailException("Access token not available");
    }//ww  w  .j  a  v a  2 s  .c  o  m

    if (TextUtils.isEmpty(fileId))
        return null;

    Request request = new Request.Builder().url(API_BASE_URL + "/files/" + fileId)
            .header("Authorization", String.format("Bearer %s", mAccessToken)).get().build();

    try {
        Response response = mHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            // new file created
            JSONObject jsonObject = new JSONObject(response.body().string());
            return buildFile(jsonObject);
        } else {
            throw new RequestFailException(response.message(), response.code());
        }
    } catch (JSONException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    } catch (IOException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }
}

From source file:com.he5ed.lib.cloudprovider.apis.BoxApi.java

License:Apache License

@Override
public synchronized File downloadFile(@NonNull CFile file, @Nullable String filename)
        throws RequestFailException {
    if (TextUtils.isEmpty(mAccessToken)) {
        throw new RequestFailException("Access token not available");
    }//from   ww w  .j  a  va  2  s .c  om

    // assign filename
    if (TextUtils.isEmpty(filename))
        filename = file.getName();

    String fileId = file.getId();
    Request request = new Request.Builder().url(API_BASE_URL + "/files/" + fileId + "/content")
            .header("Authorization", String.format("Bearer %s", mAccessToken)).get().build();

    try {
        Response response = mHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            switch (response.code()) {
            case 200:
                // redirect to url
                return downloadFile(response.request(), filename);
            case 202:
                // retry after due to file just uploaded
                delayDownloadFile(file, filename);
                break;
            case 302:
                // redirect to url
                return downloadFile(response.request(), filename);
            }
        } else {
            throw new RequestFailException(response.message(), response.code());
        }
    } catch (IOException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }
    return null;
}

From source file:com.he5ed.lib.cloudprovider.apis.BoxApi.java

License:Apache License

/**
 * Download file from redirect request/*from  w  w  w . j  ava2  s .  c o  m*/
 *
 * @param request for redirect
 * @return File
 * @throws RequestFailException
 */
private File downloadFile(@NonNull Request request, @NonNull String filename) throws RequestFailException {
    try {
        File file = new File(CloudProvider.CACHE_DIR, filename);

        Response response = mHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            if (FilesUtils.getInternalAvailableBytes() < response.body().contentLength()) {
                // insufficient storage space throw exception
                throw new RequestFailException("Insufficient storage");
            } else {
                FilesUtils.copyFile(response.body().byteStream(), new FileOutputStream(file));
            }
        } else {
            throw new RequestFailException(response.message(), response.code());
        }
        return file;
    } catch (IOException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }
}

From source file:com.he5ed.lib.cloudprovider.apis.BoxApi.java

License:Apache License

@Override
public synchronized CFile uploadFile(@NonNull File file, @Nullable CFolder parent) throws RequestFailException {
    if (TextUtils.isEmpty(mAccessToken)) {
        throw new RequestFailException("Access token not available");
    }//ww  w. j a  va 2  s.  c  om

    // create parameter as json
    final JSONObject params = new JSONObject();
    try {
        params.put("name", file.getName());
        params.put("parent", new JSONObject().put("id", parent != null ? parent.getId() : getRoot().getId()));
    } catch (JSONException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }

    // create multipart body
    MediaType fileType = MediaType.parse(FilesUtils.getFileType(file));
    RequestBody multipart = new MultipartBuilder().type(MultipartBuilder.FORM)
            .addFormDataPart("attributes", params.toString())
            .addFormDataPart("file", file.getName(), RequestBody.create(fileType, file)).build();

    Request request = new Request.Builder().url(API_UPLOAD_URL + "/files/content")
            .header("Authorization", String.format("Bearer %s", mAccessToken)).post(multipart).build();

    try {
        Response response = mHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            // new file created
            JSONObject jsonObject = new JSONObject(response.body().string());
            JSONArray entries = jsonObject.getJSONArray("entries");
            return buildFile(entries.getJSONObject(0));
        } else {
            throw new RequestFailException(response.message(), response.code());
        }
    } catch (JSONException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    } catch (IOException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }
}

From source file:com.he5ed.lib.cloudprovider.apis.BoxApi.java

License:Apache License

@Override
public synchronized CFile updateFile(@NonNull CFile file, File content) throws RequestFailException {
    if (TextUtils.isEmpty(mAccessToken)) {
        throw new RequestFailException("Access token not available");
    }//from  www .j av  a  2  s  . c  om

    // create multipart body
    MediaType fileType = MediaType.parse(FilesUtils.getFileType(content));
    RequestBody multipart = new MultipartBuilder().type(MultipartBuilder.FORM)
            .addFormDataPart("file", content.getName(), RequestBody.create(fileType, content)).build();

    Request request = new Request.Builder().url(API_UPLOAD_URL + "/files/" + file.getId() + "/content")
            .header("Authorization", String.format("Bearer %s", mAccessToken)).post(multipart).build();

    try {
        Response response = mHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            // new file created
            JSONObject jsonObject = new JSONObject(response.body().string());
            JSONArray entries = jsonObject.getJSONArray("entries");
            return buildFile(entries.getJSONObject(0));
        } else {
            throw new RequestFailException(response.message(), response.code());
        }
    } catch (JSONException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    } catch (IOException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }
}

From source file:com.he5ed.lib.cloudprovider.apis.BoxApi.java

License:Apache License

@Override
public synchronized CFile renameFile(@NonNull CFile file, String name) throws RequestFailException {
    if (TextUtils.isEmpty(mAccessToken)) {
        throw new RequestFailException("Access token not available");
    }/* w w w .  j  av a 2  s  . c o m*/

    // exist if same filename
    if (file.getName().equals(name))
        return file;

    // create parameter as json
    final JSONObject params = new JSONObject();
    try {
        params.put("name", name);
    } catch (JSONException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }

    RequestBody body = new RequestBody() {
        @Override
        public MediaType contentType() {
            return JSON;
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            sink.writeUtf8(params.toString());
        }
    };

    Request request = new Request.Builder().url(API_BASE_URL + "/files/" + file.getId())
            .header("Authorization", String.format("Bearer %s", mAccessToken)).put(body).build();

    try {
        Response response = mHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            // return file object
            return buildFile(new JSONObject(response.body().string()));
        } else {
            throw new RequestFailException(response.message(), response.code());
        }
    } catch (JSONException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    } catch (IOException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }
}

From source file:com.he5ed.lib.cloudprovider.apis.BoxApi.java

License:Apache License

@Override
public synchronized CFile moveFile(@NonNull CFile file, @Nullable CFolder folder) throws RequestFailException {
    if (TextUtils.isEmpty(mAccessToken)) {
        throw new RequestFailException("Access token not available");
    }//  ww  w  .j  av  a2s  .co m

    // create parameter as json
    final JSONObject params = new JSONObject();
    try {
        params.put("parent", new JSONObject().put("id", folder != null ? folder.getId() : getRoot().getId()));
    } catch (JSONException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }

    RequestBody body = new RequestBody() {
        @Override
        public MediaType contentType() {
            return JSON;
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            sink.writeUtf8(params.toString());
        }
    };

    Request request = new Request.Builder().url(API_BASE_URL + "/files/" + file.getId())
            .header("Authorization", String.format("Bearer %s", mAccessToken)).put(body).build();

    try {
        Response response = mHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            // return file object
            return buildFile(new JSONObject(response.body().string()));
        } else {
            throw new RequestFailException(response.message(), response.code());
        }
    } catch (JSONException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    } catch (IOException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }
}

From source file:com.he5ed.lib.cloudprovider.apis.BoxApi.java

License:Apache License

@Override
public synchronized void deleteFile(@NonNull CFile file) throws RequestFailException {
    if (TextUtils.isEmpty(mAccessToken)) {
        throw new RequestFailException("Access token not available");
    }//  w w  w.  ja  v a2s .  c  o  m

    String fileId = file.getId();
    Request request = new Request.Builder().url(API_BASE_URL + "/files/" + fileId)
            .header("Authorization", String.format("Bearer %s", mAccessToken)).delete().build();

    try {
        Response response = mHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            Log.d(TAG, "File with the id: " + fileId + " deleted");
        } else {
            throw new RequestFailException(response.message(), response.code());
        }
    } catch (IOException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }
}