Class HTTPUtility
- java.lang.Object
-
- org.gcube.informationsystem.resourceregistry.api.rest.httputils.HTTPUtility
-
public class HTTPUtility extends Object
Client-side helper shared by the Resource Registry client libraries to translate aGXResponseinto either a deserialized model element or the appropriateResourceRegistryExceptionsubclass.- Author:
- Luca Frosini (ISTI - CNR)
-
-
Constructor Summary
Constructors Constructor Description HTTPUtility()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description protected static <C> AvailableInAnotherContextExceptiongetElementAvailableInAnotherContextException(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.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.static <C> CgetHeadResponse(Class<C> clz, org.gcube.common.gxhttp.request.GXResponse response)Reads and interprets the response to aHEADrequest, which carries no body: a204 No Contentyieldsnull, and404/403are translated viagetElementNotFoundException(Class)/getElementAvailableInAnotherContextException(Class).static <C> CgetResponse(Class<C> clz, org.gcube.common.gxhttp.request.GXResponse response)Reads and interprets the response to a request sent with any method butHEAD, deserializing it into the expected type or raising the mappedResourceRegistryExceptionon error.static <C> ResourceRegistryResponse<C>getResponseWithMetadata(Class<C> clz, org.gcube.common.gxhttp.request.GXResponse response)Reads a successful response to a request sent with any method butHEAD, 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.
-
-
-
Method Detail
-
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
protected static StringBuilder getStringBuilder(InputStream inputStream) throws IOException
Reads an input stream fully into aStringBuilder, line by line.- Parameters:
inputStream- the stream to read (e.g. the body of an HTTP response)- Returns:
- the full content of
inputStream, decoded as text - Throws:
IOException- if reading frominputStreamfails
-
getResponse
public static <C> C getResponse(Class<C> clz, org.gcube.common.gxhttp.request.GXResponse response) throws Exception
Reads and interprets the response to a request sent with any method butHEAD, deserializing it into the expected type or raising the mappedResourceRegistryExceptionon error. The response is always closed before returning.- Type Parameters:
C- the expected response type:Boolean, anElementsubtype, orStringfor the raw body- Parameters:
clz- the expected response classresponse- the response received from the server- Returns:
- the response body deserialized as a
C - Throws:
Exception- the mappedResourceRegistryExceptionon an HTTP error status, or any lower-level I/O/deserialization failure
-
getHeadResponse
public static <C> C getHeadResponse(Class<C> clz, org.gcube.common.gxhttp.request.GXResponse response) throws Exception
Reads and interprets the response to aHEADrequest, which carries no body: a204 No Contentyieldsnull, and404/403are translated viagetElementNotFoundException(Class)/getElementAvailableInAnotherContextException(Class). Any other status is interpreted asgetResponse(Class, GXResponse)does. The response is always closed before returning.- Type Parameters:
C- the expected response type:Boolean, anElementsubtype, orStringfor the raw body- Parameters:
clz- the expected response classresponse- the response received from the server- Returns:
nullfor a204 No Contentresponse, otherwise the response body deserialized as aC- 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, org.gcube.common.gxhttp.request.GXResponse response) throws Exception
Reads a successful response to a request sent with any method butHEAD, together with immutable HTTP metadata, or attaches the same metadata to a mappedResourceRegistryExceptionfor an unsuccessful response. The response is always closed after its status, headers, URI, and body have been captured.- Type Parameters:
C- expected response body type- Parameters:
clz- expected response classresponse- the response received from the server- Returns:
- immutable response containing the deserialized body and HTTP metadata
- Throws:
Exception- if the response is unsuccessful or cannot be read
-
-