package com.owncloud.android.authenticator;
-import java.net.ConnectException;
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
-import javax.net.ssl.SSLHandshakeException;
+import javax.net.ssl.SSLException;
+import javax.net.ssl.SSLPeerUnverifiedException;
+import org.apache.commons.httpclient.ConnectTimeoutException;
+import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.json.JSONException;
import com.owncloud.android.AccountUtils;
import com.owncloud.android.authenticator.OnConnectCheckListener.ResultType;
+import com.owncloud.android.network.OwnCloudClientUtils;
import com.owncloud.android.utils.OwnCloudVersion;
import eu.alefzero.webdav.WebdavClient;
}
if (mUrl.startsWith("http://") || mUrl.startsWith("https://")) {
mLatestResult = (mUrl.startsWith("https://"))? ResultType.OK_SSL : ResultType.OK_NO_SSL;
- tryConnection(Uri.parse(mUrl + AccountUtils.STATUS_PATH));
+ tryConnection(mUrl + AccountUtils.STATUS_PATH);
postResult(mLatestResult);
} else {
- Uri uri = Uri.parse("https://" + mUrl + AccountUtils.STATUS_PATH);
- if (tryConnection(uri)) {
+ if (tryConnection("https://" + mUrl + AccountUtils.STATUS_PATH)) {
postResult(ResultType.OK_SSL);
return;
}
Log.d(TAG,
"establishing secure connection failed, trying non secure connection");
- uri = Uri.parse("http://" + mUrl + AccountUtils.STATUS_PATH);
- if (tryConnection(uri)) {
+ if (tryConnection("http://" + mUrl + AccountUtils.STATUS_PATH)) {
postResult(ResultType.OK_NO_SSL);
return;
}
return mOCVersion;
}
- private boolean tryConnection(Uri uri) {
- WebdavClient wc = new WebdavClient();
- wc.allowSelfsignedCertificates();
- GetMethod get = new GetMethod(uri.toString());
+ private boolean tryConnection(String urlSt) {
boolean retval = false;
+ GetMethod get = null;
try {
- int status = wc.executeMethod(get, TRY_CONNECTION_TIMEOUT);
+ WebdavClient wc = OwnCloudClientUtils.createOwnCloudClient(Uri.parse(urlSt), mContext);
+ get = new GetMethod(urlSt);
+ int status = wc.executeMethod(get, TRY_CONNECTION_TIMEOUT, TRY_CONNECTION_TIMEOUT);
+ String response = get.getResponseBodyAsString();
switch (status) {
case HttpStatus.SC_OK: {
- String response = get.getResponseBodyAsString();
JSONObject json = new JSONObject(response);
if (!json.getBoolean("installed")) {
mLatestResult = ResultType.INSTANCE_NOT_CONFIGURED;
break;
}
mOCVersion = new OwnCloudVersion(json.getString("version"));
- if (!mOCVersion.isVersionValid())
+ if (!mOCVersion.isVersionValid()) {
+ mLatestResult = ResultType.BAD_OC_VERSION;
break;
+ }
retval = true;
break;
}
Log.e(TAG, "Not handled status received from server: " + status);
}
+ } catch (JSONException e) {
+ mLatestResult = ResultType.INSTANCE_NOT_CONFIGURED;
+ Log.e(TAG, "JSON exception while trying connection (instance not configured) ", e);
+
+ } catch (SocketException e) {
+ mLatestResult = ResultType.WRONG_CONNECTION;
+ Log.e(TAG, "Socket exception while trying connection", e);
+
+ } catch (SocketTimeoutException e) {
+ mLatestResult = ResultType.TIMEOUT;
+ Log.e(TAG, "Socket timeout exception while trying connection", e);
+
+ } catch (MalformedURLException e) {
+ mLatestResult = ResultType.INCORRECT_ADDRESS;
+ Log.e(TAG, "Connect exception while trying connection", e);
+
+ } catch (UnknownHostException e) {
+ mLatestResult = ResultType.HOST_NOT_AVAILABLE;
+ Log.e(TAG, "Unknown host exception while trying connection", e);
+
+ } catch (SSLPeerUnverifiedException e) { // specially meaningful SSLException
+ mLatestResult = ResultType.SSL_UNVERIFIED_SERVER;
+ Log.e(TAG, "SSL Peer Unverified exception while trying connection", e);
+
+ } catch (SSLException e) {
+ mLatestResult = ResultType.SSL_INIT_ERROR;
+ Log.e(TAG, "SSL exception while trying connection", e);
+
+ } catch (ConnectTimeoutException e) { // timeout specific exception from org.apache.commons.httpclient
+ mLatestResult = ResultType.TIMEOUT;
+ Log.e(TAG, "Socket timeout exception while trying connection", e);
+
+ } catch (HttpException e) { // other specific exceptions from org.apache.commons.httpclient
+ mLatestResult = ResultType.UNKNOWN_ERROR;
+ Log.e(TAG, "HTTP exception while trying connection", e);
+
+ } catch (IOException e) { // UnkownsServiceException, and any other transport exceptions that could occur
+ mLatestResult = ResultType.UNKNOWN_ERROR;
+ Log.e(TAG, "I/O exception while trying connection", e);
+
} catch (Exception e) {
- if (e instanceof UnknownHostException
- || e instanceof ConnectException
- || e instanceof SocketTimeoutException) {
- mLatestResult = ResultType.HOST_NOT_AVAILABLE;
- } else if (e instanceof JSONException) {
- mLatestResult = ResultType.INSTANCE_NOT_CONFIGURED;
- } else if (e instanceof SSLHandshakeException) {
- mLatestResult = ResultType.SSL_INIT_ERROR;
- } else {
- mLatestResult = ResultType.UNKNOWN_ERROR;
- }
- e.printStackTrace();
+ mLatestResult = ResultType.UNKNOWN_ERROR;
+ Log.e(TAG, "Unexpected exception while trying connection", e);
+
+ } finally {
+ if (get != null)
+ get.releaseConnection();
}
return retval;