Search in sources :

Example 61 with Resource

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

the class CssLinkResourceTransformerTests method createTempCopy.

private void createTempCopy(String filePath, String copyFilePath) throws IOException {
    Resource location = new ClassPathResource("test/", CssLinkResourceTransformerTests.class);
    Path original = Paths.get(location.getFile().getAbsolutePath(), filePath);
    Path copy = Paths.get(location.getFile().getAbsolutePath(), copyFilePath);
    Files.deleteIfExists(copy);
    Files.copy(original, copy);
    copy.toFile().deleteOnExit();
}
Also used : Path(java.nio.file.Path) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource)

Example 62 with Resource

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

the class GzipResourceResolverTests method resolveFingerprintedGzippedFile.

@Test
public void resolveFingerprintedGzippedFile() throws IOException {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addHeader("Accept-Encoding", "gzip");
    String file = "foo-e36d2e05253c6c7085a91522ce43a0b4.css";
    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);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ClassPathResource(org.springframework.core.io.ClassPathResource) FileSystemResource(org.springframework.core.io.FileSystemResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 63 with Resource

use of org.springframework.core.io.Resource 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);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ClassPathResource(org.springframework.core.io.ClassPathResource) FileSystemResource(org.springframework.core.io.FileSystemResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 64 with Resource

use of org.springframework.core.io.Resource 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();
}
Also used : Path(java.nio.file.Path) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) ClassPathResource(org.springframework.core.io.ClassPathResource) FileSystemResource(org.springframework.core.io.FileSystemResource) Resource(org.springframework.core.io.Resource) FileSystemResource(org.springframework.core.io.FileSystemResource) File(java.io.File) ClassPathResource(org.springframework.core.io.ClassPathResource)

Example 65 with Resource

use of org.springframework.core.io.Resource 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());
}
Also used : UrlResource(org.springframework.core.io.UrlResource) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) MockServletContext(org.springframework.mock.web.test.MockServletContext) 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