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;
}
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"));
}
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();
}
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"));
}
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);
}
Aggregations