1
0
mirror of https://github.com/owncloud/android-library.git synced 2025-06-07 16:06:08 +00:00

Refactor code

This commit is contained in:
davigonz 2017-06-13 11:36:23 +02:00
parent 4b2c32511b
commit b074c34cbc

View File

@ -230,6 +230,7 @@ public class RemoteOperationResult implements Serializable {
); );
if (mHttpCode == HttpStatus.SC_BAD_REQUEST) { // 400 if (mHttpCode == HttpStatus.SC_BAD_REQUEST) { // 400
String bodyResponse = httpMethod.getResponseBodyAsString(); String bodyResponse = httpMethod.getResponseBodyAsString();
// do not get for other HTTP codes!; could not be available // 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 if (mHttpCode == HttpStatus.SC_FORBIDDEN) { // 403
String bodyResponse = httpMethod.getResponseBodyAsString();
if (bodyResponse != null && bodyResponse.length() > 0) { parseErrorMessageAndSetCode(httpMethod, ResultCode.SPECIFIC_FORBIDDEN);
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)
}
}
} }
if (mHttpCode == HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE) { // 415 if (mHttpCode == HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE) { // 415
String bodyResponse = httpMethod.getResponseBodyAsString(); parseErrorMessageAndSetCode(httpMethod, ResultCode.SPECIFIC_UNSUPPORTED_MEDIA_TYPE);
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 if (mHttpCode == HttpStatus.SC_SERVICE_UNAVAILABLE) { // 503
String bodyResponse = httpMethod.getResponseBodyAsString();
if (bodyResponse != null && bodyResponse.length() > 0) { parseErrorMessageAndSetCode(httpMethod, ResultCode.SPECIFIC_SERVICE_UNAVAILABLE);
InputStream is = new ByteArrayInputStream(bodyResponse.getBytes()); }
ErrorMessageParser xmlParser = new ErrorMessageParser(); }
try {
String errorMessage = xmlParser.parseXMLResponse(is); /**
if (errorMessage != "" && errorMessage != null) { * Parse the error message included in the body response, if any, and set the specific result
mCode = ResultCode.SPECIFIC_SERVICE_UNAVAILABLE; * code
mHttpPhrase = errorMessage; * @param httpMethod HTTP/DAV method already executed which response body will be parsed to get
} * the specific error message
} catch (Exception e) { * @param resultCode specific result code
Log_OC.w(TAG, "Error reading exception from server: " + e.getMessage()); * @throws IOException
// mCode stays as set in this(success, httpCode, headers) */
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)
} }
} }
} }