use of org.springframework.core.io.ClassPathResource in project spring-framework by spring-projects.
the class GzipResourceResolverTests method resolveGzippedFile.
@Test
public void resolveGzippedFile() throws IOException {
MockHttpServletRequest request = new MockHttpServletRequest();
request.addHeader("Accept-Encoding", "gzip");
String file = "js/foo.js";
Resource resolved = this.resolver.resolveResource(request, file, this.locations);
String gzFile = file + ".gz";
Resource resource = new ClassPathResource("test/" + gzFile, getClass());
assertEquals(resource.getDescription(), resolved.getDescription());
assertEquals(new ClassPathResource("test/" + file).getFilename(), resolved.getFilename());
assertTrue("Expected " + resolved + " to be of type " + HttpResource.class, resolved instanceof HttpResource);
}
use of org.springframework.core.io.ClassPathResource in project spring-framework by spring-projects.
the class GzipResourceResolverTests method createGzFile.
private static void createGzFile(String filePath) throws IOException {
Resource location = new ClassPathResource("test/", GzipResourceResolverTests.class);
Resource fileResource = new FileSystemResource(location.createRelative(filePath).getFile());
Path gzFilePath = Paths.get(fileResource.getFile().getAbsolutePath() + ".gz");
Files.deleteIfExists(gzFilePath);
File gzFile = Files.createFile(gzFilePath).toFile();
GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(gzFile));
FileCopyUtils.copy(fileResource.getInputStream(), out);
gzFile.deleteOnExit();
}
use of org.springframework.core.io.ClassPathResource in project spring-framework by spring-projects.
the class GzipResourceResolverTests method setUp.
@Before
public void setUp() {
this.cache = new ConcurrentMapCache("resourceCache");
Map<String, VersionStrategy> versionStrategyMap = new HashMap<>();
versionStrategyMap.put("/**", new ContentVersionStrategy());
VersionResourceResolver versionResolver = new VersionResourceResolver();
versionResolver.setStrategyMap(versionStrategyMap);
List<ResourceResolver> resolvers = new ArrayList<>();
resolvers.add(new CachingResourceResolver(this.cache));
resolvers.add(new GzipResourceResolver());
resolvers.add(versionResolver);
resolvers.add(new PathResourceResolver());
this.resolver = new DefaultResourceResolverChain(resolvers);
this.locations = new ArrayList<>();
this.locations.add(new ClassPathResource("test/", getClass()));
this.locations.add(new ClassPathResource("testalternatepath/", getClass()));
}
use of org.springframework.core.io.ClassPathResource in project spring-framework by spring-projects.
the class ResourceHttpRequestHandlerTests method getResourceWithMediaTypeResolvedThroughServletContext.
// SPR-14368
@Test
public void getResourceWithMediaTypeResolvedThroughServletContext() throws Exception {
MockServletContext servletContext = new MockServletContext() {
@Override
public String getMimeType(String filePath) {
return "foo/bar";
}
@Override
public String getVirtualServerName() {
return null;
}
};
List<Resource> paths = Collections.singletonList(new ClassPathResource("test/", getClass()));
ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
handler.setServletContext(servletContext);
handler.setLocations(paths);
handler.afterPropertiesSet();
this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css");
handler.handleRequest(this.request, this.response);
assertEquals("foo/bar", this.response.getContentType());
assertEquals("h1 { color:red; }", this.response.getContentAsString());
}
use of org.springframework.core.io.ClassPathResource in project spring-framework by spring-projects.
the class ResourceTransformerSupportTests method setUp.
@Before
public void setUp() {
VersionResourceResolver versionResolver = new VersionResourceResolver();
versionResolver.setStrategyMap(Collections.singletonMap("/**", new ContentVersionStrategy()));
PathResourceResolver pathResolver = new PathResourceResolver();
pathResolver.setAllowedLocations(new ClassPathResource("test/", getClass()));
List<ResourceResolver> resolvers = Arrays.asList(versionResolver, pathResolver);
this.transformerChain = new DefaultResourceTransformerChain(new DefaultResourceResolverChain(resolvers), null);
this.transformer = new TestResourceTransformerSupport();
this.transformer.setResourceUrlProvider(createResourceUrlProvider(resolvers));
this.request = new MockHttpServletRequest("GET", "");
}
Aggregations