Example usage for org.springframework.web.servlet.resource ResourceHttpRequestHandler getResourceResolvers

List of usage examples for org.springframework.web.servlet.resource ResourceHttpRequestHandler getResourceResolvers

Introduction

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

Prototype

public List<ResourceResolver> getResourceResolvers() 

Source Link

Document

Return the list of configured resource resolvers.

Usage

From source file:org.broadleafcommerce.common.resource.service.ResourceBundlingServiceImpl.java

@Override
public String resolveBundleResourceName(String requestedBundleName, String mappingPrefix, List<String> files) {

    ResourceHttpRequestHandler resourceRequestHandler = findResourceHttpRequestHandler(requestedBundleName);
    if (resourceRequestHandler != null && CollectionUtils.isNotEmpty(files)) {
        ResourceResolverChain resolverChain = new BroadleafDefaultResourceResolverChain(
                resourceRequestHandler.getResourceResolvers());
        List<Resource> locations = resourceRequestHandler.getLocations();

        StringBuilder combinedPathString = new StringBuilder();
        List<String> filePaths = new ArrayList<String>();
        for (String file : files) {
            String resourcePath = resolverChain.resolveUrlPath(file, locations);
            if (resourcePath == null) {
                // can't find the exact name specified in the bundle, try it with the mappingPrefix
                resourcePath = resolverChain.resolveUrlPath(mappingPrefix + file, locations);
            }/*from   w w  w  . j a  v a 2  s.c  o  m*/

            if (resourcePath != null) {
                filePaths.add(resourcePath);
                combinedPathString.append(resourcePath);
            } else {
                LOG.warn(new StringBuilder().append("Could not resolve resource path specified in bundle as [")
                        .append(file).append("] or as [").append(mappingPrefix + file)
                        .append("]. Skipping file.").toString());
            }
        }

        int version = Math.abs(combinedPathString.toString().hashCode());
        String versionedBundleName = mappingPrefix
                + addVersion(requestedBundleName, "-" + String.valueOf(version));

        createBundleIfNeeded(versionedBundleName, filePaths, resolverChain, locations);

        return versionedBundleName;
    } else {
        if (LOG.isWarnEnabled()) {
            LOG.warn("");
        }
        return null;
    }
}

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

protected void detectResourceHandlers(ApplicationContext applicationContext) {
    logger.debug("Looking for resource handler mappings");
    Map<String, SimpleUrlHandlerMapping> beans = applicationContext
            .getBeansOfType(SimpleUrlHandlerMapping.class);
    for (SimpleUrlHandlerMapping hm : beans.values()) {
        for (String pattern : hm.getUrlMap().keySet()) {
            Object handler = hm.getUrlMap().get(pattern);
            if (handler instanceof ResourceHttpRequestHandler) {
                ResourceHttpRequestHandler resourceHandler = (ResourceHttpRequestHandler) handler;
                if (logger.isDebugEnabled()) {
                    logger.debug("Found pattern=\"" + pattern + "\" mapped to locations "
                            + resourceHandler.getLocations() + " with resolvers: "
                            + resourceHandler.getResourceResolvers());
                }/*  w w w  .ja  v  a  2  s .com*/
                this.handlerMap.put(pattern, resourceHandler);
            }
        }
    }
}

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

protected void detectResourceHandlers(ApplicationContext appContext) {
    logger.debug("Looking for resource handler mappings");

    Map<String, SimpleUrlHandlerMapping> beans = appContext.getBeansOfType(SimpleUrlHandlerMapping.class);
    List<SimpleUrlHandlerMapping> mappings = new ArrayList<>(beans.values());
    AnnotationAwareOrderComparator.sort(mappings);

    for (SimpleUrlHandlerMapping mapping : mappings) {
        for (String pattern : mapping.getHandlerMap().keySet()) {
            Object handler = mapping.getHandlerMap().get(pattern);
            if (handler instanceof ResourceHttpRequestHandler) {
                ResourceHttpRequestHandler resourceHandler = (ResourceHttpRequestHandler) handler;
                if (logger.isDebugEnabled()) {
                    logger.debug("Found resource handler mapping: URL pattern=\"" + pattern + "\", "
                            + "locations=" + resourceHandler.getLocations() + ", " + "resolvers="
                            + resourceHandler.getResourceResolvers());
                }/*  w ww .ja  v  a  2  s  . c  om*/
                this.handlerMap.put(pattern, resourceHandler);
            }
        }
    }
}

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./*w w  w. ja  v  a2s  .  co  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;
}