diff --git a/src/com/owncloud/android/lib/accounts/AccountUtils.java b/src/com/owncloud/android/lib/accounts/AccountUtils.java index 79983814..fbb840cf 100644 --- a/src/com/owncloud/android/lib/accounts/AccountUtils.java +++ b/src/com/owncloud/android/lib/accounts/AccountUtils.java @@ -92,6 +92,23 @@ public class AccountUtils { return baseurl + webdavpath; } + /** + * Extracts url server from the account + * @param context + * @param account + * @return url server or null on failure + * @throws AccountNotFoundException When 'account' is unknown for the AccountManager + */ + public static String constructBasicURLForAccount(Context context, Account account) throws AccountNotFoundException { + AccountManager ama = AccountManager.get(context); + String baseurl = ama.getUserData(account, OwnCloudAccount.Constants.KEY_OC_BASE_URL); + + if (baseurl == null ) + throw new AccountNotFoundException(account, "Account not found", null); + + return baseurl; + } + public static class AccountNotFoundException extends AccountsException { diff --git a/src/com/owncloud/android/lib/network/OwnCloudClient.java b/src/com/owncloud/android/lib/network/OwnCloudClient.java index 9ec8867f..2e8fb3d2 100644 --- a/src/com/owncloud/android/lib/network/OwnCloudClient.java +++ b/src/com/owncloud/android/lib/network/OwnCloudClient.java @@ -57,6 +57,7 @@ public class OwnCloudClient extends HttpClient { private static final int MAX_REDIRECTIONS_COUNT = 3; private Uri mUri; + private Uri mWebdavUri; private Credentials mCredentials; private boolean mFollowRedirects; private String mSsoSessionCookie; @@ -117,7 +118,7 @@ public class OwnCloudClient extends HttpClient { * @throws Exception When the existence could not be determined */ public boolean existsFile(String path) throws IOException, HttpException { - HeadMethod head = new HeadMethod(mUri.toString() + WebdavUtils.encodePath(path)); + HeadMethod head = new HeadMethod(mWebdavUri.toString() + WebdavUtils.encodePath(path)); try { int status = executeMethod(head); Log.d(TAG, "HEAD to " + path + " finished with HTTP status " + status + ((status != HttpStatus.SC_OK)?"(FAIL)":"")); @@ -224,6 +225,18 @@ public class OwnCloudClient extends HttpClient { getHttpConnectionManager().getParams().setConnectionTimeout(defaultConnectionTimeout); } + /** + * Sets the Webdav URI for the helper methods that receive paths as parameters, instead of full URLs + * @param uri + */ + public void setWebdavUri(Uri uri) { + mWebdavUri = uri; + } + + public Uri getWebdavUri() { + return mWebdavUri; + } + /** * Sets the base URI for the helper methods that receive paths as parameters, instead of full URLs * @param uri diff --git a/src/com/owncloud/android/lib/network/OwnCloudClientFactory.java b/src/com/owncloud/android/lib/network/OwnCloudClientFactory.java index daea819c..a41f5528 100644 --- a/src/com/owncloud/android/lib/network/OwnCloudClientFactory.java +++ b/src/com/owncloud/android/lib/network/OwnCloudClientFactory.java @@ -70,11 +70,14 @@ public class OwnCloudClientFactory { public static OwnCloudClient createOwnCloudClient (Account account, Context appContext) throws OperationCanceledException, AuthenticatorException, IOException, AccountNotFoundException { //Log_OC.d(TAG, "Creating OwnCloudClient associated to " + account.name); - Uri uri = Uri.parse(AccountUtils.constructFullURLForAccount(appContext, account)); + Uri webdavUri = Uri.parse(AccountUtils.constructFullURLForAccount(appContext, account)); + Uri uri = Uri.parse(AccountUtils.constructBasicURLForAccount(appContext, account)); AccountManager am = AccountManager.get(appContext); boolean isOauth2 = am.getUserData(account, OwnCloudAccount.Constants.KEY_SUPPORTS_OAUTH2) != null; // TODO avoid calling to getUserData here boolean isSamlSso = am.getUserData(account, OwnCloudAccount.Constants.KEY_SUPPORTS_SAML_WEB_SSO) != null; - OwnCloudClient client = createOwnCloudClient(uri, appContext, !isSamlSso); + OwnCloudClient client = createOwnCloudClient(webdavUri, appContext, !isSamlSso); + client.setBaseUri(uri); + if (isOauth2) { String accessToken = am.blockingGetAuthToken(account, AccountTypeUtils.getAuthTokenTypeAccessToken(account.type), false); client.setBearerCredentials(accessToken); // TODO not assume that the access token is a bearer token @@ -95,11 +98,13 @@ public class OwnCloudClientFactory { public static OwnCloudClient createOwnCloudClient (Account account, Context appContext, Activity currentActivity) throws OperationCanceledException, AuthenticatorException, IOException, AccountNotFoundException { - Uri uri = Uri.parse(AccountUtils.constructFullURLForAccount(appContext, account)); + Uri webdavUri = Uri.parse(AccountUtils.constructFullURLForAccount(appContext, account)); + Uri uri = Uri.parse(AccountUtils.constructBasicURLForAccount(appContext, account)); AccountManager am = AccountManager.get(appContext); boolean isOauth2 = am.getUserData(account, OwnCloudAccount.Constants.KEY_SUPPORTS_OAUTH2) != null; // TODO avoid calling to getUserData here boolean isSamlSso = am.getUserData(account, OwnCloudAccount.Constants.KEY_SUPPORTS_SAML_WEB_SSO) != null; - OwnCloudClient client = createOwnCloudClient(uri, appContext, !isSamlSso); + OwnCloudClient client = createOwnCloudClient(webdavUri, appContext, !isSamlSso); + client.setBaseUri(uri); if (isOauth2) { // TODO avoid a call to getUserData here AccountManagerFuture future = am.getAuthToken(account, AccountTypeUtils.getAuthTokenTypeAccessToken(account.type), null, currentActivity, null, null); @@ -148,7 +153,7 @@ public class OwnCloudClientFactory { OwnCloudClient client = new OwnCloudClient(NetworkUtils.getMultiThreadedConnManager()); client.setDefaultTimeouts(DEFAULT_DATA_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT); - client.setBaseUri(uri); + client.setWebdavUri(uri); client.setFollowRedirects(followRedirects); return client; diff --git a/src/com/owncloud/android/lib/operations/remote/ChunkedUploadRemoteFileOperation.java b/src/com/owncloud/android/lib/operations/remote/ChunkedUploadRemoteFileOperation.java index 9fde7c0f..51e2bfcd 100644 --- a/src/com/owncloud/android/lib/operations/remote/ChunkedUploadRemoteFileOperation.java +++ b/src/com/owncloud/android/lib/operations/remote/ChunkedUploadRemoteFileOperation.java @@ -70,7 +70,7 @@ public class ChunkedUploadRemoteFileOperation extends UploadRemoteFileOperation } long offset = 0; - String uriPrefix = client.getBaseUri() + WebdavUtils.encodePath(mRemotePath) + "-chunking-" + Math.abs((new Random()).nextInt(9000)+1000) + "-" ; + String uriPrefix = client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath) + "-chunking-" + Math.abs((new Random()).nextInt(9000)+1000) + "-" ; long chunkCount = (long) Math.ceil((double)file.length() / CHUNK_SIZE); for (int chunkIndex = 0; chunkIndex < chunkCount ; chunkIndex++, offset += CHUNK_SIZE) { if (mPutMethod != null) { diff --git a/src/com/owncloud/android/lib/operations/remote/CreateRemoteFolderOperation.java b/src/com/owncloud/android/lib/operations/remote/CreateRemoteFolderOperation.java index f0a325ed..c2389e9b 100644 --- a/src/com/owncloud/android/lib/operations/remote/CreateRemoteFolderOperation.java +++ b/src/com/owncloud/android/lib/operations/remote/CreateRemoteFolderOperation.java @@ -80,7 +80,7 @@ public class CreateRemoteFolderOperation extends RemoteOperation { boolean noInvalidChars = FileUtils.isValidPath(mRemotePath); if (noInvalidChars) { try { - mkcol = new MkColMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath)); + mkcol = new MkColMethod(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath)); int status = client.executeMethod(mkcol, READ_TIMEOUT, CONNECTION_TIMEOUT); if (!mkcol.succeeded() && mkcol.getStatusCode() == HttpStatus.SC_CONFLICT && mCreateFullPath) { result = createParentFolder(FileUtils.getParentPath(mRemotePath), client); diff --git a/src/com/owncloud/android/lib/operations/remote/DownloadRemoteFileOperation.java b/src/com/owncloud/android/lib/operations/remote/DownloadRemoteFileOperation.java index a964abc4..12e84765 100644 --- a/src/com/owncloud/android/lib/operations/remote/DownloadRemoteFileOperation.java +++ b/src/com/owncloud/android/lib/operations/remote/DownloadRemoteFileOperation.java @@ -98,7 +98,7 @@ public class DownloadRemoteFileOperation extends RemoteOperation { protected int downloadFile(OwnCloudClient client, File targetFile) throws HttpException, IOException, OperationCancelledException { int status = -1; boolean savedFile = false; - mGet = new GetMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath)); + mGet = new GetMethod(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath)); Iterator it = null; FileOutputStream fos = null; diff --git a/src/com/owncloud/android/lib/operations/remote/ExistenceCheckRemoteOperation.java b/src/com/owncloud/android/lib/operations/remote/ExistenceCheckRemoteOperation.java index d4b49f91..8406fb0b 100644 --- a/src/com/owncloud/android/lib/operations/remote/ExistenceCheckRemoteOperation.java +++ b/src/com/owncloud/android/lib/operations/remote/ExistenceCheckRemoteOperation.java @@ -75,16 +75,16 @@ public class ExistenceCheckRemoteOperation extends RemoteOperation { RemoteOperationResult result = null; HeadMethod head = null; try { - head = new HeadMethod(client.getBaseUri() + WebdavUtils.encodePath(mPath)); + head = new HeadMethod(client.getWebdavUri() + WebdavUtils.encodePath(mPath)); int status = client.executeMethod(head, TIMEOUT, TIMEOUT); client.exhaustResponse(head.getResponseBodyAsStream()); boolean success = (status == HttpStatus.SC_OK && !mSuccessIfAbsent) || (status == HttpStatus.SC_NOT_FOUND && mSuccessIfAbsent); result = new RemoteOperationResult(success, status, head.getResponseHeaders()); - Log.d(TAG, "Existence check for " + client.getBaseUri() + WebdavUtils.encodePath(mPath) + " targeting for " + (mSuccessIfAbsent ? " absence " : " existence ") + "finished with HTTP status " + status + (!success?"(FAIL)":"")); + Log.d(TAG, "Existence check for " + client.getWebdavUri() + WebdavUtils.encodePath(mPath) + " targeting for " + (mSuccessIfAbsent ? " absence " : " existence ") + "finished with HTTP status " + status + (!success?"(FAIL)":"")); } catch (Exception e) { result = new RemoteOperationResult(e); - Log.e(TAG, "Existence check for " + client.getBaseUri() + WebdavUtils.encodePath(mPath) + " targeting for " + (mSuccessIfAbsent ? " absence " : " existence ") + ": " + result.getLogMessage(), result.getException()); + Log.e(TAG, "Existence check for " + client.getWebdavUri() + WebdavUtils.encodePath(mPath) + " targeting for " + (mSuccessIfAbsent ? " absence " : " existence ") + ": " + result.getLogMessage(), result.getException()); } finally { if (head != null) diff --git a/src/com/owncloud/android/lib/operations/remote/GetUserNameRemoteOperation.java b/src/com/owncloud/android/lib/operations/remote/GetUserNameRemoteOperation.java index 9f5c0ff6..be76976f 100644 --- a/src/com/owncloud/android/lib/operations/remote/GetUserNameRemoteOperation.java +++ b/src/com/owncloud/android/lib/operations/remote/GetUserNameRemoteOperation.java @@ -75,8 +75,8 @@ public class GetUserNameRemoteOperation extends RemoteOperation { int status = -1; // Get Method - GetMethod get = new GetMethod(client.getBaseUri() + OCS_ROUTE); - Log.d(TAG, "URL ------> " + client.getBaseUri() + OCS_ROUTE); + GetMethod get = new GetMethod(client.getWebdavUri() + OCS_ROUTE); + Log.d(TAG, "URL ------> " + client.getWebdavUri() + OCS_ROUTE); // Add the Header get.addRequestHeader(HEADER_OCS_API, HEADER_OCS_API_VALUE); diff --git a/src/com/owncloud/android/lib/operations/remote/OwnCloudServerCheckOperation.java b/src/com/owncloud/android/lib/operations/remote/OwnCloudServerCheckOperation.java index 16708194..15e54a3e 100644 --- a/src/com/owncloud/android/lib/operations/remote/OwnCloudServerCheckOperation.java +++ b/src/com/owncloud/android/lib/operations/remote/OwnCloudServerCheckOperation.java @@ -158,11 +158,11 @@ public class OwnCloudServerCheckOperation extends RemoteOperation { tryConnection(client, mUrl + AccountUtils.STATUS_PATH); } else { - client.setBaseUri(Uri.parse("https://" + mUrl + AccountUtils.STATUS_PATH)); + client.setWebdavUri(Uri.parse("https://" + mUrl + AccountUtils.STATUS_PATH)); boolean httpsSuccess = tryConnection(client, "https://" + mUrl + AccountUtils.STATUS_PATH); if (!httpsSuccess && !mLatestResult.isSslRecoverableException()) { Log.d(TAG, "establishing secure connection failed, trying non secure connection"); - client.setBaseUri(Uri.parse("http://" + mUrl + AccountUtils.STATUS_PATH)); + client.setWebdavUri(Uri.parse("http://" + mUrl + AccountUtils.STATUS_PATH)); tryConnection(client, "http://" + mUrl + AccountUtils.STATUS_PATH); } } diff --git a/src/com/owncloud/android/lib/operations/remote/ReadRemoteFileOperation.java b/src/com/owncloud/android/lib/operations/remote/ReadRemoteFileOperation.java index e0017dc6..631b5d11 100644 --- a/src/com/owncloud/android/lib/operations/remote/ReadRemoteFileOperation.java +++ b/src/com/owncloud/android/lib/operations/remote/ReadRemoteFileOperation.java @@ -77,7 +77,7 @@ public class ReadRemoteFileOperation extends RemoteOperation { /// take the duty of check the server for the current state of the file there try { - propfind = new PropFindMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath), + propfind = new PropFindMethod(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath), DavConstants.PROPFIND_ALL_PROP, DavConstants.DEPTH_0); int status; @@ -87,7 +87,7 @@ public class ReadRemoteFileOperation extends RemoteOperation { if (isMultiStatus) { // Parse response MultiStatus resp = propfind.getResponseBodyAsMultiStatus(); - WebdavEntry we = new WebdavEntry(resp.getResponses()[0], client.getBaseUri().getPath()); + WebdavEntry we = new WebdavEntry(resp.getResponses()[0], client.getWebdavUri().getPath()); RemoteFile remoteFile = new RemoteFile(we); ArrayList files = new ArrayList(); files.add(remoteFile); diff --git a/src/com/owncloud/android/lib/operations/remote/ReadRemoteFolderOperation.java b/src/com/owncloud/android/lib/operations/remote/ReadRemoteFolderOperation.java index cb9551ad..4dfb04b2 100644 --- a/src/com/owncloud/android/lib/operations/remote/ReadRemoteFolderOperation.java +++ b/src/com/owncloud/android/lib/operations/remote/ReadRemoteFolderOperation.java @@ -75,7 +75,7 @@ public class ReadRemoteFolderOperation extends RemoteOperation { try { // remote request - query = new PropFindMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath), + query = new PropFindMethod(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath), DavConstants.PROPFIND_ALL_PROP, DavConstants.DEPTH_1); int status = client.executeMethod(query); @@ -137,14 +137,14 @@ public class ReadRemoteFolderOperation extends RemoteOperation { mFolderAndFiles = new ArrayList(); // parse data from remote folder - WebdavEntry we = new WebdavEntry(dataInServer.getResponses()[0], client.getBaseUri().getPath()); + WebdavEntry we = new WebdavEntry(dataInServer.getResponses()[0], client.getWebdavUri().getPath()); mFolderAndFiles.add(fillOCFile(we)); // loop to update every child RemoteFile remoteFile = null; for (int i = 1; i < dataInServer.getResponses().length; ++i) { /// new OCFile instance with the data from the server - we = new WebdavEntry(dataInServer.getResponses()[i], client.getBaseUri().getPath()); + we = new WebdavEntry(dataInServer.getResponses()[i], client.getWebdavUri().getPath()); remoteFile = fillOCFile(we); mFolderAndFiles.add(remoteFile); } diff --git a/src/com/owncloud/android/lib/operations/remote/RemoveRemoteFileOperation.java b/src/com/owncloud/android/lib/operations/remote/RemoveRemoteFileOperation.java index b80fa526..2677a981 100644 --- a/src/com/owncloud/android/lib/operations/remote/RemoveRemoteFileOperation.java +++ b/src/com/owncloud/android/lib/operations/remote/RemoveRemoteFileOperation.java @@ -68,7 +68,7 @@ public class RemoveRemoteFileOperation extends RemoteOperation { DeleteMethod delete = null; try { - delete = new DeleteMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath)); + delete = new DeleteMethod(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath)); int status = client.executeMethod(delete, REMOVE_READ_TIMEOUT, REMOVE_CONNECTION_TIMEOUT); delete.getResponseBodyAsString(); // exhaust the response, although not interesting diff --git a/src/com/owncloud/android/lib/operations/remote/RenameRemoteFileOperation.java b/src/com/owncloud/android/lib/operations/remote/RenameRemoteFileOperation.java index 7bc5f0cc..d4fa7c89 100644 --- a/src/com/owncloud/android/lib/operations/remote/RenameRemoteFileOperation.java +++ b/src/com/owncloud/android/lib/operations/remote/RenameRemoteFileOperation.java @@ -104,8 +104,8 @@ public class RenameRemoteFileOperation extends RemoteOperation { return new RemoteOperationResult(ResultCode.INVALID_OVERWRITE); } - move = new LocalMoveMethod( client.getBaseUri() + WebdavUtils.encodePath(mOldRemotePath), - client.getBaseUri() + WebdavUtils.encodePath(mNewRemotePath)); + move = new LocalMoveMethod( client.getWebdavUri() + WebdavUtils.encodePath(mOldRemotePath), + client.getWebdavUri() + WebdavUtils.encodePath(mNewRemotePath)); int status = client.executeMethod(move, RENAME_READ_TIMEOUT, RENAME_CONNECTION_TIMEOUT); move.getResponseBodyAsString(); // exhaust response, although not interesting diff --git a/src/com/owncloud/android/lib/operations/remote/UploadRemoteFileOperation.java b/src/com/owncloud/android/lib/operations/remote/UploadRemoteFileOperation.java index 4872aaf1..c15350df 100644 --- a/src/com/owncloud/android/lib/operations/remote/UploadRemoteFileOperation.java +++ b/src/com/owncloud/android/lib/operations/remote/UploadRemoteFileOperation.java @@ -80,7 +80,7 @@ public class UploadRemoteFileOperation extends RemoteOperation { if (mCancellationRequested.get()) { throw new OperationCancelledException(); } else { - mPutMethod = new PutMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath)); + mPutMethod = new PutMethod(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath)); } }