Browse Source

Merge pull request #3133 from nextcloud/sso-fix-exception

fix exception handling
Andy Scherzinger 6 years ago
parent
commit
b31d650b28
1 changed files with 22 additions and 3 deletions
  1. 22 3
      src/main/java/com/nextcloud/android/sso/InputStreamBinder.java

+ 22 - 3
src/main/java/com/nextcloud/android/sso/InputStreamBinder.java

@@ -49,10 +49,12 @@ import org.apache.commons.httpclient.methods.PostMethod;
 import org.apache.commons.httpclient.methods.PutMethod;
 import org.apache.commons.httpclient.methods.PutMethod;
 import org.apache.commons.httpclient.methods.StringRequestEntity;
 import org.apache.commons.httpclient.methods.StringRequestEntity;
 
 
+import java.io.BufferedReader;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStream;
+import java.io.InputStreamReader;
 import java.io.ObjectInputStream;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 import java.io.ObjectOutputStream;
 import java.io.Serializable;
 import java.io.Serializable;
@@ -111,7 +113,7 @@ public class InputStreamBinder extends IInputStreamService.Stub {
             NextcloudRequest request = deserializeObjectAndCloseStream(is);
             NextcloudRequest request = deserializeObjectAndCloseStream(is);
             httpStream = processRequest(request);
             httpStream = processRequest(request);
         } catch (Exception e) {
         } catch (Exception e) {
-            Log_OC.e(TAG, e.getMessage());
+            Log_OC.e(TAG, "Error during Nextcloud request", e);
             exception = e;
             exception = e;
         }
         }
 
 
@@ -121,7 +123,7 @@ public class InputStreamBinder extends IInputStreamService.Stub {
             InputStream resultStream = new java.io.SequenceInputStream(exceptionStream, httpStream);
             InputStream resultStream = new java.io.SequenceInputStream(exceptionStream, httpStream);
             return ParcelFileDescriptorUtil.pipeFrom(resultStream, thread -> Log.d(TAG, "Done sending result"));
             return ParcelFileDescriptorUtil.pipeFrom(resultStream, thread -> Log.d(TAG, "Done sending result"));
         } catch (IOException e) {
         } catch (IOException e) {
-            Log_OC.e(TAG, e.getMessage());
+            Log_OC.e(TAG, "Error while sending response back to client app", e);
         }
         }
         return null;
         return null;
     }
     }
@@ -213,7 +215,20 @@ public class InputStreamBinder extends IInputStreamService.Stub {
         if (status >= HTTP_STATUS_CODE_OK && status < HTTP_STATUS_CODE_MULTIPLE_CHOICES) {
         if (status >= HTTP_STATUS_CODE_OK && status < HTTP_STATUS_CODE_MULTIPLE_CHOICES) {
             return method.getResponseBodyAsStream();
             return method.getResponseBodyAsStream();
         } else {
         } else {
-            throw new IllegalStateException(EXCEPTION_HTTP_REQUEST_FAILED, new IllegalStateException(String.valueOf(status)));
+            StringBuilder total = new StringBuilder();
+            InputStream inputStream = method.getResponseBodyAsStream();
+            // If response body is available
+            if (inputStream != null) {
+                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
+                String line = reader.readLine();
+                while (line != null) {
+                    total.append(line).append('\n');
+                    line = reader.readLine();
+                }
+                Log_OC.e(TAG, total.toString());
+            }
+            throw new IllegalStateException(EXCEPTION_HTTP_REQUEST_FAILED,
+                new IllegalStateException(String.valueOf(status), new Throwable(total.toString())));
         }
         }
     }
     }
 
 
@@ -227,6 +242,10 @@ public class InputStreamBinder extends IInputStreamService.Stub {
     }
     }
 
 
     private boolean validateToken(String hash, String token) {
     private boolean validateToken(String hash, String token) {
+        if (hash.isEmpty() || !hash.contains("$")) {
+            throw new IllegalStateException(EXCEPTION_INVALID_TOKEN);
+        }
+
         String salt = hash.split("\\$")[1]; // TODO extract "$"
         String salt = hash.split("\\$")[1]; // TODO extract "$"
 
 
         String newHash = EncryptionUtils.generateSHA512(token, salt);
         String newHash = EncryptionUtils.generateSHA512(token, salt);