Class ResourceHandle

  • All Implemented Interfaces:
    JcrResource, Cloneable, org.apache.sling.api.adapter.Adaptable, org.apache.sling.api.resource.Resource

    public class ResourceHandle
    extends org.apache.sling.api.resource.ResourceWrapper
    implements JcrResource, Cloneable
    the wrapper to enhance the Sling Resource class
    • Field Detail

      • resource

        protected final org.apache.sling.api.resource.Resource resource
      • properties

        @NotNull
        protected final @NotNull org.apache.sling.api.resource.ValueMap properties
      • useNodeInheritance

        protected boolean useNodeInheritance
      • lastModified

        protected transient Calendar lastModified
    • Constructor Detail

      • ResourceHandle

        protected ResourceHandle​(org.apache.sling.api.resource.Resource resource)
        creates a new wrapper instance.
    • Method Detail

      • use

        @NotNull
        public static @NotNull ResourceHandle use​(@Nullable
                                                  @Nullable org.apache.sling.api.resource.Resource resource)
        The 'adaptTo' like wrapping helper.
        Parameters:
        resource - the wrapped resource. We explicitly allow null here to avoid many null checks though many ResourceWrapper methods throw a NPE then, but you need to make sure that you check isValid() if that's possible.
        Returns:
        the wrapped resource (may be resource itself if it is a ResourceHandle), not null
      • isValid

        public static boolean isValid​(@Nullable
                                      @Nullable org.apache.sling.api.resource.Resource resource)
        the universal validation test
      • isValid

        public boolean isValid()
        a resource is valid if not 'null' and resolvable
      • getProperty

        public <T> T getProperty​(String key,
                                 T defaultValue)
      • getProperty

        public <T> T getProperty​(String key,
                                 Class<T> type)
      • getProperties

        @NotNull
        public @NotNull org.apache.sling.api.resource.ValueMap getProperties()
      • setProperty

        public void setProperty​(String name,
                                String value)
                         throws javax.jcr.RepositoryException
        Throws:
        javax.jcr.RepositoryException
      • setProperty

        public void setProperty​(String name,
                                boolean value)
                         throws javax.jcr.RepositoryException
        Throws:
        javax.jcr.RepositoryException
      • setProperty

        public void setProperty​(String name,
                                Calendar value)
                         throws javax.jcr.RepositoryException
        Throws:
        javax.jcr.RepositoryException
      • setProperty

        public void setProperty​(String name,
                                Object value,
                                int type)
                         throws javax.jcr.RepositoryException
        Throws:
        javax.jcr.RepositoryException
      • setProperty

        public void setProperty​(String name,
                                InputStream input)
                         throws javax.jcr.RepositoryException
        Throws:
        javax.jcr.RepositoryException
      • setProperty

        public void setProperty​(String name,
                                Iterable<String> values)
                         throws javax.jcr.RepositoryException
        Throws:
        javax.jcr.RepositoryException
      • setProperty

        public void setProperty​(String name,
                                Iterable<?> values,
                                int type)
                         throws javax.jcr.RepositoryException
        Throws:
        javax.jcr.RepositoryException
      • getContentResource

        public ResourceHandle getContentResource()
        Retrieves the 'content' resource of this resource. Normally the content resource is the resource ot the 'jcr:content' subnode if this resource ist not the content resource itself.
        Returns:
        the content resource if present or self
      • getContentProperty

        public <T> T getContentProperty​(String key,
                                        T defaultValue)
        Returns a property of the getContentResource().
      • getInherited

        public <T> T getInherited​(String key,
                                  T defaultValue)
      • getInherited

        public <T> T getInherited​(String key,
                                  Class<T> type)
      • getNode

        public javax.jcr.Node getNode()
        lazy getter for the node of this resource (if present, not useful for synthetic resources)
        Returns:
        the node object or null if not available
      • getPrimaryType

        public String getPrimaryType()
        retrieves the primary type of the resources node; is using the 'getPrimaryType()' method if the wrapped resource is a JcrResource.
        Specified by:
        getPrimaryType in interface JcrResource
      • isOfType

        public boolean isOfType​(String type)
        Checks whether any of the resource's primary type, super types, sling resource type and supertypes is {resourceType}.
      • getId

        public String getId()
        Lazy getter for the ID of the resources. The ID is the UUID of the resources node if available otherwise the Base64 encoded path.
        Returns:
        a hopefully useful ID (not null)
      • getStringId

        public String getStringId()
        Retrieves the inherited 'super.toString()' value as an ID.
      • getName

        public String getName()
        Specified by:
        getName in interface org.apache.sling.api.resource.Resource
        Overrides:
        getName in class org.apache.sling.api.resource.ResourceWrapper
      • getTitle

        public String getTitle()
      • getPath

        public String getPath()
        Specified by:
        getPath in interface org.apache.sling.api.resource.Resource
        Overrides:
        getPath in class org.apache.sling.api.resource.ResourceWrapper
      • isResourceType

        public boolean isResourceType​(String resourceType)
        Specified by:
        isResourceType in interface org.apache.sling.api.resource.Resource
        Overrides:
        isResourceType in class org.apache.sling.api.resource.ResourceWrapper
      • getResourceType

        public String getResourceType()
        Specified by:
        getResourceType in interface org.apache.sling.api.resource.Resource
        Overrides:
        getResourceType in class org.apache.sling.api.resource.ResourceWrapper
      • getResourceName

        public String getResourceName()
      • getResourceTitle

        public String getResourceTitle()
      • getParent

        @Nullable
        public @Nullable ResourceHandle getParent()
        Specified by:
        getParent in interface org.apache.sling.api.resource.Resource
        Overrides:
        getParent in class org.apache.sling.api.resource.ResourceWrapper
      • getParent

        @Nullable
        public @Nullable ResourceHandle getParent​(int distance)
        Returns the {distance}-th parent - getParent(1) is just getParent().
      • getParentPath

        public String getParentPath()
      • findUpwards

        public ResourceHandle findUpwards​(String basePath,
                                          Pattern namePattern,
                                          String childType)
        Retrieves a child of this resource or a parent specified by its base path, name pattern and type; for example findUpwards("jcr:content", Pattern.compile("^some.*$"), "sling:Folder").
      • toString

        public String toString()
        Overrides:
        toString in class org.apache.sling.api.resource.ResourceWrapper
      • isSynthetic

        public boolean isSynthetic()
        See Also:
        ResourceUtil.isSyntheticResource(Resource)
      • adaptTo

        public <AdapterType> AdapterType adaptTo​(Class<AdapterType> type)
        Specified by:
        adaptTo in interface org.apache.sling.api.adapter.Adaptable
        Overrides:
        adaptTo in class org.apache.sling.api.resource.ResourceWrapper
        See Also:
        ResourceWrapper.adaptTo(java.lang.Class)
      • getChildrenByResourceType

        public List<ResourceHandle> getChildrenByResourceType​(String resourceType)
        retrieves all children of a sling:resourceType
      • isRenderable

        public boolean isRenderable()
        Returns 'true' is this resource can be displayed itself.
      • isRenderableFile

        public boolean isRenderableFile()
        Returns 'true' is this resource represents a 'file' witch can be displayed (a HTML file).
      • isFile

        public boolean isFile()
        Returns 'true' is this resource represents a 'file' (an asset).
      • getLastModified

        public Calendar getLastModified()
      • getChildrenList

        @Nonnull
        public List<org.apache.sling.api.resource.Resource> getChildrenList()
        Returns the children as immutable list - sometimes better for iteration.