Search in sources :

Example 46 with Resource

use of org.springframework.core.io.Resource in project spring-framework by spring-projects.

the class SpringWildcardServletTilesApplicationContext method getResources.

@Override
public Collection<ApplicationResource> getResources(String path) {
    Resource[] resources;
    try {
        resources = this.resolver.getResources(path);
    } catch (IOException ex) {
        ((ServletContext) getContext()).log("Resource retrieval failed for path: " + path, ex);
        return Collections.emptyList();
    }
    if (ObjectUtils.isEmpty(resources)) {
        ((ServletContext) getContext()).log("No resources found for path pattern: " + path);
        return Collections.emptyList();
    }
    Collection<ApplicationResource> resourceList = new ArrayList<>(resources.length);
    for (Resource resource : resources) {
        try {
            URL url = resource.getURL();
            resourceList.add(new URLApplicationResource(url.toExternalForm(), url));
        } catch (IOException ex) {
            // Shouldn't happen with the kind of resources we're using
            throw new IllegalArgumentException("No URL for " + resource, ex);
        }
    }
    return resourceList;
}
Also used : URLApplicationResource(org.apache.tiles.request.locale.URLApplicationResource) ApplicationResource(org.apache.tiles.request.ApplicationResource) URLApplicationResource(org.apache.tiles.request.locale.URLApplicationResource) ApplicationResource(org.apache.tiles.request.ApplicationResource) URLApplicationResource(org.apache.tiles.request.locale.URLApplicationResource) Resource(org.springframework.core.io.Resource) ArrayList(java.util.ArrayList) ServletContext(javax.servlet.ServletContext) IOException(java.io.IOException) URL(java.net.URL)

Example 47 with Resource

use of org.springframework.core.io.Resource in project spring-framework by spring-projects.

the class ServletContextSupportTests method testServletContextResourcePatternResolverWithUnboundedPatternPath.

@Test
public void testServletContextResourcePatternResolverWithUnboundedPatternPath() throws IOException {
    final Set<String> dirs = new HashSet<>();
    dirs.add("/WEB-INF/mydir1/");
    dirs.add("/WEB-INF/mydir2/");
    final Set<String> paths = new HashSet<>();
    paths.add("/WEB-INF/mydir2/context2.xml");
    paths.add("/WEB-INF/mydir2/mydir3/");
    MockServletContext sc = new MockServletContext("classpath:org/springframework/web/context") {

        @Override
        public Set<String> getResourcePaths(String path) {
            if ("/WEB-INF/".equals(path)) {
                return dirs;
            }
            if ("/WEB-INF/mydir1/".equals(path)) {
                return Collections.singleton("/WEB-INF/mydir1/context1.xml");
            }
            if ("/WEB-INF/mydir2/".equals(path)) {
                return paths;
            }
            if ("/WEB-INF/mydir2/mydir3/".equals(path)) {
                return Collections.singleton("/WEB-INF/mydir2/mydir3/context3.xml");
            }
            return null;
        }
    };
    ServletContextResourcePatternResolver rpr = new ServletContextResourcePatternResolver(sc);
    Resource[] found = rpr.getResources("/WEB-INF/**/*.xml");
    Set<String> foundPaths = new HashSet<>();
    for (Resource resource : found) {
        foundPaths.add(((ServletContextResource) resource).getPath());
    }
    assertEquals(3, foundPaths.size());
    assertTrue(foundPaths.contains("/WEB-INF/mydir1/context1.xml"));
    assertTrue(foundPaths.contains("/WEB-INF/mydir2/context2.xml"));
    assertTrue(foundPaths.contains("/WEB-INF/mydir2/mydir3/context3.xml"));
}
Also used : Resource(org.springframework.core.io.Resource) MockServletContext(org.springframework.mock.web.test.MockServletContext) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 48 with Resource

use of org.springframework.core.io.Resource in project spring-framework by spring-projects.

the class TestPathHelper method loadBeanDefinitions.

private void loadBeanDefinitions(String fileName) {
    XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this.appContext);
    Resource resource = new ClassPathResource(fileName, AnnotationDrivenBeanDefinitionParserTests.class);
    reader.loadBeanDefinitions(resource);
    this.appContext.refresh();
}
Also used : XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource)

Example 49 with Resource

use of org.springframework.core.io.Resource in project spring-framework by spring-projects.

the class AppCacheManifestTransformerTests method transformManifest.

@Test
public void transformManifest() throws Exception {
    this.request = new MockHttpServletRequest("GET", "/static/test.appcache");
    Resource resource = new ClassPathResource("test/test.appcache", getClass());
    Resource result = this.transformer.transform(this.request, resource, this.chain);
    byte[] bytes = FileCopyUtils.copyToByteArray(result.getInputStream());
    String content = new String(bytes, "UTF-8");
    assertThat("should rewrite resource links", content, Matchers.containsString("/static/foo-e36d2e05253c6c7085a91522ce43a0b4.css"));
    assertThat("should rewrite resource links", content, Matchers.containsString("/static/bar-11e16cf79faee7ac698c805cf28248d2.css"));
    assertThat("should rewrite resource links", content, Matchers.containsString("/static/js/bar-bd508c62235b832d960298ca6c0b7645.js"));
    assertThat("should not rewrite external resources", content, Matchers.containsString("//example.org/style.css"));
    assertThat("should not rewrite external resources", content, Matchers.containsString("http://example.org/image.png"));
    assertThat("should generate fingerprint", content, Matchers.containsString("# Hash: 4bf0338bcbeb0a5b3a4ec9ed8864107d"));
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 50 with Resource

use of org.springframework.core.io.Resource in project spring-framework by spring-projects.

the class PathResourceResolverTests method resolveFromClasspath.

@Test
public void resolveFromClasspath() throws IOException {
    Resource location = new ClassPathResource("test/", PathResourceResolver.class);
    String requestPath = "bar.css";
    Resource actual = this.resolver.resolveResource(null, requestPath, Arrays.asList(location), null);
    assertEquals(location.createRelative(requestPath), actual);
}
Also used : UrlResource(org.springframework.core.io.UrlResource) ClassPathResource(org.springframework.core.io.ClassPathResource) ServletContextResource(org.springframework.web.context.support.ServletContextResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Aggregations

Resource (org.springframework.core.io.Resource)610 Test (org.junit.Test)257 ClassPathResource (org.springframework.core.io.ClassPathResource)231 IOException (java.io.IOException)103 FileSystemResource (org.springframework.core.io.FileSystemResource)77 UrlResource (org.springframework.core.io.UrlResource)68 File (java.io.File)64 ArrayList (java.util.ArrayList)58 ByteArrayResource (org.springframework.core.io.ByteArrayResource)49 InputStream (java.io.InputStream)46 InputStreamResource (org.springframework.core.io.InputStreamResource)31 PathMatchingResourcePatternResolver (org.springframework.core.io.support.PathMatchingResourcePatternResolver)30 URL (java.net.URL)25 HashMap (java.util.HashMap)20 MockServerWebExchange (org.springframework.mock.http.server.reactive.test.MockServerWebExchange)18 ServletContextResource (org.springframework.web.context.support.ServletContextResource)18 Map (java.util.Map)17 ResourcePatternResolver (org.springframework.core.io.support.ResourcePatternResolver)17 ResourceLoader (org.springframework.core.io.ResourceLoader)16 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)16