TypedRest for Java
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
net.typedrest.AbstractEndpoint Class Referenceabstract

Base class for building REST endpoints, i.e. More...

Inheritance diagram for net.typedrest.AbstractEndpoint:
net.typedrest.Endpoint net.typedrest.AbstractETagEndpoint net.typedrest.AbstractTriggerEndpoint net.typedrest.BlobEndpointImpl net.typedrest.EntryEndpoint net.typedrest.AbstractCollectionEndpoint< TEntity, TElementEndpoint extends Endpoint > net.typedrest.ElementEndpointImpl< TEntity > net.typedrest.ActionEndpointImpl net.typedrest.ConsumerEndpointImpl< TEntity > net.typedrest.FunctionEndpointImpl< TEntity, TResult > net.typedrest.SupplierEndpointImpl< TResult > net.typedrest.PollingEndpointImpl< TEntity >

Public Member Functions

final void setDefaultLink (String rel, String... hrefs)
 Registers one or more default links for a specific relation type. More...
 
final void setDefaultLinkTemplate (String rel, String href)
 Registers a default link template for a specific relation type. More...
 
Set< URI > getLinks (String rel)
 Retrieves all links with a specific relation type cached from the last request. More...
 
Map< URI, String > getLinksWithTitles (String rel)
 Retrieves all links (with titles) with a specific relation type cached from the last request. More...
 
URI link (String rel)
 Retrieves a single link with a specific relation type. More...
 
UriTemplate linkTemplate (String rel)
 Retrieves a link template with a specific relation type. More...
 
String toString ()
 
- Public Member Functions inherited from net.typedrest.Endpoint
URI getUri ()
 The HTTP URI of the remote resource. More...
 
Executor getExecutor ()
 The REST executor used to communicate with the remote resource. More...
 
ObjectMapper getSerializer ()
 Controls the serialization of entities sent to and received from the server. More...
 
default URI linkTemplate (String rel, String variableName, Object value)
 Retrieves a link template with a specific relation type and resolves it. More...
 

Protected Member Functions

 AbstractEndpoint (URI uri, Executor executor, ObjectMapper serializer)
 Creates a new REST endpoint with an absolute URI. More...
 
 AbstractEndpoint (Endpoint referrer, URI relativeUri)
 Creates a new REST endpoint with a relative URI. More...
 
 AbstractEndpoint (Endpoint referrer, String relativeUri)
 Creates a new REST endpoint with a relative URI. More...
 
HttpResponse executeAndHandle (Request request) throws IOException, IllegalArgumentException, IllegalAccessException, FileNotFoundException, IllegalStateException
 Executes a REST request and wraps HTTP status codes in appropriate Exception types. More...
 
HttpResponse execute (Request request) throws IOException
 Executes a REST request adding any configured defaultHeaders. More...
 
void handleResponse (HttpResponse response, Request request) throws IOException, IllegalArgumentException, IllegalAccessException, FileNotFoundException, IllegalStateException
 Handles the response of a REST request and wraps HTTP status codes in appropriate Exception types. More...
 
void handleErrors (HttpResponse response, Request request) throws IOException, IllegalArgumentException, IllegalAccessException, FileNotFoundException, IllegalStateException
 Wraps HTTP status codes in appropriate Exception types. More...
 
void handleHeaderLinks (HttpResponse response, Map< String, Map< URI, String >> links, Map< String, String > linkTemplates)
 Handles links embedded in HTTP response headers. More...
 
void handleBodyLinks (JsonNode jsonBody, Map< String, Map< URI, String >> links, Map< String, String > linkTemplates)
 Handles links embedded in JSON response bodies. More...
 
void handleCapabilities (HttpResponse response)
 Handles allowed HTTP methods and other capabilities reported by the server. More...
 
Optional< Boolean > isMethodAllowed (String method)
 Shows whether the server has indicated that a specific HTTP method is currently allowed. More...
 

Protected Attributes

final URI uri
 
final Executor executor
 
final ObjectMapper serializer
 
final Collection< Header > defaultHeaders = new LinkedList<>()
 A set of default HTTP headers to be added to each request.
 

Detailed Description

Base class for building REST endpoints, i.e.

remote HTTP resources.

Constructor & Destructor Documentation

◆ AbstractEndpoint() [1/3]

net.typedrest.AbstractEndpoint.AbstractEndpoint ( URI  uri,
Executor  executor,
ObjectMapper  serializer 
)
protected

