Search in sources :

Example 21 with Resource

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

the class AbstractBeanDefinitionReader method loadBeanDefinitions.

/**
	 * Load bean definitions from the specified resource location.
	 * <p>The location can also be a location pattern, provided that the
	 * ResourceLoader of this bean definition reader is a ResourcePatternResolver.
	 * @param location the resource location, to be loaded with the ResourceLoader
	 * (or ResourcePatternResolver) of this bean definition reader
	 * @param actualResources a Set to be filled with the actual Resource objects
	 * that have been resolved during the loading process. May be {@code null}
	 * to indicate that the caller is not interested in those Resource objects.
	 * @return the number of bean definitions found
	 * @throws BeanDefinitionStoreException in case of loading or parsing errors
	 * @see #getResourceLoader()
	 * @see #loadBeanDefinitions(org.springframework.core.io.Resource)
	 * @see #loadBeanDefinitions(org.springframework.core.io.Resource[])
	 */
public int loadBeanDefinitions(String location, Set<Resource> actualResources) throws BeanDefinitionStoreException {
    ResourceLoader resourceLoader = getResourceLoader();
    if (resourceLoader == null) {
        throw new BeanDefinitionStoreException("Cannot import bean definitions from location [" + location + "]: no ResourceLoader available");
    }
    if (resourceLoader instanceof ResourcePatternResolver) {
        // Resource pattern matching available.
        try {
            Resource[] resources = ((ResourcePatternResolver) resourceLoader).getResources(location);
            int loadCount = loadBeanDefinitions(resources);
            if (actualResources != null) {
                for (Resource resource : resources) {
                    actualResources.add(resource);
                }
            }
            if (logger.isDebugEnabled()) {
                logger.debug("Loaded " + loadCount + " bean definitions from location pattern [" + location + "]");
            }
            return loadCount;
        } catch (IOException ex) {
            throw new BeanDefinitionStoreException("Could not resolve bean definition resource pattern [" + location + "]", ex);
        }
    } else {
        // Can only load single resources by absolute URL.
        Resource resource = resourceLoader.getResource(location);
        int loadCount = loadBeanDefinitions(resource);
        if (actualResources != null) {
            actualResources.add(resource);
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Loaded " + loadCount + " bean definitions from location [" + location + "]");
        }
        return loadCount;
    }
}
Also used : ResourceLoader(org.springframework.core.io.ResourceLoader) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) ResourcePatternResolver(org.springframework.core.io.support.ResourcePatternResolver) BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException) Resource(org.springframework.core.io.Resource) IOException(java.io.IOException)

Example 22 with Resource

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

the class FileEditor method setAsText.

@Override
public void setAsText(String text) throws IllegalArgumentException {
    if (!StringUtils.hasText(text)) {
        setValue(null);
        return;
    }
    // Check whether we got an absolute file path without "file:" prefix.
    // For backwards compatibility, we'll consider those as straight file path.
    File file = null;
    if (!ResourceUtils.isUrl(text)) {
        file = new File(text);
        if (file.isAbsolute()) {
            setValue(file);
            return;
        }
    }
    // Proceed with standard resource location parsing.
    this.resourceEditor.setAsText(text);
    Resource resource = (Resource) this.resourceEditor.getValue();
    // If it's a URL or a path pointing to an existing resource, use it as-is.
    if (file == null || resource.exists()) {
        try {
            setValue(resource.getFile());
        } catch (IOException ex) {
            throw new IllegalArgumentException("Could not retrieve file for " + resource + ": " + ex.getMessage());
        }
    } else {
        // Set a relative File reference and hope for the best.
        setValue(file);
    }
}
Also used : Resource(org.springframework.core.io.Resource) IOException(java.io.IOException) File(java.io.File)

Example 23 with Resource

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

the class InputSourceEditor method setAsText.

@Override
public void setAsText(String text) throws IllegalArgumentException {
    this.resourceEditor.setAsText(text);
    Resource resource = (Resource) this.resourceEditor.getValue();
    try {
        setValue(resource != null ? new InputSource(resource.getURL().toString()) : null);
    } catch (IOException ex) {
        throw new IllegalArgumentException("Could not retrieve URL for " + resource + ": " + ex.getMessage());
    }
}
Also used : InputSource(org.xml.sax.InputSource) Resource(org.springframework.core.io.Resource) IOException(java.io.IOException)

Example 24 with Resource

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

the class BeansDtdResolver method resolveEntity.

