Interface Resource

  • All Superinterfaces:
    Adaptable
    All Known Implementing Classes:
    AbstractResource, NonExistingResource, ResourceWrapper, SyntheticResource

    @ProviderType
    public interface Resource
    extends Adaptable
    Resources are pieces of content on which Sling acts

    The Resource is also an Adaptable to get adapters to other types. A JCR based resource might support adapting to the JCR Node on which the resource is based.

    A Resource object is valid for as long as the ResourceResolver that provided this instance is valid. The same applies in general to all objects returned by this instance, especially those returned by a call to Adaptable.adaptTo(Class).

    All implementations must support returning a value map from getValueMap(), even if the map is empty.

    Implementor's Note: It is recommended to not implement this interface directly. Rather consider either extending from AbstractResource or ResourceWrapper. This will make sure your implementation will not be suffering from missing method problems should the Sling Resource API be extended in the future.

    • Method Detail

      • getPath

        @NotNull
        @NotNull String getPath()
        Returns the absolute path of this resource in the resource tree.
        Returns:
        The resource path
      • getName

        @NotNull
        @NotNull String getName()
        Returns the name of this resource. The name of a resource is the last segment of the path.
        Returns:
        The resource name
        Since:
        2.1 (Sling API Bundle 2.2.0)
      • getParent

        @Nullable
        @Nullable Resource getParent()
        Returns the parent resource or null if this resource represents the root of the resource tree.
        Returns:
        The parent resource or null.
        Throws:
        SlingException - If an error occurs trying to get the resource object from the path.
        IllegalStateException - if the resource resolver has already been closed}.
        Since:
        2.1 (Sling API Bundle 2.1.0)
        See Also:
        ResourceResolver.getParent(Resource)
      • listChildren

        @NotNull
        @NotNull Iterator<Resource> listChildren()
        Returns an iterator of the direct children of this resource.

        This method is a convenience and returns exactly the same resources as calling getResourceResolver().listChildren(resource).

        Returns:
        An iterator for child resources.
        Throws:
        SlingException - If an error occurs trying to get the resource iterator.
        IllegalStateException - if the resource resolver has already been closed}.
        Since:
        2.1 (Sling API Bundle 2.1.0)
        See Also:
        ResourceResolver.listChildren(Resource)
      • getChildren

        @NotNull
        @NotNull Iterable<Resource> getChildren()
        Returns an iterable of the direct children of this resource.

        This method is a convenience and returns exactly the same resources as calling getResourceResolver().getChildren(resource).

        Returns:
        An iterable for child resources
        Throws:
        SlingException - If an error occurs trying to get the resource iterator.
        IllegalStateException - if the resource resolver has already been closed}.
        Since:
        2.2 (Sling API Bundle 2.2.0)
        See Also:
        ResourceResolver.getChildren(Resource)
      • getChild

        @Nullable
        @Nullable Resource getChild​(@NotNull
                                    @NotNull String relPath)
        Returns the child at the given relative path of this resource or null if no such child exists.

        This method is a convenience and returns exactly the same resources as calling getResourceResolver().getResource(resource, relPath).

        Parameters:
        relPath - relative path to the child resource
        Returns:
        The child resource or null
        Throws:
        SlingException - If an error occurs trying to get the resource object from the path.
        IllegalStateException - if the resource resolver has already been closed}.
        Since:
        2.1 (Sling API Bundle 2.1.0)
        See Also:
        ResourceResolver.getResource(Resource, String)
      • getResourceType

        @NotNull
        @NotNull String getResourceType()
        The resource type is meant to point to rendering/processing scripts, editing dialogs, etc. It is usually a path in the repository, where scripts and other tools definitions are found, but the ResourceResolver is free to set this to any suitable value such as the primary node type of the JCR node from which the resource is created.

        If the resource instance represents a resource which is not actually existing, this method returns RESOURCE_TYPE_NON_EXISTING.

        Returns:
        The resource type
      • getResourceSuperType

        @Nullable
        @Nullable String getResourceSuperType()
        Returns the super type of the resource if the resource defines its own super type. Otherwise null is returned. A resource might return a resource super type to overwrite the resource type hierarchy. If a client is interested in the effective resource super type of a resource, it should call ResourceResolver.getParentResourceType(Resource).
        Returns:
        The super type of the resource or null.
        Throws:
        IllegalStateException - if this resource resolver has already been closed.
      • hasChildren

        boolean hasChildren()
        Checks if the resource has any child resources.
        Returns:
        true if the resource has any child resources
        Throws:
        IllegalStateException - if this resource resolver has already been closed.
        Since:
        2.4.4 (Sling API Bundle 2.5.0)
      • isResourceType

        boolean isResourceType​(String resourceType)
        Is just a shortcut for getResourceResolver().isResourceType(this, resourceType).
        Parameters:
        resourceType - the resource type to check this resource against
        Returns:
        true if the resource type or any of the resource's super type(s) equals the given resource type, false otherwise; false can also be returned if resourceType is null
        Since:
        2.1.0 (Sling API Bundle 2.1.0)
        See Also:
        ResourceResolver.isResourceType(Resource, String)
      • getResourceMetadata

        @NotNull
        @NotNull ResourceMetadata getResourceMetadata()
        Returns the meta data of this resource. The concrete data contained in the ResourceMetadata object returned is implementation specific except for the ResourceMetadata.RESOLUTION_PATH property which is required to be set to the part of the request URI used to resolve the resource.
        Returns:
        The resource meta data
        See Also:
        ResourceMetadata
      • getResourceResolver

        @NotNull
        @NotNull ResourceResolver getResourceResolver()
        Returns the ResourceResolver from which this resource has been retrieved.
        Returns:
        The resource resolver
      • getValueMap

        @NotNull
        @NotNull ValueMap getValueMap()
        Returns a value map for this resource. The value map allows to read the properties of the resource.
        Returns:
        A value map
        Since:
        2.5 (Sling API Bundle 2.7.0)