<service android:name=".files.services.FileUploader" >\r
</service>
- <service android:name=".files.services.InstantUploadService" />
<receiver android:name=".files.InstantUploadBroadcastReceiver">\r
<intent-filter>\r
<action android:name="com.android.camera.NEW_PICTURE" />\r
public static final String WEBDAV_PATH_1_2 = "/webdav/owncloud.php";\r
public static final String WEBDAV_PATH_2_0 = "/files/webdav.php";\r
public static final String WEBDAV_PATH_4_0 = "/remote.php/webdav";\r
+ private static final String ODAV_PATH = "/remote.php/odav";\r
public static final String CARDDAV_PATH_2_0 = "/apps/contacts/carddav.php";\r
public static final String CARDDAV_PATH_4_0 = "/remote/carddav.php";\r
public static final String STATUS_PATH = "/status.php";\r
* @param version version of owncloud\r
* @return webdav path for given OC version, null if OC version unknown\r
*/\r
- public static String getWebdavPath(OwnCloudVersion version) {\r
+ public static String getWebdavPath(OwnCloudVersion version, boolean supportsOAuth) {\r
+ if (supportsOAuth) {\r
+ return ODAV_PATH;\r
+ }\r
if (version != null) {\r
if (version.compareTo(OwnCloudVersion.owncloud_v4) >= 0)\r
return WEBDAV_PATH_4_0;\r
AccountManager ama = AccountManager.get(context);\r
String baseurl = ama.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL);\r
String strver = ama.getUserData(account, AccountAuthenticator.KEY_OC_VERSION);\r
+ boolean supportsOAuth = (ama.getUserData(account, AccountAuthenticator.KEY_SUPPORTS_OAUTH2) != null);\r
OwnCloudVersion ver = new OwnCloudVersion(strver);\r
- String webdavpath = getWebdavPath(ver);\r
+ String webdavpath = getWebdavPath(ver, supportsOAuth);\r
\r
if (webdavpath == null) return null;\r
return baseurl + webdavpath;\r
\r
public void uploadFiles() {\r
try {\r
+ /* TODO - mCreateDir can never be true at this moment; we will replace wdc.createDirectory by CreateFolderOperation when that is fixed \r
WebdavClient wdc = OwnCloudClientUtils.createOwnCloudClient(mAccount, getApplicationContext());\r
-\r
// create last directory in path if necessary\r
if (mCreateDir) {\r
wdc.createDirectory(mUploadPath);\r
}\r
+ */\r
\r
String[] local = new String[mStreamsToUpload.size()], remote = new String[mStreamsToUpload.size()];\r
\r
*/\r
public static final String ACCOUNT_TYPE = "owncloud";\r
public static final String AUTH_TOKEN_TYPE = "org.owncloud";\r
+ public static final String AUTH_TOKEN_TYPE_PASSWORD = "owncloud.password";\r
+ public static final String AUTH_TOKEN_TYPE_ACCESS_TOKEN = "owncloud.oauth2.access_token";\r
+ public static final String AUTH_TOKEN_TYPE_REFRESH_TOKEN = "owncloud.oauth2.refresh_token";\r
\r
public static final String KEY_AUTH_TOKEN_TYPE = "authTokenType";\r
public static final String KEY_REQUIRED_FEATURES = "requiredFeatures";\r
* http://server/path or https://owncloud.server\r
*/\r
public static final String KEY_OC_BASE_URL = "oc_base_url";\r
-\r
- private static final String TAG = "AccountAuthenticator";\r
+ /**\r
+ * Flag signaling if the ownCloud server can be accessed with OAuth2 access tokens.\r
+ */\r
+ public static final String KEY_SUPPORTS_OAUTH2 = "oc_supports_oauth2";\r
+ \r
+ private static final String TAG = AccountAuthenticator.class.getSimpleName();\r
+ \r
private Context mContext;\r
\r
public AccountAuthenticator(Context context) {\r
return e.getFailureBundle();\r
}\r
final AccountManager am = AccountManager.get(mContext);\r
- final String password = am.getPassword(account);\r
- if (password != null) {\r
- final Bundle result = new Bundle();\r
- result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);\r
- result.putString(AccountManager.KEY_ACCOUNT_TYPE, ACCOUNT_TYPE);\r
- result.putString(AccountManager.KEY_AUTHTOKEN, password);\r
- return result;\r
+ if (authTokenType.equals(AUTH_TOKEN_TYPE_ACCESS_TOKEN)) {\r
+ final String accessToken = am.peekAuthToken(account, authTokenType);\r
+ if (accessToken != null) {\r
+ final Bundle result = new Bundle();\r
+ result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);\r
+ result.putString(AccountManager.KEY_ACCOUNT_TYPE, ACCOUNT_TYPE);\r
+ result.putString(AccountManager.KEY_AUTHTOKEN, accessToken);\r
+ return result;\r
+ }\r
+ \r
+ } else if (authTokenType.equals(AUTH_TOKEN_TYPE_PASSWORD)) {\r
+ final String password = am.getPassword(account);\r
+ if (password != null) {\r
+ final Bundle result = new Bundle();\r
+ result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);\r
+ result.putString(AccountManager.KEY_ACCOUNT_TYPE, ACCOUNT_TYPE);\r
+ result.putString(AccountManager.KEY_AUTHTOKEN, password);\r
+ return result;\r
+ }\r
}\r
\r
final Intent intent = new Intent(mContext, AuthenticatorActivity.class);\r
response);\r
intent.putExtra(KEY_AUTH_TOKEN_TYPE, authTokenType);\r
intent.putExtra(KEY_LOGIN_OPTIONS, options);\r
- intent.putExtra(AuthenticatorActivity.PARAM_USERNAME, account.name);\r
+ intent.putExtra(AuthenticatorActivity.PARAM_USERNAME, account.name); // TODO fix, this will pass the accountName, not the username\r
\r
final Bundle bundle = new Bundle();\r
bundle.putParcelable(AccountManager.KEY_INTENT, intent);\r
\r
private void validateAuthTokenType(String authTokenType)\r
throws UnsupportedAuthTokenTypeException {\r
- if (!authTokenType.equals(AUTH_TOKEN_TYPE)) {\r
+ if (!authTokenType.equals(AUTH_TOKEN_TYPE) &&\r
+ !authTokenType.equals(AUTH_TOKEN_TYPE_PASSWORD) &&\r
+ !authTokenType.equals(AUTH_TOKEN_TYPE_ACCESS_TOKEN) &&\r
+ !authTokenType.equals(AUTH_TOKEN_TYPE_REFRESH_TOKEN) ) {\r
throw new UnsupportedAuthTokenTypeException();\r
}\r
}\r
mPath);
return;
}
- WebdavClient wc = OwnCloudClientUtils.createOwnCloudClient(mOCAccount, mContext);
FileDataStorageManager storageManager = new FileDataStorageManager(mOCAccount, mContext.getContentResolver());
OCFile file = storageManager.getFileByLocalPath(mPath); // a fresh object is needed; many things could have occurred to the file since it was registered to observe
// again, assuming that local files are linked to a remote file AT MOST, SOMETHING TO BE DONE;
true,
true,
mContext);
- RemoteOperationResult result = sfo.execute(wc);
+ RemoteOperationResult result = sfo.execute(mOCAccount, mContext);
if (result.getCode() == ResultCode.SYNC_CONFLICT) {
// ISSUE 5: if the user is not running the app (this is a service!), this can be very intrusive; a notification should be preferred
Intent i = new Intent(mContext, ConflictsResolveActivity.class);
package com.owncloud.android.files.services;\r
\r
import java.io.File;\r
+import java.io.IOException;\r
import java.util.AbstractList;\r
import java.util.Iterator;\r
import java.util.Vector;\r
import com.owncloud.android.ui.fragment.FileDetailFragment;\r
\r
import android.accounts.Account;\r
+import android.accounts.AccountsException;\r
import android.app.Notification;\r
import android.app.NotificationManager;\r
import android.app.PendingIntent;\r
\r
notifyDownloadStart(mCurrentDownload);\r
\r
- /// prepare client object to send the request to the ownCloud server\r
- if (mDownloadClient == null || !mLastAccount.equals(mCurrentDownload.getAccount())) {\r
- mLastAccount = mCurrentDownload.getAccount();\r
- mStorageManager = new FileDataStorageManager(mLastAccount, getContentResolver());\r
- mDownloadClient = OwnCloudClientUtils.createOwnCloudClient(mLastAccount, getApplicationContext());\r
- }\r
-\r
- /// perform the download\r
RemoteOperationResult downloadResult = null;\r
try {\r
- downloadResult = mCurrentDownload.execute(mDownloadClient);\r
+ /// prepare client object to send the request to the ownCloud server\r
+ if (mDownloadClient == null || !mLastAccount.equals(mCurrentDownload.getAccount())) {\r
+ mLastAccount = mCurrentDownload.getAccount();\r
+ mStorageManager = new FileDataStorageManager(mLastAccount, getContentResolver());\r
+ mDownloadClient = OwnCloudClientUtils.createOwnCloudClient(mLastAccount, this);\r
+ }\r
+\r
+ /// perform the download\r
+ if (downloadResult == null) {\r
+ downloadResult = mCurrentDownload.execute(mDownloadClient);\r
+ }\r
if (downloadResult.isSuccess()) {\r
saveDownloadedFile();\r
}\r
\r
+ } catch (AccountsException e) {\r
+ Log.e(TAG, "Error while trying to get autorization for " + mLastAccount.name, e);\r
+ downloadResult = new RemoteOperationResult(e);\r
+ } catch (IOException e) {\r
+ Log.e(TAG, "Error while trying to get autorization for " + mLastAccount.name, e);\r
+ downloadResult = new RemoteOperationResult(e);\r
+ \r
} finally {\r
synchronized(mPendingDownloads) {\r
mPendingDownloads.remove(downloadKey);\r
+++ /dev/null
-/* ownCloud Android client application
- * Copyright (C) 2012 Bartek Przybylski
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- */
-package com.owncloud.android.files.services;
-
-import java.io.File;
-
-import com.owncloud.android.AccountUtils;
-import com.owncloud.android.datamodel.OCFile;
-import com.owncloud.android.network.OwnCloudClientUtils;
-
-import android.accounts.Account;
-import android.content.Context;
-import eu.alefzero.webdav.WebdavClient;
-
-public class FileOperation {
-
- Context mContext;
-
- public FileOperation(Context contex){
- this.mContext = contex;
- }
-
- /**
- * Deletes a file from ownCloud - locally and remote.
- * @param file The file to delete
- * @return True on success, otherwise false
- */
- public boolean delete(OCFile file){
-
- Account account = AccountUtils.getCurrentOwnCloudAccount(mContext);
- WebdavClient client = OwnCloudClientUtils.createOwnCloudClient(account, mContext);
- if(client.deleteFile(file.getRemotePath())){
- File localFile = new File(file.getStoragePath());
- return localFile.delete();
- }
-
- return false;
- }
-
-}
package com.owncloud.android.files.services;
import java.io.File;
+import java.io.IOException;
import java.util.AbstractList;
import java.util.Iterator;
import java.util.Vector;
import com.owncloud.android.datamodel.OCFile;
import com.owncloud.android.files.InstantUploadBroadcastReceiver;
import com.owncloud.android.operations.ChunkedUploadFileOperation;
+import com.owncloud.android.operations.CreateFolderOperation;
+import com.owncloud.android.operations.RemoteOperation;
import com.owncloud.android.operations.RemoteOperationResult;
import com.owncloud.android.operations.UploadFileOperation;
import com.owncloud.android.operations.RemoteOperationResult.ResultCode;
import android.accounts.Account;
import android.accounts.AccountManager;
+import android.accounts.AccountsException;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
notifyUploadStart(mCurrentUpload);
+ RemoteOperationResult uploadResult = null;
- /// prepare client object to send requests to the ownCloud server
- if (mUploadClient == null || !mLastAccount.equals(mCurrentUpload.getAccount())) {
- mLastAccount = mCurrentUpload.getAccount();
- mStorageManager = new FileDataStorageManager(mLastAccount, getContentResolver());
- mUploadClient = OwnCloudClientUtils.createOwnCloudClient(mLastAccount, getApplicationContext());
- }
+ try {
+ /// prepare client object to send requests to the ownCloud server
+ if (mUploadClient == null || !mLastAccount.equals(mCurrentUpload.getAccount())) {
+ mLastAccount = mCurrentUpload.getAccount();
+ mStorageManager = new FileDataStorageManager(mLastAccount, getContentResolver());
+ mUploadClient = OwnCloudClientUtils.createOwnCloudClient(mLastAccount, getApplicationContext());
+ }
- /// create remote folder for instant uploads
- if (mCurrentUpload.isRemoteFolderToBeCreated()) {
- mUploadClient.createDirectory(InstantUploadBroadcastReceiver.INSTANT_UPLOAD_DIR); // ignoring result; fail could just mean that it already exists, but local database is not synchronized; the upload will be tried anyway
- }
+ /// create remote folder for instant uploads
+ if (mCurrentUpload.isRemoteFolderToBeCreated()) {
+ RemoteOperation operation = new CreateFolderOperation( InstantUploadBroadcastReceiver.INSTANT_UPLOAD_DIR,
+ mStorageManager.getFileByPath(OCFile.PATH_SEPARATOR).getFileId(), // TODO generalize this : INSTANT_UPLOAD_DIR could not be a child of root
+ mStorageManager);
+ operation.execute(mUploadClient); // ignoring result; fail could just mean that it already exists, but local database is not synchronized; the upload will be tried anyway
+ }
- /// perform the upload
- RemoteOperationResult uploadResult = null;
- try {
+ /// perform the upload
uploadResult = mCurrentUpload.execute(mUploadClient);
if (uploadResult.isSuccess()) {
saveUploadedFile();
}
+ } catch (AccountsException e) {
+ Log.e(TAG, "Error while trying to get autorization for " + mLastAccount.name, e);
+ uploadResult = new RemoteOperationResult(e);
+
+ } catch (IOException e) {
+ Log.e(TAG, "Error while trying to get autorization for " + mLastAccount.name, e);
+ uploadResult = new RemoteOperationResult(e);
+
} finally {
synchronized(mPendingUploads) {
mPendingUploads.remove(uploadKey);
}
}
-
+
/// notify result
notifyUploadResult(uploadResult, mCurrentUpload);
+++ /dev/null
-/* ownCloud Android client application
- * Copyright (C) 2012 Bartek Przybylski
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- */
-
-package com.owncloud.android.files.services;
-
-import java.util.HashMap;
-import java.util.LinkedList;
-import java.util.List;
-
-import com.owncloud.android.network.OwnCloudClientUtils;
-
-import eu.alefzero.webdav.WebdavClient;
-
-import android.accounts.Account;
-import android.app.Service;
-import android.content.Intent;
-import android.os.IBinder;
-import android.util.Log;
-
-public class InstantUploadService extends Service {
-
- public static String KEY_FILE_PATH = "KEY_FILEPATH";
- public static String KEY_FILE_SIZE = "KEY_FILESIZE";
- public static String KEY_MIME_TYPE = "KEY_MIMETYPE";
- public static String KEY_DISPLAY_NAME = "KEY_FILENAME";
- public static String KEY_ACCOUNT = "KEY_ACCOUNT";
-
- private static String TAG = "InstantUploadService";
- private static String INSTANT_UPLOAD_DIR = "/InstantUpload";
- private UploaderRunnable mUploaderRunnable;
-
- @Override
- public IBinder onBind(Intent arg0) {
- return null;
- }
-
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- if (intent == null ||
- !intent.hasExtra(KEY_ACCOUNT) || !intent.hasExtra(KEY_DISPLAY_NAME) ||
- !intent.hasExtra(KEY_FILE_PATH) || !intent.hasExtra(KEY_FILE_SIZE) ||
- !intent.hasExtra(KEY_MIME_TYPE)) {
- Log.w(TAG, "Not all required information was provided, abording");
- return Service.START_NOT_STICKY;
- }
-
- if (mUploaderRunnable == null) {
- mUploaderRunnable = new UploaderRunnable();
- }
-
- String filename = intent.getStringExtra(KEY_DISPLAY_NAME);
- String filepath = intent.getStringExtra(KEY_FILE_PATH);
- String mimetype = intent.getStringExtra(KEY_MIME_TYPE);
- Account account = intent.getParcelableExtra(KEY_ACCOUNT);
- long filesize = intent.getLongExtra(KEY_FILE_SIZE, -1);
-
- mUploaderRunnable.addElementToQueue(filename, filepath, mimetype, filesize, account);
-
- // starting new thread for new download doesnt seems like a good idea
- // maybe some thread pool or single background thread would be better
- Log.d(TAG, "Starting instant upload thread");
- new Thread(mUploaderRunnable).start();
-
- return Service.START_STICKY;
- }
-
- private class UploaderRunnable implements Runnable {
-
- Object mLock;
- List<HashMap<String, Object>> mHashMapList;
-
- public UploaderRunnable() {
- mHashMapList = new LinkedList<HashMap<String, Object>>();
- mLock = new Object();
- }
-
- public void addElementToQueue(String filename,
- String filepath,
- String mimetype,
- long length,
- Account account) {
- HashMap<String, Object> new_map = new HashMap<String, Object>();
- new_map.put(KEY_ACCOUNT, account);
- new_map.put(KEY_DISPLAY_NAME, filename);
- new_map.put(KEY_FILE_PATH, filepath);
- new_map.put(KEY_MIME_TYPE, mimetype);
- new_map.put(KEY_FILE_SIZE, length);
-
- synchronized (mLock) {
- mHashMapList.add(new_map);
- }
- }
-
- private HashMap<String, Object> getFirstObject() {
- synchronized (mLock) {
- if (mHashMapList.size() == 0)
- return null;
- HashMap<String, Object> ret = mHashMapList.get(0);
- mHashMapList.remove(0);
- return ret;
- }
- }
-
- public void run() {
- HashMap<String, Object> working_map;
-
- while ((working_map = getFirstObject()) != null) {
- Account account = (Account) working_map.get(KEY_ACCOUNT);
- String filename = (String) working_map.get(KEY_DISPLAY_NAME);
- String filepath = (String) working_map.get(KEY_FILE_PATH);
- String mimetype = (String) working_map.get(KEY_MIME_TYPE);
-
- WebdavClient wdc = OwnCloudClientUtils.createOwnCloudClient(account, getApplicationContext());
-
- wdc.createDirectory(INSTANT_UPLOAD_DIR); // fail could just mean that it already exists; put will be tried anyway
- try {
- wdc.putFile(filepath, INSTANT_UPLOAD_DIR + "/" + filename, mimetype);
- } catch (Exception e) {
- // nothing to do; this service is deprecated, indeed
- }
- }
- }
- }
-
-}
--- /dev/null
+/* ownCloud Android client application
+ * Copyright (C) 2012 ownCloud Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+package com.owncloud.android.network;
+
+import java.util.Map;
+
+import org.apache.commons.httpclient.Credentials;
+import org.apache.commons.httpclient.HttpMethod;
+import org.apache.commons.httpclient.auth.AuthChallengeParser;
+import org.apache.commons.httpclient.auth.AuthScheme;
+import org.apache.commons.httpclient.auth.AuthenticationException;
+import org.apache.commons.httpclient.auth.InvalidCredentialsException;
+import org.apache.commons.httpclient.auth.MalformedChallengeException;
+
+import android.util.Log;
+
+/**
+ * Bearer authentication scheme as defined in RFC 6750.
+ *
+ * @author David A. Velasco
+ */
+
+public class BearerAuthScheme implements AuthScheme /*extends RFC2617Scheme*/ {
+
+ private static final String TAG = BearerAuthScheme.class.getSimpleName();
+
+ public static final String AUTH_POLICY = "Bearer";
+
+ /** Whether the bearer authentication process is complete */
+ private boolean mComplete;
+
+ /** Authentication parameter map */
+ private Map mParams = null;
+
+
+ /**
+ * Default constructor for the bearer authentication scheme.
+ */
+ public BearerAuthScheme() {
+ mComplete = false;
+ }
+
+ /**
+ * Constructor for the basic authentication scheme.
+ *
+ * @param challenge Authentication challenge
+ *
+ * @throws MalformedChallengeException Thrown if the authentication challenge is malformed
+ *
+ * @deprecated Use parameterless constructor and {@link AuthScheme#processChallenge(String)} method
+ */
+ public BearerAuthScheme(final String challenge) throws MalformedChallengeException {
+ processChallenge(challenge);
+ mComplete = true;
+ }
+
+ /**
+ * Returns textual designation of the bearer authentication scheme.
+ *
+ * @return "Bearer"
+ */
+ public String getSchemeName() {
+ return "bearer";
+ }
+
+ /**
+ * Processes the Bearer challenge.
+ *
+ * @param challenge The challenge string
+ *
+ * @throws MalformedChallengeException Thrown if the authentication challenge is malformed
+ */
+ public void processChallenge(String challenge) throws MalformedChallengeException {
+ String s = AuthChallengeParser.extractScheme(challenge);
+ if (!s.equalsIgnoreCase(getSchemeName())) {
+ throw new MalformedChallengeException(
+ "Invalid " + getSchemeName() + " challenge: " + challenge);
+ }
+ mParams = AuthChallengeParser.extractParams(challenge);
+ mComplete = true;
+ }
+
+ /**
+ * Tests if the Bearer authentication process has been completed.
+ *
+ * @return 'true' if Bearer authorization has been processed, 'false' otherwise.
+ */
+ public boolean isComplete() {
+ return this.mComplete;
+ }
+
+ /**
+ * Produces bearer authorization string for the given set of
+ * {@link Credentials}.
+ *
+ * @param credentials The set of credentials to be used for authentication
+ * @param method Method name is ignored by the bearer authentication scheme
+ * @param uri URI is ignored by the bearer authentication scheme
+ * @throws InvalidCredentialsException If authentication credentials are not valid or not applicable
+ * for this authentication scheme
+ * @throws AuthenticationException If authorization string cannot be generated due to an authentication failure
+ * @return A bearer authorization string
+ *
+ * @deprecated Use {@link #authenticate(Credentials, HttpMethod)}
+ */
+ public String authenticate(Credentials credentials, String method, String uri) throws AuthenticationException {
+ Log.d(TAG, "enter BearerScheme.authenticate(Credentials, String, String)");
+
+ BearerCredentials bearer = null;
+ try {
+ bearer = (BearerCredentials) credentials;
+ } catch (ClassCastException e) {
+ throw new InvalidCredentialsException(
+ "Credentials cannot be used for bearer authentication: "
+ + credentials.getClass().getName());
+ }
+ return BearerAuthScheme.authenticate(bearer);
+ }
+
+
+ /**
+ * Returns 'false'. Bearer authentication scheme is request based.
+ *
+ * @return 'false'.
+ */
+ public boolean isConnectionBased() {
+ return false;
+ }
+
+ /**
+ * Produces bearer authorization string for the given set of {@link Credentials}.
+ *
+ * @param credentials The set of credentials to be used for authentication
+ * @param method The method being authenticated
+ * @throws InvalidCredentialsException If authentication credentials are not valid or not applicable for this authentication
+ * scheme.
+ * @throws AuthenticationException If authorization string cannot be generated due to an authentication failure.
+ *
+ * @return a basic authorization string
+ */
+ public String authenticate(Credentials credentials, HttpMethod method) throws AuthenticationException {
+ Log.d(TAG, "enter BearerScheme.authenticate(Credentials, HttpMethod)");
+
+ if (method == null) {
+ throw new IllegalArgumentException("Method may not be null");
+ }
+ BearerCredentials bearer = null;
+ try {
+ bearer = (BearerCredentials) credentials;
+ } catch (ClassCastException e) {
+ throw new InvalidCredentialsException(
+ "Credentials cannot be used for bearer authentication: "
+ + credentials.getClass().getName());
+ }
+ return BearerAuthScheme.authenticate(
+ bearer,
+ method.getParams().getCredentialCharset());
+ }
+
+ /**
+ * @deprecated Use {@link #authenticate(BearerCredentials, String)}
+ *
+ * Returns a bearer Authorization header value for the given
+ * {@link BearerCredentials}.
+ *
+ * @param credentials The credentials to encode.
+ *
+ * @return A bearer authorization string
+ */
+ public static String authenticate(BearerCredentials credentials) {
+ return authenticate(credentials, "ISO-8859-1");
+ }
+
+ /**
+ * Returns a bearer Authorization header value for the given
+ * {@link BearerCredentials} and charset.
+ *
+ * @param credentials The credentials to encode.
+ * @param charset The charset to use for encoding the credentials
+ *
+ * @return A bearer authorization string
+ *
+ * @since 3.0
+ */
+ public static String authenticate(BearerCredentials credentials, String charset) {
+ Log.d(TAG, "enter BearerAuthScheme.authenticate(BearerCredentials, String)");
+
+ if (credentials == null) {
+ throw new IllegalArgumentException("Credentials may not be null");
+ }
+ if (charset == null || charset.length() == 0) {
+ throw new IllegalArgumentException("charset may not be null or empty");
+ }
+ StringBuffer buffer = new StringBuffer();
+ buffer.append(credentials.getAccessToken());
+
+ //return "Bearer " + EncodingUtil.getAsciiString(EncodingUtil.getBytes(buffer.toString(), charset));
+ return "Bearer " + buffer.toString();
+ }
+
+ /**
+ * Returns a String identifying the authentication challenge. This is
+ * used, in combination with the host and port to determine if
+ * authorization has already been attempted or not. Schemes which
+ * require multiple requests to complete the authentication should
+ * return a different value for each stage in the request.
+ *
+ * Additionally, the ID should take into account any changes to the
+ * authentication challenge and return a different value when appropriate.
+ * For example when the realm changes in basic authentication it should be
+ * considered a different authentication attempt and a different value should
+ * be returned.
+ *
+ * This method simply returns the realm for the challenge.
+ *
+ * @return String a String identifying the authentication challenge.
+ *
+ * @deprecated no longer used
+ */
+ @Override
+ public String getID() {
+ return getRealm();
+ }
+
+ /**
+ * Returns authentication parameter with the given name, if available.
+ *
+ * @param name The name of the parameter to be returned
+ *
+ * @return The parameter with the given name
+ */
+ @Override
+ public String getParameter(String name) {
+ if (name == null) {
+ throw new IllegalArgumentException("Parameter name may not be null");
+ }
+ if (mParams == null) {
+ return null;
+ }
+ return (String) mParams.get(name.toLowerCase());
+ }
+
+ /**
+ * Returns authentication realm. The realm may not be null.
+ *
+ * @return The authentication realm
+ */
+ @Override
+ public String getRealm() {
+ return getParameter("realm");
+ }
+
+}
--- /dev/null
+/* ownCloud Android client application
+ * Copyright (C) 2012 ownCloud Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+package com.owncloud.android.network;
+
+import org.apache.commons.httpclient.Credentials;
+import org.apache.commons.httpclient.util.LangUtils;
+
+/**
+ * Bearer token {@link Credentials}
+ *
+ * @author David A. Velasco
+ */
+public class BearerCredentials implements Credentials {
+
+
+ private String mAccessToken;
+
+
+ /**
+ * The constructor with the bearer token
+ *
+ * @param token The bearer token
+ */
+ public BearerCredentials(String token) {
+ if (token == null) {
+ throw new IllegalArgumentException("Bearer token may not be null");
+ }
+ mAccessToken = token;
+ }
+
+
+ /**
+ * Returns the access token
+ *
+ * @return The access token
+ */
+ public String getAccessToken() {
+ return mAccessToken;
+ }
+
+
+ /**
+ * Get this object string.
+ *
+ * @return The access token
+ */
+ public String toString() {
+ return mAccessToken;
+ }
+
+ /**
+ * Does a hash of the access token.
+ *
+ * @return The hash code of the access token
+ */
+ public int hashCode() {
+ int hash = LangUtils.HASH_SEED;
+ hash = LangUtils.hashCode(hash, mAccessToken);
+ return hash;
+ }
+
+ /**
+ * These credentials are assumed equal if accessToken is the same.
+ *
+ * @param o The other object to compare with.
+ *
+ * @return 'True' if the object is equivalent.
+ */
+ public boolean equals(Object o) {
+ if (o == null) return false;
+ if (this == o) return true;
+ if (this.getClass().equals(o.getClass())) {
+ BearerCredentials that = (BearerCredentials) o;
+ if (LangUtils.equals(mAccessToken, that.mAccessToken)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+}
+
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
-import java.net.URL;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import android.accounts.Account;
import android.accounts.AccountManager;
+import android.accounts.AuthenticatorException;
+import android.accounts.OperationCanceledException;
import android.content.Context;
import android.net.Uri;
import android.util.Log;
public class OwnCloudClientUtils {
- final private static String TAG = "OwnCloudClientFactory";
+ final private static String TAG = OwnCloudClientUtils.class.getSimpleName();
/** Default timeout for waiting data from the server */
public static final int DEFAULT_DATA_TIMEOUT = 60000;
/**
* Creates a WebdavClient setup for an ownCloud account
*
- * @param account The ownCloud account
- * @param context The application context
- * @return A WebdavClient object ready to be used
+ * Do not call this method from the main thread.
+ *
+ * @param account The ownCloud account
+ * @param appContext Android application context
+ * @return A WebdavClient object ready to be used
+ * @throws AuthenticatorException If the authenticator failed to get the authorization token for the account.
+ * @throws OperationCanceledException If the authenticator operation was cancelled while getting the authorization token for the account.
+ * @throws IOException If there was some I/O error while getting the authorization token for the account.
*/
- public static WebdavClient createOwnCloudClient (Account account, Context context) {
+ public static WebdavClient createOwnCloudClient (Account account, Context appContext) throws OperationCanceledException, AuthenticatorException, IOException {
//Log.d(TAG, "Creating WebdavClient associated to " + account.name);
- Uri uri = Uri.parse(AccountUtils.constructFullURLForAccount(context, account));
- WebdavClient client = createOwnCloudClient(uri, context);
+ Uri uri = Uri.parse(AccountUtils.constructFullURLForAccount(appContext, account));
+ WebdavClient client = createOwnCloudClient(uri, appContext);
+ AccountManager am = AccountManager.get(appContext);
+ if (am.getUserData(account, AccountAuthenticator.KEY_SUPPORTS_OAUTH2) != null) { // TODO avoid a call to getUserData here
+ String accessToken = am.blockingGetAuthToken(account, AccountAuthenticator.AUTH_TOKEN_TYPE_ACCESS_TOKEN, false);
+ client.setBearerCredentials(accessToken); // TODO not assume that the access token is a bearer token
- String username = account.name.substring(0, account.name.lastIndexOf('@'));
- /*if (ama.getUserData(account, AccountAuthenticator.KEY_SUPPORTS_OAUTH2)) {
- // TODO - this is a trap; the OAuth access token shouldn't be saved as the account password
- String accessToken = AccountManager.get(context).getPassword(account);
- client.setCredentials("bearer", accessToken);
-
- } else {*/
- String password = AccountManager.get(context).getPassword(account);
- //String password = am.blockingGetAuthToken(mAccount, AccountAuthenticator.AUTH_TOKEN_TYPE, true);
- client.setCredentials(username, password);
- //}
+ } else {
+ String username = account.name.substring(0, account.name.lastIndexOf('@'));
+ //String password = am.getPassword(account);
+ String password = am.blockingGetAuthToken(account, AccountAuthenticator.AUTH_TOKEN_TYPE_PASSWORD, false);
+ client.setBasicCredentials(username, password);
+ }
return client;
}
WebdavClient client = createOwnCloudClient(uri, context);
- client.setCredentials(username, password);
+ client.setBasicCredentials(username, password);
return client;
}
--- /dev/null
+/* ownCloud Android client application
+ * Copyright (C) 2012 ownCloud Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+package com.owncloud.android.operations;
+
+import org.apache.jackrabbit.webdav.client.methods.MkColMethod;
+
+import com.owncloud.android.datamodel.DataStorageManager;
+import com.owncloud.android.datamodel.OCFile;
+
+import android.util.Log;
+
+import eu.alefzero.webdav.WebdavClient;
+import eu.alefzero.webdav.WebdavUtils;
+
+/**
+ * Remote operation performing the creation of a new folder in the ownCloud server.
+ *
+ * @author David A. Velasco
+ */
+public class CreateFolderOperation extends RemoteOperation {
+
+ private static final String TAG = CreateFolderOperation.class.getSimpleName();
+
+ private static final int READ_TIMEOUT = 10000;
+ private static final int CONNECTION_TIMEOUT = 5000;
+
+ protected String mRemotePath;
+ protected long mParentDirId;
+ protected DataStorageManager mStorageManager;
+
+ /**
+ * Constructor
+ *
+ * @param remoetPath Full path to the new directory to create in the remote server.
+ * @param parentDirId Local database id for the parent folder.
+ * @param storageManager Reference to the local database corresponding to the account where the file is contained.
+ */
+ public CreateFolderOperation(String remotePath, long parentDirId, DataStorageManager storageManager) {
+ mRemotePath = remotePath;
+ mParentDirId = parentDirId;
+ mStorageManager = storageManager;
+ }
+
+
+ /**
+ * Performs the remove operation
+ *
+ * @param client Client object to communicate with the remote ownCloud server.
+ */
+ @Override
+ protected RemoteOperationResult run(WebdavClient client) {
+ RemoteOperationResult result = null;
+ MkColMethod mkcol = null;
+ try {
+ mkcol = new MkColMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath));
+ int status = client.executeMethod(mkcol, READ_TIMEOUT, CONNECTION_TIMEOUT);
+ if (mkcol.succeeded()) {
+ // Save new directory in local database
+ OCFile newDir = new OCFile(mRemotePath);
+ newDir.setMimetype("DIR");
+ newDir.setParentId(mParentDirId);
+ mStorageManager.saveFile(newDir);
+ }
+
+ result = new RemoteOperationResult(mkcol.succeeded(), status);
+ Log.d(TAG, "Create directory " + mRemotePath + ": " + result.getLogMessage());
+ client.exhaustResponse(mkcol.getResponseBodyAsStream());
+
+ } catch (Exception e) {
+ result = new RemoteOperationResult(e);
+ Log.e(TAG, "Create directory " + mRemotePath + ": " + result.getLogMessage(), e);
+
+ } finally {
+ if (mkcol != null)
+ mkcol.releaseConnection();
+ }
+ return result;
+ }
+
+}
&& cm.getActiveNetworkInfo().isConnectedOrConnecting();
}
+
+ public String getAccessToken() {
+ return mAccessToken;
+ }
+
}
*/
package com.owncloud.android.operations;
+import java.io.IOException;
+
+import com.owncloud.android.network.OwnCloudClientUtils;
+
+import android.accounts.Account;
+import android.accounts.AccountsException;
+import android.accounts.AuthenticatorException;
+import android.accounts.OperationCanceledException;
+import android.content.Context;
import android.os.Handler;
+import android.util.Log;
import eu.alefzero.webdav.WebdavClient;
*/
public abstract class RemoteOperation implements Runnable {
- /** Object to interact with the ownCloud server */
+ private static final String TAG = RemoteOperation.class.getSimpleName();
+
+ /** ownCloud account in the remote ownCloud server to operate */
+ private Account mAccount = null;
+
+ /** Android Application context */
+ private Context mContext = null;
+
+ /** Object to interact with the remote server */
private WebdavClient mClient = null;
/** Callback object to notify about the execution of the remote operation */
*/
protected abstract RemoteOperationResult run(WebdavClient client);
+
+ /**
+ * Synchronously executes the remote operation on the received ownCloud account.
+ *
+ * Do not call this method from the main thread.
+ *
+ * This method should be used whenever an ownCloud account is available, instead of {@link #execute(WebdavClient)}.
+ *
+ * @param account ownCloud account in remote ownCloud server to reach during the execution of the operation.
+ * @param context Android context for the component calling the method.
+ * @return Result of the operation.
+ */
+ public final RemoteOperationResult execute(Account account, Context context) {
+ if (account == null)
+ throw new IllegalArgumentException("Trying to execute a remote operation with a NULL Account");
+ if (context == null)
+ throw new IllegalArgumentException("Trying to execute a remote operation with a NULL Context");
+ mAccount = account;
+ mContext = context.getApplicationContext();
+ try {
+ mClient = OwnCloudClientUtils.createOwnCloudClient(mAccount, mContext);
+ } catch (Exception e) {
+ Log.e(TAG, "Error while trying to access to " + mAccount.name, e);
+ return new RemoteOperationResult(e);
+ }
+ return run(mClient);
+ }
+
/**
* Synchronously executes the remote operation
*
+ * Do not call this method from the main thread.
+ *
* @param client Client object to reach an ownCloud server during the execution of the operation.
* @return Result of the operation.
*/
}
+ /**
+ * Asynchronously executes the remote operation
+ *
+ * This method should be used whenever an ownCloud account is available, instead of {@link #execute(WebdavClient)}.
+ *
+ * @param account ownCloud account in remote ownCloud server to reach during the execution of the operation.
+ * @param context Android context for the component calling the method.
+ * @param listener Listener to be notified about the execution of the operation.
+ * @param listenerHandler Handler associated to the thread where the methods of the listener objects must be called.
+ * @return Thread were the remote operation is executed.
+ */
+ public final Thread execute(Account account, Context context, OnRemoteOperationListener listener, Handler listenerHandler) {
+ if (account == null)
+ throw new IllegalArgumentException("Trying to execute a remote operation with a NULL Account");
+ if (context == null)
+ throw new IllegalArgumentException("Trying to execute a remote operation with a NULL Context");
+ mAccount = account;
+ mContext = context.getApplicationContext();
+ mClient = null; // the client instance will be created from mAccount and mContext in the runnerThread to create below
+
+ if (listener == null) {
+ throw new IllegalArgumentException("Trying to execute a remote operation asynchronously without a listener to notiy the result");
+ }
+ mListener = listener;
+
+ if (listenerHandler == null) {
+ throw new IllegalArgumentException("Trying to execute a remote operation asynchronously without a handler to the listener's thread");
+ }
+ mListenerHandler = listenerHandler;
+
+ Thread runnerThread = new Thread(this);
+ runnerThread.start();
+ return runnerThread;
+ }
+
+
/**
* Asynchronously executes the remote operation
*
*/
@Override
public final void run() {
- final RemoteOperationResult result = execute(mClient);
+ RemoteOperationResult result = null;
+ try{
+ if (mClient == null) {
+ if (mAccount != null && mContext != null) {
+ mClient = OwnCloudClientUtils.createOwnCloudClient(mAccount, mContext);
+ } else {
+ throw new IllegalStateException("Trying to run a remote operation asynchronously with no client instance or account");
+ }
+ }
+ result = run(mClient);
+
+ } catch (IOException e) {
+ Log.e(TAG, "Error while trying to access to " + mAccount.name, e);
+ result = new RemoteOperationResult(e);
+
+ } catch (AccountsException e) {
+ Log.e(TAG, "Error while trying to access to " + mAccount.name, e);
+ result = new RemoteOperationResult(e);
+ }
+ final RemoteOperationResult resultToSend = result;
if (mListenerHandler != null && mListener != null) {
mListenerHandler.post(new Runnable() {
@Override
public void run() {
- mListener.onRemoteOperationFinish(RemoteOperation.this, result);
+ mListener.onRemoteOperationFinish(RemoteOperation.this, resultToSend);
}
});
}
*/
public class RenameFileOperation extends RemoteOperation {
- private static final String TAG = RemoveFileOperation.class.getSimpleName();
+ private static final String TAG = RenameFileOperation.class.getSimpleName();
private static final int RENAME_READ_TIMEOUT = 10000;
private static final int RENAME_CONNECTION_TIMEOUT = 5000;
return null;\r
}\r
\r
- protected void initClientForCurrentAccount() throws UnknownHostException {\r
+ protected void initClientForCurrentAccount() throws OperationCanceledException, AuthenticatorException, IOException {\r
if (AccountUtils.constructFullURLForAccount(getContext(), account) == null) {\r
throw new UnknownHostException();\r
}\r
import com.owncloud.android.operations.RemoteOperationResult.ResultCode;\r
import com.owncloud.android.ui.activity.ErrorsWhileCopyingHandlerActivity;\r
import android.accounts.Account;\r
+import android.accounts.AccountsException;\r
+import android.accounts.AuthenticatorException;\r
+import android.accounts.OperationCanceledException;\r
import android.app.Notification;\r
import android.app.NotificationManager;\r
import android.app.PendingIntent;\r
this.setStorageManager(new FileDataStorageManager(account, getContentProvider()));\r
try {\r
this.initClientForCurrentAccount();\r
- } catch (UnknownHostException e) {\r
+ } catch (IOException e) {\r
+ /// the account is unknown for the Synchronization Manager, or unreachable for this context; don't try this again\r
+ mSyncResult.tooManyRetries = true;\r
+ notifyFailedSynchronization();\r
+ return;\r
+ } catch (AccountsException e) {\r
/// the account is unknown for the Synchronization Manager, or unreachable for this context; don't try this again\r
mSyncResult.tooManyRetries = true;\r
notifyFailedSynchronization();\r
\r
URL uri = null;\r
mDiscoveredVersion = mConnChkRunnable.getDiscoveredVersion();\r
- String webdav_path = AccountUtils.getWebdavPath(mDiscoveredVersion);\r
+ String webdav_path = AccountUtils.getWebdavPath(mDiscoveredVersion, false);\r
\r
if (webdav_path == null) {\r
onAuthenticationResult(false, getString(R.string.auth_bad_oc_version_title));\r
findViewById(R.id.buttonOK).setEnabled(mStatusCorrect);\r
}\r
\r
- @Override\r
public void onFocusChange(View view, boolean hasFocus) {\r
if (view.getId() == R.id.host_URL) {\r
if (!hasFocus) {\r
\r
showDialog(DIALOG_LOGIN_PROGRESS);\r
String accessToken = ((GetOAuth2AccessToken)operation).getResultTokenMap().get(OAuth2Context.KEY_ACCESS_TOKEN);\r
- Log.d(TAG, "ACCESS TOKEN: " + accessToken);\r
+ Log.d(TAG, "Got ACCESS TOKEN: " + accessToken);\r
mAuthChkOperation = new ExistenceCheckOperation("", this, accessToken);\r
WebdavClient client = OwnCloudClientUtils.createOwnCloudClient(uri, getApplicationContext());\r
mAuthChkOperation.execute(client, this, mHandler);\r
\r
if (result.isSuccess()) {\r
TextView tv = (TextView) findViewById(R.id.oAuth_URL);\r
- tv.setError("OOOOOKKKKKK");\r
- Log.d(TAG, "OOOOK!!!!");\r
- /**\r
+ Log.d(TAG, "Checked access - time to save the account");\r
+ \r
Uri uri = Uri.parse(mBaseUrl);\r
String username = "OAuth_user" + (new java.util.Random(System.currentTimeMillis())).nextLong(); \r
String accountName = username + "@" + uri.getHost();\r
// TODO - check that accountName does not exist\r
Account account = new Account(accountName, AccountAuthenticator.ACCOUNT_TYPE);\r
AccountManager accManager = AccountManager.get(this);\r
- /// TODO SAVE THE ACCESS TOKEN, HERE OR IN SOME BETTER PLACE\r
- //accManager.addAccountExplicitly(account, mAccesToken, null); //// IS THIS REALLY NEEDED? IS NOT REDUNDANT WITH SETACCOUNTAUTHENTICATORRESULT?\r
+ accManager.addAccountExplicitly(account, "", null); // with our implementation, the password is never input in the app\r
\r
// Add this account as default in the preferences, if there is none\r
Account defaultAccount = AccountUtils.getCurrentOwnCloudAccount(this);\r
final Intent intent = new Intent();\r
intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, AccountAuthenticator.ACCOUNT_TYPE);\r
intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, account.name);\r
- intent.putExtra(AccountManager.KEY_AUTHTOKEN, AccountAuthenticator.ACCOUNT_TYPE);\r
intent.putExtra(AccountManager.KEY_USERDATA, username);\r
- intent.putExtra(AccountManager.KEY_AUTHTOKEN, mAccessToken)\r
\r
+ accManager.setAuthToken(account, AccountAuthenticator.AUTH_TOKEN_TYPE_ACCESS_TOKEN, ((ExistenceCheckOperation) operation).getAccessToken());\r
+ \r
accManager.setUserData(account, AccountAuthenticator.KEY_OC_VERSION, mConnChkRunnable.getDiscoveredVersion().toString());\r
accManager.setUserData(account, AccountAuthenticator.KEY_OC_BASE_URL, mBaseUrl);\r
+ accManager.setUserData(account, AccountAuthenticator.KEY_SUPPORTS_OAUTH2, "TRUE");\r
\r
setAccountAuthenticatorResult(intent.getExtras());\r
setResult(RESULT_OK, intent);\r
ContentResolver.requestSync(account, "org.owncloud", bundle);\r
\r
finish();\r
- */\r
\r
} else { \r
TextView tv = (TextView) findViewById(R.id.oAuth_URL);\r
tv.setError(result.getLogMessage());\r
- Log.d(TAG, "NOOOOO " + result.getLogMessage());\r
+ Log.d(TAG, "Access failed: " + result.getLogMessage());\r
}\r
}\r
}\r
import com.owncloud.android.files.services.FileObserverService;\r
import com.owncloud.android.files.services.FileUploader;\r
import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;\r
-import com.owncloud.android.network.OwnCloudClientUtils;\r
+import com.owncloud.android.operations.CreateFolderOperation;\r
import com.owncloud.android.operations.OnRemoteOperationListener;\r
import com.owncloud.android.operations.RemoteOperation;\r
import com.owncloud.android.operations.RemoteOperationResult;\r
import com.owncloud.android.ui.fragment.OCFileListFragment;\r
\r
import com.owncloud.android.R;\r
-import eu.alefzero.webdav.WebdavClient;\r
\r
/**\r
* Displays, what files the user has available in his ownCloud.\r
private OCFileListFragment mFileList;\r
\r
private boolean mDualPane;\r
+ private Handler mHandler;\r
\r
private static final int DIALOG_SETUP_ACCOUNT = 0;\r
private static final int DIALOG_CREATE_DIR = 1;\r
public void onCreate(Bundle savedInstanceState) {\r
Log.d(getClass().toString(), "onCreate() start");\r
super.onCreate(savedInstanceState);\r
+ \r
+ mHandler = new Handler();\r
\r
/// Load of parameters from received intent\r
mCurrentDir = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_FILE); // no check necessary, mCurrenDir == null if the parameter is not in the intent\r
\r
// Create directory\r
path += directoryName + OCFile.PATH_SEPARATOR;\r
- Thread thread = new Thread(new DirectoryCreator(path, AccountUtils.getCurrentOwnCloudAccount(FileDisplayActivity.this), new Handler()));\r
- thread.start();\r
+ RemoteOperation operation = new CreateFolderOperation(path, mCurrentDir.getFileId(), mStorageManager);\r
+ operation.execute( AccountUtils.getCurrentOwnCloudAccount(FileDisplayActivity.this), \r
+ FileDisplayActivity.this, \r
+ FileDisplayActivity.this, \r
+ mHandler);\r
\r
dialog.dismiss();\r
\r
return !mDirectories.isEmpty();\r
}\r
\r
- private class DirectoryCreator implements Runnable {\r
- private String mTargetPath;\r
- private Account mAccount;\r
- private Handler mHandler; \r
- \r
- public DirectoryCreator(String targetPath, Account account, Handler handler) {\r
- mTargetPath = targetPath;\r
- mAccount = account;\r
- mHandler = handler;\r
- }\r
- \r
- @Override\r
- public void run() {\r
- WebdavClient wdc = OwnCloudClientUtils.createOwnCloudClient(mAccount, getApplicationContext());\r
- boolean created = wdc.createDirectory(mTargetPath);\r
- if (created) {\r
- mHandler.post(new Runnable() {\r
- @Override\r
- public void run() { \r
- dismissDialog(DIALOG_SHORT_WAIT);\r
- \r
- // Save new directory in local database\r
- OCFile newDir = new OCFile(mTargetPath);\r
- newDir.setMimetype("DIR");\r
- newDir.setParentId(mCurrentDir.getFileId());\r
- mStorageManager.saveFile(newDir);\r
- \r
- // Display the new folder right away\r
- mFileList.listDirectory();\r
- }\r
- });\r
- \r
- } else {\r
- mHandler.post(new Runnable() {\r
- @Override\r
- public void run() {\r
- dismissDialog(DIALOG_SHORT_WAIT);\r
- try {\r
- Toast msg = Toast.makeText(FileDisplayActivity.this, R.string.create_dir_fail_msg, Toast.LENGTH_LONG); \r
- msg.show();\r
- \r
- } catch (NotFoundException e) {\r
- Log.e(TAG, "Error while trying to show fail message " , e);\r
- }\r
- }\r
- });\r
- }\r
- }\r
- \r
- }\r
\r
// Custom array adapter to override text colors\r
private class CustomArrayAdapter<T> extends ArrayAdapter<T> {\r
\r
} else if (operation instanceof SynchronizeFileOperation) {\r
onSynchronizeFileOperationFinish((SynchronizeFileOperation)operation, result);\r
+ \r
+ } else if (operation instanceof CreateFolderOperation) {\r
+ onCreateFolderOperationFinish((CreateFolderOperation)operation, result);\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Updates the view associated to the activity after the finish of an operation trying create a new folder\r
+ * \r
+ * @param operation Creation operation performed.\r
+ * @param result Result of the creation.\r
+ */\r
+ private void onCreateFolderOperationFinish(CreateFolderOperation operation, RemoteOperationResult result) {\r
+ if (result.isSuccess()) {\r
+ dismissDialog(DIALOG_SHORT_WAIT);\r
+ mFileList.listDirectory();\r
+ \r
+ } else {\r
+ dismissDialog(DIALOG_SHORT_WAIT);\r
+ try {\r
+ Toast msg = Toast.makeText(FileDisplayActivity.this, R.string.create_dir_fail_msg, Toast.LENGTH_LONG); \r
+ msg.show();\r
+ \r
+ } catch (NotFoundException e) {\r
+ Log.e(TAG, "Error while trying to show fail message " , e);\r
+ }\r
}\r
}\r
\r
\r
} else {\r
mLastRemoteOperation = new SynchronizeFileOperation(mFile, null, mStorageManager, mAccount, true, false, getActivity());\r
- WebdavClient wc = OwnCloudClientUtils.createOwnCloudClient(mAccount, getSherlockActivity().getApplicationContext());\r
- mLastRemoteOperation.execute(wc, this, mHandler);\r
+ mLastRemoteOperation.execute(mAccount, getSherlockActivity(), this, mHandler);\r
\r
// update ui \r
boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;\r
mLastRemoteOperation = new RemoveFileOperation( mFile, \r
true, \r
mStorageManager);\r
- WebdavClient wc = OwnCloudClientUtils.createOwnCloudClient(mAccount, getSherlockActivity().getApplicationContext());\r
- mLastRemoteOperation.execute(wc, this, mHandler);\r
+ mLastRemoteOperation.execute(mAccount, getSherlockActivity(), this, mHandler);\r
\r
boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;\r
getActivity().showDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT);\r
\r
\r
// this is a temporary class for sharing purposes, it need to be replaced in transfer service\r
+ /*\r
@SuppressWarnings("unused")\r
private class ShareRunnable implements Runnable {\r
private String mPath;\r
}\r
}\r
}\r
+ */\r
\r
public void onDismiss(EditNameDialog dialog) {\r
if (dialog.getResult()) {\r
mAccount, \r
newFilename, \r
new FileDataStorageManager(mAccount, getActivity().getContentResolver()));\r
- WebdavClient wc = OwnCloudClientUtils.createOwnCloudClient(mAccount, getSherlockActivity().getApplicationContext());\r
- mLastRemoteOperation.execute(wc, this, mHandler);\r
+ mLastRemoteOperation.execute(mAccount, getSherlockActivity(), this, mHandler);\r
boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;\r
getActivity().showDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT);\r
}\r
case R.id.download_file_item: {
Account account = AccountUtils.getCurrentOwnCloudAccount(getSherlockActivity());
RemoteOperation operation = new SynchronizeFileOperation(mTargetFile, null, mContainerActivity.getStorageManager(), account, true, false, getSherlockActivity());
- WebdavClient wc = OwnCloudClientUtils.createOwnCloudClient(account, getSherlockActivity().getApplicationContext());
- operation.execute(wc, mContainerActivity, mHandler);
+ operation.execute(account, getSherlockActivity(), mContainerActivity, mHandler);
getSherlockActivity().showDialog(FileDisplayActivity.DIALOG_SHORT_WAIT);
return true;
}
AccountUtils.getCurrentOwnCloudAccount(getActivity()),
newFilename,
mContainerActivity.getStorageManager());
- WebdavClient wc = OwnCloudClientUtils.createOwnCloudClient(AccountUtils.getCurrentOwnCloudAccount(getSherlockActivity()), getSherlockActivity().getApplicationContext());
- operation.execute(wc, mContainerActivity, mHandler);
+ operation.execute(AccountUtils.getCurrentOwnCloudAccount(getSherlockActivity()), getSherlockActivity(), mContainerActivity, mHandler);
getActivity().showDialog(FileDisplayActivity.DIALOG_SHORT_WAIT);
}
}
RemoteOperation operation = new RemoveFileOperation( mTargetFile,
true,
mContainerActivity.getStorageManager());
- WebdavClient wc = OwnCloudClientUtils.createOwnCloudClient(AccountUtils.getCurrentOwnCloudAccount(getSherlockActivity()), getSherlockActivity().getApplicationContext());
- operation.execute(wc, mContainerActivity, mHandler);
+ operation.execute(AccountUtils.getCurrentOwnCloudAccount(getSherlockActivity()), getSherlockActivity(), mContainerActivity, mHandler);
getActivity().showDialog(FileDisplayActivity.DIALOG_SHORT_WAIT);
}
import java.io.FileOutputStream;\r
import java.io.IOException;\r
import java.io.InputStream;\r
+import java.util.ArrayList;\r
+import java.util.List;\r
\r
import org.apache.commons.httpclient.Credentials;\r
+import org.apache.commons.httpclient.HostConfiguration;\r
import org.apache.commons.httpclient.HttpClient;\r
import org.apache.commons.httpclient.HttpConnectionManager;\r
import org.apache.commons.httpclient.HttpException;\r
+import org.apache.commons.httpclient.HttpMethod;\r
import org.apache.commons.httpclient.HttpMethodBase;\r
+import org.apache.commons.httpclient.HttpState;\r
import org.apache.commons.httpclient.HttpVersion;\r
import org.apache.commons.httpclient.UsernamePasswordCredentials;\r
+import org.apache.commons.httpclient.auth.AuthPolicy;\r
import org.apache.commons.httpclient.auth.AuthScope;\r
import org.apache.commons.httpclient.methods.GetMethod;\r
import org.apache.commons.httpclient.methods.HeadMethod;\r
import org.apache.http.params.CoreProtocolPNames;\r
import org.apache.jackrabbit.webdav.client.methods.DavMethod;\r
import org.apache.jackrabbit.webdav.client.methods.DeleteMethod;\r
-import org.apache.jackrabbit.webdav.client.methods.MkColMethod;\r
+\r
+import com.owncloud.android.network.BearerAuthScheme;\r
+import com.owncloud.android.network.BearerCredentials;\r
\r
import android.net.Uri;\r
import android.util.Log;\r
getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);\r
}\r
\r
- public void setCredentials(String username, String password) {\r
+ public void setBearerCredentials(String accessToken) {\r
+ AuthPolicy.registerAuthScheme(BearerAuthScheme.AUTH_POLICY, BearerAuthScheme.class);\r
+ \r
+ List<String> authPrefs = new ArrayList<String>(1);\r
+ authPrefs.add(BearerAuthScheme.AUTH_POLICY);\r
+ getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs); \r
+ \r
+ mCredentials = new BearerCredentials(accessToken);\r
+ getState().setCredentials(AuthScope.ANY, mCredentials);\r
+ }\r
+\r
+ public void setBasicCredentials(String username, String password) {\r
+ List<String> authPrefs = new ArrayList<String>(1);\r
+ authPrefs.add(AuthPolicy.BASIC);\r
+ getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs); \r
+ \r
getParams().setAuthenticationPreemptive(true);\r
- getState().setCredentials(AuthScope.ANY,\r
- getCredentials(username, password));\r
+ mCredentials = new UsernamePasswordCredentials(username, password);\r
+ getState().setCredentials(AuthScope.ANY, mCredentials);\r
}\r
\r
- private Credentials getCredentials(String username, String password) {\r
- if (mCredentials == null)\r
- mCredentials = new UsernamePasswordCredentials(username, password);\r
- return mCredentials;\r
- }\r
\r
/**\r
* Downloads a file in remoteFilepath to the local targetPath.\r
return status;\r
}\r
\r
- /**\r
- * Creates a remote directory with the received path.\r
- * \r
- * @param path Path of the directory to create, URL DECODED\r
- * @return 'True' when the directory is successfully created\r
- */\r
- public boolean createDirectory(String path) {\r
- boolean result = false;\r
- int status = -1;\r
- MkColMethod mkcol = new MkColMethod(mUri.toString() + WebdavUtils.encodePath(path));\r
- try {\r
- Log.d(TAG, "Creating directory " + path);\r
- status = executeMethod(mkcol);\r
- Log.d(TAG, "Status returned: " + status);\r
- result = mkcol.succeeded();\r
- \r
- Log.d(TAG, "MKCOL to " + path + " finished with HTTP status " + status + (!result?"(FAIL)":""));\r
- exhaustResponse(mkcol.getResponseBodyAsStream());\r
- \r
- } catch (Exception e) {\r
- logException(e, "creating directory " + path);\r
- \r
- } finally {\r
- mkcol.releaseConnection(); // let the connection available for other methods\r
- }\r
- return result;\r
- }\r
- \r
\r
/**\r
* Check if a file exists in the OC server\r
public Uri getBaseUri() {\r
return mUri;\r
}\r
+ \r
\r
+ @Override\r
+ public int executeMethod(HostConfiguration hostconfig, final HttpMethod method, final HttpState state) throws IOException, HttpException {\r
+ if (mCredentials instanceof BearerAuthScheme) {\r
+ method.getHostAuthState().setAuthScheme(AuthPolicy.getAuthScheme(BearerAuthScheme.AUTH_POLICY));\r
+ }\r
+ return super.executeMethod(hostconfig, method, state);\r
+ }\r
}\r