Class DefaultClientlibService

    • Field Detail

      • 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
      • CRUD_CACHE_FOLDER_PROPS

        public static final Map<String,​Object> CRUD_CACHE_FOLDER_PROPS
      • resolverFactory

        protected org.apache.sling.api.resource.ResourceResolverFactory resolverFactory
      • 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 the Clientlib.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
    • Constructor Detail

      • DefaultClientlibService

        public DefaultClientlibService()
    • Method Detail

      • 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 interface ClientlibService
        Returns:
        the corresponding element or null if we can't find it.
      • 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

        protected String getMinifiedSibling​(String path)
      • getUnminifiedSibling

        protected String getUnminifiedSibling​(String path)
      • getMinifiedSibling

        @NotNull
        public @NotNull org.apache.sling.api.resource.Resource getMinifiedSibling​(@NotNull
                                                                                  @NotNull org.apache.sling.api.resource.Resource resource)
        Specified by:
        getMinifiedSibling in interface ClientlibService
      • 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 the categoryToPathCache for caching the paths to avoid executing a query each time. We retrieve the paths with an createAdministrativeResolver() 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)
      • isClientlibPermitted

        protected boolean isClientlibPermitted​(@NotNull
                                               @NotNull List<ResourceFilter> filters,
                                               org.apache.sling.api.resource.Resource clientlibResource)
        Checks whether a clientlib matches all ResourceFilters for the given category.
      • createAdministrativeResolver

        protected org.apache.sling.api.resource.ResourceResolver createAdministrativeResolver()
      • 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 interface ClientlibService
        clientlibRef - reference to the clientlib / category to render
        minified - when true, the minified version
        rawEncoding - the needed encoding, if applicable
        forceRefreshCache - if true, the cache will be refreshed even if it is up to date
        requestedHash - the hash value the client requested
        ifModifiedSince - the value of the If-Modified-Since header, if present
        Returns:
        information about the file
        Throws:
        IOException
        javax.jcr.RepositoryException
      • 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 interface ClientlibService
        clientlibRef - reference to the clientlib / category to render
        minified - when true, the minified version
        outputStream - the stream to write to
        encoding - 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.
      • adjustEncoding

        protected String adjustEncoding​(String encoding)
      • 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 interface ClientlibService
        Parameters:
        requestedType - if not null, only clientlibs / files of that type are checked
        userResolver - if not null, we use this resolver instead of an anonymous resolver to check readability
        onlyErrors - 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 interface ClientlibService
        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.
      • isReachableFrom

        protected boolean isReachableFrom​(List<String> paths,
                                          String path)
      • isAncestorOrSelf

        protected boolean isAncestorOrSelf​(@Nullable
                                           @Nullable String parentPath,
                                           @Nullable
                                           @Nullable String childPath)
        Returns true if the parent is an {ancestor} of the {resource} (and both are not null, of course.