Example usage for org.springframework.core.io Resource createRelative

List of usage examples for org.springframework.core.io Resource createRelative

Introduction

In this page you can find the example usage for org.springframework.core.io Resource createRelative.

Prototype

Resource createRelative(String relativePath) throws IOException;

Source Link

Document

Create a resource relative to this resource.

Usage

From source file:org.springframework.mock.web.MockServletContext.java

@Override
@Nullable//from   www .j a  v a  2 s.  c o  m
public Set<String> getResourcePaths(String path) {
    String actualPath = (path.endsWith("/") ? path : path + "/");
    Resource resource = this.resourceLoader.getResource(getResourceLocation(actualPath));
    try {
        File file = resource.getFile();
        String[] fileList = file.list();
        if (ObjectUtils.isEmpty(fileList)) {
            return null;
        }
        Set<String> resourcePaths = new LinkedHashSet<>(fileList.length);
        for (String fileEntry : fileList) {
            String resultPath = actualPath + fileEntry;
            if (resource.createRelative(fileEntry).getFile().isDirectory()) {
                resultPath += "/";
            }
            resourcePaths.add(resultPath);
        }
        return resourcePaths;
    } catch (IOException ex) {
        logger.warn("Couldn't get resource paths for " + resource, ex);
        return null;
    }
}

From source file:org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.java

/**
 * Determine JPA's default "META-INF/orm.xml" resource for use with Spring's default
 * persistence unit, if any.//from w  w w.j a va2  s.c  om
 * <p>Checks whether a "META-INF/orm.xml" file exists in the classpath and uses it
 * if it is not co-located with a "META-INF/persistence.xml" file.
 */
@Nullable
private Resource getOrmXmlForDefaultPersistenceUnit() {
    Resource ormXml = this.resourcePatternResolver
            .getResource(this.defaultPersistenceUnitRootLocation + DEFAULT_ORM_XML_RESOURCE);
    if (ormXml.exists()) {
        try {
            Resource persistenceXml = ormXml.createRelative(PERSISTENCE_XML_FILENAME);
            if (!persistenceXml.exists()) {
                return ormXml;
            }
        } catch (IOException ex) {
            // Cannot resolve relative persistence.xml file - let's assume it's not there.
            return ormXml;
        }
    }
    return null;
}

From source file:org.springframework.web.servlet.resource.PathExtensionResourceResolver.java

@Override
public Resource resolveResource(HttpServletRequest request, String requestPath, List<Resource> locations,
        ResourceResolverChain chain) {/*from w  ww. j  a va2  s  .  c  om*/

    Resource resource = chain.resolveResource(request, requestPath, locations);
    if ((resource != null) && !this.compareTimeStamp) {
        return resource;
    }

    for (Resource location : locations) {
        String baseFilename = StringUtils.getFilename(requestPath);
        try {
            Resource basePath = location.createRelative(StringUtils.delete(requestPath, baseFilename));
            if (basePath.getFile().isDirectory()) {
                for (String fileName : basePath.getFile().list(new ExtensionFilenameFilter(baseFilename))) {
                    //Always use the first match
                    Resource matched = basePath.createRelative(fileName);
                    if ((resource == null) || (matched.lastModified() > resource.lastModified())) {
                        return matched;
                    } else {
                        return resource;
                    }
                }
            }
        } catch (IOException e) {
            logger.trace("Error occurred locating resource based on file extension mapping", e);
        }
    }

    return resource;
}

From source file:org.springframework.web.servlet.resource.PathResourceResolver.java

private Resource getResource(String path, List<? extends Resource> locations) {
    for (Resource location : locations) {
        try {//w w w .  j av  a  2s  .c om
            if (logger.isTraceEnabled()) {
                logger.trace("Checking location=[" + location + "]");
            }
            Resource resource = location.createRelative(path);
            if (resource.exists() && resource.isReadable()) {
                if (logger.isTraceEnabled()) {
                    logger.trace("Found match");
                }
                return resource;
            } else if (logger.isTraceEnabled()) {
                logger.trace("No match");
            }
        } catch (IOException ex) {
            logger.trace("Failure checking for relative resource. Trying next location.", ex);
        }
    }
    return null;
}