Class ResourceIdentifier
- java.lang.Object
-
- org.eclipse.hono.util.ResourceIdentifier
-
public final class ResourceIdentifier extends Object
A unique identifier for a resource within Hono.Each resource identifier consists of an arbitrary number of path segments. The first segment always contains the name of the endpoint that the resource belongs to.
The basic scheme is
<endpoint>/[tenant]/[resource-id]
. The resource-id usually represents a device identifier.Examples:
- telemetry/DEFAULT_TENANT/4711
- telemetry
- telemetry/
- telemetry//
- telemetry//4711
- telemetry/DEFAULT_TENANT
- telemetry/DEFAULT_TENANT/
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description String
elementAt(int index)
Gets the element at a given index of the resource path.boolean
equals(Object o)
static ResourceIdentifier
from(String endpoint, String tenantId, String resourceId)
Creates a resource identifier for an endpoint, a tenantId and a resourceId.static ResourceIdentifier
from(ResourceIdentifier resourceIdentifier, String tenantId, String resourceId)
Creates a resource identifier for an endpoint from an other resource identifier.static ResourceIdentifier
fromPath(String[] path)
Creates a resource identifier from path segments.static ResourceIdentifier
fromString(String resource)
Creates a resource identifier from its string representation.String
getBasePath()
Gets a string representation of this resource identifier's endpoint and tenantId.String
getEndpoint()
Gets the endpoint part of this identifier.String
getPathWithoutBase()
Gets a string representation of the resource identifiers' parts without the base path.String
getResourceId()
Gets the resourceId part of this identifier.String[]
getResourcePath()
Gets a copy of the full resource path of this identifier, including extended elements.String
getTenantId()
Gets the tenant part of this identifier.boolean
hasEventEndpoint()
Checks if this resource identifier contains the event endpoint.int
hashCode()
static boolean
isValid(String resource)
Checks whether the given string is a valid resource identifier string representation.int
length()
Gets the number of elements in the resource path.String[]
toPath()
Gets this resource identifier as path segments.String
toString()
Gets a string representation of this resource identifier.
-
-
-
Method Detail
-
isValid
public static boolean isValid(String resource)
Checks whether the given string is a valid resource identifier string representation.- Parameters:
resource
- the resource string to parse.- Returns:
true
if the given string is a valid resource identifier string.
-
fromString
public static ResourceIdentifier fromString(String resource)
Creates a resource identifier from its string representation.The given string is split up into segments using a forward slash as the separator. The first segment is used as the endpoint, the second segment is used as the tenant ID and the third segment (if present) is used as the resourceId part.
- Parameters:
resource
- the resource string to parse.- Returns:
- the resource identifier.
- Throws:
NullPointerException
- if the given string isnull
.IllegalArgumentException
- if the given string is empty or contains an empty first segment.
-
from
public static ResourceIdentifier from(String endpoint, String tenantId, String resourceId)
Creates a resource identifier for an endpoint, a tenantId and a resourceId.- Parameters:
endpoint
- the endpoint of the resource.tenantId
- the tenant identifier (may benull
).resourceId
- the resourceId part (may benull
).- Returns:
- the resource identifier.
- Throws:
NullPointerException
- if endpoint isnull
.IllegalArgumentException
- if endpoint is empty.
-
from
public static ResourceIdentifier from(ResourceIdentifier resourceIdentifier, String tenantId, String resourceId)
Creates a resource identifier for an endpoint from an other resource identifier. It uses all data from the original resource identifier but sets the new tenantId and resourceId.- Parameters:
resourceIdentifier
- original resource identifier to copy values from.tenantId
- the tenant identifier (may benull
).resourceId
- the resourceId part (may benull
).- Returns:
- the resource identifier.
- Throws:
NullPointerException
- if resourceIdentifier isnull
.
-
fromPath
public static ResourceIdentifier fromPath(String[] path)
Creates a resource identifier from path segments.- Parameters:
path
- the segments of the resource path.- Returns:
- the resource identifier.
- Throws:
NullPointerException
- if path isnull
.IllegalArgumentException
- if the path contains no segments or starts with an empty segment.
-
toPath
public String[] toPath()
Gets this resource identifier as path segments.- Returns:
- the segments.
-
elementAt
public String elementAt(int index)
Gets the element at a given index of the resource path.- Parameters:
index
- The index (starting from zero).- Returns:
- The element at the index.
- Throws:
ArrayIndexOutOfBoundsException
- if the resource path's length is shorter than the index.
-
length
public int length()
Gets the number of elements in the resource path.- Returns:
- The resource path length.
-
getEndpoint
public String getEndpoint()
Gets the endpoint part of this identifier.- Returns:
- the endpoint (not
null
or empty).
-
getTenantId
public String getTenantId()
Gets the tenant part of this identifier.- Returns:
- the tenantId or
null
if not set.
-
getResourceId
public String getResourceId()
Gets the resourceId part of this identifier.E.g. for a resource telemetry/DEFAULT_TENANT/4711, the return value will be 4711.
- Returns:
- the resourceId or
null
if not set.
-
getResourcePath
public String[] getResourcePath()
Gets a copy of the full resource path of this identifier, including extended elements.- Returns:
- The full resource path.
-
toString
public String toString()
Gets a string representation of this resource identifier.The string representation consists of all path segments separated by a forward slash ("/").
-
getBasePath
public String getBasePath()
Gets a string representation of this resource identifier's endpoint and tenantId.E.g. for a resource telemetry/DEFAULT_TENANT/4711, the return value will be telemetry/DEFAULT_TENANT.
- Returns:
- A string consisting of the properties separated by a forward slash.
-
getPathWithoutBase
public String getPathWithoutBase()
Gets a string representation of the resource identifiers' parts without the base path.E.g. for a resource command_response/myTenant/deviceId/some/path, the return value will be deviceId/some/path.
If this resource identifier doesn't contain any additional path segments after the base path, an empty string is returned.
- Returns:
- A string with all parts after the base bath.
- See Also:
getBasePath()
-
hasEventEndpoint
public boolean hasEventEndpoint()
Checks if this resource identifier contains the event endpoint.- Returns:
true
if this resource's endpoint is eitherEventConstants.EVENT_ENDPOINT
orEventConstants.EVENT_ENDPOINT_SHORT
.
-
-