-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #135 from nextcloud/virusHandling
Parse virus error message
- Loading branch information
Showing
3 changed files
with
187 additions
and
175 deletions.
There are no files selected for viewing
162 changes: 162 additions & 0 deletions
162
src/com/owncloud/android/lib/common/operations/ExceptionParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
146 changes: 0 additions & 146 deletions
146
src/com/owncloud/android/lib/common/operations/InvalidCharacterExceptionParser.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters