Search in sources :

Example 1 with OwnCloudClientManager

use of com.owncloud.android.lib.common.OwnCloudClientManager in project android by nextcloud.

the class InputStreamBinder method processRequestV2.

private Response processRequestV2(final NextcloudRequest request, final InputStream requestBodyInputStream) throws UnsupportedOperationException, com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException, OperationCanceledException, AuthenticatorException, IOException {
    Account account = accountManager.getAccountByName(request.getAccountName());
    if (account == null) {
        throw new IllegalStateException(EXCEPTION_ACCOUNT_NOT_FOUND);
    }
    // Validate token
    if (!isValid(request)) {
        throw new IllegalStateException(EXCEPTION_INVALID_TOKEN);
    }
    // Validate URL
    if (request.getUrl().length() == 0 || request.getUrl().charAt(0) != PATH_SEPARATOR) {
        throw new IllegalStateException(EXCEPTION_INVALID_REQUEST_URL, new IllegalStateException("URL need to start with a /"));
    }
    OwnCloudClientManager ownCloudClientManager = OwnCloudClientManagerFactory.getDefaultSingleton();
    OwnCloudAccount ocAccount = new OwnCloudAccount(account, context);
    OwnCloudClient client = ownCloudClientManager.getClientFor(ocAccount, context);
    HttpMethodBase method = buildMethod(request, client.getBaseUri(), requestBodyInputStream);
    if (request.getParameterV2() != null && !request.getParameterV2().isEmpty()) {
        method.setQueryString(convertListToNVP(request.getParameterV2()));
    } else {
        method.setQueryString(convertMapToNVP(request.getParameter()));
    }
    method.addRequestHeader("OCS-APIREQUEST", "true");
    for (Map.Entry<String, List<String>> header : request.getHeader().entrySet()) {
        // https://stackoverflow.com/a/3097052
        method.addRequestHeader(header.getKey(), TextUtils.join(",", header.getValue()));
        if ("OCS-APIREQUEST".equalsIgnoreCase(header.getKey())) {
            throw new IllegalStateException("The 'OCS-APIREQUEST' header will be automatically added by the Nextcloud SSO Library. " + "Please remove the header before making a request");
        }
    }
    client.setFollowRedirects(request.isFollowRedirects());
    int status = client.executeMethod(method);
    // Check if status code is 2xx --> https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#2xx_Success
    if (status >= HTTP_STATUS_CODE_OK && status < HTTP_STATUS_CODE_MULTIPLE_CHOICES) {
        return new Response(method);
    } else {
        InputStream inputStream = method.getResponseBodyAsStream();
        String total = "No response body";
        // If response body is available
        if (inputStream != null) {
            total = inputStreamToString(inputStream);
            Log_OC.e(TAG, total);
        }
        method.releaseConnection();
        throw new IllegalStateException(EXCEPTION_HTTP_REQUEST_FAILED, new IllegalStateException(String.valueOf(status), new IllegalStateException(total)));
    }
}
Also used : Account(android.accounts.Account) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) HttpMethodBase(org.apache.commons.httpclient.HttpMethodBase) ObjectInputStream(java.io.ObjectInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OwnCloudClientManager(com.owncloud.android.lib.common.OwnCloudClientManager) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) List(java.util.List) OwnCloudClient(com.owncloud.android.lib.common.OwnCloudClient) Map(java.util.Map)

Example 2 with OwnCloudClientManager

use of com.owncloud.android.lib.common.OwnCloudClientManager in project android by nextcloud.

the class OwnCloudClientManagerTest method testUserId.

/**
 * Like on files app we create & store an account in Android's account manager.
 */
@Test
public void testUserId() throws OperationCanceledException, AuthenticatorException, IOException, AccountUtils.AccountNotFoundException {
    Bundle arguments = InstrumentationRegistry.getArguments();
    Uri url = Uri.parse(arguments.getString("TEST_SERVER_URL"));
    String loginName = arguments.getString("TEST_SERVER_USERNAME");
    String password = arguments.getString("TEST_SERVER_PASSWORD");
    AccountManager accountManager = AccountManager.get(targetContext);
    String accountName = AccountUtils.buildAccountName(url, loginName);
    Account newAccount = new Account(accountName, "nextcloud");
    accountManager.addAccountExplicitly(newAccount, password, null);
    accountManager.setUserData(newAccount, AccountUtils.Constants.KEY_OC_BASE_URL, url.toString());
    accountManager.setUserData(newAccount, AccountUtils.Constants.KEY_USER_ID, loginName);
    OwnCloudClientManager manager = new OwnCloudClientManager();
    OwnCloudAccount account = new OwnCloudAccount(newAccount, targetContext);
    OwnCloudClient client = manager.getClientFor(account, targetContext);
    assertEquals(loginName, client.getUserId());
    accountManager.removeAccountExplicitly(newAccount);
    assertEquals(1, accountManager.getAccounts().length);
}
Also used : Account(android.accounts.Account) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) Bundle(android.os.Bundle) OwnCloudClientManager(com.owncloud.android.lib.common.OwnCloudClientManager) AccountManager(android.accounts.AccountManager) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) OwnCloudClient(com.owncloud.android.lib.common.OwnCloudClient) Uri(android.net.Uri) Test(org.junit.Test)

