mirror of
https://github.com/owncloud/android-library.git
synced 2026-08-16 19:03:00 +00:00
Create http + webdav methods wrapeer
This commit is contained in:
@@ -45,12 +45,11 @@ import org.apache.commons.httpclient.HttpConnectionManager;
|
||||
import org.apache.commons.httpclient.HttpMethod;
|
||||
import org.apache.commons.httpclient.HttpMethodBase;
|
||||
import org.apache.commons.httpclient.HttpStatus;
|
||||
import org.apache.commons.httpclient.HttpVersion;
|
||||
import org.apache.commons.httpclient.URI;
|
||||
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.HttpParams;
|
||||
import com.owncloud.android.lib.common.methods.HttpBaseMethod;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
@@ -103,47 +102,50 @@ public class OwnCloudClient extends HttpClient {
|
||||
|
||||
private String mRedirectedLocation;
|
||||
|
||||
private static OkHttpClient mClient = null;
|
||||
private static OkHttpClient mOkHttpClient = null;
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
|
||||
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();
|
||||
|
||||
if (mClient == null) {
|
||||
new OkHttpClient.Builder()
|
||||
if (mOkHttpClient == null) {
|
||||
mOkHttpClient = new OkHttpClient.Builder()
|
||||
.addInterceptor(chain ->
|
||||
chain.proceed(
|
||||
chain.request()
|
||||
@@ -175,10 +177,8 @@ public class OwnCloudClient extends HttpClient {
|
||||
// applyProxySettings();
|
||||
|
||||
clearCredentials();
|
||||
|
||||
}
|
||||
|
||||
|
||||
private void applyProxySettings() {
|
||||
String proxyHost = System.getProperty("http.proxyHost");
|
||||
String proxyPortSt = System.getProperty("http.proxyPort");
|
||||
@@ -198,7 +198,6 @@ public class OwnCloudClient extends HttpClient {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void setCredentials(OwnCloudCredentials credentials) {
|
||||
if (credentials != null) {
|
||||
mCredentials = credentials;
|
||||
@@ -248,7 +247,6 @@ public class OwnCloudClient extends HttpClient {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Requests the received method.
|
||||
*
|
||||
@@ -300,6 +298,11 @@ public class OwnCloudClient extends HttpClient {
|
||||
return status;
|
||||
}
|
||||
|
||||
public int executeHttpMethod (HttpBaseMethod method) throws Exception {
|
||||
int status = method.execute();
|
||||
return status;
|
||||
}
|
||||
|
||||
private void checkFirstRedirection(HttpMethod method) {
|
||||
Header[] httpHeaders = method.getResponseHeaders();
|
||||
|
||||
@@ -338,7 +341,6 @@ public class OwnCloudClient extends HttpClient {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public RedirectionPath followRedirection(HttpMethod method) throws IOException {
|
||||
int redirectionsCount = 0;
|
||||
int status = method.getStatusCode();
|
||||
@@ -562,6 +564,10 @@ public class OwnCloudClient extends HttpClient {
|
||||
return mAccount;
|
||||
}
|
||||
|
||||
public OkHttpClient getOkHttpClient() {
|
||||
return mOkHttpClient;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enables or disables silent refresh of credentials, if supported by credentials themselves.
|
||||
*/
|
||||
|
||||
@@ -25,15 +25,17 @@ package com.owncloud.android.lib.common.authentication;
|
||||
|
||||
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.auth.AuthPolicy;
|
||||
import org.apache.commons.httpclient.auth.AuthScope;
|
||||
import org.apache.commons.httpclient.auth.AuthState;
|
||||
import org.apache.commons.httpclient.auth.BasicScheme;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import okhttp3.Credentials;
|
||||
|
||||
public class OwnCloudBasicCredentials implements OwnCloudCredentials {
|
||||
|
||||
private String mUsername;
|
||||
@@ -58,14 +60,22 @@ public class OwnCloudBasicCredentials implements OwnCloudCredentials {
|
||||
|
||||
List<String> authPrefs = new ArrayList<String>(1);
|
||||
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().setCredentialCharset(OwnCloudCredentialsFactory.CREDENTIAL_CHARSET);
|
||||
client.getState().setCredentials(
|
||||
AuthScope.ANY,
|
||||
new UsernamePasswordCredentials(mUsername, mPassword)
|
||||
);
|
||||
client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(mUsername, mPassword));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.owncloud.android.lib.common.methods;
|
||||
|
||||
public interface HttpBaseMethod {
|
||||
|
||||
int execute() throws Exception;
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user