9c5283c1bcd7b953e9ada076ccdb5f5f343f6b04
[pub/Android/ownCloud.git] /
1 /* ownCloud Android Library is available under MIT license
2 * Copyright (C) 2014 ownCloud (http://www.owncloud.org/)
3 *
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:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
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
21 * THE SOFTWARE.
22 *
23 */
24
25 package com.owncloud.android.oc_framework.operations.remote;
26
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;
31
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;
37
38 import android.content.Context;
39 import android.net.ConnectivityManager;
40 import android.net.Uri;
41 import android.util.Log;
42
43 /**
44 * Checks if the server is valid and if the server supports the Share API
45 *
46 * @author David A. Velasco
47 * @author masensio
48 *
49 */
50
51 public class OwnCloudServerCheckOperation extends RemoteOperation {
52
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;
55
56 private static final String TAG = OwnCloudServerCheckOperation.class.getSimpleName();
57
58 private static final String OCVERSION_SHARED_SUPPORTED = "5.0.13";
59
60 private static final String NODE_INSTALLED = "installed";
61 private static final String NODE_VERSION = "version";
62 private static final String NODE_VERSIONSTRING = "versionstring";
63
64 private String mUrl;
65 private RemoteOperationResult mLatestResult;
66 private Context mContext;
67 private OwnCloudVersion mOCVersion;
68 private OwnCloudVersion mOCVersionString;
69
70 public OwnCloudServerCheckOperation(String url, Context context) {
71 mUrl = url;
72 mContext = context;
73 mOCVersion = null;
74 mOCVersionString = null;
75 }
76
77 public OwnCloudVersion getDiscoveredVersion() {
78 return mOCVersion;
79 }
80 public boolean isSharedSupported() {
81 OwnCloudVersion shareServer = new OwnCloudVersion(OCVERSION_SHARED_SUPPORTED);
82 if (isOnline()) {
83 return mOCVersionString.compareTo(shareServer) >= 0;
84 } else {
85 return false;
86 }
87
88 }
89
90 private boolean tryConnection(WebdavClient wc, String urlSt) {
91 boolean retval = false;
92 GetMethod get = null;
93 try {
94 get = new GetMethod(urlSt);
95 int status = wc.executeMethod(get, TRY_CONNECTION_TIMEOUT, TRY_CONNECTION_TIMEOUT);
96 String response = get.getResponseBodyAsString();
97 if (status == HttpStatus.SC_OK) {
98 JSONObject json = new JSONObject(response);
99 if (!json.getBoolean(NODE_INSTALLED)) {
100 mLatestResult = new RemoteOperationResult(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED);
101 } else {
102 mOCVersion = new OwnCloudVersion(json.getString(NODE_VERSION));
103 mOCVersionString = new OwnCloudVersion(json.getString(NODE_VERSIONSTRING), true);
104 if (!mOCVersion.isVersionValid()) {
105 mLatestResult = new RemoteOperationResult(RemoteOperationResult.ResultCode.BAD_OC_VERSION);
106
107 } else {
108 mLatestResult = new RemoteOperationResult(urlSt.startsWith("https://") ?
109 RemoteOperationResult.ResultCode.OK_SSL :
110 RemoteOperationResult.ResultCode.OK_NO_SSL
111 );
112
113 retval = true;
114 }
115 }
116
117 } else {
118 mLatestResult = new RemoteOperationResult(false, status, get.getResponseHeaders());
119 }
120
121 } catch (JSONException e) {
122 mLatestResult = new RemoteOperationResult(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED);
123
124 } catch (Exception e) {
125 mLatestResult = new RemoteOperationResult(e);
126
127 } finally {
128 if (get != null)
129 get.releaseConnection();
130 }
131
132 if (mLatestResult.isSuccess()) {
133 Log.i(TAG, "Connection check at " + urlSt + ": " + mLatestResult.getLogMessage());
134
135 } else if (mLatestResult.getException() != null) {
136 Log.e(TAG, "Connection check at " + urlSt + ": " + mLatestResult.getLogMessage(), mLatestResult.getException());
137
138 } else {
139 Log.e(TAG, "Connection check at " + urlSt + ": " + mLatestResult.getLogMessage());
140 }
141
142 return retval;
143 }
144
145 private boolean isOnline() {
146 ConnectivityManager cm = (ConnectivityManager) mContext
147 .getSystemService(Context.CONNECTIVITY_SERVICE);
148 return cm != null && cm.getActiveNetworkInfo() != null
149 && cm.getActiveNetworkInfo().isConnectedOrConnecting();
150 }
151
152 @Override
153 protected RemoteOperationResult run(WebdavClient client) {
154 if (!isOnline()) {
155 return new RemoteOperationResult(RemoteOperationResult.ResultCode.NO_NETWORK_CONNECTION);
156 }
157 if (mUrl.startsWith("http://") || mUrl.startsWith("https://")) {
158 tryConnection(client, mUrl + AccountUtils.STATUS_PATH);
159
160 } else {
161 client.setBaseUri(Uri.parse("https://" + mUrl + AccountUtils.STATUS_PATH));
162 boolean httpsSuccess = tryConnection(client, "https://" + mUrl + AccountUtils.STATUS_PATH);
163 if (!httpsSuccess && !mLatestResult.isSslRecoverableException()) {
164 Log.d(TAG, "establishing secure connection failed, trying non secure connection");
165 client.setBaseUri(Uri.parse("http://" + mUrl + AccountUtils.STATUS_PATH));
166 tryConnection(client, "http://" + mUrl + AccountUtils.STATUS_PATH);
167 }
168 }
169 return mLatestResult;
170 }
171
172 }