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.
Within the telemetry and registration endpoints the remaining two segments have the following semantics:
- the tenant ID
- an (optional) device ID
The basic scheme is
<endpoint>/[tenant]/[device-id]
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.static ResourceIdentifier
fromStringAssumingDefaultTenant(String resource)
Creates a resource identifier from its string representation assuming the default tenant.String
getBasePath()
Gets a string representation of this resource identifier's endpoint and tenantId.String
getEndpoint()
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()
boolean
hasEventEndpoint()
Checks if this resource identifier contains the event endpoint.int
hashCode()
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
-
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.
-
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 device ID.
- Parameters:
resource
- the resource string to parse.- Returns:
- the resource identifier.
- Throws:
NullPointerException
- if the given string isnull
.IllegalArgumentException
- if the given string does not represent a valid resource identifier.
-
fromStringAssumingDefaultTenant
public static ResourceIdentifier fromStringAssumingDefaultTenant(String resource)
Creates a resource identifier from its string representation assuming the default tenant.The given string is split up into segments using a forward slash as the separator. The first segment is used as the endpoint and the second segment (if present) is used as the device ID. The tenant ID is always set to
Constants.DEFAULT_TENANT
.- Parameters:
resource
- the resource string to parse.- Returns:
- the resource identifier.
- Throws:
NullPointerException
- if the given string isnull
.IllegalArgumentException
- if the given string does not represent a valid resource identifier.
-
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 resource identifier (may benull
).- Returns:
- the resource identifier.
- Throws:
NullPointerException
- if endpoint isnull
.
-
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 resource identifier (may benull
).- Returns:
- the resource identifier.
- Throws:
NullPointerException
- if endpoint 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 anull
segment.
-
getEndpoint
public String getEndpoint()
- Returns:
- the endpoint
-
getTenantId
public String getTenantId()
- 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
.
-
-