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.

    Within the telemetry and registration endpoints the remaining two segments have the following semantics:

    1. the tenant ID
    2. an (optional) device ID

    The basic scheme is <endpoint>/[tenant]/[device-id]

    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

      • 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 is null.
      • fromStringAssumingDefaultTenant

        @Deprecated
        public static ResourceIdentifier fromStringAssumingDefaultTenant​(String resource)
        Deprecated.
        This method will be removed in Hono 1.5.
        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 is null.
      • 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 resource identifier (may be null).
        Returns:
        the resource identifier.
        Throws:
        NullPointerException - if endpoint is null.
      • 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 resource identifier (may be null).
        Returns:
        the resource identifier.
        Throws:
        NullPointerException - if endpoint 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 a null 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 ("/").

        Overrides:
        toString in class Object
        Returns:
        the resource id.
      • 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