Fixed crash by URL with whitespaces in login page; reviewed error handling and user...
authorDavid A. Velasco <dvelasco@solidgear.es>
Thu, 2 Aug 2012 11:28:33 +0000 (13:28 +0200)
committerDavid A. Velasco <dvelasco@solidgear.es>
Thu, 2 Aug 2012 11:28:33 +0000 (13:28 +0200)
res/values/strings.xml
src/com/owncloud/android/authenticator/ConnectionCheckerRunnable.java
src/com/owncloud/android/authenticator/OnConnectCheckListener.java
src/com/owncloud/android/ui/activity/AuthenticatorActivity.java
src/eu/alefzero/webdav/WebdavClient.java

index c0f0de0..c482937 100644 (file)
     <string name="auth_testing_connection">Testing connection…</string>
     <string name="auth_not_configured_title">Malformed ownCloud configuration</string>
     <string name="auth_not_configured_message">It seems that your ownCloud instance is not correctly configured. Contact your administrator for more details.</string>
-    <string name="auth_unknown_error_title">Unknown error occurred</string>
+    <string name="auth_unknown_error_title">Unknown error occurred!</string>
     <string name="auth_unknown_error_message">Unknown error occurred. Please contact authors and include logs from your device.</string>
-    <string name="auth_unknow_host_title">Can\'t establish connection</string>
-    <string name="auth_unknow_host_message">Couldn\'t establish connection to host. Please check hostname and server availability and try again.</string>
+    <string name="auth_unknown_host_title">Couldn\'t find host</string>
+    <string name="auth_unknown_host_message">Couldn\'t find the entered host. Please check hostname and server availability and try again.</string>
     <string name="auth_incorrect_path_title">ownCloud instance not found</string>
     <string name="auth_incorrect_path_message">Application couldn\'t find ownClound instance at given path. Please check your path and try again.</string>
-    <string name="auth_secure_connection">Secure connection established</string>
-    <string name="auth_unknow_error">Unknown error occurred!</string>
+    <string name="auth_timeout_title">The server took too long to respond</string>
+    <string name="auth_incorrect_address_title">Malformed URL</string>
+       <string name="auth_ssl_general_error_title">SSL initialization failed</string>
+       <string name="auth_ssl_unverified_server_title">Unverified SSL server\'s identity</string>
+       <string name="auth_bad_oc_version_title">Unrecognized ownCloud server version</string>
+       <string name="auth_wrong_connection_title">Couldn\'t establish connection</string>
+       <string name="auth_secure_connection">Secure connection established</string>
     <string name="auth_login_details">Login details</string>
     
     <string name="crashlog_message">Application terminated unexpectedly. Would you like to submit crash report?</string>
index d1d1070..52e4eb0 100644 (file)
 
 package com.owncloud.android.authenticator;
 
+import java.io.IOException;
 import java.net.ConnectException;
+import java.net.MalformedURLException;
+import java.net.SocketException;
 import java.net.SocketTimeoutException;
+import java.net.URI;
+import java.net.URL;
 import java.net.UnknownHostException;
 
+import javax.net.ssl.SSLException;
 import javax.net.ssl.SSLHandshakeException;
+import javax.net.ssl.SSLPeerUnverifiedException;
 
+import org.apache.commons.httpclient.HttpException;
 import org.apache.commons.httpclient.HttpStatus;
 import org.apache.commons.httpclient.methods.GetMethod;
 import org.json.JSONException;
@@ -75,19 +83,17 @@ public class ConnectionCheckerRunnable implements Runnable {
         }
         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;
             }
