in velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/ImportSupport.java [198:303]
protected Reader acquireRemoteURLReader(String url) throws IOException
{
// remote URL
URLConnection uc = null;
HttpURLConnection huc = null;
InputStream i = null;
try
{
// handle remote URLs ourselves, using java.net.URL
URL u = ConversionUtils.toURL(url);
uc = u.openConnection();
i = uc.getInputStream();
// check response code for HTTP URLs, per spec,
if (uc instanceof HttpURLConnection)
{
huc = (HttpURLConnection)uc;
int status = huc.getResponseCode();
if (status < 200 || status > 299)
{
throw new IOException(status + " " + url);
}
}
// okay, we've got a stream; encode it appropriately
Reader r = null;
String charSet;
// charSet extracted according to RFC 2045, section 5.1
String contentType = uc.getContentType();
if (contentType != null)
{
charSet = ImportSupport.getContentTypeAttribute(contentType, "charset");
if (charSet == null)
{
charSet = RuntimeConstants.ENCODING_DEFAULT;
}
}
else
{
charSet = RuntimeConstants.ENCODING_DEFAULT;
}
try
{
r = new InputStreamReader(i, charSet);
}
catch (UnsupportedEncodingException ueex)
{
r = new InputStreamReader(i, RuntimeConstants.ENCODING_DEFAULT);
}
if (huc == null)
{
return r;
}
else
{
return new SafeClosingHttpURLConnectionReader(r, huc);
}
}
catch (IOException ex)
{
if (i != null)
{
try
{
i.close();
}
catch (IOException ioe)
{
getLog().error("Could not close InputStream", ioe);
}
}
if (huc != null)
{
huc.disconnect();
}
throw new IOException("Problem accessing the remote URL \""
+ url + "\". " + ex);
}
catch (RuntimeException ex)
{
if (i != null)
{
try
{
i.close();
}
catch (IOException ioe)
{
getLog().error("Could not close InputStream", ioe);
}
}
if (huc != null)
{
huc.disconnect();
}
// because the spec makes us
throw new IOException("Problem accessing the remote URL \"" + url + "\" :" + ex.getMessage(), ex);
}
}