/** * This implementation creates a {@code UrlResource}, applying the given path * relative to the path of the underlying URL of this resource descriptor. * @see java.net.URL#URL(java.net.URL, String) */ @Override public Resource createRelative(String relativePath) throws MalformedURLException { if (relativePath.startsWith("/")) { relativePath = relativePath.substring(1); } return new UrlResource(new URL(this.url, relativePath)); }
for (URL url : ((URLClassLoader) classLoader).getURLs()) { try { UrlResource jarResource = new UrlResource( ResourceUtils.JAR_URL_PREFIX + url + ResourceUtils.JAR_URL_SEPARATOR); if (jarResource.exists()) { result.add(jarResource);
for (Resource mapperLocation : mapperLocations) { try { if (ResourceUtils.isJarURL(mapperLocation.getURL())) { String key = new UrlResource(ResourceUtils.extractJarFileURL(mapperLocation.getURL())) .getFile().getPath(); fileSet.add(key); if (JAR_MAPPER.get(key) != null) { fileSet.add(mapperLocation.getFile().getPath());
protected File getWorkingDirectory() { if (this.uri.startsWith("file:")) { try { return new UrlResource(StringUtils.cleanPath(this.uri)).getFile(); } catch (Exception e) { throw new IllegalStateException( "Cannot convert uri to file: " + this.uri); } } return this.basedir; }
@Test public void testUrlResource() throws IOException { Resource resource = new UrlResource(getClass().getResource("Resource.class")); doTestResource(resource); assertEquals(new UrlResource(getClass().getResource("Resource.class")), resource); Resource resource2 = new UrlResource("file:core/io/Resource.class"); assertEquals(resource2, new UrlResource("file:core/../core/io/./Resource.class")); assertEquals("test.txt", new UrlResource("file:/dir/test.txt?argh").getFilename()); assertEquals("test.txt", new UrlResource("file:\\dir\\test.txt?argh").getFilename()); assertEquals("test.txt", new UrlResource("file:\\dir/test.txt?argh").getFilename()); }
@Override public Resource loadAsResource(String keyName) { try { URL url = new URL(getBaseUrl() + keyName); Resource resource = new UrlResource(url); if (resource.exists() || resource.isReadable()) { return resource; } else { return null; } } catch (MalformedURLException e) { e.printStackTrace(); return null; } }
public Resource run() { Resource cpr = new ClassPathResource(cfgFile); if (cpr.exists()) { return cpr; } try { //see if it's a URL URL url = new URL(cfgFile); cpr = new UrlResource(url); if (cpr.exists()) { return cpr; } } catch (MalformedURLException e) { //ignore } //try loading it our way URL url = ClassLoaderUtils.getResource(cfgFile, BusApplicationContext.class); if (url != null) { cpr = new UrlResource(url); if (cpr.exists()) { return cpr; } } cpr = new FileSystemResource(cfgFile); if (cpr.exists()) { return cpr; } return null; } });
public static ProcessEngine buildProcessEngine(URL resource) { log.debug("==== BUILDING SPRING APPLICATION CONTEXT AND PROCESS ENGINE ========================================="); ApplicationContext applicationContext = new GenericXmlApplicationContext(new UrlResource(resource)); Map<String, ProcessEngine> beansOfType = applicationContext.getBeansOfType(ProcessEngine.class); if ((beansOfType == null) || (beansOfType.isEmpty())) { throw new ActivitiException("no " + ProcessEngine.class.getName() + " defined in the application context " + resource.toString()); } ProcessEngine processEngine = beansOfType.values().iterator().next(); log.debug("==== SPRING PROCESS ENGINE CREATED =================================================================="); return processEngine; }
/** * Fetch the resource. Designed so that extensions * can decide how the resource should be retrieved. * * @param r the resource which can be {@link URL}, {@link URI}, {@link String} * or {@link AbstractResource} * @return the x 509 cRL * @throws Exception the exception */ protected X509CRL fetchInternal(@NotNull final Object r) throws Exception { Resource rs = null; if (r instanceof URI) { rs = new UrlResource(((URI) r).toURL()); } else if (r instanceof URL) { rs = new UrlResource(((URL) r)); } else if (r instanceof AbstractResource) { rs = (AbstractResource) r; } else if (r instanceof String) { rs = new UrlResource(new URL(r.toString())); } if (rs == null) { throw new IllegalArgumentException("Resource " + r + " could not be identified"); } try (InputStream ins = rs.getInputStream()) { return (X509CRL) CertUtils.getCertificateFactory().generateCRL(ins); } } }
@Test public void testInvalidPath() throws Exception { // Use mock ResourceResolver: i.e. we're only testing upfront validations... Resource resource = mock(Resource.class); when(resource.getFilename()).thenThrow(new AssertionError("Resource should not be resolved")); when(resource.getInputStream()).thenThrow(new AssertionError("Resource should not be resolved")); ResourceResolver resolver = mock(ResourceResolver.class); when(resolver.resolveResource(any(), any(), any(), any())).thenReturn(Mono.just(resource)); ResourceWebHandler handler = new ResourceWebHandler(); handler.setLocations(Collections.singletonList(new ClassPathResource("test/", getClass()))); handler.setResourceResolvers(Collections.singletonList(resolver)); handler.afterPropertiesSet(); testInvalidPath("../testsecret/secret.txt", handler); testInvalidPath("test/../../testsecret/secret.txt", handler); testInvalidPath(":/../../testsecret/secret.txt", handler); Resource location = new UrlResource(getClass().getResource("./test/")); this.handler.setLocations(Collections.singletonList(location)); Resource secretResource = new UrlResource(getClass().getResource("testsecret/secret.txt")); String secretPath = secretResource.getURL().getPath(); testInvalidPath("file:" + secretPath, handler); testInvalidPath("/file:" + secretPath, handler); testInvalidPath("url:" + secretPath, handler); testInvalidPath("/url:" + secretPath, handler); testInvalidPath("/../.." + secretPath, handler); testInvalidPath("/%2E%2E/testsecret/secret.txt", handler); testInvalidPath("/%2E%2E/testsecret/secret.txt", handler); testInvalidPath("%2F%2F%2E%2E%2F%2F%2E%2E" + secretPath, handler); }
public static List<URL> nestedClasses(Archive archive, String... paths) { List<URL> extras = new ArrayList<>(); try { for (String path : paths) { UrlResource classes = new UrlResource(archive.getUrl().toString() + path); if (classes.exists()) { extras.add(classes.getURL()); } } } catch (Exception e) { throw new IllegalStateException("Cannot create urls for resources", e); } return extras; }
/** * Locates a jar (within the classpath) containing the given resource. * * @param loader class loader to use for locating the jar * @param resourceName resource to look for * @return the containing jar */ public static Resource findContainingJar(ClassLoader loader, String resourceName) { String binaryName = resourceName; try { for (Enumeration<URL> urls = loader.getResources(binaryName); urls.hasMoreElements();) { URL url = urls.nextElement(); // remove jar: if ("jar".equals(url.getProtocol())) { return new UrlResource(decode(url.getPath()).replaceAll("!.*$", "")); } } } catch (IOException ex) { throw new IllegalArgumentException("Cannot find jar for class " + resourceName, ex); } return null; }
for (Resource rootDirResource : rootDirResources) { rootDirResource = resolveRootDirResource(rootDirResource); URL rootDirUrl = rootDirResource.getURL(); if (equinoxResolveMethod != null && rootDirUrl.getProtocol().startsWith("bundle")) { URL resolvedUrl = (URL) ReflectionUtils.invokeMethod(equinoxResolveMethod, null, rootDirUrl); if (resolvedUrl != null) { rootDirUrl = resolvedUrl; rootDirResource = new UrlResource(rootDirUrl); if (rootDirUrl.getProtocol().startsWith(ResourceUtils.URL_PROTOCOL_VFS)) { result.addAll(VfsResourceMatchingDelegate.findMatchingResources(rootDirUrl, subPattern, getPathMatcher()));
private void testResolvePathWithTraversal(HttpMethod httpMethod) throws Exception { Resource location = new ClassPathResource("test/", getClass()); this.handler.setLocations(Collections.singletonList(location)); testResolvePathWithTraversal(httpMethod, "../testsecret/secret.txt", location); testResolvePathWithTraversal(httpMethod, "test/../../testsecret/secret.txt", location); testResolvePathWithTraversal(httpMethod, ":/../../testsecret/secret.txt", location); location = new UrlResource(getClass().getResource("./test/")); this.handler.setLocations(Collections.singletonList(location)); Resource secretResource = new UrlResource(getClass().getResource("testsecret/secret.txt")); String secretPath = secretResource.getURL().getPath(); testResolvePathWithTraversal(httpMethod, "file:" + secretPath, location); testResolvePathWithTraversal(httpMethod, "/file:" + secretPath, location); testResolvePathWithTraversal(httpMethod, "url:" + secretPath, location); testResolvePathWithTraversal(httpMethod, "/url:" + secretPath, location); testResolvePathWithTraversal(httpMethod, "////../.." + secretPath, location); testResolvePathWithTraversal(httpMethod, "/%2E%2E/testsecret/secret.txt", location); testResolvePathWithTraversal(httpMethod, "%2F%2F%2E%2E%2F%2Ftestsecret/secret.txt", location); testResolvePathWithTraversal(httpMethod, "url:" + secretPath, location); // The following tests fail with a MalformedURLException on Windows // testResolvePathWithTraversal(location, "/" + secretPath); // testResolvePathWithTraversal(location, "/ " + secretPath); }
@Test public void checkResource() throws IOException { Resource location = new ClassPathResource("test/", PathResourceResolver.class); testCheckResource(location, "../testsecret/secret.txt"); testCheckResource(location, "test/../../testsecret/secret.txt"); location = new UrlResource(getClass().getResource("./test/")); String secretPath = new UrlResource(getClass().getResource("testsecret/secret.txt")).getURL().getPath(); testCheckResource(location, "file:" + secretPath); testCheckResource(location, "/file:" + secretPath); testCheckResource(location, "/" + secretPath); testCheckResource(location, "////../.." + secretPath); testCheckResource(location, "/%2E%2E/testsecret/secret.txt"); testCheckResource(location, "/%2e%2e/testsecret/secret.txt"); testCheckResource(location, " " + secretPath); testCheckResource(location, "/ " + secretPath); testCheckResource(location, "url:" + secretPath); }
@Test // SPR-12624 public void checkRelativeLocation() throws Exception { String locationUrl= new UrlResource(getClass().getResource("./test/")).getURL().toExternalForm(); Resource location = new UrlResource(locationUrl.replace("/springframework","/../org/springframework")); List<Resource> locations = singletonList(location); assertNotNull(this.resolver.resolveResource(null, "main.css", locations, null).block(TIMEOUT)); }
@Test public void testPersistenceUnitRootUrlWithJar() throws Exception { ClassPathResource archive = new ClassPathResource("/org/springframework/orm/jpa/jpa-archive.jar"); String newRoot = "jar:" + archive.getURL().toExternalForm() + "!/META-INF/persist.xml"; Resource insideArchive = new UrlResource(newRoot); // make sure the location actually exists assertTrue(insideArchive.exists()); URL url = PersistenceUnitReader.determinePersistenceUnitRootUrl(insideArchive); assertTrue("the archive location should have been returned", archive.getURL().sameFile(url)); }
@Override protected Resource getResourceByPath(String path) { if (m_configResources != null) for (Resource r : m_configResources) try { URL url = r.getURL(); if (url != null && path.equals(url.toExternalForm())) return r; } catch (IOException e) { // NOPMD // Ignore } try { if (path.startsWith("http:") || path.startsWith("https:")) return new UrlResource(path); if (path.startsWith("mongo:") || path.startsWith("mongos:")) return new MongoDbResource(this, path); if (path.startsWith("classpath:")) return new ClassPathResource(path, Configuration.class); return new FileSystemResource(path); } catch (Throwable t) { LOGGER.error( "Failed to load " + path + ": " + t); throw CommonUtils.runtimeException(t); } }
@Override public Resource loadAsResource(String filename) { try { Path file = load(filename); Resource resource = new UrlResource(file.toUri()); if (resource.exists() || resource.isReadable()) { return resource; } else { return null; } } catch (MalformedURLException e) { e.printStackTrace(); return null; } }
@Ignore @Test // this test is quite slow. TODO: re-enable with JUnit categories public void testNonFileResourceExists() throws Exception { Resource resource = new UrlResource("http://www.springframework.org"); assertTrue(resource.exists()); }