Package com.janeirodigital.sai.httputils
Class HttpUtils
java.lang.Object
com.janeirodigital.sai.httputils.HttpUtils
Assorted utility methods related to working with HTTP requests and responses
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final ContentTypestatic final Stringstatic final Stringstatic final Set<ContentType> -
Method Summary
Modifier and TypeMethodDescriptionstatic URIaddChildToUriPath(URI baseUri, String child) Adds a child to the end of the path ofbaseUrlstatic okhttp3.HeadersaddHttpHeader(HttpHeader name, String value) Add the HTTP header identified bynamewith the providedvalue.static okhttp3.HeadersaddHttpHeader(HttpHeader name, String value, okhttp3.Headers headers) Add the HTTP header identified bynamewith the providedvalue.static okhttp3.HeadersaddLinkRelationHeader(LinkRelation type, String target) Add an HTTP Link Relation header oftypewith the providedtarget.static okhttp3.HeadersaddLinkRelationHeader(LinkRelation type, String target, okhttp3.Headers headers) Add an HTTP Link Relation header oftypewith the providedtarget.protected static okhttp3.ResponsecheckRdfResponse(okhttp3.Response response) Check the providedresponsefor viable for an RDF resource.protected static okhttp3.ResponsecheckResponse(okhttp3.Response response) Check the providedresponsefor viabilitystatic okhttp3.ResponsedeleteResource(okhttp3.OkHttpClient httpClient, URI uri) CallsdeleteResource(OkHttpClient, URI, Headers)without any additional headers supplied.static okhttp3.ResponsedeleteResource(okhttp3.OkHttpClient httpClient, URI uri, okhttp3.Headers headers) Perform an HTTP DELETE on the resource aturi.protected static ContentTypegetContentType(okhttp3.Response response) Get the HTTP Content-Type of the responsestatic StringgetLinkRelationString(LinkRelation type, String target) Get a formatted HTTP Link Relation string in compliance with RFC 8288static org.apache.jena.rdf.model.ModelgetRdfModelFromResponse(okhttp3.Response response) Get a Jena RDF Model from the body of the OkHttp Response.static okhttp3.ResponsegetRdfResource(okhttp3.OkHttpClient httpClient, URI uri) Perform an HTTP GET on an RDF resource aturi.static okhttp3.ResponsegetRdfResource(okhttp3.OkHttpClient httpClient, URI uri, okhttp3.Headers headers) Perform an HTTP GET on an RDF resource aturi.static okhttp3.ResponsegetRequiredRdfResource(okhttp3.OkHttpClient httpClient, URI uri) Perform an HTTP GET on an RDF resource aturi.static okhttp3.ResponsegetRequiredResource(okhttp3.OkHttpClient httpClient, URI uri) Perform an HTTP GET on the resource aturi, and throws an exception if the resource cannot be found or the response is otherwise unsuccessful.static okhttp3.ResponsegetResource(okhttp3.OkHttpClient httpClient, URI uri) CallsgetResource(OkHttpClient, URI, Headers)without any additional headers supplied.static okhttp3.ResponsegetResource(okhttp3.OkHttpClient httpClient, URI uri, okhttp3.Headers headers) Perform an HTTP GET on the resource aturi.static StringgetResponseFailureMessage(okhttp3.Response response) Generate a failure message based on an OkHttp Responsestatic okhttp3.ResponseputRdfContainer(okhttp3.OkHttpClient httpClient, URI uri, org.apache.jena.rdf.model.Resource resource, ContentType contentType) static okhttp3.ResponseputRdfContainer(okhttp3.OkHttpClient httpClient, URI uri, org.apache.jena.rdf.model.Resource resource, ContentType contentType, String jsonLdContext) Perform an HTTP PUT on the resource aturitreated as a Basic Container, with a serialized Jena Resource as the request body.static okhttp3.ResponseputRdfResource(okhttp3.OkHttpClient httpClient, URI uri, org.apache.jena.rdf.model.Resource resource, ContentType contentType) Perform an HTTP PUT on the resource aturiusing a serialized Jena Resource as the request body.static okhttp3.ResponseputRdfResource(okhttp3.OkHttpClient httpClient, URI uri, org.apache.jena.rdf.model.Resource resource, ContentType contentType, String jsonLdContext) Perform an HTTP PUT on the resource aturiusing a serialized Jena Resource as the request body with a jsonLdContextstatic okhttp3.ResponseputRdfResource(okhttp3.OkHttpClient httpClient, URI uri, org.apache.jena.rdf.model.Resource resource, ContentType contentType, String jsonLdContext, okhttp3.Headers headers) Perform an HTTP PUT with optional headers on the resource aturiusing a serialized Jena Resource as the request body.static okhttp3.ResponseputRdfResource(okhttp3.OkHttpClient httpClient, URI uri, org.apache.jena.rdf.model.Resource resource, ContentType contentType, okhttp3.Headers headers) Perform an HTTP PUT with optional headers on the resource aturiusing a serialized Jena Resource as the request body.static okhttp3.ResponseputResource(okhttp3.OkHttpClient httpClient, URI uri, okhttp3.Headers headers, String body, ContentType contentType) Perform an HTTP PUT on the resource aturi.static URIrequestUrlToUri(okhttp3.HttpUrl url) Convenience wrapper around urlToUri which takes the OkHttp HttpUrl type as input.static okhttp3.HeaderssetHttpHeader(HttpHeader name, String value) Set the HTTP header identified bynamewith the providedvalue.static okhttp3.HeaderssetHttpHeader(HttpHeader name, String value, okhttp3.Headers headers) Set the HTTP header identified bynamewith the providedvalue.static URLstringToUrl(String urlString) Converts a string to a URLstatic URLCoverts a URI to a URLstatic URLReturns the scheme, domain name, port, and path of a URL, removing any query parameters or fragments.static URIWrap conversion from URI to URI which should never fail on a well-formed URI.
-
Field Details
-
RDF_CONTENT_TYPES
-
DEFAULT_RDF_CONTENT_TYPE
-
LDP_BASIC_CONTAINER
- See Also:
-
LDP_CONTAINER
- See Also:
-
-
Method Details
-
getResource
public static okhttp3.Response getResource(okhttp3.OkHttpClient httpClient, URI uri, okhttp3.Headers headers) throws SaiHttpException Perform an HTTP GET on the resource aturi. The response MUST be closed outside of this call. The body of the response is returned as a one-shot stream, and MUST BE CLOSED separately by the caller.- Parameters:
httpClient- OkHttpClient to perform the GET withuri- URI of the resource to GETheaders- Optional OkHttp Headers to include- Returns:
- OkHttp Response
- Throws:
SaiHttpException- See Also:
-
getResource
public static okhttp3.Response getResource(okhttp3.OkHttpClient httpClient, URI uri) throws SaiHttpException CallsgetResource(OkHttpClient, URI, Headers)without any additional headers supplied.- Parameters:
httpClient- OkHttpClient to perform the GET withuri- URI of the resource to GET- Returns:
- OkHttp Response
- Throws:
SaiHttpException
-
getRequiredResource
public static okhttp3.Response getRequiredResource(okhttp3.OkHttpClient httpClient, URI uri) throws SaiHttpException, SaiHttpNotFoundException Perform an HTTP GET on the resource aturi, and throws an exception if the resource cannot be found or the response is otherwise unsuccessful. The body of the response is returned as a one-shot stream, and MUST BE CLOSED separately by the caller.- Parameters:
httpClient- OkHttpClient to perform the GET withuri- URI of the resource to GET- Returns:
- OkHttp Response
- Throws:
SaiHttpExceptionSaiHttpNotFoundException- See Also:
-
putResource
public static okhttp3.Response putResource(okhttp3.OkHttpClient httpClient, URI uri, okhttp3.Headers headers, String body, ContentType contentType) throws SaiHttpException Perform an HTTP PUT on the resource aturi. ResponseBody is closed automatically.- Parameters:
httpClient- OkHttpClient to perform the PUT withuri- URI of the resource to PUTheaders- Optional OkHttp Headers to includebody- Body of the PUT requestcontentType-ContentTypeof the PUT request- Returns:
- OkHttp Response
- Throws:
SaiHttpException
-
deleteResource
public static okhttp3.Response deleteResource(okhttp3.OkHttpClient httpClient, URI uri, okhttp3.Headers headers) throws SaiHttpException Perform an HTTP DELETE on the resource aturi. ResponseBody is closed automatically.- Parameters:
httpClient- OkHttpClient to perform the DELETE withuri- URI of the resource to DELETEheaders- Optional OkHttp headers to include- Returns:
- OkHttp Response
- Throws:
SaiHttpException
-
deleteResource
public static okhttp3.Response deleteResource(okhttp3.OkHttpClient httpClient, URI uri) throws SaiHttpException CallsdeleteResource(OkHttpClient, URI, Headers)without any additional headers supplied.- Parameters:
httpClient- OkHttpClient to perform the DELETE withuri- URI of the resource to DELETE- Returns:
- OkHttp Response
- Throws:
SaiHttpException
-
getRdfResource
public static okhttp3.Response getRdfResource(okhttp3.OkHttpClient httpClient, URI uri) throws SaiHttpException Perform an HTTP GET on an RDF resource aturi. Checks that the response is representative of an RDF resource.- Parameters:
httpClient- OkHttpClient to perform the GET withuri- URI of the resource to GET- Returns:
- OkHttp Response
- Throws:
SaiHttpException
-
getRequiredRdfResource
public static okhttp3.Response getRequiredRdfResource(okhttp3.OkHttpClient httpClient, URI uri) throws SaiHttpException, SaiHttpNotFoundException Perform an HTTP GET on an RDF resource aturi. Checks that the response is representative of an RDF resource. Requires the resource to be found, or an exception is thrown.- Parameters:
httpClient- OkHttpClient to perform the GET withuri- URI of the resource to GET- Returns:
- OkHttp Response
- Throws:
SaiHttpExceptionSaiHttpNotFoundException- when no resource is found
-
getRdfResource
public static okhttp3.Response getRdfResource(okhttp3.OkHttpClient httpClient, URI uri, okhttp3.Headers headers) throws SaiHttpException Perform an HTTP GET on an RDF resource aturi. Checks that the response is representative of an RDF resource.- Parameters:
httpClient- OkHttpClient to perform the GET withuri- URI of the resource to GETheaders- Optional OkHttp headers to include- Returns:
- OkHttp Response
- Throws:
SaiHttpException
-
getRdfModelFromResponse
public static org.apache.jena.rdf.model.Model getRdfModelFromResponse(okhttp3.Response response) throws SaiHttpException Get a Jena RDF Model from the body of the OkHttp Response.- Parameters:
response- OkHttp Response- Returns:
- Jena Model
- Throws:
SaiHttpException
-
putRdfResource
public static okhttp3.Response putRdfResource(okhttp3.OkHttpClient httpClient, URI uri, org.apache.jena.rdf.model.Resource resource, ContentType contentType) throws SaiHttpException Perform an HTTP PUT on the resource aturiusing a serialized Jena Resource as the request body.- Parameters:
httpClient- OkHttpClient to perform the PUT withuri- URI of the resource to PUTresource- Jena Resource for the request bodycontentType- ContentType of the request- Returns:
- OkHttp Response
- Throws:
SaiHttpException
-
putRdfResource
public static okhttp3.Response putRdfResource(okhttp3.OkHttpClient httpClient, URI uri, org.apache.jena.rdf.model.Resource resource, ContentType contentType, String jsonLdContext) throws SaiHttpException Perform an HTTP PUT on the resource aturiusing a serialized Jena Resource as the request body with a jsonLdContext- Parameters:
httpClient- OkHttpClient to perform the PUT withuri- URI of the resource to PUTresource- Jena Resource for the request bodycontentType- ContentType of the requestjsonLdContext- JSON-LD context string to include- Returns:
- OkHttp Response
- Throws:
SaiHttpException
-
putRdfResource
public static okhttp3.Response putRdfResource(okhttp3.OkHttpClient httpClient, URI uri, org.apache.jena.rdf.model.Resource resource, ContentType contentType, okhttp3.Headers headers) throws SaiHttpException Perform an HTTP PUT with optional headers on the resource aturiusing a serialized Jena Resource as the request body.- Parameters:
httpClient- OkHttpClient to perform the PUT withuri- URI of the resource to PUTresource- Jena Resource for the request bodycontentType- ContentType of the requestheaders- Optional OkHttp Headers- Returns:
- OkHttp Response
- Throws:
SaiHttpException
-
putRdfResource
public static okhttp3.Response putRdfResource(okhttp3.OkHttpClient httpClient, URI uri, org.apache.jena.rdf.model.Resource resource, ContentType contentType, String jsonLdContext, okhttp3.Headers headers) throws SaiHttpException Perform an HTTP PUT with optional headers on the resource aturiusing a serialized Jena Resource as the request body.- Parameters:
httpClient- OkHttpClient to perform the PUT withuri- URI of the resource to PUTresource- Jena Resource for the request bodycontentType- ContentType of the requestjsonLdContext- Optional JSON-LD context string to includeheaders- Optional OkHttp Headers- Returns:
- OkHttp Response
- Throws:
SaiHttpException
-
putRdfContainer
public static okhttp3.Response putRdfContainer(okhttp3.OkHttpClient httpClient, URI uri, org.apache.jena.rdf.model.Resource resource, ContentType contentType, String jsonLdContext) throws SaiHttpException Perform an HTTP PUT on the resource aturitreated as a Basic Container, with a serialized Jena Resource as the request body.- Parameters:
httpClient- OkHttpClient to perform the PUT withuri- URI of the resource to PUTresource- Jena Resource for the request body- Returns:
- OkHttp Response
- Throws:
SaiHttpException
-
putRdfContainer
public static okhttp3.Response putRdfContainer(okhttp3.OkHttpClient httpClient, URI uri, org.apache.jena.rdf.model.Resource resource, ContentType contentType) throws SaiHttpException - Throws:
SaiHttpException
-
setHttpHeader
Set the HTTP header identified bynamewith the providedvalue. Ifheadersare provided, they will be included in the Headers returned. If the Header to set already exists in that set, it will be updated.- Parameters:
name-HttpHeaderto setvalue- Value to use for the headerheaders- Optional OkHttp Headers to include- Returns:
- Updated OkHttp Headers
-
setHttpHeader
Set the HTTP header identified bynamewith the providedvalue.- Parameters:
name-HttpHeaderto setvalue- Value to use for the header- Returns:
- Updated OkHttp Headers
-
addHttpHeader
Add the HTTP header identified bynamewith the providedvalue. Ifheadersare provided, they will be included in the Headers returned.- Parameters:
name-HttpHeaderto addvalue- Value to use for the added headerheaders- Optional OkHttp Headers to include- Returns:
- Populated OkHttp Headers
-
addHttpHeader
Add the HTTP header identified bynamewith the providedvalue.- Parameters:
name-HttpHeaderto addvalue- Value to use for the added header- Returns:
- Populated OkHttp Headers
-
addLinkRelationHeader
public static okhttp3.Headers addLinkRelationHeader(LinkRelation type, String target, okhttp3.Headers headers) Add an HTTP Link Relation header oftypewith the providedtarget. Ifheadersare provided, they will be included in the Headers returned.- Parameters:
type- Link relation typetarget- Link relation targetheaders- Optional OkHttp Headers to include- Returns:
- Populated OkHttp Headers
- See Also:
-
addLinkRelationHeader
Add an HTTP Link Relation header oftypewith the providedtarget.- Parameters:
type- Link relation typetarget- Link relation target- Returns:
- Populated OkHttp Headers
- See Also:
-
getLinkRelationString
Get a formatted HTTP Link Relation string in compliance with RFC 8288- Parameters:
type- Link relation typetarget- Link relation target- Returns:
- Formatted Link Relation string
-
checkResponse
protected static okhttp3.Response checkResponse(okhttp3.Response response) Check the providedresponsefor viability- Parameters:
response- Response to check- Returns:
- Checked response
-
checkRdfResponse
protected static okhttp3.Response checkRdfResponse(okhttp3.Response response) throws SaiHttpException Check the providedresponsefor viable for an RDF resource.- Parameters:
response- Response to check- Returns:
- Checked response
- Throws:
SaiHttpException
-
getContentType
Get the HTTP Content-Type of the response- Parameters:
response- Response to get Content-Type from- Returns:
ContentTypeof response- Throws:
SaiHttpException
-
urlToUri
Wrap conversion from URI to URI which should never fail on a well-formed URI.- Parameters:
url- covert this URI to a URI- Returns:
- IRI java native object for a URI (useful for Jena graph operations)
-
requestUrlToUri
Convenience wrapper around urlToUri which takes the OkHttp HttpUrl type as input.- Parameters:
url- HttpUrl from OkHttp- Returns:
- IRI java native object for a URI (useful for Jena graph operations)
-
urlToBase
Returns the scheme, domain name, port, and path of a URL, removing any query parameters or fragments.- Parameters:
url- to trim- Returns:
- Base URL
- Throws:
SaiHttpException
-
stringToUrl
Converts a string to a URL- Parameters:
urlString- String to convert to URL- Returns:
- Converted URL
- Throws:
SaiHttpException
-
uriToUrl
Coverts a URI to a URL- Parameters:
uri- URI to convert- Returns:
- Converted URL
- Throws:
SaiHttpException
-
addChildToUriPath
Adds a child to the end of the path ofbaseUrl- Parameters:
baseUri- Base URL to append tochild- Child to add to the path- Returns:
- URI with
childappended - Throws:
SaiHttpException
-
getResponseFailureMessage
Generate a failure message based on an OkHttp Response- Parameters:
response- OkHttp Response- Returns:
- Failure message string
-