mirror of
https://github.com/owncloud/android-library.git
synced 2025-06-07 16:06:08 +00:00
Refactor httm methods to return response instead of response code
This commit is contained in:
parent
25bba85ee1
commit
01d4592de0
@ -59,6 +59,7 @@ import java.util.Arrays;
|
|||||||
|
|
||||||
import okhttp3.OkHttpClient;
|
import okhttp3.OkHttpClient;
|
||||||
import okhttp3.Protocol;
|
import okhttp3.Protocol;
|
||||||
|
import okhttp3.Response;
|
||||||
|
|
||||||
public class OwnCloudClient extends HttpClient {
|
public class OwnCloudClient extends HttpClient {
|
||||||
|
|
||||||
@ -309,21 +310,21 @@ public class OwnCloudClient extends HttpClient {
|
|||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int executeHttpMethod (HttpBaseMethod method) throws Exception {
|
public Response executeHttpMethod (HttpBaseMethod method) throws Exception {
|
||||||
|
|
||||||
boolean repeatWithFreshCredentials;
|
boolean repeatWithFreshCredentials;
|
||||||
int repeatCounter = 0;
|
int repeatCounter = 0;
|
||||||
int status;
|
Response response;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
status = method.execute();
|
response = method.execute();
|
||||||
repeatWithFreshCredentials = checkUnauthorizedAccess(status, repeatCounter);
|
repeatWithFreshCredentials = checkUnauthorizedAccess(response.code(), repeatCounter);
|
||||||
if (repeatWithFreshCredentials) {
|
if (repeatWithFreshCredentials) {
|
||||||
repeatCounter++;
|
repeatCounter++;
|
||||||
}
|
}
|
||||||
} while (repeatWithFreshCredentials);
|
} while (repeatWithFreshCredentials);
|
||||||
|
|
||||||
return status;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkFirstRedirection(HttpMethod method) {
|
private void checkFirstRedirection(HttpMethod method) {
|
||||||
|
@ -4,15 +4,10 @@ import okhttp3.Request;
|
|||||||
import okhttp3.Response;
|
import okhttp3.Response;
|
||||||
|
|
||||||
public abstract class HttpBaseMethod {
|
public abstract class HttpBaseMethod {
|
||||||
public abstract int execute() throws Exception;
|
public abstract Response execute() throws Exception;
|
||||||
protected Request mRequest;
|
protected Request mRequest;
|
||||||
protected Response mResponse;
|
|
||||||
|
|
||||||
public Request getRequest() {
|
public Request getRequest() {
|
||||||
return mRequest;
|
return mRequest;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Response getResponse() {
|
|
||||||
return mResponse;
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -4,6 +4,7 @@ import java.io.IOException;
|
|||||||
|
|
||||||
import okhttp3.OkHttpClient;
|
import okhttp3.OkHttpClient;
|
||||||
import okhttp3.Request;
|
import okhttp3.Request;
|
||||||
|
import okhttp3.Response;
|
||||||
|
|
||||||
public class GetMethod extends HttpMethod {
|
public class GetMethod extends HttpMethod {
|
||||||
|
|
||||||
@ -12,13 +13,12 @@ public class GetMethod extends HttpMethod {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int execute() throws IOException {
|
public Response execute() throws IOException {
|
||||||
mRequest = mBaseRequest
|
mRequest = mBaseRequest
|
||||||
.newBuilder()
|
.newBuilder()
|
||||||
.get()
|
.get()
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
mResponse = mOkHttpClient.newCall(mRequest).execute();
|
return mOkHttpClient.newCall(mRequest).execute();
|
||||||
return mResponse.code();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -10,6 +10,7 @@ import at.bitfire.dav4android.exception.HttpException;
|
|||||||
import at.bitfire.dav4android.exception.UnauthorizedException;
|
import at.bitfire.dav4android.exception.UnauthorizedException;
|
||||||
import okhttp3.HttpUrl;
|
import okhttp3.HttpUrl;
|
||||||
import okhttp3.OkHttpClient;
|
import okhttp3.OkHttpClient;
|
||||||
|
import okhttp3.Response;
|
||||||
|
|
||||||
public class PropfindMethod extends DavMethod {
|
public class PropfindMethod extends DavMethod {
|
||||||
|
|
||||||
@ -22,8 +23,7 @@ public class PropfindMethod extends DavMethod {
|
|||||||
};
|
};
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int execute() throws IOException, HttpException, DavException {
|
public Response execute() throws IOException, HttpException, DavException {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
mDavResource.propfind(mDepth, PropertyUtils.INSTANCE.getAllPropSet());
|
mDavResource.propfind(mDepth, PropertyUtils.INSTANCE.getAllPropSet());
|
||||||
mMembers = mDavResource.getMembers();
|
mMembers = mDavResource.getMembers();
|
||||||
@ -32,9 +32,7 @@ public class PropfindMethod extends DavMethod {
|
|||||||
}
|
}
|
||||||
|
|
||||||
mRequest = mDavResource.getRequest();
|
mRequest = mDavResource.getRequest();
|
||||||
mResponse = mDavResource.getResponse();
|
return mDavResource.getResponse();
|
||||||
|
|
||||||
return mResponse.code();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getDepth() {
|
public int getDepth() {
|
||||||
|
@ -35,6 +35,7 @@ import com.owncloud.android.lib.common.utils.Log_OC;
|
|||||||
import org.apache.commons.httpclient.HttpStatus;
|
import org.apache.commons.httpclient.HttpStatus;
|
||||||
|
|
||||||
import okhttp3.HttpUrl;
|
import okhttp3.HttpUrl;
|
||||||
|
import okhttp3.Response;
|
||||||
|
|
||||||
import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK;
|
import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK;
|
||||||
|
|
||||||
@ -86,7 +87,7 @@ public class ExistenceCheckRemoteOperation extends RemoteOperation {
|
|||||||
HttpUrl.parse(client.getNewWebDavUri() + WebdavUtils.encodePath(mPath)),
|
HttpUrl.parse(client.getNewWebDavUri() + WebdavUtils.encodePath(mPath)),
|
||||||
0);
|
0);
|
||||||
|
|
||||||
int status = client.executeHttpMethod(propfindMethod);
|
Response response = client.executeHttpMethod(propfindMethod);
|
||||||
|
|
||||||
// if (previousFollowRedirects) {
|
// if (previousFollowRedirects) {
|
||||||
// mRedirectionPath = client.followRedirection(propfind);
|
// mRedirectionPath = client.followRedirection(propfind);
|
||||||
@ -101,21 +102,22 @@ public class ExistenceCheckRemoteOperation extends RemoteOperation {
|
|||||||
* 404 NOT FOUND: path doesn't exist,
|
* 404 NOT FOUND: path doesn't exist,
|
||||||
* 207 MULTI_STATUS: path exists.
|
* 207 MULTI_STATUS: path exists.
|
||||||
*/
|
*/
|
||||||
boolean isSuccess = ((status == HttpStatus.SC_OK || status == HttpStatus.SC_MULTI_STATUS) &&
|
boolean isSuccess = ((response.code() == HttpStatus.SC_OK
|
||||||
!mSuccessIfAbsent) ||
|
|| response.code() == HttpStatus.SC_MULTI_STATUS)
|
||||||
(status == HttpStatus.SC_MULTI_STATUS && !mSuccessIfAbsent) ||
|
&& !mSuccessIfAbsent)
|
||||||
(status == HttpStatus.SC_NOT_FOUND && mSuccessIfAbsent);
|
|| (response.code() == HttpStatus.SC_MULTI_STATUS && !mSuccessIfAbsent)
|
||||||
|
|| (response.code() == HttpStatus.SC_NOT_FOUND && mSuccessIfAbsent);
|
||||||
|
|
||||||
result = isSuccess
|
result = isSuccess
|
||||||
? new RemoteOperationResult(OK)
|
? new RemoteOperationResult(OK)
|
||||||
: new RemoteOperationResult(
|
: new RemoteOperationResult(
|
||||||
false, propfindMethod.getRequest(), propfindMethod.getResponse()
|
false, propfindMethod.getRequest(), response
|
||||||
);
|
);
|
||||||
|
|
||||||
Log_OC.d(TAG, "Existence check for " + client.getWebdavUri() +
|
Log_OC.d(TAG, "Existence check for " + client.getWebdavUri() +
|
||||||
WebdavUtils.encodePath(mPath) + " targeting for " +
|
WebdavUtils.encodePath(mPath) + " targeting for " +
|
||||||
(mSuccessIfAbsent ? " absence " : " existence ") +
|
(mSuccessIfAbsent ? " absence " : " existence ") +
|
||||||
"finished with HTTP status " + status + (!isSuccess?"(FAIL)":""));
|
"finished with HTTP status " + response.code() + (!isSuccess?"(FAIL)":""));
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
result = new RemoteOperationResult(e);
|
result = new RemoteOperationResult(e);
|
||||||
@ -130,7 +132,6 @@ public class ExistenceCheckRemoteOperation extends RemoteOperation {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the sequence of redirections followed during the execution of the operation.
|
* Gets the sequence of redirections followed during the execution of the operation.
|
||||||
*
|
*
|
||||||
@ -146,4 +147,4 @@ public class ExistenceCheckRemoteOperation extends RemoteOperation {
|
|||||||
public boolean wasRedirected() {
|
public boolean wasRedirected() {
|
||||||
return (mRedirectionPath != null && mRedirectionPath.getRedirectionsCount() > 0);
|
return (mRedirectionPath != null && mRedirectionPath.getRedirectionsCount() > 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -34,6 +34,7 @@ import com.owncloud.android.lib.common.utils.Log_OC;
|
|||||||
import com.owncloud.android.lib.common.http.nonwebdav.GetMethod;
|
import com.owncloud.android.lib.common.http.nonwebdav.GetMethod;
|
||||||
|
|
||||||
import okhttp3.Request;
|
import okhttp3.Request;
|
||||||
|
import okhttp3.Response;
|
||||||
|
|
||||||
import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK;
|
import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK;
|
||||||
|
|
||||||
@ -75,13 +76,12 @@ public class GetRemoteUserInfoOperation extends RemoteOperation {
|
|||||||
.build();
|
.build();
|
||||||
|
|
||||||
GetMethod getMethod = new GetMethod(client.getOkHttpClient(), request);
|
GetMethod getMethod = new GetMethod(client.getOkHttpClient(), request);
|
||||||
int status = client.executeHttpMethod(getMethod);
|
Response response = client.executeHttpMethod(getMethod);
|
||||||
String response = getMethod.getResponse().body().string();
|
|
||||||
|
|
||||||
if (isSuccess(status)) {
|
if (isSuccess(response.code())) {
|
||||||
Log_OC.d(TAG, "Successful response");
|
Log_OC.d(TAG, "Successful response");
|
||||||
|
|
||||||
JSONObject respJSON = new JSONObject(response);
|
JSONObject respJSON = new JSONObject(response.body().string());
|
||||||
JSONObject respOCS = respJSON.getJSONObject(NODE_OCS);
|
JSONObject respOCS = respJSON.getJSONObject(NODE_OCS);
|
||||||
JSONObject respData = respOCS.getJSONObject(NODE_DATA);
|
JSONObject respData = respOCS.getJSONObject(NODE_DATA);
|
||||||
|
|
||||||
@ -97,12 +97,12 @@ public class GetRemoteUserInfoOperation extends RemoteOperation {
|
|||||||
result.setData(data);
|
result.setData(data);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
result = new RemoteOperationResult(false, getMethod.getRequest(), getMethod.getResponse());
|
result = new RemoteOperationResult(false, getMethod.getRequest(), response);
|
||||||
Log_OC.e(TAG, "Failed response while getting user information ");
|
Log_OC.e(TAG, "Failed response while getting user information ");
|
||||||
if (response != null) {
|
if (response != null) {
|
||||||
Log_OC.e(TAG, "*** status code: " + status + " ; response message: " + response);
|
Log_OC.e(TAG, "*** status code: " + response.code() + " ; response message: " + response);
|
||||||
} else {
|
} else {
|
||||||
Log_OC.e(TAG, "*** status code: " + status);
|
Log_OC.e(TAG, "*** status code: " + response.code());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user