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