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 java
.io
.IOException
;
22 import java
.net
.ConnectException
;
23 import java
.net
.MalformedURLException
;
24 import java
.net
.SocketException
;
25 import java
.net
.SocketTimeoutException
;
28 import java
.net
.UnknownHostException
;
30 import javax
.net
.ssl
.SSLException
;
31 import javax
.net
.ssl
.SSLHandshakeException
;
32 import javax
.net
.ssl
.SSLPeerUnverifiedException
;
34 import org
.apache
.commons
.httpclient
.HttpException
;
35 import org
.apache
.commons
.httpclient
.HttpStatus
;
36 import org
.apache
.commons
.httpclient
.methods
.GetMethod
;
37 import org
.json
.JSONException
;
38 import org
.json
.JSONObject
;
40 import com
.owncloud
.android
.AccountUtils
;
41 import com
.owncloud
.android
.authenticator
.OnConnectCheckListener
.ResultType
;
42 import com
.owncloud
.android
.utils
.OwnCloudVersion
;
44 import eu
.alefzero
.webdav
.WebdavClient
;
45 import android
.content
.Context
;
46 import android
.net
.ConnectivityManager
;
47 import android
.net
.Uri
;
48 import android
.os
.Handler
;
49 import android
.util
.Log
;
51 public class ConnectionCheckerRunnable
implements Runnable
{
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;
56 private static final String TAG
= "ConnectionCheckerRunnable";
57 private OnConnectCheckListener mListener
;
59 private Handler mHandler
;
60 private ResultType mLatestResult
;
61 private Context mContext
;
62 private OwnCloudVersion mOCVersion
;
64 public void setListener(OnConnectCheckListener listener
, Handler handler
) {
69 public ConnectionCheckerRunnable(String url
, Context context
) {
81 postResult(ResultType
.NO_NETWORK_CONNECTION
);
84 if (mUrl
.startsWith("http://") || mUrl
.startsWith("https://")) {
85 mLatestResult
= (mUrl
.startsWith("https://"))? ResultType
.OK_SSL
: ResultType
.OK_NO_SSL
;
86 tryConnection(mUrl
+ AccountUtils
.STATUS_PATH
);
87 postResult(mLatestResult
);
89 if (tryConnection("https://" + mUrl
+ AccountUtils
.STATUS_PATH
)) {
90 postResult(ResultType
.OK_SSL
);
94 "establishing secure connection failed, trying non secure connection");
96 if (tryConnection("http://" + mUrl
+ AccountUtils
.STATUS_PATH
)) {
97 postResult(ResultType
.OK_NO_SSL
);
100 postResult(mLatestResult
);
104 public OwnCloudVersion
getDiscoveredVersion() {
108 private boolean tryConnection(String urlSt
) {
109 boolean retval
= false
;
111 WebdavClient wc
= new WebdavClient();
112 wc
.allowSelfsignedCertificates();
113 URL url
= new URL(urlSt
); // better than android.net.Uri in this case; provides URL validation
114 GetMethod get
= new GetMethod(url
.toString());
115 int status
= wc
.executeMethod(get
, TRY_CONNECTION_TIMEOUT
);
117 case HttpStatus
.SC_OK
: {
118 String response
= get
.getResponseBodyAsString();
119 JSONObject json
= new JSONObject(response
);
120 if (!json
.getBoolean("installed")) {
121 mLatestResult
= ResultType
.INSTANCE_NOT_CONFIGURED
;
124 mOCVersion
= new OwnCloudVersion(json
.getString("version"));
125 if (!mOCVersion
.isVersionValid()) {
126 mLatestResult
= ResultType
.BAD_OC_VERSION
;
132 case HttpStatus
.SC_NOT_FOUND
:
133 mLatestResult
= ResultType
.FILE_NOT_FOUND
;
135 case HttpStatus
.SC_INTERNAL_SERVER_ERROR
:
136 mLatestResult
= ResultType
.INSTANCE_NOT_CONFIGURED
;
139 mLatestResult
= ResultType
.UNKNOWN_ERROR
;
140 Log
.e(TAG
, "Not handled status received from server: " + status
);
143 } catch (JSONException e
) {
144 mLatestResult
= ResultType
.INSTANCE_NOT_CONFIGURED
;
145 Log
.e(TAG
, "JSON exception while trying connection (instance not configured) ", e
);
147 } catch (SocketException e
) {
148 mLatestResult
= ResultType
.WRONG_CONNECTION
;
149 Log
.e(TAG
, "Socket exception while trying connection", e
);
151 } catch (SocketTimeoutException e
) {
152 mLatestResult
= ResultType
.TIMEOUT
;
153 Log
.e(TAG
, "Socket timeout exception while trying connection", e
);
155 } catch (MalformedURLException e
) {
156 mLatestResult
= ResultType
.INCORRECT_ADDRESS
;
157 Log
.e(TAG
, "Connect exception while trying connection", e
);
159 } catch (UnknownHostException e
) {
160 mLatestResult
= ResultType
.HOST_NOT_AVAILABLE
;
161 Log
.e(TAG
, "Unknown host exception while trying connection", e
);
163 } catch (SSLPeerUnverifiedException e
) { // specially meaningful SSLException
164 mLatestResult
= ResultType
.SSL_UNVERIFIED_SERVER
;
165 Log
.e(TAG
, "SSL Peer Unverified exception while trying connection", e
);
167 } catch (SSLException e
) {
168 mLatestResult
= ResultType
.SSL_INIT_ERROR
;
169 Log
.e(TAG
, "SSL exception while trying connection", e
);
171 } catch (HttpException e
) { // specific exceptions from org.apache.commons.httpclient
172 mLatestResult
= ResultType
.UNKNOWN_ERROR
;
173 Log
.e(TAG
, "HTTP exception while trying connection", e
);
175 } catch (IOException e
) { // UnkownsServiceException, and any other weird I/O Exception that could occur
176 mLatestResult
= ResultType
.UNKNOWN_ERROR
;
177 Log
.e(TAG
, "I/O exception while trying connection", e
);
179 } catch (Exception e
) {
180 mLatestResult
= ResultType
.UNKNOWN_ERROR
;
181 Log
.e(TAG
, "Unexpected exception while trying connection", e
);
187 private boolean isOnline() {
188 ConnectivityManager cm
= (ConnectivityManager
) mContext
189 .getSystemService(Context
.CONNECTIVITY_SERVICE
);
190 return cm
!= null
&& cm
.getActiveNetworkInfo() != null
191 && cm
.getActiveNetworkInfo().isConnectedOrConnecting();
194 private void postResult(final ResultType result
) {
195 if (mHandler
!= null
&& mListener
!= null
) {
196 mHandler
.post(new Runnable() {
199 mListener
.onConnectionCheckResult(result
);