1
0
mirror of https://github.com/owncloud/android-library.git synced 2025-08-28 03:01:49 +00:00

Implement GetRemoteStatusOperation using new wrapper

This commit is contained in:
davigonz 2018-06-06 16:55:12 +02:00
parent 01d4592de0
commit a730980c34

View File

@ -1,5 +1,5 @@
/* ownCloud Android Library is available under MIT license /* 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 * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
@ -27,7 +27,9 @@ package com.owncloud.android.lib.resources.status;
import java.util.ArrayList; import java.util.ArrayList;
import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import com.owncloud.android.lib.common.http.HttpConstants;
import com.owncloud.android.lib.common.http.nonwebdav.GetMethod;
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 org.json.JSONException; import org.json.JSONException;
@ -44,11 +46,17 @@ 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 okhttp3.Request;
import okhttp3.Response;
import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK;
/** /**
* Checks if the server is valid and if the server supports the Share API * Checks if the server is valid and if the server supports the Share API
* *
* @author David A. Velasco * @author David A. Velasco
* @author masensio * @author masensio
* @author David González Verdugo
*/ */
public class GetRemoteStatusOperation extends RemoteOperation { public class GetRemoteStatusOperation extends RemoteOperation {
@ -75,47 +83,52 @@ public class GetRemoteStatusOperation extends RemoteOperation {
private boolean tryConnection(OwnCloudClient client) { private boolean tryConnection(OwnCloudClient client) {
boolean retval = false; boolean retval = false;
GetMethod get = null;
String baseUrlSt = client.getBaseUri().toString(); String baseUrlSt = client.getBaseUri().toString();
try { try {
get = new GetMethod(baseUrlSt + OwnCloudClient.STATUS_PATH);
HttpParams params = get.getParams().getDefaultParams(); String url = baseUrlSt + OwnCloudClient.STATUS_PATH;
params.setParameter(HttpMethodParams.USER_AGENT,
OwnCloudClientManagerFactory.getUserAgent()); final Request request = new Request.Builder()
get.getParams().setDefaults(params); .url(url)
.build();
client.setFollowRedirects(false); client.setFollowRedirects(false);
GetMethod getMethod = new GetMethod(client.getOkHttpClient(), request);
Response response = client.executeHttpMethod(getMethod);
mLatestResult = new RemoteOperationResult(OK);
boolean isRedirectToNonSecureConnection = false; boolean isRedirectToNonSecureConnection = false;
int status = client.executeMethod(get, TRY_CONNECTION_TIMEOUT, TRY_CONNECTION_TIMEOUT);
mLatestResult = new RemoteOperationResult((status == HttpStatus.SC_OK), get);
String redirectedLocation = mLatestResult.getRedirectedLocation(); // TODO Check this, although OkHttp should take care of the redirections
while (redirectedLocation != null && redirectedLocation.length() > 0 // boolean isRedirectToNonSecureConnection = false;
&& !mLatestResult.isSuccess()) { // String redirectedLocation = mLatestResult.getRedirectedLocation();
// while (redirectedLocation != null && redirectedLocation.length() > 0
// && !mLatestResult.isSuccess()) {
//
// isRedirectToNonSecureConnection |= (
// baseUrlSt.startsWith(HTTPS_PREFIX) &&
// redirectedLocation.startsWith(HTTP_PREFIX)
// );
// get.releaseConnection();
// get = new GetMethod(redirectedLocation);
// status = client.executeMethod(get, TRY_CONNECTION_TIMEOUT, TRY_CONNECTION_TIMEOUT);
// mLatestResult = new RemoteOperationResult(
// (status == HttpStatus.SC_OK),
// get
// );
// redirectedLocation = mLatestResult.getRedirectedLocation();
// }
isRedirectToNonSecureConnection |= ( if (response.code() == HttpStatus.SC_OK) {
baseUrlSt.startsWith(HTTPS_PREFIX) &&
redirectedLocation.startsWith(HTTP_PREFIX)
);
get.releaseConnection();
get = new GetMethod(redirectedLocation);
status = client.executeMethod(get, TRY_CONNECTION_TIMEOUT, TRY_CONNECTION_TIMEOUT);
mLatestResult = new RemoteOperationResult(
(status == HttpStatus.SC_OK),
get
);
redirectedLocation = mLatestResult.getRedirectedLocation();
}
String response = get.getResponseBodyAsString(); JSONObject respJSON = new JSONObject(response.body().string());
if (status == HttpStatus.SC_OK) { if (!respJSON.getBoolean(NODE_INSTALLED)) {
JSONObject json = new JSONObject(response);
if (!json.getBoolean(NODE_INSTALLED)) {
mLatestResult = new RemoteOperationResult( mLatestResult = new RemoteOperationResult(
RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED); RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED);
} else { } else {
String version = json.getString(NODE_VERSION); String version = respJSON.getString(NODE_VERSION);
OwnCloudVersion ocVersion = new OwnCloudVersion(version); OwnCloudVersion ocVersion = new OwnCloudVersion(version);
/// the version object will be returned even if the version is invalid, no error code; /// the version object will be returned even if the version is invalid, no error code;
/// every app will decide how to act if (ocVersion.isVersionValid() == false) /// every app will decide how to act if (ocVersion.isVersionValid() == false)
@ -133,14 +146,14 @@ public class GetRemoteStatusOperation extends RemoteOperation {
); );
} }
ArrayList<Object> data = new ArrayList<Object>(); ArrayList<Object> data = new ArrayList<>();
data.add(ocVersion); data.add(ocVersion);
mLatestResult.setData(data); mLatestResult.setData(data);
retval = true; retval = true;
} }
} else { } else {
mLatestResult = new RemoteOperationResult(false, get); mLatestResult = new RemoteOperationResult(false, getMethod.getRequest(), response);
} }
} catch (JSONException e) { } catch (JSONException e) {
@ -149,10 +162,6 @@ public class GetRemoteStatusOperation extends RemoteOperation {
} catch (Exception e) { } catch (Exception e) {
mLatestResult = new RemoteOperationResult(e); mLatestResult = new RemoteOperationResult(e);
} finally {
if (get != null)
get.releaseConnection();
} }
if (mLatestResult.isSuccess()) { if (mLatestResult.isSuccess()) {
@ -196,5 +205,4 @@ public class GetRemoteStatusOperation extends RemoteOperation {
} }
return mLatestResult; return mLatestResult;
} }
} }