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();
/** * 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); } } }
public RemoteOperationResult(boolean success, String bodyResponse, int httpCode) { mSuccess = success; mHttpCode = httpCode; if (success) { mCode = ResultCode.OK; } else if (httpCode > 0) { switch (httpCode) { case HttpStatus.SC_BAD_REQUEST: try { 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); } } }
@Test public void testString() throws IOException, XmlPullParserException { String emptyString = ""; InputStream is = new ByteArrayInputStream(emptyString.getBytes()); ExceptionParser xmlParser = new ExceptionParser(is); Assert.assertFalse(xmlParser.isVirusException()); Assert.assertFalse(xmlParser.isInvalidCharacterException()); } }
/** * Parse is as an Invalid Path Exception * * @param is InputStream xml * @return if The exception is an Invalid Char Exception * @throws IOException */ public ExceptionParser(InputStream is) throws 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); } catch (Exception e) { Log_OC.e(TAG, "Error parsing exception: " + e.getMessage()); } finally { is.close(); } }
@Test public void testInvalidCharacterUploadException() throws IOException, XmlPullParserException { String virusException = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<d:error xmlns:d=\"DAV:\" xmlns:s=\"http://sabredav.org/ns\">\n" + " <s:exception>OCP\\Files\\InvalidPathException</s:exception>\n" + " <s:message>Wrong Path</s:message>\n" + "</d:error>"; InputStream is = new ByteArrayInputStream(virusException.getBytes()); ExceptionParser xmlParser = new ExceptionParser(is); Assert.assertTrue(xmlParser.isInvalidCharacterException()); Assert.assertFalse(xmlParser.isVirusException()); }
@Test public void testEmptyString() throws IOException, XmlPullParserException { String emptyString = ""; InputStream is = new ByteArrayInputStream(emptyString.getBytes()); ExceptionParser xmlParser = new ExceptionParser(is); Assert.assertFalse(xmlParser.isVirusException()); Assert.assertFalse(xmlParser.isInvalidCharacterException()); }
@Test public void testInvalidCharacterException() throws IOException, XmlPullParserException { String virusException = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<d:error xmlns:d=\"DAV:\" xmlns:s=\"http://sabredav.org/ns\">\n" + " <s:exception>OC\\Connector\\Sabre\\Exception\\InvalidPath</s:exception>\n" + " <s:message>Wrong Path</s:message>\n" + "</d:error>"; InputStream is = new ByteArrayInputStream(virusException.getBytes()); ExceptionParser xmlParser = new ExceptionParser(is); Assert.assertTrue(xmlParser.isInvalidCharacterException()); Assert.assertFalse(xmlParser.isVirusException()); }
@Test public void testVirusException() throws IOException, XmlPullParserException { String virusException = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<d:error xmlns:d=\"DAV:\" xmlns:s=\"http://sabredav.org/ns\">\n" + " <s:exception>OCA\\DAV\\Connector\\Sabre\\Exception\\UnsupportedMediaType" + "</s:exception>\n" + " <s:message>Virus Eicar-Test-Signature is detected in the file. " + "Upload cannot be completed.</s:message>\n" + "</d:error>"; InputStream is = new ByteArrayInputStream(virusException.getBytes()); ExceptionParser xmlParser = new ExceptionParser(is); Assert.assertTrue(xmlParser.isVirusException()); Assert.assertFalse(xmlParser.isInvalidCharacterException()); }