Example usage for android.net Uri buildUpon

List of usage examples for android.net Uri buildUpon

Introduction

In this page you can find the example usage for android.net Uri buildUpon.

Prototype

public abstract Builder buildUpon();

Source Link

Document

Constructs a new builder, copying the attributes from this Uri.

Usage

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

@Override
public CFolder getFolderInfo(@NonNull String folderId) throws RequestFailException {
    if (TextUtils.isEmpty(mAccessToken)) {
        throw new RequestFailException("Access token not available");
    }/*from   w w w .  j a  va  2  s .  c  o  m*/

    Uri uri = Uri.parse(mMetadataUrl);
    String url = uri.buildUpon().appendEncodedPath("nodes/" + folderId).build().toString();

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

    try {
        Response response = mHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            JSONObject jsonObject = new JSONObject(response.body().string());
            return buildFolder(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.CloudDriveApi.java

@Override
public CFile getFileInfo(@NonNull String fileId) throws RequestFailException {
    if (TextUtils.isEmpty(mAccessToken)) {
        throw new RequestFailException("Access token not available");
    }//from  w ww  .  j ava2s  . c  om

    Uri uri = Uri.parse(mMetadataUrl);
    String url = uri.buildUpon().appendEncodedPath("nodes/" + fileId).build().toString();

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

    try {
        Response response = mHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            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.CloudDriveApi.java

/**
 * Get root folder id/*w ww  .  ja  v a 2  s  .c o m*/
 *
 * @return root id as String
 */
private String getRootId() {
    Uri uri = Uri.parse(mMetadataUrl);
    String url = uri.buildUpon().appendEncodedPath("nodes")
            .appendQueryParameter("filters", "kind:FOLDER AND isRoot:true").build().toString();

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

    try {
        Response response = mHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            JSONObject jsonObject = new JSONObject(response.body().string());
            JSONArray entries = jsonObject.getJSONArray("data");
            if (entries.length() > 0) {
                JSONObject root = entries.getJSONObject(0);
                return ROOT_ID = root.getString("id");
            }
        } else {
            Log.e(TAG, response.message());
        }
    } catch (JSONException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}

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

@Override
public CFile updateFile(@NonNull CFile file, File content) throws RequestFailException {
    if (TextUtils.isEmpty(mAccessToken)) {
        throw new RequestFailException("Access token not available");
    }/*  w ww.j av  a 2s .  co m*/

    Uri uri = Uri.parse(mContentUrl);
    String url = uri.buildUpon().appendEncodedPath("nodes/" + file.getId() + "/content").build().toString();

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

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

    try {
        Response response = mHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            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:net.sf.xfd.provider.PublicProvider.java

@Nullable
@Override/*from   w ww  . j a v  a 2  s  .  c om*/
public ParcelFileDescriptor openFile(@NonNull Uri uri, @NonNull String requestedMode, CancellationSignal signal)
        throws FileNotFoundException {
    String path = uri.getPath();

    assertAbsolute(path);

    final int readableMode = ParcelFileDescriptor.parseMode(requestedMode);

    if (signal != null) {
        final Thread theThread = Thread.currentThread();

        signal.setOnCancelListener(theThread::interrupt);
    }

    path = canonString(path);

    if (!path.equals(uri.getPath())) {
        uri = uri.buildUpon().path(path).build();
    }

    try {
        if (!checkAccess(uri, requestedMode)) {
            return null;
        }

        final OS rooted = base.getOS();

        if (rooted == null) {
            throw new FileNotFoundException("Failed to open " + uri.getPath() + ": unable to acquire access");
        }

        int openFlags;

        if ((readableMode & MODE_READ_ONLY) == readableMode) {
            openFlags = OS.O_RDONLY;
        } else if ((readableMode & MODE_WRITE_ONLY) == readableMode) {
            openFlags = OS.O_WRONLY;
        } else {
            openFlags = OS.O_RDWR;
        }

        if (signal == null) {
            openFlags |= NativeBits.O_NONBLOCK;
        }

        //noinspection WrongConstant
        @Fd
        int fd = rooted.open(path, openFlags, 0);

        return ParcelFileDescriptor.adoptFd(fd);
    } catch (IOException e) {
        throw new FileNotFoundException("Unable to open " + uri.getPath() + ": " + e.getMessage());
    } finally {
        if (signal != null) {
            signal.setOnCancelListener(null);
        }

        Thread.interrupted();
    }
}

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

@Override
public File getThumbnail(@NonNull CFile file) throws RequestFailException {
    if (TextUtils.isEmpty(mAccessToken)) {
        throw new RequestFailException("Access token not available");
    }/*w  w  w .  ja v a  2s  .  c om*/

    Uri uri = Uri.parse(mMetadataUrl);
    String url = uri.buildUpon().appendEncodedPath("nodes/" + file.getId()).appendQueryParameter("asset", "ALL")
            .appendQueryParameter("tempLink", "true").build().toString();

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

    try {
        Response response = mHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            JSONObject jsonObject = new JSONObject(response.body().string());
            if (jsonObject.has("assets")) {
                JSONArray assets = jsonObject.getJSONArray("assets");
                JSONObject asset = assets.getJSONObject(0);
                if (asset.has("tempLink")) {
                    String fileUrl = asset.getString("tempLink");
                    String filename = asset.getString("name");
                    return downloadThumbnail(fileUrl, filename);
                }
                return null;
            } else {
                return null;
            }
        } else {
            throw new RequestFailException(response.message(), response.code());
        }
    } catch (IOException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return null;
}

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

/**
 * Get continue folder items//  w  ww.  j  ava  2s .co m
 *
 * @param folderId of the folder to explore
 * @param startToken nextToken from previous request for access more content
 * @return List that contains CFile and CFolder
 * @throws RequestFailException that content various error types
 */
public synchronized List<Object> exploreFolderContinue(String folderId, String startToken)
        throws RequestFailException {
    if (TextUtils.isEmpty(mAccessToken)) {
        throw new RequestFailException("Access token not available");
    }

    List<Object> list = new ArrayList<>();

    Uri uri = Uri.parse(mMetadataUrl);
    String url = uri.buildUpon().appendEncodedPath("nodes/" + folderId + "/children")
            .appendQueryParameter("startToken", startToken).build().toString();

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

    try {
        Response response = mHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            JSONObject jsonObject = new JSONObject(response.body().string());
            JSONArray entries = jsonObject.getJSONArray("data");
            if (entries.length() > 0) {
                list.addAll(createItemList(entries));
            } else {
                // return null if no item found
                return null;
            }
            // pagination available
            if (jsonObject.has("nextToken")) {
                list.addAll(exploreFolderContinue(folderId, jsonObject.getString("nextToken")));
            }
            return list;
        } 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.CloudDriveApi.java

@Override
public void deleteFolder(@NonNull CFolder folder) throws RequestFailException {
    if (TextUtils.isEmpty(mAccessToken)) {
        throw new RequestFailException("Access token not available");
    }/* w ww.  j  ava  2  s. c  o  m*/

    Uri uri = Uri.parse(mMetadataUrl);
    String url = uri.buildUpon().appendEncodedPath("trash/" + folder.getId()).build().toString();

    // create parameter as json
    final JSONObject params = new JSONObject();
    try {
        params.put("kind", "FOLDER");
    } 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(url)
            .header("Authorization", String.format("Bearer %s", mAccessToken)).put(body).build();

    try {
        Response response = mHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            Log.d(TAG, "CFolder with the id: " + folder.getName() + " 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.CloudDriveApi.java

@Override
public void deleteFile(@NonNull CFile file) throws RequestFailException {
    if (TextUtils.isEmpty(mAccessToken)) {
        throw new RequestFailException("Access token not available");
    }//from ww w.ja v a 2s  .  co m

    Uri uri = Uri.parse(mMetadataUrl);
    String url = uri.buildUpon().appendEncodedPath("trash/" + file.getId()).build().toString();

    // create parameter as json
    final JSONObject params = new JSONObject();
    try {
        params.put("kind", "FOLDER");
    } 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(url)
            .header("Authorization", String.format("Bearer %s", mAccessToken)).put(body).build();

    try {
        Response response = mHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            Log.d(TAG, "File with the id: " + file.getName() + " 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.CloudDriveApi.java

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

    Uri uri = Uri.parse(mContentUrl);
    String url = uri.buildUpon().appendEncodedPath("nodes").build().toString();

    // create parameter as json
    final JSONObject params = new JSONObject();
    try {
        params.put("name", file.getName());
        params.put("kind", "FILE");
        ArrayList<String> parentList = new ArrayList<>();
        parentList.add(parent != null ? parent.getId() : getRoot().getId());
        params.put("parents", new JSONArray(parentList));
    } 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("metadata", params.toString())
            .addFormDataPart("content", file.getName(), RequestBody.create(fileType, file)).build();

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

    try {
        Response response = mHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            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());
    }
}