Class HalResource

  • All Implemented Interfaces:
    HalObject

    @ProviderType
    public final class HalResource
    extends Object
    implements HalObject
    Bean representation of a HAL resource.
    • Constructor Detail

      • HalResource

        public HalResource()
        Create an empty HAL resource, with no object state or links
      • HalResource

        public HalResource​(String uri)
        Create a HAL resource with empty state that only contains a self link with the given URI
        Parameters:
        uri - the URI under which this resource can be retrieved
      • HalResource

        public HalResource​(com.fasterxml.jackson.databind.JsonNode model)
        Create a new HalResource with the state from the given JSON object
        Parameters:
        model - JSON model - must be an ObjectNode
        Throws:
        IllegalArgumentException - if model is not an object node
      • HalResource

        public HalResource​(com.fasterxml.jackson.databind.node.ObjectNode model)
        Create a new HalResource with the state from the given JSON object
        Parameters:
        model - JSON model - must be an ObjectNode
        Throws:
        IllegalArgumentException - if model is not an object node
      • HalResource

        public HalResource​(Object pojo)
        Create a new HalResource with the state from the given POJO
        Parameters:
        pojo - a simple java object that will be mapped by a standard jackson ObjectMapper
        Throws:
        IllegalArgumentException - if the object can not be converted to a Jackson JSON object
      • HalResource

        public HalResource​(com.fasterxml.jackson.databind.JsonNode model,
                           String uri)
        Create a new HalResource with the state from the given JSON object
        Parameters:
        model - JSON model - must be an ObjectNode
        Throws:
        IllegalArgumentException - if model is not an object node
      • HalResource

        public HalResource​(Object pojo,
                           String uri)
        Create a new HalResource with the state from the given POJO
        Parameters:
        pojo - a simple java object that will be mapped by a standard jackson ObjectMapper
        uri - the URI under which this resource can be retrieved
        Throws:
        IllegalArgumentException - if the object can not be converted to a Jackson JSON object
    • Method Detail

      • getModel

        public com.fasterxml.jackson.databind.node.ObjectNode getModel()
        Description copied from interface: HalObject
        Returns the JSON model.
        Specified by:
        getModel in interface HalObject
        Returns:
        Model
      • adaptTo

        public <T> T adaptTo​(Class<T> type)
        Type Parameters:
        T - return type
        Parameters:
        type - a class that matches the structure of this resource's model
        Returns:
        a new instance of the given class, populated with the properties of this resource's model
      • hasLink

        public boolean hasLink​(String relation)
        Parameters:
        relation - Link relation
        Returns:
        True if has link for the given relation
      • hasEmbedded

        public boolean hasEmbedded​(String relation)
        Parameters:
        relation - Embedded resource relation
        Returns:
        True if has embedded resource for the given relation
      • getLink

        public Link getLink()
        Returns:
        Self link for the resource. Can be null
      • getLinks

        public com.google.common.collect.ListMultimap<String,​Link> getLinks()
        Returns:
        All links
      • getEmbedded

        public com.google.common.collect.ListMultimap<String,​HalResource> getEmbedded()
        Returns:
        All embedded resources
      • getLink

        public Link getLink​(String relation)
        Parameters:
        relation - Link relation
        Returns:
        Link for the given relation
      • getLinks

        public List<Link> getLinks​(String relation)
        Parameters:
        relation - Link relation
        Returns:
        All links for the given relation
      • collectLinks

        public List<Link> collectLinks​(String rel)
        recursively collects links within this resource and all embedded resources
        Parameters:
        rel - the relation your interested in
        Returns:
        a list of all links
      • getEmbeddedResource

        public HalResource getEmbeddedResource​(String relation)
        Parameters:
        relation - Embedded resource relation
        Returns:
        Embedded resources for the given relation
      • getEmbedded

        public List<HalResource> getEmbedded​(String relation)
        Parameters:
        relation - Embedded resource relation
        Returns:
        All embedded resources for the given relation
      • collectEmbedded

        public List<HalResource> collectEmbedded​(String rel)
        recursively collects embedded resources of a specific rel
        Parameters:
        rel - the relation your interested in
        Returns:
        a list of all embedded resources
      • setLink

        public HalResource setLink​(Link link)
        Sets link for the self relation. Overwrites existing one.
        Parameters:
        link - Link to set
        Returns:
        HAL resource
      • setLink

        public HalResource setLink​(String relation,
                                   Link link)
        Sets link for the given relation. Overwrites existing one. If link is null it gets ignored.
        Parameters:
        relation - Link relation
        link - Link to add
        Returns:
        HAL resource
      • addLinks

        public HalResource addLinks​(String relation,
                                    Link... links)
        Adds links for the given relation
        Parameters:
        relation - Link relation
        links - Links to add
        Returns:
        HAL resource
      • addLinks

        public HalResource addLinks​(String relation,
                                    Iterable<Link> links)
        Adds links for the given relation
        Parameters:
        relation - Link relation
        links - Links to add
        Returns:
        HAL resource
      • setEmbedded

        public HalResource setEmbedded​(String relation,
                                       HalResource resource)
        Embed resource for the given relation. Overwrites existing one.
        Parameters:
        relation - Embedded resource relation
        resource - Resource to embed
        Returns:
        HAL resource
      • addEmbedded

        public HalResource addEmbedded​(String relation,
                                       HalResource... resources)
        Embed resources for the given relation
        Parameters:
        relation - Embedded resource relation
        resources - Resources to embed
        Returns:
        HAL resource
      • addEmbedded

        public HalResource addEmbedded​(String relation,
                                       Iterable<HalResource> resources)
        Embed resources for the given relation
        Parameters:
        relation - Embedded resource relation
        resources - Resources to embed
        Returns:
        HAL resource
      • removeLinks

        public HalResource removeLinks​(String relation)
        Removes all links for the given relation.
        Parameters:
        relation - Link relation
        Returns:
        HAL resource
      • removeEmbedded

        public HalResource removeEmbedded​(String relation)
        Removes all embedded resources for the given relation.
        Parameters:
        relation - Embedded resource relation
        Returns:
        HAL resource
      • removeLink

        public HalResource removeLink​(String relation,
                                      int index)
        Removes one link for the given relation and index.
        Parameters:
        relation - Link relation
        index - Array index
        Returns:
        HAL resource
      • removeLinkWithHref

        public HalResource removeLinkWithHref​(String relation,
                                              String href)
        Remove the link with the given relation and href
        Parameters:
        relation - Link relation
        href - to identify the link to remove
        Returns:
        this HAL resource
      • removeEmbedded

        public HalResource removeEmbedded​(String relation,
                                          int index)
        Removes one embedded resource for the given relation and index.
        Parameters:
        relation - Embedded resource relation
        index - Array index
        Returns:
        HAL resource
      • removeLinks

        public HalResource removeLinks()
        Removes all links.
        Returns:
        HAL resource
      • removeEmbedded

        public HalResource removeEmbedded()
        Removes all embedded resources.
        Returns:
        HAL resource
      • renameEmbedded

        public HalResource renameEmbedded​(String relToRename,
                                          String newRel)
        Changes the rel of embedded resources
        Parameters:
        relToRename - the rel that you want to change
        newRel - the new rel for all embedded items
        Returns:
        HAL resource
      • addState

        public HalResource addState​(com.fasterxml.jackson.databind.node.ObjectNode state)
        Adds state to the resource.
        Parameters:
        state - Resource state
        Returns:
        HAL resource
      • getStateFieldNames

        public List<String> getStateFieldNames()
        Returns:
        JSON field names for the state object
      • removeState

        public HalResource removeState()
        Removes all state attributes
        Returns:
        HAL resource