ConnectionCheckerRunnable.java 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /* ownCloud Android client application
  2. * Copyright (C) 2012 Bartek Przybylski
  3. *
  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.
  8. *
  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.
  13. *
  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/>.
  16. *
  17. */
  18. package com.owncloud.android.authenticator;
  19. import java.io.IOException;
  20. import java.net.MalformedURLException;
  21. import java.net.SocketException;
  22. import java.net.SocketTimeoutException;
  23. import java.net.UnknownHostException;
  24. import javax.net.ssl.SSLException;
  25. import javax.net.ssl.SSLPeerUnverifiedException;
  26. import org.apache.commons.httpclient.ConnectTimeoutException;
  27. import org.apache.commons.httpclient.HttpException;
  28. import org.apache.commons.httpclient.HttpStatus;
  29. import org.apache.commons.httpclient.methods.GetMethod;
  30. import org.json.JSONException;
  31. import org.json.JSONObject;
  32. import com.owncloud.android.AccountUtils;
  33. import com.owncloud.android.authenticator.OnConnectCheckListener.ResultType;
  34. import com.owncloud.android.utils.OwnCloudClientUtils;
  35. import com.owncloud.android.utils.OwnCloudVersion;
  36. import eu.alefzero.webdav.WebdavClient;
  37. import android.content.Context;
  38. import android.net.ConnectivityManager;
  39. import android.net.Uri;
  40. import android.os.Handler;
  41. import android.util.Log;
  42. public class ConnectionCheckerRunnable implements Runnable {
  43. /** Maximum time to wait for a response from the server when the connection is being tested, in MILLISECONDs. */
  44. public static final int TRY_CONNECTION_TIMEOUT = 5000;
  45. private static final String TAG = "ConnectionCheckerRunnable";
  46. private OnConnectCheckListener mListener;
  47. private String mUrl;
  48. private Handler mHandler;
  49. private ResultType mLatestResult;
  50. private Context mContext;
  51. private OwnCloudVersion mOCVersion;
  52. public void setListener(OnConnectCheckListener listener, Handler handler) {
  53. mListener = listener;
  54. mHandler = handler;
  55. }
  56. public ConnectionCheckerRunnable(String url, Context context) {
  57. mListener = null;
  58. mHandler = null;
  59. mUrl = url;
  60. mContext = context;
  61. mOCVersion = null;
  62. }
  63. @Override
  64. public void run() {
  65. if (!isOnline()) {
  66. postResult(ResultType.NO_NETWORK_CONNECTION);
  67. return;
  68. }
  69. if (mUrl.startsWith("http://") || mUrl.startsWith("https://")) {
  70. mLatestResult = (mUrl.startsWith("https://"))? ResultType.OK_SSL : ResultType.OK_NO_SSL;
  71. tryConnection(mUrl + AccountUtils.STATUS_PATH);
  72. postResult(mLatestResult);
  73. } else {
  74. if (tryConnection("https://" + mUrl + AccountUtils.STATUS_PATH)) {
  75. postResult(ResultType.OK_SSL);
  76. return;
  77. }
  78. Log.d(TAG,
  79. "establishing secure connection failed, trying non secure connection");
  80. if (tryConnection("http://" + mUrl + AccountUtils.STATUS_PATH)) {
  81. postResult(ResultType.OK_NO_SSL);
  82. return;
  83. }
  84. postResult(mLatestResult);
  85. }
  86. }
  87. public OwnCloudVersion getDiscoveredVersion() {
  88. return mOCVersion;
  89. }
  90. private boolean tryConnection(String urlSt) {
  91. boolean retval = false;
  92. GetMethod get = null;
  93. try {
  94. WebdavClient wc = OwnCloudClientUtils.createOwnCloudClient(Uri.parse(urlSt));
  95. get = new GetMethod(urlSt);
  96. int status = wc.executeMethod(get, TRY_CONNECTION_TIMEOUT, TRY_CONNECTION_TIMEOUT);
  97. String response = get.getResponseBodyAsString();
  98. switch (status) {
  99. case HttpStatus.SC_OK: {
  100. JSONObject json = new JSONObject(response);
  101. if (!json.getBoolean("installed")) {
  102. mLatestResult = ResultType.INSTANCE_NOT_CONFIGURED;
  103. break;
  104. }
  105. mOCVersion = new OwnCloudVersion(json.getString("version"));
  106. if (!mOCVersion.isVersionValid()) {
  107. mLatestResult = ResultType.BAD_OC_VERSION;
  108. break;
  109. }
  110. retval = true;
  111. break;
  112. }
  113. case HttpStatus.SC_NOT_FOUND:
  114. mLatestResult = ResultType.FILE_NOT_FOUND;
  115. break;
  116. case HttpStatus.SC_INTERNAL_SERVER_ERROR:
  117. mLatestResult = ResultType.INSTANCE_NOT_CONFIGURED;
  118. break;
  119. default:
  120. mLatestResult = ResultType.UNKNOWN_ERROR;
  121. Log.e(TAG, "Not handled status received from server: " + status);
  122. }
  123. } catch (JSONException e) {
  124. mLatestResult = ResultType.INSTANCE_NOT_CONFIGURED;
  125. Log.e(TAG, "JSON exception while trying connection (instance not configured) ", e);
  126. } catch (SocketException e) {
  127. mLatestResult = ResultType.WRONG_CONNECTION;
  128. Log.e(TAG, "Socket exception while trying connection", e);
  129. } catch (SocketTimeoutException e) {
  130. mLatestResult = ResultType.TIMEOUT;
  131. Log.e(TAG, "Socket timeout exception while trying connection", e);
  132. } catch (MalformedURLException e) {
  133. mLatestResult = ResultType.INCORRECT_ADDRESS;
  134. Log.e(TAG, "Connect exception while trying connection", e);
  135. } catch (UnknownHostException e) {
  136. mLatestResult = ResultType.HOST_NOT_AVAILABLE;
  137. Log.e(TAG, "Unknown host exception while trying connection", e);
  138. } catch (SSLPeerUnverifiedException e) { // specially meaningful SSLException
  139. mLatestResult = ResultType.SSL_UNVERIFIED_SERVER;
  140. Log.e(TAG, "SSL Peer Unverified exception while trying connection", e);
  141. } catch (SSLException e) {
  142. mLatestResult = ResultType.SSL_INIT_ERROR;
  143. Log.e(TAG, "SSL exception while trying connection", e);
  144. } catch (ConnectTimeoutException e) { // timeout specific exception from org.apache.commons.httpclient
  145. mLatestResult = ResultType.TIMEOUT;
  146. Log.e(TAG, "Socket timeout exception while trying connection", e);
  147. } catch (HttpException e) { // other specific exceptions from org.apache.commons.httpclient
  148. mLatestResult = ResultType.UNKNOWN_ERROR;
  149. Log.e(TAG, "HTTP exception while trying connection", e);
  150. } catch (IOException e) { // UnkownsServiceException, and any other transport exceptions that could occur
  151. mLatestResult = ResultType.UNKNOWN_ERROR;
  152. Log.e(TAG, "I/O exception while trying connection", e);
  153. } catch (Exception e) {
  154. mLatestResult = ResultType.UNKNOWN_ERROR;
  155. Log.e(TAG, "Unexpected exception while trying connection", e);
  156. } finally {
  157. if (get != null)
  158. get.releaseConnection();
  159. }
  160. return retval;
  161. }
  162. private boolean isOnline() {
  163. ConnectivityManager cm = (ConnectivityManager) mContext
  164. .getSystemService(Context.CONNECTIVITY_SERVICE);
  165. return cm != null && cm.getActiveNetworkInfo() != null
  166. && cm.getActiveNetworkInfo().isConnectedOrConnecting();
  167. }
  168. private void postResult(final ResultType result) {
  169. if (mHandler != null && mListener != null) {
  170. mHandler.post(new Runnable() {
  171. @Override
  172. public void run() {
  173. mListener.onConnectionCheckResult(result);
  174. }
  175. });
  176. }
  177. }
  178. }