diff --git a/src/com/owncloud/android/lib/common/operations/RemoteOperationResult.java b/src/com/owncloud/android/lib/common/operations/RemoteOperationResult.java index b39f394b..ea60f379 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. @@ -229,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 @@ -247,54 +249,60 @@ public class RemoteOperationResult implements Serializable { } } - if (mHttpCode == HttpStatus.SC_FORBIDDEN) { - String bodyResponse = httpMethod.getResponseBodyAsString(); + if (mHttpCode == HttpStatus.SC_FORBIDDEN) { // 403 - 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_SERVICE_UNAVAILABLE) { - String bodyResponse = httpMethod.getResponseBodyAsString(); + if (mHttpCode == HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE) { // 415 - 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_UNSUPPORTED_MEDIA_TYPE); + } + + if (mHttpCode == HttpStatus.SC_SERVICE_UNAVAILABLE) { // 503 + + 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) } } } /** * 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.