mirror of
https://github.com/owncloud/android-library.git
synced 2025-06-07 16:06:08 +00:00
Working on authentication operations
This commit is contained in:
parent
4f0cd82a7d
commit
dfab453e6b
@ -77,7 +77,6 @@ public class OwnCloudClient extends HttpClient {
|
||||
private static byte[] sExhaustBuffer = new byte[1024];
|
||||
|
||||
private static int sIntanceCounter = 0;
|
||||
private boolean mFollowRedirects = true;
|
||||
private OwnCloudCredentials mCredentials = null;
|
||||
private int mInstanceNumber = 0;
|
||||
|
||||
@ -292,7 +291,7 @@ public class OwnCloudClient extends HttpClient {
|
||||
|
||||
checkFirstRedirection(method);
|
||||
|
||||
if (mFollowRedirects) {
|
||||
if (mOkHttpClient.followRedirects()) {
|
||||
status = followRedirection(method).getLastStatus();
|
||||
}
|
||||
|
||||
@ -312,7 +311,7 @@ public class OwnCloudClient extends HttpClient {
|
||||
|
||||
public int executeHttpMethod (HttpBaseMethod method) throws Exception {
|
||||
|
||||
boolean repeatWithFreshCredentials = false;
|
||||
boolean repeatWithFreshCredentials;
|
||||
int repeatCounter = 0;
|
||||
int status;
|
||||
|
||||
@ -457,7 +456,9 @@ public class OwnCloudClient extends HttpClient {
|
||||
}
|
||||
|
||||
public Uri getNewWebDavUri() {
|
||||
return Uri.parse(mBaseUri + NEW_WEBDAV_PATH_4_0 + mCredentials.getUsername());
|
||||
return !(mCredentials instanceof OwnCloudAnonymousCredentials)
|
||||
? Uri.parse(mBaseUri + NEW_WEBDAV_PATH_4_0)
|
||||
: Uri.parse(mBaseUri + NEW_WEBDAV_PATH_4_0 + mCredentials.getUsername());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -483,11 +484,15 @@ public class OwnCloudClient extends HttpClient {
|
||||
}
|
||||
|
||||
public void setFollowRedirects(boolean followRedirects) {
|
||||
mFollowRedirects = followRedirects;
|
||||
mOkHttpClient
|
||||
.newBuilder()
|
||||
.followRedirects(followRedirects)
|
||||
.followSslRedirects(followRedirects)
|
||||
.build();
|
||||
}
|
||||
|
||||
public boolean getFollowRedirects() {
|
||||
return mFollowRedirects;
|
||||
return mOkHttpClient.followRedirects();
|
||||
}
|
||||
|
||||
private void logCookiesAtRequest(Header[] headers, String when) {
|
||||
|
@ -0,0 +1,26 @@
|
||||
package com.owncloud.android.lib.common.methods.nonwebdav;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
|
||||
public class GetMethod extends HttpMethod {
|
||||
|
||||
public GetMethod(OkHttpClient okHttpClient, String url) {
|
||||
super(okHttpClient, url);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int execute() throws IOException {
|
||||
final Request request =
|
||||
new Request.Builder()
|
||||
.url(mUrl)
|
||||
.get()
|
||||
.build();
|
||||
|
||||
Response response = mOkHttpClient.newCall(request).execute();
|
||||
return response.code();
|
||||
}
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package com.owncloud.android.lib.common.methods.nonwebdav;
|
||||
|
||||
import com.owncloud.android.lib.common.methods.HttpBaseMethod;
|
||||
|
||||
import okhttp3.OkHttpClient;
|
||||
|
||||
public abstract class HttpMethod implements HttpBaseMethod {
|
||||
|
||||
protected OkHttpClient mOkHttpClient;
|
||||
protected String mUrl;
|
||||
|
||||
public HttpMethod (OkHttpClient okHttpClient, String url) {
|
||||
mOkHttpClient = okHttpClient;
|
||||
mUrl = url;
|
||||
}
|
||||
}
|
@ -1,4 +1,6 @@
|
||||
package com.owncloud.android.lib.common.methods;
|
||||
package com.owncloud.android.lib.common.methods.webdav;
|
||||
|
||||
import com.owncloud.android.lib.common.methods.HttpBaseMethod;
|
||||
|
||||
import at.bitfire.dav4android.DavResource;
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.owncloud.android.lib.common.methods;
|
||||
package com.owncloud.android.lib.common.methods.webdav;
|
||||
|
||||
import java.io.IOException;
|
||||
|
@ -24,6 +24,20 @@
|
||||
|
||||
package com.owncloud.android.lib.common.operations;
|
||||
|
||||
import android.accounts.Account;
|
||||
import android.accounts.AccountsException;
|
||||
|
||||
import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException;
|
||||
import com.owncloud.android.lib.common.network.CertificateCombinedException;
|
||||
import com.owncloud.android.lib.common.utils.Log_OC;
|
||||
|
||||
import org.apache.commons.httpclient.ConnectTimeoutException;
|
||||
import org.apache.commons.httpclient.HttpException;
|
||||
import org.apache.commons.httpclient.HttpMethod;
|
||||
import org.apache.commons.httpclient.HttpStatus;
|
||||
import org.apache.jackrabbit.webdav.DavException;
|
||||
import org.json.JSONException;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
@ -34,24 +48,15 @@ import java.net.SocketException;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import android.accounts.Account;
|
||||
import android.accounts.AccountsException;
|
||||
|
||||
import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException;
|
||||
import com.owncloud.android.lib.common.network.CertificateCombinedException;
|
||||
import com.owncloud.android.lib.common.utils.Log_OC;
|
||||
|
||||
import org.apache.commons.httpclient.ConnectTimeoutException;
|
||||
import org.apache.commons.httpclient.Header;
|
||||
import org.apache.commons.httpclient.HttpException;
|
||||
import org.apache.commons.httpclient.HttpMethod;
|
||||
import org.apache.commons.httpclient.HttpStatus;
|
||||
import org.apache.jackrabbit.webdav.DavException;
|
||||
import org.json.JSONException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.net.ssl.SSLException;
|
||||
|
||||
import okhttp3.Headers;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
|
||||
|
||||
/**
|
||||
* The result of a remote operation required to an ownCloud server.
|
||||
@ -222,12 +227,12 @@ public class RemoteOperationResult implements Serializable {
|
||||
* result.
|
||||
*/
|
||||
public RemoteOperationResult(boolean success, HttpMethod httpMethod) throws IOException {
|
||||
this(
|
||||
success,
|
||||
httpMethod.getStatusCode(),
|
||||
httpMethod.getStatusText(),
|
||||
httpMethod.getResponseHeaders()
|
||||
);
|
||||
// this(
|
||||
// success,
|
||||
// httpMethod.getStatusCode(),
|
||||
// httpMethod.getStatusText(),
|
||||
// httpMethod.getResponseHeaders()
|
||||
// );
|
||||
|
||||
if (mHttpCode == HttpStatus.SC_BAD_REQUEST) { // 400
|
||||
|
||||
@ -265,6 +270,63 @@ public class RemoteOperationResult implements Serializable {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Public constructor from separate elements of an HTTP or DAV response.
|
||||
*
|
||||
* To be used when the result needs to be interpreted from the response of an HTTP/DAV method.
|
||||
*
|
||||
* Determines a {@link ResultCode} from the already executed method received as a parameter. Generally,
|
||||
* will depend on the HTTP code and HTTP response headers received. In some cases will inspect also the
|
||||
* response body
|
||||
*
|
||||
* @param success
|
||||
* @param request
|
||||
* @param response
|
||||
* @throws IOException
|
||||
*/
|
||||
public RemoteOperationResult(boolean success, Request request, Response response) throws IOException {
|
||||
this(success, response.code(), HttpStatus.getStatusText(response.code()), response.headers());
|
||||
|
||||
// TODO success parameter might not be needed
|
||||
if (mHttpCode == HttpStatus.SC_BAD_REQUEST) { // 400
|
||||
|
||||
String bodyResponse = response.body().string();
|
||||
// do not get for other HTTP codes!; could not be available
|
||||
|
||||
if (bodyResponse != null && bodyResponse.length() > 0) {
|
||||
InputStream is = new ByteArrayInputStream(bodyResponse.getBytes());
|
||||
InvalidCharacterExceptionParser xmlParser = new InvalidCharacterExceptionParser();
|
||||
try {
|
||||
if (xmlParser.parseXMLResponse(is)) {
|
||||
mCode = ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER;
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
Log_OC.w(TAG, "Error reading exception from server: " + e.getMessage());
|
||||
// mCode stays as set in this(success, httpCode, headers)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// before
|
||||
switch (mHttpCode) {
|
||||
case HttpStatus.SC_FORBIDDEN:
|
||||
// TODO
|
||||
// parseErrorMessageAndSetCode(request, response, ResultCode.SPECIFIC_FORBIDDEN);
|
||||
break;
|
||||
case HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE:
|
||||
// TODO
|
||||
// parseErrorMessageAndSetCode(request, response, ResultCode.SPECIFIC_UNSUPPORTED_MEDIA_TYPE);
|
||||
break;
|
||||
case HttpStatus.SC_SERVICE_UNAVAILABLE:
|
||||
// TODO
|
||||
// parseErrorMessageAndSetCode(request, response, ResultCode.SPECIFIC_SERVICE_UNAVAILABLE);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the error message included in the body response, if any, and set the specific result
|
||||
* code
|
||||
@ -308,25 +370,61 @@ public class RemoteOperationResult implements Serializable {
|
||||
* @param success The operation was considered successful or not.
|
||||
* @param httpCode HTTP status code returned by an HTTP/DAV method.
|
||||
* @param httpPhrase HTTP status line phrase returned by an HTTP/DAV method
|
||||
* @param httpHeaders HTTP response header returned by an HTTP/DAV method
|
||||
* @param headers HTTP response header returned by an HTTP/DAV method
|
||||
*/
|
||||
public RemoteOperationResult(boolean success, int httpCode, String httpPhrase, Header[] httpHeaders) {
|
||||
public RemoteOperationResult(boolean success, int httpCode, String httpPhrase, Headers headers) {
|
||||
this(success, httpCode, httpPhrase);
|
||||
if (httpHeaders != null) {
|
||||
for (Header httpHeader : httpHeaders) {
|
||||
if ("location".equals(httpHeader.getName().toLowerCase())) {
|
||||
mRedirectedLocation = httpHeader.getValue();
|
||||
if (headers != null) {
|
||||
for (Map.Entry<String, List<String>> header : headers.toMultimap().entrySet()) {
|
||||
if ("location".equals(header.getKey().toLowerCase())) {
|
||||
mRedirectedLocation = header.getValue().get(0);
|
||||
continue;
|
||||
}
|
||||
if ("www-authenticate".equals(httpHeader.getName().toLowerCase())) {
|
||||
mAuthenticate.add(httpHeader.getValue().toLowerCase());
|
||||
if ("www-authenticate".equals(header.getKey().toLowerCase())) {
|
||||
mAuthenticate.add(header.getValue().get(0).toLowerCase());
|
||||
}
|
||||
}
|
||||
}
|
||||
if (isIdPRedirection()) {
|
||||
mCode = ResultCode.UNAUTHORIZED; // overrides default ResultCode.UNKNOWN
|
||||
// overrides default ResultCode.UNKNOWN
|
||||
mCode = com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.UNAUTHORIZED;
|
||||
}
|
||||
}
|
||||
//
|
||||
// /**
|
||||
// * Public constructor from separate elements of an HTTP or DAV response.
|
||||
// *
|
||||
// * To be used when the result needs to be interpreted from HTTP response elements that could come from
|
||||
// * different requests (WARNING: black magic, try to avoid).
|
||||
// *
|
||||
// * If all the fields come from the same HTTP/DAV response, {@link #RemoteOperationResult(boolean, HttpMethod)}
|
||||
// * should be used instead.
|
||||
// *
|
||||
// * Determines a {@link ResultCode} depending on the HTTP code and HTTP response headers received.
|
||||
// *
|
||||
// * @param success The operation was considered successful or not.
|
||||
// * @param httpCode HTTP status code returned by an HTTP/DAV method.
|
||||
// * @param httpPhrase HTTP status line phrase returned by an HTTP/DAV method
|
||||
// * @param httpHeaders HTTP response header returned by an HTTP/DAV method
|
||||
// */
|
||||
// public RemoteOperationResult(boolean success, int httpCode, String httpPhrase, Header[] httpHeaders) {
|
||||
// this(success, httpCode, httpPhrase);
|
||||
// if (httpHeaders != null) {
|
||||
// for (Header httpHeader : httpHeaders) {
|
||||
// if ("location".equals(httpHeader.getName().toLowerCase())) {
|
||||
// mRedirectedLocation = httpHeader.getValue();
|
||||
// continue;
|
||||
// }
|
||||
// if ("www-authenticate".equals(httpHeader.getName().toLowerCase())) {
|
||||
// mAuthenticate.add(httpHeader.getValue().toLowerCase());
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// if (isIdPRedirection()) {
|
||||
// // overrides default ResultCode.UNKNOWN
|
||||
// mCode = ResultCode.UNAUTHORIZED; // overrides default ResultCode.UNKNOWN
|
||||
// }
|
||||
// }
|
||||
|
||||
/**
|
||||
* Private constructor for results built interpreting a HTTP or DAV response.
|
||||
|
@ -25,18 +25,20 @@
|
||||
package com.owncloud.android.lib.resources.files;
|
||||
|
||||
import org.apache.commons.httpclient.HttpStatus;
|
||||
import org.apache.jackrabbit.webdav.DavConstants;
|
||||
import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.owncloud.android.lib.common.OwnCloudClient;
|
||||
import com.owncloud.android.lib.common.methods.webdav.PropfindMethod;
|
||||
import com.owncloud.android.lib.common.network.RedirectionPath;
|
||||
import com.owncloud.android.lib.common.network.WebdavUtils;
|
||||
import com.owncloud.android.lib.common.operations.RemoteOperation;
|
||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
|
||||
import com.owncloud.android.lib.common.utils.Log_OC;
|
||||
|
||||
import at.bitfire.dav4android.DavOCResource;
|
||||
import okhttp3.HttpUrl;
|
||||
|
||||
import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK;
|
||||
|
||||
/**
|
||||
* Operation to check the existence or absence of a path in a remote server.
|
||||
*
|
||||
@ -57,8 +59,6 @@ public class ExistenceCheckRemoteOperation extends RemoteOperation {
|
||||
|
||||
/** Sequence of redirections followed. Available only after executing the operation */
|
||||
private RedirectionPath mRedirectionPath = null;
|
||||
// TODO move to {@link RemoteOperation}, that needs a nice refactoring
|
||||
|
||||
/**
|
||||
* Full constructor. Success of the operation will depend upon the value of successIfAbsent.
|
||||
*
|
||||
@ -73,38 +73,50 @@ public class ExistenceCheckRemoteOperation extends RemoteOperation {
|
||||
|
||||
@Override
|
||||
protected RemoteOperationResult run(OwnCloudClient client) {
|
||||
RemoteOperationResult result = null;
|
||||
PropFindMethod propfind = null;
|
||||
boolean previousFollowRedirects = client.getFollowRedirects();
|
||||
RemoteOperationResult result;
|
||||
|
||||
// TODO Complete redirection stuff, although OkHttp should follow redirections by default
|
||||
// boolean previousFollowRedirects = client.getFollowRedirects();
|
||||
|
||||
try {
|
||||
propfind = new PropFindMethod(client.getWebdavUri() + WebdavUtils.encodePath(mPath),
|
||||
WebdavUtils.getAllPropSet(), DavConstants.DEPTH_0);
|
||||
client.setFollowRedirects(false);
|
||||
int status = client.executeMethod(propfind, TIMEOUT, TIMEOUT);
|
||||
if (previousFollowRedirects) {
|
||||
mRedirectionPath = client.followRedirection(propfind);
|
||||
status = mRedirectionPath.getLastStatus();
|
||||
}
|
||||
if (status != FORBIDDEN_ERROR && status != SERVICE_UNAVAILABLE_ERROR) {
|
||||
client.exhaustResponse(propfind.getResponseBodyAsStream());
|
||||
}
|
||||
|
||||
DavOCResource davOCResource = new DavOCResource(
|
||||
client.getOkHttpClient(),
|
||||
HttpUrl.parse(client.getNewWebDavUri() + WebdavUtils.encodePath(mPath))
|
||||
);
|
||||
|
||||
// client.setFollowRedirects(false);
|
||||
|
||||
PropfindMethod propfindMethod = new PropfindMethod(davOCResource, 0);
|
||||
int status = client.executeHttpMethod(propfindMethod);
|
||||
|
||||
// if (previousFollowRedirects) {
|
||||
// mRedirectionPath = client.followRedirection(propfind);
|
||||
// status = mRedirectionPath.getLastStatus();
|
||||
// }
|
||||
// if (status != FORBIDDEN_ERROR && status != SERVICE_UNAVAILABLE_ERROR) {
|
||||
// client.exhaustResponse(propfind.getResponseBodyAsStream());
|
||||
// }
|
||||
|
||||
/**
|
||||
* PROPFIND method
|
||||
* 404 NOT FOUND: path doesn't exist,
|
||||
* 207 MULTI_STATUS: path exists.
|
||||
*/
|
||||
boolean success = ((status == HttpStatus.SC_OK || status == HttpStatus.SC_MULTI_STATUS) &&
|
||||
boolean isSuccess = ((status == HttpStatus.SC_OK || status == HttpStatus.SC_MULTI_STATUS) &&
|
||||
!mSuccessIfAbsent) ||
|
||||
(status == HttpStatus.SC_MULTI_STATUS && !mSuccessIfAbsent) ||
|
||||
(status == HttpStatus.SC_NOT_FOUND && mSuccessIfAbsent);
|
||||
result = new RemoteOperationResult(
|
||||
success,
|
||||
propfind
|
||||
);
|
||||
|
||||
result = isSuccess
|
||||
? new RemoteOperationResult(OK)
|
||||
: new RemoteOperationResult(false, davOCResource.getRequest(), davOCResource.getResponse());
|
||||
|
||||
|
||||
Log_OC.d(TAG, "Existence check for " + client.getWebdavUri() +
|
||||
WebdavUtils.encodePath(mPath) + " targeting for " +
|
||||
(mSuccessIfAbsent ? " absence " : " existence ") +
|
||||
"finished with HTTP status " + status + (!success?"(FAIL)":""));
|
||||
"finished with HTTP status " + status + (!isSuccess?"(FAIL)":""));
|
||||
|
||||
} catch (Exception e) {
|
||||
result = new RemoteOperationResult(e);
|
||||
@ -114,9 +126,7 @@ public class ExistenceCheckRemoteOperation extends RemoteOperation {
|
||||
result.getLogMessage(), result.getException());
|
||||
|
||||
} finally {
|
||||
if (propfind != null)
|
||||
propfind.releaseConnection();
|
||||
client.setFollowRedirects(previousFollowRedirects);
|
||||
// client.setFollowRedirects(previousFollowRedirects);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
@ -1,22 +1,22 @@
|
||||
/* ownCloud Android Library is available under MIT license
|
||||
* Copyright (C) 2016 ownCloud GmbH.
|
||||
*
|
||||
* Copyright (C) 2018 ownCloud GmbH.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
@ -25,27 +25,27 @@
|
||||
package com.owncloud.android.lib.resources.files;
|
||||
|
||||
import com.owncloud.android.lib.common.OwnCloudClient;
|
||||
import com.owncloud.android.lib.common.methods.PropfindMethod;
|
||||
import com.owncloud.android.lib.common.network.WebdavEntry;
|
||||
import com.owncloud.android.lib.common.http.HttpConstants;
|
||||
import com.owncloud.android.lib.common.http.webdav.PropfindMethod;
|
||||
import com.owncloud.android.lib.common.network.WebdavUtils;
|
||||
import com.owncloud.android.lib.common.operations.RemoteOperation;
|
||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
|
||||
import com.owncloud.android.lib.common.utils.Log_OC;
|
||||
|
||||
import org.apache.commons.httpclient.HttpStatus;
|
||||
import org.apache.jackrabbit.webdav.MultiStatus;
|
||||
import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import at.bitfire.dav4android.DavOCResource;
|
||||
import at.bitfire.dav4android.DavResource;
|
||||
import okhttp3.HttpUrl;
|
||||
import okhttp3.Response;
|
||||
|
||||
import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK;
|
||||
|
||||
/**
|
||||
* Remote operation performing the read of remote file or folder in the ownCloud server.
|
||||
*
|
||||
* @author David A. Velasco
|
||||
* @author masensio
|
||||
* @author David González Verdugo
|
||||
*/
|
||||
|
||||
public class ReadRemoteFolderOperation extends RemoteOperation {
|
||||
@ -53,7 +53,6 @@ public class ReadRemoteFolderOperation extends RemoteOperation {
|
||||
private static final String TAG = ReadRemoteFolderOperation.class.getSimpleName();
|
||||
|
||||
private String mRemotePath;
|
||||
private ArrayList<Object> mFolderAndFiles;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
@ -72,55 +71,57 @@ public class ReadRemoteFolderOperation extends RemoteOperation {
|
||||
@Override
|
||||
protected RemoteOperationResult run(OwnCloudClient client) {
|
||||
RemoteOperationResult result = null;
|
||||
PropFindMethod query = null;
|
||||
|
||||
try {
|
||||
|
||||
final HttpUrl location = HttpUrl.parse(client.getNewWebDavUri() + WebdavUtils.encodePath(mRemotePath));
|
||||
DavOCResource davOCResource = new DavOCResource(client.getOkHttpClient(), location);
|
||||
PropfindMethod propfindMethod = new PropfindMethod(davOCResource, 1);
|
||||
PropfindMethod propfindMethod = new PropfindMethod(
|
||||
client.getOkHttpClient(),
|
||||
HttpUrl.parse(client.getNewWebDavUri() + WebdavUtils.encodePath(mRemotePath)),
|
||||
1);
|
||||
|
||||
int status = client.executeHttpMethod(propfindMethod);
|
||||
Response response = client.executeHttpMethod(propfindMethod);
|
||||
|
||||
// TODO Refactor from here down
|
||||
// remote request
|
||||
// query = new PropFindMethod(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath),
|
||||
// WebdavUtils.getAllPropSet(), // PropFind Properties
|
||||
// DavConstants.DEPTH_1);
|
||||
// int status = client.executeMethod(query);
|
||||
//
|
||||
// // check and process response
|
||||
// boolean isSuccess = (
|
||||
// status == HttpStatus.SC_MULTI_STATUS ||
|
||||
// status == HttpStatus.SC_OK
|
||||
// );
|
||||
// if (isSuccess) {
|
||||
// // get data from remote folder
|
||||
// MultiStatus dataInServer = query.getResponseBodyAsMultiStatus();
|
||||
// readData(dataInServer, client);
|
||||
//
|
||||
// // Result of the operation
|
||||
// result = new RemoteOperationResult(true, query);
|
||||
// // Add data to the result
|
||||
// if (result.isSuccess()) {
|
||||
// result.setData(mFolderAndFiles);
|
||||
// }
|
||||
// } else {
|
||||
// // synchronization failed
|
||||
// result = new RemoteOperationResult(false, query);
|
||||
// }
|
||||
boolean isSuccess = (response.code() == HttpConstants.HTTP_MULTI_STATUS
|
||||
|| response.code() == HttpConstants.HTTP_OK);
|
||||
|
||||
if (isSuccess) {
|
||||
|
||||
ArrayList<Object> mFolderAndFiles = new ArrayList<>();
|
||||
|
||||
// parse data from remote folder
|
||||
mFolderAndFiles.add(
|
||||
new RemoteFile(propfindMethod.getDavResource(), client.getAccount().getDisplayName())
|
||||
);
|
||||
|
||||
// loop to update every child
|
||||
for (DavResource resource : propfindMethod.getMembers()) {
|
||||
RemoteFile file = new RemoteFile(resource, client.getAccount().getDisplayName());
|
||||
mFolderAndFiles.add(file);
|
||||
}
|
||||
|
||||
// Result of the operation
|
||||
result = new RemoteOperationResult(OK);
|
||||
result.setData(mFolderAndFiles);
|
||||
|
||||
} else {
|
||||
|
||||
// synchronization failed
|
||||
result = new RemoteOperationResult(
|
||||
false,
|
||||
propfindMethod.getRequest(),
|
||||
response
|
||||
);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
result = new RemoteOperationResult(e);
|
||||
} finally {
|
||||
if (query != null)
|
||||
query.releaseConnection(); // let the connection available for other methods
|
||||
if (result.isSuccess()) {
|
||||
Log_OC.i(TAG, "Synchronized " + mRemotePath + ": " + result.getLogMessage());
|
||||
} else {
|
||||
if (result.isException()) {
|
||||
Log_OC.e(TAG, "Synchronized " + mRemotePath + ": " + result.getLogMessage(),
|
||||
result.getException());
|
||||
result.getException());
|
||||
} else {
|
||||
Log_OC.e(TAG, "Synchronized " + mRemotePath + ": " + result.getLogMessage());
|
||||
}
|
||||
@ -128,57 +129,4 @@ public class ReadRemoteFolderOperation extends RemoteOperation {
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public boolean isMultiStatus(int status) {
|
||||
return (status == HttpStatus.SC_MULTI_STATUS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the data retrieved from the server about the contents of the target folder
|
||||
*
|
||||
* @param remoteData Full response got from the server with the data of the target
|
||||
* folder and its direct children.
|
||||
* @param client Client instance to the remote server where the data were
|
||||
* retrieved.
|
||||
* @return
|
||||
*/
|
||||
private void readData(MultiStatus remoteData, OwnCloudClient client) {
|
||||
mFolderAndFiles = new ArrayList<Object>();
|
||||
|
||||
// parse data from remote folder
|
||||
WebdavEntry we = new WebdavEntry(remoteData.getResponses()[0],
|
||||
client.getNewWebDavUri().getPath());
|
||||
mFolderAndFiles.add(fillOCFile(we));
|
||||
|
||||
// loop to update every child
|
||||
RemoteFile remoteFile = null;
|
||||
for (int i = 1; i < remoteData.getResponses().length; ++i) {
|
||||
/// new OCFile instance with the data from the server
|
||||
we = new WebdavEntry(remoteData.getResponses()[i], client.getNewWebDavUri().getPath());
|
||||
remoteFile = fillOCFile(we);
|
||||
mFolderAndFiles.add(remoteFile);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates and populates a new {@link RemoteFile} object with the data read from the server.
|
||||
*
|
||||
* @param we WebDAV entry read from the server for a WebDAV resource (remote file or folder).
|
||||
* @return New OCFile instance representing the remote resource described by we.
|
||||
*/
|
||||
private RemoteFile fillOCFile(WebdavEntry we) {
|
||||
RemoteFile file = new RemoteFile(we.decodedPath());
|
||||
file.setCreationTimestamp(we.createTimestamp());
|
||||
file.setLength(we.contentLength());
|
||||
file.setMimeType(we.contentType());
|
||||
file.setModifiedTimestamp(we.modifiedTimestamp());
|
||||
file.setEtag(we.etag());
|
||||
file.setPermissions(we.permissions());
|
||||
file.setRemoteId(we.remoteId());
|
||||
file.setSize(we.size());
|
||||
file.setQuotaUsedBytes(we.quotaUsedBytes());
|
||||
file.setQuotaAvailableBytes(we.quotaAvailableBytes());
|
||||
file.setPrivateLink(we.privateLink());
|
||||
return file;
|
||||
}
|
||||
}
|
||||
}
|
@ -31,6 +31,7 @@ import android.net.Uri;
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
|
||||
import com.owncloud.android.lib.common.OwnCloudClient;
|
||||
import com.owncloud.android.lib.common.network.WebdavEntry;
|
||||
import com.owncloud.android.lib.refactor.OCContext;
|
||||
import com.owncloud.android.lib.refactor.operations.RemoteOperation;
|
||||
@ -173,7 +174,7 @@ public class RemoteFile implements Parcelable, Serializable {
|
||||
|
||||
/**
|
||||
* Create new {@link RemoteFile} with given path.
|
||||
* <p>
|
||||
*
|
||||
* The path received must be URL-decoded. Path separator must be OCFile.PATH_SEPARATOR, and it must be the first character in 'path'.
|
||||
*
|
||||
* @param path The remote path of the file.
|
||||
@ -201,8 +202,8 @@ public class RemoteFile implements Parcelable, Serializable {
|
||||
this.setPrivateLink(webdavEntry.privateLink());
|
||||
}
|
||||
|
||||
public RemoteFile(final DavResource davResource, OCContext context) {
|
||||
this(getRemotePathFromUrl(davResource.getLocation(), context));
|
||||
public RemoteFile(final DavResource davResource, String displayName) {
|
||||
this(getRemotePathFromUrl(davResource.getLocation(), displayName));
|
||||
final PropertyCollection properties = davResource.getProperties();
|
||||
this.setCreationTimestamp(properties.get(CreationDate.class) != null
|
||||
? Long.parseLong(properties.get(CreationDate.class).getCreationDate())
|
||||
@ -227,9 +228,9 @@ public class RemoteFile implements Parcelable, Serializable {
|
||||
}
|
||||
|
||||
|
||||
private static String getRemotePathFromUrl(HttpUrl url, OCContext context) {
|
||||
private static String getRemotePathFromUrl(HttpUrl url, String displayName) {
|
||||
final String pathToRemove =
|
||||
"/" + RemoteOperation.WEBDAV_PATH_4_0 + "/" + context.getOCAccount().getDisplayName();
|
||||
"/" + RemoteOperation.WEBDAV_PATH_4_0 + "/" + displayName;
|
||||
return Uri.decode(url.encodedPath()).replace(pathToRemove, "");
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user