From a1420ab00e79125e0a2810540365de8ef07225cd Mon Sep 17 00:00:00 2001 From: masensio Date: Fri, 13 Mar 2015 13:37:23 +0100 Subject: [PATCH 01/10] Add userAgent in RemoteOperation --- .../common/operations/RemoteOperation.java | 117 +++++++++++++----- 1 file changed, 83 insertions(+), 34 deletions(-) diff --git a/src/com/owncloud/android/lib/common/operations/RemoteOperation.java b/src/com/owncloud/android/lib/common/operations/RemoteOperation.java index 835a99d5..09f471c9 100644 --- a/src/com/owncloud/android/lib/common/operations/RemoteOperation.java +++ b/src/com/owncloud/android/lib/common/operations/RemoteOperation.java @@ -60,6 +60,9 @@ public abstract class RemoteOperation implements Runnable { /** OCS API header value */ public static final String OCS_API_HEADER_VALUE = "true"; + /** User agent header */ + public static final String USER_AGENT_HEADER = "User-Agent"; + /** ownCloud account in the remote ownCloud server to operate */ private Account mAccount = null; @@ -78,11 +81,14 @@ public abstract class RemoteOperation implements Runnable { /** Activity */ private Activity mCallerActivity; + /** User agent */ + private String mUserAgent; + /** * Abstract method to implement the operation in derived classes. */ - protected abstract RemoteOperationResult run(OwnCloudClient client); + protected abstract RemoteOperationResult run(OwnCloudClient client); /** @@ -90,19 +96,23 @@ public abstract class RemoteOperation implements Runnable { * * Do not call this method from the main thread. * - * This method should be used whenever an ownCloud account is available, instead of {@link #execute(OwnCloudClient)}. + * This method should be used whenever an ownCloud account is available, instead of + * {@link #execute(OwnCloudClient, java.lang.String)}. * - * @param account ownCloud account in remote ownCloud server to reach during the execution of the operation. + * @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 userAgent userAgent string * @return Result of the operation. */ - public RemoteOperationResult execute(Account account, Context context) { + public RemoteOperationResult execute(Account account, Context context, String userAgent) { 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(); + mUserAgent = userAgent; try { OwnCloudAccount ocAccount = new OwnCloudAccount(mAccount, mContext); mClient = OwnCloudClientManagerFactory.getDefaultSingleton(). @@ -120,13 +130,16 @@ public abstract class RemoteOperation implements Runnable { * * Do not call this method from the main thread. * - * @param client Client object to reach an ownCloud server during the execution of the operation. + * @param client Client object to reach an ownCloud server during the execution of + * the operation. + * @param userAgent userAgent string * @return Result of the operation. */ - public RemoteOperationResult execute(OwnCloudClient client) { + public RemoteOperationResult execute(OwnCloudClient client, String userAgent) { if (client == null) throw new IllegalArgumentException("Trying to execute a remote operation with a NULL OwnCloudClient"); mClient = client; + mUserAgent = userAgent; return run(client); } @@ -134,29 +147,35 @@ public abstract class RemoteOperation implements Runnable { /** * Asynchronously executes the remote operation * - * This method should be used whenever an ownCloud account is available, instead of {@link #execute(OwnCloudClient)}. + * This method should be used whenever an ownCloud account is available, instead of + * {@link #execute(OwnCloudClient, java.lang.String)}. * * @deprecated This method will be removed in version 1.0. - * Use {@link #execute(Account, Context, OnRemoteOperationListener, Handler)} - * instead. + * Use {@link #execute(Account, Context, String, OnRemoteOperationListener, + * Handler)} instead. * - * @param account ownCloud account in remote ownCloud server to reach during the execution of the operation. + * @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. + * @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. */ @Deprecated - public Thread execute(Account account, Context context, OnRemoteOperationListener listener, Handler listenerHandler, Activity callerActivity) { + public Thread execute(Account account, Context context, OnRemoteOperationListener listener, + Handler listenerHandler, Activity callerActivity) { if (account == null) - throw new IllegalArgumentException("Trying to execute a remote operation with a NULL Account"); + 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"); + throw new IllegalArgumentException + ("Trying to execute a remote operation with a NULL Context"); mAccount = account; mContext = context.getApplicationContext(); mCallerActivity = callerActivity; - mClient = null; // the client instance will be created from mAccount and mContext in the runnerThread to create below - + mClient = null; // the client instance will be created from mAccount + // and mContext in the runnerThread to create below mListener = listener; mListenerHandler = listenerHandler; @@ -171,27 +190,31 @@ public abstract class RemoteOperation implements Runnable { * Asynchronously executes the remote operation * * This method should be used whenever an ownCloud account is available, - * instead of {@link #execute(OwnCloudClient, OnRemoteOperationListener, Handler))}. + * instead of {@link #execute(OwnCloudClient, String, OnRemoteOperationListener, Handler))}. * * @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 userAgent userAgent string * @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 Thread execute(Account account, Context context, OnRemoteOperationListener listener, - Handler listenerHandler) { + public Thread execute(Account account, Context context, String userAgent, + OnRemoteOperationListener listener, Handler listenerHandler) { if (account == null) - throw new IllegalArgumentException("Trying to execute a remote operation with a NULL Account"); + 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"); + throw new IllegalArgumentException + ("Trying to execute a remote operation with a NULL Context"); mAccount = account; mContext = context.getApplicationContext(); mCallerActivity = null; - mClient = null; // the client instance will be created from mAccount and mContext in the runnerThread to create below + mClient = null; // the client instance will be created from + // mAccount and mContext in the runnerThread to create below mListener = listener; @@ -206,24 +229,34 @@ public abstract class RemoteOperation implements Runnable { /** * Asynchronously executes the remote operation * - * @param client Client object to reach an ownCloud server during the execution of the operation. + * @param client Client object to reach an ownCloud server + * during the execution of the operation. + * @param userAgent userAgent string * @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. + * @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 Thread execute(OwnCloudClient client, OnRemoteOperationListener listener, Handler listenerHandler) { + public Thread execute(OwnCloudClient client, String userAgent, + OnRemoteOperationListener listener, Handler listenerHandler) { if (client == null) { - throw new IllegalArgumentException("Trying to execute a remote operation with a NULL OwnCloudClient"); + throw new IllegalArgumentException + ("Trying to execute a remote operation with a NULL OwnCloudClient"); } mClient = client; + mUserAgent = userAgent; if (listener == null) { - throw new IllegalArgumentException("Trying to execute a remote operation asynchronously without a listener to notiy the result"); + 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"); + throw new IllegalArgumentException + ("Trying to execute a remote operation asynchronously " + + "without a handler to the listener's thread"); } mListenerHandler = listenerHandler; @@ -234,7 +267,8 @@ public abstract class RemoteOperation implements Runnable { /** * Asynchronous execution of the operation - * started by {@link RemoteOperation#execute(OwnCloudClient, OnRemoteOperationListener, Handler)}, + * started by {@link RemoteOperation#execute(OwnCloudClient, String, + * OnRemoteOperationListener, Handler)}, * and result posting. * * TODO refactor && clean the code; now it's a mess @@ -259,12 +293,15 @@ public abstract class RemoteOperation implements Runnable { } } else { - throw new IllegalStateException("Trying to run a remote operation asynchronously with no client instance or account"); + throw new IllegalStateException("Trying to run a remote operation " + + "asynchronously with no client instance or account"); } } } catch (IOException e) { - Log_OC.e(TAG, "Error while trying to access to " + mAccount.name, new AccountsException("I/O exception while trying to authorize the account", e)); + Log_OC.e(TAG, "Error while trying to access to " + mAccount.name, + new AccountsException("I/O exception while trying to authorize the account", + e)); result = new RemoteOperationResult(e); } catch (AccountsException e) { @@ -278,9 +315,11 @@ public abstract class RemoteOperation implements Runnable { repeat = false; /** DEPRECATED BLOCK - will be removed at version 1.0 ; don't trust in this code * to trigger authentication update */ - if (mCallerActivity != null && mAccount != null && mContext != null && !result.isSuccess() && + if (mCallerActivity != null && mAccount != null && mContext != null && + !result.isSuccess() && (result.getCode() == ResultCode.UNAUTHORIZED || result.isIdPRedirection())) { - /// possible fail due to lack of authorization in an operation performed in foreground + /// possible fail due to lack of authorization + // in an operation performed in foreground OwnCloudCredentials cred = mClient.getCredentials(); if (cred != null) { /// confirmed : unauthorized operation @@ -294,7 +333,9 @@ public abstract class RemoteOperation implements Runnable { am.clearPassword(mAccount); } mClient = null; - repeat = true; // when repeated, the creation of a new OwnCloudClient after erasing the saved credentials will trigger the login activity + // when repeated, the creation of a new OwnCloudClient after erasing the saved + // credentials will trigger the login activity + repeat = true; result = null; } } @@ -327,5 +368,13 @@ public abstract class RemoteOperation implements Runnable { return mClient; } + /** + * Returns the user agent string + * @return + */ + public final String getUserAgent() { + return mUserAgent; + } + } From fc07af23643b3c6a1b7df7c4c81b57482111d4a1 Mon Sep 17 00:00:00 2001 From: masensio Date: Fri, 13 Mar 2015 13:53:36 +0100 Subject: [PATCH 02/10] Add user agent in all the request --- .../ChunkedUploadRemoteFileOperation.java | 1 + .../files/CreateRemoteFolderOperation.java | 9 ++++++--- .../files/DownloadRemoteFileOperation.java | 20 +++++++++++++------ .../files/ExistenceCheckRemoteOperation.java | 17 ++++++++++++---- .../files/MoveRemoteFileOperation.java | 1 + .../files/ReadRemoteFileOperation.java | 1 + .../files/ReadRemoteFolderOperation.java | 1 + .../files/RemoveRemoteFileOperation.java | 4 +++- .../files/RenameRemoteFileOperation.java | 17 +++++++++++----- .../files/UploadRemoteFileOperation.java | 16 ++++++++++----- .../shares/CreateRemoteShareOperation.java | 5 ++++- .../GetRemoteSharesForFileOperation.java | 1 + .../shares/GetRemoteSharesOperation.java | 1 + .../shares/RemoveRemoteShareOperation.java | 1 + .../status/GetRemoteStatusOperation.java | 1 + .../users/GetRemoteUserNameOperation.java | 1 + 16 files changed, 72 insertions(+), 25 deletions(-) diff --git a/src/com/owncloud/android/lib/resources/files/ChunkedUploadRemoteFileOperation.java b/src/com/owncloud/android/lib/resources/files/ChunkedUploadRemoteFileOperation.java index 89379dda..beeef181 100644 --- a/src/com/owncloud/android/lib/resources/files/ChunkedUploadRemoteFileOperation.java +++ b/src/com/owncloud/android/lib/resources/files/ChunkedUploadRemoteFileOperation.java @@ -75,6 +75,7 @@ public class ChunkedUploadRemoteFileOperation extends UploadRemoteFileOperation } mPutMethod = new PutMethod(uriPrefix + chunkCount + "-" + chunkIndex); mPutMethod.addRequestHeader(OC_CHUNKED_HEADER, OC_CHUNKED_HEADER); + mPutMethod.addRequestHeader(USER_AGENT_HEADER, getUserAgent()); ((ChunkFromFileChannelRequestEntity)mEntity).setOffset(offset); mPutMethod.setRequestEntity(mEntity); status = client.executeMethod(mPutMethod); diff --git a/src/com/owncloud/android/lib/resources/files/CreateRemoteFolderOperation.java b/src/com/owncloud/android/lib/resources/files/CreateRemoteFolderOperation.java index 0d248e6f..9f611b2a 100644 --- a/src/com/owncloud/android/lib/resources/files/CreateRemoteFolderOperation.java +++ b/src/com/owncloud/android/lib/resources/files/CreateRemoteFolderOperation.java @@ -78,7 +78,8 @@ public class CreateRemoteFolderOperation extends RemoteOperation { result = createFolder(client); if (!result.isSuccess() && mCreateFullPath && RemoteOperationResult.ResultCode.CONFLICT == result.getCode()) { - result = createParentFolder(FileUtils.getParentPath(mRemotePath), client); + result = createParentFolder(FileUtils.getParentPath(mRemotePath), client, + getUserAgent()); if (result.isSuccess()) { result = createFolder(client); // second (and last) try } @@ -97,6 +98,7 @@ public class CreateRemoteFolderOperation extends RemoteOperation { MkColMethod mkcol = null; try { mkcol = new MkColMethod(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath)); + mkcol.addRequestHeader(USER_AGENT_HEADER, getUserAgent()); int status = client.executeMethod(mkcol, READ_TIMEOUT, CONNECTION_TIMEOUT); result = new RemoteOperationResult(mkcol.succeeded(), status, mkcol.getResponseHeaders()); Log_OC.d(TAG, "Create directory " + mRemotePath + ": " + result.getLogMessage()); @@ -113,10 +115,11 @@ public class CreateRemoteFolderOperation extends RemoteOperation { return result; } - private RemoteOperationResult createParentFolder(String parentPath, OwnCloudClient client) { + private RemoteOperationResult createParentFolder(String parentPath, OwnCloudClient client, + String userAgent) { RemoteOperation operation = new CreateRemoteFolderOperation(parentPath, mCreateFullPath); - return operation.execute(client); + return operation.execute(client, userAgent); } diff --git a/src/com/owncloud/android/lib/resources/files/DownloadRemoteFileOperation.java b/src/com/owncloud/android/lib/resources/files/DownloadRemoteFileOperation.java index 551685f4..8675f065 100644 --- a/src/com/owncloud/android/lib/resources/files/DownloadRemoteFileOperation.java +++ b/src/com/owncloud/android/lib/resources/files/DownloadRemoteFileOperation.java @@ -82,22 +82,27 @@ public class DownloadRemoteFileOperation extends RemoteOperation { try { tmpFile.getParentFile().mkdirs(); int status = downloadFile(client, tmpFile); - result = new RemoteOperationResult(isSuccess(status), status, (mGet != null ? mGet.getResponseHeaders() : null)); - Log_OC.i(TAG, "Download of " + mRemotePath + " to " + getTmpPath() + ": " + result.getLogMessage()); + result = new RemoteOperationResult(isSuccess(status), status, + (mGet != null ? mGet.getResponseHeaders() : null)); + Log_OC.i(TAG, "Download of " + mRemotePath + " to " + getTmpPath() + ": " + + result.getLogMessage()); } catch (Exception e) { result = new RemoteOperationResult(e); - Log_OC.e(TAG, "Download of " + mRemotePath + " to " + getTmpPath() + ": " + result.getLogMessage(), e); + Log_OC.e(TAG, "Download of " + mRemotePath + " to " + getTmpPath() + ": " + + result.getLogMessage(), e); } return result; } - 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; boolean savedFile = false; mGet = new GetMethod(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath)); + mGet.addRequestHeader(USER_AGENT_HEADER, getUserAgent()); Iterator it = null; FileOutputStream fos = null; @@ -110,7 +115,9 @@ public class DownloadRemoteFileOperation extends RemoteOperation { long transferred = 0; Header contentLength = mGet.getResponseHeader("Content-Length"); - long totalToTransfer = (contentLength != null && contentLength.getValue().length() >0) ? Long.parseLong(contentLength.getValue()) : 0; + long totalToTransfer = (contentLength != null && + contentLength.getValue().length() >0) ? + Long.parseLong(contentLength.getValue()) : 0; byte[] bytes = new byte[4096]; int readResult = 0; @@ -126,7 +133,8 @@ public class DownloadRemoteFileOperation extends RemoteOperation { synchronized (mDataTransferListeners) { it = mDataTransferListeners.iterator(); while (it.hasNext()) { - it.next().onTransferProgress(readResult, transferred, totalToTransfer, targetFile.getName()); + it.next().onTransferProgress(readResult, transferred, totalToTransfer, + targetFile.getName()); } } } diff --git a/src/com/owncloud/android/lib/resources/files/ExistenceCheckRemoteOperation.java b/src/com/owncloud/android/lib/resources/files/ExistenceCheckRemoteOperation.java index 9b54ba01..1d8b2e5a 100644 --- a/src/com/owncloud/android/lib/resources/files/ExistenceCheckRemoteOperation.java +++ b/src/com/owncloud/android/lib/resources/files/ExistenceCheckRemoteOperation.java @@ -58,7 +58,8 @@ public class ExistenceCheckRemoteOperation extends RemoteOperation { * * @param remotePath Path to append to the URL owned by the client instance. * @param context Android application context. - * @param successIfAbsent When 'true', the operation finishes in success if the path does NOT exist in the remote server (HTTP 404). + * @param successIfAbsent When 'true', the operation finishes in success if the path does + * NOT exist in the remote server (HTTP 404). */ public ExistenceCheckRemoteOperation(String remotePath, Context context, boolean successIfAbsent) { mPath = (remotePath != null) ? remotePath : ""; @@ -76,15 +77,23 @@ public class ExistenceCheckRemoteOperation extends RemoteOperation { HeadMethod head = null; try { head = new HeadMethod(client.getWebdavUri() + WebdavUtils.encodePath(mPath)); + head.addRequestHeader(USER_AGENT_HEADER, getUserAgent()); int status = client.executeMethod(head, TIMEOUT, TIMEOUT); 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()); - Log_OC.d(TAG, "Existence check for " + client.getWebdavUri() + WebdavUtils.encodePath(mPath) + " targeting for " + (mSuccessIfAbsent ? " absence " : " existence ") + "finished with HTTP status " + status + (!success?"(FAIL)":"")); + Log_OC.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_OC.e(TAG, "Existence check for " + client.getWebdavUri() + WebdavUtils.encodePath(mPath) + " targeting for " + (mSuccessIfAbsent ? " absence " : " existence ") + ": " + result.getLogMessage(), result.getException()); + Log_OC.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/resources/files/MoveRemoteFileOperation.java b/src/com/owncloud/android/lib/resources/files/MoveRemoteFileOperation.java index 1eb88765..419851f3 100644 --- a/src/com/owncloud/android/lib/resources/files/MoveRemoteFileOperation.java +++ b/src/com/owncloud/android/lib/resources/files/MoveRemoteFileOperation.java @@ -113,6 +113,7 @@ public class MoveRemoteFileOperation extends RemoteOperation { client.getWebdavUri() + WebdavUtils.encodePath(mTargetRemotePath), mOverwrite ); + move.addRequestHeader(USER_AGENT_HEADER, getUserAgent()); int status = client.executeMethod(move, MOVE_READ_TIMEOUT, MOVE_CONNECTION_TIMEOUT); /// process response diff --git a/src/com/owncloud/android/lib/resources/files/ReadRemoteFileOperation.java b/src/com/owncloud/android/lib/resources/files/ReadRemoteFileOperation.java index 12c2017f..7700621a 100644 --- a/src/com/owncloud/android/lib/resources/files/ReadRemoteFileOperation.java +++ b/src/com/owncloud/android/lib/resources/files/ReadRemoteFileOperation.java @@ -79,6 +79,7 @@ public class ReadRemoteFileOperation extends RemoteOperation { propfind = new PropFindMethod(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath), WebdavUtils.getFilePropSet(), // PropFind Properties DavConstants.DEPTH_0); + propfind.addRequestHeader(USER_AGENT_HEADER, getUserAgent()); int status; status = client.executeMethod(propfind, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT); diff --git a/src/com/owncloud/android/lib/resources/files/ReadRemoteFolderOperation.java b/src/com/owncloud/android/lib/resources/files/ReadRemoteFolderOperation.java index 1e93aa22..1c49c688 100644 --- a/src/com/owncloud/android/lib/resources/files/ReadRemoteFolderOperation.java +++ b/src/com/owncloud/android/lib/resources/files/ReadRemoteFolderOperation.java @@ -76,6 +76,7 @@ public class ReadRemoteFolderOperation extends RemoteOperation { query = new PropFindMethod(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath), WebdavUtils.getAllPropSet(), // PropFind Properties DavConstants.DEPTH_1); + query.addRequestHeader(USER_AGENT_HEADER, getUserAgent()); int status = client.executeMethod(query); // check and process response diff --git a/src/com/owncloud/android/lib/resources/files/RemoveRemoteFileOperation.java b/src/com/owncloud/android/lib/resources/files/RemoveRemoteFileOperation.java index a031c65e..470b4533 100644 --- a/src/com/owncloud/android/lib/resources/files/RemoveRemoteFileOperation.java +++ b/src/com/owncloud/android/lib/resources/files/RemoveRemoteFileOperation.java @@ -68,10 +68,12 @@ public class RemoveRemoteFileOperation extends RemoteOperation { try { delete = new DeleteMethod(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath)); + delete.addRequestHeader(USER_AGENT_HEADER, getUserAgent()); int status = client.executeMethod(delete, REMOVE_READ_TIMEOUT, REMOVE_CONNECTION_TIMEOUT); delete.getResponseBodyAsString(); // exhaust the response, although not interesting - result = new RemoteOperationResult((delete.succeeded() || status == HttpStatus.SC_NOT_FOUND), status, delete.getResponseHeaders()); + result = new RemoteOperationResult((delete.succeeded() || + status == HttpStatus.SC_NOT_FOUND), status, delete.getResponseHeaders()); Log_OC.i(TAG, "Remove " + mRemotePath + ": " + result.getLogMessage()); } catch (Exception e) { diff --git a/src/com/owncloud/android/lib/resources/files/RenameRemoteFileOperation.java b/src/com/owncloud/android/lib/resources/files/RenameRemoteFileOperation.java index a3161248..592084da 100644 --- a/src/com/owncloud/android/lib/resources/files/RenameRemoteFileOperation.java +++ b/src/com/owncloud/android/lib/resources/files/RenameRemoteFileOperation.java @@ -63,13 +63,15 @@ public class RenameRemoteFileOperation extends RemoteOperation { * @param newName New name to set as the name of file. * @param isFolder 'true' for folder and 'false' for files */ - public RenameRemoteFileOperation(String oldName, String oldRemotePath, String newName, boolean isFolder) { + public RenameRemoteFileOperation(String oldName, String oldRemotePath, String newName, + boolean isFolder) { mOldName = oldName; mOldRemotePath = oldRemotePath; mNewName = newName; String parent = (new File(mOldRemotePath)).getParent(); - parent = (parent.endsWith(FileUtils.PATH_SEPARATOR)) ? parent : parent + FileUtils.PATH_SEPARATOR; + parent = (parent.endsWith(FileUtils.PATH_SEPARATOR)) ? parent : parent + + FileUtils.PATH_SEPARATOR; mNewRemotePath = parent + mNewName; if (isFolder) { mNewRemotePath += FileUtils.PATH_SEPARATOR; @@ -102,17 +104,22 @@ public class RenameRemoteFileOperation extends RemoteOperation { return new RemoteOperationResult(ResultCode.INVALID_OVERWRITE); } - move = new LocalMoveMethod( client.getWebdavUri() + WebdavUtils.encodePath(mOldRemotePath), + move = new LocalMoveMethod( client.getWebdavUri() + + WebdavUtils.encodePath(mOldRemotePath), client.getWebdavUri() + WebdavUtils.encodePath(mNewRemotePath)); + move.addRequestHeader(USER_AGENT_HEADER, getUserAgent()); int status = client.executeMethod(move, RENAME_READ_TIMEOUT, RENAME_CONNECTION_TIMEOUT); move.getResponseBodyAsString(); // exhaust response, although not interesting result = new RemoteOperationResult(move.succeeded(), status, move.getResponseHeaders()); - Log_OC.i(TAG, "Rename " + mOldRemotePath + " to " + mNewRemotePath + ": " + result.getLogMessage()); + Log_OC.i(TAG, "Rename " + mOldRemotePath + " to " + mNewRemotePath + ": " + + result.getLogMessage()); } catch (Exception e) { result = new RemoteOperationResult(e); - Log_OC.e(TAG, "Rename " + mOldRemotePath + " to " + ((mNewRemotePath==null) ? mNewName : mNewRemotePath) + ": " + result.getLogMessage(), e); + Log_OC.e(TAG, "Rename " + mOldRemotePath + " to " + + ((mNewRemotePath==null) ? mNewName : mNewRemotePath) + ": " + + result.getLogMessage(), e); } finally { if (move != null) diff --git a/src/com/owncloud/android/lib/resources/files/UploadRemoteFileOperation.java b/src/com/owncloud/android/lib/resources/files/UploadRemoteFileOperation.java index dd707462..b8119430 100644 --- a/src/com/owncloud/android/lib/resources/files/UploadRemoteFileOperation.java +++ b/src/com/owncloud/android/lib/resources/files/UploadRemoteFileOperation.java @@ -80,13 +80,16 @@ public class UploadRemoteFileOperation extends RemoteOperation { if (mCancellationRequested.get()) { throw new OperationCancelledException(); } else { - mPutMethod = new PutMethod(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath)); + mPutMethod = new PutMethod(client.getWebdavUri() + + WebdavUtils.encodePath(mRemotePath)); + mPutMethod.addRequestHeader(USER_AGENT_HEADER, getUserAgent()); } } int status = uploadFile(client); - result = new RemoteOperationResult(isSuccess(status), status, (mPutMethod != null ? mPutMethod.getResponseHeaders() : null)); + result = new RemoteOperationResult(isSuccess(status), status, + (mPutMethod != null ? mPutMethod.getResponseHeaders() : null)); } catch (Exception e) { // TODO something cleaner with cancellations @@ -100,16 +103,19 @@ public class UploadRemoteFileOperation extends RemoteOperation { } public boolean isSuccess(int status) { - return ((status == HttpStatus.SC_OK || status == HttpStatus.SC_CREATED || status == HttpStatus.SC_NO_CONTENT)); + return ((status == HttpStatus.SC_OK || status == HttpStatus.SC_CREATED || + status == HttpStatus.SC_NO_CONTENT)); } - protected int uploadFile(OwnCloudClient client) throws HttpException, IOException, OperationCancelledException { + protected int uploadFile(OwnCloudClient client) throws HttpException, IOException, + OperationCancelledException { int status = -1; try { File f = new File(mLocalPath); mEntity = new FileRequestEntity(f, mMimeType); synchronized (mDataTransferListeners) { - ((ProgressiveDataTransferer)mEntity).addDatatransferProgressListeners(mDataTransferListeners); + ((ProgressiveDataTransferer)mEntity) + .addDatatransferProgressListeners(mDataTransferListeners); } mPutMethod.setRequestEntity(mEntity); status = client.executeMethod(mPutMethod); diff --git a/src/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.java b/src/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.java index 3e153c06..c377ecbc 100644 --- a/src/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.java +++ b/src/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.java @@ -104,7 +104,10 @@ public class CreateRemoteShareOperation extends RemoteOperation { post = new PostMethod(client.getBaseUri() + ShareUtils.SHARING_API_PATH); //Log_OC.d(TAG, "URL ------> " + client.getBaseUri() + ShareUtils.SHARING_API_PATH); - post.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded; charset=utf-8"); // necessary for special characters + post.setRequestHeader( "Content-Type", + "application/x-www-form-urlencoded; charset=utf-8"); // necessary for special characters + post.addRequestHeader(USER_AGENT_HEADER, getUserAgent()); + post.addParameter(PARAM_PATH, mRemoteFilePath); post.addParameter(PARAM_SHARE_TYPE, Integer.toString(mShareType.getValue())); post.addParameter(PARAM_SHARE_WITH, mShareWith); diff --git a/src/com/owncloud/android/lib/resources/shares/GetRemoteSharesForFileOperation.java b/src/com/owncloud/android/lib/resources/shares/GetRemoteSharesForFileOperation.java index 3c53e7f7..599201e5 100644 --- a/src/com/owncloud/android/lib/resources/shares/GetRemoteSharesForFileOperation.java +++ b/src/com/owncloud/android/lib/resources/shares/GetRemoteSharesForFileOperation.java @@ -86,6 +86,7 @@ public class GetRemoteSharesForFileOperation extends RemoteOperation { try { // Get Method get = new GetMethod(client.getBaseUri() + ShareUtils.SHARING_API_PATH); + get.addRequestHeader(USER_AGENT_HEADER, getUserAgent()); // Add Parameters to Get Method get.setQueryString(new NameValuePair[] { diff --git a/src/com/owncloud/android/lib/resources/shares/GetRemoteSharesOperation.java b/src/com/owncloud/android/lib/resources/shares/GetRemoteSharesOperation.java index 38b75194..d414913d 100644 --- a/src/com/owncloud/android/lib/resources/shares/GetRemoteSharesOperation.java +++ b/src/com/owncloud/android/lib/resources/shares/GetRemoteSharesOperation.java @@ -67,6 +67,7 @@ public class GetRemoteSharesOperation extends RemoteOperation { try{ get = new GetMethod(client.getBaseUri() + ShareUtils.SHARING_API_PATH); get.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); + get.addRequestHeader(USER_AGENT_HEADER, getUserAgent()); status = client.executeMethod(get); if(isSuccess(status)) { String response = get.getResponseBodyAsString(); diff --git a/src/com/owncloud/android/lib/resources/shares/RemoveRemoteShareOperation.java b/src/com/owncloud/android/lib/resources/shares/RemoveRemoteShareOperation.java index dea4fe93..e3ddd89c 100644 --- a/src/com/owncloud/android/lib/resources/shares/RemoveRemoteShareOperation.java +++ b/src/com/owncloud/android/lib/resources/shares/RemoveRemoteShareOperation.java @@ -72,6 +72,7 @@ public class RemoveRemoteShareOperation extends RemoteOperation { delete = new DeleteMethod(client.getBaseUri() + ShareUtils.SHARING_API_PATH + id); delete.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); + delete.addRequestHeader(USER_AGENT_HEADER, getUserAgent()); status = client.executeMethod(delete); diff --git a/src/com/owncloud/android/lib/resources/status/GetRemoteStatusOperation.java b/src/com/owncloud/android/lib/resources/status/GetRemoteStatusOperation.java index 3bd7e065..5d841062 100644 --- a/src/com/owncloud/android/lib/resources/status/GetRemoteStatusOperation.java +++ b/src/com/owncloud/android/lib/resources/status/GetRemoteStatusOperation.java @@ -75,6 +75,7 @@ public class GetRemoteStatusOperation extends RemoteOperation { String baseUrlSt = client.getBaseUri().toString(); try { get = new GetMethod(baseUrlSt + AccountUtils.STATUS_PATH); + get.addRequestHeader(USER_AGENT_HEADER, getUserAgent()); client.setFollowRedirects(false); boolean isRedirectToNonSecureConnection = false; diff --git a/src/com/owncloud/android/lib/resources/users/GetRemoteUserNameOperation.java b/src/com/owncloud/android/lib/resources/users/GetRemoteUserNameOperation.java index cecd639f..e7bcb78c 100644 --- a/src/com/owncloud/android/lib/resources/users/GetRemoteUserNameOperation.java +++ b/src/com/owncloud/android/lib/resources/users/GetRemoteUserNameOperation.java @@ -79,6 +79,7 @@ public class GetRemoteUserNameOperation extends RemoteOperation { try { get = new GetMethod(client.getBaseUri() + OCS_ROUTE); get.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); + get.addRequestHeader(USER_AGENT_HEADER, getUserAgent()); status = client.executeMethod(get); if(isSuccess(status)) { String response = get.getResponseBodyAsString(); From 87582c4a8ea293d582ee7d245bd5df8dc6c2e7c2 Mon Sep 17 00:00:00 2001 From: masensio Date: Wed, 18 Mar 2015 17:08:19 +0100 Subject: [PATCH 03/10] Add user-agent string to setup options --- .../android/lib/common/OwnCloudClient.java | 5 +- .../lib/common/OwnCloudClientFactory.java | 57 +++++++++++++------ .../lib/common/OwnCloudClientManager.java | 5 +- .../lib/common/SimpleFactoryManager.java | 8 ++- .../lib/common/SingleSessionManager.java | 9 ++- .../common/operations/RemoteOperation.java | 15 +++-- 6 files changed, 65 insertions(+), 34 deletions(-) diff --git a/src/com/owncloud/android/lib/common/OwnCloudClient.java b/src/com/owncloud/android/lib/common/OwnCloudClient.java index 482456f4..ab12a5cb 100644 --- a/src/com/owncloud/android/lib/common/OwnCloudClient.java +++ b/src/com/owncloud/android/lib/common/OwnCloudClient.java @@ -54,7 +54,6 @@ import com.owncloud.android.lib.common.utils.Log_OC; public class OwnCloudClient extends HttpClient { private static final String TAG = OwnCloudClient.class.getSimpleName(); - public static final String USER_AGENT = "Android-ownCloud"; private static final int MAX_REDIRECTIONS_COUNT = 3; private static final String PARAM_SINGLE_COOKIE_HEADER = "http.protocol.single-cookie-header"; private static final boolean PARAM_SINGLE_COOKIE_HEADER_VALUE = true; @@ -71,7 +70,7 @@ public class OwnCloudClient extends HttpClient { /** * Constructor */ - public OwnCloudClient(Uri baseUri, HttpConnectionManager connectionMgr) { + public OwnCloudClient(Uri baseUri, HttpConnectionManager connectionMgr, String userAgent) { super(connectionMgr); if (baseUri == null) { @@ -82,7 +81,7 @@ public class OwnCloudClient extends HttpClient { mInstanceNumber = sIntanceCounter++; Log_OC.d(TAG + " #" + mInstanceNumber, "Creating OwnCloudClient"); - getParams().setParameter(HttpMethodParams.USER_AGENT, USER_AGENT); + getParams().setParameter(HttpMethodParams.USER_AGENT, userAgent); getParams().setParameter( CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); diff --git a/src/com/owncloud/android/lib/common/OwnCloudClientFactory.java b/src/com/owncloud/android/lib/common/OwnCloudClientFactory.java index fb45bdac..b0cd5fa3 100644 --- a/src/com/owncloud/android/lib/common/OwnCloudClientFactory.java +++ b/src/com/owncloud/android/lib/common/OwnCloudClientFactory.java @@ -61,19 +61,29 @@ public class OwnCloudClientFactory { * * @param account The ownCloud account * @param appContext Android application context + * @param userAgent OwnCloud userAgent string * @return A OwnCloudClient 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. + * @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. * @throws AccountNotFoundException If 'account' is unknown for the AccountManager */ - public static OwnCloudClient createOwnCloudClient (Account account, Context appContext) throws OperationCanceledException, AuthenticatorException, IOException, AccountNotFoundException { + public static OwnCloudClient createOwnCloudClient (Account account, Context appContext, + String userAgent) + throws OperationCanceledException, AuthenticatorException, IOException, + AccountNotFoundException { //Log_OC.d(TAG, "Creating OwnCloudClient associated to " + account.name); Uri baseUri = Uri.parse(AccountUtils.getBaseUrlForAccount(appContext, account)); AccountManager am = AccountManager.get(appContext); - boolean isOauth2 = am.getUserData(account, AccountUtils.Constants.KEY_SUPPORTS_OAUTH2) != null; // TODO avoid calling to getUserData here - boolean isSamlSso = am.getUserData(account, AccountUtils.Constants.KEY_SUPPORTS_SAML_WEB_SSO) != null; - OwnCloudClient client = createOwnCloudClient(baseUri, appContext, !isSamlSso); + // TODO avoid calling to getUserData here + boolean isOauth2 = + am.getUserData(account, AccountUtils.Constants.KEY_SUPPORTS_OAUTH2) != null; + boolean isSamlSso = + am.getUserData(account, AccountUtils.Constants.KEY_SUPPORTS_SAML_WEB_SSO) != null; + OwnCloudClient client = createOwnCloudClient(baseUri, appContext, !isSamlSso, userAgent); if (isOauth2) { String accessToken = am.blockingGetAuthToken( @@ -116,12 +126,18 @@ 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, String userAgent) + throws OperationCanceledException, AuthenticatorException, IOException, + AccountNotFoundException { Uri baseUri = Uri.parse(AccountUtils.getBaseUrlForAccount(appContext, account)); AccountManager am = AccountManager.get(appContext); - boolean isOauth2 = am.getUserData(account, AccountUtils.Constants.KEY_SUPPORTS_OAUTH2) != null; // TODO avoid calling to getUserData here - boolean isSamlSso = am.getUserData(account, AccountUtils.Constants.KEY_SUPPORTS_SAML_WEB_SSO) != null; - OwnCloudClient client = createOwnCloudClient(baseUri, appContext, !isSamlSso); + // TODO avoid calling to getUserData here + boolean isOauth2 = + am.getUserData(account, AccountUtils.Constants.KEY_SUPPORTS_OAUTH2) != null; + boolean isSamlSso = + am.getUserData(account, AccountUtils.Constants.KEY_SUPPORTS_SAML_WEB_SSO) != null; + OwnCloudClient client = createOwnCloudClient(baseUri, appContext, !isSamlSso, userAgent); if (isOauth2) { // TODO avoid a call to getUserData here AccountManagerFuture future = am.getAuthToken( @@ -159,7 +175,8 @@ public class OwnCloudClientFactory { } else { String username = account.name.substring(0, account.name.lastIndexOf('@')); //String password = am.getPassword(account); - //String password = am.blockingGetAuthToken(account, MainApp.getAuthTokenTypePass(), false); + //String password = am.blockingGetAuthToken(account, MainApp.getAuthTokenTypePass(), + // false); AccountManagerFuture future = am.getAuthToken( account, AccountTypeUtils.getAuthTokenTypePass(account.type), @@ -182,23 +199,29 @@ public class OwnCloudClientFactory { } /** - * Creates a OwnCloudClient to access a URL and sets the desired parameters for ownCloud client connections. + * Creates a OwnCloudClient to access a URL and sets the desired parameters for ownCloud + * client connections. * * @param uri URL to the ownCloud server; BASE ENTRY POINT, not WebDavPATH * @param context Android context where the OwnCloudClient is being created. + * @param userAgent OwnCloud userAgent string * @return A OwnCloudClient object ready to be used */ - public static OwnCloudClient createOwnCloudClient(Uri uri, Context context, boolean followRedirects) { + public static OwnCloudClient createOwnCloudClient(Uri uri, Context context, + boolean followRedirects, String userAgent) { try { NetworkUtils.registerAdvancedSslContext(true, context); } catch (GeneralSecurityException e) { - Log_OC.e(TAG, "Advanced SSL Context could not be loaded. Default SSL management in the system will be used for HTTPS connections", e); + Log_OC.e(TAG, "Advanced SSL Context could not be loaded. Default SSL management in" + + " the system will be used for HTTPS connections", e); } catch (IOException e) { - Log_OC.e(TAG, "The local server truststore could not be read. Default SSL management in the system will be used for HTTPS connections", e); + Log_OC.e(TAG, "The local server truststore could not be read. Default SSL management" + + " in the system will be used for HTTPS connections", e); } - OwnCloudClient client = new OwnCloudClient(uri, NetworkUtils.getMultiThreadedConnManager()); + OwnCloudClient client = new OwnCloudClient(uri, NetworkUtils.getMultiThreadedConnManager(), + userAgent); client.setDefaultTimeouts(DEFAULT_DATA_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT); client.setFollowRedirects(followRedirects); diff --git a/src/com/owncloud/android/lib/common/OwnCloudClientManager.java b/src/com/owncloud/android/lib/common/OwnCloudClientManager.java index 40dab1ae..c9994f91 100644 --- a/src/com/owncloud/android/lib/common/OwnCloudClientManager.java +++ b/src/com/owncloud/android/lib/common/OwnCloudClientManager.java @@ -42,8 +42,9 @@ import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundExce public interface OwnCloudClientManager { - public OwnCloudClient getClientFor(OwnCloudAccount account, Context context) - throws AccountNotFoundException, OperationCanceledException, AuthenticatorException, IOException; + public OwnCloudClient getClientFor(OwnCloudAccount account, Context context, String userAgent) + throws AccountNotFoundException, OperationCanceledException, AuthenticatorException, + IOException; public OwnCloudClient removeClientFor(OwnCloudAccount account); diff --git a/src/com/owncloud/android/lib/common/SimpleFactoryManager.java b/src/com/owncloud/android/lib/common/SimpleFactoryManager.java index b06299d0..ad7120c0 100644 --- a/src/com/owncloud/android/lib/common/SimpleFactoryManager.java +++ b/src/com/owncloud/android/lib/common/SimpleFactoryManager.java @@ -40,15 +40,17 @@ public class SimpleFactoryManager implements OwnCloudClientManager { private static final String TAG = SimpleFactoryManager.class.getSimpleName(); @Override - public OwnCloudClient getClientFor(OwnCloudAccount account, Context context) - throws AccountNotFoundException, OperationCanceledException, AuthenticatorException, IOException { + public OwnCloudClient getClientFor(OwnCloudAccount account, Context context, String userAgent) + throws AccountNotFoundException, OperationCanceledException, AuthenticatorException, + IOException { Log_OC.d(TAG, "getClientFor(OwnCloudAccount ... : "); OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient( account.getBaseUri(), context.getApplicationContext(), - true); + true, + userAgent); Log_OC.v(TAG, " new client {" + (account.getName() != null ? diff --git a/src/com/owncloud/android/lib/common/SingleSessionManager.java b/src/com/owncloud/android/lib/common/SingleSessionManager.java index 7d60b17d..a69ac35c 100644 --- a/src/com/owncloud/android/lib/common/SingleSessionManager.java +++ b/src/com/owncloud/android/lib/common/SingleSessionManager.java @@ -62,8 +62,10 @@ public class SingleSessionManager implements OwnCloudClientManager { @Override - public synchronized OwnCloudClient getClientFor(OwnCloudAccount account, Context context) - throws AccountNotFoundException, OperationCanceledException, AuthenticatorException, IOException { + public synchronized OwnCloudClient getClientFor(OwnCloudAccount account, Context context, + String userAgent) + throws AccountNotFoundException, OperationCanceledException, AuthenticatorException, + IOException { Log_OC.d(TAG, "getClientFor(OwnCloudAccount ... : "); if (account == null) { @@ -107,7 +109,8 @@ public class SingleSessionManager implements OwnCloudClientManager { client = OwnCloudClientFactory.createOwnCloudClient( account.getBaseUri(), context.getApplicationContext(), - true); // TODO remove dependency on OwnCloudClientFactory + true, + userAgent); // TODO remove dependency on OwnCloudClientFactory client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); // enable cookie tracking diff --git a/src/com/owncloud/android/lib/common/operations/RemoteOperation.java b/src/com/owncloud/android/lib/common/operations/RemoteOperation.java index 09f471c9..4fc22829 100644 --- a/src/com/owncloud/android/lib/common/operations/RemoteOperation.java +++ b/src/com/owncloud/android/lib/common/operations/RemoteOperation.java @@ -107,16 +107,18 @@ public abstract class RemoteOperation implements Runnable { */ public RemoteOperationResult execute(Account account, Context context, String userAgent) { if (account == null) - throw new IllegalArgumentException("Trying to execute a remote operation with a NULL Account"); + 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"); + throw new IllegalArgumentException("Trying to execute a remote operation with a NULL " + + "Context"); mAccount = account; mContext = context.getApplicationContext(); mUserAgent = userAgent; try { OwnCloudAccount ocAccount = new OwnCloudAccount(mAccount, mContext); mClient = OwnCloudClientManagerFactory.getDefaultSingleton(). - getClientFor(ocAccount, mContext); + getClientFor(ocAccount, mContext, mUserAgent); } catch (Exception e) { Log_OC.e(TAG, "Error while trying to access to " + mAccount.name, e); return new RemoteOperationResult(e); @@ -137,7 +139,8 @@ public abstract class RemoteOperation implements Runnable { */ public RemoteOperationResult execute(OwnCloudClient client, String userAgent) { if (client == null) - throw new IllegalArgumentException("Trying to execute a remote operation with a NULL OwnCloudClient"); + throw new IllegalArgumentException("Trying to execute a remote operation with a NULL " + + "OwnCloudClient"); mClient = client; mUserAgent = userAgent; return run(client); @@ -284,12 +287,12 @@ public abstract class RemoteOperation implements Runnable { /** DEPRECATED BLOCK - will be removed at version 1.0 */ if (mCallerActivity != null) { mClient = OwnCloudClientFactory.createOwnCloudClient( - mAccount, mContext, mCallerActivity); + mAccount, mContext, mCallerActivity, mUserAgent); } else { /** EOF DEPRECATED */ OwnCloudAccount ocAccount = new OwnCloudAccount(mAccount, mContext); mClient = OwnCloudClientManagerFactory.getDefaultSingleton(). - getClientFor(ocAccount, mContext); + getClientFor(ocAccount, mContext, mUserAgent); } } else { From 43f6fe93e67a08bddbbf20c5f372b325476ed686 Mon Sep 17 00:00:00 2001 From: masensio Date: Fri, 20 Mar 2015 14:42:32 +0100 Subject: [PATCH 04/10] Fix unit test --- sample_client/res/values/setup.xml | 1 + .../lib/sampleclient/MainActivity.java | 11 ++--- test_client/res/values/setup.xml | 1 + .../lib/test_project/TestActivity.java | 25 ++++++----- test_client/tests/res/values/strings.xml | 1 + .../lib/test_project/test/MoveFileTest.java | 45 ++++++++++++------- .../test_project/test/OwnCloudClientTest.java | 33 +++++++++----- .../test/SimpleFactoryManagerTest.java | 15 ++++--- .../test/SingleSessionManagerTest.java | 15 ++++--- 9 files changed, 95 insertions(+), 52 deletions(-) diff --git a/sample_client/res/values/setup.xml b/sample_client/res/values/setup.xml index d20d0535..268eec7b 100644 --- a/sample_client/res/values/setup.xml +++ b/sample_client/res/values/setup.xml @@ -27,4 +27,5 @@ + Android-ownCloud diff --git a/sample_client/src/com/owncloud/android/lib/sampleclient/MainActivity.java b/sample_client/src/com/owncloud/android/lib/sampleclient/MainActivity.java index 11ea54f2..2e15b25a 100644 --- a/sample_client/src/com/owncloud/android/lib/sampleclient/MainActivity.java +++ b/sample_client/src/com/owncloud/android/lib/sampleclient/MainActivity.java @@ -79,7 +79,8 @@ public class MainActivity extends Activity implements OnRemoteOperationListener, mHandler = new Handler(); Uri serverUri = Uri.parse(getString(R.string.server_base_url)); - mClient = OwnCloudClientFactory.createOwnCloudClient(serverUri, this, true); + mClient = OwnCloudClientFactory.createOwnCloudClient(serverUri, this, true, + getString(R.string.user_agent)); mClient.setCredentials( OwnCloudCredentialsFactory.newBasicCredentials( getString(R.string.username), @@ -149,7 +150,7 @@ public class MainActivity extends Activity implements OnRemoteOperationListener, private void startRefresh() { ReadRemoteFolderOperation refreshOperation = new ReadRemoteFolderOperation(FileUtils.PATH_SEPARATOR); - refreshOperation.execute(mClient, this, mHandler); + refreshOperation.execute(mClient, getString(R.string.user_agent), this, mHandler); } private void startUpload() { @@ -159,7 +160,7 @@ public class MainActivity extends Activity implements OnRemoteOperationListener, String mimeType = getString(R.string.sample_file_mimetype); UploadRemoteFileOperation uploadOperation = new UploadRemoteFileOperation(fileToUpload.getAbsolutePath(), remotePath, mimeType); uploadOperation.addDatatransferProgressListener(this); - uploadOperation.execute(mClient, this, mHandler); + uploadOperation.execute(mClient, getString(R.string.user_agent), this, mHandler); } private void startRemoteDeletion() { @@ -167,7 +168,7 @@ public class MainActivity extends Activity implements OnRemoteOperationListener, File fileToUpload = upFolder.listFiles()[0]; String remotePath = FileUtils.PATH_SEPARATOR + fileToUpload.getName(); RemoveRemoteFileOperation removeOperation = new RemoveRemoteFileOperation(remotePath); - removeOperation.execute(mClient, this, mHandler); + removeOperation.execute(mClient, getString(R.string.user_agent), this, mHandler); } private void startDownload() { @@ -178,7 +179,7 @@ public class MainActivity extends Activity implements OnRemoteOperationListener, String remotePath = FileUtils.PATH_SEPARATOR + fileToUpload.getName(); DownloadRemoteFileOperation downloadOperation = new DownloadRemoteFileOperation(remotePath, downFolder.getAbsolutePath()); downloadOperation.addDatatransferProgressListener(this); - downloadOperation.execute(mClient, this, mHandler); + downloadOperation.execute(mClient, getString(R.string.user_agent), this, mHandler); } @SuppressWarnings("deprecation") diff --git a/test_client/res/values/setup.xml b/test_client/res/values/setup.xml index 5af3917a..76c64ccf 100644 --- a/test_client/res/values/setup.xml +++ b/test_client/res/values/setup.xml @@ -28,4 +28,5 @@ + Android-ownCloud diff --git a/test_client/src/com/owncloud/android/lib/test_project/TestActivity.java b/test_client/src/com/owncloud/android/lib/test_project/TestActivity.java index c399bb49..a74d24ca 100644 --- a/test_client/src/com/owncloud/android/lib/test_project/TestActivity.java +++ b/test_client/src/com/owncloud/android/lib/test_project/TestActivity.java @@ -72,6 +72,7 @@ public class TestActivity extends Activity { private String mServerUri; private String mUser; private String mPass; + private static String mUserAgent; private static final int BUFFER_SIZE = 1024; @@ -90,6 +91,7 @@ public class TestActivity extends Activity { mServerUri = getString(R.string.server_base_url); mUser = getString(R.string.username); mPass = getString(R.string.password); + mUserAgent = getString(R.string.user_agent); Protocol pr = Protocol.getProtocol("https"); if (pr == null || !(pr.getSocketFactory() instanceof SelfSignedConfidentSslSocketFactory)) { @@ -104,7 +106,8 @@ public class TestActivity extends Activity { } } - mClient = new OwnCloudClient(Uri.parse(mServerUri), NetworkUtils.getMultiThreadedConnManager()); + mClient = new OwnCloudClient(Uri.parse(mServerUri), NetworkUtils.getMultiThreadedConnManager(), + mUserAgent); mClient.setDefaultTimeouts( OwnCloudClientFactory.DEFAULT_DATA_TIMEOUT, OwnCloudClientFactory.DEFAULT_CONNECTION_TIMEOUT); @@ -156,7 +159,7 @@ public class TestActivity extends Activity { CreateRemoteFolderOperation createOperation = new CreateRemoteFolderOperation(remotePath, createFullPath); - RemoteOperationResult result = createOperation.execute(client); + RemoteOperationResult result = createOperation.execute(client, mUserAgent); return result; } @@ -174,7 +177,7 @@ public class TestActivity extends Activity { public RemoteOperationResult renameFile(String oldName, String oldRemotePath, String newName, boolean isFolder) { RenameRemoteFileOperation renameOperation = new RenameRemoteFileOperation(oldName, oldRemotePath, newName, isFolder); - RemoteOperationResult result = renameOperation.execute(mClient); + RemoteOperationResult result = renameOperation.execute(mClient, mUserAgent); return result; } @@ -187,7 +190,7 @@ public class TestActivity extends Activity { */ public RemoteOperationResult removeFile(String remotePath) { RemoveRemoteFileOperation removeOperation = new RemoveRemoteFileOperation(remotePath); - RemoteOperationResult result = removeOperation.execute(mClient); + RemoteOperationResult result = removeOperation.execute(mClient, mUserAgent); return result; } @@ -199,7 +202,7 @@ public class TestActivity extends Activity { */ public static RemoteOperationResult removeFile(String remotePath, OwnCloudClient client) { RemoveRemoteFileOperation removeOperation = new RemoveRemoteFileOperation(remotePath); - RemoteOperationResult result = removeOperation.execute(client); + RemoteOperationResult result = removeOperation.execute(client, mUserAgent); return result; } @@ -213,7 +216,7 @@ public class TestActivity extends Activity { public RemoteOperationResult readFile(String remotePath) { ReadRemoteFolderOperation readOperation= new ReadRemoteFolderOperation(remotePath); - RemoteOperationResult result = readOperation.execute(mClient); + RemoteOperationResult result = readOperation.execute(mClient, mUserAgent); return result; } @@ -232,7 +235,7 @@ public class TestActivity extends Activity { folder.mkdirs(); DownloadRemoteFileOperation downloadOperation = new DownloadRemoteFileOperation(remoteFile.getRemotePath(), folder.getAbsolutePath()); - RemoteOperationResult result = downloadOperation.execute(mClient); + RemoteOperationResult result = downloadOperation.execute(mClient, mUserAgent); return result; } @@ -273,7 +276,7 @@ public class TestActivity extends Activity { ); } - RemoteOperationResult result = uploadOperation.execute(client); + RemoteOperationResult result = uploadOperation.execute(client, mUserAgent); return result; } @@ -284,7 +287,7 @@ public class TestActivity extends Activity { public RemoteOperationResult getShares(){ GetRemoteSharesOperation getOperation = new GetRemoteSharesOperation(); - RemoteOperationResult result = getOperation.execute(mClient); + RemoteOperationResult result = getOperation.execute(mClient, mUserAgent); return result; } @@ -312,7 +315,7 @@ public class TestActivity extends Activity { String password, int permissions){ CreateRemoteShareOperation createOperation = new CreateRemoteShareOperation(path, shareType, shareWith, publicUpload, password, permissions); - RemoteOperationResult result = createOperation.execute(mClient); + RemoteOperationResult result = createOperation.execute(mClient, mUserAgent); return result; } @@ -326,7 +329,7 @@ public class TestActivity extends Activity { public RemoteOperationResult removeShare(int idShare) { RemoveRemoteShareOperation removeOperation = new RemoveRemoteShareOperation(idShare); - RemoteOperationResult result = removeOperation.execute(mClient); + RemoteOperationResult result = removeOperation.execute(mClient, mUserAgent); return result; diff --git a/test_client/tests/res/values/strings.xml b/test_client/tests/res/values/strings.xml index 66f25f1e..bc3a4bb1 100644 --- a/test_client/tests/res/values/strings.xml +++ b/test_client/tests/res/values/strings.xml @@ -26,5 +26,6 @@ Oc_framework-testTest + Android-ownCloud diff --git a/test_client/tests/src/com/owncloud/android/lib/test_project/test/MoveFileTest.java b/test_client/tests/src/com/owncloud/android/lib/test_project/test/MoveFileTest.java index e83f0478..14a3bed5 100644 --- a/test_client/tests/src/com/owncloud/android/lib/test_project/test/MoveFileTest.java +++ b/test_client/tests/src/com/owncloud/android/lib/test_project/test/MoveFileTest.java @@ -279,7 +279,8 @@ public class MoveFileTest extends RemoteTest { mBaseFolderPath + TARGET_PATH_TO_FILE_1, false ); - RemoteOperationResult result = moveOperation.execute(mClient); + RemoteOperationResult result = moveOperation.execute(mClient, + getContext().getString(R.string.user_agent)); assertTrue(result.isSuccess()); // move & rename file, different location @@ -288,7 +289,8 @@ public class MoveFileTest extends RemoteTest { mBaseFolderPath + TARGET_PATH_TO_FILE_2_RENAMED, false ); - result = moveOperation.execute(mClient); + result = moveOperation.execute(mClient, + getContext().getString(R.string.user_agent)); assertTrue(result.isSuccess()); // move & rename file, same location (rename file) @@ -297,7 +299,8 @@ public class MoveFileTest extends RemoteTest { mBaseFolderPath + SRC_PATH_TO_FILE_3_RENAMED, false ); - result = moveOperation.execute(mClient); + result = moveOperation.execute(mClient, + getContext().getString(R.string.user_agent)); assertTrue(result.isSuccess()); // move empty folder @@ -306,7 +309,8 @@ public class MoveFileTest extends RemoteTest { mBaseFolderPath + TARGET_PATH_TO_EMPTY_FOLDER, false ); - result = moveOperation.execute(mClient); + result = moveOperation.execute(mClient, + getContext().getString(R.string.user_agent)); assertTrue(result.isSuccess()); // move non-empty folder @@ -315,7 +319,8 @@ public class MoveFileTest extends RemoteTest { mBaseFolderPath + TARGET_PATH_TO_FULL_FOLDER_1, false ); - result = moveOperation.execute(mClient); + result = moveOperation.execute(mClient, + getContext().getString(R.string.user_agent)); assertTrue(result.isSuccess()); // move & rename folder, different location @@ -324,7 +329,8 @@ public class MoveFileTest extends RemoteTest { mBaseFolderPath + TARGET_PATH_TO_FULL_FOLDER_2_RENAMED, false ); - result = moveOperation.execute(mClient); + result = moveOperation.execute(mClient, + getContext().getString(R.string.user_agent)); assertTrue(result.isSuccess()); // move & rename folder, same location (rename folder) @@ -333,7 +339,8 @@ public class MoveFileTest extends RemoteTest { mBaseFolderPath + SRC_PATH_TO_FULL_FOLDER_3_RENAMED, false ); - result = moveOperation.execute(mClient); + result = moveOperation.execute(mClient, + getContext().getString(R.string.user_agent)); assertTrue(result.isSuccess()); // move for nothing (success, but no interaction with network) @@ -342,7 +349,8 @@ public class MoveFileTest extends RemoteTest { mBaseFolderPath + SRC_PATH_TO_FILE_4, false ); - result = moveOperation.execute(mClient); + result = moveOperation.execute(mClient, + getContext().getString(R.string.user_agent)); assertTrue(result.isSuccess()); // move overwriting @@ -351,7 +359,8 @@ public class MoveFileTest extends RemoteTest { mBaseFolderPath + TARGET_PATH_TO_ALREADY_EXISTENT_EMPTY_FOLDER_4, true ); - result = moveOperation.execute(mClient); + result = moveOperation.execute(mClient, + getContext().getString(R.string.user_agent)); assertTrue(result.isSuccess()); @@ -363,7 +372,8 @@ public class MoveFileTest extends RemoteTest { mBaseFolderPath + TARGET_PATH_TO_NON_EXISTENT_FILE, false ); - result = moveOperation.execute(mClient); + result = moveOperation.execute(mClient, + getContext().getString(R.string.user_agent)); assertTrue(result.getCode() == ResultCode.FILE_NOT_FOUND); // folder to move into does no exist @@ -372,7 +382,8 @@ public class MoveFileTest extends RemoteTest { mBaseFolderPath + TARGET_PATH_TO_FILE_5_INTO_NON_EXISTENT_FOLDER, false ); - result = moveOperation.execute(mClient); + result = moveOperation.execute(mClient, + getContext().getString(R.string.user_agent)); assertTrue(result.getHttpCode() == HttpStatus.SC_CONFLICT); // target location (renaming) has invalid characters @@ -381,7 +392,8 @@ public class MoveFileTest extends RemoteTest { mBaseFolderPath + TARGET_PATH_RENAMED_WITH_INVALID_CHARS, false ); - result = moveOperation.execute(mClient); + result = moveOperation.execute(mClient, + getContext().getString(R.string.user_agent)); assertTrue(result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME); // name collision @@ -390,7 +402,8 @@ public class MoveFileTest extends RemoteTest { mBaseFolderPath + TARGET_PATH_TO_ALREADY_EXISTENT_FILE_7, false ); - result = moveOperation.execute(mClient); + result = moveOperation.execute(mClient, + getContext().getString(R.string.user_agent)); assertTrue(result.getCode() == ResultCode.INVALID_OVERWRITE); // move a folder into a descendant @@ -399,7 +412,8 @@ public class MoveFileTest extends RemoteTest { mBaseFolderPath + SRC_PATH_TO_EMPTY_FOLDER, false ); - result = moveOperation.execute(mClient); + result = moveOperation.execute(mClient, + getContext().getString(R.string.user_agent)); assertTrue(result.getCode() == ResultCode.INVALID_MOVE_INTO_DESCENDANT); } @@ -436,7 +450,8 @@ public class MoveFileTest extends RemoteTest { mClient = new OwnCloudClient( Uri.parse(mServerUri), - NetworkUtils.getMultiThreadedConnManager() + NetworkUtils.getMultiThreadedConnManager(), + getContext().getString(R.string.user_agent) ); mClient.setDefaultTimeouts( OwnCloudClientFactory.DEFAULT_DATA_TIMEOUT, diff --git a/test_client/tests/src/com/owncloud/android/lib/test_project/test/OwnCloudClientTest.java b/test_client/tests/src/com/owncloud/android/lib/test_project/test/OwnCloudClientTest.java index 57413400..232b7716 100644 --- a/test_client/tests/src/com/owncloud/android/lib/test_project/test/OwnCloudClientTest.java +++ b/test_client/tests/src/com/owncloud/android/lib/test_project/test/OwnCloudClientTest.java @@ -64,6 +64,7 @@ public class OwnCloudClientTest extends AndroidTestCase { private Uri mServerUri; private String mUsername; private String mPassword; + private String mUserAgent; public OwnCloudClientTest() { super(); @@ -90,12 +91,14 @@ public class OwnCloudClientTest extends AndroidTestCase { mServerUri = Uri.parse(getContext().getString(R.string.server_base_url)); mUsername = getContext().getString(R.string.username); mPassword = getContext().getString(R.string.password); + mUserAgent = getContext().getString(R.string.user_agent); } public void testConstructor() { try { - new OwnCloudClient(null, NetworkUtils.getMultiThreadedConnManager()); + new OwnCloudClient(null, NetworkUtils.getMultiThreadedConnManager(), + mUserAgent); throw new AssertionFailedError("Accepted NULL parameter"); } catch(Exception e) { @@ -104,7 +107,7 @@ public class OwnCloudClientTest extends AndroidTestCase { } try { - new OwnCloudClient(mServerUri, null); + new OwnCloudClient(mServerUri, null, mUserAgent); throw new AssertionFailedError("Accepted NULL parameter"); } catch(Exception e) { @@ -113,17 +116,19 @@ public class OwnCloudClientTest extends AndroidTestCase { } OwnCloudClient client = - new OwnCloudClient(mServerUri, NetworkUtils.getMultiThreadedConnManager()); + new OwnCloudClient(mServerUri, NetworkUtils.getMultiThreadedConnManager(), + mUserAgent); assertNotNull("OwnCloudClient instance not built", client); assertEquals("Wrong user agent", client.getParams().getParameter(HttpMethodParams.USER_AGENT), - OwnCloudClient.USER_AGENT); + mUserAgent); } public void testGetSetCredentials() { OwnCloudClient client = - new OwnCloudClient(mServerUri, NetworkUtils.getMultiThreadedConnManager()); + new OwnCloudClient(mServerUri, NetworkUtils.getMultiThreadedConnManager(), + mUserAgent); assertNotNull("Returned NULL credentials", client.getCredentials()); assertEquals("Not instanced without credentials", @@ -146,7 +151,8 @@ public class OwnCloudClientTest extends AndroidTestCase { public void testExecuteMethodWithTimeouts() throws HttpException, IOException { OwnCloudClient client = - new OwnCloudClient(mServerUri, NetworkUtils.getMultiThreadedConnManager()); + new OwnCloudClient(mServerUri, NetworkUtils.getMultiThreadedConnManager(), + mUserAgent); int connectionTimeout = client.getConnectionTimeout(); int readTimeout = client.getDataTimeout(); @@ -194,7 +200,8 @@ public class OwnCloudClientTest extends AndroidTestCase { public void testExecuteMethod() { OwnCloudClient client = - new OwnCloudClient(mServerUri, NetworkUtils.getMultiThreadedConnManager()); + new OwnCloudClient(mServerUri, NetworkUtils.getMultiThreadedConnManager(), + mUserAgent); HeadMethod head = new HeadMethod(client.getWebdavUri() + "/"); int status = -1; try { @@ -215,7 +222,8 @@ public class OwnCloudClientTest extends AndroidTestCase { public void testExhaustResponse() { OwnCloudClient client = - new OwnCloudClient(mServerUri, NetworkUtils.getMultiThreadedConnManager()); + new OwnCloudClient(mServerUri, NetworkUtils.getMultiThreadedConnManager(), + mUserAgent); PropFindMethod propfind = null; try { @@ -257,7 +265,8 @@ public class OwnCloudClientTest extends AndroidTestCase { public void testGetSetDefaultTimeouts() { OwnCloudClient client = - new OwnCloudClient(mServerUri, NetworkUtils.getMultiThreadedConnManager()); + new OwnCloudClient(mServerUri, NetworkUtils.getMultiThreadedConnManager(), + mUserAgent); int oldDataTimeout = client.getDataTimeout(); int oldConnectionTimeout = client.getConnectionTimeout(); @@ -297,7 +306,8 @@ public class OwnCloudClientTest extends AndroidTestCase { public void testGetWebdavUri() { OwnCloudClient client = - new OwnCloudClient(mServerUri, NetworkUtils.getMultiThreadedConnManager()); + new OwnCloudClient(mServerUri, NetworkUtils.getMultiThreadedConnManager(), + mUserAgent); client.setCredentials(OwnCloudCredentialsFactory.newBearerCredentials("fakeToken")); Uri webdavUri = client.getWebdavUri(); assertTrue("WebDAV URI does not point to the right entry point for OAuth2 " + @@ -335,7 +345,8 @@ public class OwnCloudClientTest extends AndroidTestCase { public void testGetSetBaseUri() { OwnCloudClient client = - new OwnCloudClient(mServerUri, NetworkUtils.getMultiThreadedConnManager()); + new OwnCloudClient(mServerUri, NetworkUtils.getMultiThreadedConnManager(), + mUserAgent); assertEquals("Returned base URI different that URI passed to constructor", mServerUri, client.getBaseUri()); diff --git a/test_client/tests/src/com/owncloud/android/lib/test_project/test/SimpleFactoryManagerTest.java b/test_client/tests/src/com/owncloud/android/lib/test_project/test/SimpleFactoryManagerTest.java index 6122e9f3..bd109792 100644 --- a/test_client/tests/src/com/owncloud/android/lib/test_project/test/SimpleFactoryManagerTest.java +++ b/test_client/tests/src/com/owncloud/android/lib/test_project/test/SimpleFactoryManagerTest.java @@ -95,13 +95,16 @@ public class SimpleFactoryManagerTest extends AndroidTestCase { public void testGetClientFor() { try { - OwnCloudClient client = mSFMgr.getClientFor(mValidAccount, getContext()); + OwnCloudClient client = mSFMgr.getClientFor(mValidAccount, getContext(), + getContext().getString(R.string.user_agent)); assertNotSame("Got same client instances for same account", - client, mSFMgr.getClientFor(mValidAccount, getContext())); + client, mSFMgr.getClientFor(mValidAccount, getContext(), + getContext().getString(R.string.user_agent))); assertNotSame("Got same client instances for different accounts", - client, mSFMgr.getClientFor(mAnonymousAccount, getContext())); + client, mSFMgr.getClientFor(mAnonymousAccount, getContext(), + getContext().getString(R.string.user_agent))); } catch (Exception e) { throw new AssertionFailedError("Exception getting client for account: " + e.getMessage()); @@ -111,10 +114,12 @@ public class SimpleFactoryManagerTest extends AndroidTestCase { public void testRemoveClientFor() { try { - OwnCloudClient client = mSFMgr.getClientFor(mValidAccount, getContext()); + OwnCloudClient client = mSFMgr.getClientFor(mValidAccount, getContext(), + getContext().getString(R.string.user_agent)); mSFMgr.removeClientFor(mValidAccount); assertNotSame("Got same client instance after removing it from manager", - client, mSFMgr.getClientFor(mValidAccount, getContext())); + client, mSFMgr.getClientFor(mValidAccount, getContext(), + getContext().getString(R.string.user_agent))); } catch (Exception e) { throw new AssertionFailedError("Exception getting client for account: " + e.getMessage()); diff --git a/test_client/tests/src/com/owncloud/android/lib/test_project/test/SingleSessionManagerTest.java b/test_client/tests/src/com/owncloud/android/lib/test_project/test/SingleSessionManagerTest.java index f565cc0e..b81cd05f 100644 --- a/test_client/tests/src/com/owncloud/android/lib/test_project/test/SingleSessionManagerTest.java +++ b/test_client/tests/src/com/owncloud/android/lib/test_project/test/SingleSessionManagerTest.java @@ -94,13 +94,16 @@ public class SingleSessionManagerTest extends AndroidTestCase { public void testGetClientFor() { try { - OwnCloudClient client1 = mSSMgr.getClientFor(mValidAccount, getContext()); - OwnCloudClient client2 = mSSMgr.getClientFor(mAnonymousAccount, getContext()); + OwnCloudClient client1 = mSSMgr.getClientFor(mValidAccount, getContext(), + getContext().getString(R.string.user_agent)); + OwnCloudClient client2 = mSSMgr.getClientFor(mAnonymousAccount, getContext(), + getContext().getString(R.string.user_agent)); assertNotSame("Got same client instances for different accounts", client1, client2); assertSame("Got different client instances for same account", - client1, mSSMgr.getClientFor(mValidAccount, getContext())); + client1, mSSMgr.getClientFor(mValidAccount, getContext(), + getContext().getString(R.string.user_agent))); } catch (Exception e) { throw new AssertionFailedError("Exception getting client for account: " + e.getMessage()); @@ -111,10 +114,12 @@ public class SingleSessionManagerTest extends AndroidTestCase { public void testRemoveClientFor() { try { - OwnCloudClient client1 = mSSMgr.getClientFor(mValidAccount, getContext()); + OwnCloudClient client1 = mSSMgr.getClientFor(mValidAccount, getContext(), + getContext().getString(R.string.user_agent)); mSSMgr.removeClientFor(mValidAccount); assertNotSame("Got same client instance after removing it from manager", - client1, mSSMgr.getClientFor(mValidAccount, getContext())); + client1, mSSMgr.getClientFor(mValidAccount, getContext(), + getContext().getString(R.string.user_agent))); } catch (Exception e) { throw new AssertionFailedError("Exception getting client for account: " + e.getMessage()); } From 94ac3a93d3da2719c24c4f62c95e5de3e7b5e650 Mon Sep 17 00:00:00 2001 From: masensio Date: Mon, 23 Mar 2015 14:24:03 +0100 Subject: [PATCH 05/10] Fix bug: User agent: jakarta is shown when doing GET request --- .../android/lib/common/OwnCloudClient.java | 21 ++++++++++++------- .../status/GetRemoteStatusOperation.java | 8 ++++++- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/com/owncloud/android/lib/common/OwnCloudClient.java b/src/com/owncloud/android/lib/common/OwnCloudClient.java index ab12a5cb..2badcd55 100644 --- a/src/com/owncloud/android/lib/common/OwnCloudClient.java +++ b/src/com/owncloud/android/lib/common/OwnCloudClient.java @@ -41,6 +41,7 @@ import org.apache.commons.httpclient.URI; import org.apache.commons.httpclient.cookie.CookiePolicy; import org.apache.commons.httpclient.methods.HeadMethod; import org.apache.commons.httpclient.params.HttpMethodParams; +import org.apache.commons.httpclient.params.HttpParams; import org.apache.http.HttpStatus; import org.apache.http.params.CoreProtocolPNames; @@ -66,6 +67,7 @@ public class OwnCloudClient extends HttpClient { private int mInstanceNumber = 0; private Uri mBaseUri; + private String mUserAgent; /** * Constructor @@ -80,8 +82,9 @@ public class OwnCloudClient extends HttpClient { mInstanceNumber = sIntanceCounter++; Log_OC.d(TAG + " #" + mInstanceNumber, "Creating OwnCloudClient"); - - getParams().setParameter(HttpMethodParams.USER_AGENT, userAgent); + + mUserAgent = userAgent; + getParams().setParameter(HttpMethodParams.USER_AGENT, mUserAgent); getParams().setParameter( CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); @@ -167,9 +170,9 @@ public class OwnCloudClient extends HttpClient { * The timeouts are both in milliseconds; 0 means 'infinite'; * < 0 means 'do not change the default' * - * @param method HTTP method request. - * @param readTimeout Timeout to set for data reception - * @param conntionTimout Timeout to set for connection establishment + * @param method HTTP method request. + * @param readTimeout Timeout to set for data reception + * @param connectionTimeout Timeout to set for connection establishment */ public int executeMethod(HttpMethodBase method, int readTimeout, int connectionTimeout) throws HttpException, IOException { @@ -206,7 +209,11 @@ public class OwnCloudClient extends HttpClient { */ customRedirectionNeeded = mFollowRedirects; } - + + // Update User Agent + HttpParams params = method.getParams(); + params.setParameter(HttpMethodParams.USER_AGENT, mUserAgent); + Log_OC.d(TAG + " #" + mInstanceNumber, "REQUEST " + method.getName() + " " + method.getPath()); @@ -226,7 +233,7 @@ public class OwnCloudClient extends HttpClient { return status; } catch (IOException e) { - Log_OC.d(TAG + " #" + mInstanceNumber, "Exception occured", e); + Log_OC.d(TAG + " #" + mInstanceNumber, "Exception occurred", e); throw e; } } diff --git a/src/com/owncloud/android/lib/resources/status/GetRemoteStatusOperation.java b/src/com/owncloud/android/lib/resources/status/GetRemoteStatusOperation.java index 5d841062..7091163b 100644 --- a/src/com/owncloud/android/lib/resources/status/GetRemoteStatusOperation.java +++ b/src/com/owncloud/android/lib/resources/status/GetRemoteStatusOperation.java @@ -28,6 +28,8 @@ import java.util.ArrayList; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.GetMethod; +import org.apache.commons.httpclient.params.HttpMethodParams; +import org.apache.commons.httpclient.params.HttpParams; import org.json.JSONException; import org.json.JSONObject; @@ -76,7 +78,11 @@ public class GetRemoteStatusOperation extends RemoteOperation { try { get = new GetMethod(baseUrlSt + AccountUtils.STATUS_PATH); get.addRequestHeader(USER_AGENT_HEADER, getUserAgent()); - + + HttpParams params = get.getParams().getDefaultParams(); + params.setParameter(HttpMethodParams.USER_AGENT, getUserAgent()); + get.getParams().setDefaults(params); + client.setFollowRedirects(false); boolean isRedirectToNonSecureConnection = false; int status = client.executeMethod(get, TRY_CONNECTION_TIMEOUT, TRY_CONNECTION_TIMEOUT); From 9990eeb7ff68dd6e3a8056768ddce93b4e04bf99 Mon Sep 17 00:00:00 2001 From: masensio Date: Tue, 24 Mar 2015 14:31:40 +0100 Subject: [PATCH 06/10] Refactor parameter user_agent out of RemoteOperation and children --- .../lib/sampleclient/MainActivity.java | 11 +++-- .../android/lib/common/OwnCloudClient.java | 10 ++--- .../lib/common/OwnCloudClientFactory.java | 16 +++---- .../lib/common/OwnCloudClientManager.java | 2 +- .../common/OwnCloudClientManagerFactory.java | 12 ++++- .../lib/common/SimpleFactoryManager.java | 5 +-- .../lib/common/SingleSessionManager.java | 6 +-- .../common/operations/RemoteOperation.java | 43 +++++------------- .../ChunkedUploadRemoteFileOperation.java | 1 - .../files/CreateRemoteFolderOperation.java | 9 ++-- .../files/DownloadRemoteFileOperation.java | 1 - .../files/ExistenceCheckRemoteOperation.java | 1 - .../files/MoveRemoteFileOperation.java | 1 - .../files/ReadRemoteFileOperation.java | 1 - .../files/ReadRemoteFolderOperation.java | 1 - .../files/RemoveRemoteFileOperation.java | 1 - .../files/RenameRemoteFileOperation.java | 1 - .../files/UploadRemoteFileOperation.java | 1 - .../shares/CreateRemoteShareOperation.java | 1 - .../GetRemoteSharesForFileOperation.java | 1 - .../shares/GetRemoteSharesOperation.java | 1 - .../shares/RemoveRemoteShareOperation.java | 1 - .../status/GetRemoteStatusOperation.java | 5 ++- .../users/GetRemoteUserNameOperation.java | 1 - .../lib/test_project/TestActivity.java | 25 +++++------ .../lib/test_project/test/MoveFileTest.java | 45 +++++++------------ .../test_project/test/OwnCloudClientTest.java | 29 +++++------- .../test/SimpleFactoryManagerTest.java | 15 +++---- .../test/SingleSessionManagerTest.java | 15 +++---- 29 files changed, 96 insertions(+), 166 deletions(-) diff --git a/sample_client/src/com/owncloud/android/lib/sampleclient/MainActivity.java b/sample_client/src/com/owncloud/android/lib/sampleclient/MainActivity.java index 2e15b25a..11ea54f2 100644 --- a/sample_client/src/com/owncloud/android/lib/sampleclient/MainActivity.java +++ b/sample_client/src/com/owncloud/android/lib/sampleclient/MainActivity.java @@ -79,8 +79,7 @@ public class MainActivity extends Activity implements OnRemoteOperationListener, mHandler = new Handler(); Uri serverUri = Uri.parse(getString(R.string.server_base_url)); - mClient = OwnCloudClientFactory.createOwnCloudClient(serverUri, this, true, - getString(R.string.user_agent)); + mClient = OwnCloudClientFactory.createOwnCloudClient(serverUri, this, true); mClient.setCredentials( OwnCloudCredentialsFactory.newBasicCredentials( getString(R.string.username), @@ -150,7 +149,7 @@ public class MainActivity extends Activity implements OnRemoteOperationListener, private void startRefresh() { ReadRemoteFolderOperation refreshOperation = new ReadRemoteFolderOperation(FileUtils.PATH_SEPARATOR); - refreshOperation.execute(mClient, getString(R.string.user_agent), this, mHandler); + refreshOperation.execute(mClient, this, mHandler); } private void startUpload() { @@ -160,7 +159,7 @@ public class MainActivity extends Activity implements OnRemoteOperationListener, String mimeType = getString(R.string.sample_file_mimetype); UploadRemoteFileOperation uploadOperation = new UploadRemoteFileOperation(fileToUpload.getAbsolutePath(), remotePath, mimeType); uploadOperation.addDatatransferProgressListener(this); - uploadOperation.execute(mClient, getString(R.string.user_agent), this, mHandler); + uploadOperation.execute(mClient, this, mHandler); } private void startRemoteDeletion() { @@ -168,7 +167,7 @@ public class MainActivity extends Activity implements OnRemoteOperationListener, File fileToUpload = upFolder.listFiles()[0]; String remotePath = FileUtils.PATH_SEPARATOR + fileToUpload.getName(); RemoveRemoteFileOperation removeOperation = new RemoveRemoteFileOperation(remotePath); - removeOperation.execute(mClient, getString(R.string.user_agent), this, mHandler); + removeOperation.execute(mClient, this, mHandler); } private void startDownload() { @@ -179,7 +178,7 @@ public class MainActivity extends Activity implements OnRemoteOperationListener, String remotePath = FileUtils.PATH_SEPARATOR + fileToUpload.getName(); DownloadRemoteFileOperation downloadOperation = new DownloadRemoteFileOperation(remotePath, downFolder.getAbsolutePath()); downloadOperation.addDatatransferProgressListener(this); - downloadOperation.execute(mClient, getString(R.string.user_agent), this, mHandler); + downloadOperation.execute(mClient, this, mHandler); } @SuppressWarnings("deprecation") diff --git a/src/com/owncloud/android/lib/common/OwnCloudClient.java b/src/com/owncloud/android/lib/common/OwnCloudClient.java index 2badcd55..b6804708 100644 --- a/src/com/owncloud/android/lib/common/OwnCloudClient.java +++ b/src/com/owncloud/android/lib/common/OwnCloudClient.java @@ -67,12 +67,11 @@ public class OwnCloudClient extends HttpClient { private int mInstanceNumber = 0; private Uri mBaseUri; - private String mUserAgent; /** * Constructor */ - public OwnCloudClient(Uri baseUri, HttpConnectionManager connectionMgr, String userAgent) { + public OwnCloudClient(Uri baseUri, HttpConnectionManager connectionMgr) { super(connectionMgr); if (baseUri == null) { @@ -83,8 +82,8 @@ public class OwnCloudClient extends HttpClient { mInstanceNumber = sIntanceCounter++; Log_OC.d(TAG + " #" + mInstanceNumber, "Creating OwnCloudClient"); - mUserAgent = userAgent; - getParams().setParameter(HttpMethodParams.USER_AGENT, mUserAgent); + String userAgent = OwnCloudClientManagerFactory.getUserAgent(); + getParams().setParameter(HttpMethodParams.USER_AGENT, userAgent); getParams().setParameter( CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); @@ -212,7 +211,8 @@ public class OwnCloudClient extends HttpClient { // Update User Agent HttpParams params = method.getParams(); - params.setParameter(HttpMethodParams.USER_AGENT, mUserAgent); + String userAgent = OwnCloudClientManagerFactory.getUserAgent(); + params.setParameter(HttpMethodParams.USER_AGENT, userAgent); Log_OC.d(TAG + " #" + mInstanceNumber, "REQUEST " + method.getName() + " " + method.getPath()); diff --git a/src/com/owncloud/android/lib/common/OwnCloudClientFactory.java b/src/com/owncloud/android/lib/common/OwnCloudClientFactory.java index b0cd5fa3..bc6d61c5 100644 --- a/src/com/owncloud/android/lib/common/OwnCloudClientFactory.java +++ b/src/com/owncloud/android/lib/common/OwnCloudClientFactory.java @@ -61,7 +61,6 @@ public class OwnCloudClientFactory { * * @param account The ownCloud account * @param appContext Android application context - * @param userAgent OwnCloud userAgent string * @return A OwnCloudClient object ready to be used * @throws AuthenticatorException If the authenticator failed to get the authorization * token for the account. @@ -71,8 +70,7 @@ public class OwnCloudClientFactory { * authorization token for the account. * @throws AccountNotFoundException If 'account' is unknown for the AccountManager */ - public static OwnCloudClient createOwnCloudClient (Account account, Context appContext, - String userAgent) + public static OwnCloudClient createOwnCloudClient (Account account, Context appContext) throws OperationCanceledException, AuthenticatorException, IOException, AccountNotFoundException { //Log_OC.d(TAG, "Creating OwnCloudClient associated to " + account.name); @@ -83,7 +81,7 @@ public class OwnCloudClientFactory { am.getUserData(account, AccountUtils.Constants.KEY_SUPPORTS_OAUTH2) != null; boolean isSamlSso = am.getUserData(account, AccountUtils.Constants.KEY_SUPPORTS_SAML_WEB_SSO) != null; - OwnCloudClient client = createOwnCloudClient(baseUri, appContext, !isSamlSso, userAgent); + OwnCloudClient client = createOwnCloudClient(baseUri, appContext, !isSamlSso); if (isOauth2) { String accessToken = am.blockingGetAuthToken( @@ -127,7 +125,7 @@ public class OwnCloudClientFactory { public static OwnCloudClient createOwnCloudClient (Account account, Context appContext, - Activity currentActivity, String userAgent) + Activity currentActivity) throws OperationCanceledException, AuthenticatorException, IOException, AccountNotFoundException { Uri baseUri = Uri.parse(AccountUtils.getBaseUrlForAccount(appContext, account)); @@ -137,7 +135,7 @@ public class OwnCloudClientFactory { am.getUserData(account, AccountUtils.Constants.KEY_SUPPORTS_OAUTH2) != null; boolean isSamlSso = am.getUserData(account, AccountUtils.Constants.KEY_SUPPORTS_SAML_WEB_SSO) != null; - OwnCloudClient client = createOwnCloudClient(baseUri, appContext, !isSamlSso, userAgent); + OwnCloudClient client = createOwnCloudClient(baseUri, appContext, !isSamlSso); if (isOauth2) { // TODO avoid a call to getUserData here AccountManagerFuture future = am.getAuthToken( @@ -204,11 +202,10 @@ public class OwnCloudClientFactory { * * @param uri URL to the ownCloud server; BASE ENTRY POINT, not WebDavPATH * @param context Android context where the OwnCloudClient is being created. - * @param userAgent OwnCloud userAgent string * @return A OwnCloudClient object ready to be used */ public static OwnCloudClient createOwnCloudClient(Uri uri, Context context, - boolean followRedirects, String userAgent) { + boolean followRedirects) { try { NetworkUtils.registerAdvancedSslContext(true, context); } catch (GeneralSecurityException e) { @@ -220,8 +217,7 @@ public class OwnCloudClientFactory { " in the system will be used for HTTPS connections", e); } - OwnCloudClient client = new OwnCloudClient(uri, NetworkUtils.getMultiThreadedConnManager(), - userAgent); + OwnCloudClient client = new OwnCloudClient(uri, NetworkUtils.getMultiThreadedConnManager()); client.setDefaultTimeouts(DEFAULT_DATA_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT); client.setFollowRedirects(followRedirects); diff --git a/src/com/owncloud/android/lib/common/OwnCloudClientManager.java b/src/com/owncloud/android/lib/common/OwnCloudClientManager.java index c9994f91..e16723f1 100644 --- a/src/com/owncloud/android/lib/common/OwnCloudClientManager.java +++ b/src/com/owncloud/android/lib/common/OwnCloudClientManager.java @@ -42,7 +42,7 @@ import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundExce public interface OwnCloudClientManager { - public OwnCloudClient getClientFor(OwnCloudAccount account, Context context, String userAgent) + public OwnCloudClient getClientFor(OwnCloudAccount account, Context context) throws AccountNotFoundException, OperationCanceledException, AuthenticatorException, IOException; diff --git a/src/com/owncloud/android/lib/common/OwnCloudClientManagerFactory.java b/src/com/owncloud/android/lib/common/OwnCloudClientManagerFactory.java index 30384529..bd54a481 100644 --- a/src/com/owncloud/android/lib/common/OwnCloudClientManagerFactory.java +++ b/src/com/owncloud/android/lib/common/OwnCloudClientManagerFactory.java @@ -34,6 +34,8 @@ public class OwnCloudClientManagerFactory { private static OwnCloudClientManager sDefaultSingleton; + private static String sUserAgent; + public static OwnCloudClientManager newDefaultOwnCloudClientManager() { return newOwnCloudClientManager(sDefaultPolicy); } @@ -71,7 +73,15 @@ public class OwnCloudClientManagerFactory { } sDefaultPolicy = policy; } - + + public static void setUserAgent(String userAgent){ + sUserAgent = userAgent; + } + + public static String getUserAgent() { + return sUserAgent; + } + private static boolean defaultSingletonMustBeUpdated(Policy policy) { if (sDefaultSingleton == null) { return false; diff --git a/src/com/owncloud/android/lib/common/SimpleFactoryManager.java b/src/com/owncloud/android/lib/common/SimpleFactoryManager.java index ad7120c0..35ddbda5 100644 --- a/src/com/owncloud/android/lib/common/SimpleFactoryManager.java +++ b/src/com/owncloud/android/lib/common/SimpleFactoryManager.java @@ -40,7 +40,7 @@ public class SimpleFactoryManager implements OwnCloudClientManager { private static final String TAG = SimpleFactoryManager.class.getSimpleName(); @Override - public OwnCloudClient getClientFor(OwnCloudAccount account, Context context, String userAgent) + public OwnCloudClient getClientFor(OwnCloudAccount account, Context context) throws AccountNotFoundException, OperationCanceledException, AuthenticatorException, IOException { @@ -49,8 +49,7 @@ public class SimpleFactoryManager implements OwnCloudClientManager { OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient( account.getBaseUri(), context.getApplicationContext(), - true, - userAgent); + true); Log_OC.v(TAG, " new client {" + (account.getName() != null ? diff --git a/src/com/owncloud/android/lib/common/SingleSessionManager.java b/src/com/owncloud/android/lib/common/SingleSessionManager.java index a69ac35c..4019a6bc 100644 --- a/src/com/owncloud/android/lib/common/SingleSessionManager.java +++ b/src/com/owncloud/android/lib/common/SingleSessionManager.java @@ -62,8 +62,7 @@ public class SingleSessionManager implements OwnCloudClientManager { @Override - public synchronized OwnCloudClient getClientFor(OwnCloudAccount account, Context context, - String userAgent) + public synchronized OwnCloudClient getClientFor(OwnCloudAccount account, Context context) throws AccountNotFoundException, OperationCanceledException, AuthenticatorException, IOException { @@ -109,8 +108,7 @@ public class SingleSessionManager implements OwnCloudClientManager { client = OwnCloudClientFactory.createOwnCloudClient( account.getBaseUri(), context.getApplicationContext(), - true, - userAgent); // TODO remove dependency on OwnCloudClientFactory + true); // TODO remove dependency on OwnCloudClientFactory client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); // enable cookie tracking diff --git a/src/com/owncloud/android/lib/common/operations/RemoteOperation.java b/src/com/owncloud/android/lib/common/operations/RemoteOperation.java index 4fc22829..bae2a3d4 100644 --- a/src/com/owncloud/android/lib/common/operations/RemoteOperation.java +++ b/src/com/owncloud/android/lib/common/operations/RemoteOperation.java @@ -81,9 +81,6 @@ public abstract class RemoteOperation implements Runnable { /** Activity */ private Activity mCallerActivity; - /** User agent */ - private String mUserAgent; - /** * Abstract method to implement the operation in derived classes. @@ -97,15 +94,14 @@ public abstract class RemoteOperation implements Runnable { * Do not call this method from the main thread. * * This method should be used whenever an ownCloud account is available, instead of - * {@link #execute(OwnCloudClient, java.lang.String)}. + * {@link #execute(OwnCloudClient)}. * * @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 userAgent userAgent string * @return Result of the operation. */ - public RemoteOperationResult execute(Account account, Context context, String userAgent) { + public RemoteOperationResult execute(Account account, Context context) { if (account == null) throw new IllegalArgumentException("Trying to execute a remote operation with a NULL " + "Account"); @@ -114,11 +110,10 @@ public abstract class RemoteOperation implements Runnable { "Context"); mAccount = account; mContext = context.getApplicationContext(); - mUserAgent = userAgent; try { OwnCloudAccount ocAccount = new OwnCloudAccount(mAccount, mContext); mClient = OwnCloudClientManagerFactory.getDefaultSingleton(). - getClientFor(ocAccount, mContext, mUserAgent); + getClientFor(ocAccount, mContext); } catch (Exception e) { Log_OC.e(TAG, "Error while trying to access to " + mAccount.name, e); return new RemoteOperationResult(e); @@ -134,15 +129,13 @@ public abstract class RemoteOperation implements Runnable { * * @param client Client object to reach an ownCloud server during the execution of * the operation. - * @param userAgent userAgent string * @return Result of the operation. */ - public RemoteOperationResult execute(OwnCloudClient client, String userAgent) { + public RemoteOperationResult execute(OwnCloudClient client) { if (client == null) throw new IllegalArgumentException("Trying to execute a remote operation with a NULL " + "OwnCloudClient"); mClient = client; - mUserAgent = userAgent; return run(client); } @@ -151,10 +144,10 @@ public abstract class RemoteOperation implements Runnable { * Asynchronously executes the remote operation * * This method should be used whenever an ownCloud account is available, instead of - * {@link #execute(OwnCloudClient, java.lang.String)}. + * {@link #execute(OwnCloudClient)}. * * @deprecated This method will be removed in version 1.0. - * Use {@link #execute(Account, Context, String, OnRemoteOperationListener, + * Use {@link #execute(Account, Context, OnRemoteOperationListener, * Handler)} instead. * * @param account ownCloud account in remote ownCloud server to reach during @@ -193,18 +186,17 @@ public abstract class RemoteOperation implements Runnable { * Asynchronously executes the remote operation * * This method should be used whenever an ownCloud account is available, - * instead of {@link #execute(OwnCloudClient, String, OnRemoteOperationListener, Handler))}. + * instead of {@link #execute(OwnCloudClient, OnRemoteOperationListener, Handler))}. * * @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 userAgent userAgent string * @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 Thread execute(Account account, Context context, String userAgent, + public Thread execute(Account account, Context context, OnRemoteOperationListener listener, Handler listenerHandler) { if (account == null) @@ -234,20 +226,18 @@ public abstract class RemoteOperation implements Runnable { * * @param client Client object to reach an ownCloud server * during the execution of the operation. - * @param userAgent userAgent string * @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 Thread execute(OwnCloudClient client, String userAgent, + public Thread execute(OwnCloudClient client, OnRemoteOperationListener listener, Handler listenerHandler) { if (client == null) { throw new IllegalArgumentException ("Trying to execute a remote operation with a NULL OwnCloudClient"); } mClient = client; - mUserAgent = userAgent; if (listener == null) { throw new IllegalArgumentException @@ -270,7 +260,7 @@ public abstract class RemoteOperation implements Runnable { /** * Asynchronous execution of the operation - * started by {@link RemoteOperation#execute(OwnCloudClient, String, + * started by {@link RemoteOperation#execute(OwnCloudClient, * OnRemoteOperationListener, Handler)}, * and result posting. * @@ -287,12 +277,12 @@ public abstract class RemoteOperation implements Runnable { /** DEPRECATED BLOCK - will be removed at version 1.0 */ if (mCallerActivity != null) { mClient = OwnCloudClientFactory.createOwnCloudClient( - mAccount, mContext, mCallerActivity, mUserAgent); + mAccount, mContext, mCallerActivity); } else { /** EOF DEPRECATED */ OwnCloudAccount ocAccount = new OwnCloudAccount(mAccount, mContext); mClient = OwnCloudClientManagerFactory.getDefaultSingleton(). - getClientFor(ocAccount, mContext, mUserAgent); + getClientFor(ocAccount, mContext); } } else { @@ -371,13 +361,4 @@ public abstract class RemoteOperation implements Runnable { return mClient; } - /** - * Returns the user agent string - * @return - */ - public final String getUserAgent() { - return mUserAgent; - } - - } diff --git a/src/com/owncloud/android/lib/resources/files/ChunkedUploadRemoteFileOperation.java b/src/com/owncloud/android/lib/resources/files/ChunkedUploadRemoteFileOperation.java index beeef181..89379dda 100644 --- a/src/com/owncloud/android/lib/resources/files/ChunkedUploadRemoteFileOperation.java +++ b/src/com/owncloud/android/lib/resources/files/ChunkedUploadRemoteFileOperation.java @@ -75,7 +75,6 @@ public class ChunkedUploadRemoteFileOperation extends UploadRemoteFileOperation } mPutMethod = new PutMethod(uriPrefix + chunkCount + "-" + chunkIndex); mPutMethod.addRequestHeader(OC_CHUNKED_HEADER, OC_CHUNKED_HEADER); - mPutMethod.addRequestHeader(USER_AGENT_HEADER, getUserAgent()); ((ChunkFromFileChannelRequestEntity)mEntity).setOffset(offset); mPutMethod.setRequestEntity(mEntity); status = client.executeMethod(mPutMethod); diff --git a/src/com/owncloud/android/lib/resources/files/CreateRemoteFolderOperation.java b/src/com/owncloud/android/lib/resources/files/CreateRemoteFolderOperation.java index 9f611b2a..0d248e6f 100644 --- a/src/com/owncloud/android/lib/resources/files/CreateRemoteFolderOperation.java +++ b/src/com/owncloud/android/lib/resources/files/CreateRemoteFolderOperation.java @@ -78,8 +78,7 @@ public class CreateRemoteFolderOperation extends RemoteOperation { result = createFolder(client); if (!result.isSuccess() && mCreateFullPath && RemoteOperationResult.ResultCode.CONFLICT == result.getCode()) { - result = createParentFolder(FileUtils.getParentPath(mRemotePath), client, - getUserAgent()); + result = createParentFolder(FileUtils.getParentPath(mRemotePath), client); if (result.isSuccess()) { result = createFolder(client); // second (and last) try } @@ -98,7 +97,6 @@ public class CreateRemoteFolderOperation extends RemoteOperation { MkColMethod mkcol = null; try { mkcol = new MkColMethod(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath)); - mkcol.addRequestHeader(USER_AGENT_HEADER, getUserAgent()); int status = client.executeMethod(mkcol, READ_TIMEOUT, CONNECTION_TIMEOUT); result = new RemoteOperationResult(mkcol.succeeded(), status, mkcol.getResponseHeaders()); Log_OC.d(TAG, "Create directory " + mRemotePath + ": " + result.getLogMessage()); @@ -115,11 +113,10 @@ public class CreateRemoteFolderOperation extends RemoteOperation { return result; } - private RemoteOperationResult createParentFolder(String parentPath, OwnCloudClient client, - String userAgent) { + private RemoteOperationResult createParentFolder(String parentPath, OwnCloudClient client) { RemoteOperation operation = new CreateRemoteFolderOperation(parentPath, mCreateFullPath); - return operation.execute(client, userAgent); + return operation.execute(client); } diff --git a/src/com/owncloud/android/lib/resources/files/DownloadRemoteFileOperation.java b/src/com/owncloud/android/lib/resources/files/DownloadRemoteFileOperation.java index 8675f065..bcb209db 100644 --- a/src/com/owncloud/android/lib/resources/files/DownloadRemoteFileOperation.java +++ b/src/com/owncloud/android/lib/resources/files/DownloadRemoteFileOperation.java @@ -102,7 +102,6 @@ public class DownloadRemoteFileOperation extends RemoteOperation { int status = -1; boolean savedFile = false; mGet = new GetMethod(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath)); - mGet.addRequestHeader(USER_AGENT_HEADER, getUserAgent()); Iterator it = null; FileOutputStream fos = null; diff --git a/src/com/owncloud/android/lib/resources/files/ExistenceCheckRemoteOperation.java b/src/com/owncloud/android/lib/resources/files/ExistenceCheckRemoteOperation.java index 1d8b2e5a..154c8b14 100644 --- a/src/com/owncloud/android/lib/resources/files/ExistenceCheckRemoteOperation.java +++ b/src/com/owncloud/android/lib/resources/files/ExistenceCheckRemoteOperation.java @@ -77,7 +77,6 @@ public class ExistenceCheckRemoteOperation extends RemoteOperation { HeadMethod head = null; try { head = new HeadMethod(client.getWebdavUri() + WebdavUtils.encodePath(mPath)); - head.addRequestHeader(USER_AGENT_HEADER, getUserAgent()); int status = client.executeMethod(head, TIMEOUT, TIMEOUT); client.exhaustResponse(head.getResponseBodyAsStream()); boolean success = (status == HttpStatus.SC_OK && !mSuccessIfAbsent) || diff --git a/src/com/owncloud/android/lib/resources/files/MoveRemoteFileOperation.java b/src/com/owncloud/android/lib/resources/files/MoveRemoteFileOperation.java index 419851f3..1eb88765 100644 --- a/src/com/owncloud/android/lib/resources/files/MoveRemoteFileOperation.java +++ b/src/com/owncloud/android/lib/resources/files/MoveRemoteFileOperation.java @@ -113,7 +113,6 @@ public class MoveRemoteFileOperation extends RemoteOperation { client.getWebdavUri() + WebdavUtils.encodePath(mTargetRemotePath), mOverwrite ); - move.addRequestHeader(USER_AGENT_HEADER, getUserAgent()); int status = client.executeMethod(move, MOVE_READ_TIMEOUT, MOVE_CONNECTION_TIMEOUT); /// process response diff --git a/src/com/owncloud/android/lib/resources/files/ReadRemoteFileOperation.java b/src/com/owncloud/android/lib/resources/files/ReadRemoteFileOperation.java index 7700621a..12c2017f 100644 --- a/src/com/owncloud/android/lib/resources/files/ReadRemoteFileOperation.java +++ b/src/com/owncloud/android/lib/resources/files/ReadRemoteFileOperation.java @@ -79,7 +79,6 @@ public class ReadRemoteFileOperation extends RemoteOperation { propfind = new PropFindMethod(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath), WebdavUtils.getFilePropSet(), // PropFind Properties DavConstants.DEPTH_0); - propfind.addRequestHeader(USER_AGENT_HEADER, getUserAgent()); int status; status = client.executeMethod(propfind, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT); diff --git a/src/com/owncloud/android/lib/resources/files/ReadRemoteFolderOperation.java b/src/com/owncloud/android/lib/resources/files/ReadRemoteFolderOperation.java index 1c49c688..1e93aa22 100644 --- a/src/com/owncloud/android/lib/resources/files/ReadRemoteFolderOperation.java +++ b/src/com/owncloud/android/lib/resources/files/ReadRemoteFolderOperation.java @@ -76,7 +76,6 @@ public class ReadRemoteFolderOperation extends RemoteOperation { query = new PropFindMethod(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath), WebdavUtils.getAllPropSet(), // PropFind Properties DavConstants.DEPTH_1); - query.addRequestHeader(USER_AGENT_HEADER, getUserAgent()); int status = client.executeMethod(query); // check and process response diff --git a/src/com/owncloud/android/lib/resources/files/RemoveRemoteFileOperation.java b/src/com/owncloud/android/lib/resources/files/RemoveRemoteFileOperation.java index 470b4533..5ae333fc 100644 --- a/src/com/owncloud/android/lib/resources/files/RemoveRemoteFileOperation.java +++ b/src/com/owncloud/android/lib/resources/files/RemoveRemoteFileOperation.java @@ -68,7 +68,6 @@ public class RemoveRemoteFileOperation extends RemoteOperation { try { delete = new DeleteMethod(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath)); - delete.addRequestHeader(USER_AGENT_HEADER, getUserAgent()); 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/resources/files/RenameRemoteFileOperation.java b/src/com/owncloud/android/lib/resources/files/RenameRemoteFileOperation.java index 592084da..4dc4c2c2 100644 --- a/src/com/owncloud/android/lib/resources/files/RenameRemoteFileOperation.java +++ b/src/com/owncloud/android/lib/resources/files/RenameRemoteFileOperation.java @@ -107,7 +107,6 @@ public class RenameRemoteFileOperation extends RemoteOperation { move = new LocalMoveMethod( client.getWebdavUri() + WebdavUtils.encodePath(mOldRemotePath), client.getWebdavUri() + WebdavUtils.encodePath(mNewRemotePath)); - move.addRequestHeader(USER_AGENT_HEADER, getUserAgent()); 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/resources/files/UploadRemoteFileOperation.java b/src/com/owncloud/android/lib/resources/files/UploadRemoteFileOperation.java index b8119430..9514431a 100644 --- a/src/com/owncloud/android/lib/resources/files/UploadRemoteFileOperation.java +++ b/src/com/owncloud/android/lib/resources/files/UploadRemoteFileOperation.java @@ -82,7 +82,6 @@ public class UploadRemoteFileOperation extends RemoteOperation { } else { mPutMethod = new PutMethod(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath)); - mPutMethod.addRequestHeader(USER_AGENT_HEADER, getUserAgent()); } } diff --git a/src/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.java b/src/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.java index c377ecbc..e6637e3a 100644 --- a/src/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.java +++ b/src/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.java @@ -106,7 +106,6 @@ public class CreateRemoteShareOperation extends RemoteOperation { post.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded; charset=utf-8"); // necessary for special characters - post.addRequestHeader(USER_AGENT_HEADER, getUserAgent()); post.addParameter(PARAM_PATH, mRemoteFilePath); post.addParameter(PARAM_SHARE_TYPE, Integer.toString(mShareType.getValue())); diff --git a/src/com/owncloud/android/lib/resources/shares/GetRemoteSharesForFileOperation.java b/src/com/owncloud/android/lib/resources/shares/GetRemoteSharesForFileOperation.java index 599201e5..3c53e7f7 100644 --- a/src/com/owncloud/android/lib/resources/shares/GetRemoteSharesForFileOperation.java +++ b/src/com/owncloud/android/lib/resources/shares/GetRemoteSharesForFileOperation.java @@ -86,7 +86,6 @@ public class GetRemoteSharesForFileOperation extends RemoteOperation { try { // Get Method get = new GetMethod(client.getBaseUri() + ShareUtils.SHARING_API_PATH); - get.addRequestHeader(USER_AGENT_HEADER, getUserAgent()); // Add Parameters to Get Method get.setQueryString(new NameValuePair[] { diff --git a/src/com/owncloud/android/lib/resources/shares/GetRemoteSharesOperation.java b/src/com/owncloud/android/lib/resources/shares/GetRemoteSharesOperation.java index d414913d..38b75194 100644 --- a/src/com/owncloud/android/lib/resources/shares/GetRemoteSharesOperation.java +++ b/src/com/owncloud/android/lib/resources/shares/GetRemoteSharesOperation.java @@ -67,7 +67,6 @@ public class GetRemoteSharesOperation extends RemoteOperation { try{ get = new GetMethod(client.getBaseUri() + ShareUtils.SHARING_API_PATH); get.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); - get.addRequestHeader(USER_AGENT_HEADER, getUserAgent()); status = client.executeMethod(get); if(isSuccess(status)) { String response = get.getResponseBodyAsString(); diff --git a/src/com/owncloud/android/lib/resources/shares/RemoveRemoteShareOperation.java b/src/com/owncloud/android/lib/resources/shares/RemoveRemoteShareOperation.java index e3ddd89c..dea4fe93 100644 --- a/src/com/owncloud/android/lib/resources/shares/RemoveRemoteShareOperation.java +++ b/src/com/owncloud/android/lib/resources/shares/RemoveRemoteShareOperation.java @@ -72,7 +72,6 @@ public class RemoveRemoteShareOperation extends RemoteOperation { delete = new DeleteMethod(client.getBaseUri() + ShareUtils.SHARING_API_PATH + id); delete.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); - delete.addRequestHeader(USER_AGENT_HEADER, getUserAgent()); status = client.executeMethod(delete); diff --git a/src/com/owncloud/android/lib/resources/status/GetRemoteStatusOperation.java b/src/com/owncloud/android/lib/resources/status/GetRemoteStatusOperation.java index 7091163b..aecc12f3 100644 --- a/src/com/owncloud/android/lib/resources/status/GetRemoteStatusOperation.java +++ b/src/com/owncloud/android/lib/resources/status/GetRemoteStatusOperation.java @@ -38,6 +38,7 @@ import android.net.ConnectivityManager; import android.net.Uri; import com.owncloud.android.lib.common.OwnCloudClient; +import com.owncloud.android.lib.common.OwnCloudClientManagerFactory; import com.owncloud.android.lib.common.accounts.AccountUtils; import com.owncloud.android.lib.common.operations.RemoteOperation; import com.owncloud.android.lib.common.operations.RemoteOperationResult; @@ -77,10 +78,10 @@ public class GetRemoteStatusOperation extends RemoteOperation { String baseUrlSt = client.getBaseUri().toString(); try { get = new GetMethod(baseUrlSt + AccountUtils.STATUS_PATH); - get.addRequestHeader(USER_AGENT_HEADER, getUserAgent()); HttpParams params = get.getParams().getDefaultParams(); - params.setParameter(HttpMethodParams.USER_AGENT, getUserAgent()); + params.setParameter(HttpMethodParams.USER_AGENT, + OwnCloudClientManagerFactory.getUserAgent()); get.getParams().setDefaults(params); client.setFollowRedirects(false); diff --git a/src/com/owncloud/android/lib/resources/users/GetRemoteUserNameOperation.java b/src/com/owncloud/android/lib/resources/users/GetRemoteUserNameOperation.java index e7bcb78c..cecd639f 100644 --- a/src/com/owncloud/android/lib/resources/users/GetRemoteUserNameOperation.java +++ b/src/com/owncloud/android/lib/resources/users/GetRemoteUserNameOperation.java @@ -79,7 +79,6 @@ public class GetRemoteUserNameOperation extends RemoteOperation { try { get = new GetMethod(client.getBaseUri() + OCS_ROUTE); get.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); - get.addRequestHeader(USER_AGENT_HEADER, getUserAgent()); status = client.executeMethod(get); if(isSuccess(status)) { String response = get.getResponseBodyAsString(); diff --git a/test_client/src/com/owncloud/android/lib/test_project/TestActivity.java b/test_client/src/com/owncloud/android/lib/test_project/TestActivity.java index a74d24ca..c399bb49 100644 --- a/test_client/src/com/owncloud/android/lib/test_project/TestActivity.java +++ b/test_client/src/com/owncloud/android/lib/test_project/TestActivity.java @@ -72,7 +72,6 @@ public class TestActivity extends Activity { private String mServerUri; private String mUser; private String mPass; - private static String mUserAgent; private static final int BUFFER_SIZE = 1024; @@ -91,7 +90,6 @@ public class TestActivity extends Activity { mServerUri = getString(R.string.server_base_url); mUser = getString(R.string.username); mPass = getString(R.string.password); - mUserAgent = getString(R.string.user_agent); Protocol pr = Protocol.getProtocol("https"); if (pr == null || !(pr.getSocketFactory() instanceof SelfSignedConfidentSslSocketFactory)) { @@ -106,8 +104,7 @@ public class TestActivity extends Activity { } } - mClient = new OwnCloudClient(Uri.parse(mServerUri), NetworkUtils.getMultiThreadedConnManager(), - mUserAgent); + mClient = new OwnCloudClient(Uri.parse(mServerUri), NetworkUtils.getMultiThreadedConnManager()); mClient.setDefaultTimeouts( OwnCloudClientFactory.DEFAULT_DATA_TIMEOUT, OwnCloudClientFactory.DEFAULT_CONNECTION_TIMEOUT); @@ -159,7 +156,7 @@ public class TestActivity extends Activity { CreateRemoteFolderOperation createOperation = new CreateRemoteFolderOperation(remotePath, createFullPath); - RemoteOperationResult result = createOperation.execute(client, mUserAgent); + RemoteOperationResult result = createOperation.execute(client); return result; } @@ -177,7 +174,7 @@ public class TestActivity extends Activity { public RemoteOperationResult renameFile(String oldName, String oldRemotePath, String newName, boolean isFolder) { RenameRemoteFileOperation renameOperation = new RenameRemoteFileOperation(oldName, oldRemotePath, newName, isFolder); - RemoteOperationResult result = renameOperation.execute(mClient, mUserAgent); + RemoteOperationResult result = renameOperation.execute(mClient); return result; } @@ -190,7 +187,7 @@ public class TestActivity extends Activity { */ public RemoteOperationResult removeFile(String remotePath) { RemoveRemoteFileOperation removeOperation = new RemoveRemoteFileOperation(remotePath); - RemoteOperationResult result = removeOperation.execute(mClient, mUserAgent); + RemoteOperationResult result = removeOperation.execute(mClient); return result; } @@ -202,7 +199,7 @@ public class TestActivity extends Activity { */ public static RemoteOperationResult removeFile(String remotePath, OwnCloudClient client) { RemoveRemoteFileOperation removeOperation = new RemoveRemoteFileOperation(remotePath); - RemoteOperationResult result = removeOperation.execute(client, mUserAgent); + RemoteOperationResult result = removeOperation.execute(client); return result; } @@ -216,7 +213,7 @@ public class TestActivity extends Activity { public RemoteOperationResult readFile(String remotePath) { ReadRemoteFolderOperation readOperation= new ReadRemoteFolderOperation(remotePath); - RemoteOperationResult result = readOperation.execute(mClient, mUserAgent); + RemoteOperationResult result = readOperation.execute(mClient); return result; } @@ -235,7 +232,7 @@ public class TestActivity extends Activity { folder.mkdirs(); DownloadRemoteFileOperation downloadOperation = new DownloadRemoteFileOperation(remoteFile.getRemotePath(), folder.getAbsolutePath()); - RemoteOperationResult result = downloadOperation.execute(mClient, mUserAgent); + RemoteOperationResult result = downloadOperation.execute(mClient); return result; } @@ -276,7 +273,7 @@ public class TestActivity extends Activity { ); } - RemoteOperationResult result = uploadOperation.execute(client, mUserAgent); + RemoteOperationResult result = uploadOperation.execute(client); return result; } @@ -287,7 +284,7 @@ public class TestActivity extends Activity { public RemoteOperationResult getShares(){ GetRemoteSharesOperation getOperation = new GetRemoteSharesOperation(); - RemoteOperationResult result = getOperation.execute(mClient, mUserAgent); + RemoteOperationResult result = getOperation.execute(mClient); return result; } @@ -315,7 +312,7 @@ public class TestActivity extends Activity { String password, int permissions){ CreateRemoteShareOperation createOperation = new CreateRemoteShareOperation(path, shareType, shareWith, publicUpload, password, permissions); - RemoteOperationResult result = createOperation.execute(mClient, mUserAgent); + RemoteOperationResult result = createOperation.execute(mClient); return result; } @@ -329,7 +326,7 @@ public class TestActivity extends Activity { public RemoteOperationResult removeShare(int idShare) { RemoveRemoteShareOperation removeOperation = new RemoveRemoteShareOperation(idShare); - RemoteOperationResult result = removeOperation.execute(mClient, mUserAgent); + RemoteOperationResult result = removeOperation.execute(mClient); return result; diff --git a/test_client/tests/src/com/owncloud/android/lib/test_project/test/MoveFileTest.java b/test_client/tests/src/com/owncloud/android/lib/test_project/test/MoveFileTest.java index 14a3bed5..e83f0478 100644 --- a/test_client/tests/src/com/owncloud/android/lib/test_project/test/MoveFileTest.java +++ b/test_client/tests/src/com/owncloud/android/lib/test_project/test/MoveFileTest.java @@ -279,8 +279,7 @@ public class MoveFileTest extends RemoteTest { mBaseFolderPath + TARGET_PATH_TO_FILE_1, false ); - RemoteOperationResult result = moveOperation.execute(mClient, - getContext().getString(R.string.user_agent)); + RemoteOperationResult result = moveOperation.execute(mClient); assertTrue(result.isSuccess()); // move & rename file, different location @@ -289,8 +288,7 @@ public class MoveFileTest extends RemoteTest { mBaseFolderPath + TARGET_PATH_TO_FILE_2_RENAMED, false ); - result = moveOperation.execute(mClient, - getContext().getString(R.string.user_agent)); + result = moveOperation.execute(mClient); assertTrue(result.isSuccess()); // move & rename file, same location (rename file) @@ -299,8 +297,7 @@ public class MoveFileTest extends RemoteTest { mBaseFolderPath + SRC_PATH_TO_FILE_3_RENAMED, false ); - result = moveOperation.execute(mClient, - getContext().getString(R.string.user_agent)); + result = moveOperation.execute(mClient); assertTrue(result.isSuccess()); // move empty folder @@ -309,8 +306,7 @@ public class MoveFileTest extends RemoteTest { mBaseFolderPath + TARGET_PATH_TO_EMPTY_FOLDER, false ); - result = moveOperation.execute(mClient, - getContext().getString(R.string.user_agent)); + result = moveOperation.execute(mClient); assertTrue(result.isSuccess()); // move non-empty folder @@ -319,8 +315,7 @@ public class MoveFileTest extends RemoteTest { mBaseFolderPath + TARGET_PATH_TO_FULL_FOLDER_1, false ); - result = moveOperation.execute(mClient, - getContext().getString(R.string.user_agent)); + result = moveOperation.execute(mClient); assertTrue(result.isSuccess()); // move & rename folder, different location @@ -329,8 +324,7 @@ public class MoveFileTest extends RemoteTest { mBaseFolderPath + TARGET_PATH_TO_FULL_FOLDER_2_RENAMED, false ); - result = moveOperation.execute(mClient, - getContext().getString(R.string.user_agent)); + result = moveOperation.execute(mClient); assertTrue(result.isSuccess()); // move & rename folder, same location (rename folder) @@ -339,8 +333,7 @@ public class MoveFileTest extends RemoteTest { mBaseFolderPath + SRC_PATH_TO_FULL_FOLDER_3_RENAMED, false ); - result = moveOperation.execute(mClient, - getContext().getString(R.string.user_agent)); + result = moveOperation.execute(mClient); assertTrue(result.isSuccess()); // move for nothing (success, but no interaction with network) @@ -349,8 +342,7 @@ public class MoveFileTest extends RemoteTest { mBaseFolderPath + SRC_PATH_TO_FILE_4, false ); - result = moveOperation.execute(mClient, - getContext().getString(R.string.user_agent)); + result = moveOperation.execute(mClient); assertTrue(result.isSuccess()); // move overwriting @@ -359,8 +351,7 @@ public class MoveFileTest extends RemoteTest { mBaseFolderPath + TARGET_PATH_TO_ALREADY_EXISTENT_EMPTY_FOLDER_4, true ); - result = moveOperation.execute(mClient, - getContext().getString(R.string.user_agent)); + result = moveOperation.execute(mClient); assertTrue(result.isSuccess()); @@ -372,8 +363,7 @@ public class MoveFileTest extends RemoteTest { mBaseFolderPath + TARGET_PATH_TO_NON_EXISTENT_FILE, false ); - result = moveOperation.execute(mClient, - getContext().getString(R.string.user_agent)); + result = moveOperation.execute(mClient); assertTrue(result.getCode() == ResultCode.FILE_NOT_FOUND); // folder to move into does no exist @@ -382,8 +372,7 @@ public class MoveFileTest extends RemoteTest { mBaseFolderPath + TARGET_PATH_TO_FILE_5_INTO_NON_EXISTENT_FOLDER, false ); - result = moveOperation.execute(mClient, - getContext().getString(R.string.user_agent)); + result = moveOperation.execute(mClient); assertTrue(result.getHttpCode() == HttpStatus.SC_CONFLICT); // target location (renaming) has invalid characters @@ -392,8 +381,7 @@ public class MoveFileTest extends RemoteTest { mBaseFolderPath + TARGET_PATH_RENAMED_WITH_INVALID_CHARS, false ); - result = moveOperation.execute(mClient, - getContext().getString(R.string.user_agent)); + result = moveOperation.execute(mClient); assertTrue(result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME); // name collision @@ -402,8 +390,7 @@ public class MoveFileTest extends RemoteTest { mBaseFolderPath + TARGET_PATH_TO_ALREADY_EXISTENT_FILE_7, false ); - result = moveOperation.execute(mClient, - getContext().getString(R.string.user_agent)); + result = moveOperation.execute(mClient); assertTrue(result.getCode() == ResultCode.INVALID_OVERWRITE); // move a folder into a descendant @@ -412,8 +399,7 @@ public class MoveFileTest extends RemoteTest { mBaseFolderPath + SRC_PATH_TO_EMPTY_FOLDER, false ); - result = moveOperation.execute(mClient, - getContext().getString(R.string.user_agent)); + result = moveOperation.execute(mClient); assertTrue(result.getCode() == ResultCode.INVALID_MOVE_INTO_DESCENDANT); } @@ -450,8 +436,7 @@ public class MoveFileTest extends RemoteTest { mClient = new OwnCloudClient( Uri.parse(mServerUri), - NetworkUtils.getMultiThreadedConnManager(), - getContext().getString(R.string.user_agent) + NetworkUtils.getMultiThreadedConnManager() ); mClient.setDefaultTimeouts( OwnCloudClientFactory.DEFAULT_DATA_TIMEOUT, diff --git a/test_client/tests/src/com/owncloud/android/lib/test_project/test/OwnCloudClientTest.java b/test_client/tests/src/com/owncloud/android/lib/test_project/test/OwnCloudClientTest.java index 232b7716..21c47788 100644 --- a/test_client/tests/src/com/owncloud/android/lib/test_project/test/OwnCloudClientTest.java +++ b/test_client/tests/src/com/owncloud/android/lib/test_project/test/OwnCloudClientTest.java @@ -97,8 +97,7 @@ public class OwnCloudClientTest extends AndroidTestCase { public void testConstructor() { try { - new OwnCloudClient(null, NetworkUtils.getMultiThreadedConnManager(), - mUserAgent); + new OwnCloudClient(null, NetworkUtils.getMultiThreadedConnManager()); throw new AssertionFailedError("Accepted NULL parameter"); } catch(Exception e) { @@ -107,7 +106,7 @@ public class OwnCloudClientTest extends AndroidTestCase { } try { - new OwnCloudClient(mServerUri, null, mUserAgent); + new OwnCloudClient(mServerUri, null); throw new AssertionFailedError("Accepted NULL parameter"); } catch(Exception e) { @@ -116,8 +115,7 @@ public class OwnCloudClientTest extends AndroidTestCase { } OwnCloudClient client = - new OwnCloudClient(mServerUri, NetworkUtils.getMultiThreadedConnManager(), - mUserAgent); + new OwnCloudClient(mServerUri, NetworkUtils.getMultiThreadedConnManager()); assertNotNull("OwnCloudClient instance not built", client); assertEquals("Wrong user agent", client.getParams().getParameter(HttpMethodParams.USER_AGENT), @@ -127,8 +125,7 @@ public class OwnCloudClientTest extends AndroidTestCase { public void testGetSetCredentials() { OwnCloudClient client = - new OwnCloudClient(mServerUri, NetworkUtils.getMultiThreadedConnManager(), - mUserAgent); + new OwnCloudClient(mServerUri, NetworkUtils.getMultiThreadedConnManager()); assertNotNull("Returned NULL credentials", client.getCredentials()); assertEquals("Not instanced without credentials", @@ -151,8 +148,7 @@ public class OwnCloudClientTest extends AndroidTestCase { public void testExecuteMethodWithTimeouts() throws HttpException, IOException { OwnCloudClient client = - new OwnCloudClient(mServerUri, NetworkUtils.getMultiThreadedConnManager(), - mUserAgent); + new OwnCloudClient(mServerUri, NetworkUtils.getMultiThreadedConnManager()); int connectionTimeout = client.getConnectionTimeout(); int readTimeout = client.getDataTimeout(); @@ -200,8 +196,7 @@ public class OwnCloudClientTest extends AndroidTestCase { public void testExecuteMethod() { OwnCloudClient client = - new OwnCloudClient(mServerUri, NetworkUtils.getMultiThreadedConnManager(), - mUserAgent); + new OwnCloudClient(mServerUri, NetworkUtils.getMultiThreadedConnManager()); HeadMethod head = new HeadMethod(client.getWebdavUri() + "/"); int status = -1; try { @@ -222,8 +217,7 @@ public class OwnCloudClientTest extends AndroidTestCase { public void testExhaustResponse() { OwnCloudClient client = - new OwnCloudClient(mServerUri, NetworkUtils.getMultiThreadedConnManager(), - mUserAgent); + new OwnCloudClient(mServerUri, NetworkUtils.getMultiThreadedConnManager()); PropFindMethod propfind = null; try { @@ -265,8 +259,7 @@ public class OwnCloudClientTest extends AndroidTestCase { public void testGetSetDefaultTimeouts() { OwnCloudClient client = - new OwnCloudClient(mServerUri, NetworkUtils.getMultiThreadedConnManager(), - mUserAgent); + new OwnCloudClient(mServerUri, NetworkUtils.getMultiThreadedConnManager()); int oldDataTimeout = client.getDataTimeout(); int oldConnectionTimeout = client.getConnectionTimeout(); @@ -306,8 +299,7 @@ public class OwnCloudClientTest extends AndroidTestCase { public void testGetWebdavUri() { OwnCloudClient client = - new OwnCloudClient(mServerUri, NetworkUtils.getMultiThreadedConnManager(), - mUserAgent); + new OwnCloudClient(mServerUri, NetworkUtils.getMultiThreadedConnManager()); client.setCredentials(OwnCloudCredentialsFactory.newBearerCredentials("fakeToken")); Uri webdavUri = client.getWebdavUri(); assertTrue("WebDAV URI does not point to the right entry point for OAuth2 " + @@ -345,8 +337,7 @@ public class OwnCloudClientTest extends AndroidTestCase { public void testGetSetBaseUri() { OwnCloudClient client = - new OwnCloudClient(mServerUri, NetworkUtils.getMultiThreadedConnManager(), - mUserAgent); + new OwnCloudClient(mServerUri, NetworkUtils.getMultiThreadedConnManager()); assertEquals("Returned base URI different that URI passed to constructor", mServerUri, client.getBaseUri()); diff --git a/test_client/tests/src/com/owncloud/android/lib/test_project/test/SimpleFactoryManagerTest.java b/test_client/tests/src/com/owncloud/android/lib/test_project/test/SimpleFactoryManagerTest.java index bd109792..6122e9f3 100644 --- a/test_client/tests/src/com/owncloud/android/lib/test_project/test/SimpleFactoryManagerTest.java +++ b/test_client/tests/src/com/owncloud/android/lib/test_project/test/SimpleFactoryManagerTest.java @@ -95,16 +95,13 @@ public class SimpleFactoryManagerTest extends AndroidTestCase { public void testGetClientFor() { try { - OwnCloudClient client = mSFMgr.getClientFor(mValidAccount, getContext(), - getContext().getString(R.string.user_agent)); + OwnCloudClient client = mSFMgr.getClientFor(mValidAccount, getContext()); assertNotSame("Got same client instances for same account", - client, mSFMgr.getClientFor(mValidAccount, getContext(), - getContext().getString(R.string.user_agent))); + client, mSFMgr.getClientFor(mValidAccount, getContext())); assertNotSame("Got same client instances for different accounts", - client, mSFMgr.getClientFor(mAnonymousAccount, getContext(), - getContext().getString(R.string.user_agent))); + client, mSFMgr.getClientFor(mAnonymousAccount, getContext())); } catch (Exception e) { throw new AssertionFailedError("Exception getting client for account: " + e.getMessage()); @@ -114,12 +111,10 @@ public class SimpleFactoryManagerTest extends AndroidTestCase { public void testRemoveClientFor() { try { - OwnCloudClient client = mSFMgr.getClientFor(mValidAccount, getContext(), - getContext().getString(R.string.user_agent)); + OwnCloudClient client = mSFMgr.getClientFor(mValidAccount, getContext()); mSFMgr.removeClientFor(mValidAccount); assertNotSame("Got same client instance after removing it from manager", - client, mSFMgr.getClientFor(mValidAccount, getContext(), - getContext().getString(R.string.user_agent))); + client, mSFMgr.getClientFor(mValidAccount, getContext())); } catch (Exception e) { throw new AssertionFailedError("Exception getting client for account: " + e.getMessage()); diff --git a/test_client/tests/src/com/owncloud/android/lib/test_project/test/SingleSessionManagerTest.java b/test_client/tests/src/com/owncloud/android/lib/test_project/test/SingleSessionManagerTest.java index b81cd05f..f565cc0e 100644 --- a/test_client/tests/src/com/owncloud/android/lib/test_project/test/SingleSessionManagerTest.java +++ b/test_client/tests/src/com/owncloud/android/lib/test_project/test/SingleSessionManagerTest.java @@ -94,16 +94,13 @@ public class SingleSessionManagerTest extends AndroidTestCase { public void testGetClientFor() { try { - OwnCloudClient client1 = mSSMgr.getClientFor(mValidAccount, getContext(), - getContext().getString(R.string.user_agent)); - OwnCloudClient client2 = mSSMgr.getClientFor(mAnonymousAccount, getContext(), - getContext().getString(R.string.user_agent)); + OwnCloudClient client1 = mSSMgr.getClientFor(mValidAccount, getContext()); + OwnCloudClient client2 = mSSMgr.getClientFor(mAnonymousAccount, getContext()); assertNotSame("Got same client instances for different accounts", client1, client2); assertSame("Got different client instances for same account", - client1, mSSMgr.getClientFor(mValidAccount, getContext(), - getContext().getString(R.string.user_agent))); + client1, mSSMgr.getClientFor(mValidAccount, getContext())); } catch (Exception e) { throw new AssertionFailedError("Exception getting client for account: " + e.getMessage()); @@ -114,12 +111,10 @@ public class SingleSessionManagerTest extends AndroidTestCase { public void testRemoveClientFor() { try { - OwnCloudClient client1 = mSSMgr.getClientFor(mValidAccount, getContext(), - getContext().getString(R.string.user_agent)); + OwnCloudClient client1 = mSSMgr.getClientFor(mValidAccount, getContext()); mSSMgr.removeClientFor(mValidAccount); assertNotSame("Got same client instance after removing it from manager", - client1, mSSMgr.getClientFor(mValidAccount, getContext(), - getContext().getString(R.string.user_agent))); + client1, mSSMgr.getClientFor(mValidAccount, getContext())); } catch (Exception e) { throw new AssertionFailedError("Exception getting client for account: " + e.getMessage()); } From decf89e39b44e0c0ce4280e69435adec4403ced8 Mon Sep 17 00:00:00 2001 From: masensio Date: Tue, 24 Mar 2015 14:43:36 +0100 Subject: [PATCH 07/10] Update uger agent string --- sample_client/res/values/setup.xml | 2 +- test_client/res/values/setup.xml | 2 +- test_client/tests/res/values/strings.xml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/sample_client/res/values/setup.xml b/sample_client/res/values/setup.xml index 268eec7b..2784e15a 100644 --- a/sample_client/res/values/setup.xml +++ b/sample_client/res/values/setup.xml @@ -27,5 +27,5 @@ - Android-ownCloud + Mozilla/5.0 Android-ownCloud sample diff --git a/test_client/res/values/setup.xml b/test_client/res/values/setup.xml index 76c64ccf..3af3d23c 100644 --- a/test_client/res/values/setup.xml +++ b/test_client/res/values/setup.xml @@ -28,5 +28,5 @@ - Android-ownCloud + Android-ownCloud test project diff --git a/test_client/tests/res/values/strings.xml b/test_client/tests/res/values/strings.xml index bc3a4bb1..3f513463 100644 --- a/test_client/tests/res/values/strings.xml +++ b/test_client/tests/res/values/strings.xml @@ -26,6 +26,6 @@ Oc_framework-testTest - Android-ownCloud + Mozilla/5.0 Android-ownCloud test cases From e0da66d25d59ce6967f3b4e1da21fe28aab4c790 Mon Sep 17 00:00:00 2001 From: masensio Date: Tue, 24 Mar 2015 14:52:18 +0100 Subject: [PATCH 08/10] Update uger agent string --- sample_client/res/values/setup.xml | 2 +- test_client/res/values/setup.xml | 2 +- test_client/tests/res/values/strings.xml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/sample_client/res/values/setup.xml b/sample_client/res/values/setup.xml index 2784e15a..3e5a6939 100644 --- a/sample_client/res/values/setup.xml +++ b/sample_client/res/values/setup.xml @@ -27,5 +27,5 @@ - Mozilla/5.0 Android-ownCloud sample + Mozilla/5.0 (Android) ownCloud sample diff --git a/test_client/res/values/setup.xml b/test_client/res/values/setup.xml index 3af3d23c..f72929ab 100644 --- a/test_client/res/values/setup.xml +++ b/test_client/res/values/setup.xml @@ -28,5 +28,5 @@ - Android-ownCloud test project + Mozilla/5.0 (Android) ownCloud test project diff --git a/test_client/tests/res/values/strings.xml b/test_client/tests/res/values/strings.xml index 3f513463..12c3d635 100644 --- a/test_client/tests/res/values/strings.xml +++ b/test_client/tests/res/values/strings.xml @@ -26,6 +26,6 @@ Oc_framework-testTest - Mozilla/5.0 Android-ownCloud test cases + Mozilla/5.0 (Android) ownCloud test cases From 0e011fb0c7dc3b6089943dbb901340f9128909ac Mon Sep 17 00:00:00 2001 From: masensio Date: Tue, 24 Mar 2015 14:53:28 +0100 Subject: [PATCH 09/10] Remove static string in RemoteOperation --- .../android/lib/common/operations/RemoteOperation.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/com/owncloud/android/lib/common/operations/RemoteOperation.java b/src/com/owncloud/android/lib/common/operations/RemoteOperation.java index bae2a3d4..c5aaeb14 100644 --- a/src/com/owncloud/android/lib/common/operations/RemoteOperation.java +++ b/src/com/owncloud/android/lib/common/operations/RemoteOperation.java @@ -60,9 +60,6 @@ public abstract class RemoteOperation implements Runnable { /** OCS API header value */ public static final String OCS_API_HEADER_VALUE = "true"; - /** User agent header */ - public static final String USER_AGENT_HEADER = "User-Agent"; - /** ownCloud account in the remote ownCloud server to operate */ private Account mAccount = null; From ab268fbccde448c8b90447ef9df8e2069053607f Mon Sep 17 00:00:00 2001 From: masensio Date: Wed, 25 Mar 2015 09:36:40 +0100 Subject: [PATCH 10/10] Changes in test because Travis is failing --- test_client/tests/res/values/strings.xml | 1 - .../android/lib/test_project/test/OwnCloudClientTest.java | 5 ----- 2 files changed, 6 deletions(-) diff --git a/test_client/tests/res/values/strings.xml b/test_client/tests/res/values/strings.xml index 12c3d635..66f25f1e 100644 --- a/test_client/tests/res/values/strings.xml +++ b/test_client/tests/res/values/strings.xml @@ -26,6 +26,5 @@ Oc_framework-testTest - Mozilla/5.0 (Android) ownCloud test cases diff --git a/test_client/tests/src/com/owncloud/android/lib/test_project/test/OwnCloudClientTest.java b/test_client/tests/src/com/owncloud/android/lib/test_project/test/OwnCloudClientTest.java index 21c47788..5328e9cf 100644 --- a/test_client/tests/src/com/owncloud/android/lib/test_project/test/OwnCloudClientTest.java +++ b/test_client/tests/src/com/owncloud/android/lib/test_project/test/OwnCloudClientTest.java @@ -64,7 +64,6 @@ public class OwnCloudClientTest extends AndroidTestCase { private Uri mServerUri; private String mUsername; private String mPassword; - private String mUserAgent; public OwnCloudClientTest() { super(); @@ -91,7 +90,6 @@ public class OwnCloudClientTest extends AndroidTestCase { mServerUri = Uri.parse(getContext().getString(R.string.server_base_url)); mUsername = getContext().getString(R.string.username); mPassword = getContext().getString(R.string.password); - mUserAgent = getContext().getString(R.string.user_agent); } @@ -117,9 +115,6 @@ public class OwnCloudClientTest extends AndroidTestCase { OwnCloudClient client = new OwnCloudClient(mServerUri, NetworkUtils.getMultiThreadedConnManager()); assertNotNull("OwnCloudClient instance not built", client); - assertEquals("Wrong user agent", - client.getParams().getParameter(HttpMethodParams.USER_AGENT), - mUserAgent); }