Deprecated as of 4.3.13, in favor of selective use of ClassPathResource.ClassPathResource(String, ClassLoader) vs ClassPathResource.ClassPathResource(String, Class)
private boolean isResourceUnderLocation(Resource resource, Resource location) throws IOException { if (resource.getClass() != location.getClass()) { return false; } String resourcePath; String locationPath; if (resource instanceof UrlResource) { resourcePath = resource.getURL().toExternalForm(); locationPath = StringUtils.cleanPath(location.getURL().toString()); } else if (resource instanceof ClassPathResource) { resourcePath = ((ClassPathResource) resource).getPath(); locationPath = StringUtils.cleanPath(((ClassPathResource) location).getPath()); } else { resourcePath = resource.getURL().getPath(); locationPath = StringUtils.cleanPath(location.getURL().getPath()); } if (locationPath.equals(resourcePath)) { return true; } locationPath = (locationPath.endsWith("/") || locationPath.isEmpty() ? locationPath : locationPath + "/"); return (resourcePath.startsWith(locationPath) && !isInvalidEncodedPath(resourcePath)); }
private boolean isResourceUnderLocation(Resource resource) throws IOException { if (resource.getClass() != this.location.getClass()) { return false; } String resourcePath; String locationPath; if (resource instanceof UrlResource) { resourcePath = resource.getURL().toExternalForm(); locationPath = StringUtils.cleanPath(this.location.getURL().toString()); } else if (resource instanceof ClassPathResource) { resourcePath = ((ClassPathResource) resource).getPath(); locationPath = StringUtils.cleanPath(((ClassPathResource) this.location).getPath()); } else { resourcePath = resource.getURL().getPath(); locationPath = StringUtils.cleanPath(this.location.getURL().getPath()); } if (locationPath.equals(resourcePath)) { return true; } locationPath = (locationPath.endsWith("/") || locationPath.isEmpty() ? locationPath : locationPath + "/"); if (!resourcePath.startsWith(locationPath)) { return false; } if (resourcePath.contains("%") && StringUtils.uriDecode(resourcePath, StandardCharsets.UTF_8).contains("../")) { return false; } return true; }
private boolean isResourceUnderLocation(Resource resource, Resource location) throws IOException { if (resource.getClass() != location.getClass()) { return false; } String resourcePath; String locationPath; if (resource instanceof UrlResource) { resourcePath = resource.getURL().toExternalForm(); locationPath = StringUtils.cleanPath(location.getURL().toString()); } else if (resource instanceof ClassPathResource) { resourcePath = ((ClassPathResource) resource).getPath(); locationPath = StringUtils.cleanPath(((ClassPathResource) location).getPath()); } else if (resource instanceof ServletContextResource) { resourcePath = ((ServletContextResource) resource).getPath(); locationPath = StringUtils.cleanPath(((ServletContextResource) location).getPath()); } else { resourcePath = resource.getURL().getPath(); locationPath = StringUtils.cleanPath(location.getURL().getPath()); } if (locationPath.equals(resourcePath)) { return true; } locationPath = (locationPath.endsWith("/") || locationPath.isEmpty() ? locationPath : locationPath + "/"); return (resourcePath.startsWith(locationPath) && !isInvalidEncodedPath(resourcePath)); }
private boolean isLoadCandidate(Resource resource) { if (resource == null || !resource.exists()) { return false; } if (resource instanceof ClassPathResource) { // A simple package without a '.' may accidentally get loaded as an XML // document if we're not careful. The result of getInputStream() will be // a file list of the package content. We double check here that it's not // actually a package. String path = ((ClassPathResource) resource).getPath(); if (path.indexOf('.') == -1) { try { return Package.getPackage(path) == null; } catch (Exception ex) { // Ignore } } } return true; }
private boolean isResourceUnderLocation(Resource resource, Resource location) throws IOException { if (resource.getClass() != location.getClass()) { return false; } String resourcePath; String locationPath; if (resource instanceof UrlResource) { resourcePath = resource.getURL().toExternalForm(); locationPath = StringUtils.cleanPath(location.getURL().toString()); } else if (resource instanceof ClassPathResource) { resourcePath = ((ClassPathResource) resource).getPath(); locationPath = StringUtils.cleanPath(((ClassPathResource) location).getPath()); } else if (resource instanceof ServletContextResource) { resourcePath = ((ServletContextResource) resource).getPath(); locationPath = StringUtils.cleanPath(((ServletContextResource) location).getPath()); } else { resourcePath = resource.getURL().getPath(); locationPath = StringUtils.cleanPath(location.getURL().getPath()); } if (locationPath.equals(resourcePath)) { return true; } locationPath = (locationPath.endsWith("/") || locationPath.isEmpty() ? locationPath : locationPath + "/"); return (resourcePath.startsWith(locationPath) && !isInvalidEncodedPath(resourcePath)); }
/** * need to ensure that when method is invoked first time , this class is not loaded, * so this class is will be loaded by appClassLoader */ public static void registerXmlBeanDefinationScannerAgent(XmlBeanDefinitionReader reader, Resource resource) { String path; if (resource instanceof ClassPathResource) { path = ((ClassPathResource)resource).getPath(); } else { try { path = convertToClasspathURL(resource.getURL().getPath()); } catch (IOException e) { e.printStackTrace(); LOGGER.error("Cannot get url from resource: {}", resource); return; } } instances.put(path, new XmlBeanDefinationScannerAgent(reader)); }
@Test public void initAllowedLocations() { PathResourceResolver resolver = (PathResourceResolver) this.handler.getResourceResolvers().get(0); Resource[] locations = resolver.getAllowedLocations(); assertEquals(3, locations.length); assertEquals("test/", ((ClassPathResource) locations[0]).getPath()); assertEquals("testalternatepath/", ((ClassPathResource) locations[1]).getPath()); assertEquals("META-INF/resources/webjars/", ((ClassPathResource) locations[2]).getPath()); }
@Test public void initAllowedLocations() { PathResourceResolver resolver = (PathResourceResolver) this.handler.getResourceResolvers().get(0); Resource[] locations = resolver.getAllowedLocations(); assertEquals(3, locations.length); assertEquals("test/", ((ClassPathResource) locations[0]).getPath()); assertEquals("testalternatepath/", ((ClassPathResource) locations[1]).getPath()); assertEquals("META-INF/resources/webjars/", ((ClassPathResource) locations[2]).getPath()); }
@Test public void testFileSystemResourceWithImport() throws URISyntaxException { String file = getClass().getResource(RESOURCE_CONTEXT.getPath()).toURI().getPath(); DefaultListableBeanFactory xbf = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(new FileSystemResource(file)); // comes from "resourceImport.xml" xbf.getBean("resource1", ResourceTestBean.class); // comes from "resource.xml" xbf.getBean("resource2", ResourceTestBean.class); }
@Test public void initAllowedLocationsWithExplicitConfiguration() throws Exception { ClassPathResource location1 = new ClassPathResource("test/", getClass()); ClassPathResource location2 = new ClassPathResource("testalternatepath/", getClass()); PathResourceResolver pathResolver = new PathResourceResolver(); pathResolver.setAllowedLocations(location1); ResourceWebHandler handler = new ResourceWebHandler(); handler.setResourceResolvers(Collections.singletonList(pathResolver)); handler.setLocations(Arrays.asList(location1, location2)); handler.afterPropertiesSet(); Resource[] locations = pathResolver.getAllowedLocations(); assertEquals(1, locations.length); assertEquals("test/", ((ClassPathResource) locations[0]).getPath()); }
@Test public void testUrlResourceWithImport() { URL url = getClass().getResource(RESOURCE_CONTEXT.getPath()); DefaultListableBeanFactory xbf = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(new UrlResource(url)); // comes from "resourceImport.xml" xbf.getBean("resource1", ResourceTestBean.class); // comes from "resource.xml" xbf.getBean("resource2", ResourceTestBean.class); }
@Test public void initAllowedLocationsWithExplicitConfiguration() throws Exception { ClassPathResource location1 = new ClassPathResource("test/", getClass()); ClassPathResource location2 = new ClassPathResource("testalternatepath/", getClass()); PathResourceResolver pathResolver = new PathResourceResolver(); pathResolver.setAllowedLocations(location1); ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler(); handler.setResourceResolvers(Collections.singletonList(pathResolver)); handler.setServletContext(new MockServletContext()); handler.setLocations(Arrays.asList(location1, location2)); handler.afterPropertiesSet(); Resource[] locations = pathResolver.getAllowedLocations(); assertEquals(1, locations.length); assertEquals("test/", ((ClassPathResource) locations[0]).getPath()); }
InputStream inputStream = getClass().getResourceAsStream(REFTYPES_CONTEXT.getPath()); try { reader.loadBeanDefinitions(new InputSource(inputStream));
filePath = ((ContextResource) flowResource).getPathWithinContext(); } else if (flowResource instanceof ClassPathResource) { filePath = ((ClassPathResource) flowResource).getPath(); } else if (flowResource instanceof FileSystemResource) { filePath = truncateFilePath(((FileSystemResource) flowResource).getPath(), basePath);
protected static String getResourceFileName(Resource resource) { String name = resource.getFilename(); if (resource instanceof ClassPathResource) { name = ((ClassPathResource) resource).getPath(); } else { int colonIndex = name.indexOf(':'); if (colonIndex >= 0) { name = name.substring(colonIndex + 1); } } return name; }
@Override public boolean test(String resourceName) { for (ClassPathResource hiddenResource : this.hiddenResources) { if (hiddenResource.getFilename() != null && resourceName.equals(hiddenResource.getPath())) { return true; } } return false; }
static void populateChangeSetResourceInfo(ChangeSet changeSet, Resource file) { changeSet.setFile(file.getFilename()); if (file instanceof ClassPathResource) { changeSet.setResourcePath(((ClassPathResource) file).getPath()); } } }
private ResourceDatabasePopulator createPopulator(ClassPathResource script) { logger.info("Creating database populator using script '{}'", script.getPath()); ResourceDatabasePopulator populator = new ResourceDatabasePopulator(); populator.setSeparator(DB_TYPE_ORACLE.equals(dbType) ? "/" : ";"); populator.setIgnoreFailedDrops(true); populator.setSqlScriptEncoding(UTF_8.name()); populator.addScript(script); return populator; }