mirror of
https://github.com/owncloud/android-library.git
synced 2026-08-18 11:52:56 +00:00
Working on authentication operations
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
+3
-1
@@ -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
-1
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user