Search in sources :

Example 46 with UrlResource

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);
        }
    }
}
Also used : UrlResource(org.springframework.core.io.UrlResource) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry)

Example 47 with UrlResource

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);
}
Also used : UrlResource(org.springframework.core.io.UrlResource) UrlResource(org.springframework.core.io.UrlResource) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource)

Example 48 with UrlResource

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());
}
Also used : UrlResource(org.springframework.core.io.UrlResource) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Example 49 with UrlResource

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);
}
Also used : UrlResource(org.springframework.core.io.UrlResource) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) URL(java.net.URL) Test(org.junit.Test)

Example 50 with UrlResource

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()]);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) UrlResource(org.springframework.core.io.UrlResource) UrlResource(org.springframework.core.io.UrlResource) VfsResource(org.springframework.core.io.VfsResource) Resource(org.springframework.core.io.Resource) FileSystemResource(org.springframework.core.io.FileSystemResource) URL(java.net.URL)

Aggregations

UrlResource (org.springframework.core.io.UrlResource)100 Resource (org.springframework.core.io.Resource)41 URL (java.net.URL)33 Test (org.junit.jupiter.api.Test)30 Test (org.junit.Test)24 ClassPathResource (org.springframework.core.io.ClassPathResource)19 IOException (java.io.IOException)14 ArrayList (java.util.ArrayList)14 FileSystemResource (org.springframework.core.io.FileSystemResource)13 MalformedURLException (java.net.MalformedURLException)11 Requisition (org.opennms.netmgt.provision.persist.requisition.Requisition)10 lombok.val (lombok.val)9 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)9 GenericBean (org.springframework.beans.testfixture.beans.GenericBean)9 File (java.io.File)8 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)8 InputStream (java.io.InputStream)6 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)6 Properties (java.util.Properties)5 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)5