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

add support for copy methode

This commit is contained in:
theScrabi 2018-06-13 14:14:15 +02:00 committed by davigonz
parent e4e8a5a5e3
commit b183e3f08a
3 changed files with 42 additions and 71 deletions

@ -1 +1 @@
Subproject commit 466380e9bb8414d919391dbb14de7926dbfd387e Subproject commit aec2f98c69e4e86d148226c7ff00fe5dd2be49e4

View File

@ -0,0 +1,30 @@
package com.owncloud.android.lib.common.http.methods.webdav;
import at.bitfire.dav4android.exception.UnauthorizedException;
import okhttp3.HttpUrl;
public class CopyMethod extends DavMethod {
final String destinationUrl;
final boolean forceOverride;
public CopyMethod(HttpUrl httpUrl, String destinationUrl, boolean forceOverride) {
super(httpUrl);
this.destinationUrl = destinationUrl;
this.forceOverride = forceOverride;
}
@Override
public int execute() throws Exception {
try {
mDavResource.copy(destinationUrl, forceOverride);
mRequest = mDavResource.getRequest();
mResponse = mDavResource.getResponse();
} catch (UnauthorizedException davException) {
// Do nothing, we will use the 401 code to handle the situation
}
return super.getStatusCode();
}
}

View File

@ -27,6 +27,7 @@ package com.owncloud.android.lib.resources.files;
import android.util.Log; import android.util.Log;
import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.http.methods.webdav.CopyMethod;
import com.owncloud.android.lib.common.network.WebdavUtils; import com.owncloud.android.lib.common.network.WebdavUtils;
import com.owncloud.android.lib.common.operations.RemoteOperation; import com.owncloud.android.lib.common.operations.RemoteOperation;
import com.owncloud.android.lib.common.operations.RemoteOperationResult; import com.owncloud.android.lib.common.operations.RemoteOperationResult;
@ -37,10 +38,11 @@ import org.apache.commons.httpclient.HttpStatus;
import org.apache.jackrabbit.webdav.DavException; import org.apache.jackrabbit.webdav.DavException;
import org.apache.jackrabbit.webdav.MultiStatusResponse; import org.apache.jackrabbit.webdav.MultiStatusResponse;
import org.apache.jackrabbit.webdav.Status; import org.apache.jackrabbit.webdav.Status;
import org.apache.jackrabbit.webdav.client.methods.CopyMethod;
import java.io.IOException; import java.io.IOException;
import okhttp3.HttpUrl;
/** /**
* Remote operation moving a remote file or folder in the ownCloud server to a different folder * Remote operation moving a remote file or folder in the ownCloud server to a different folder
@ -106,32 +108,26 @@ public class CopyRemoteFileOperation extends RemoteOperation {
} }
/// perform remote operation /// perform remote operation
CopyMethod copyMethod = null;
RemoteOperationResult result = null; RemoteOperationResult result = null;
try { try {
copyMethod = new CopyMethod( CopyMethod copyMethod = new CopyMethod(
client.getWebdavUri() + WebdavUtils.encodePath(mSrcRemotePath), HttpUrl.parse(client.getWebdavUri() + WebdavUtils.encodePath(mSrcRemotePath)),
client.getWebdavUri() + WebdavUtils.encodePath(mTargetRemotePath), client.getWebdavUri() + WebdavUtils.encodePath(mTargetRemotePath),
mOverwrite mOverwrite);
); final int status = client.executeHttpMethod(copyMethod);
int status = client.executeMethod(copyMethod, COPY_READ_TIMEOUT, COPY_CONNECTION_TIMEOUT);
/// process response if (status == HttpStatus.SC_PRECONDITION_FAILED && !mOverwrite) {
if (status == HttpStatus.SC_MULTI_STATUS) {
result = processPartialError(copyMethod);
} else if (status == HttpStatus.SC_PRECONDITION_FAILED && !mOverwrite) {
result = new RemoteOperationResult(ResultCode.INVALID_OVERWRITE); result = new RemoteOperationResult(ResultCode.INVALID_OVERWRITE);
client.exhaustResponse(copyMethod.getResponseBodyAsStream()); client.exhaustResponse(copyMethod.getResponseAsStream());
/// for other errors that could be explicitly handled, check first: /// for other errors that could be explicitly handled, check first:
/// http://www.webdav.org/specs/rfc4918.html#rfc.section.9.9.4 /// http://www.webdav.org/specs/rfc4918.html#rfc.section.9.9.4
} else { } else {
result = new RemoteOperationResult(isSuccess(status), copyMethod); result = new RemoteOperationResult(copyMethod);
client.exhaustResponse(copyMethod.getResponseBodyAsStream()); client.exhaustResponse(copyMethod.getResponseAsStream());
} }
Log.i(TAG, "Copy " + mSrcRemotePath + " to " + mTargetRemotePath + ": " + Log.i(TAG, "Copy " + mSrcRemotePath + " to " + mTargetRemotePath + ": " +
@ -142,63 +138,8 @@ public class CopyRemoteFileOperation extends RemoteOperation {
Log.e(TAG, "Copy " + mSrcRemotePath + " to " + mTargetRemotePath + ": " + Log.e(TAG, "Copy " + mSrcRemotePath + " to " + mTargetRemotePath + ": " +
result.getLogMessage(), e); result.getLogMessage(), e);
} finally {
if (copyMethod != null)
copyMethod.releaseConnection();
} }
return result; return result;
} }
/**
* Analyzes a multistatus response from the OC server to generate an appropriate result.
* <p/>
* In WebDAV, a COPY request on collections (folders) can be PARTIALLY successful: some
* children are copied, some other aren't.
* <p/>
* According to the WebDAV specification, a multistatus response SHOULD NOT include partial
* successes (201, 204) nor for descendants of already failed children (424) in the response
* entity. But SHOULD NOT != MUST NOT, so take carefully.
*
* @param copyMethod Copy operation just finished with a multistatus response
* @return A result for the {@link com.owncloud.android.lib.resources.files.CopyRemoteFileOperation} caller
* @throws java.io.IOException If the response body could not be parsed
* @throws org.apache.jackrabbit.webdav.DavException If the status code is other than MultiStatus or if obtaining
* the response XML document fails
*/
private RemoteOperationResult processPartialError(CopyMethod copyMethod)
throws IOException, DavException {
// Adding a list of failed descendants to the result could be interesting; or maybe not.
// For the moment, let's take the easy way.
/// check that some error really occurred
MultiStatusResponse[] responses = copyMethod.getResponseBodyAsMultiStatus().getResponses();
Status[] status;
boolean failFound = false;
for (int i = 0; i < responses.length && !failFound; i++) {
status = responses[i].getStatus();
failFound = (
status != null &&
status.length > 0 &&
status[0].getStatusCode() > 299
);
}
RemoteOperationResult result;
if (failFound) {
result = new RemoteOperationResult(ResultCode.PARTIAL_COPY_DONE);
} else {
result = new RemoteOperationResult(true, copyMethod);
}
return result;
}
protected boolean isSuccess(int status) {
return status == HttpStatus.SC_CREATED || status == HttpStatus.SC_NO_CONTENT;
}
} }