use of org.springframework.core.io.UrlResource in project spring-framework by spring-projects.
the class ServletContextResourcePatternResolver method doRetrieveMatchingJarEntries.
/**
* Extract entries from the given jar by pattern.
* @param jarFilePath the path to the jar file
* @param entryPattern the pattern for jar entries to match
* @param result the Set of matching Resources to add to
*/
private void doRetrieveMatchingJarEntries(String jarFilePath, String entryPattern, Set<Resource> result) {
if (logger.isDebugEnabled()) {
logger.debug("Searching jar file [" + jarFilePath + "] for entries matching [" + entryPattern + "]");
}
try {
JarFile jarFile = new JarFile(jarFilePath);
try {
for (Enumeration<JarEntry> entries = jarFile.entries(); entries.hasMoreElements(); ) {
JarEntry entry = entries.nextElement();
String entryPath = entry.getName();
if (getPathMatcher().match(entryPattern, entryPath)) {
result.add(new UrlResource(ResourceUtils.URL_PROTOCOL_JAR, ResourceUtils.FILE_URL_PREFIX + jarFilePath + ResourceUtils.JAR_URL_SEPARATOR + entryPath));
}
}
} finally {
jarFile.close();
}
} catch (IOException ex) {
if (logger.isWarnEnabled()) {
logger.warn("Cannot search for matching resources in jar file [" + jarFilePath + "] because the jar cannot be opened through the file system", ex);
}
}
}
use of org.springframework.core.io.UrlResource in project spring-framework by spring-projects.
the class ResourceHttpRequestHandlerTests method testInvalidPath.
private void testInvalidPath(HttpMethod httpMethod) throws Exception {
this.request.setMethod(httpMethod.name());
Resource location = new ClassPathResource("test/", getClass());
this.handler.setLocations(Collections.singletonList(location));
testInvalidPath(location, "../testsecret/secret.txt");
testInvalidPath(location, "test/../../testsecret/secret.txt");
testInvalidPath(location, ":/../../testsecret/secret.txt");
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(location, "file:" + secretPath);
testInvalidPath(location, "/file:" + secretPath);
testInvalidPath(location, "url:" + secretPath);
testInvalidPath(location, "/url:" + secretPath);
testInvalidPath(location, "/" + secretPath);
testInvalidPath(location, "////../.." + secretPath);
testInvalidPath(location, "/%2E%2E/testsecret/secret.txt");
testInvalidPath(location, "/ " + secretPath);
testInvalidPath(location, "url:" + secretPath);
}
use of org.springframework.core.io.UrlResource in project spring-framework by spring-projects.
the class DefaultListableBeanFactoryTests method testDoubleArrayConstructorWithOptionalAutowiring.
@Test
public void testDoubleArrayConstructorWithOptionalAutowiring() throws MalformedURLException {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
bf.registerSingleton("resource1", new UrlResource("http://localhost:8080"));
bf.registerSingleton("resource2", new UrlResource("http://localhost:9090"));
RootBeanDefinition rbd = new RootBeanDefinition(ArrayBean.class);
rbd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR);
bf.registerBeanDefinition("arrayBean", rbd);
ArrayBean ab = (ArrayBean) bf.getBean("arrayBean");
assertNull(ab.getIntegerArray());
assertNull(ab.getResourceArray());
}
use of org.springframework.core.io.UrlResource in project spring-framework by spring-projects.
the class XmlBeanFactoryTests method testUrlResourceWithImport.
@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);
}
use of org.springframework.core.io.UrlResource in project spring-framework by spring-projects.
the class PathMatchingResourcePatternResolver method findPathMatchingResources.
/**
* Find all resources that match the given location pattern via the
* Ant-style PathMatcher. Supports resources in jar files and zip files
* and in the file system.
* @param locationPattern the location pattern to match
* @return the result as Resource array
* @throws IOException in case of I/O errors
* @see #doFindPathMatchingJarResources
* @see #doFindPathMatchingFileResources
* @see org.springframework.util.PathMatcher
*/
protected Resource[] findPathMatchingResources(String locationPattern) throws IOException {
String rootDirPath = determineRootDir(locationPattern);
String subPattern = locationPattern.substring(rootDirPath.length());
Resource[] rootDirResources = getResources(rootDirPath);
Set<Resource> result = new LinkedHashSet<>(16);
for (Resource rootDirResource : rootDirResources) {
rootDirResource = resolveRootDirResource(rootDirResource);
URL rootDirURL = rootDirResource.getURL();
if (equinoxResolveMethod != null) {
if (rootDirURL.getProtocol().startsWith("bundle")) {
rootDirURL = (URL) ReflectionUtils.invokeMethod(equinoxResolveMethod, null, rootDirURL);
rootDirResource = new UrlResource(rootDirURL);
}
}
if (rootDirURL.getProtocol().startsWith(ResourceUtils.URL_PROTOCOL_VFS)) {
result.addAll(VfsResourceMatchingDelegate.findMatchingResources(rootDirURL, subPattern, getPathMatcher()));
} else if (ResourceUtils.isJarURL(rootDirURL) || isJarResource(rootDirResource)) {
result.addAll(doFindPathMatchingJarResources(rootDirResource, rootDirURL, subPattern));
} else {
result.addAll(doFindPathMatchingFileResources(rootDirResource, subPattern));
}
}
if (logger.isDebugEnabled()) {
logger.debug("Resolved location pattern [" + locationPattern + "] to resources " + result);
}
return result.toArray(new Resource[result.size()]);
}
Aggregations