@@ -99,12 +105,13 @@ public class ConnectionCheckerRunnable implements Runnable {
         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;
         try {
+            WebdavClient wc = new WebdavClient();
+            wc.allowSelfsignedCertificates();
+            URL url = new URL(urlSt);   // better than android.net.Uri in this case; provides URL validation
+            GetMethod get = new GetMethod(url.toString());
             int status = wc.executeMethod(get, TRY_CONNECTION_TIMEOUT);
             switch (status) {
             case HttpStatus.SC_OK: {
@@ -115,8 +122,10 @@ public class ConnectionCheckerRunnable implements Runnable {
                     break;
                 }
                 mOCVersion = new OwnCloudVersion(json.getString("version"));
-                if (!mOCVersion.isVersionValid())
+                if (!mOCVersion.isVersionValid()) {
+                    mLatestResult = ResultType.BAD_OC_VERSION;
                     break;
+                }
                 retval = true;
                 break;
             }
@@ -131,19 +140,45 @@ public class ConnectionCheckerRunnable implements Runnable {
                 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 (HttpException e) { // 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 weird I/O Exception 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);
         }
 
         return retval;
index bca71ae..3d66c3c 100644 (file)
@@ -3,7 +3,7 @@ package com.owncloud.android.authenticator;
 public interface OnConnectCheckListener {
 
     enum ResultType {
-        OK_SSL, OK_NO_SSL, SSL_INIT_ERROR, HOST_NOT_AVAILABLE, TIMEOUT, NO_NETWORK_CONNECTION, INORRECT_ADDRESS, INSTANCE_NOT_CONFIGURED, FILE_NOT_FOUND, UNKNOWN_ERROR
+        OK_SSL, OK_NO_SSL, SSL_INIT_ERROR, HOST_NOT_AVAILABLE, TIMEOUT, NO_NETWORK_CONNECTION, INCORRECT_ADDRESS, INSTANCE_NOT_CONFIGURED, FILE_NOT_FOUND, UNKNOWN_ERROR, WRONG_CONNECTION,  SSL_UNVERIFIED_SERVER, BAD_OC_VERSION
     }
 
     public void onConnectionCheckResult(ResultType type);
index 4aa7cdd..dbf9576 100644 (file)
@@ -249,7 +249,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
 \r
     private void continueConnection(String prefix) {\r
         String url = ((TextView) findViewById(R.id.host_URL)).getText()\r
-                .toString();\r
+                .toString().trim();\r
         String username = ((TextView) findViewById(R.id.account_username))\r
                 .getText().toString();\r
         String password = ((TextView) findViewById(R.id.account_password))\r
@@ -302,12 +302,33 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
                 mStatusIcon = android.R.drawable.ic_partial_secure;\r
             }\r
             break;\r
+        case BAD_OC_VERSION:\r
+            mStatusIcon = R.drawable.common_error;\r
+            mStatusText = R.string.auth_bad_oc_version_title;\r
+            break;\r
+        case WRONG_CONNECTION:\r
+            mStatusIcon = R.drawable.common_error;\r
+            mStatusText = R.string.auth_wrong_connection_title;\r
+            break;\r
         case TIMEOUT:\r
-        case INORRECT_ADDRESS:\r
+            mStatusIcon = R.drawable.common_error;\r
+            mStatusText = R.string.auth_timeout_title;\r
+            break;\r
+        case INCORRECT_ADDRESS:\r
+            mStatusIcon = R.drawable.common_error;\r
+            mStatusText = R.string.auth_incorrect_address_title;\r
+            break;\r
+        case SSL_UNVERIFIED_SERVER:\r
+            mStatusIcon = R.drawable.common_error;\r
+            mStatusText = R.string.auth_ssl_unverified_server_title;\r
+            break;\r
         case SSL_INIT_ERROR:\r
+            mStatusIcon = R.drawable.common_error;\r
+            mStatusText = R.string.auth_ssl_general_error_title;\r
+            break;\r
         case HOST_NOT_AVAILABLE:\r
             mStatusIcon = R.drawable.common_error;\r
-            mStatusText = R.string.auth_unknow_host_title;\r
+            mStatusText = R.string.auth_unknown_host_title;\r
             break;\r
         case NO_NETWORK_CONNECTION:\r
             mStatusIcon = R.drawable.no_network;\r
@@ -319,7 +340,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
             break;\r
         case UNKNOWN_ERROR:\r
             mStatusIcon = R.drawable.common_error;\r
-            mStatusText = R.string.auth_unknow_error;\r
+            mStatusText = R.string.auth_unknown_error_title;\r
             break;\r
         case FILE_NOT_FOUND:\r
             mStatusIcon = R.drawable.common_error;\r
@@ -341,7 +362,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
         if (view.getId() == R.id.host_URL) {\r
             if (!hasFocus) {\r
                 TextView tv = ((TextView) findViewById(R.id.host_URL));\r
-                String uri = tv.getText().toString();\r
+                String uri = tv.getText().toString().trim();\r
                 if (uri.length() != 0) {\r
                     setResultIconAndText(R.drawable.progress_small,\r
                             R.string.auth_testing_connection);\r
index 1e64bdf..1fabe3a 100644 (file)
@@ -255,10 +255,10 @@ public class WebdavClient extends HttpClient {
      */\r
     public static int tryToLogin(Uri uri, String username, String password) {\r
         int returnCode = 0;\r
-        WebdavClient client = new WebdavClient();\r
-        client.setCredentials(username, password);\r
-        HeadMethod head = new HeadMethod(uri.toString());\r
         try {\r
+            WebdavClient client = new WebdavClient();\r
+            client.setCredentials(username, password);\r
+            HeadMethod head = new HeadMethod(uri.toString());\r
             returnCode = client.executeMethod(head);\r
         } catch (HttpException e) {\r
             Log.e(TAG, "HTTP exception trying to login at " + uri.getEncodedPath(), e);\r