@Override
public InputSource resolveEntity(String publicId, String systemId) throws IOException {
    if (logger.isTraceEnabled()) {
        logger.trace("Trying to resolve XML entity with public ID [" + publicId + "] and system ID [" + systemId + "]");
    }
    if (systemId != null && systemId.endsWith(DTD_EXTENSION)) {
        int lastPathSeparator = systemId.lastIndexOf("/");
        int dtdNameStart = systemId.indexOf(DTD_NAME, lastPathSeparator);
        if (dtdNameStart != -1) {
            String dtdFile = DTD_NAME + DTD_EXTENSION;
            if (logger.isTraceEnabled()) {
                logger.trace("Trying to locate [" + dtdFile + "] in Spring jar on classpath");
            }
            try {
                Resource resource = new ClassPathResource(dtdFile, getClass());
                InputSource source = new InputSource(resource.getInputStream());
                source.setPublicId(publicId);
                source.setSystemId(systemId);
                if (logger.isDebugEnabled()) {
                    logger.debug("Found beans DTD [" + systemId + "] in classpath: " + dtdFile);
                }
                return source;
            } catch (IOException ex) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Could not resolve beans DTD [" + systemId + "]: not found in classpath", ex);
                }
            }
        }
    }
    // Use the default behavior -> download from website or wherever.
    return null;
}
Also used : InputSource(org.xml.sax.InputSource) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) IOException(java.io.IOException) ClassPathResource(org.springframework.core.io.ClassPathResource)

Example 25 with Resource

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

the class MimeMessageHelper method addInline.

/**
	 * Add an inline element to the MimeMessage, taking the content from an
	 * {@code org.springframework.core.InputStreamResource}, and
	 * specifying the content type explicitly.
	 * <p>You can determine the content type for any given filename via a Java
	 * Activation Framework's FileTypeMap, for example the one held by this helper.
	 * <p>Note that the InputStream returned by the InputStreamSource implementation
	 * needs to be a <i>fresh one on each call</i>, as JavaMail will invoke
	 * {@code getInputStream()} multiple times.
	 * <p><b>NOTE:</b> Invoke {@code addInline} <i>after</i> {@code setText};
	 * else, mail readers might not be able to resolve inline references correctly.
	 * @param contentId the content ID to use. Will end up as "Content-ID" header
	 * in the body part, surrounded by angle brackets: e.g. "myId" -> "&lt;myId&gt;".
	 * Can be referenced in HTML source via src="cid:myId" expressions.
	 * @param inputStreamSource the resource to take the content from
	 * @param contentType the content type to use for the element
	 * @throws MessagingException in case of errors
	 * @see #setText
	 * @see #getFileTypeMap
	 * @see #addInline(String, org.springframework.core.io.Resource)
	 * @see #addInline(String, javax.activation.DataSource)
	 */
public void addInline(String contentId, InputStreamSource inputStreamSource, String contentType) throws MessagingException {
    Assert.notNull(inputStreamSource, "InputStreamSource must not be null");
    if (inputStreamSource instanceof Resource && ((Resource) inputStreamSource).isOpen()) {
        throw new IllegalArgumentException("Passed-in Resource contains an open stream: invalid argument. " + "JavaMail requires an InputStreamSource that creates a fresh stream for every call.");
    }
    DataSource dataSource = createDataSource(inputStreamSource, contentType, "inline");
    addInline(contentId, dataSource);
}
Also used : Resource(org.springframework.core.io.Resource) FileDataSource(javax.activation.FileDataSource) DataSource(javax.activation.DataSource)

Aggregations

Resource (org.springframework.core.io.Resource)541 Test (org.junit.Test)248 ClassPathResource (org.springframework.core.io.ClassPathResource)217 IOException (java.io.IOException)86 FileSystemResource (org.springframework.core.io.FileSystemResource)67 File (java.io.File)60 UrlResource (org.springframework.core.io.UrlResource)60 ArrayList (java.util.ArrayList)49 ByteArrayResource (org.springframework.core.io.ByteArrayResource)46 InputStream (java.io.InputStream)33 InputStreamResource (org.springframework.core.io.InputStreamResource)31 PathMatchingResourcePatternResolver (org.springframework.core.io.support.PathMatchingResourcePatternResolver)24 URL (java.net.URL)20 MockServerWebExchange (org.springframework.mock.http.server.reactive.test.MockServerWebExchange)18 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)16 ServletContextResource (org.springframework.web.context.support.ServletContextResource)16 URI (java.net.URI)15 ResourceLoader (org.springframework.core.io.ResourceLoader)15 HashMap (java.util.HashMap)13 LinkedHashSet (java.util.LinkedHashSet)13