Creates a new REST endpoint with an absolute URI.

Parameters
uriThe HTTP URI of the remote element.
executorThe REST executor used to communicate with the remote element.
serializerControls the serialization of entities sent to and received from the server.

◆ AbstractEndpoint() [2/3]

net.typedrest.AbstractEndpoint.AbstractEndpoint ( Endpoint  referrer,
URI  relativeUri 
)
protected

Creates a new REST endpoint with a relative URI.

Parameters
referrerThe parent endpoint containing this one.
relativeUriThe URI of this endpoint relative to the referrer's.

◆ AbstractEndpoint() [3/3]

net.typedrest.AbstractEndpoint.AbstractEndpoint ( Endpoint  referrer,
String  relativeUri 
)
protected

Creates a new REST endpoint with a relative URI.

Parameters
referrerThe parent endpoint containing this one.
relativeUriThe URI of this endpoint relative to the referrer's. Prefix ./ to append a trailing slash to the referrer URI if missing. Prefix ./ to append a trailing slash to the referrer URI if missing.

Member Function Documentation

◆ execute()

HttpResponse net.typedrest.AbstractEndpoint.execute ( Request  request) throws IOException
protected

Executes a REST request adding any configured defaultHeaders.

Parameters
requestThe request to execute.
Returns
The HTTP response to the request.
Exceptions
IOExceptionNetwork communication failed.
RuntimeExceptionOther non-success status code.

◆ executeAndHandle()

HttpResponse net.typedrest.AbstractEndpoint.executeAndHandle ( Request  request) throws IOException, IllegalArgumentException, IllegalAccessException, FileNotFoundException, IllegalStateException
protected

Executes a REST request and wraps HTTP status codes in appropriate Exception types.

Parameters
requestThe request to execute.
Returns
The HTTP response to the request.
Exceptions
IOExceptionNetwork communication failed.
IllegalArgumentExceptionHttpStatus#SC_BAD_REQUEST
IllegalAccessExceptionHttpStatus#SC_UNAUTHORIZED or HttpStatus#SC_FORBIDDEN
FileNotFoundExceptionHttpStatus#SC_NOT_FOUND or HttpStatus#SC_GONE
IllegalStateExceptionHttpStatus#SC_CONFLICT, HttpStatus#SC_PRECONDITION_FAILED or HttpStatus#SC_REQUESTED_RANGE_NOT_SATISFIABLE
RuntimeExceptionOther non-success status code.

◆ getLinks()

Set<URI> net.typedrest.AbstractEndpoint.getLinks ( String  rel)

Retrieves all links with a specific relation type cached from the last request.

Parameters
relThe relation type of the links to look for.
Returns
The hrefs of the links resolved relative to this endpoint's URI.

Implements net.typedrest.Endpoint.

◆ getLinksWithTitles()

Map<URI, String> net.typedrest.AbstractEndpoint.getLinksWithTitles ( String  rel)

Retrieves all links (with titles) with a specific relation type cached from the last request.

