Pārlūkot izejas kodu

Fixed crash by URL with whitespaces in login page; reviewed error handling and user info in connection checking

David A. Velasco 12 gadi atpakaļ
vecāks
revīzija
38ece35da5

+ 10 - 5
res/values/strings.xml

@@ -131,14 +131,19 @@
     <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>

+ 57 - 22
src/com/owncloud/android/authenticator/ConnectionCheckerRunnable.java

@@ -18,12 +18,20 @@
 
 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;

+ 1 - 1
src/com/owncloud/android/authenticator/OnConnectCheckListener.java

@@ -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);

+ 26 - 5
src/com/owncloud/android/ui/activity/AuthenticatorActivity.java

@@ -249,7 +249,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
 
     private void continueConnection(String prefix) {
         String url = ((TextView) findViewById(R.id.host_URL)).getText()
-                .toString();
+                .toString().trim();
         String username = ((TextView) findViewById(R.id.account_username))
                 .getText().toString();
         String password = ((TextView) findViewById(R.id.account_password))
@@ -302,12 +302,33 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
                 mStatusIcon = android.R.drawable.ic_partial_secure;
             }
             break;
+        case BAD_OC_VERSION:
+            mStatusIcon = R.drawable.common_error;
+            mStatusText = R.string.auth_bad_oc_version_title;
+            break;
+        case WRONG_CONNECTION:
+            mStatusIcon = R.drawable.common_error;
+            mStatusText = R.string.auth_wrong_connection_title;
+            break;
         case TIMEOUT:
-        case INORRECT_ADDRESS:
+            mStatusIcon = R.drawable.common_error;
+            mStatusText = R.string.auth_timeout_title;
+            break;
+        case INCORRECT_ADDRESS:
+            mStatusIcon = R.drawable.common_error;
+            mStatusText = R.string.auth_incorrect_address_title;
+            break;
+        case SSL_UNVERIFIED_SERVER:
+            mStatusIcon = R.drawable.common_error;
+            mStatusText = R.string.auth_ssl_unverified_server_title;
+            break;
         case SSL_INIT_ERROR:
+            mStatusIcon = R.drawable.common_error;
+            mStatusText = R.string.auth_ssl_general_error_title;
+            break;
         case HOST_NOT_AVAILABLE:
             mStatusIcon = R.drawable.common_error;
-            mStatusText = R.string.auth_unknow_host_title;
+            mStatusText = R.string.auth_unknown_host_title;
             break;
         case NO_NETWORK_CONNECTION:
             mStatusIcon = R.drawable.no_network;
@@ -319,7 +340,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
             break;
         case UNKNOWN_ERROR:
             mStatusIcon = R.drawable.common_error;
-            mStatusText = R.string.auth_unknow_error;
+            mStatusText = R.string.auth_unknown_error_title;
             break;
         case FILE_NOT_FOUND:
             mStatusIcon = R.drawable.common_error;
@@ -341,7 +362,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
         if (view.getId() == R.id.host_URL) {
             if (!hasFocus) {
                 TextView tv = ((TextView) findViewById(R.id.host_URL));
-                String uri = tv.getText().toString();
+                String uri = tv.getText().toString().trim();
                 if (uri.length() != 0) {
                     setResultIconAndText(R.drawable.progress_small,
                             R.string.auth_testing_connection);

+ 3 - 3
src/eu/alefzero/webdav/WebdavClient.java

@@ -255,10 +255,10 @@ public class WebdavClient extends HttpClient {
      */
     public static int tryToLogin(Uri uri, String username, String password) {
         int returnCode = 0;
-        WebdavClient client = new WebdavClient();
-        client.setCredentials(username, password);
-        HeadMethod head = new HeadMethod(uri.toString());
         try {
+            WebdavClient client = new WebdavClient();
+            client.setCredentials(username, password);
+            HeadMethod head = new HeadMethod(uri.toString());
             returnCode = client.executeMethod(head);
         } catch (HttpException e) {
             Log.e(TAG, "HTTP exception trying to login at " + uri.getEncodedPath(), e);