e43f06188da257a89ccc1aa6b419ab3975a80d7c
[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
83 return mOCVersionString.compareTo(shareServer) >= 0;
84 }
85
86 private boolean tryConnection(WebdavClient wc, String urlSt) {
87 boolean retval = false;
88 GetMethod get = null;
89 try {
90 get = new GetMethod(urlSt);
91 int status = wc.executeMethod(get, TRY_CONNECTION_TIMEOUT, TRY_CONNECTION_TIMEOUT);
92 String response = get.getResponseBodyAsString();
93 if (status == HttpStatus.SC_OK) {
94 JSONObject json = new JSONObject(response);
95 if (!json.getBoolean(NODE_INSTALLED)) {
96 mLatestResult = new RemoteOperationResult(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED);
97 } else {
98 mOCVersion = new OwnCloudVersion(json.getString(NODE_VERSION));
99 mOCVersionString = new OwnCloudVersion(json.getString(NODE_VERSIONSTRING), true);
100 if (!mOCVersion.isVersionValid()) {
101 mLatestResult = new RemoteOperationResult(RemoteOperationResult.ResultCode.BAD_OC_VERSION);
102
103 } else {
104 mLatestResult = new RemoteOperationResult(urlSt.startsWith("https://") ?
105 RemoteOperationResult.ResultCode.OK_SSL :
106 RemoteOperationResult.ResultCode.OK_NO_SSL
107 );
108
109 retval = true;
110 }
111 }
112
113 } else {
114 mLatestResult = new RemoteOperationResult(false, status, get.getResponseHeaders());
115 }
116
117 } catch (JSONException e) {
118 mLatestResult = new RemoteOperationResult(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED);
119
120 } catch (Exception e) {
121 mLatestResult = new RemoteOperationResult(e);
122
123 } finally {
124 if (get != null)
125 get.releaseConnection();
126 }
127
128 if (mLatestResult.isSuccess()) {
129 Log.i(TAG, "Connection check at " + urlSt + ": " + mLatestResult.getLogMessage());
130
131 } else if (mLatestResult.getException() != null) {
132 Log.e(TAG, "Connection check at " + urlSt + ": " + mLatestResult.getLogMessage(), mLatestResult.getException());
133
134 } else {
135 Log.e(TAG, "Connection check at " + urlSt + ": " + mLatestResult.getLogMessage());
136 }
137
138 return retval;
139 }
140
141 private boolean isOnline() {
142 ConnectivityManager cm = (ConnectivityManager) mContext
143 .getSystemService(Context.CONNECTIVITY_SERVICE);
144 return cm != null && cm.getActiveNetworkInfo() != null
145 && cm.getActiveNetworkInfo().isConnectedOrConnecting();
146 }
147
148 @Override
149 protected RemoteOperationResult run(WebdavClient client) {
150 if (!isOnline()) {
151 return new RemoteOperationResult(RemoteOperationResult.ResultCode.NO_NETWORK_CONNECTION);
152 }
153 if (mUrl.startsWith("http://") || mUrl.startsWith("https://")) {
154 tryConnection(client, mUrl + AccountUtils.STATUS_PATH);
155
156 } else {
157 client.setBaseUri(Uri.parse("https://" + mUrl + AccountUtils.STATUS_PATH));
158 boolean httpsSuccess = tryConnection(client, "https://" + mUrl + AccountUtils.STATUS_PATH);
159 if (!httpsSuccess && !mLatestResult.isSslRecoverableException()) {
160 Log.d(TAG, "establishing secure connection failed, trying non secure connection");
161 client.setBaseUri(Uri.parse("http://" + mUrl + AccountUtils.STATUS_PATH));
162 tryConnection(client, "http://" + mUrl + AccountUtils.STATUS_PATH);
163 }
164 }
165 return mLatestResult;
166 }
167
168 }