Example 3 with OwnCloudClientManager

use of com.owncloud.android.lib.common.OwnCloudClientManager in project android by nextcloud.

the class InputStreamBinder method processRequest.

private HttpMethodBase processRequest(final NextcloudRequest request, final InputStream requestBodyInputStream) throws UnsupportedOperationException, com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException, OperationCanceledException, AuthenticatorException, IOException {
    Account account = accountManager.getAccountByName(request.getAccountName());
    if (account == null) {
        throw new IllegalStateException(EXCEPTION_ACCOUNT_NOT_FOUND);
    }
    // Validate token
    if (!isValid(request)) {
        throw new IllegalStateException(EXCEPTION_INVALID_TOKEN);
    }
    // Validate URL
    if (request.getUrl().length() == 0 || request.getUrl().charAt(0) != PATH_SEPARATOR) {
        throw new IllegalStateException(EXCEPTION_INVALID_REQUEST_URL, new IllegalStateException("URL need to start with a /"));
    }
    OwnCloudClientManager ownCloudClientManager = OwnCloudClientManagerFactory.getDefaultSingleton();
    OwnCloudAccount ocAccount = new OwnCloudAccount(account, context);
    OwnCloudClient client = ownCloudClientManager.getClientFor(ocAccount, context);
    HttpMethodBase method = buildMethod(request, client.getBaseUri(), requestBodyInputStream);
    if (request.getParameterV2() != null && !request.getParameterV2().isEmpty()) {
        method.setQueryString(convertListToNVP(request.getParameterV2()));
    } else {
        method.setQueryString(convertMapToNVP(request.getParameter()));
    }
    method.addRequestHeader("OCS-APIREQUEST", "true");
    for (Map.Entry<String, List<String>> header : request.getHeader().entrySet()) {
        // https://stackoverflow.com/a/3097052
        method.addRequestHeader(header.getKey(), TextUtils.join(",", header.getValue()));
        if ("OCS-APIREQUEST".equalsIgnoreCase(header.getKey())) {
            throw new IllegalStateException("The 'OCS-APIREQUEST' header will be automatically added by the Nextcloud SSO Library. " + "Please remove the header before making a request");
        }
    }
    client.setFollowRedirects(request.isFollowRedirects());
    int status = client.executeMethod(method);
    // Check if status code is 2xx --> https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#2xx_Success
    if (status >= HTTP_STATUS_CODE_OK && status < HTTP_STATUS_CODE_MULTIPLE_CHOICES) {
        return method;
    } else {
        InputStream inputStream = method.getResponseBodyAsStream();
        String total = "No response body";
        // If response body is available
        if (inputStream != null) {
            total = inputStreamToString(inputStream);
            Log_OC.e(TAG, total);
        }
        method.releaseConnection();
        throw new IllegalStateException(EXCEPTION_HTTP_REQUEST_FAILED, new IllegalStateException(String.valueOf(status), new IllegalStateException(total)));
    }
}
Also used : Account(android.accounts.Account) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) HttpMethodBase(org.apache.commons.httpclient.HttpMethodBase) ObjectInputStream(java.io.ObjectInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OwnCloudClientManager(com.owncloud.android.lib.common.OwnCloudClientManager) List(java.util.List) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) OwnCloudClient(com.owncloud.android.lib.common.OwnCloudClient) Map(java.util.Map)

Aggregations

Account (android.accounts.Account)3 OwnCloudAccount (com.owncloud.android.lib.common.OwnCloudAccount)3 OwnCloudClient (com.owncloud.android.lib.common.OwnCloudClient)3 OwnCloudClientManager (com.owncloud.android.lib.common.OwnCloudClientManager)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 ObjectInputStream (java.io.ObjectInputStream)2 List (java.util.List)2 Map (java.util.Map)2 HttpMethodBase (org.apache.commons.httpclient.HttpMethodBase)2 AccountManager (android.accounts.AccountManager)1 Uri (android.net.Uri)1 Bundle (android.os.Bundle)1 Test (org.junit.Test)1