|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
package com.sun.faces.application.resource; |
|
|
|
import java.util.ArrayList; |
|
import java.util.Arrays; |
|
import java.util.Collections; |
|
import java.util.HashMap; |
|
import java.util.List; |
|
import java.util.Locale; |
|
import java.util.Map; |
|
import java.util.MissingResourceException; |
|
import java.util.ResourceBundle; |
|
import java.util.concurrent.locks.ReentrantLock; |
|
import java.util.logging.Level; |
|
import java.util.logging.Logger; |
|
import java.util.regex.Pattern; |
|
import java.util.regex.PatternSyntaxException; |
|
import java.util.stream.Stream; |
|
|
|
import javax.faces.application.ProjectStage; |
|
import javax.faces.application.ResourceHandler; |
|
import javax.faces.application.ResourceVisitOption; |
|
import javax.faces.component.UIViewRoot; |
|
import javax.faces.context.FacesContext; |
|
|
|
import com.sun.faces.config.WebConfiguration; |
|
import com.sun.faces.util.FacesLogger; |
|
import com.sun.faces.util.Util; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class ResourceManager { |
|
|
|
private static final Logger LOGGER = FacesLogger.RESOURCE.getLogger(); |
|
|
|
|
|
|
|
|
|
private static final Pattern CONFIG_MIMETYPE_PATTERN = Pattern.compile("[a-z-]*/[a-z0-9.\\*-]*"); |
|
|
|
private FaceletWebappResourceHelper faceletWebappResourceHelper = new FaceletWebappResourceHelper(); |
|
|
|
|
|
|
|
|
|
private ResourceHelper webappResourceHelper = new WebappResourceHelper(); |
|
|
|
|
|
|
|
|
|
private ClasspathResourceHelper classpathResourceHelper = new ClasspathResourceHelper(); |
|
|
|
|
|
|
|
|
|
|
|
private ResourceCache cache; |
|
|
|
|
|
|
|
|
|
|
|
private List<Pattern> compressableTypes; |
|
|
|
|
|
|
|
|
|
|
|
|
|
private ReentrantLock lock = new ReentrantLock(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public ResourceManager(ResourceCache cache) { |
|
this.cache = cache; |
|
Map<String, Object> throwAwayMap = new HashMap<>(); |
|
initCompressableTypes(throwAwayMap); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
public ResourceManager(Map<String, Object> appMap, ResourceCache cache) { |
|
this.cache = cache; |
|
initCompressableTypes(appMap); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public ResourceInfo findResource(String libraryName, String resourceName, String contentType, FacesContext ctx) { |
|
return findResource(libraryName, resourceName, contentType, false, ctx); |
|
} |
|
|
|
public ResourceInfo findViewResource(String resourceName, String contentType, FacesContext facesContext) { |
|
String localePrefix = getLocalePrefix(facesContext); |
|
List<String> contracts = getResourceLibraryContracts(facesContext); |
|
|
|
ResourceInfo info = getFromCache(resourceName, null, localePrefix, contracts); |
|
|
|
if (info == null) { |
|
if (isCompressable(contentType, facesContext)) { |
|
info = findResourceCompressed(null, resourceName, true, localePrefix, contracts, facesContext); |
|
} else { |
|
info = findResourceNonCompressed(null, resourceName, true, localePrefix, contracts, facesContext); |
|
} |
|
} |
|
|
|
return info; |
|
} |
|
|
|
public ResourceInfo findResource(String libraryName, String resourceName, String contentType, boolean isViewResource, FacesContext ctx) { |
|
|
|
String localePrefix = getLocalePrefix(ctx); |
|
List<String> contracts = getResourceLibraryContracts(ctx); |
|
|
|
ResourceInfo info = getFromCache(resourceName, libraryName, localePrefix, contracts); |
|
|
|
if (info == null) { |
|
if (isCompressable(contentType, ctx)) { |
|
info = findResourceCompressed(libraryName, resourceName, isViewResource, localePrefix, contracts, ctx); |
|
} else { |
|
info = findResourceNonCompressed(libraryName, resourceName, isViewResource, localePrefix, contracts, ctx); |
|
} |
|
} |
|
|
|
return info; |
|
} |
|
|
|
public Stream<String> getViewResources(FacesContext facesContext, String path, int maxDepth, ResourceVisitOption... options) { |
|
return faceletWebappResourceHelper.getViewResources(facesContext, path, maxDepth, options); |
|
} |
|
|
|
|
|
|
|
|
|
private ResourceInfo findResourceCompressed(String libraryName, String resourceName, boolean isViewResource, String localePrefix, List<String> contracts, FacesContext ctx) { |
|
|
|
ResourceInfo info = null; |
|
|
|
lock.lock(); |
|
try { |
|
info = getFromCache(resourceName, libraryName, localePrefix, contracts); |
|
if (info == null) { |
|
info = doLookup(libraryName, resourceName, localePrefix, true, isViewResource, contracts, ctx); |
|
if (info != null) { |
|
addToCache(info, contracts); |
|
} |
|
} |
|
} finally { |
|
lock.unlock(); |
|
} |
|
|
|
return info; |
|
} |
|
|
|
private ResourceInfo findResourceNonCompressed(String libraryName, String resourceName, boolean isViewResource, String localePrefix, List<String> contracts, FacesContext ctx) { |
|
ResourceInfo info = doLookup(libraryName, resourceName, localePrefix, false, isViewResource, contracts, ctx); |
|
|
|
if (info == null && contracts != null) { |
|
info = doLookup(libraryNameFromContracts(libraryName, contracts), resourceName, localePrefix, false, isViewResource, contracts, ctx); |
|
} |
|
|
|
if (info != null && !info.isDoNotCache()) { |
|
addToCache(info, contracts); |
|
} |
|
|
|
return info; |
|
} |
|
|
|
private String libraryNameFromContracts(String libraryName, List<String> contracts) { |
|
|
|
|
|
for (String contract : contracts) { |
|
if (contract.equals(libraryName)) { |
|
return null; |
|
} |
|
} |
|
|
|
return libraryName; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private ResourceInfo doLookup(String libraryName, String resourceName, String localePrefix, boolean compressable, boolean isViewResource, List<String> contracts, FacesContext ctx) { |
|
|
|
|
|
for (String contract : contracts) { |
|
ResourceInfo info = getResourceInfo(libraryName, resourceName, localePrefix, contract, compressable, isViewResource, ctx, null); |
|
if (info != null) { |
|
return info; |
|
} |
|
} |
|
|
|
return getResourceInfo(libraryName, resourceName, localePrefix, null, compressable, isViewResource, ctx, null); |
|
} |
|
|
|
private ResourceInfo getResourceInfo(String libraryName, String resourceName, String localePrefix, String contract, boolean compressable, boolean isViewResource, FacesContext ctx, LibraryInfo library) { |
|
if (libraryName != null && !nameContainsForbiddenSequence(libraryName)) { |
|
library = findLibrary(libraryName, localePrefix, contract, ctx); |
|
|
|
if (library == null && localePrefix != null) { |
|
|
|
library = findLibrary(libraryName, null, contract, ctx); |
|
} |
|
|
|
if (library == null) { |
|
|
|
library = findLibraryOnClasspathWithZipDirectoryEntryScan(libraryName, localePrefix, contract, ctx, false); |
|
|
|
if (library == null && localePrefix != null) { |
|
|
|
|
|
library = findLibraryOnClasspathWithZipDirectoryEntryScan(libraryName, null, contract, ctx, false); |
|
} |
|
|
|
if (library == null) { |
|
return null; |
|
} |
|
} |
|
} else if (nameContainsForbiddenSequence(libraryName)) { |
|
return null; |
|
} |
|
|
|
String resName = trimLeadingSlash(resourceName); |
|
if (nameContainsForbiddenSequence(resName) || (!isViewResource && resName.startsWith("WEB-INF"))) { |
|
return null; |
|
} |
|
|
|
ResourceInfo info = findResource(library, resourceName, localePrefix, compressable, isViewResource, ctx); |
|
if (info == null && localePrefix != null) { |
|
|
|
|
|
info = findResource(library, resourceName, null, compressable, isViewResource, ctx); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (info == null && library != null && library.getHelper() instanceof WebappResourceHelper) { |
|
LibraryInfo altLibrary = classpathResourceHelper.findLibrary(libraryName, localePrefix, contract, ctx); |
|
if (altLibrary != null) { |
|
VersionInfo originalVersion = library.getVersion(); |
|
VersionInfo altVersion = altLibrary.getVersion(); |
|
if (originalVersion == null && altVersion == null) { |
|
library = altLibrary; |
|
} else if (originalVersion == null && altVersion != null) { |
|
library = null; |
|
} else if (originalVersion != null && altVersion == null) { |
|
library = null; |
|
} else if (originalVersion.compareTo(altVersion) == 0) { |
|
library = altLibrary; |
|
} |
|
|
|
} |
|
|
|
if (library != null) { |
|
info = findResource(library, resourceName, localePrefix, compressable, isViewResource, ctx); |
|
if (info == null && localePrefix != null) { |
|
|
|
|
|
info = findResource(library, resourceName, null, compressable, isViewResource, ctx); |
|
} |
|
} |
|
} |
|
|
|
return info; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
private String trimLeadingSlash(String s) { |
|
if (s.charAt(0) == '/') { |
|
return s.substring(1); |
|
} else { |
|
return s; |
|
} |
|
} |
|
|
|
private static boolean nameContainsForbiddenSequence(String name) { |
|
boolean result = false; |
|
if (name != null) { |
|
name = name.toLowerCase(); |
|
|
|
result = name.startsWith(".") || |
|
name.contains("../") || |
|
name.contains("..\\") || |
|
name.startsWith("/") || |
|
name.startsWith("\\") || |
|
name.endsWith("/") || |
|
|
|
name.contains("..%2f") || |
|
name.contains("..%5c") || |
|
name.startsWith("%2f") || |
|
name.startsWith("%5c") || |
|
name.endsWith("%2f") || |
|
|
|
name.contains("..\\u002f") || |
|
name.contains("..\\u005c") || |
|
name.startsWith("\\u002f") || |
|
name.startsWith("\\u005c") || |
|
name.endsWith("\\u002f") |
|
|
|
; |
|
} |
|
|
|
return result; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private ResourceInfo getFromCache(String name, String library, String localePrefix, List<String> contracts) { |
|
if (cache == null) { |
|
return null; |
|
} |
|
|
|
return cache.get(name, library, localePrefix, contracts); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void addToCache(ResourceInfo info, List<String> contracts) { |
|
if (cache == null) { |
|
return; |
|
} |
|
|
|
cache.add(info, contracts); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LibraryInfo findLibrary(String libraryName, String localePrefix, String contract, FacesContext ctx) { |
|
|
|
LibraryInfo library = webappResourceHelper.findLibrary(libraryName, localePrefix, contract, ctx); |
|
|
|
if (library == null) { |
|
library = classpathResourceHelper.findLibrary(libraryName, localePrefix, contract, ctx); |
|
} |
|
|
|
if (library == null && contract == null) { |
|
|
|
library = faceletWebappResourceHelper.findLibrary(libraryName, localePrefix, contract, ctx); |
|
} |
|
|
|
|
|
return library; |
|
} |
|
|
|
LibraryInfo findLibraryOnClasspathWithZipDirectoryEntryScan(String libraryName, |
|
String localePrefix, |
|
String contract, FacesContext ctx, boolean forceScan) { |
|
return classpathResourceHelper.findLibraryWithZipDirectoryEntryScan(libraryName, localePrefix, contract, ctx, forceScan); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private ResourceInfo findResource(LibraryInfo library, |
|
String resourceName, |
|
String localePrefix, |
|
boolean compressable, |
|
boolean skipToFaceletResourceHelper, |
|
FacesContext ctx) { |
|
|
|
if (library != null) { |
|
return library.getHelper().findResource(library, |
|
resourceName, |
|
localePrefix, |
|
compressable, |
|
ctx); |
|
} else { |
|
ResourceInfo resource = null; |
|
|
|
if (!skipToFaceletResourceHelper) { |
|
resource = webappResourceHelper.findResource(null, |
|
resourceName, |
|
localePrefix, |
|
compressable, |
|
ctx); |
|
} |
|
if (resource == null && !skipToFaceletResourceHelper) { |
|
resource = classpathResourceHelper.findResource(null, |
|
resourceName, |
|
localePrefix, |
|
compressable, |
|
ctx); |
|
} |
|
if (resource == null) { |
|
resource = faceletWebappResourceHelper.findResource(library, |
|
resourceName, |
|
localePrefix, |
|
compressable, |
|
ctx); |
|
} |
|
return resource; |
|
} |
|
|
|
} |
|
|
|
ResourceInfo findResource(String resourceId) { |
|
|
|
String libraryName = null; |
|
String resourceName = null; |
|
int end = 0, start = 0; |
|
if (-1 != (end = resourceId.lastIndexOf("/"))) { |
|
resourceName = resourceId.substring(end+1); |
|
if (-1 != (start = resourceId.lastIndexOf("/", end - 1))) { |
|
libraryName = resourceId.substring(start+1, end); |
|
} else { |
|
libraryName = resourceId.substring(0, end); |
|
} |
|
} |
|
FacesContext context = FacesContext.getCurrentInstance(); |
|
LibraryInfo info = this.findLibrary(libraryName, null, null, context); |
|
ResourceInfo resourceInfo = this.findResource(info, resourceName, libraryName, true, false, context); |
|
|
|
return resourceInfo; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private String getLocalePrefix(FacesContext context) { |
|
|
|
String localePrefix = null; |
|
|
|
localePrefix = context.getExternalContext().getRequestParameterMap().get("loc"); |
|
|
|
|
|
|
|
if(localePrefix != null && !nameContainsForbiddenSequence(localePrefix)){ |
|
return localePrefix; |
|
} |
|
|
|
String appBundleName = context.getApplication().getMessageBundle(); |
|
if (null != appBundleName) { |
|
|
|
Locale locale = null; |
|
if (context.getViewRoot() != null) { |
|
locale = context.getViewRoot().getLocale(); |
|
} else { |
|
locale = context.getApplication().getViewHandler().calculateLocale(context); |
|
} |
|
|
|
try { |
|
ResourceBundle appBundle = |
|
ResourceBundle.getBundle(appBundleName, |
|
locale, |
|
Util.getCurrentLoader(ResourceManager.class)); |
|
localePrefix = |
|
appBundle |
|
.getString(ResourceHandler.LOCALE_PREFIX); |
|
} catch (MissingResourceException mre) { |
|
if (LOGGER.isLoggable(Level.FINEST)) { |
|
LOGGER.log(Level.FINEST, "Ignoring missing resource", mre); |
|
} |
|
} |
|
} |
|
return localePrefix; |
|
|
|
} |
|
|
|
private List<String> getResourceLibraryContracts(FacesContext context) { |
|
UIViewRoot viewRoot = context.getViewRoot(); |
|
if(viewRoot == null) { |
|
|
|
if(context.getApplication().getResourceHandler().isResourceRequest(context)) { |
|
|
|
|
|
String param = context.getExternalContext().getRequestParameterMap().get("con"); |
|
if(!nameContainsForbiddenSequence(param) && param != null && param.trim().length() > 0) { |
|
return Arrays.asList(param); |
|
} |
|
} |
|
|
|
return Collections.emptyList(); |
|
} |
|
return context.getResourceLibraryContracts(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private boolean isCompressable(String contentType, FacesContext ctx) { |
|
|
|
|
|
if (contentType == null || ctx.isProjectStage(ProjectStage.Development)) { |
|
return false; |
|
} else { |
|
if (compressableTypes != null && !compressableTypes.isEmpty()) { |
|
for (Pattern p : compressableTypes) { |
|
boolean matches = p.matcher(contentType).matches(); |
|
if (matches) { |
|
return true; |
|
} |
|
} |
|
} |
|
} |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void initCompressableTypes(Map<String, Object> appMap) { |
|
|
|
WebConfiguration config = WebConfiguration.getInstance(); |
|
String value = config.getOptionValue(WebConfiguration.WebContextInitParameter.CompressableMimeTypes); |
|
if (value != null && value.length() > 0) { |
|
String[] values = Util.split(appMap, value, ","); |
|
if (values != null) { |
|
for (String s : values) { |
|
String pattern = s.trim(); |
|
if (!isPatternValid(pattern)) { |
|
continue; |
|
} |
|
if (pattern.endsWith("/*")) { |
|
pattern = pattern.substring(0, pattern.indexOf("/*")); |
|
pattern += "/[a-z0-9.-]*"; |
|
} |
|
if (compressableTypes == null) { |
|
compressableTypes = new ArrayList<>(values.length); |
|
} |
|
try { |
|
compressableTypes.add(Pattern.compile(pattern)); |
|
} catch (PatternSyntaxException pse) { |
|
if (LOGGER.isLoggable(Level.WARNING)) { |
|
|
|
LOGGER.log(Level.WARNING, |
|
"jsf.resource.mime.type.configration.invalid", |
|
new Object[] { pattern, pse.getPattern()}); |
|
} |
|
} |
|
} |
|
} |
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private boolean isPatternValid(String input) { |
|
|
|
return (CONFIG_MIMETYPE_PATTERN.matcher(input).matches()); |
|
|
|
} |
|
|
|
|
|
} |
|
|