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

Add user agent in all the request

This commit is contained in:
masensio 2015-03-13 13:53:36 +01:00
parent a1420ab00e
commit fc07af2364
16 changed files with 72 additions and 25 deletions

View File

@ -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);

View File

@ -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);
}

View File

@ -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<OnDatatransferProgressListener> 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());
}
}
}

View File

@ -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)

View File

@ -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

View File

@ -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);

View File

@ -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

View File

@ -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) {

View File

@ -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)

View File

@ -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);

View File

@ -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);

View File

@ -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[] {

View File

@ -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();

View File

@ -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);

View File

@ -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;

View File

@ -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();