Merge branch 'saml_based_federated_single_sign_on' into saml_based_federated_single_s...
[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.utils.OwnCloudVersion;
28
29 import eu.alefzero.webdav.WebdavClient;
30 import android.content.Context;
31 import android.net.ConnectivityManager;
32 import android.net.Uri;
33
34 public class OwnCloudServerCheckOperation extends RemoteOperation {
35
36 /** Maximum time to wait for a response from the server when the connection is being tested, in MILLISECONDs. */
37 public static final int TRY_CONNECTION_TIMEOUT = 5000;
38
39 private static final String TAG = OwnCloudServerCheckOperation.class.getSimpleName();
40
41 private String mUrl;
42 private RemoteOperationResult mLatestResult;
43 private Context mContext;
44 private OwnCloudVersion mOCVersion;
45
46 public OwnCloudServerCheckOperation(String url, Context context) {
47 mUrl = url;
48 mContext = context;
49 mOCVersion = null;
50 }
51
52 public OwnCloudVersion getDiscoveredVersion() {
53 return mOCVersion;
54 }
55
56 private boolean tryConnection(WebdavClient wc, String urlSt) {
57 boolean retval = false;
58 GetMethod get = null;
59 try {
60 get = new GetMethod(urlSt);
61 int status = wc.executeMethod(get, TRY_CONNECTION_TIMEOUT, TRY_CONNECTION_TIMEOUT);
62 String response = get.getResponseBodyAsString();
63 if (status == HttpStatus.SC_OK) {
64 JSONObject json = new JSONObject(response);
65 if (!json.getBoolean("installed")) {
66 mLatestResult = new RemoteOperationResult(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED);
67 } else {
68 mOCVersion = new OwnCloudVersion(json.getString("version"));
69 if (!mOCVersion.isVersionValid()) {
70 mLatestResult = new RemoteOperationResult(RemoteOperationResult.ResultCode.BAD_OC_VERSION);
71
72 } else {
73 mLatestResult = new RemoteOperationResult(urlSt.startsWith("https://") ?
74 RemoteOperationResult.ResultCode.OK_SSL :
75 RemoteOperationResult.ResultCode.OK_NO_SSL
76 );
77
78 retval = true;
79 }
80 }
81
82 } else {
83 mLatestResult = new RemoteOperationResult(false, status, get.getResponseHeaders());
84 }
85
86 } catch (JSONException e) {
87 mLatestResult = new RemoteOperationResult(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED);
88
89 } catch (Exception e) {
90 mLatestResult = new RemoteOperationResult(e);
91
92 } finally {
93 if (get != null)
94 get.releaseConnection();
95 }
96
97 if (mLatestResult.isSuccess()) {
98 Log_OC.i(TAG, "Connection check at " + urlSt + ": " + mLatestResult.getLogMessage());
99
100 } else if (mLatestResult.getException() != null) {
101 Log_OC.e(TAG, "Connection check at " + urlSt + ": " + mLatestResult.getLogMessage(), mLatestResult.getException());
102
103 } else {
104 Log_OC.e(TAG, "Connection check at " + urlSt + ": " + mLatestResult.getLogMessage());
105 }
106
107 return retval;
108 }
109
110 private boolean isOnline() {
111 ConnectivityManager cm = (ConnectivityManager) mContext
112 .getSystemService(Context.CONNECTIVITY_SERVICE);
113 return cm != null && cm.getActiveNetworkInfo() != null
114 && cm.getActiveNetworkInfo().isConnectedOrConnecting();
115 }
116
117 @Override
118 protected RemoteOperationResult run(WebdavClient client) {
119 if (!isOnline()) {
120 return new RemoteOperationResult(RemoteOperationResult.ResultCode.NO_NETWORK_CONNECTION);
121 }
122 if (mUrl.startsWith("http://") || mUrl.startsWith("https://")) {
123 tryConnection(client, mUrl + AccountUtils.STATUS_PATH);
124
125 } else {
126 client.setBaseUri(Uri.parse("https://" + mUrl + AccountUtils.STATUS_PATH));
127 boolean httpsSuccess = tryConnection(client, "https://" + mUrl + AccountUtils.STATUS_PATH);
128 if (!httpsSuccess && !mLatestResult.isSslRecoverableException()) {
129 Log_OC.d(TAG, "establishing secure connection failed, trying non secure connection");
130 client.setBaseUri(Uri.parse("http://" + mUrl + AccountUtils.STATUS_PATH));
131 tryConnection(client, "http://" + mUrl + AccountUtils.STATUS_PATH);
132 }
133 }
134 return mLatestResult;
135 }
136
137 }