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