Handle HTTP 404 File Not Found response correctly

This commit is contained in:
Reinhard Pointner 2016-03-28 10:59:11 +00:00
parent c9bbdf5e65
commit 1868e9eb06
2 changed files with 19 additions and 14 deletions

View File

@ -17,6 +17,9 @@ public class JsonUtilities {
public static final Object[] EMPTY_ARRAY = new Object[0]; public static final Object[] EMPTY_ARRAY = new Object[0];
public static Object readJson(CharSequence json) { public static Object readJson(CharSequence json) {
if (json.length() == 0) {
return EMPTY_MAP;
}
return JsonReader.jsonToJava(json.toString(), singletonMap(JsonReader.USE_MAPS, true)); return JsonReader.jsonToJava(json.toString(), singletonMap(JsonReader.USE_MAPS, true));
} }

View File

@ -78,29 +78,28 @@ public final class WebRequest {
return new InputStreamReader(inputStream, charset); 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()); 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))); 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))); return getDocument(new InputSource(new StringReader(xml)));
} }
public static Document getDocument(InputSource source) throws IOException, SAXException { public static Document getDocument(InputSource source) throws Exception {
try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false);
factory.setValidating(false); factory.setFeature("http://xml.org/sax/features/namespaces", false);
factory.setFeature("http://xml.org/sax/features/namespaces", false); factory.setFeature("http://xml.org/sax/features/validation", false);
factory.setFeature("http://xml.org/sax/features/validation", false); return factory.newDocumentBuilder().parse(source);
return factory.newDocumentBuilder().parse(source);
} catch (ParserConfigurationException e) {
// will never happen
throw new RuntimeException(e);
}
} }
public static ByteBuffer fetch(URL resource) throws IOException { 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 { public static void validateXml(String xml) throws SAXException, ParserConfigurationException, IOException {
if (xml.isEmpty())
return;
SAXParserFactory sax = SAXParserFactory.newInstance(); SAXParserFactory sax = SAXParserFactory.newInstance();
sax.setValidating(false); sax.setValidating(false);
sax.setNamespaceAware(false); sax.setNamespaceAware(false);