Class DefaultClientlibService
- java.lang.Object
-
- com.composum.sling.clientlibs.service.DefaultClientlibService
-
- All Implemented Interfaces:
ClientlibService
public class DefaultClientlibService extends Object implements ClientlibService
Service related toClientlib.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface com.composum.sling.clientlibs.service.ClientlibService
ClientlibService.ClientlibInfo
-
-
Field Summary
Fields Modifier and Type Field Description protected static StringCATEGORYCACHETop node for the category cache within theClientlibConfiguration.Config.clientlibs_cache_root().protected org.apache.commons.collections4.map.LRUMapcategoryToPathCacheCache (String, Pair>) that maps categories to a pair of the query time and the resulting list of paths to client libraries with that path. protected ClientlibConfigurationclientlibConfigstatic Map<String,Object>CRUD_CACHE_FOLDER_PROPSprotected CssProcessorcssProcessorprotected ThreadPoolExecutorexecutorServiceprotected GzipProcessorgzipProcessorprotected JavascriptProcessorjavascriptProcessorprotected LazyCreationServicelazyCreationServiceprotected LinkRendererlinkRendererstatic PatternMINIFIED_PATTERNstatic StringMINIFIED_SELECTORprotected static Comparator<org.apache.sling.api.resource.Resource>orderResourceComparatorCompares two client libraries by theClientlib.PROP_ORDERattribute, resorting to path to at least ensure a predictable order when there is no order attribute.protected List<ClientlibPermissionPlugin>permissionPluginsprotected EnumMap<Clientlib.Type,ClientlibProcessor>processorMapstatic StringPROP_HASHProperty at content node of cache files that contains the hash value usable as etag that determines the current content of the client library and changes on each added / updated file.protected static StringQUERY_CLIENTLIBSXPath Query that matches all clientlibs.protected static StringQUERY_SUFFIX_REFERENCERSXpath Query suffix for a query that matches all clientlib folders referencing other stuff.protected EnumMap<Clientlib.Type,ClientlibRenderer>rendererMapprotected org.apache.sling.api.resource.ResourceResolverFactoryresolverFactoryprotected SequencerServicesequencerstatic PatternUNMINIFIED_PATTERN-
Fields inherited from interface com.composum.sling.clientlibs.service.ClientlibService
ENCODING_GZIP
-
-
Constructor Summary
Constructors Constructor Description DefaultClientlibService()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidactivate(org.osgi.service.component.ComponentContext context)protected StringadjustEncoding(String encoding)protected voidbindPermissionPlugin(ClientlibPermissionPlugin permissionPlugin)voidclearCache(org.apache.sling.api.resource.ResourceResolver resolver)Clears the whole cache for all clientlibs.protected org.apache.sling.api.resource.ResourceResolvercreateAdministrativeResolver()protected LazyCreationService.CreationStrategycreationStrategy()protected voiddeactivate(org.osgi.service.component.ComponentContext context)voiddeliverContent(org.apache.sling.api.resource.ResourceResolver resolver, ClientlibRef clientlibRef, boolean minified, OutputStream outputStream, String encoding)Writes the cached content to outputStream.protected StringgetCachePath(ClientlibRef ref, boolean minified, String encoding)Uses the category if given, or the single clientlib otherwise.protected @NotNull List<ResourceFilter>getCategoryPermissionFilters(String category)Retrieves theResourceFilters for a category from allClientlibPermissionPlugins.ClientlibConfiguration.ConfiggetClientlibConfig()protected ClientlibService.ClientlibInfogetFileHints(FileHandle file, ClientlibLink link)@NotNull org.apache.sling.api.resource.ResourcegetMinifiedSibling(@NotNull org.apache.sling.api.resource.Resource resource)protected StringgetMinifiedSibling(String path)protected StringgetUnminifiedSibling(String path)protected LazyCreationService.InitializationStrategyinitializationStrategy(ClientlibRef clientlibRef, String encoding, String hash, ProcessorContext context)protected booleanisAncestorOrSelf(@Nullable String parentPath, @Nullable String childPath)Returns true if the parent is an {ancestor} of the {resource} (and both are not null, of course.protected booleanisClientlibPermitted(@NotNull List<ResourceFilter> filters, org.apache.sling.api.resource.Resource clientlibResource)Checks whether a clientlib matches allResourceFilters for the given category.protected booleanisReachableFrom(List<String> paths, String path)protected org.apache.sling.api.resource.ResourceminificationVariant(org.apache.sling.api.resource.Resource resource)For files we use the correct sibling wrt.ClientlibService.ClientlibInfoprepareContent(org.apache.sling.api.SlingHttpServletRequest request, ClientlibRef clientlibRef, boolean minified, String rawEncoding, boolean forceRefreshCache, String requestedHash, long ifModifiedSince)Writes the content to a cache if it wasn't there, and returns a collection of information to be put into the response headers.protected voidrefreshSession(org.apache.sling.api.resource.ResourceResolver resolver, boolean logwarning)Resets unmodified resources to the currently saved state.protected List<String>removeChildren(List<String> unreachablePaths)For each path contained here, remove all paths that are children of it, thus removing consequential errors.voidrenderClientlibLinks(ClientlibElement clientlib, Writer writer, org.apache.sling.api.SlingHttpServletRequest request, RendererContext context)Renders the references to the ressources of the clientlibs into the page.ClientlibElementresolve(ClientlibRef ref, org.apache.sling.api.resource.ResourceResolver resolver)Resolves the element corresponding to the ref.protected List<org.apache.sling.api.resource.Resource>retrieveCategoryResources(String category, org.apache.sling.api.resource.ResourceResolver resolver)Retrieves the Clientlib-resources for a category.protected org.apache.sling.api.resource.ResourceretrieveResource(String path, org.apache.sling.api.resource.ResourceResolver resolver)Retrieve a resource from a resolver; if we don't find it, we try to retrieve the (un)minified sibling.protected org.apache.sling.api.resource.ResourceretrieveResourceRaw(String path, org.apache.sling.api.resource.ResourceResolver resolver)protected List<org.apache.sling.api.resource.Resource>retrieveResourcesForCategoryUncached(String category, org.apache.sling.api.resource.ResourceResolver resolver)protected Future<Void>startProcessing(ClientlibRef clientlibRef, String encoding, ProcessorContext context, OutputStream outputStream)Starts the processing (generation of the embedded content) of the clientlib / -category in the background.protected voidunbindPermissionPlugin(ClientlibPermissionPlugin permissionPlugin)@Nullable StringverifyClientlibPermissions(@Nullable Clientlib.Type requestedType, @Nullable org.apache.sling.api.resource.ResourceResolver userResolver, boolean onlyErrors)Generates a human readable descriptions of inconsistencies of the client libraries wrt.
-
-
-
Field Detail
-
MINIFIED_SELECTOR
public static final String MINIFIED_SELECTOR
- See Also:
- Constant Field Values
-
UNMINIFIED_PATTERN
public static final Pattern UNMINIFIED_PATTERN
-
MINIFIED_PATTERN
public static final Pattern MINIFIED_PATTERN
-
PROP_HASH
public static final String PROP_HASH
Property at content node of cache files that contains the hash value usable as etag that determines the current content of the client library and changes on each added / updated file.- See Also:
- Constant Field Values
-
CATEGORYCACHE
protected static final String CATEGORYCACHE
Top node for the category cache within theClientlibConfiguration.Config.clientlibs_cache_root().- See Also:
- Constant Field Values
-
clientlibConfig
protected ClientlibConfiguration clientlibConfig
-
resolverFactory
protected org.apache.sling.api.resource.ResourceResolverFactory resolverFactory
-
sequencer
protected SequencerService sequencer
-
lazyCreationService
protected LazyCreationService lazyCreationService
-
javascriptProcessor
protected JavascriptProcessor javascriptProcessor
-
cssProcessor
protected CssProcessor cssProcessor
-
linkRenderer
protected LinkRenderer linkRenderer
-
gzipProcessor
protected GzipProcessor gzipProcessor
-
permissionPlugins
protected final List<ClientlibPermissionPlugin> permissionPlugins
-
executorService
protected ThreadPoolExecutor executorService
-
rendererMap
protected EnumMap<Clientlib.Type,ClientlibRenderer> rendererMap
-
processorMap
protected EnumMap<Clientlib.Type,ClientlibProcessor> processorMap
-
categoryToPathCache
protected final org.apache.commons.collections4.map.LRUMap categoryToPathCache
Cache (String, Pair>) that maps categories to a pair of the query time and the resulting list of paths to client libraries with that path. We set an arbitrary limit of 100 since otherwise we'd be open to a DOS attack by retrieving random categories.
-
orderResourceComparator
protected static final Comparator<org.apache.sling.api.resource.Resource> orderResourceComparator
Compares two client libraries by theClientlib.PROP_ORDERattribute, resorting to path to at least ensure a predictable order when there is no order attribute.
-
QUERY_CLIENTLIBS
protected static final String QUERY_CLIENTLIBS
XPath Query that matches all clientlibs.- See Also:
- Constant Field Values
-
QUERY_SUFFIX_REFERENCERS
protected static final String QUERY_SUFFIX_REFERENCERS
Xpath Query suffix for a query that matches all clientlib folders referencing other stuff.- See Also:
- Constant Field Values
-
-
Method Detail
-
bindPermissionPlugin
protected void bindPermissionPlugin(ClientlibPermissionPlugin permissionPlugin)
-
unbindPermissionPlugin
protected void unbindPermissionPlugin(ClientlibPermissionPlugin permissionPlugin)
-
activate
protected void activate(org.osgi.service.component.ComponentContext context)
-
deactivate
protected void deactivate(org.osgi.service.component.ComponentContext context)
-
resolve
public ClientlibElement resolve(ClientlibRef ref, org.apache.sling.api.resource.ResourceResolver resolver)
Description copied from interface:ClientlibServiceResolves the element corresponding to the ref.- Specified by:
resolvein interfaceClientlibService- Returns:
- the corresponding element or null if we can't find it.
-
minificationVariant
protected org.apache.sling.api.resource.Resource minificationVariant(org.apache.sling.api.resource.Resource resource)
For files we use the correct sibling wrt.ClientlibConfiguration.Config.clientlibs_minified_use()andClientlibConfiguration.Config.debug().
-
retrieveResource
protected org.apache.sling.api.resource.Resource retrieveResource(String path, org.apache.sling.api.resource.ResourceResolver resolver)
Retrieve a resource from a resolver; if we don't find it, we try to retrieve the (un)minified sibling.
-
retrieveResourceRaw
protected org.apache.sling.api.resource.Resource retrieveResourceRaw(String path, org.apache.sling.api.resource.ResourceResolver resolver)
-
getMinifiedSibling
@NotNull public @NotNull org.apache.sling.api.resource.Resource getMinifiedSibling(@NotNull @NotNull org.apache.sling.api.resource.Resource resource)- Specified by:
getMinifiedSiblingin interfaceClientlibService
-
retrieveCategoryResources
protected List<org.apache.sling.api.resource.Resource> retrieveCategoryResources(String category, org.apache.sling.api.resource.ResourceResolver resolver)
Retrieves the Clientlib-resources for a category. Uses thecategoryToPathCachefor caching the paths to avoid executing a query each time. We retrieve the paths with ancreateAdministrativeResolver()to find everything independent of the users rights, and re-retrieve the resources with the users resolver afterwards, filtering out inaccessible things.
-
retrieveResourcesForCategoryUncached
protected List<org.apache.sling.api.resource.Resource> retrieveResourcesForCategoryUncached(String category, org.apache.sling.api.resource.ResourceResolver resolver)
-
getCategoryPermissionFilters
@NotNull protected @NotNull List<ResourceFilter> getCategoryPermissionFilters(String category)
Retrieves theResourceFilters for a category from allClientlibPermissionPlugins.
-
isClientlibPermitted
protected boolean isClientlibPermitted(@NotNull @NotNull List<ResourceFilter> filters, org.apache.sling.api.resource.Resource clientlibResource)Checks whether a clientlib matches allResourceFilters for the given category.
-
createAdministrativeResolver
protected org.apache.sling.api.resource.ResourceResolver createAdministrativeResolver()
-
getClientlibConfig
public ClientlibConfiguration.Config getClientlibConfig()
- Specified by:
getClientlibConfigin interfaceClientlibService
-
renderClientlibLinks
public void renderClientlibLinks(ClientlibElement clientlib, Writer writer, org.apache.sling.api.SlingHttpServletRequest request, RendererContext context) throws IOException, javax.jcr.RepositoryException
Description copied from interface:ClientlibServiceRenders the references to the ressources of the clientlibs into the page.- Specified by:
renderClientlibLinksin interfaceClientlibService- Parameters:
clientlib- aClientliborClientlibCategory- Throws:
IOExceptionjavax.jcr.RepositoryException
-
prepareContent
public ClientlibService.ClientlibInfo prepareContent(org.apache.sling.api.SlingHttpServletRequest request, ClientlibRef clientlibRef, boolean minified, String rawEncoding, boolean forceRefreshCache, String requestedHash, long ifModifiedSince) throws IOException, javax.jcr.RepositoryException
Writes the content to a cache if it wasn't there, and returns a collection of information to be put into the response headers.If the requestedHash (generated by the rendering process) equal to the saved hash, we have no reason check whether the cache file is current. If it is not equal we also do not check if the If-Modified-Since header ifModified is older than the files modification date.
We try to handle the following conditions. If the user cannot access the cache, the cache-file should not be recreated. If a parallel process recreates the cache-file before this process manages to acquire the lock to recreate it, it should not be recreated again.
- Specified by:
prepareContentin interfaceClientlibServiceclientlibRef- reference to the clientlib / category to renderminified- when true, the minified versionrawEncoding- the needed encoding, if applicableforceRefreshCache- if true, the cache will be refreshed even if it is up to daterequestedHash- the hash value the client requestedifModifiedSince- the value of the If-Modified-Since header, if present- Returns:
- information about the file
- Throws:
IOExceptionjavax.jcr.RepositoryException
-
creationStrategy
protected LazyCreationService.CreationStrategy creationStrategy()
-
initializationStrategy
protected LazyCreationService.InitializationStrategy initializationStrategy(ClientlibRef clientlibRef, String encoding, String hash, ProcessorContext context)
-
startProcessing
protected Future<Void> startProcessing(ClientlibRef clientlibRef, String encoding, ProcessorContext context, OutputStream outputStream)
Starts the processing (generation of the embedded content) of the clientlib / -category in the background.
-
getFileHints
protected ClientlibService.ClientlibInfo getFileHints(FileHandle file, ClientlibLink link)
-
deliverContent
public void deliverContent(org.apache.sling.api.resource.ResourceResolver resolver, ClientlibRef clientlibRef, boolean minified, OutputStream outputStream, String encoding) throws IOException, javax.jcr.RepositoryExceptionDescription copied from interface:ClientlibServiceWrites the cached content to outputStream.- Specified by:
deliverContentin interfaceClientlibServiceclientlibRef- reference to the clientlib / category to renderminified- when true, the minified versionoutputStream- the stream to write toencoding- the needed encoding, if applicable- Throws:
IOExceptionjavax.jcr.RepositoryException
-
getCachePath
protected String getCachePath(ClientlibRef ref, boolean minified, String encoding)
Uses the category if given, or the single clientlib otherwise.- Parameters:
ref- what we create the cache for- Returns:
- the name of the file that is used for caching this resource. Not null.
-
refreshSession
protected void refreshSession(org.apache.sling.api.resource.ResourceResolver resolver, boolean logwarning)Resets unmodified resources to the currently saved state.
-
verifyClientlibPermissions
@Nullable public @Nullable String verifyClientlibPermissions(@Nullable @Nullable Clientlib.Type requestedType, @Nullable @Nullable org.apache.sling.api.resource.ResourceResolver userResolver, boolean onlyErrors)
Description copied from interface:ClientlibServiceGenerates a human readable descriptions of inconsistencies of the client libraries wrt. the given resolver (or an anonymous resolver, of none is given). Checks for each client library readable for the given resolver, that all elements of these client libraries and the files referenced from them are readable, too. Also we check that for all categories either all client libraries with this reference are readable, or none of them. All these are errors, since if that's not the case, this can cause a permanent recalculation of the clientlibs content, or, even worse, break the rendering. If {onlyErrors} is false, we also include informational messages about all unreadable libraries.- Specified by:
verifyClientlibPermissionsin interfaceClientlibService- Parameters:
requestedType- if not null, only clientlibs / files of that type are checkeduserResolver- if not null, we use this resolver instead of an anonymous resolver to check readabilityonlyErrors- if true, we skip informational messages- Returns:
- null if everything is OK, otherwise a description of the problems
-
clearCache
public void clearCache(org.apache.sling.api.resource.ResourceResolver resolver) throws org.apache.sling.api.resource.PersistenceExceptionDescription copied from interface:ClientlibServiceClears the whole cache for all clientlibs. Obviously something to used sparingly.- Specified by:
clearCachein interfaceClientlibService- Parameters:
resolver- the resolver to use.- Throws:
org.apache.sling.api.resource.PersistenceException
-
removeChildren
protected List<String> removeChildren(List<String> unreachablePaths)
For each path contained here, remove all paths that are children of it, thus removing consequential errors.
-
-