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

Create http + webdav methods wrapeer

This commit is contained in:
davigonz 2018-06-01 08:52:08 +02:00
parent b9ba124541
commit 11938a1fcc
7 changed files with 153 additions and 85 deletions

View File

@ -45,12 +45,11 @@ import org.apache.commons.httpclient.HttpConnectionManager;
import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpMethodBase; import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.HttpVersion;
import org.apache.commons.httpclient.URI; import org.apache.commons.httpclient.URI;
import org.apache.commons.httpclient.URIException; import org.apache.commons.httpclient.URIException;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.params.HttpMethodParams; import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.httpclient.params.HttpParams; import org.apache.commons.httpclient.params.HttpParams;
import com.owncloud.android.lib.common.methods.HttpBaseMethod;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -103,47 +102,50 @@ public class OwnCloudClient extends HttpClient {
private String mRedirectedLocation; private String mRedirectedLocation;
private static OkHttpClient mClient = null; private static OkHttpClient mOkHttpClient = null;
/** /**
* Constructor * Constructor
*/ */
// public OwnCloudClient(Uri baseUri, HttpConnectionManager connectionMgr) {
//
// super(connectionMgr);
//
// if (baseUri == null) {
// throw new IllegalArgumentException("Parameter 'baseUri' cannot be NULL");
// }
// mBaseUri = baseUri;
//
// mInstanceNumber = sIntanceCounter++;
// Log_OC.d(TAG + " #" + mInstanceNumber, "Creating OwnCloudClient");
//
// String userAgent = OwnCloudClientManagerFactory.getUserAgent();
// getParams().setParameter(HttpMethodParams.USER_AGENT, userAgent);
// getParams().setParameter(
// PARAM_PROTOCOL_VERSION,
// HttpVersion.HTTP_1_1
// );
//
// getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
// getParams().setParameter(
// PARAM_SINGLE_COOKIE_HEADER, // to avoid problems with some web servers
// PARAM_SINGLE_COOKIE_HEADER_VALUE
// );
//
// applyProxySettings();
//
// clearCredentials();
// }
public OwnCloudClient(Uri baseUri, HttpConnectionManager connectionMgr) { public OwnCloudClient(Uri baseUri, HttpConnectionManager connectionMgr) {
super(connectionMgr); super(connectionMgr);
if (baseUri == null) {
throw new IllegalArgumentException("Parameter 'baseUri' cannot be NULL");
}
mBaseUri = baseUri;
mInstanceNumber = sIntanceCounter++;
Log_OC.d(TAG + " #" + mInstanceNumber, "Creating OwnCloudClient");
String userAgent = OwnCloudClientManagerFactory.getUserAgent();
getParams().setParameter(HttpMethodParams.USER_AGENT, userAgent);
getParams().setParameter(
PARAM_PROTOCOL_VERSION,
HttpVersion.HTTP_1_1
);
getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
getParams().setParameter(
PARAM_SINGLE_COOKIE_HEADER, // to avoid problems with some web servers
PARAM_SINGLE_COOKIE_HEADER_VALUE
);
applyProxySettings();
clearCredentials();
}
public OwnCloudClient(Uri baseUri) {
String userAgent = OwnCloudClientManagerFactory.getUserAgent(); String userAgent = OwnCloudClientManagerFactory.getUserAgent();
if (mClient == null) { if (mOkHttpClient == null) {
new OkHttpClient.Builder() mOkHttpClient = new OkHttpClient.Builder()
.addInterceptor(chain -> .addInterceptor(chain ->
chain.proceed( chain.proceed(
chain.request() chain.request()
@ -175,10 +177,8 @@ public class OwnCloudClient extends HttpClient {
// applyProxySettings(); // applyProxySettings();
clearCredentials(); clearCredentials();
} }
private void applyProxySettings() { private void applyProxySettings() {
String proxyHost = System.getProperty("http.proxyHost"); String proxyHost = System.getProperty("http.proxyHost");
String proxyPortSt = System.getProperty("http.proxyPort"); String proxyPortSt = System.getProperty("http.proxyPort");
@ -198,7 +198,6 @@ public class OwnCloudClient extends HttpClient {
} }
} }
public void setCredentials(OwnCloudCredentials credentials) { public void setCredentials(OwnCloudCredentials credentials) {
if (credentials != null) { if (credentials != null) {
mCredentials = credentials; mCredentials = credentials;
@ -248,7 +247,6 @@ public class OwnCloudClient extends HttpClient {
} }
} }
/** /**
* Requests the received method. * Requests the received method.
* *
@ -300,6 +298,11 @@ public class OwnCloudClient extends HttpClient {
return status; return status;
} }
public int executeHttpMethod (HttpBaseMethod method) throws Exception {
int status = method.execute();
return status;
}
private void checkFirstRedirection(HttpMethod method) { private void checkFirstRedirection(HttpMethod method) {
Header[] httpHeaders = method.getResponseHeaders(); Header[] httpHeaders = method.getResponseHeaders();
@ -338,7 +341,6 @@ public class OwnCloudClient extends HttpClient {
} }
} }
public RedirectionPath followRedirection(HttpMethod method) throws IOException { public RedirectionPath followRedirection(HttpMethod method) throws IOException {
int redirectionsCount = 0; int redirectionsCount = 0;
int status = method.getStatusCode(); int status = method.getStatusCode();
@ -562,6 +564,10 @@ public class OwnCloudClient extends HttpClient {
return mAccount; return mAccount;
} }
public OkHttpClient getOkHttpClient() {
return mOkHttpClient;
}
/** /**
* Enables or disables silent refresh of credentials, if supported by credentials themselves. * Enables or disables silent refresh of credentials, if supported by credentials themselves.
*/ */

View File

@ -25,15 +25,17 @@ package com.owncloud.android.lib.common.authentication;
import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.OwnCloudClient;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.httpclient.UsernamePasswordCredentials; import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthPolicy; import org.apache.commons.httpclient.auth.AuthPolicy;
import org.apache.commons.httpclient.auth.AuthScope; import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.auth.AuthState; import org.apache.commons.httpclient.auth.AuthState;
import org.apache.commons.httpclient.auth.BasicScheme; import org.apache.commons.httpclient.auth.BasicScheme;
import java.util.ArrayList;
import java.util.List;
import okhttp3.Credentials;
public class OwnCloudBasicCredentials implements OwnCloudCredentials { public class OwnCloudBasicCredentials implements OwnCloudCredentials {
private String mUsername; private String mUsername;
@ -58,14 +60,22 @@ public class OwnCloudBasicCredentials implements OwnCloudCredentials {
List<String> authPrefs = new ArrayList<String>(1); List<String> authPrefs = new ArrayList<String>(1);
authPrefs.add(AuthPolicy.BASIC); authPrefs.add(AuthPolicy.BASIC);
client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
client.getOkHttpClient().newBuilder()
.addInterceptor(chain ->
chain.proceed(
chain.request()
.newBuilder()
.addHeader("Authorization", Credentials.basic(mUsername, mPassword))
.build()
)
).build();
//TODO
client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
client.getParams().setAuthenticationPreemptive(mAuthenticationPreemptive); client.getParams().setAuthenticationPreemptive(mAuthenticationPreemptive);
client.getParams().setCredentialCharset(OwnCloudCredentialsFactory.CREDENTIAL_CHARSET); client.getParams().setCredentialCharset(OwnCloudCredentialsFactory.CREDENTIAL_CHARSET);
client.getState().setCredentials( client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(mUsername, mPassword));
AuthScope.ANY,
new UsernamePasswordCredentials(mUsername, mPassword)
);
} }
@Override @Override

View File

@ -0,0 +1,14 @@
package com.owncloud.android.lib.common.methods;
import java.io.IOException;
import at.bitfire.dav4android.DavResource;
public abstract class DavMethod implements HttpBaseMethod {
protected DavResource mDavResource;
public DavMethod(DavResource davResource) {
mDavResource = davResource;
}
}

View File

@ -0,0 +1,6 @@
package com.owncloud.android.lib.common.methods;
public interface HttpBaseMethod {
int execute() throws Exception;
}

View File

@ -0,0 +1,23 @@
package com.owncloud.android.lib.common.methods;
import java.io.IOException;
import at.bitfire.dav4android.DavResource;
import at.bitfire.dav4android.PropertyUtils;
import at.bitfire.dav4android.exception.DavException;
import at.bitfire.dav4android.exception.HttpException;
public class PropfindMethod extends DavMethod {
private int mDepth;
public PropfindMethod(DavResource davResource, int depth) {
super(davResource);
mDepth = depth;
};
public int execute() throws DavException, IOException, HttpException {
mDavResource.propfind(mDepth, PropertyUtils.INSTANCE.getAllPropSet());
return mDavResource.getResponse().code();
}
}

View File

@ -92,11 +92,12 @@ public class UploadRemoteFileOperation extends RemoteOperation<Void> {
} }
} }
// public void cancel() { public void cancel() {
// synchronized (mCancellationRequested) { synchronized (mCancellationRequested) {
// mCancellationRequested.set(true); mCancellationRequested.set(true);
// TODO
// if (mPutMethod != null) // if (mPutMethod != null)
// mPutMethod.abort(); // mPutMethod.abort();
// } }
// } }
} }

View File

@ -24,21 +24,22 @@
package com.owncloud.android.lib.resources.files; package com.owncloud.android.lib.resources.files;
import java.util.ArrayList;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.jackrabbit.webdav.DavConstants;
import org.apache.jackrabbit.webdav.MultiStatus;
import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;
import com.owncloud.android.lib.common.OwnCloudClient; 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.network.WebdavEntry;
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;
import com.owncloud.android.lib.common.utils.Log_OC; import com.owncloud.android.lib.common.utils.Log_OC;
import at.bitfire.dav4android.DavResource; 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 okhttp3.HttpUrl;
/** /**
* Remote operation performing the read of remote file or folder in the ownCloud server. * Remote operation performing the read of remote file or folder in the ownCloud server.
@ -75,32 +76,40 @@ public class ReadRemoteFolderOperation extends RemoteOperation {
try { try {
final HttpUrl location = HttpUrl.parse(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath));
DavOCResource davOCResource = new DavOCResource(client.getOkHttpClient(), location);
PropfindMethod propfindMethod = new PropfindMethod(davOCResource, 1);
int status = client.executeHttpMethod(propfindMethod);
// TODO Refactor from here down
// remote request // remote request
query = new PropFindMethod(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath), // query = new PropFindMethod(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath),
WebdavUtils.getAllPropSet(), // PropFind Properties // WebdavUtils.getAllPropSet(), // PropFind Properties
DavConstants.DEPTH_1); // DavConstants.DEPTH_1);
int status = client.executeMethod(query); // int status = client.executeMethod(query);
//
// check and process response // // check and process response
boolean isSuccess = ( // boolean isSuccess = (
status == HttpStatus.SC_MULTI_STATUS || // status == HttpStatus.SC_MULTI_STATUS ||
status == HttpStatus.SC_OK // status == HttpStatus.SC_OK
); // );
if (isSuccess) { // if (isSuccess) {
// get data from remote folder // // get data from remote folder
MultiStatus dataInServer = query.getResponseBodyAsMultiStatus(); // MultiStatus dataInServer = query.getResponseBodyAsMultiStatus();
readData(dataInServer, client); // readData(dataInServer, client);
//
// Result of the operation // // Result of the operation
result = new RemoteOperationResult(true, query); // result = new RemoteOperationResult(true, query);
// Add data to the result // // Add data to the result
if (result.isSuccess()) { // if (result.isSuccess()) {
result.setData(mFolderAndFiles); // result.setData(mFolderAndFiles);
} // }
} else { // } else {
// synchronization failed // // synchronization failed
result = new RemoteOperationResult(false, query); // result = new RemoteOperationResult(false, query);
} // }
} catch (Exception e) { } catch (Exception e) {
result = new RemoteOperationResult(e); result = new RemoteOperationResult(e);
@ -119,7 +128,6 @@ public class ReadRemoteFolderOperation extends RemoteOperation {
Log_OC.e(TAG, "Synchronized " + mRemotePath + ": " + result.getLogMessage()); Log_OC.e(TAG, "Synchronized " + mRemotePath + ": " + result.getLogMessage());
} }
} }
} }
return result; return result;
} }