Refine search
private InputStream getInputStream(String location) throws IOException { URL url = ResourceUtils.getURL(location); return url.openStream(); }
/** * Return whether the given resource location is a URL: either a * special "classpath" or "classpath*" pseudo URL or a standard URL. * @param resourceLocation the location String to check * @return whether the location qualifies as a URL * @see ResourcePatternResolver#CLASSPATH_ALL_URL_PREFIX * @see org.springframework.util.ResourceUtils#CLASSPATH_URL_PREFIX * @see org.springframework.util.ResourceUtils#isUrl(String) * @see java.net.URL */ public static boolean isUrl(@Nullable String resourceLocation) { return (resourceLocation != null && (resourceLocation.startsWith(ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX) || ResourceUtils.isUrl(resourceLocation))); }
/** * This implementation determines the underlying File * (or jar file, in case of a resource in a jar/zip). */ @Override protected File getFileForLastModifiedCheck() throws IOException { URL url = getURL(); if (ResourceUtils.isJarURL(url)) { URL actualUrl = ResourceUtils.extractArchiveURL(url); if (actualUrl.getProtocol().startsWith(ResourceUtils.URL_PROTOCOL_VFS)) { return VfsResourceDelegate.getResource(actualUrl).getFile(); } return ResourceUtils.getFile(actualUrl, "Jar URL"); } else { return getFile(); } }
/** * Try to determine the persistence unit root URL based on the given * "defaultPersistenceUnitRootLocation". * @return the persistence unit root URL to pass to the JPA PersistenceProvider * @see #setDefaultPersistenceUnitRootLocation */ @Nullable private URL determineDefaultPersistenceUnitRootUrl() { if (this.defaultPersistenceUnitRootLocation == null) { return null; } try { URL url = this.resourcePatternResolver.getResource(this.defaultPersistenceUnitRootLocation).getURL(); return (ResourceUtils.isJarURL(url) ? ResourceUtils.extractJarFileURL(url) : url); } catch (IOException ex) { throw new PersistenceException("Unable to resolve persistence unit root URL", ex); } }
if (ResourceUtils.isJarURL(originalURL)) { return ResourceUtils.extractJarFileURL(originalURL); String urlToString = originalURL.toExternalForm(); if (!urlToString.contains(META_INF)) { if (logger.isInfoEnabled()) { persistenceUnitRoot = persistenceUnitRoot.substring(0, persistenceUnitRoot.length() - 1); return new URL(persistenceUnitRoot);
/** * Extract the URL for the outermost archive from the given jar/war URL * (which may point to a resource in a jar file or to a jar file itself). * <p>In the case of a jar file nested within a war file, this will return * a URL to the war file since that is the one resolvable in the file system. * @param jarUrl the original URL * @return the URL for the actual jar file * @throws MalformedURLException if no valid jar file URL could be extracted * @since 4.1.8 * @see #extractJarFileURL(URL) */ public static URL extractArchiveURL(URL jarUrl) throws MalformedURLException { String urlFile = jarUrl.getFile(); int endIndex = urlFile.indexOf(WAR_URL_SEPARATOR); if (endIndex != -1) { // Tomcat's "war:file:...mywar.war*/WEB-INF/lib/myjar.jar!/myentry.txt" String warFile = urlFile.substring(0, endIndex); if (URL_PROTOCOL_WAR.equals(jarUrl.getProtocol())) { return new URL(warFile); } int startIndex = warFile.indexOf(WAR_URL_PREFIX); if (startIndex != -1) { return new URL(warFile.substring(startIndex + WAR_URL_PREFIX.length())); } } // Regular "jar:file:...myjar.jar!/myentry.txt" return extractJarFileURL(jarUrl); }
public static InputStream getInputStream(URL resourceLocation) throws IOException, URISyntaxException { InputStream result = null; if (FILE_SCHEME.equals(resourceLocation.getProtocol())) { final File f = new File(resourceLocation.toURI()); result = new FileInputStream(f); } else { result = ResourceUtils.getURL(resourceLocation.toString()).openStream(); } return result; }
@Override public Resource getResource(String location) { Assert.notNull(location, "Location must not be null"); for (ProtocolResolver protocolResolver : this.protocolResolvers) { Resource resource = protocolResolver.resolve(location, this); if (resource != null) { return resource; } } if (location.startsWith("/")) { return getResourceByPath(location); } else if (location.startsWith(CLASSPATH_URL_PREFIX)) { return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()), getClassLoader()); } else { try { // Try to parse the location as a URL... URL url = new URL(location); return (ResourceUtils.isFileURL(url) ? new FileUrlResource(url) : new UrlResource(url)); } catch (MalformedURLException ex) { // No URL -> resolve as resource path. return getResourceByPath(location); } } }
@Test public void isJarURL() throws Exception { assertTrue(ResourceUtils.isJarURL(new URL("jar:file:myjar.jar!/mypath"))); assertTrue(ResourceUtils.isJarURL(new URL(null, "zip:file:myjar.jar!/mypath", new DummyURLStreamHandler()))); assertTrue(ResourceUtils.isJarURL(new URL(null, "wsjar:file:myjar.jar!/mypath", new DummyURLStreamHandler()))); assertTrue(ResourceUtils.isJarURL(new URL(null, "jar:war:file:mywar.war*/myjar.jar!/mypath", new DummyURLStreamHandler()))); assertFalse(ResourceUtils.isJarURL(new URL("file:myjar.jar"))); assertFalse(ResourceUtils.isJarURL(new URL("http:myserver/myjar.jar"))); }
private ConfigurationSource getConfigurationSource(URL url) throws IOException { InputStream stream = url.openStream(); if (FILE_PROTOCOL.equals(url.getProtocol())) { return new ConfigurationSource(stream, ResourceUtils.getFile(url)); } return new ConfigurationSource(stream, url); }
@Test public void extractJarFileURL() throws Exception { assertEquals(new URL("file:myjar.jar"), ResourceUtils.extractJarFileURL(new URL("jar:file:myjar.jar!/mypath"))); assertEquals(new URL("file:/myjar.jar"), ResourceUtils.extractJarFileURL(new URL(null, "jar:myjar.jar!/mypath", new DummyURLStreamHandler()))); assertEquals(new URL("file:myjar.jar"), ResourceUtils.extractJarFileURL(new URL(null, "zip:file:myjar.jar!/mypath", new DummyURLStreamHandler()))); assertEquals(new URL("file:myjar.jar"), ResourceUtils.extractJarFileURL(new URL(null, "wsjar:file:myjar.jar!/mypath", new DummyURLStreamHandler()))); assertEquals(new URL("file:myjar.jar"), ResourceUtils.extractJarFileURL(new URL("file:myjar.jar"))); assertEquals(new URL("file:myjar.jar"), ResourceUtils.extractJarFileURL(new URL("jar:file:myjar.jar!/"))); assertEquals(new URL("file:myjar.jar"), ResourceUtils.extractJarFileURL(new URL(null, "zip:file:myjar.jar!/", new DummyURLStreamHandler()))); assertEquals(new URL("file:myjar.jar"), ResourceUtils.extractJarFileURL(new URL(null, "wsjar:file:myjar.jar!/", new DummyURLStreamHandler()))); }
@Override public long contentLength() throws IOException { URL url = getURL(); if (ResourceUtils.isFileURL(url)) { // Proceed with file system resolution File file = getFile(); long length = file.length(); if (length == 0L && !file.exists()) { throw new FileNotFoundException(getDescription() + " cannot be resolved in the file system for checking its content length"); } return length; } else { // Try a URL connection content-length header URLConnection con = url.openConnection(); customizeConnection(con); return con.getContentLengthLong(); } }
try { final URL resourceURL = rulesXmlResource.getURL(); if ("jar".equals(resourceURL.getProtocol()) || "wsjar".equals(resourceURL.getProtocol())) { URL jarUrl = org.springframework.util.ResourceUtils.extractJarFileURL(resourceURL); file = org.springframework.util.ResourceUtils.getFile(jarUrl); } else if ("vfs".equals(rulesXmlResource.getURL().getProtocol())) {
@Override public long lastModified() throws IOException { URL url = getURL(); boolean fileCheck = false; if (ResourceUtils.isFileURL(url) || ResourceUtils.isJarURL(url)) { // Proceed with file system resolution fileCheck = true; try { File fileToCheck = getFileForLastModifiedCheck(); long lastModified = fileToCheck.lastModified(); if (lastModified > 0L || fileToCheck.exists()) { return lastModified; } } catch (FileNotFoundException ex) { // Defensively fall back to URL connection check instead } } // Try a URL connection last-modified header URLConnection con = url.openConnection(); customizeConnection(con); long lastModified = con.getLastModified(); if (fileCheck && lastModified == 0 && con.getContentLengthLong() <= 0) { throw new FileNotFoundException(getDescription() + " cannot be resolved in the file system for checking its last-modified timestamp"); } return lastModified; }
private void addUrl(URL url) { if (url.getProtocol().equals("file") && url.getPath().endsWith("/")) { try { this.folders.add(ResourceUtils.getFile(url)); } catch (Exception ex) { logger.warn("Unable to get classpath URL " + url); logger.trace("Unable to get classpath URL " + url, ex); } } }
private void configureSslKeyStore(AbstractHttp11JsseProtocol<?> protocol, Ssl ssl) { try { protocol.setKeystoreFile(ResourceUtils.getURL(ssl.getKeyStore()).toString()); } catch (FileNotFoundException ex) { throw new WebServerException("Could not load key store: " + ex.getMessage(), ex); } if (ssl.getKeyStoreType() != null) { protocol.setKeystoreType(ssl.getKeyStoreType()); } if (ssl.getKeyStoreProvider() != null) { protocol.setKeystoreProvider(ssl.getKeyStoreProvider()); } }
@Override public long lastModified() throws IOException { URL url = getURL(); if (ResourceUtils.isFileURL(url) || ResourceUtils.isJarURL(url)) { // Proceed with file system resolution... return super.lastModified(); } else { // Try a URL connection last-modified header... URLConnection con = url.openConnection(); ResourceUtils.useCachesIfNecessary(con); if (con instanceof HttpURLConnection) { ((HttpURLConnection) con).setRequestMethod("HEAD"); } return con.getLastModified(); } }
@Override public long contentLength() throws IOException { URL url = getURL(); if (ResourceUtils.isFileURL(url)) { // Proceed with file system resolution... return getFile().length(); } else { // Try a URL connection content-length header... URLConnection con = url.openConnection(); ResourceUtils.useCachesIfNecessary(con); if (con instanceof HttpURLConnection) { ((HttpURLConnection) con).setRequestMethod("HEAD"); } return con.getContentLength(); } }
/** * This implementation returns a File reference for the underlying class path * resource, provided that it refers to a file in the file system. * @see org.springframework.util.ResourceUtils#getFile(java.net.URL, String) */ @Override public File getFile() throws IOException { URL url = getURL(); if (url.getProtocol().startsWith(ResourceUtils.URL_PROTOCOL_VFS)) { return VfsResourceDelegate.getResource(url).getFile(); } return ResourceUtils.getFile(url, getDescription()); }