Example usage for org.springframework.web.servlet.resource DefaultResourceResolverChain DefaultResourceResolverChain

List of usage examples for org.springframework.web.servlet.resource DefaultResourceResolverChain DefaultResourceResolverChain

Introduction

In this page you can find the example usage for org.springframework.web.servlet.resource DefaultResourceResolverChain DefaultResourceResolverChain.

Prototype

public DefaultResourceResolverChain(@Nullable List<? extends ResourceResolver> resolvers) 

Source Link

Usage

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

@Nullable
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");
    }/*from  w ww .jav  a 2s . c om*/
    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;
}

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

/**
 * Compare the given path against configured resource handler mappings and
 * if a match is found use the {@code ResourceResolver} chain of the matched
 * {@code ResourceHttpRequestHandler} to resolve the URL path to expose for
 * public use.//from  w  w  w. j a  va  2s.c o  m
 * <p>It is expected that the given path is what Spring MVC would use for
 * request mapping purposes, i.e. excluding context and servlet path portions.
 * <p>If several handler mappings match, the handler used will be the one
 * configured with the most specific pattern.
 * @param lookupPath the lookup path to check
 * @return the resolved public URL path, or {@code null} if unresolved
 */
@Nullable
public final String getForLookupPath(String lookupPath) {

    // Clean duplicate slashes or pathWithinPattern won't match lookupPath
    String previous;
    do {
        previous = lookupPath;
        lookupPath = StringUtils.replace(lookupPath, "//", "/");
    } while (!lookupPath.equals(previous));

    if (logger.isTraceEnabled()) {
        logger.trace("Getting resource URL for lookup path \"" + lookupPath + "\"");
    }

    List<String> matchingPatterns = new ArrayList<>();
    for (String pattern : this.handlerMap.keySet()) {
        if (getPathMatcher().match(pattern, lookupPath)) {
            matchingPatterns.add(pattern);
        }
    }

    if (!matchingPatterns.isEmpty()) {
        Comparator<String> patternComparator = getPathMatcher().getPatternComparator(lookupPath);
        Collections.sort(matchingPatterns, patternComparator);
        for (String pattern : matchingPatterns) {
            String pathWithinMapping = getPathMatcher().extractPathWithinPattern(pattern, lookupPath);
            String pathMapping = lookupPath.substring(0, lookupPath.indexOf(pathWithinMapping));
            if (logger.isTraceEnabled()) {
                logger.trace("Invoking ResourceResolverChain for URL pattern \"" + pattern + "\"");
            }
            ResourceHttpRequestHandler handler = this.handlerMap.get(pattern);
            ResourceResolverChain chain = new DefaultResourceResolverChain(handler.getResourceResolvers());
            String resolved = chain.resolveUrlPath(pathWithinMapping, handler.getLocations());
            if (resolved == null) {
                continue;
            }
            if (logger.isTraceEnabled()) {
                logger.trace("Resolved public resource URL path \"" + resolved + "\"");
            }
            return pathMapping + resolved;
        }
    }

    if (logger.isDebugEnabled()) {
        logger.debug("No matching resource mapping for lookup path \"" + lookupPath + "\"");
    }
    return null;
}