Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 712ef8d3 authored by Tobias Kaminsky's avatar Tobias Kaminsky Committed by GitHub
Browse files

Merge pull request #135 from nextcloud/virusHandling

Parse virus error message
parents 351df3a4 36b6ffa7
Loading
Loading
Loading
Loading
+162 −0
Original line number Diff line number Diff line

/* 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;
    }
}
+0 −146
Original line number Diff line number Diff line

/* 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 for Invalid Character server exception
 * @author masensio
 */
public class InvalidCharacterExceptionParser {

    private static final String EXCEPTION_STRING = "OC\\Connector\\Sabre\\Exception\\InvalidPath";
	private static final String EXCEPTION_UPLOAD_STRING = "OCP\\Files\\InvalidPathException";

    // 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";
    /**
	 * Parse is as an Invalid Path Exception
	 * @param is
	 * @return if The exception is an Invalid Char Exception
	 * @throws XmlPullParserException
	 * @throws IOException
	 */
	public boolean parseXMLResponse(InputStream is) throws XmlPullParserException,
            IOException {
        boolean result = false;

		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();
			result = readError(parser);

		} finally {
			is.close();
		}
		return result;
	}

	/**
	 * Parse OCS node
	 * @param parser
	 * @return List of ShareRemoteFiles
	 * @throws XmlPullParserException
	 * @throws IOException
	 */
	private boolean readError (XmlPullParser parser) throws XmlPullParserException, IOException {
		String exception = "";
		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();
			// read NODE_EXCEPTION
			if (name.equalsIgnoreCase(NODE_EXCEPTION)) {
				exception = readText(parser);
			} else {
				skip(parser);
			}

		}
		return exception.equalsIgnoreCase(EXCEPTION_STRING) ||
				exception.equalsIgnoreCase(EXCEPTION_UPLOAD_STRING);


	}

	/**
	 * 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;
	}
}
+25 −29
Original line number Diff line number Diff line
@@ -128,7 +128,8 @@ public class RemoteOperationResult implements Serializable {
        OLD_ANDROID_API,
        UNTRUSTED_DOMAIN,
        ETAG_CHANGED,
        ETAG_UNCHANGED
        ETAG_UNCHANGED,
        VIRUS_DETECTED
    }

    private boolean mSuccess = false;
@@ -223,17 +224,15 @@ public class RemoteOperationResult implements Serializable {

        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);
@@ -241,8 +240,7 @@ public class RemoteOperationResult implements Serializable {
                    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);
            }
        }
    }
@@ -319,32 +317,30 @@ public class RemoteOperationResult implements Serializable {
     *                   result.
     */
    public RemoteOperationResult(boolean success, HttpMethod httpMethod) throws IOException {
        this(
                success,
                httpMethod.getStatusCode(),
                httpMethod.getStatusText(),
                httpMethod.getResponseHeaders()
        );

        if (mHttpCode == HttpStatus.SC_BAD_REQUEST) {   // 400
        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();
            // 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)) {
                    ExceptionParser xmlParser = new ExceptionParser(is);

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

                    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)
            }
        }
    }
    }

    /**
     * Public constructor from separate elements of an HTTP or DAV response.