Class SlingResourceUtil


  • public class SlingResourceUtil
    extends Object
    A set of utility functions related to the handling of Sling Resources, without going down to JCR specifics. This is a replacement for the JCR usage ridden PropertyUtil that contains all "pure" Sling functions that do not require the Resources to be JCR resources.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean addMixin​(org.apache.sling.api.resource.Resource resource, String mixin)
      Adds a mixin if it isn't there already.
      static void addMixin​(org.apache.sling.api.resource.Resource resource, String... mixins)
      Adds one or more mixins to the resource, keeping the current ones.
      static String appendPaths​(String path, String childpath)
      Appends two paths: determines the given child of a path.
      static String commonParent​(Collection<String> paths)
      Finds the longest path that is a parent of all given paths - e.g.
      static Iterable<org.apache.sling.api.resource.Resource> descendants​(org.apache.sling.api.resource.Resource resource)
      Returns an iterator that goes through all descendants of a resource, parents come before their children.
      static Stream<org.apache.sling.api.resource.Resource> descendantsStream​(org.apache.sling.api.resource.Resource resource)
      Returns a stream that goes through all descendants of a resource, parents come before their children.
      static Stream<org.apache.sling.api.resource.Resource> descendantsStream​(org.apache.sling.api.resource.Resource resource, Function<org.apache.sling.api.resource.Resource,​Boolean> leafFilter)
      Returns a stream that goes through all descendants of a resource until a filter is hit, parents come before their children.
      static Stream<org.apache.sling.api.resource.Resource> getChildrenAsStream​(org.apache.sling.api.resource.Resource resource)
      Returns the children of a resource as Stream.
      static org.apache.sling.api.resource.Resource getFirstExistingParent​(org.apache.sling.api.resource.ResourceResolver resolver, String path)
      Retrieves the first parent (including path itself) that actually exists.
      static String getPath​(org.apache.sling.api.resource.Resource resource)
      Returns the path of a resource, or null if it is null.
      static List<String> getPaths​(List<org.apache.sling.api.resource.Resource> resources)
      Returns the list of paths of a number of resources.
      static boolean isSameOrDescendant​(String parent, String descendant)
      Checks whether {descendant} is the same path as parent node or a path of a descendant of the parent node.
      static boolean isSameOrDescendant​(org.apache.sling.api.resource.Resource parent, org.apache.sling.api.resource.Resource descendant)
      Checks whether {descendant} is the same path as parent node or a path of a descendant of the parent node.
      static String relativePath​(String node, String other)
      Returns the shortest relative path that leads from a node to another node.
      static Stream<org.apache.sling.api.resource.Resource> selfAndAncestors​(org.apache.sling.api.resource.Resource r)
      Returns a stream of the resource and all its parents.
      static void setProperty​(org.apache.sling.api.resource.ModifiableValueMap valueMap, String key, Object value)
      Sets a property on a ModifiableValueMap.
    • Constructor Detail

      • SlingResourceUtil

        public SlingResourceUtil()
    • Method Detail

      • relativePath

        public static String relativePath​(@NotNull
                                          String node,
                                          @NotNull
                                          String other)
        Returns the shortest relative path that leads from a node to another node. Postcondition: ResourceUtil.normalize(node + "/" + result), is(ResourceUtil.normalize(other)). In most cases appendPaths(String, String)(node, result) will return {other}. )}

        Examples: relativePath("/foo", "/foo/bar") = "bar" ; relativePath("/foo/bar", "/foo") = ".." ; relativePath("foo", "foo") = "../bar" .

        Parameters:
        node - the parent
        other - a path of a child of parent / parent itself
        Returns:
        the (relative) path from which other can be read from node - e.g. with Resource.getChild(String). If node and other are the same, this is empty.
        Throws:
        IllegalArgumentException - in those cases where there is no sensible answer: one of the paths is empty or one absolute and one relative path
      • isSameOrDescendant

        public static boolean isSameOrDescendant​(@Nullable
                                                 String parent,
                                                 @Nullable
                                                 String descendant)
        Checks whether {descendant} is the same path as parent node or a path of a descendant of the parent node. (We don't check whether the resources exist - just check the paths.
        Parameters:
        parent - the parent or null
        descendant - the descendant or null
        Returns:
        true if descendant is a descendant of parent , false if any is null.
      • isSameOrDescendant

        public static boolean isSameOrDescendant​(@Nullable
                                                 org.apache.sling.api.resource.Resource parent,
                                                 @Nullable
                                                 org.apache.sling.api.resource.Resource descendant)
        Checks whether {descendant} is the same path as parent node or a path of a descendant of the parent node.
        Parameters:
        parent - the parent or null
        descendant - the descendant or null
        Returns:
        true if descendant is a descendant of parent , false if any is null.
      • getPath

        @Nullable
        public static String getPath​(@Nullable
                                     org.apache.sling.api.resource.Resource resource)
        Returns the path of a resource, or null if it is null. For use e.g. in logging statements.
      • getPaths

        @NotNull
        public static List<String> getPaths​(@Nullable
                                            List<org.apache.sling.api.resource.Resource> resources)
        Returns the list of paths of a number of resources. For use e.g. in logging statements.
      • addMixin

        public static boolean addMixin​(@NotNull
                                       org.apache.sling.api.resource.Resource resource,
                                       @NotNull
                                       String mixin)
        Adds a mixin if it isn't there already.
        Returns:
        true if we needed to add the mixin.
      • descendants

        @NotNull
        public static Iterable<org.apache.sling.api.resource.Resource> descendants​(@Nullable
                                                                                   org.apache.sling.api.resource.Resource resource)
        Returns an iterator that goes through all descendants of a resource, parents come before their children.
        Parameters:
        resource - a resource or null
        Returns:
        an iterable running through the resource and it's descendants, not null
      • descendantsStream

        @NotNull
        public static Stream<org.apache.sling.api.resource.Resource> descendantsStream​(@Nullable
                                                                                       org.apache.sling.api.resource.Resource resource)
        Returns a stream that goes through all descendants of a resource, parents come before their children.
        Parameters:
        resource - a resource or null
        Returns:
        a stream running through the resource and it's the descendants, not null
      • descendantsStream

        @Nonnull
        public static Stream<org.apache.sling.api.resource.Resource> descendantsStream​(@Nullable
                                                                                       org.apache.sling.api.resource.Resource resource,
                                                                                       @Nullable
                                                                                       Function<org.apache.sling.api.resource.Resource,​Boolean> leafFilter)
        Returns a stream that goes through all descendants of a resource until a filter is hit, parents come before their children.
        Parameters:
        resource - a resource or null
        leafFilter - if this returns true, this assumes the resource is a leaf and does not return it's descendants.
        Returns:
        a stream running through the resource and it's the descendants, not null
      • selfAndAncestors

        @NotNull
        public static Stream<org.apache.sling.api.resource.Resource> selfAndAncestors​(@Nullable
                                                                                      org.apache.sling.api.resource.Resource r)
        Returns a stream of the resource and all its parents.
      • appendPaths

        @Nullable
        public static String appendPaths​(@Nullable
                                         String path,
                                         @Nullable
                                         String childpath)
        Appends two paths: determines the given child of a path.
        Parameters:
        path - an absolute or relative path. We ignore if it ends with /. If it's null, we return null (there is no child of no path).
        childpath - the relative path of the child to the path. Absolute paths are treated as relative paths: we ignore starting and ending / . if empty or null we return path
        Returns:
        the paths concatenated. If path is absolute, this is absolute; if path is relative, this is relative.
      • commonParent

        @Nullable
        public static String commonParent​(@Nullable
                                          Collection<String> paths)
        Finds the longest path that is a parent of all given paths - e.g. /foo/bar for /foo/bar/a/b, /foo/bar/a/c and /foo/bar/b/c .
        Parameters:
        paths - a collection of absolute paths or a collection of relative paths. Null / empty values are ignored.
        Returns:
        null if collection is empty, otherwise the common parent. It might be an empty string if the paths have no common parent (e.g. "a" and "b" or the illegitimate call with "/a" and "b").
      • getFirstExistingParent

        @Nullable
        public static org.apache.sling.api.resource.Resource getFirstExistingParent​(@Nullable
                                                                                    org.apache.sling.api.resource.ResourceResolver resolver,
                                                                                    @Nullable
                                                                                    String path)
        Retrieves the first parent (including path itself) that actually exists.
        Parameters:
        path - an absolute path
        Returns:
        path or the longest path parent to path that exists in resolver. Null if path is empty.
      • addMixin

        public static void addMixin​(@Nullable
                                    org.apache.sling.api.resource.Resource resource,
                                    @Nullable
                                    String... mixins)
        Adds one or more mixins to the resource, keeping the current ones.
      • setProperty

        public static void setProperty​(@NotNull
                                       org.apache.sling.api.resource.ModifiableValueMap valueMap,
                                       @NotNull
                                       String key,
                                       @Nullable
                                       Object value)
        Sets a property on a ModifiableValueMap. This abbreviates the procedure if it's not known whether the value is null - Map.put(Object, Object) throws up if it is.
      • getChildrenAsStream

        @Nonnull
        public static Stream<org.apache.sling.api.resource.Resource> getChildrenAsStream​(@Nullable
                                                                                         org.apache.sling.api.resource.Resource resource)
        Returns the children of a resource as Stream.
        Parameters:
        a - resource or null
        Returns:
        stream with the children, possibly empty.