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.TypeinheritanceTypeprotected CalendarlastModifiedprotected @NotNull org.apache.sling.api.resource.ValueMappropertiesprotected org.apache.sling.api.resource.Resourceresourceprotected booleanuseNodeInheritance
-
Constructor Summary
Constructors Modifier Constructor Description protectedResourceHandle(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)ResourceHandleclone()Returns a shallow clone, if you want to modify attributes likesetInheritanceType(InheritedValues.Type)without affecting the original.ResourceHandlefindUpwards(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> TgetContentProperty(String key, Class<T> type)Returns a property of thegetContentResource().<T> TgetContentProperty(String key, T defaultValue)Returns a property of thegetContentResource().ResourceHandlegetContentResource()Retrieves the 'content' resource of this resource.StringgetId()Lazy getter for the ID of the resources.<T> TgetInherited(String key, Class<T> type)<T> TgetInherited(String key, Class<T> type, InheritedValues.Type inheritanceType)<T> TgetInherited(String key, T defaultValue)<T> TgetInherited(String key, T defaultValue, InheritedValues.Type inheritanceType)InheritedValuesgetInheritedValues()InheritedValuesgetInheritedValues(InheritedValues.Type type)CalendargetLastModified()StringgetName()javax.jcr.NodegetNode()lazy getter for the node of this resource (if present, not useful for synthetic resources)@Nullable ResourceHandlegetParent()@Nullable ResourceHandlegetParent(int distance)Returns the {distance}-th parent - getParent(1) is just getParent().StringgetParentPath()StringgetPath()StringgetPrimaryType()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.ValueMapgetProperties()StringgetProperty(String key)<T> TgetProperty(String key, Class<T> type)<T> TgetProperty(String key, T defaultValue)StringgetResourceName()StringgetResourceTitle()StringgetResourceType()StringgetStringId()Retrieves the inherited 'super.toString()' value as an ID.StringgetTitle()booleanisFile()Returns 'true' is this resource represents a 'file' (an asset).booleanisOfType(String type)Checks whether any of the resource's primary type, super types, sling resource type and supertypes is {resourceType}.booleanisRenderable()Returns 'true' is this resource can be displayed itself.booleanisRenderableFile()Returns 'true' is this resource represents a 'file' witch can be displayed (a HTML file).booleanisResourceType(String resourceType)booleanisSynthetic()booleanisValid()a resource is valid if not 'null' and resolvablestatic booleanisValid(@Nullable org.apache.sling.api.resource.Resource resource)the universal validation testvoidsetInheritanceType(InheritedValues.Type type)Deprecated.please preferwithInheritanceType(InheritedValues.Type)to keep this effectively immutablevoidsetProperty(String name, boolean value)voidsetProperty(String name, InputStream input)voidsetProperty(String name, Iterable<?> values, int type)voidsetProperty(String name, Iterable<String> values)voidsetProperty(String name, Object value, int type)voidsetProperty(String name, String value)voidsetProperty(String name, Calendar value)voidsetUseNodeInheritance(boolean nodeInheritance)Deprecated.please usewithInheritanceType(InheritedValues.Type)withInheritedValues.Type.sameContentto keep ResourceHandle effectively immutable.StringtoString()static @NotNull ResourceHandleuse(@Nullable org.apache.sling.api.resource.Resource resource)The 'adaptTo' like wrapping helper.ResourceHandlewithInheritanceType(InheritedValues.Type type)Returns aResourceHandlewith 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.sameContentto 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 aResourceHandlewith the given inheritance type forgetInherited(String, Class)andgetInherited(String, Object).- Parameters:
type- the type- Returns:
- the resource handle; might be
thisif 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
nullif 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:
getPrimaryTypein 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:
getNamein interfaceorg.apache.sling.api.resource.Resource- Overrides:
getNamein classorg.apache.sling.api.resource.ResourceWrapper
-
getTitle
public String getTitle()
-
getPath
public String getPath()
- Specified by:
getPathin interfaceorg.apache.sling.api.resource.Resource- Overrides:
getPathin classorg.apache.sling.api.resource.ResourceWrapper
-
isResourceType
public boolean isResourceType(String resourceType)
- Specified by:
isResourceTypein interfaceorg.apache.sling.api.resource.Resource- Overrides:
isResourceTypein classorg.apache.sling.api.resource.ResourceWrapper
-
getResourceType
public String getResourceType()
- Specified by:
getResourceTypein interfaceorg.apache.sling.api.resource.Resource- Overrides:
getResourceTypein classorg.apache.sling.api.resource.ResourceWrapper
-
getResourceName
public String getResourceName()
-
getResourceTitle
public String getResourceTitle()
-
getParent
@Nullable public @Nullable ResourceHandle getParent()
- Specified by:
getParentin interfaceorg.apache.sling.api.resource.Resource- Overrides:
getParentin 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:
toStringin 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:
adaptToin interfaceorg.apache.sling.api.adapter.Adaptable- Overrides:
adaptToin 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()
-
-