Merge branch 'develop' into refactor_remote_operation_to_create_folder
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / OwnCloudServerCheckOperation.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 */
17
18 package com.owncloud.android.operations;
19
20 import org.apache.commons.httpclient.HttpStatus;
21 import org.apache.commons.httpclient.methods.GetMethod;
22 import org.json.JSONException;
23 import org.json.JSONObject;
24
25 import com.owncloud.android.Log_OC;
26 import com.owncloud.android.authentication.AccountUtils;
27 import com.owncloud.android.oc_framework.network.webdav.WebdavClient;
28 import com.owncloud.android.oc_framework.operations.RemoteOperation;
29 import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
30 import com.owncloud.android.oc_framework.utils.OwnCloudVersion;
31
32 import android.content.Context;
33 import android.net.ConnectivityManager;
34 import android.net.Uri;
35
36 public class OwnCloudServerCheckOperation extends RemoteOperation {
37
38 /** Maximum time to wait for a response from the server when the connection is being tested, in MILLISECONDs. */
39 public static final int TRY_CONNECTION_TIMEOUT = 5000;
40
41 private static final String TAG = OwnCloudServerCheckOperation.class.getSimpleName();
42
43 private String mUrl;
44 private RemoteOperationResult mLatestResult;
45 private Context mContext;
46 private OwnCloudVersion mOCVersion;
47
48 public OwnCloudServerCheckOperation(String url, Context context) {
49 mUrl = url;
50 mContext = context;
51 mOCVersion = null;
52 }
53
54 public OwnCloudVersion getDiscoveredVersion() {
55 return mOCVersion;
56 }
57
58 private boolean tryConnection(WebdavClient wc, String urlSt) {
59 boolean retval = false;
60 GetMethod get = null;
61 try {
62 get = new GetMethod(urlSt);
63 int status = wc.executeMethod(get, TRY_CONNECTION_TIMEOUT, TRY_CONNECTION_TIMEOUT);
64 String response = get.getResponseBodyAsString();
65 if (status == HttpStatus.SC_OK) {
66 JSONObject json = new JSONObject(response);
67 if (!json.getBoolean("installed")) {
68 mLatestResult = new RemoteOperationResult(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED);
69 } else {
70 mOCVersion = new OwnCloudVersion(json.getString("version"));
71 if (!mOCVersion.isVersionValid()) {
72 mLatestResult = new RemoteOperationResult(RemoteOperationResult.ResultCode.BAD_OC_VERSION);
73
74 } else {
75 mLatestResult = new RemoteOperationResult(urlSt.startsWith("https://") ?
76 RemoteOperationResult.ResultCode.OK_SSL :
77 RemoteOperationResult.ResultCode.OK_NO_SSL
78 );
79
80 retval = true;
81 }
82 }
83
84 } else {
85 mLatestResult = new RemoteOperationResult(false, status, get.getResponseHeaders());
86 }
87
88 } catch (JSONException e) {
89 mLatestResult = new RemoteOperationResult(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED);
90
91 } catch (Exception e) {
92 mLatestResult = new RemoteOperationResult(e);
93
94 } finally {
95 if (get != null)
96 get.releaseConnection();
97 }
98
99 if (mLatestResult.isSuccess()) {
100 Log_OC.i(TAG, "Connection check at " + urlSt + ": " + mLatestResult.getLogMessage());
101
102 } else if (mLatestResult.getException() != null) {
103 Log_OC.e(TAG, "Connection check at " + urlSt + ": " + mLatestResult.getLogMessage(), mLatestResult.getException());
104
105 } else {
106 Log_OC.e(TAG, "Connection check at " + urlSt + ": " + mLatestResult.getLogMessage());
107 }
108
109 return retval;
110 }
111
112 private boolean isOnline() {
113 ConnectivityManager cm = (ConnectivityManager) mContext
114 .getSystemService(Context.CONNECTIVITY_SERVICE);
115 return cm != null && cm.getActiveNetworkInfo() != null
116 && cm.getActiveNetworkInfo().isConnectedOrConnecting();
117 }
118
119 @Override
120 protected RemoteOperationResult run(WebdavClient client) {
121 if (!isOnline()) {
122 return new RemoteOperationResult(RemoteOperationResult.ResultCode.NO_NETWORK_CONNECTION);
123 }
124 if (mUrl.startsWith("http://") || mUrl.startsWith("https://")) {
125 tryConnection(client, mUrl + AccountUtils.STATUS_PATH);
126
127 } else {
128 client.setBaseUri(Uri.parse("https://" + mUrl + AccountUtils.STATUS_PATH));
129 boolean httpsSuccess = tryConnection(client, "https://" + mUrl + AccountUtils.STATUS_PATH);
130 if (!httpsSuccess && !mLatestResult.isSslRecoverableException()) {
131 Log_OC.d(TAG, "establishing secure connection failed, trying non secure connection");
132 client.setBaseUri(Uri.parse("http://" + mUrl + AccountUtils.STATUS_PATH));
133 tryConnection(client, "http://" + mUrl + AccountUtils.STATUS_PATH);
134 }
135 }
136 return mLatestResult;
137 }
138
139 }