1 /* ownCloud Android Library is available under MIT license
2 * Copyright (C) 2014 ownCloud (http://www.owncloud.org/)
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
18 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 package com.owncloud.android.oc_framework.operations.remote;
27 import org.apache.commons.httpclient.HttpStatus;
28 import org.apache.commons.httpclient.methods.GetMethod;
29 import org.json.JSONException;
30 import org.json.JSONObject;
32 import com.owncloud.android.oc_framework.accounts.AccountUtils;
33 import com.owncloud.android.oc_framework.network.webdav.WebdavClient;
34 import com.owncloud.android.oc_framework.operations.RemoteOperation;
35 import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
36 import com.owncloud.android.oc_framework.utils.OwnCloudVersion;
38 import android.content.Context;
39 import android.net.ConnectivityManager;
40 import android.net.Uri;
41 import android.util.Log;
44 * Checks if the server is valid and if the server supports the Share API
46 * @author David A. Velasco
51 public class OwnCloudServerCheckOperation extends RemoteOperation {
53 /** Maximum time to wait for a response from the server when the connection is being tested, in MILLISECONDs. */
54 public static final int TRY_CONNECTION_TIMEOUT = 5000;
56 private static final String TAG = OwnCloudServerCheckOperation.class.getSimpleName();
58 private static final String OCVERSION_SHARED_SUPPORTED = "5.0.13";
60 private static final String NODE_INSTALLED = "installed";
61 private static final String NODE_VERSION = "version";
62 private static final String NODE_VERSIONSTRING = "versionstring";
65 private RemoteOperationResult mLatestResult;
66 private Context mContext;
67 private OwnCloudVersion mOCVersion;
68 private OwnCloudVersion mOCVersionString;
70 public OwnCloudServerCheckOperation(String url, Context context) {
74 mOCVersionString = null;
77 public OwnCloudVersion getDiscoveredVersion() {
80 public boolean isSharedSupported() {
81 OwnCloudVersion shareServer = new OwnCloudVersion(OCVERSION_SHARED_SUPPORTED);
83 return mOCVersionString.compareTo(shareServer) >= 0;
86 private boolean tryConnection(WebdavClient wc, String urlSt) {
87 boolean retval = false;
90 get = new GetMethod(urlSt);
91 int status = wc.executeMethod(get, TRY_CONNECTION_TIMEOUT, TRY_CONNECTION_TIMEOUT);
92 String response = get.getResponseBodyAsString();
93 if (status == HttpStatus.SC_OK) {
94 JSONObject json = new JSONObject(response);
95 if (!json.getBoolean(NODE_INSTALLED)) {
96 mLatestResult = new RemoteOperationResult(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED);
98 mOCVersion = new OwnCloudVersion(json.getString(NODE_VERSION));
99 mOCVersionString = new OwnCloudVersion(json.getString(NODE_VERSIONSTRING), true);
100 if (!mOCVersion.isVersionValid()) {
101 mLatestResult = new RemoteOperationResult(RemoteOperationResult.ResultCode.BAD_OC_VERSION);
104 mLatestResult = new RemoteOperationResult(urlSt.startsWith("https://") ?
105 RemoteOperationResult.ResultCode.OK_SSL :
106 RemoteOperationResult.ResultCode.OK_NO_SSL
114 mLatestResult = new RemoteOperationResult(false, status, get.getResponseHeaders());
117 } catch (JSONException e) {
118 mLatestResult = new RemoteOperationResult(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED);
120 } catch (Exception e) {
121 mLatestResult = new RemoteOperationResult(e);
125 get.releaseConnection();
128 if (mLatestResult.isSuccess()) {
129 Log.i(TAG, "Connection check at " + urlSt + ": " + mLatestResult.getLogMessage());
131 } else if (mLatestResult.getException() != null) {
132 Log.e(TAG, "Connection check at " + urlSt + ": " + mLatestResult.getLogMessage(), mLatestResult.getException());
135 Log.e(TAG, "Connection check at " + urlSt + ": " + mLatestResult.getLogMessage());
141 private boolean isOnline() {
142 ConnectivityManager cm = (ConnectivityManager) mContext
143 .getSystemService(Context.CONNECTIVITY_SERVICE);
144 return cm != null && cm.getActiveNetworkInfo() != null
145 && cm.getActiveNetworkInfo().isConnectedOrConnecting();
149 protected RemoteOperationResult run(WebdavClient client) {
151 return new RemoteOperationResult(RemoteOperationResult.ResultCode.NO_NETWORK_CONNECTION);
153 if (mUrl.startsWith("http://") || mUrl.startsWith("https://")) {
154 tryConnection(client, mUrl + AccountUtils.STATUS_PATH);
157 client.setBaseUri(Uri.parse("https://" + mUrl + AccountUtils.STATUS_PATH));
158 boolean httpsSuccess = tryConnection(client, "https://" + mUrl + AccountUtils.STATUS_PATH);
159 if (!httpsSuccess && !mLatestResult.isSslRecoverableException()) {
160 Log.d(TAG, "establishing secure connection failed, trying non secure connection");
161 client.setBaseUri(Uri.parse("http://" + mUrl + AccountUtils.STATUS_PATH));
162 tryConnection(client, "http://" + mUrl + AccountUtils.STATUS_PATH);
165 return mLatestResult;