Search in sources :

Example 51 with Resource

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

the class PathResourceResolverTests method checkResource.

@Test
public void checkResource() throws IOException {
    Resource location = new ClassPathResource("test/", PathResourceResolver.class);
    testCheckResource(location, "../testsecret/secret.txt");
    testCheckResource(location, "test/../../testsecret/secret.txt");
    location = new UrlResource(getClass().getResource("./test/"));
    String secretPath = new UrlResource(getClass().getResource("testsecret/secret.txt")).getURL().getPath();
    testCheckResource(location, "file:" + secretPath);
    testCheckResource(location, "/file:" + secretPath);
    testCheckResource(location, "/" + secretPath);
    testCheckResource(location, "////../.." + secretPath);
    testCheckResource(location, "/%2E%2E/testsecret/secret.txt");
    testCheckResource(location, "/%2e%2e/testsecret/secret.txt");
    testCheckResource(location, " " + secretPath);
    testCheckResource(location, "/  " + secretPath);
    testCheckResource(location, "url:" + secretPath);
}
Also used : UrlResource(org.springframework.core.io.UrlResource) 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)

Example 52 with Resource

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

the class PathResourceResolverTests method testCheckResource.

private void testCheckResource(Resource location, String requestPath) throws IOException {
    Resource actual = this.resolver.resolveResource(null, requestPath, Arrays.asList(location), null);
    if (!location.createRelative(requestPath).exists() && !requestPath.contains(":")) {
        fail(requestPath + " doesn't actually exist as a relative path");
    }
    assertNull(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)

Example 53 with Resource

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

the class PathResourceResolverTests method checkFileLocation.

// SPR-12747
@Test
public void checkFileLocation() throws Exception {
    Resource resource = new ClassPathResource("test/main.css", PathResourceResolver.class);
    assertTrue(this.resolver.checkResource(resource, resource));
}
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)

Example 54 with Resource

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

the class ResourceHttpRequestHandlerTests method getMediaTypeWithFavorPathExtensionOff.

// SPR-14577
@Test
public void getMediaTypeWithFavorPathExtensionOff() throws Exception {
    ContentNegotiationManagerFactoryBean factory = new ContentNegotiationManagerFactoryBean();
    factory.setFavorPathExtension(false);
    factory.afterPropertiesSet();
    ContentNegotiationManager manager = factory.getObject();
    List<Resource> paths = Collections.singletonList(new ClassPathResource("test/", getClass()));
    ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
    handler.setServletContext(new MockServletContext());
    handler.setLocations(paths);
    handler.setContentNegotiationManager(manager);
    handler.afterPropertiesSet();
    this.request.addHeader("Accept", "application/json,text/plain,*/*");
    this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.html");
    handler.handleRequest(this.request, this.response);
    assertEquals("text/html", this.response.getContentType());
}
Also used : ContentNegotiationManagerFactoryBean(org.springframework.web.accept.ContentNegotiationManagerFactoryBean) ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) UrlResource(org.springframework.core.io.UrlResource) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Example 55 with Resource

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

the class ResourceHttpRequestHandlerTests method setUp.

@Before
public void setUp() throws Exception {
    dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
    dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
    List<Resource> paths = new ArrayList<>(2);
    paths.add(new ClassPathResource("test/", getClass()));
    paths.add(new ClassPathResource("testalternatepath/", getClass()));
    paths.add(new ClassPathResource("META-INF/resources/webjars/"));
    this.handler = new ResourceHttpRequestHandler();
    this.handler.setLocations(paths);
    this.handler.setCacheSeconds(3600);
    this.handler.setServletContext(new TestServletContext());
    this.handler.afterPropertiesSet();
    this.request = new MockHttpServletRequest("GET", "");
    this.response = new MockHttpServletResponse();
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ArrayList(java.util.ArrayList) UrlResource(org.springframework.core.io.UrlResource) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) SimpleDateFormat(java.text.SimpleDateFormat) ClassPathResource(org.springframework.core.io.ClassPathResource) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Before(org.junit.Before)

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