Search in sources :

Example 76 with RemoteOperationResult

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

the class CreateFolderOperation method normalCreate.

private RemoteOperationResult normalCreate(OwnCloudClient client) {
    RemoteOperationResult result = new CreateFolderRemoteOperation(remotePath, true).execute(client);
    if (result.isSuccess()) {
        RemoteOperationResult remoteFolderOperationResult = new ReadFolderRemoteOperation(remotePath).execute(client);
        createdRemoteFolder = (RemoteFile) remoteFolderOperationResult.getData().get(0);
        saveFolderInDB();
    } else {
        Log_OC.e(TAG, remotePath + " hasn't been created");
    }
    return result;
}
Also used : CreateFolderRemoteOperation(com.owncloud.android.lib.resources.files.CreateFolderRemoteOperation) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) ReadFolderRemoteOperation(com.owncloud.android.lib.resources.files.ReadFolderRemoteOperation)

Example 77 with RemoteOperationResult

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

the class CreateFileFromTemplateOperation method run.

protected RemoteOperationResult run(OwnCloudClient client) {
    RemoteOperationResult result;
    Utf8PostMethod postMethod = null;
    try {
        postMethod = new Utf8PostMethod(client.getBaseUri() + NEW_FROM_TEMPLATE_URL + JSON_FORMAT);
        postMethod.setParameter("path", path);
        postMethod.setParameter("template", String.valueOf(templateId));
        // remote request
        postMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
        int status = client.executeMethod(postMethod, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT);
        if (status == HttpStatus.SC_OK) {
            String response = postMethod.getResponseBodyAsString();
            // Parse the response
            JSONObject respJSON = new JSONObject(response);
            String url = respJSON.getJSONObject(NODE_OCS).getJSONObject(NODE_DATA).getString("url");
            ArrayList<Object> templateArray = new ArrayList<>();
            templateArray.add(url);
            result = new RemoteOperationResult(true, postMethod);
            result.setData(templateArray);
        } else {
            result = new RemoteOperationResult(false, postMethod);
            client.exhaustResponse(postMethod.getResponseBodyAsStream());
        }
    } catch (Exception e) {
        result = new RemoteOperationResult(e);
        Log_OC.e(TAG, "Create file from template " + templateId + " failed: " + result.getLogMessage(), result.getException());
    } finally {
        if (postMethod != null) {
            postMethod.releaseConnection();
        }
    }
    return result;
}
Also used : Utf8PostMethod(org.apache.commons.httpclient.methods.Utf8PostMethod) JSONObject(org.json.JSONObject) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) ArrayList(java.util.ArrayList) JSONObject(org.json.JSONObject)

Example 78 with RemoteOperationResult

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

the class TextEditorLoadUrlTask method doInBackground.

@Override
protected String doInBackground(Void... voids) {
    final EditorWebView editorWebView = editorWebViewWeakReference.get();
    if (editorWebView == null) {
        return "";
    }
    Editor editor = FileMenuFilter.getEditor(editorWebView.getContentResolver(), user, file.getMimeType());
    if (editor == null) {
        return "";
    }
    RemoteOperationResult result = new DirectEditingOpenFileRemoteOperation(file.getRemotePath(), editor.getId()).execute(account, editorWebViewWeakReference.get());
    if (!result.isSuccess()) {
        return "";
    }
    return (String) result.getData().get(0);
}
Also used : DirectEditingOpenFileRemoteOperation(com.nextcloud.android.lib.resources.directediting.DirectEditingOpenFileRemoteOperation) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) Editor(com.owncloud.android.lib.common.Editor) EditorWebView(com.owncloud.android.ui.activity.EditorWebView)

Example 79 with RemoteOperationResult

use of com.owncloud.android.lib.common.operations.RemoteOperationResult in project android by owncloud.

the class GetSharesForFileOperation method run.

@Override
protected RemoteOperationResult run(OwnCloudClient client) {
    GetRemoteSharesForFileOperation operation = new GetRemoteSharesForFileOperation(mPath, mReshares, mSubfiles);
    RemoteOperationResult result = operation.execute(client);
    if (result.isSuccess()) {
        // Update DB with the response
        Log_OC.d(TAG, "File = " + mPath + " Share list size  " + result.getData().size());
        ArrayList<OCShare> shares = new ArrayList<OCShare>();
        for (Object obj : result.getData()) {
            shares.add((OCShare) obj);
        }
        getStorageManager().saveSharesDB(shares);
    } else if (result.getCode() == RemoteOperationResult.ResultCode.SHARE_NOT_FOUND) {
        // no share on the file - remove local shares
        getStorageManager().removeSharesForFile(mPath);
    }
    return result;
}
Also used : RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) ArrayList(java.util.ArrayList) OCShare(com.owncloud.android.lib.resources.shares.OCShare) GetRemoteSharesForFileOperation(com.owncloud.android.lib.resources.shares.GetRemoteSharesForFileOperation)

Example 80 with RemoteOperationResult

use of com.owncloud.android.lib.common.operations.RemoteOperationResult in project android by owncloud.

the class OAuth2GetAccessToken method run.

/*
    public Map<String, String> getResultTokenMap() {
        return mResultTokenMap;
    }
    */
