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
.oc_framework
.network
.webdav
.WebdavClient
;
28 import com
.owncloud
.android
.oc_framework
.operations
.RemoteOperation
;
29 import com
.owncloud
.android
.oc_framework
.operations
.RemoteOperationResult
;
30 import com
.owncloud
.android
.oc_framework
.utils
.OwnCloudVersion
;
32 import android
.content
.Context
;
33 import android
.net
.ConnectivityManager
;
34 import android
.net
.Uri
;
36 public class OwnCloudServerCheckOperation
extends RemoteOperation
{
38 /** Maximum time to wait for a response from the server when the connection is being tested, in MILLISECONDs. */
39 public static final int TRY_CONNECTION_TIMEOUT
= 5000;
41 private static final String TAG
= OwnCloudServerCheckOperation
.class.getSimpleName();
44 private RemoteOperationResult mLatestResult
;
45 private Context mContext
;
46 private OwnCloudVersion mOCVersion
;
48 public OwnCloudServerCheckOperation(String url
, Context context
) {
54 public OwnCloudVersion
getDiscoveredVersion() {
58 private boolean tryConnection(WebdavClient wc
, String urlSt
) {
59 boolean retval
= false
;
62 get
= new GetMethod(urlSt
);
63 int status
= wc
.executeMethod(get
, TRY_CONNECTION_TIMEOUT
, TRY_CONNECTION_TIMEOUT
);
64 String response
= get
.getResponseBodyAsString();
65 if (status
== HttpStatus
.SC_OK
) {
66 JSONObject json
= new JSONObject(response
);
67 if (!json
.getBoolean("installed")) {
68 mLatestResult
= new RemoteOperationResult(RemoteOperationResult
.ResultCode
.INSTANCE_NOT_CONFIGURED
);
70 mOCVersion
= new OwnCloudVersion(json
.getString("version"));
71 if (!mOCVersion
.isVersionValid()) {
72 mLatestResult
= new RemoteOperationResult(RemoteOperationResult
.ResultCode
.BAD_OC_VERSION
);
75 mLatestResult
= new RemoteOperationResult(urlSt
.startsWith("https://") ?
76 RemoteOperationResult
.ResultCode
.OK_SSL
:
77 RemoteOperationResult
.ResultCode
.OK_NO_SSL
85 mLatestResult
= new RemoteOperationResult(false
, status
, get
.getResponseHeaders());
88 } catch (JSONException e
) {
89 mLatestResult
= new RemoteOperationResult(RemoteOperationResult
.ResultCode
.INSTANCE_NOT_CONFIGURED
);
91 } catch (Exception e
) {
92 mLatestResult
= new RemoteOperationResult(e
);
96 get
.releaseConnection();
99 if (mLatestResult
.isSuccess()) {
100 Log_OC
.i(TAG
, "Connection check at " + urlSt
+ ": " + mLatestResult
.getLogMessage());
102 } else if (mLatestResult
.getException() != null
) {
103 Log_OC
.e(TAG
, "Connection check at " + urlSt
+ ": " + mLatestResult
.getLogMessage(), mLatestResult
.getException());
106 Log_OC
.e(TAG
, "Connection check at " + urlSt
+ ": " + mLatestResult
.getLogMessage());
112 private boolean isOnline() {
113 ConnectivityManager cm
= (ConnectivityManager
) mContext
114 .getSystemService(Context
.CONNECTIVITY_SERVICE
);
115 return cm
!= null
&& cm
.getActiveNetworkInfo() != null
116 && cm
.getActiveNetworkInfo().isConnectedOrConnecting();
120 protected RemoteOperationResult
run(WebdavClient client
) {
122 return new RemoteOperationResult(RemoteOperationResult
.ResultCode
.NO_NETWORK_CONNECTION
);
124 if (mUrl
.startsWith("http://") || mUrl
.startsWith("https://")) {
125 tryConnection(client
, mUrl
+ AccountUtils
.STATUS_PATH
);
128 client
.setBaseUri(Uri
.parse("https://" + mUrl
+ AccountUtils
.STATUS_PATH
));
129 boolean httpsSuccess
= tryConnection(client
, "https://" + mUrl
+ AccountUtils
.STATUS_PATH
);
130 if (!httpsSuccess
&& !mLatestResult
.isSslRecoverableException()) {
131 Log_OC
.d(TAG
, "establishing secure connection failed, trying non secure connection");
132 client
.setBaseUri(Uri
.parse("http://" + mUrl
+ AccountUtils
.STATUS_PATH
));
133 tryConnection(client
, "http://" + mUrl
+ AccountUtils
.STATUS_PATH
);
136 return mLatestResult
;