Package io.wcm.caravan.hal.resource
Class HalResource
- java.lang.Object
-
- io.wcm.caravan.hal.resource.HalResource
-
-
Field Summary
Fields Modifier and Type Field Description static String
CONTENT_TYPE
The mime content type
-
Constructor Summary
Constructors Constructor Description HalResource()
Create an empty HAL resource, with no object state or linksHalResource(com.fasterxml.jackson.databind.JsonNode model)
Create a new HalResource with the state from the given JSON objectHalResource(com.fasterxml.jackson.databind.JsonNode model, String uri)
Create a new HalResource with the state from the given JSON objectHalResource(com.fasterxml.jackson.databind.node.ObjectNode model)
Create a new HalResource with the state from the given JSON objectHalResource(Object pojo)
Create a new HalResource with the state from the given POJOHalResource(Object pojo, String uri)
Create a new HalResource with the state from the given POJOHalResource(String uri)
Create a HAL resource with empty state that only contains a self link with the given URI
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <T> T
adaptTo(Class<T> type)
HalResource
addEmbedded(String relation, HalResource... resources)
Embed resources for the given relationHalResource
addEmbedded(String relation, Iterable<HalResource> resources)
Embed resources for the given relationHalResource
addLinks(String relation, Link... links)
Adds links for the given relationHalResource
addLinks(String relation, Iterable<Link> links)
Adds links for the given relationHalResource
addState(com.fasterxml.jackson.databind.node.ObjectNode state)
Adds state to the resource.List<HalResource>
collectEmbedded(String rel)
recursively collects embedded resources of a specific relList<Link>
collectLinks(String rel)
recursively collects links within this resource and all embedded resourcescom.google.common.collect.ListMultimap<String,HalResource>
getEmbedded()
List<HalResource>
getEmbedded(String relation)
HalResource
getEmbeddedResource(String relation)
Link
getLink()
Link
getLink(String relation)
com.google.common.collect.ListMultimap<String,Link>
getLinks()
List<Link>
getLinks(String relation)
com.fasterxml.jackson.databind.node.ObjectNode
getModel()
Returns the JSON model.List<String>
getStateFieldNames()
boolean
hasEmbedded(String relation)
boolean
hasLink(String relation)
HalResource
removeEmbedded()
Removes all embedded resources.HalResource
removeEmbedded(String relation)
Removes all embedded resources for the given relation.HalResource
removeEmbedded(String relation, int index)
Removes one embedded resource for the given relation and index.HalResource
removeLink(String relation, int index)
Removes one link for the given relation and index.HalResource
removeLinks()
Removes all links.HalResource
removeLinks(String relation)
Removes all links for the given relation.HalResource
removeLinkWithHref(String relation, String href)
Remove the link with the given relation and hrefHalResource
removeState()
Removes all state attributesHalResource
renameEmbedded(String relToRename, String newRel)
Changes the rel of embedded resourcesHalResource
setEmbedded(String relation, HalResource resource)
Embed resource for the given relation.HalResource
setLink(Link link)
Sets link for theself
relation.HalResource
setLink(String relation, Link link)
Sets link for the given relation.
-
-
-
Field Detail
-
CONTENT_TYPE
public static final String CONTENT_TYPE
The mime content type- See Also:
- Constant Field Values
-
-
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 jacksonObjectMapper
- 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 jacksonObjectMapper
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.
-
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
-
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 theself
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. Iflink
isnull
it gets ignored.- Parameters:
relation
- Link relationlink
- Link to add- Returns:
- HAL resource
-
addLinks
public HalResource addLinks(String relation, Link... links)
Adds links for the given relation- Parameters:
relation
- Link relationlinks
- Links to add- Returns:
- HAL resource
-
addLinks
public HalResource addLinks(String relation, Iterable<Link> links)
Adds links for the given relation- Parameters:
relation
- Link relationlinks
- 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 relationresource
- Resource to embed- Returns:
- HAL resource
-
addEmbedded
public HalResource addEmbedded(String relation, HalResource... resources)
Embed resources for the given relation- Parameters:
relation
- Embedded resource relationresources
- 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 relationresources
- 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 relationindex
- 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 relationhref
- 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 relationindex
- 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 changenewRel
- 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
-
-