From 1868e9eb06e0cd2c849bca6f8fcf0d65865e7046 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Mon, 28 Mar 2016 10:59:11 +0000 Subject: [PATCH] Handle HTTP 404 File Not Found response correctly --- source/net/filebot/util/JsonUtilities.java | 3 +++ source/net/filebot/web/WebRequest.java | 30 ++++++++++++---------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/source/net/filebot/util/JsonUtilities.java b/source/net/filebot/util/JsonUtilities.java index 68f7d75f..9546274b 100644 --- a/source/net/filebot/util/JsonUtilities.java +++ b/source/net/filebot/util/JsonUtilities.java @@ -17,6 +17,9 @@ public class JsonUtilities { public static final Object[] EMPTY_ARRAY = new Object[0]; public static Object readJson(CharSequence json) { + if (json.length() == 0) { + return EMPTY_MAP; + } return JsonReader.jsonToJava(json.toString(), singletonMap(JsonReader.USE_MAPS, true)); } diff --git a/source/net/filebot/web/WebRequest.java b/source/net/filebot/web/WebRequest.java index 2499aba3..6930ebec 100644 --- a/source/net/filebot/web/WebRequest.java +++ b/source/net/filebot/web/WebRequest.java @@ -78,29 +78,28 @@ public final class WebRequest { return new InputStreamReader(inputStream, charset); } - public static Document getDocument(URL url) throws IOException, SAXException { + public static Document getDocument(URL url) throws Exception { return getDocument(url.openConnection()); } - public static Document getDocument(URLConnection connection) throws IOException, SAXException { + public static Document getDocument(URLConnection connection) throws Exception { return getDocument(new InputSource(getReader(connection))); } - public static Document getDocument(String xml) throws IOException, SAXException { + public static Document getDocument(String xml) throws Exception { + if (xml.isEmpty()) { + return DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); + } + return getDocument(new InputSource(new StringReader(xml))); } - public static Document getDocument(InputSource source) throws IOException, SAXException { - try { - DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - factory.setValidating(false); - factory.setFeature("http://xml.org/sax/features/namespaces", false); - factory.setFeature("http://xml.org/sax/features/validation", false); - return factory.newDocumentBuilder().parse(source); - } catch (ParserConfigurationException e) { - // will never happen - throw new RuntimeException(e); - } + public static Document getDocument(InputSource source) throws Exception { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + factory.setValidating(false); + factory.setFeature("http://xml.org/sax/features/namespaces", false); + factory.setFeature("http://xml.org/sax/features/validation", false); + return factory.newDocumentBuilder().parse(source); } public static ByteBuffer fetch(URL resource) throws IOException { @@ -311,6 +310,9 @@ public final class WebRequest { } public static void validateXml(String xml) throws SAXException, ParserConfigurationException, IOException { + if (xml.isEmpty()) + return; + SAXParserFactory sax = SAXParserFactory.newInstance(); sax.setValidating(false); sax.setNamespaceAware(false);