Search in sources :

Example 71 with Resource

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

the class VersionResourceResolverTests method resolveResourceNoResourceAfterVersionRemoved.

@Test
public void resolveResourceNoResourceAfterVersionRemoved() throws Exception {
    String versionFile = "bar-version.css";
    String version = "version";
    String file = "bar.css";
    given(this.chain.resolveResource(null, versionFile, this.locations)).willReturn(null);
    given(this.chain.resolveResource(null, file, this.locations)).willReturn(null);
    given(this.versionStrategy.extractVersion(versionFile)).willReturn(version);
    given(this.versionStrategy.removeVersion(versionFile, version)).willReturn(file);
    this.resolver.setStrategyMap(Collections.singletonMap("/**", this.versionStrategy));
    Resource actual = this.resolver.resolveResourceInternal(null, versionFile, this.locations, this.chain);
    assertNull(actual);
    verify(this.versionStrategy, times(1)).removeVersion(versionFile, version);
}
Also used : ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) Test(org.junit.Test)

Example 72 with Resource

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

the class WebJarsResourceResolverTests method resolveResourceWebJar.

@Test
public void resolveResourceWebJar() {
    Resource expected = mock(Resource.class);
    String file = "underscorejs/underscore.js";
    String expectedPath = "underscorejs/1.8.3/underscore.js";
    this.locations = Collections.singletonList(new ClassPathResource("/META-INF/resources/webjars/", getClass()));
    given(this.chain.resolveResource(this.request, expectedPath, this.locations)).willReturn(expected);
    Resource actual = this.resolver.resolveResource(this.request, file, this.locations, this.chain);
    assertEquals(expected, actual);
    verify(this.chain, times(1)).resolveResource(this.request, file, this.locations);
}
Also used : ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 73 with Resource

use of org.springframework.core.io.Resource in project camel by apache.

the class RoutesCollector method loadXmlRests.

private void loadXmlRests(ApplicationContext applicationContext, CamelContext camelContext, String directory) {
    LOG.info("Loading additional Camel XML rests from: {}", directory);
    try {
        final Resource[] xmlRests = applicationContext.getResources(directory);
        for (final Resource xmlRest : xmlRests) {
            final RestsDefinition xmlDefinitions = camelContext.loadRestsDefinition(xmlRest.getInputStream());
            camelContext.addRestDefinitions(xmlDefinitions.getRests());
            for (final RestDefinition xmlDefinition : xmlDefinitions.getRests()) {
                final List<RouteDefinition> routeDefinitions = xmlDefinition.asRouteDefinition(camelContext);
                camelContext.addRouteDefinitions(routeDefinitions);
            }
        }
    } catch (FileNotFoundException e) {
        LOG.debug("No XML rests found in {}. Skipping XML rests detection.", directory);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : RestDefinition(org.apache.camel.model.rest.RestDefinition) RouteDefinition(org.apache.camel.model.RouteDefinition) Resource(org.springframework.core.io.Resource) RestsDefinition(org.apache.camel.model.rest.RestsDefinition) FileNotFoundException(java.io.FileNotFoundException) FileNotFoundException(java.io.FileNotFoundException)

Example 74 with Resource

use of org.springframework.core.io.Resource in project camel by apache.

the class RoutesCollector method loadXmlRoutes.

// Helpers
private void loadXmlRoutes(ApplicationContext applicationContext, CamelContext camelContext, String directory) throws Exception {
    LOG.info("Loading additional Camel XML routes from: {}", directory);
    try {
        Resource[] xmlRoutes = applicationContext.getResources(directory);
        for (Resource xmlRoute : xmlRoutes) {
            LOG.debug("Found XML route: {}", xmlRoute);
            RoutesDefinition xmlDefinition = camelContext.loadRoutesDefinition(xmlRoute.getInputStream());
            camelContext.addRouteDefinitions(xmlDefinition.getRoutes());
        }
    } catch (FileNotFoundException e) {
        LOG.debug("No XML routes found in {}. Skipping XML routes detection.", directory);
    }
}
Also used : Resource(org.springframework.core.io.Resource) RoutesDefinition(org.apache.camel.model.RoutesDefinition) FileNotFoundException(java.io.FileNotFoundException)

Example 75 with Resource

use of org.springframework.core.io.Resource in project spring-boot-admin by codecentric.

the class ConcatenatingResourceResolverTest method test_concatenation.

@Test
public void test_concatenation() throws IOException {
    Resource testResource = new ClassPathResource("/testResource.txt");
    List<Resource> resources = asList(testResource, testResource, testResource);
    Resource resolvedResource = new ConcatenatingResourceResolver(";".getBytes()).resolveResource(null, "/foo.txt", resources, null);
    assertThat(resolvedResource.getFilename(), is("foo.txt"));
    assertThat(resolvedResource.lastModified(), is(testResource.lastModified()));
    assertThat(resolvedResource.getDescription(), is("Byte array resource [(class path resource [testResource.txt], class path resource [testResource.txt], class path resource [testResource.txt])]"));
    assertThat(copyToByteArray(resolvedResource.getInputStream()), is("Foobar;Foobar;Foobar".getBytes()));
}
Also used : ClassPathResource(org.springframework.core.io.ClassPathResource) 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