use of com.owncloud.android.lib.common.OwnCloudClient in project android by owncloud.
the class AuthenticatorAsyncTask method doInBackground.
@Override
protected RemoteOperationResult doInBackground(Object... params) {
RemoteOperationResult result;
if (params != null && params.length == 2) {
String url = (String) params[0];
OwnCloudCredentials credentials = (OwnCloudCredentials) params[1];
// Client
Uri uri = Uri.parse(url);
OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(uri, mContext, true);
client.setCredentials(credentials);
// Operation - try credentials
ExistenceCheckRemoteOperation operation = new ExistenceCheckRemoteOperation(REMOTE_PATH, mContext, SUCCESS_IF_ABSENT);
result = operation.execute(client);
String targetUrlAfterPermanentRedirection = null;
if (operation.wasRedirected()) {
RedirectionPath redirectionPath = operation.getRedirectionPath();
targetUrlAfterPermanentRedirection = redirectionPath.getLastPermanentLocation();
}
// Operation - get display name
if (result.isSuccess()) {
GetRemoteUserInfoOperation remoteUserNameOperation = new GetRemoteUserInfoOperation();
if (targetUrlAfterPermanentRedirection != null) {
// we can't assume that any subpath of the domain is correctly redirected; ugly stuff
client = OwnCloudClientFactory.createOwnCloudClient(Uri.parse(AccountUtils.trimWebdavSuffix(targetUrlAfterPermanentRedirection)), mContext, true);
client.setCredentials(credentials);
}
result = remoteUserNameOperation.execute(client);
}
// let the caller knows what is real URL that should be accessed for the account
// being authenticated if the initial URL is being redirected permanently (HTTP code 301)
result.setLastPermanentLocation(targetUrlAfterPermanentRedirection);
} else {
result = new RemoteOperationResult(RemoteOperationResult.ResultCode.UNKNOWN_ERROR);
}
return result;
}
use of com.owncloud.android.lib.common.OwnCloudClient in project android by owncloud.
the class GetShareWithUsersAsyncTask method doInBackground.
@Override
protected Pair<RemoteOperation, RemoteOperationResult> doInBackground(Object... params) {
GetSharesForFileOperation operation = null;
RemoteOperationResult result = null;
if (params != null && params.length == 3) {
OCFile file = (OCFile) params[0];
Account account = (Account) params[1];
FileDataStorageManager fileDataStorageManager = (FileDataStorageManager) params[2];
try {
// Get shares request
operation = new GetSharesForFileOperation(file.getRemotePath(), false, false);
OwnCloudAccount ocAccount = new OwnCloudAccount(account, MainApp.getAppContext());
OwnCloudClient client = OwnCloudClientManagerFactory.getDefaultSingleton().getClientFor(ocAccount, MainApp.getAppContext());
result = operation.execute(client, fileDataStorageManager);
} catch (Exception e) {
result = new RemoteOperationResult(e);
Log_OC.e(TAG, "Exception while getting shares", e);
}
} else {
result = new RemoteOperationResult(RemoteOperationResult.ResultCode.UNKNOWN_ERROR);
}
return new Pair(operation, result);
}
use of com.owncloud.android.lib.common.OwnCloudClient in project android by nextcloud.
the class GetShareWithUsersAsyncTask method doInBackground.
@Override
protected Pair<RemoteOperation, RemoteOperationResult> doInBackground(Object... params) {
GetSharesForFileOperation operation = null;
RemoteOperationResult result = null;
if (params != null && params.length == 3) {
OCFile file = (OCFile) params[0];
Account account = (Account) params[1];
FileDataStorageManager fileDataStorageManager = (FileDataStorageManager) params[2];
try {
// Get shares request
operation = new GetSharesForFileOperation(file.getRemotePath(), false, false);
OwnCloudAccount ocAccount = new OwnCloudAccount(account, MainApp.getAppContext());
OwnCloudClient client = OwnCloudClientManagerFactory.getDefaultSingleton().getClientFor(ocAccount, MainApp.getAppContext());
result = operation.execute(client, fileDataStorageManager);
} catch (Exception e) {
result = new RemoteOperationResult(e);
Log_OC.e(TAG, "Exception while getting shares", e);
}
} else {
result = new RemoteOperationResult(RemoteOperationResult.ResultCode.UNKNOWN_ERROR);
}
return new Pair(operation, result);
}
use of com.owncloud.android.lib.common.OwnCloudClient 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)));
}
}
use of com.owncloud.android.lib.common.OwnCloudClient in project android by nextcloud.
the class PushUtils method deleteRegistrationForAccount.
private static void deleteRegistrationForAccount(Account account) {
Context context = MainApp.getAppContext();
OwnCloudAccount ocAccount;
arbitraryDataProvider = new ArbitraryDataProvider(MainApp.getAppContext().getContentResolver());
try {
ocAccount = new OwnCloudAccount(account, context);
OwnCloudClient mClient = OwnCloudClientManagerFactory.getDefaultSingleton().getClientFor(ocAccount, context);
RemoteOperation unregisterAccountDeviceForNotificationsOperation = new UnregisterAccountDeviceForNotificationsOperation();
RemoteOperationResult remoteOperationResult = unregisterAccountDeviceForNotificationsOperation.execute(mClient);
if (remoteOperationResult.getHttpCode() == HttpStatus.SC_ACCEPTED) {
String arbitraryValue;
if (!TextUtils.isEmpty(arbitraryValue = arbitraryDataProvider.getValue(account.name, KEY_PUSH))) {
Gson gson = new Gson();
PushConfigurationState pushArbitraryData = gson.fromJson(arbitraryValue, PushConfigurationState.class);
RemoteOperationResult unregisterResult = new UnregisterAccountDeviceForProxyOperation(context.getResources().getString(R.string.push_server_url), pushArbitraryData.getDeviceIdentifier(), pushArbitraryData.getDeviceIdentifierSignature(), pushArbitraryData.getUserPublicKey()).run();
if (unregisterResult.isSuccess()) {
arbitraryDataProvider.deleteKeyForAccount(account.name, KEY_PUSH);
}
}
}
} catch (com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException e) {
Log_OC.d(TAG, "Failed to find an account");
} catch (AuthenticatorException e) {
Log_OC.d(TAG, "Failed via AuthenticatorException");
} catch (IOException e) {
Log_OC.d(TAG, "Failed via IOException");
} catch (OperationCanceledException e) {
Log_OC.d(TAG, "Failed via OperationCanceledException");
}
}
Aggregations