Class 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:

    1. telemetry/DEFAULT_TENANT/4711
    2. telemetry
    3. telemetry/
    4. telemetry//
    5. telemetry//4711
    6. telemetry/DEFAULT_TENANT
    7. telemetry/DEFAULT_TENANT/
    • 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 is null.
        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 be null).
        resourceId - the resourceId part (may be null).
        Returns:
        the resource identifier.
        Throws:
        NullPointerException - if endpoint is null.
        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 be null).
        resourceId - the resourceId part (may be null).
        Returns:
        the resource identifier.
        Throws:
        NullPointerException - if resourceIdentifier is null.
      • 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 is null.
        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 ("/").

        Overrides:
        toString in class Object
        Returns:
        the string representation.
      • 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()
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object