From 4b2c32511be8b1495345053aee5e9b7b20ba6f15 Mon Sep 17 00:00:00 2001 From: davigonz Date: Mon, 12 Jun 2017 10:45:08 +0200 Subject: [PATCH 1/2] Improve feedback when uploading virus files --- .../operations/RemoteOperationResult.java | 43 ++++++++++++++----- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/src/com/owncloud/android/lib/common/operations/RemoteOperationResult.java b/src/com/owncloud/android/lib/common/operations/RemoteOperationResult.java index b39f394b..61c24ea1 100644 --- a/src/com/owncloud/android/lib/common/operations/RemoteOperationResult.java +++ b/src/com/owncloud/android/lib/common/operations/RemoteOperationResult.java @@ -119,7 +119,8 @@ public class RemoteOperationResult implements Serializable { DELAYED_FOR_WIFI, LOCAL_FILE_NOT_FOUND, SERVICE_UNAVAILABLE, - SPECIFIC_SERVICE_UNAVAILABLE + SPECIFIC_SERVICE_UNAVAILABLE, + SPECIFIC_UNSUPPORTED_MEDIA_TYPE } private boolean mSuccess = false; @@ -135,7 +136,7 @@ public class RemoteOperationResult implements Serializable { /** * Public constructor from result code. - *

+ * * To be used when the caller takes the responsibility of interpreting the result of a {@link RemoteOperation} * * @param code {@link ResultCode} decided by the caller. @@ -150,9 +151,9 @@ public class RemoteOperationResult implements Serializable { /** * Public constructor from exception. - *

+ * * To be used when an exception prevented the end of the {@link RemoteOperation}. - *

+ * * Determines a {@link ResultCode} depending on the type of the exception. * * @param e Exception that interrupted the {@link RemoteOperation} @@ -209,9 +210,9 @@ public class RemoteOperationResult implements Serializable { /** * Public constructor from separate elements of an HTTP or DAV response. - *

+ * * To be used when the result needs to be interpreted from the response of an HTTP/DAV method. - *

+ * * Determines a {@link ResultCode} from the already executed method received as a parameter. Generally, * will depend on the HTTP code and HTTP response headers received. In some cases will inspect also the * response body. @@ -247,7 +248,7 @@ public class RemoteOperationResult implements Serializable { } } - if (mHttpCode == HttpStatus.SC_FORBIDDEN) { + if (mHttpCode == HttpStatus.SC_FORBIDDEN) { // 403 String bodyResponse = httpMethod.getResponseBodyAsString(); if (bodyResponse != null && bodyResponse.length() > 0) { @@ -266,7 +267,27 @@ public class RemoteOperationResult implements Serializable { } } - if (mHttpCode == HttpStatus.SC_SERVICE_UNAVAILABLE) { + if (mHttpCode == HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE) { // 415 + + String bodyResponse = httpMethod.getResponseBodyAsString(); + + if (bodyResponse != null && bodyResponse.length() > 0) { + InputStream is = new ByteArrayInputStream(bodyResponse.getBytes()); + ErrorMessageParser xmlParser = new ErrorMessageParser(); + try { + String errorMessage = xmlParser.parseXMLResponse(is); + if (errorMessage != null && errorMessage.length() > 0) { + mCode = ResultCode.SPECIFIC_UNSUPPORTED_MEDIA_TYPE; + mHttpPhrase = errorMessage; + } + } catch (Exception e) { + Log_OC.w(TAG, "Error reading exception from server: " + e.getMessage()); + // mCode stays as set in this(success, httpCode, headers) + } + } + } + + if (mHttpCode == HttpStatus.SC_SERVICE_UNAVAILABLE) { // 503 String bodyResponse = httpMethod.getResponseBodyAsString(); if (bodyResponse != null && bodyResponse.length() > 0) { @@ -288,13 +309,13 @@ public class RemoteOperationResult implements Serializable { /** * Public constructor from separate elements of an HTTP or DAV response. - *

+ * * To be used when the result needs to be interpreted from HTTP response elements that could come from * different requests (WARNING: black magic, try to avoid). - *

+ * * If all the fields come from the same HTTP/DAV response, {@link #RemoteOperationResult(boolean, HttpMethod)} * should be used instead. - *

+ * * Determines a {@link ResultCode} depending on the HTTP code and HTTP response headers received. * * @param success The operation was considered successful or not. From b074c34cbc109edcb5c3e339aa318bf98410f45c Mon Sep 17 00:00:00 2001 From: davigonz Date: Tue, 13 Jun 2017 11:36:23 +0200 Subject: [PATCH 2/2] Refactor code --- .../operations/RemoteOperationResult.java | 75 ++++++++----------- 1 file changed, 31 insertions(+), 44 deletions(-) diff --git a/src/com/owncloud/android/lib/common/operations/RemoteOperationResult.java b/src/com/owncloud/android/lib/common/operations/RemoteOperationResult.java index 61c24ea1..ea60f379 100644 --- a/src/com/owncloud/android/lib/common/operations/RemoteOperationResult.java +++ b/src/com/owncloud/android/lib/common/operations/RemoteOperationResult.java @@ -230,6 +230,7 @@ public class RemoteOperationResult implements Serializable { ); if (mHttpCode == HttpStatus.SC_BAD_REQUEST) { // 400 + String bodyResponse = httpMethod.getResponseBodyAsString(); // do not get for other HTTP codes!; could not be available @@ -249,60 +250,46 @@ public class RemoteOperationResult implements Serializable { } if (mHttpCode == HttpStatus.SC_FORBIDDEN) { // 403 - String bodyResponse = httpMethod.getResponseBodyAsString(); - if (bodyResponse != null && bodyResponse.length() > 0) { - InputStream is = new ByteArrayInputStream(bodyResponse.getBytes()); - ErrorMessageParser xmlParser = new ErrorMessageParser(); - try { - String errorMessage = xmlParser.parseXMLResponse(is); - if (errorMessage != null && errorMessage.length() > 0) { - mCode = ResultCode.SPECIFIC_FORBIDDEN; - mHttpPhrase = errorMessage; - } - } catch (Exception e) { - Log_OC.w(TAG, "Error reading exception from server: " + e.getMessage()); - // mCode stays as set in this(success, httpCode, headers) - } - } + parseErrorMessageAndSetCode(httpMethod, ResultCode.SPECIFIC_FORBIDDEN); } if (mHttpCode == HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE) { // 415 - String bodyResponse = httpMethod.getResponseBodyAsString(); - - if (bodyResponse != null && bodyResponse.length() > 0) { - InputStream is = new ByteArrayInputStream(bodyResponse.getBytes()); - ErrorMessageParser xmlParser = new ErrorMessageParser(); - try { - String errorMessage = xmlParser.parseXMLResponse(is); - if (errorMessage != null && errorMessage.length() > 0) { - mCode = ResultCode.SPECIFIC_UNSUPPORTED_MEDIA_TYPE; - mHttpPhrase = errorMessage; - } - } catch (Exception e) { - Log_OC.w(TAG, "Error reading exception from server: " + e.getMessage()); - // mCode stays as set in this(success, httpCode, headers) - } - } + parseErrorMessageAndSetCode(httpMethod, ResultCode.SPECIFIC_UNSUPPORTED_MEDIA_TYPE); } if (mHttpCode == HttpStatus.SC_SERVICE_UNAVAILABLE) { // 503 - String bodyResponse = httpMethod.getResponseBodyAsString(); - if (bodyResponse != null && bodyResponse.length() > 0) { - InputStream is = new ByteArrayInputStream(bodyResponse.getBytes()); - ErrorMessageParser xmlParser = new ErrorMessageParser(); - try { - String errorMessage = xmlParser.parseXMLResponse(is); - if (errorMessage != "" && errorMessage != null) { - mCode = ResultCode.SPECIFIC_SERVICE_UNAVAILABLE; - mHttpPhrase = errorMessage; - } - } catch (Exception e) { - Log_OC.w(TAG, "Error reading exception from server: " + e.getMessage()); - // mCode stays as set in this(success, httpCode, headers) + parseErrorMessageAndSetCode(httpMethod, ResultCode.SPECIFIC_SERVICE_UNAVAILABLE); + } + } + + /** + * Parse the error message included in the body response, if any, and set the specific result + * code + * @param httpMethod HTTP/DAV method already executed which response body will be parsed to get + * the specific error message + * @param resultCode specific result code + * @throws IOException + */ + private void parseErrorMessageAndSetCode(HttpMethod httpMethod, ResultCode resultCode) + throws IOException { + + String bodyResponse = httpMethod.getResponseBodyAsString(); + + if (bodyResponse != null && bodyResponse.length() > 0) { + InputStream is = new ByteArrayInputStream(bodyResponse.getBytes()); + ErrorMessageParser xmlParser = new ErrorMessageParser(); + try { + String errorMessage = xmlParser.parseXMLResponse(is); + if (errorMessage != "" && errorMessage != null) { + mCode = resultCode; + mHttpPhrase = errorMessage; } + } catch (Exception e) { + Log_OC.w(TAG, "Error reading exception from server: " + e.getMessage()); + // mCode stays as set in this(success, httpCode, headers) } } }