From 7f19e46d1cc9f229f87fc9ec09cd666d40274cfd Mon Sep 17 00:00:00 2001 From: Pavel Yakunin Date: Sun, 28 Dec 2014 18:15:44 +0300 Subject: [PATCH] Treat 404 from a URL as missing file, not fatal error --- .../com/typesafe/config/impl/Parseable.java | 62 ++++++++++++------- 1 file changed, 38 insertions(+), 24 deletions(-) diff --git a/config/src/main/java/com/typesafe/config/impl/Parseable.java b/config/src/main/java/com/typesafe/config/impl/Parseable.java index 616ffaa1..4132b86d 100644 --- a/config/src/main/java/com/typesafe/config/impl/Parseable.java +++ b/config/src/main/java/com/typesafe/config/impl/Parseable.java @@ -463,32 +463,46 @@ public abstract class Parseable implements ConfigParseable { @Override protected Reader reader(ConfigParseOptions options) throws IOException { - if (ConfigImpl.traceLoadsEnabled()) - trace("Loading config from a URL: " + input.toExternalForm()); - URLConnection connection = input.openConnection(); - - // allow server to serve multiple types from one URL - String acceptContent = acceptContentType(options); - if (acceptContent != null) { - connection.setRequestProperty("Accept", acceptContent); - } - - connection.connect(); - - // save content type for later - contentType = connection.getContentType(); - if (contentType != null) { + try { if (ConfigImpl.traceLoadsEnabled()) - trace("URL sets Content-Type: '" + contentType + "'"); - contentType = contentType.trim(); - int semi = contentType.indexOf(';'); - if (semi >= 0) - contentType = contentType.substring(0, semi); + trace("Loading config from a URL: " + input.toExternalForm()); + URLConnection connection = input.openConnection(); + + // allow server to serve multiple types from one URL + String acceptContent = acceptContentType(options); + if (acceptContent != null) { + connection.setRequestProperty("Accept", acceptContent); + } + + connection.connect(); + + // save content type for later + contentType = connection.getContentType(); + if (contentType != null) { + if (ConfigImpl.traceLoadsEnabled()) + trace("URL sets Content-Type: '" + contentType + "'"); + contentType = contentType.trim(); + int semi = contentType.indexOf(';'); + if (semi >= 0) + contentType = contentType.substring(0, semi); + } + + InputStream stream = connection.getInputStream(); + + return readerFromStream(stream); + } catch (FileNotFoundException fnf) { + // If the resource is not found (HTTP response + // code 404 or something alike), then it's fine to + // treat it according to the allowMissing setting + // and "include" spec. But if we have something + // like HTTP 503 it seems to be better to fail + // early, because this may be a sign of broken + // environment. Java throws FileNotFoundException + // if it sees 404 or 410. + throw fnf; + } catch (IOException e) { + throw new ConfigException.BugOrBroken("Cannot load config from URL: " + input.toExternalForm(), e); } - - InputStream stream = connection.getInputStream(); - - return readerFromStream(stream); } @Override