@Override
protected RemoteOperationResult run(OwnCloudClient client) {
    RemoteOperationResult result = null;
    PostMethod postMethod = null;
    try {
        parseAuthorizationResponse();
        if (mOAuth2ParsedAuthorizationResponse.keySet().contains(OAuth2Constants.KEY_ERROR)) {
            if (OAuth2Constants.VALUE_ERROR_ACCESS_DENIED.equals(mOAuth2ParsedAuthorizationResponse.get(OAuth2Constants.KEY_ERROR))) {
                result = new RemoteOperationResult(ResultCode.OAUTH2_ERROR_ACCESS_DENIED);
            } else {
                result = new RemoteOperationResult(ResultCode.OAUTH2_ERROR);
            }
        }
        if (result == null) {
            NameValuePair[] nameValuePairs = new NameValuePair[4];
            nameValuePairs[0] = new NameValuePair(OAuth2Constants.KEY_GRANT_TYPE, mGrantType);
            nameValuePairs[1] = new NameValuePair(OAuth2Constants.KEY_CODE, mOAuth2ParsedAuthorizationResponse.get(OAuth2Constants.KEY_CODE));
            nameValuePairs[2] = new NameValuePair(OAuth2Constants.KEY_REDIRECT_URI, mRedirectUri);
            nameValuePairs[3] = new NameValuePair(OAuth2Constants.KEY_CLIENT_ID, mClientId);
            //nameValuePairs[4] = new NameValuePair(OAuth2Constants.KEY_SCOPE, mOAuth2ParsedAuthorizationResponse.get(OAuth2Constants.KEY_SCOPE));         
            postMethod = new PostMethod(client.getWebdavUri().toString());
            postMethod.setRequestBody(nameValuePairs);
            int status = client.executeMethod(postMethod);
            String response = postMethod.getResponseBodyAsString();
            if (response != null && response.length() > 0) {
                JSONObject tokenJson = new JSONObject(response);
                parseAccessTokenResult(tokenJson);
                if (mResultTokenMap.get(OAuth2Constants.KEY_ERROR) != null || mResultTokenMap.get(OAuth2Constants.KEY_ACCESS_TOKEN) == null) {
                    result = new RemoteOperationResult(ResultCode.OAUTH2_ERROR);
                } else {
                    result = new RemoteOperationResult(true, postMethod);
                    ArrayList<Object> data = new ArrayList<Object>();
                    data.add(mResultTokenMap);
                    result.setData(data);
                }
            } else {
                result = new RemoteOperationResult(false, postMethod);
                client.exhaustResponse(postMethod.getResponseBodyAsStream());
            }
        }
    } catch (Exception e) {
        result = new RemoteOperationResult(e);
    } finally {
        if (postMethod != null)
            // let the connection available for other methods
            postMethod.releaseConnection();
        if (result.isSuccess()) {
            Log_OC.i(TAG, "OAuth2 TOKEN REQUEST with auth code " + mOAuth2ParsedAuthorizationResponse.get("code") + " to " + client.getWebdavUri() + ": " + result.getLogMessage());
        } else if (result.getException() != null) {
            Log_OC.e(TAG, "OAuth2 TOKEN REQUEST with auth code " + mOAuth2ParsedAuthorizationResponse.get("code") + " to " + client.getWebdavUri() + ": " + result.getLogMessage(), result.getException());
        } else if (result.getCode() == ResultCode.OAUTH2_ERROR) {
            Log_OC.e(TAG, "OAuth2 TOKEN REQUEST with auth code " + mOAuth2ParsedAuthorizationResponse.get("code") + " to " + client.getWebdavUri() + ": " + ((mResultTokenMap != null) ? mResultTokenMap.get(OAuth2Constants.KEY_ERROR) : "NULL"));
        } else {
            Log_OC.e(TAG, "OAuth2 TOKEN REQUEST with auth code " + mOAuth2ParsedAuthorizationResponse.get("code") + " to " + client.getWebdavUri() + ": " + result.getLogMessage());
        }
    }
    return result;
}
Also used : NameValuePair(org.apache.commons.httpclient.NameValuePair) JSONObject(org.json.JSONObject) PostMethod(org.apache.commons.httpclient.methods.PostMethod) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) ArrayList(java.util.ArrayList) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException)

Aggregations

RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)172 OCFile (com.owncloud.android.datamodel.OCFile)48 RemoteOperation (com.owncloud.android.lib.common.operations.RemoteOperation)24 ArrayList (java.util.ArrayList)24 FileDataStorageManager (com.owncloud.android.datamodel.FileDataStorageManager)22 IOException (java.io.IOException)22 Account (android.accounts.Account)19 User (com.nextcloud.client.account.User)18 OCShare (com.owncloud.android.lib.resources.shares.OCShare)18 File (java.io.File)18 OperationCancelledException (com.owncloud.android.lib.common.operations.OperationCancelledException)17 OwnCloudAccount (com.owncloud.android.lib.common.OwnCloudAccount)16 Context (android.content.Context)15 OwnCloudClient (com.owncloud.android.lib.common.OwnCloudClient)12 RemoteFile (com.owncloud.android.lib.resources.files.model.RemoteFile)12 FileNotFoundException (java.io.FileNotFoundException)12 Intent (android.content.Intent)11 JSONObject (org.json.JSONObject)11 Test (org.junit.Test)11 Uri (android.net.Uri)10