Skip to content

Commit

Permalink
Merge pull request #135 from nextcloud/virusHandling
Browse files Browse the repository at this point in the history
Parse virus error message
  • Loading branch information
tobiasKaminsky authored Mar 22, 2018
2 parents 351df3a + 36b6ffa commit 712ef8d
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 175 deletions.
162 changes: 162 additions & 0 deletions src/com/owncloud/android/lib/common/operations/ExceptionParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@

/* ownCloud Android Library is available under MIT license
* Copyright (C) 2015 ownCloud Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
package com.owncloud.android.lib.common.operations;

import android.util.Xml;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.IOException;
import java.io.InputStream;

/**
* Parser server exception
*
* @author masensio, tobiaskaminsky
*/
public class ExceptionParser {

private static final String INVALID_PATH_EXCEPTION_STRING = "OC\\Connector\\Sabre\\Exception\\InvalidPath";
private static final String INVALID_PATH_EXCEPTION_UPLOAD_STRING = "OCP\\Files\\InvalidPathException";
private static final String VIRUS_EXCEPTION_STRING = "OCA\\DAV\\Connector\\Sabre\\Exception\\UnsupportedMediaType";

// No namespaces
private static final String ns = null;

// Nodes for XML Parser
private static final String NODE_ERROR = "d:error";
private static final String NODE_EXCEPTION = "s:exception";
private static final String NODE_MESSAGE = "s:message";

private String exception;
private String message;

/**
* Parse is as an Invalid Path Exception
*
* @param is InputStream xml
* @return if The exception is an Invalid Char Exception
* @throws XmlPullParserException
* @throws IOException
*/
public ExceptionParser(InputStream is) throws XmlPullParserException, IOException {

try {
// XMLPullParser
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);

XmlPullParser parser = Xml.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(is, null);
parser.nextTag();
readError(parser);

} finally {
is.close();
}
}

public boolean isInvalidCharacterException() {
return exception.equalsIgnoreCase(INVALID_PATH_EXCEPTION_STRING) || exception.equalsIgnoreCase(INVALID_PATH_EXCEPTION_UPLOAD_STRING);
}

public boolean isVirusException() {
return exception.equalsIgnoreCase(VIRUS_EXCEPTION_STRING) && message.startsWith("Virus");
}

/**
* Parse OCS node
*
* @param parser
* @return List of ShareRemoteFiles
* @throws XmlPullParserException
* @throws IOException
*/
private void readError(XmlPullParser parser) throws XmlPullParserException, IOException {
parser.require(XmlPullParser.START_TAG, ns, NODE_ERROR);
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();

if (name.equalsIgnoreCase(NODE_EXCEPTION)) {
exception = readText(parser);
} else if (name.equalsIgnoreCase(NODE_MESSAGE)) {
message = readText(parser);
} else {
skip(parser);
}
}
}

/**
* Skip tags in parser procedure
*
* @param parser
* @throws XmlPullParserException
* @throws IOException
*/
private void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
if (parser.getEventType() != XmlPullParser.START_TAG) {
throw new IllegalStateException();
}
int depth = 1;
while (depth != 0) {
switch (parser.next()) {
case XmlPullParser.END_TAG:
depth--;
break;
case XmlPullParser.START_TAG:
depth++;
break;
}
}
}

/**
* Read the text from a node
*
* @param parser
* @return Text of the node
* @throws IOException
* @throws XmlPullParserException
*/
private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
String result = "";
if (parser.next() == XmlPullParser.TEXT) {
result = parser.getText();
parser.nextTag();
}
return result;
}

public String getMessage() {
return message;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ public enum ResultCode {
OLD_ANDROID_API,
UNTRUSTED_DOMAIN,
ETAG_CHANGED,
ETAG_UNCHANGED
ETAG_UNCHANGED,
VIRUS_DETECTED
}

private boolean mSuccess = false;
Expand Down Expand Up @@ -223,26 +224,23 @@ public RemoteOperationResult(boolean success, String bodyResponse, int httpCode)

if (success) {
mCode = ResultCode.OK;

} else if (httpCode > 0) {
switch (httpCode) {
case HttpStatus.SC_BAD_REQUEST:

InputStream is = new ByteArrayInputStream(bodyResponse.getBytes());
InvalidCharacterExceptionParser xmlParser = new InvalidCharacterExceptionParser();
try {
if (xmlParser.parseXMLResponse(is))
InputStream is = new ByteArrayInputStream(bodyResponse.getBytes());
ExceptionParser xmlParser = new ExceptionParser(is);
if (xmlParser.isInvalidCharacterException()) {
mCode = ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER;

}
} catch (Exception e) {
mCode = ResultCode.UNHANDLED_HTTP_CODE;
Log_OC.e(TAG, "Exception reading exception from server", e);
}
break;
default:
mCode = ResultCode.UNHANDLED_HTTP_CODE;
Log_OC.d(TAG, "RemoteOperationResult has processed UNHANDLED_HTTP_CODE: " +
httpCode);
Log_OC.d(TAG, "RemoteOperationResult has processed UNHANDLED_HTTP_CODE: " + httpCode);
}
}
}
Expand Down Expand Up @@ -319,29 +317,27 @@ public RemoteOperationResult(Exception e) {
* result.
*/
public RemoteOperationResult(boolean success, HttpMethod httpMethod) throws IOException {
this(
success,
httpMethod.getStatusCode(),
httpMethod.getStatusText(),
httpMethod.getResponseHeaders()
);

if (mHttpCode == HttpStatus.SC_BAD_REQUEST) { // 400
String bodyResponse = httpMethod.getResponseBodyAsString();
// do not get for other HTTP codes!; could not be available

if (bodyResponse != null && bodyResponse.length() > 0) {
InputStream is = new ByteArrayInputStream(bodyResponse.getBytes());
InvalidCharacterExceptionParser xmlParser = new InvalidCharacterExceptionParser();
try {
if (xmlParser.parseXMLResponse(is)) {
this(success, httpMethod.getStatusCode(), httpMethod.getStatusText(), httpMethod.getResponseHeaders());

if (mHttpCode == HttpStatus.SC_BAD_REQUEST || mHttpCode == HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE) {
try {
String bodyResponse = httpMethod.getResponseBodyAsString();

if (bodyResponse != null && bodyResponse.length() > 0) {
InputStream is = new ByteArrayInputStream(bodyResponse.getBytes());
ExceptionParser xmlParser = new ExceptionParser(is);

if (xmlParser.isInvalidCharacterException()) {
mCode = ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER;
}

} catch (Exception e) {
Log_OC.w(TAG, "Error reading exception from server: " + e.getMessage());
// mCode stays as set in this(success, httpCode, headers)
if (xmlParser.isVirusException()) {
mCode = ResultCode.VIRUS_DETECTED;
mHttpPhrase = xmlParser.getMessage();
}
}
} catch (Exception e) {
Log_OC.w(TAG, "Error reading exception from server: " + e.getMessage());
// mCode stays as set in this(success, httpCode, headers)
}
}
}
Expand Down

0 comments on commit 712ef8d

Please sign in to comment.