List of usage examples for org.springframework.web.servlet.resource ResourceHttpRequestHandler getLocations
public List<Resource> getLocations()
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 ww. j a va2s. c om*/ 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 .j a v a2 s . c o m this.handlerMap.put(pattern, resourceHandler); } } } }
From source file:org.springframework.web.servlet.resource.PublicResourceUrlProvider.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 determine the URL path to expose for * public use./*w ww . ja va 2s . c om*/ * * <p>It is expected the given path is what Spring MVC would use for request * mapping purposes, i.e. excluding context and servlet path portions. * * @param lookupPath the look path to check * * @return the resolved URL path or {@code null} if unresolved */ public final String getForLookupPath(String lookupPath) { if (logger.isDebugEnabled()) { logger.debug("Checking lookup path=" + lookupPath); } for (String pattern : this.handlerMap.keySet()) { if (!getPathMatcher().match(pattern, lookupPath)) { continue; } String pathWithinMapping = getPathMatcher().extractPathWithinPattern(pattern, lookupPath); String pathMapping = lookupPath.substring(0, lookupPath.indexOf(pathWithinMapping)); if (logger.isDebugEnabled()) { logger.debug("Found matching resource mapping=\"" + pattern + "\", " + "resource URL path=\"" + pathWithinMapping + "\""); } ResourceHttpRequestHandler handler = this.handlerMap.get(pattern); ResourceResolverChain chain = handler.createResourceResolverChain(); String resolved = chain.resolveUrlPath(pathWithinMapping, handler.getLocations()); if (resolved == null) { throw new IllegalStateException("Failed to get public resource URL path for " + pathWithinMapping); } if (logger.isDebugEnabled()) { logger.debug("Returning public resource URL path=\"" + resolved + "\""); } return pathMapping + resolved; } logger.debug("No matching resource mapping"); return null; }
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()); }//from w w w. jav 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.// ww w .j a v a 2 s . com * <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; }