Package com.composum.sling.core
Class ResourceHandle
- java.lang.Object
-
- org.apache.sling.api.resource.ResourceWrapper
-
- com.composum.sling.core.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 Summary
Fields Modifier and Type Field Description protected InheritedValues.Type
inheritanceType
protected Calendar
lastModified
protected @NotNull org.apache.sling.api.resource.ValueMap
properties
protected org.apache.sling.api.resource.Resource
resource
protected boolean
useNodeInheritance
-
Constructor Summary
Constructors Modifier Constructor Description protected
ResourceHandle(org.apache.sling.api.resource.Resource resource)
creates a new wrapper instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description <AdapterType>
AdapterTypeadaptTo(Class<AdapterType> type)
ResourceHandle
clone()
Returns a shallow clone, if you want to modify attributes likesetInheritanceType(InheritedValues.Type)
without affecting the original.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").List<ResourceHandle>
getChildrenByResourceType(String resourceType)
retrieves all children of a sling:resourceTypeList<ResourceHandle>
getChildrenByType(String type)
retrieves all children of a typeList<org.apache.sling.api.resource.Resource>
getChildrenList()
Returns the children as immutable list - sometimes better for iteration.<T> T
getContentProperty(String key, Class<T> type)
Returns a property of thegetContentResource()
.<T> T
getContentProperty(String key, T defaultValue)
Returns a property of thegetContentResource()
.ResourceHandle
getContentResource()
Retrieves the 'content' resource of this resource.String
getId()
Lazy getter for the ID of the resources.<T> T
getInherited(String key, Class<T> type)
<T> T
getInherited(String key, Class<T> type, InheritedValues.Type inheritanceType)
<T> T
getInherited(String key, T defaultValue)
<T> T
getInherited(String key, T defaultValue, InheritedValues.Type inheritanceType)
InheritedValues
getInheritedValues()
InheritedValues
getInheritedValues(InheritedValues.Type type)
Calendar
getLastModified()
String
getName()
javax.jcr.Node
getNode()
lazy getter for the node of this resource (if present, not useful for synthetic resources)@Nullable ResourceHandle
getParent()
@Nullable ResourceHandle
getParent(int distance)
Returns the {distance}-th parent - getParent(1) is just getParent().String
getParentPath()
String
getPath()
String
getPrimaryType()
retrieves the primary type of the resources node; is using the 'getPrimaryType()' method if the wrapped resource is a JcrResource.@NotNull org.apache.sling.api.resource.ValueMap
getProperties()
String
getProperty(String key)
<T> T
getProperty(String key, Class<T> type)
<T> T
getProperty(String key, T defaultValue)
String
getResourceName()
String
getResourceTitle()
String
getResourceType()
String
getStringId()
Retrieves the inherited 'super.toString()' value as an ID.String
getTitle()
boolean
isFile()
Returns 'true' is this resource represents a 'file' (an asset).boolean
isOfType(String type)
Checks whether any of the resource's primary type, super types, sling resource type and supertypes is {resourceType}.boolean
isRenderable()
Returns 'true' is this resource can be displayed itself.boolean
isRenderableFile()
Returns 'true' is this resource represents a 'file' witch can be displayed (a HTML file).boolean
isResourceType(String resourceType)
boolean
isSynthetic()
boolean
isValid()
a resource is valid if not 'null' and resolvablestatic boolean
isValid(@Nullable org.apache.sling.api.resource.Resource resource)
the universal validation testvoid
setInheritanceType(InheritedValues.Type type)
Deprecated.please preferwithInheritanceType(InheritedValues.Type)
to keep this effectively immutablevoid
setProperty(String name, boolean value)
void
setProperty(String name, InputStream input)
void
setProperty(String name, Iterable<?> values, int type)
void
setProperty(String name, Iterable<String> values)
void
setProperty(String name, Object value, int type)
void
setProperty(String name, String value)
void
setProperty(String name, Calendar value)
void
setUseNodeInheritance(boolean nodeInheritance)
Deprecated.please usewithInheritanceType(InheritedValues.Type)
withInheritedValues.Type.sameContent
to keep ResourceHandle effectively immutable.String
toString()
static @NotNull ResourceHandle
use(@Nullable org.apache.sling.api.resource.Resource resource)
The 'adaptTo' like wrapping helper.ResourceHandle
withInheritanceType(InheritedValues.Type type)
Returns aResourceHandle
with the given inheritance type forgetInherited(String, Class)
andgetInherited(String, Object)
.
-
-
-
Field Detail
-
resource
protected final org.apache.sling.api.resource.Resource resource
-
properties
@NotNull protected final @NotNull org.apache.sling.api.resource.ValueMap properties
-
inheritanceType
protected InheritedValues.Type inheritanceType
-
useNodeInheritance
protected boolean useNodeInheritance
-
lastModified
protected transient Calendar lastModified
-
-
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 checkisValid()
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
-
clone
public ResourceHandle clone()
Returns a shallow clone, if you want to modify attributes likesetInheritanceType(InheritedValues.Type)
without affecting the original.
-
isValid
public boolean isValid()
a resource is valid if not 'null' and resolvable
-
getProperty
public <T> T getProperty(String key, T defaultValue)
-
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, Class<T> type)
Returns a property of thegetContentResource()
.
-
getContentProperty
public <T> T getContentProperty(String key, T defaultValue)
Returns a property of thegetContentResource()
.
-
setUseNodeInheritance
@Deprecated public void setUseNodeInheritance(boolean nodeInheritance)
Deprecated.please usewithInheritanceType(InheritedValues.Type)
withInheritedValues.Type.sameContent
to keep ResourceHandle effectively immutable.Sets inheritance type forgetInherited(String, Class)
andgetInherited(String, Object)
toInheritedValues.Type.sameContent
.- See Also:
clone()
,withInheritanceType(InheritedValues.Type)
-
setInheritanceType
@Deprecated public void setInheritanceType(InheritedValues.Type type)
Deprecated.please preferwithInheritanceType(InheritedValues.Type)
to keep this effectively immutableSets inheritance type forgetInherited(String, Class)
andgetInherited(String, Object)
.- Parameters:
type
- the type- See Also:
clone()
,withInheritanceType(InheritedValues.Type)
-
withInheritanceType
public ResourceHandle withInheritanceType(InheritedValues.Type type)
Returns aResourceHandle
with the given inheritance type forgetInherited(String, Class)
andgetInherited(String, Object)
.- Parameters:
type
- the type- Returns:
- the resource handle; might be
this
if the type is unchanged.
-
getInheritedValues
public InheritedValues getInheritedValues()
-
getInheritedValues
public InheritedValues getInheritedValues(InheritedValues.Type type)
-
getInherited
public <T> T getInherited(String key, T defaultValue)
-
getInherited
public <T> T getInherited(String key, T defaultValue, InheritedValues.Type inheritanceType)
-
getInherited
public <T> T getInherited(String key, Class<T> type, InheritedValues.Type inheritanceType)
-
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 interfaceJcrResource
-
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 interfaceorg.apache.sling.api.resource.Resource
- Overrides:
getName
in classorg.apache.sling.api.resource.ResourceWrapper
-
getTitle
public String getTitle()
-
getPath
public String getPath()
- Specified by:
getPath
in interfaceorg.apache.sling.api.resource.Resource
- Overrides:
getPath
in classorg.apache.sling.api.resource.ResourceWrapper
-
isResourceType
public boolean isResourceType(String resourceType)
- Specified by:
isResourceType
in interfaceorg.apache.sling.api.resource.Resource
- Overrides:
isResourceType
in classorg.apache.sling.api.resource.ResourceWrapper
-
getResourceType
public String getResourceType()
- Specified by:
getResourceType
in interfaceorg.apache.sling.api.resource.Resource
- Overrides:
getResourceType
in classorg.apache.sling.api.resource.ResourceWrapper
-
getResourceName
public String getResourceName()
-
getResourceTitle
public String getResourceTitle()
-
getParent
@Nullable public @Nullable ResourceHandle getParent()
- Specified by:
getParent
in interfaceorg.apache.sling.api.resource.Resource
- Overrides:
getParent
in classorg.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 classorg.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 interfaceorg.apache.sling.api.adapter.Adaptable
- Overrides:
adaptTo
in classorg.apache.sling.api.resource.ResourceWrapper
- See Also:
ResourceWrapper.adaptTo(java.lang.Class)
-
getChildrenByType
public List<ResourceHandle> getChildrenByType(String type)
retrieves all children of a type
-
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()
-
-