Class HTTPUtility
java.lang.Object
org.gcube.informationsystem.resourceregistry.api.rest.httputils.HTTPUtility
Client-side helper shared by the Resource Registry client libraries to translate a
raw
HttpURLConnection response into either a deserialized model element or
the appropriate ResourceRegistryException subclass.- Author:
- Luca Frosini (ISTI - CNR)
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected static <C> AvailableInAnotherContextExceptionBuilds the most specificAvailableInAnotherContextExceptionsubclass for the given model class, used when the server returned403 Forbiddenfor a request whose response body could not be otherwise mapped (e.g. on aHEADcall).protected static <C> NotFoundExceptiongetElementNotFoundException(Class<C> clz) Builds the most specificNotFoundExceptionsubclass for the given model class, used when the server returned404 Not Foundfor a request whose response body could not be otherwise mapped (e.g. on aHEADcall).static <C> CgetResponse(Class<C> clz, HttpURLConnection connection) Reads and interprets the response of an already-executedHttpURLConnectionrequest, deserializing it into the expected type or raising the mappedResourceRegistryExceptionon error.static <C> ResourceRegistryResponse<C>getResponseWithMetadata(Class<C> clz, HttpURLConnection connection) Reads a successful response together with immutable HTTP metadata, or attaches the same metadata to a mappedResourceRegistryExceptionfor an unsuccessful response.protected static StringBuildergetStringBuilder(InputStream inputStream) Reads an input stream fully into aStringBuilder, line by line.
-
Constructor Details
-
HTTPUtility
public HTTPUtility()
-
-
Method Details
-
getElementNotFoundException
protected static <C> NotFoundException getElementNotFoundException(Class<C> clz) throws NotFoundException, ResourceRegistryException Builds the most specificNotFoundExceptionsubclass for the given model class, used when the server returned404 Not Foundfor a request whose response body could not be otherwise mapped (e.g. on aHEADcall).- Type Parameters:
C- the expected model class of the missing element- Parameters:
clz-Resource,Facet,Relation, or another element class- Returns:
ResourceNotFoundException,FacetNotFoundException, orRelationNotFoundExceptionfor a matchingclz, otherwise a genericNotFoundException- Throws:
NotFoundException- never thrown directly; declared for symmetry with the exception being builtResourceRegistryException- never thrown directly; declared for symmetry with the exception being built
-
getElementAvailableInAnotherContextException
protected static <C> AvailableInAnotherContextException getElementAvailableInAnotherContextException(Class<C> clz) Builds the most specificAvailableInAnotherContextExceptionsubclass for the given model class, used when the server returned403 Forbiddenfor a request whose response body could not be otherwise mapped (e.g. on aHEADcall).- Type Parameters:
C- the expected model class of the out-of-scope element- Parameters:
clz-Resource,Facet,Relation, or another element class- Returns:
ResourceAvailableInAnotherContextException,FacetAvailableInAnotherContextException, orRelationAvailableInAnotherContextExceptionfor a matchingclz, otherwise a genericAvailableInAnotherContextException
-
getStringBuilder
Reads an input stream fully into aStringBuilder, line by line.- Parameters:
inputStream- the stream to read (e.g. an HTTP connection's body or error stream)- Returns:
- the full content of
inputStream, decoded as text - Throws:
IOException- if reading frominputStreamfails
-
getResponse
Reads and interprets the response of an already-executedHttpURLConnectionrequest, deserializing it into the expected type or raising the mappedResourceRegistryExceptionon error. OnHEADrequests, a204 No Contentyieldsnull, and404/403are translated viagetElementNotFoundException(Class)/getElementAvailableInAnotherContextException(Class)since aHEADresponse carries no error body to unmarshal. The connection is always closed before returning.- Type Parameters:
C- the expected response type:Boolean, anElementsubtype, orStringfor the raw body- Parameters:
clz- the expected response classconnection- the connection whose request has already been sent- Returns:
- the response body deserialized as a
C, ornullfor a204 No ContentHEADresponse - Throws:
Exception- the mappedResourceRegistryExceptionon an HTTP error status, or any lower-level I/O/deserialization failure
-
getResponseWithMetadata
public static <C> ResourceRegistryResponse<C> getResponseWithMetadata(Class<C> clz, HttpURLConnection connection) throws Exception Reads a successful response together with immutable HTTP metadata, or attaches the same metadata to a mappedResourceRegistryExceptionfor an unsuccessful response. The connection is always closed after its status, headers, URI, and body have been captured.- Type Parameters:
C- expected response body type- Parameters:
clz- expected response classconnection- executed connection- Returns:
- immutable response containing the deserialized body and HTTP metadata
- Throws:
Exception- if the response is unsuccessful or cannot be read
-