1
0
mirror of https://github.com/owncloud/android-library.git synced 2025-06-08 00:16:09 +00:00

OC-2746: Changes from comments: new method to construct base url server

This commit is contained in:
masensio 2014-01-28 14:39:36 +01:00
parent 79995d6522
commit b4d67521b2
14 changed files with 60 additions and 25 deletions

View File

@ -92,6 +92,23 @@ public class AccountUtils {
return baseurl + webdavpath; 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 { public static class AccountNotFoundException extends AccountsException {

View File

@ -57,6 +57,7 @@ public class OwnCloudClient extends HttpClient {
private static final int MAX_REDIRECTIONS_COUNT = 3; private static final int MAX_REDIRECTIONS_COUNT = 3;
private Uri mUri; private Uri mUri;
private Uri mWebdavUri;
private Credentials mCredentials; private Credentials mCredentials;
private boolean mFollowRedirects; private boolean mFollowRedirects;
private String mSsoSessionCookie; private String mSsoSessionCookie;
@ -117,7 +118,7 @@ public class OwnCloudClient extends HttpClient {
* @throws Exception When the existence could not be determined * @throws Exception When the existence could not be determined
*/ */
public boolean existsFile(String path) throws IOException, HttpException { 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 { try {
int status = executeMethod(head); int status = executeMethod(head);
Log.d(TAG, "HEAD to " + path + " finished with HTTP status " + status + ((status != HttpStatus.SC_OK)?"(FAIL)":"")); 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); 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 * Sets the base URI for the helper methods that receive paths as parameters, instead of full URLs
* @param uri * @param uri

View File

@ -70,11 +70,14 @@ public class OwnCloudClientFactory {
public static OwnCloudClient createOwnCloudClient (Account account, Context appContext) throws OperationCanceledException, AuthenticatorException, IOException, AccountNotFoundException { public static OwnCloudClient createOwnCloudClient (Account account, Context appContext) throws OperationCanceledException, AuthenticatorException, IOException, AccountNotFoundException {
//Log_OC.d(TAG, "Creating OwnCloudClient associated to " + account.name); //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); AccountManager am = AccountManager.get(appContext);
boolean isOauth2 = am.getUserData(account, OwnCloudAccount.Constants.KEY_SUPPORTS_OAUTH2) != null; // TODO avoid calling to getUserData here 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; 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) { if (isOauth2) {
String accessToken = am.blockingGetAuthToken(account, AccountTypeUtils.getAuthTokenTypeAccessToken(account.type), false); String accessToken = am.blockingGetAuthToken(account, AccountTypeUtils.getAuthTokenTypeAccessToken(account.type), false);
client.setBearerCredentials(accessToken); // TODO not assume that the access token is a bearer token 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 { 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); AccountManager am = AccountManager.get(appContext);
boolean isOauth2 = am.getUserData(account, OwnCloudAccount.Constants.KEY_SUPPORTS_OAUTH2) != null; // TODO avoid calling to getUserData here 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; 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 if (isOauth2) { // TODO avoid a call to getUserData here
AccountManagerFuture<Bundle> future = am.getAuthToken(account, AccountTypeUtils.getAuthTokenTypeAccessToken(account.type), null, currentActivity, null, null); AccountManagerFuture<Bundle> 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()); OwnCloudClient client = new OwnCloudClient(NetworkUtils.getMultiThreadedConnManager());
client.setDefaultTimeouts(DEFAULT_DATA_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT); client.setDefaultTimeouts(DEFAULT_DATA_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT);
client.setBaseUri(uri); client.setWebdavUri(uri);
client.setFollowRedirects(followRedirects); client.setFollowRedirects(followRedirects);
return client; return client;

View File

@ -70,7 +70,7 @@ public class ChunkedUploadRemoteFileOperation extends UploadRemoteFileOperation
} }
long offset = 0; 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); long chunkCount = (long) Math.ceil((double)file.length() / CHUNK_SIZE);
for (int chunkIndex = 0; chunkIndex < chunkCount ; chunkIndex++, offset += CHUNK_SIZE) { for (int chunkIndex = 0; chunkIndex < chunkCount ; chunkIndex++, offset += CHUNK_SIZE) {
if (mPutMethod != null) { if (mPutMethod != null) {

View File

@ -80,7 +80,7 @@ public class CreateRemoteFolderOperation extends RemoteOperation {
boolean noInvalidChars = FileUtils.isValidPath(mRemotePath); boolean noInvalidChars = FileUtils.isValidPath(mRemotePath);
if (noInvalidChars) { if (noInvalidChars) {
try { 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); int status = client.executeMethod(mkcol, READ_TIMEOUT, CONNECTION_TIMEOUT);
if (!mkcol.succeeded() && mkcol.getStatusCode() == HttpStatus.SC_CONFLICT && mCreateFullPath) { if (!mkcol.succeeded() && mkcol.getStatusCode() == HttpStatus.SC_CONFLICT && mCreateFullPath) {
result = createParentFolder(FileUtils.getParentPath(mRemotePath), client); result = createParentFolder(FileUtils.getParentPath(mRemotePath), client);

View File

@ -98,7 +98,7 @@ public class DownloadRemoteFileOperation extends RemoteOperation {
protected int downloadFile(OwnCloudClient client, File targetFile) throws HttpException, IOException, OperationCancelledException { protected int downloadFile(OwnCloudClient client, File targetFile) throws HttpException, IOException, OperationCancelledException {
int status = -1; int status = -1;
boolean savedFile = false; boolean savedFile = false;
mGet = new GetMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath)); mGet = new GetMethod(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath));
Iterator<OnDatatransferProgressListener> it = null; Iterator<OnDatatransferProgressListener> it = null;
FileOutputStream fos = null; FileOutputStream fos = null;

View File

@ -75,16 +75,16 @@ public class ExistenceCheckRemoteOperation extends RemoteOperation {
RemoteOperationResult result = null; RemoteOperationResult result = null;
HeadMethod head = null; HeadMethod head = null;
try { try {
head = new HeadMethod(client.getBaseUri() + WebdavUtils.encodePath(mPath)); head = new HeadMethod(client.getWebdavUri() + WebdavUtils.encodePath(mPath));
int status = client.executeMethod(head, TIMEOUT, TIMEOUT); int status = client.executeMethod(head, TIMEOUT, TIMEOUT);
client.exhaustResponse(head.getResponseBodyAsStream()); client.exhaustResponse(head.getResponseBodyAsStream());
boolean success = (status == HttpStatus.SC_OK && !mSuccessIfAbsent) || (status == HttpStatus.SC_NOT_FOUND && mSuccessIfAbsent); boolean success = (status == HttpStatus.SC_OK && !mSuccessIfAbsent) || (status == HttpStatus.SC_NOT_FOUND && mSuccessIfAbsent);
result = new RemoteOperationResult(success, status, head.getResponseHeaders()); 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) { } catch (Exception e) {
result = new RemoteOperationResult(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 { } finally {
if (head != null) if (head != null)

View File

@ -75,8 +75,8 @@ public class GetUserNameRemoteOperation extends RemoteOperation {
int status = -1; int status = -1;
// Get Method // Get Method
GetMethod get = new GetMethod(client.getBaseUri() + OCS_ROUTE); GetMethod get = new GetMethod(client.getWebdavUri() + OCS_ROUTE);
Log.d(TAG, "URL ------> " + client.getBaseUri() + OCS_ROUTE); Log.d(TAG, "URL ------> " + client.getWebdavUri() + OCS_ROUTE);
// Add the Header // Add the Header
get.addRequestHeader(HEADER_OCS_API, HEADER_OCS_API_VALUE); get.addRequestHeader(HEADER_OCS_API, HEADER_OCS_API_VALUE);

View File

@ -158,11 +158,11 @@ public class OwnCloudServerCheckOperation extends RemoteOperation {
tryConnection(client, mUrl + AccountUtils.STATUS_PATH); tryConnection(client, mUrl + AccountUtils.STATUS_PATH);
} else { } 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); boolean httpsSuccess = tryConnection(client, "https://" + mUrl + AccountUtils.STATUS_PATH);
if (!httpsSuccess && !mLatestResult.isSslRecoverableException()) { if (!httpsSuccess && !mLatestResult.isSslRecoverableException()) {
Log.d(TAG, "establishing secure connection failed, trying non secure connection"); 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); tryConnection(client, "http://" + mUrl + AccountUtils.STATUS_PATH);
} }
} }

View File

@ -77,7 +77,7 @@ public class ReadRemoteFileOperation extends RemoteOperation {
/// take the duty of check the server for the current state of the file there /// take the duty of check the server for the current state of the file there
try { try {
propfind = new PropFindMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath), propfind = new PropFindMethod(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath),
DavConstants.PROPFIND_ALL_PROP, DavConstants.PROPFIND_ALL_PROP,
DavConstants.DEPTH_0); DavConstants.DEPTH_0);
int status; int status;
@ -87,7 +87,7 @@ public class ReadRemoteFileOperation extends RemoteOperation {
if (isMultiStatus) { if (isMultiStatus) {
// Parse response // Parse response
MultiStatus resp = propfind.getResponseBodyAsMultiStatus(); 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); RemoteFile remoteFile = new RemoteFile(we);
ArrayList<Object> files = new ArrayList<Object>(); ArrayList<Object> files = new ArrayList<Object>();
files.add(remoteFile); files.add(remoteFile);

View File

@ -75,7 +75,7 @@ public class ReadRemoteFolderOperation extends RemoteOperation {
try { try {
// remote request // remote request
query = new PropFindMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath), query = new PropFindMethod(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath),
DavConstants.PROPFIND_ALL_PROP, DavConstants.PROPFIND_ALL_PROP,
DavConstants.DEPTH_1); DavConstants.DEPTH_1);
int status = client.executeMethod(query); int status = client.executeMethod(query);
@ -137,14 +137,14 @@ public class ReadRemoteFolderOperation extends RemoteOperation {
mFolderAndFiles = new ArrayList<Object>(); mFolderAndFiles = new ArrayList<Object>();
// parse data from remote folder // 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)); mFolderAndFiles.add(fillOCFile(we));
// loop to update every child // loop to update every child
RemoteFile remoteFile = null; RemoteFile remoteFile = null;
for (int i = 1; i < dataInServer.getResponses().length; ++i) { for (int i = 1; i < dataInServer.getResponses().length; ++i) {
/// new OCFile instance with the data from the server /// 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); remoteFile = fillOCFile(we);
mFolderAndFiles.add(remoteFile); mFolderAndFiles.add(remoteFile);
} }

View File

@ -68,7 +68,7 @@ public class RemoveRemoteFileOperation extends RemoteOperation {
DeleteMethod delete = null; DeleteMethod delete = null;
try { 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); int status = client.executeMethod(delete, REMOVE_READ_TIMEOUT, REMOVE_CONNECTION_TIMEOUT);
delete.getResponseBodyAsString(); // exhaust the response, although not interesting delete.getResponseBodyAsString(); // exhaust the response, although not interesting

View File

@ -104,8 +104,8 @@ public class RenameRemoteFileOperation extends RemoteOperation {
return new RemoteOperationResult(ResultCode.INVALID_OVERWRITE); return new RemoteOperationResult(ResultCode.INVALID_OVERWRITE);
} }
move = new LocalMoveMethod( client.getBaseUri() + WebdavUtils.encodePath(mOldRemotePath), move = new LocalMoveMethod( client.getWebdavUri() + WebdavUtils.encodePath(mOldRemotePath),
client.getBaseUri() + WebdavUtils.encodePath(mNewRemotePath)); client.getWebdavUri() + WebdavUtils.encodePath(mNewRemotePath));
int status = client.executeMethod(move, RENAME_READ_TIMEOUT, RENAME_CONNECTION_TIMEOUT); int status = client.executeMethod(move, RENAME_READ_TIMEOUT, RENAME_CONNECTION_TIMEOUT);
move.getResponseBodyAsString(); // exhaust response, although not interesting move.getResponseBodyAsString(); // exhaust response, although not interesting

View File

@ -80,7 +80,7 @@ public class UploadRemoteFileOperation extends RemoteOperation {
if (mCancellationRequested.get()) { if (mCancellationRequested.get()) {
throw new OperationCancelledException(); throw new OperationCancelledException();
} else { } else {
mPutMethod = new PutMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath)); mPutMethod = new PutMethod(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath));
} }
} }