public class ResourceUtil
extends java.lang.Object
ResourceUtil
class provides helper methods dealing with
resources.
This class is not intended to be extended or instantiated because it just provides static utility methods not intended to be overwritten.
Modifier and Type | Class and Description |
---|---|
static class |
ResourceUtil.BatchResourceRemover
A batch resource remover deletes resources in batches.
|
Constructor and Description |
---|
ResourceUtil() |
Modifier and Type | Method and Description |
---|---|
static <T> @NotNull java.util.Iterator<T> |
adaptTo(@NotNull java.util.Iterator<Resource> iterator,
java.lang.Class<T> type)
Return an iterator for objects of the specified type.
|
static java.lang.String |
createUniqueChildName(Resource parent,
java.lang.String name)
Create a unique name for a child of the
parent . |
static @Nullable java.lang.String |
findResourceSuperType(@NotNull Resource resource)
Deprecated.
|
static @NotNull ResourceUtil.BatchResourceRemover |
getBatchResourceRemover(int threshold)
Create a batch resource remover.
|
static @NotNull java.lang.String |
getName(@NotNull Resource rsrc)
Deprecated.
since 2.1.0, use
Resource.getName() instead |
static @NotNull java.lang.String |
getName(@NotNull java.lang.String path)
Utility method returns the name of the given
path , which is
normalized by normalize(String) before resolving the name. |
static @NotNull Resource |
getOrCreateResource(@NotNull ResourceResolver resolver,
@NotNull java.lang.String path,
java.util.Map<java.lang.String,java.lang.Object> resourceProperties,
java.lang.String intermediateResourceType,
boolean autoCommit)
Creates or gets the resource at the given path.
|
static @NotNull Resource |
getOrCreateResource(@NotNull ResourceResolver resolver,
@NotNull java.lang.String path,
java.lang.String resourceType,
java.lang.String intermediateResourceType,
boolean autoCommit)
Creates or gets the resource at the given path.
|
static @Nullable Resource |
getParent(@NotNull Resource rsrc)
Deprecated.
since 2.1.0, use
Resource.getParent() instead |
static @Nullable java.lang.String |
getParent(@NotNull java.lang.String path)
Utility method returns the parent path of the given
path ,
which is normalized by normalize(String) before resolving the
parent. |
static java.lang.String |
getParent(java.lang.String path,
int level)
Utility method returns the ancestor's path at the given
level
relative to path , which is normalized by normalize(String)
before resolving the ancestor. |
static @Nullable java.lang.String |
getResourceSuperType(@NotNull ResourceResolver resourceResolver,
java.lang.String resourceType)
Deprecated.
|
static @NotNull ValueMap |
getValueMap(Resource res)
Returns an
ValueMap object for the given
Resource . |
static boolean |
isA(@NotNull Resource resource,
java.lang.String resourceType)
Deprecated.
|
static boolean |
isNonExistingResource(@NotNull Resource res)
Returns
true if the resource res is a
non-existing resource. |
static boolean |
isStarResource(@NotNull Resource res)
Returns
true if the resource res is a "star
resource". |
static boolean |
isSyntheticResource(@NotNull Resource res)
Returns
true if the resource res is a synthetic
resource. |
static @NotNull java.util.Iterator<Resource> |
listChildren(@NotNull Resource parent)
Deprecated.
since 2.1.0, use
Resource.listChildren() instead |
static @Nullable java.lang.String |
normalize(@NotNull java.lang.String path)
Resolves relative path segments '.' and '..' in the absolute path.
|
static @NotNull java.lang.String |
resourceTypeToPath(@NotNull java.lang.String type)
Helper method, which returns the given resource type as returned from the
Resource.getResourceType() as a
relative path. |
static @NotNull Resource |
unwrap(@NotNull Resource rsrc)
Unwrap the resource and return the wrapped implementation.
|
@Nullable public static @Nullable java.lang.String normalize(@NotNull @NotNull java.lang.String path)
null
if not possible (.. points above root) or if path is not
absolute.path
- The path to normalizenull
.@Nullable public static @Nullable java.lang.String getParent(@NotNull @NotNull java.lang.String path)
path
,
which is normalized by normalize(String)
before resolving the
parent.path
- The path whose parent is to be returned.null
if path
is the root path (
/
) or if path
is a single name
containing no slash (/
) characters.java.lang.IllegalArgumentException
- If the path cannot be normalized by the
normalize(String)
method.java.lang.NullPointerException
- If path
is null
.public static java.lang.String getParent(java.lang.String path, int level)
level
relative to path
, which is normalized by normalize(String)
before resolving the ancestor.
level
= 0 returns the path
.level
= 1 returns the parent of path
, if it exists, null
otherwise.level
= 2 returns the grandparent of path
, if it exists, null
otherwise.path
- The path whose ancestor is to be returned.level
- The relative level of the ancestor, relative to path
.null
if path
doesn't have an ancestor at the
specified level
.java.lang.IllegalArgumentException
- If the path cannot be normalized by the
normalize(String)
method or if level
< 0.java.lang.NullPointerException
- If path
is null
.@Deprecated @Nullable public static @Nullable Resource getParent(@NotNull @NotNull Resource rsrc)
Resource.getParent()
insteadrsrc
- The resource to get the parent of.java.lang.NullPointerException
- If rsrc
is null
.SlingException
- If an error occurs trying to
get the resource object from the path.java.lang.IllegalStateException
- if the resource resolver has already been
closed}.@Deprecated @NotNull public static @NotNull java.lang.String getName(@NotNull @NotNull Resource rsrc)
Resource.getName()
insteadrsrc
- The resource to get the name from.java.lang.NullPointerException
- If rsrc
is null
.@NotNull public static @NotNull java.lang.String getName(@NotNull @NotNull java.lang.String path)
path
, which is
normalized by normalize(String)
before resolving the name.path
- The path whose name (the last path element) is to be
returned.path
is the root path (
/
) or if path
is a single name
containing no slash (/
) characters.java.lang.IllegalArgumentException
- If the path cannot be normalized by the
normalize(String)
method.java.lang.NullPointerException
- If path
is null
.public static boolean isSyntheticResource(@NotNull @NotNull Resource res)
true
if the resource res
is a synthetic
resource.
This method checks whether the resource is an instance of the
org.apache.sling.resource.SyntheticResource
class.
res
- The Resource
to check whether it is a synthetic
resource.true
if res
is a synthetic resource.
false
is returned if res
is
null
or not an instance of the
org.apache.sling.resource.SyntheticResource
class.public static boolean isStarResource(@NotNull @NotNull Resource res)
true
if the resource res
is a "star
resource". A star resource is a resource returned from the
ResourceResolver.resolve(HttpServletRequest)
whose path
terminates in a /*
. Generally such resource result from
requests to something like /some/path/*
or
/some/path/*.html
which may be used web applications to
uniformly handle resources to be created.
This method checks whether the resource path ends with a /*
indicating such a star resource.
res
- The Resource
to check whether it is a star
resource.true
if res
is to be considered a star
resource.java.lang.NullPointerException
- if res
is null
.public static boolean isNonExistingResource(@NotNull @NotNull Resource res)
true
if the resource res
is a
non-existing resource.
This method checks the resource type of the resource to match the
well-known resource type sling:nonexisting
of the
NonExistingResource
class defined in the Sling API.
res
- The Resource
to check whether it is a
non-existing resource.true
if res
is to be considered a
non-existing resource.java.lang.NullPointerException
- if res
is null
.@Deprecated @NotNull public static @NotNull java.util.Iterator<Resource> listChildren(@NotNull @NotNull Resource parent)
Resource.listChildren()
insteadIterator
of Resource
objects loaded from
the children of the given Resource
.
This is a convenience method for
ResourceResolver.listChildren(Resource)
.
parent
- The Resource
whose children are requested.Iterator
of Resource
objects.java.lang.NullPointerException
- If parent
is null
.java.lang.IllegalStateException
- if the resource resolver has already been
closed}.SlingException
- If any error occurs acquiring
the child resource iterator.ResourceResolver.listChildren(Resource)
@NotNull public static @NotNull ValueMap getValueMap(Resource res)
ValueMap
object for the given
Resource
. This method calls Resource.getValueMap()
.
If null
is provided as the resource an empty map is returned as
well.
For backward compatibility reasons the map returned is not immutable, but it is not recommend to rely on this behavior.
res
- The Resource
to adapt to the value map.@NotNull public static @NotNull java.lang.String resourceTypeToPath(@NotNull @NotNull java.lang.String type)
Resource.getResourceType()
as a
relative path.type
- The resource type to be converted into a path@Deprecated @Nullable public static @Nullable java.lang.String getResourceSuperType(@NotNull @NotNull ResourceResolver resourceResolver, java.lang.String resourceType)
ResourceResolver.getParentResourceType(String)
resourceTypeToPath(String)
and uses the
resourceResolver
to get the corresponding resource. If the
resource exists, the Resource.getResourceSuperType()
method is
called.resourceResolver
- The ResourceResolver
used to access
the resource whose path (relative or absolute) is given by the
resourceType
parameter.resourceType
- The resource type whose super type is to be returned.
This type is turned into a path by calling the
resourceTypeToPath(String)
method before trying to
get the resource through the resourceResolver
.resourceType
or
null
if the resource type does not exists or returns
null
for its super type.java.lang.IllegalStateException
- if the resource resolver has already been
closed}.@Deprecated @Nullable public static @Nullable java.lang.String findResourceSuperType(@NotNull @NotNull Resource resource)
ResourceResolver.getParentResourceType(Resource)
Resource.getResourceSuperType()
. If that returns
null
getResourceSuperType(ResourceResolver, String)
is invoked with the resource type of the resource.resource
- The resource to return the resource super type for.resource
or null
if no super type could be computed.java.lang.IllegalStateException
- if the resource resolver has already been
closed}.@Deprecated public static boolean isA(@NotNull @NotNull Resource resource, java.lang.String resourceType)
ResourceResolver.isResourceType(Resource, String)
resource
- the resource to checkresourceType
- the resource type to check the resource againstfalse
if resource
is null
.
Otherwise returns the result of calling
Resource.isResourceType(String)
with the given
resourceType
.java.lang.IllegalStateException
- if the resource resolver has already been
closed}.@NotNull public static <T> @NotNull java.util.Iterator<T> adaptTo(@NotNull @NotNull java.util.Iterator<Resource> iterator, java.lang.Class<T> type)
Adaptable.adaptTo(Class)
. If a resource in the original
iterator is not adaptable to the given class, this object is skipped.
This implies that the number of objects returned by the new iterator
might be less than the number of resource objects.T
- The adapted typeiterator
- A resource iterator.type
- The adapted type@NotNull public static @NotNull Resource getOrCreateResource(@NotNull @NotNull ResourceResolver resolver, @NotNull @NotNull java.lang.String path, java.lang.String resourceType, java.lang.String intermediateResourceType, boolean autoCommit) throws PersistenceException
resolver
- The resource resolver to use for creationpath
- The full path to be createdresourceType
- The optional resource type of the final resource to createintermediateResourceType
- THe optional resource type of all intermediate resourcesautoCommit
- If set to true, a commit is performed after each resource creation.SlingException
- If an error occurs trying to
get/create the resource object from the path.java.lang.IllegalStateException
- if the resource resolver has already been
closed}.PersistenceException
- If a persistence error occurs.@NotNull public static @NotNull Resource getOrCreateResource(@NotNull @NotNull ResourceResolver resolver, @NotNull @NotNull java.lang.String path, java.util.Map<java.lang.String,java.lang.Object> resourceProperties, java.lang.String intermediateResourceType, boolean autoCommit) throws PersistenceException
ResourceResolver.revert()
is called on the resolver before the
creation is retried.resolver
- The resource resolver to use for creationpath
- The full path to be createdresourceProperties
- The optional resource properties of the final resource to createintermediateResourceType
- THe optional resource type of all intermediate resourcesautoCommit
- If set to true, a commit is performed after each resource creation.SlingException
- If an error occurs trying to
get/create the resource object from the path.java.lang.IllegalStateException
- if the resource resolver has already been
closed}.PersistenceException
- If a persistence error occurs.public static java.lang.String createUniqueChildName(Resource parent, java.lang.String name) throws PersistenceException
parent
.
Creates a unique name and test if child already exists.
If child resource with the same name exists, iterate until a unique one is found.parent
- The parent resourcename
- The name of the child resourceparent
PersistenceException
- if it can not find unique name for child resource.java.lang.NullPointerException
- if parent
is nulljava.lang.IllegalStateException
- if the resource resolver has already been
closed}.@NotNull public static @NotNull Resource unwrap(@NotNull @NotNull Resource rsrc)
rsrc
- The resource to unwrap@NotNull public static @NotNull ResourceUtil.BatchResourceRemover getBatchResourceRemover(int threshold)
threshold
- The threshold for the intermediate saves.Copyright © 2018 The Apache Software Foundation. All rights reserved.