1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
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.
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.
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/>.
18 package com
.owncloud
.android
.operations
;
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
;
25 import com
.owncloud
.android
.Log_OC
;
26 import com
.owncloud
.android
.authentication
.AccountUtils
;
27 import com
.owncloud
.android
.utils
.OwnCloudVersion
;
29 import eu
.alefzero
.webdav
.WebdavClient
;
30 import android
.content
.Context
;
31 import android
.net
.ConnectivityManager
;
32 import android
.net
.Uri
;
34 public class OwnCloudServerCheckOperation
extends RemoteOperation
{
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;
39 private static final String TAG
= OwnCloudServerCheckOperation
.class.getSimpleName();
42 private RemoteOperationResult mLatestResult
;
43 private Context mContext
;
44 private OwnCloudVersion mOCVersion
;
46 public OwnCloudServerCheckOperation(String url
, Context context
) {
52 public OwnCloudVersion
getDiscoveredVersion() {
56 private boolean tryConnection(WebdavClient wc
, String urlSt
) {
57 boolean retval
= false
;
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
);
68 mOCVersion
= new OwnCloudVersion(json
.getString("version"));
69 if (!mOCVersion
.isVersionValid()) {
70 mLatestResult
= new RemoteOperationResult(RemoteOperationResult
.ResultCode
.BAD_OC_VERSION
);
73 mLatestResult
= new RemoteOperationResult(urlSt
.startsWith("https://") ?
74 RemoteOperationResult
.ResultCode
.OK_SSL
:
75 RemoteOperationResult
.ResultCode
.OK_NO_SSL
83 mLatestResult
= new RemoteOperationResult(false
, status
, get
.getResponseHeaders());
86 } catch (JSONException e
) {
87 mLatestResult
= new RemoteOperationResult(RemoteOperationResult
.ResultCode
.INSTANCE_NOT_CONFIGURED
);
89 } catch (Exception e
) {
90 mLatestResult
= new RemoteOperationResult(e
);
94 get
.releaseConnection();
97 if (mLatestResult
.isSuccess()) {
98 Log_OC
.i(TAG
, "Connection check at " + urlSt
+ ": " + mLatestResult
.getLogMessage());
100 } else if (mLatestResult
.getException() != null
) {
101 Log_OC
.e(TAG
, "Connection check at " + urlSt
+ ": " + mLatestResult
.getLogMessage(), mLatestResult
.getException());
104 Log_OC
.e(TAG
, "Connection check at " + urlSt
+ ": " + mLatestResult
.getLogMessage());
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();
118 protected RemoteOperationResult
run(WebdavClient client
) {
120 return new RemoteOperationResult(RemoteOperationResult
.ResultCode
.NO_NETWORK_CONNECTION
);
122 if (mUrl
.startsWith("http://") || mUrl
.startsWith("https://")) {
123 tryConnection(client
, mUrl
+ AccountUtils
.STATUS_PATH
);
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
);
134 return mLatestResult
;