1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
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.
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.
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/>.
19 package com
.owncloud
.android
.authenticator
;
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
;
26 import com
.owncloud
.android
.AccountUtils
;
27 import com
.owncloud
.android
.network
.SslAnalyzer
;
28 import com
.owncloud
.android
.operations
.RemoteOperation
;
29 import com
.owncloud
.android
.operations
.RemoteOperationResult
;
30 import com
.owncloud
.android
.utils
.OwnCloudVersion
;
32 import eu
.alefzero
.webdav
.WebdavClient
;
33 import android
.content
.Context
;
34 import android
.net
.ConnectivityManager
;
35 import android
.net
.Uri
;
36 import android
.util
.Log
;
38 public class ConnectionCheckOperation
extends RemoteOperation
{
40 /** Maximum time to wait for a response from the server when the connection is being tested, in MILLISECONDs. */
41 public static final int TRY_CONNECTION_TIMEOUT
= 5000;
43 private static final String TAG
= ConnectionCheckerRunnable
.class.getCanonicalName();
46 private RemoteOperationResult mLatestResult
;
47 private Context mContext
;
48 private OwnCloudVersion mOCVersion
;
50 public ConnectionCheckOperation(String url
, Context context
) {
56 public OwnCloudVersion
getDiscoveredVersion() {
60 private boolean tryConnection(WebdavClient wc
, String urlSt
) {
61 boolean retval
= false
;
64 get
= new GetMethod(urlSt
);
65 int status
= wc
.executeMethod(get
, TRY_CONNECTION_TIMEOUT
, TRY_CONNECTION_TIMEOUT
);
66 String response
= get
.getResponseBodyAsString();
67 if (status
== HttpStatus
.SC_OK
) {
68 JSONObject json
= new JSONObject(response
);
69 if (!json
.getBoolean("installed")) {
70 mLatestResult
= new RemoteOperationResult(RemoteOperationResult
.ResultCode
.INSTANCE_NOT_CONFIGURED
);
72 mOCVersion
= new OwnCloudVersion(json
.getString("version"));
73 if (!mOCVersion
.isVersionValid()) {
74 mLatestResult
= new RemoteOperationResult(RemoteOperationResult
.ResultCode
.BAD_OC_VERSION
);
82 mLatestResult
= new RemoteOperationResult(false
, status
);
85 } catch (JSONException e
) {
86 mLatestResult
= new RemoteOperationResult(RemoteOperationResult
.ResultCode
.INSTANCE_NOT_CONFIGURED
);
87 //Log.e(TAG, "JSON exception while trying connection (instance not configured) ", e);
89 } catch (Exception e
) {
90 mLatestResult
= new RemoteOperationResult(e
);
91 //Log.e(TAG, "Unexpected exception while trying connection", e);
95 get
.releaseConnection();
101 private boolean isOnline() {
102 ConnectivityManager cm
= (ConnectivityManager
) mContext
103 .getSystemService(Context
.CONNECTIVITY_SERVICE
);
104 return cm
!= null
&& cm
.getActiveNetworkInfo() != null
105 && cm
.getActiveNetworkInfo().isConnectedOrConnecting();
109 protected RemoteOperationResult
run(WebdavClient client
) {
111 return new RemoteOperationResult(RemoteOperationResult
.ResultCode
.NO_NETWORK_CONNECTION
);
113 if (mUrl
.startsWith("http://") || mUrl
.startsWith("https://")) {
114 mLatestResult
= new RemoteOperationResult(
115 mUrl
.startsWith("https://") ? RemoteOperationResult
.ResultCode
.OK_SSL
: RemoteOperationResult
.ResultCode
.OK_NO_SSL
117 tryConnection(client
, mUrl
+ AccountUtils
.STATUS_PATH
);
118 return mLatestResult
;
121 client
.setBaseUri(Uri
.parse("https://" + mUrl
+ AccountUtils
.STATUS_PATH
));
122 if (tryConnection(client
, "https://" + mUrl
+ AccountUtils
.STATUS_PATH
)) {
123 return new RemoteOperationResult(RemoteOperationResult
.ResultCode
.OK_SSL
);
125 } else if (!SslAnalyzer
.isRecoverable(mLatestResult
)) {
127 Log
.d(TAG
, "establishing secure connection failed, trying non secure connection");
128 client
.setBaseUri(Uri
.parse("http://" + mUrl
+ AccountUtils
.STATUS_PATH
));
129 if (tryConnection(client
, "http://" + mUrl
+ AccountUtils
.STATUS_PATH
)) {
130 return new RemoteOperationResult(RemoteOperationResult
.ResultCode
.OK_NO_SSL
);
133 return mLatestResult
;