Search in sources :

Example 41 with Resource

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

the class XmlBeanDefinitionReaderTests method withOpenInputStream.

@Test(expected = BeanDefinitionStoreException.class)
public void withOpenInputStream() {
    SimpleBeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry();
    Resource resource = new InputStreamResource(getClass().getResourceAsStream("test.xml"));
    new XmlBeanDefinitionReader(registry).loadBeanDefinitions(resource);
}
Also used : SimpleBeanDefinitionRegistry(org.springframework.beans.factory.support.SimpleBeanDefinitionRegistry) ClassPathResource(org.springframework.core.io.ClassPathResource) InputStreamResource(org.springframework.core.io.InputStreamResource) Resource(org.springframework.core.io.Resource) InputStreamResource(org.springframework.core.io.InputStreamResource) Test(org.junit.Test)

Example 42 with Resource

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

the class FreeMarkerConfigurationFactory method getTemplateLoaderForPath.

/**
	 * Determine a FreeMarker TemplateLoader for the given path.
	 * <p>Default implementation creates either a FileTemplateLoader or
	 * a SpringTemplateLoader.
	 * @param templateLoaderPath the path to load templates from
	 * @return an appropriate TemplateLoader
	 * @see freemarker.cache.FileTemplateLoader
	 * @see SpringTemplateLoader
	 */
protected TemplateLoader getTemplateLoaderForPath(String templateLoaderPath) {
    if (isPreferFileSystemAccess()) {
        // (for hot detection of template changes, if possible).
        try {
            Resource path = getResourceLoader().getResource(templateLoaderPath);
            // will fail if not resolvable in the file system
            File file = path.getFile();
            if (logger.isDebugEnabled()) {
                logger.debug("Template loader path [" + path + "] resolved to file path [" + file.getAbsolutePath() + "]");
            }
            return new FileTemplateLoader(file);
        } catch (IOException ex) {
            if (logger.isDebugEnabled()) {
                logger.debug("Cannot resolve template loader path [" + templateLoaderPath + "] to [java.io.File]: using SpringTemplateLoader as fallback", ex);
            }
            return new SpringTemplateLoader(getResourceLoader(), templateLoaderPath);
        }
    } else {
        // Always load via SpringTemplateLoader (without hot detection of template changes).
        logger.debug("File system access not preferred: using SpringTemplateLoader");
        return new SpringTemplateLoader(getResourceLoader(), templateLoaderPath);
    }
}
Also used : Resource(org.springframework.core.io.Resource) IOException(java.io.IOException) FileTemplateLoader(freemarker.cache.FileTemplateLoader) File(java.io.File)

Example 43 with Resource

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

the class ResourceHttpRequestHandler method getResource.

protected Resource getResource(HttpServletRequest request) throws IOException {
    String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
    if (path == null) {
        throw new IllegalStateException("Required request attribute '" + HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE + "' is not set");
    }
    path = processPath(path);
    if (!StringUtils.hasText(path) || isInvalidPath(path)) {
        if (logger.isTraceEnabled()) {
            logger.trace("Ignoring invalid resource path [" + path + "]");
        }
        return null;
    }
    if (path.contains("%")) {
        try {
            // Use URLDecoder (vs UriUtils) to preserve potentially decoded UTF-8 chars
            if (isInvalidPath(URLDecoder.decode(path, "UTF-8"))) {
                if (logger.isTraceEnabled()) {
                    logger.trace("Ignoring invalid resource path with escape sequences [" + path + "].");
                }
                return null;
            }
        } catch (IllegalArgumentException ex) {
        // ignore
        }
    }
    ResourceResolverChain resolveChain = new DefaultResourceResolverChain(getResourceResolvers());
    Resource resource = resolveChain.resolveResource(request, path, getLocations());
    if (resource == null || getResourceTransformers().isEmpty()) {
        return resource;
    }
    ResourceTransformerChain transformChain = new DefaultResourceTransformerChain(resolveChain, getResourceTransformers());
    resource = transformChain.transform(request, resource);
    return resource;
}
Also used : Resource(org.springframework.core.io.Resource)

Example 44 with Resource

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

the class VersionResourceResolver method resolveUrlPathInternal.

@Override
protected String resolveUrlPathInternal(String resourceUrlPath, List<? extends Resource> locations, ResourceResolverChain chain) {
    String baseUrl = chain.resolveUrlPath(resourceUrlPath, locations);
    if (StringUtils.hasText(baseUrl)) {
        VersionStrategy versionStrategy = getStrategyForPath(resourceUrlPath);
        if (versionStrategy == null) {
            return null;
        }
        if (logger.isTraceEnabled()) {
            logger.trace("Getting the original resource to determine version for path \"" + resourceUrlPath + "\"");
        }
        Resource resource = chain.resolveResource(null, baseUrl, locations);
        String version = versionStrategy.getResourceVersion(resource);
        if (logger.isTraceEnabled()) {
            logger.trace("Determined version [" + version + "] for " + resource);
        }
        return versionStrategy.addVersion(baseUrl, version);
    }
    return baseUrl;
}
Also used : AbstractResource(org.springframework.core.io.AbstractResource) Resource(org.springframework.core.io.Resource)

Example 45 with Resource

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

the class CachingResourceResolver method resolveResourceInternal.

@Override
protected Resource resolveResourceInternal(HttpServletRequest request, String requestPath, List<? extends Resource> locations, ResourceResolverChain chain) {
    String key = computeKey(request, requestPath);
    Resource resource = this.cache.get(key, Resource.class);
    if (resource != null) {
        if (logger.isTraceEnabled()) {
            logger.trace("Found match: " + resource);
        }
        return resource;
    }
    resource = chain.resolveResource(request, requestPath, locations);
    if (resource != null) {
        if (logger.isTraceEnabled()) {
            logger.trace("Putting resolved resource in cache: " + resource);
        }
        this.cache.put(key, resource);
    }
    return resource;
}
Also used : Resource(org.springframework.core.io.Resource)

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