Parameters
relThe relation type of the links to look for.
Returns
A map of hrefs (resolved relative to this endpoint's URI) to titles (may be null).

Implements net.typedrest.Endpoint.

◆ handleBodyLinks()

void net.typedrest.AbstractEndpoint.handleBodyLinks ( JsonNode  jsonBody,
Map< String, Map< URI, String >>  links,
Map< String, String >  linkTemplates 
)
protected

Handles links embedded in JSON response bodies.

Parameters
jsonBodyThe body to check for links.
linksA dictionary to add found links to.
linkTemplatesA dictionary to add found link templates to.

◆ handleCapabilities()

void net.typedrest.AbstractEndpoint.handleCapabilities ( HttpResponse  response)
protected

Handles allowed HTTP methods and other capabilities reported by the server.

Parameters
responseThe response to check for the "Allow" header.

◆ handleErrors()

void net.typedrest.AbstractEndpoint.handleErrors ( HttpResponse  response,
Request  request 
) throws IOException, IllegalArgumentException, IllegalAccessException, FileNotFoundException, IllegalStateException
protected

Wraps HTTP status codes in appropriate Exception types.

Parameters
responseThe response to check for errors.
requestThe original request the reponse is for.
Exceptions
IOExceptionNetwork communication failed.
IllegalArgumentExceptionHttpStatus#SC_BAD_REQUEST
IllegalAccessExceptionHttpStatus#SC_UNAUTHORIZED or HttpStatus#SC_FORBIDDEN
FileNotFoundExceptionHttpStatus#SC_NOT_FOUND or HttpStatus#SC_GONE
IllegalStateExceptionHttpStatus#SC_CONFLICT, HttpStatus#SC_PRECONDITION_FAILED or HttpStatus#SC_REQUESTED_RANGE_NOT_SATISFIABLE
RuntimeExceptionOther non-success status code.

◆ handleHeaderLinks()

void net.typedrest.AbstractEndpoint.handleHeaderLinks ( HttpResponse  response,
Map< String, Map< URI, String >>  links,
Map< String, String >  linkTemplates 
)
protected

Handles links embedded in HTTP response headers.

Parameters
responseThe response to check for links.
linksA dictionary to add found links to.
linkTemplatesA dictionary to add found link templates to.

◆ handleResponse()

void net.typedrest.AbstractEndpoint.handleResponse ( HttpResponse  response,
Request  request 
) throws IOException, IllegalArgumentException, IllegalAccessException, FileNotFoundException, IllegalStateException
protected

Handles the response of a REST request and wraps HTTP status codes in appropriate Exception types.

Parameters
responseThe response to handle.
requestThe original request the reponse is for.
Exceptions
IOExceptionNetwork communication failed.
IllegalArgumentExceptionHttpStatus#SC_BAD_REQUEST
IllegalAccessExceptionHttpStatus#SC_UNAUTHORIZED or HttpStatus#SC_FORBIDDEN
FileNotFoundExceptionHttpStatus#SC_NOT_FOUND or HttpStatus#SC_GONE
IllegalStateExceptionHttpStatus#SC_CONFLICT, HttpStatus#SC_PRECONDITION_FAILED or HttpStatus#SC_REQUESTED_RANGE_NOT_SATISFIABLE
RuntimeExceptionOther non-success status code.

◆ isMethodAllowed()

Optional<Boolean> net.typedrest.AbstractEndpoint.isMethodAllowed ( String  method)
protected

Shows whether the server has indicated that a specific HTTP method is currently allowed.

Uses cached data from last response.

Parameters
methodThe HTTP method (e.g. GET, POST, ...) to check.
Returns
An indicator whether the method is allowed. If no request has been sent yet or the server did not specify allowed methods Optional#empty() is returned.

◆ link()

URI net.typedrest.AbstractEndpoint.link ( String  rel)

Retrieves a single link with a specific relation type.

Uses cached data from last response if possible. Tries lazy lookup with HTTP HEAD on cache miss.

Parameters
relThe relation type of the link to look for.
Returns
The href of the link resolved relative to this endpoint's URI.
Exceptions
RuntimeExceptionNo link with the specified relation type could be found.

Implements net.typedrest.Endpoint.

◆ linkTemplate()

UriTemplate net.typedrest.AbstractEndpoint.linkTemplate ( String  rel)

Retrieves a link template with a specific relation type.

Uses cached data from last response if possible. Tries lazy lookup with HTTP HEAD on cache miss.

Parameters
relThe relation type of the template to look for.
Returns
The unresolved link template.
Exceptions
RuntimeExceptionNo link template with the specified relation type could be found.

Implements net.typedrest.Endpoint.

◆ setDefaultLink()

final void net.typedrest.AbstractEndpoint.setDefaultLink ( String  rel,
String...  hrefs 
)

Registers one or more default links for a specific relation type.

These links are used when no links with this relation type are provided by the server.

This method is not thread-safe! Call this before performing any requests.

Parameters
relThe relation type of the links to add.
hrefsThe hrefs of links relative to this endpoint's URI. Use null or an empty list to remove all previous entries for the relation type.
See also
Endpoint::getLinks(java.lang.String)
Endpoint::getLinksWithTitles(java.lang.String)
Endpoint::link(java.lang.String)

◆ setDefaultLinkTemplate()

final void net.typedrest.AbstractEndpoint.setDefaultLinkTemplate ( String  rel,
String  href 
)

Registers a default link template for a specific relation type.

This template is used when no template with this relation type is provided by the server.

This method is not thread-safe! Call this before performing any requests.

Parameters
relThe relation type of the link template to add.
hrefThe href of the link template relative to this endpoint's URI. Use null to remove any previous entry for the relation type.
See also
Endpoint::linkTemplate(java.lang.String)

The documentation for this class was generated from the following file: