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 String
CATEGORYCACHE
Top node for the category cache within theClientlibConfiguration.Config.clientlibs_cache_root()
.protected 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. protected ClientlibConfiguration
clientlibConfig
static Map<String,Object>
CRUD_CACHE_FOLDER_PROPS
protected CssProcessor
cssProcessor
protected ThreadPoolExecutor
executorService
protected GzipProcessor
gzipProcessor
protected JavascriptProcessor
javascriptProcessor
protected LazyCreationService
lazyCreationService
protected LinkRenderer
linkRenderer
static Pattern
MINIFIED_PATTERN
static String
MINIFIED_SELECTOR
protected static Comparator<org.apache.sling.api.resource.Resource>
orderResourceComparator
Compares two client libraries by theClientlib.PROP_ORDER
attribute, resorting to path to at least ensure a predictable order when there is no order attribute.protected List<ClientlibPermissionPlugin>
permissionPlugins
protected EnumMap<Clientlib.Type,ClientlibProcessor>
processorMap
static 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.protected static String
QUERY_CLIENTLIBS
XPath Query that matches all clientlibs.protected static String
QUERY_SUFFIX_REFERENCERS
Xpath Query suffix for a query that matches all clientlib folders referencing other stuff.protected EnumMap<Clientlib.Type,ClientlibRenderer>
rendererMap
protected org.apache.sling.api.resource.ResourceResolverFactory
resolverFactory
protected SequencerService
sequencer
static Pattern
UNMINIFIED_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 void
activate(org.osgi.service.component.ComponentContext context)
protected String
adjustEncoding(String encoding)
protected void
bindPermissionPlugin(ClientlibPermissionPlugin permissionPlugin)
void
clearCache(org.apache.sling.api.resource.ResourceResolver resolver)
Clears the whole cache for all clientlibs.protected org.apache.sling.api.resource.ResourceResolver
createAdministrativeResolver()
protected LazyCreationService.CreationStrategy
creationStrategy()
protected void
deactivate(org.osgi.service.component.ComponentContext context)
void
deliverContent(org.apache.sling.api.resource.ResourceResolver resolver, ClientlibRef clientlibRef, boolean minified, OutputStream outputStream, String encoding)
Writes the cached content to outputStream.protected String
getCachePath(ClientlibRef ref, boolean minified, String encoding)
Uses the category if given, or the single clientlib otherwise.protected @NotNull List<ResourceFilter>
getCategoryPermissionFilters(String category)
Retrieves theResourceFilter
s for a category from allClientlibPermissionPlugin
s.ClientlibConfiguration.Config
getClientlibConfig()
protected ClientlibService.ClientlibInfo
getFileHints(FileHandle file, ClientlibLink link)
@NotNull org.apache.sling.api.resource.Resource
getMinifiedSibling(@NotNull org.apache.sling.api.resource.Resource resource)
protected String
getMinifiedSibling(String path)
protected String
getUnminifiedSibling(String path)
protected LazyCreationService.InitializationStrategy
initializationStrategy(ClientlibRef clientlibRef, String encoding, String hash, ProcessorContext context)
protected boolean
isAncestorOrSelf(@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 boolean
isClientlibPermitted(@NotNull List<ResourceFilter> filters, org.apache.sling.api.resource.Resource clientlibResource)
Checks whether a clientlib matches allResourceFilter
s for the given category.protected boolean
isReachableFrom(List<String> paths, String path)
protected org.apache.sling.api.resource.Resource
minificationVariant(org.apache.sling.api.resource.Resource resource)
For files we use the correct sibling wrt.ClientlibService.ClientlibInfo
prepareContent(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 void
refreshSession(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.void
renderClientlibLinks(ClientlibElement clientlib, Writer writer, org.apache.sling.api.SlingHttpServletRequest request, RendererContext context)
Renders the references to the ressources of the clientlibs into the page.ClientlibElement
resolve(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.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.protected org.apache.sling.api.resource.Resource
retrieveResourceRaw(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 void
unbindPermissionPlugin(ClientlibPermissionPlugin permissionPlugin)
@Nullable String
verifyClientlibPermissions(@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_ORDER
attribute, 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:ClientlibService
Resolves the element corresponding to the ref.- Specified by:
resolve
in 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:
getMinifiedSibling
in 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 thecategoryToPathCache
for 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 theResourceFilter
s for a category from allClientlibPermissionPlugin
s.
-
isClientlibPermitted
protected boolean isClientlibPermitted(@NotNull @NotNull List<ResourceFilter> filters, org.apache.sling.api.resource.Resource clientlibResource)
Checks whether a clientlib matches allResourceFilter
s for the given category.
-
createAdministrativeResolver
protected org.apache.sling.api.resource.ResourceResolver createAdministrativeResolver()
-
getClientlibConfig
public ClientlibConfiguration.Config getClientlibConfig()
- Specified by:
getClientlibConfig
in 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:ClientlibService
Renders the references to the ressources of the clientlibs into the page.- Specified by:
renderClientlibLinks
in interfaceClientlibService
- Parameters:
clientlib
- aClientlib
orClientlibCategory
- Throws:
IOException
javax.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:
prepareContent
in interfaceClientlibService
clientlibRef
- 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:
IOException
javax.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.RepositoryException
Description copied from interface:ClientlibService
Writes the cached content to outputStream.- Specified by:
deliverContent
in interfaceClientlibService
clientlibRef
- reference to the clientlib / category to renderminified
- when true, the minified versionoutputStream
- the stream to write toencoding
- the needed encoding, if applicable- Throws:
IOException
javax.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:ClientlibService
Generates 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:
verifyClientlibPermissions
in 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.PersistenceException
Description copied from interface:ClientlibService
Clears the whole cache for all clientlibs. Obviously something to used sparingly.- Specified by:
clearCache
in 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.
-
-