public static String readURIToLocalURI(String uri) throws URISyntaxException, IOException { final PipelineContext pipelineContext = StaticExternalContext.getStaticContext().getPipelineContext(); final URLConnection urlConnection = new URI(uri).toURL().openConnection(); InputStream inputStream = null; try { inputStream = urlConnection.getInputStream(); return inputStreamToAnyURI(pipelineContext, inputStream, REQUEST_SCOPE); } finally { if (inputStream != null) inputStream.close(); } }
/** * Get the last modification date of an open URLConnection. * * <p>This handles the (broken at some point in the Java libraries) case of the file: protocol. * * @return last modified timestamp "as is" */ public static long getLastModified(URLConnection urlConnection) { try { long lastModified = urlConnection.getLastModified(); if (lastModified == 0 && "file".equals(urlConnection.getURL().getProtocol())) lastModified = new File( URLDecoder.decode( urlConnection.getURL().getFile(), STANDARD_PARAMETER_ENCODING)) .lastModified(); return lastModified; } catch (UnsupportedEncodingException e) { // Should not happen as we are using a required encoding throw new OXFException(e); } }
/** * Get the last modification date of a URL. * * @return last modified timestamp "as is" */ public static long getLastModified(URL url) throws IOException { if ("file".equals(url.getProtocol())) { // Optimize file: access. Also, this prevents throwing an exception if the file doesn't exist // as we try to close the stream below. return new File(URLDecoder.decode(url.getFile(), STANDARD_PARAMETER_ENCODING)).lastModified(); } else { // Use URLConnection final URLConnection urlConnection = url.openConnection(); if (urlConnection instanceof HttpURLConnection) ((HttpURLConnection) urlConnection).setRequestMethod("HEAD"); try { return getLastModified(urlConnection); } finally { final InputStream is = urlConnection.getInputStream(); if (is != null) is.close(); } } }