Class HttpUtils

java.lang.Object
com.janeirodigital.sai.httputils.HttpUtils

public class HttpUtils extends Object
Assorted utility methods related to working with HTTP requests and responses
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final ContentType
     
    static final String
     
    static final String
     
    static final Set<ContentType>
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static URI
    addChildToUriPath(URI baseUri, String child)
    Adds a child to the end of the path of baseUrl
    static okhttp3.Headers
    Add the HTTP header identified by name with the provided value.
    static okhttp3.Headers
    addHttpHeader(HttpHeader name, String value, okhttp3.Headers headers)
    Add the HTTP header identified by name with the provided value.
    static okhttp3.Headers
    Add an HTTP Link Relation header of type with the provided target.
    static okhttp3.Headers
    addLinkRelationHeader(LinkRelation type, String target, okhttp3.Headers headers)
    Add an HTTP Link Relation header of type with the provided target.
    protected static okhttp3.Response
    checkRdfResponse(okhttp3.Response response)
    Check the provided response for viable for an RDF resource.
    protected static okhttp3.Response
    checkResponse(okhttp3.Response response)
    Check the provided response for viability
    static okhttp3.Response
    deleteResource(okhttp3.OkHttpClient httpClient, URI uri)
    Calls deleteResource(OkHttpClient, URI, Headers) without any additional headers supplied.
    static okhttp3.Response
    deleteResource(okhttp3.OkHttpClient httpClient, URI uri, okhttp3.Headers headers)
    Perform an HTTP DELETE on the resource at uri.
    protected static ContentType
    getContentType(okhttp3.Response response)
    Get the HTTP Content-Type of the response
    static String
    Get a formatted HTTP Link Relation string in compliance with RFC 8288
    static org.apache.jena.rdf.model.Model
    getRdfModelFromResponse(okhttp3.Response response)
    Get a Jena RDF Model from the body of the OkHttp Response.
    static okhttp3.Response
    getRdfResource(okhttp3.OkHttpClient httpClient, URI uri)
    Perform an HTTP GET on an RDF resource at uri.
    static okhttp3.Response
    getRdfResource(okhttp3.OkHttpClient httpClient, URI uri, okhttp3.Headers headers)
    Perform an HTTP GET on an RDF resource at uri.
    static okhttp3.Response
    getRequiredRdfResource(okhttp3.OkHttpClient httpClient, URI uri)
    Perform an HTTP GET on an RDF resource at uri.
    static okhttp3.Response
    getRequiredResource(okhttp3.OkHttpClient httpClient, URI uri)
    Perform an HTTP GET on the resource at uri, and throws an exception if the resource cannot be found or the response is otherwise unsuccessful.
    static okhttp3.Response
    getResource(okhttp3.OkHttpClient httpClient, URI uri)
    Calls getResource(OkHttpClient, URI, Headers) without any additional headers supplied.
    static okhttp3.Response
    getResource(okhttp3.OkHttpClient httpClient, URI uri, okhttp3.Headers headers)
    Perform an HTTP GET on the resource at uri.
    static String
    getResponseFailureMessage(okhttp3.Response response)
    Generate a failure message based on an OkHttp Response
    static okhttp3.Response
    putRdfContainer(okhttp3.OkHttpClient httpClient, URI uri, org.apache.jena.rdf.model.Resource resource, ContentType contentType)
     
    static okhttp3.Response
    putRdfContainer(okhttp3.OkHttpClient httpClient, URI uri, org.apache.jena.rdf.model.Resource resource, ContentType contentType, String jsonLdContext)
    Perform an HTTP PUT on the resource at uri treated as a Basic Container, with a serialized Jena Resource as the request body.
    static okhttp3.Response
    putRdfResource(okhttp3.OkHttpClient httpClient, URI uri, org.apache.jena.rdf.model.Resource resource, ContentType contentType)
    Perform an HTTP PUT on the resource at uri using a serialized Jena Resource as the request body.
    static okhttp3.Response
    putRdfResource(okhttp3.OkHttpClient httpClient, URI uri, org.apache.jena.rdf.model.Resource resource, ContentType contentType, String jsonLdContext)
    Perform an HTTP PUT on the resource at uri using a serialized Jena Resource as the request body with a jsonLdContext
    static okhttp3.Response
    putRdfResource(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 at uri using a serialized Jena Resource as the request body.
    static okhttp3.Response
    putRdfResource(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 at uri using a serialized Jena Resource as the request body.
    static okhttp3.Response
    putResource(okhttp3.OkHttpClient httpClient, URI uri, okhttp3.Headers headers, String body, ContentType contentType)
    Perform an HTTP PUT on the resource at uri.
    static URI
    requestUrlToUri(okhttp3.HttpUrl url)
    Convenience wrapper around urlToUri which takes the OkHttp HttpUrl type as input.
    static okhttp3.Headers
    Set the HTTP header identified by name with the provided value.
    static okhttp3.Headers
    setHttpHeader(HttpHeader name, String value, okhttp3.Headers headers)
    Set the HTTP header identified by name with the provided value.
    static URL
    stringToUrl(String urlString)
    Converts a string to a URL
    static URL
    Coverts a URI to a URL
    static URL
    Returns the scheme, domain name, port, and path of a URL, removing any query parameters or fragments.
    static URI
    Wrap conversion from URI to URI which should never fail on a well-formed URI.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • 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 at uri. 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 with
      uri - URI of the resource to GET
      headers - 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
      Calls getResource(OkHttpClient, URI, Headers) without any additional headers supplied.
      Parameters:
      httpClient - OkHttpClient to perform the GET with
      uri - 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 at uri, 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 with
      uri - URI of the resource to GET
      Returns:
      OkHttp Response
      Throws:
      SaiHttpException
      SaiHttpNotFoundException
      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 at uri. ResponseBody is closed automatically.
      Parameters:
      httpClient - OkHttpClient to perform the PUT with
      uri - URI of the resource to PUT
      headers - Optional OkHttp Headers to include
      body - Body of the PUT request
      contentType - ContentType of 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 at uri. ResponseBody is closed automatically.
      Parameters:
      httpClient - OkHttpClient to perform the DELETE with
      uri - URI of the resource to DELETE
      headers - Optional OkHttp headers to include
      Returns:
      OkHttp Response
      Throws:
      SaiHttpException
    • deleteResource

      public static okhttp3.Response deleteResource(okhttp3.OkHttpClient httpClient, URI uri) throws SaiHttpException
      Calls deleteResource(OkHttpClient, URI, Headers) without any additional headers supplied.
      Parameters:
      httpClient - OkHttpClient to perform the DELETE with
      uri - 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 at uri. Checks that the response is representative of an RDF resource.
      Parameters:
      httpClient - OkHttpClient to perform the GET with
      uri - 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 at uri. 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 with
      uri - URI of the resource to GET
      Returns:
      OkHttp Response
      Throws:
      SaiHttpException
      SaiHttpNotFoundException - 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 at uri. Checks that the response is representative of an RDF resource.
      Parameters:
      httpClient - OkHttpClient to perform the GET with
      uri - URI of the resource to GET
      headers - 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 at uri using a serialized Jena Resource as the request body.
      Parameters:
      httpClient - OkHttpClient to perform the PUT with
      uri - URI of the resource to PUT
      resource - Jena Resource for the request body
      contentType - 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 at uri using a serialized Jena Resource as the request body with a jsonLdContext
      Parameters:
      httpClient - OkHttpClient to perform the PUT with
      uri - URI of the resource to PUT
      resource - Jena Resource for the request body
      contentType - ContentType of the request
      jsonLdContext - 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 at uri using a serialized Jena Resource as the request body.
      Parameters:
      httpClient - OkHttpClient to perform the PUT with
      uri - URI of the resource to PUT
      resource - Jena Resource for the request body
      contentType - ContentType of the request
      headers - 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 at uri using a serialized Jena Resource as the request body.
      Parameters:
      httpClient - OkHttpClient to perform the PUT with
      uri - URI of the resource to PUT
      resource - Jena Resource for the request body
      contentType - ContentType of the request
      jsonLdContext - Optional JSON-LD context string to include
      headers - 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 at uri treated as a Basic Container, with a serialized Jena Resource as the request body.
      Parameters:
      httpClient - OkHttpClient to perform the PUT with
      uri - URI of the resource to PUT
      resource - 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

      public static okhttp3.Headers setHttpHeader(HttpHeader name, String value, okhttp3.Headers headers)
      Set the HTTP header identified by name with the provided value. If headers are 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 - HttpHeader to set
      value - Value to use for the header
      headers - Optional OkHttp Headers to include
      Returns:
      Updated OkHttp Headers
    • setHttpHeader

      public static okhttp3.Headers setHttpHeader(HttpHeader name, String value)
      Set the HTTP header identified by name with the provided value.
      Parameters:
      name - HttpHeader to set
      value - Value to use for the header
      Returns:
      Updated OkHttp Headers
    • addHttpHeader

      public static okhttp3.Headers addHttpHeader(HttpHeader name, String value, okhttp3.Headers headers)
      Add the HTTP header identified by name with the provided value. If headers are provided, they will be included in the Headers returned.
      Parameters:
      name - HttpHeader to add
      value - Value to use for the added header
      headers - Optional OkHttp Headers to include
      Returns:
      Populated OkHttp Headers
    • addHttpHeader

      public static okhttp3.Headers addHttpHeader(HttpHeader name, String value)
      Add the HTTP header identified by name with the provided value.
      Parameters:
      name - HttpHeader to add
      value - 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 of type with the provided target. If headers are provided, they will be included in the Headers returned.
      Parameters:
      type - Link relation type
      target - Link relation target
      headers - Optional OkHttp Headers to include
      Returns:
      Populated OkHttp Headers
      See Also:
    • addLinkRelationHeader

      public static okhttp3.Headers addLinkRelationHeader(LinkRelation type, String target)
      Add an HTTP Link Relation header of type with the provided target.
      Parameters:
      type - Link relation type
      target - Link relation target
      Returns:
      Populated OkHttp Headers
      See Also:
    • getLinkRelationString

      public static String getLinkRelationString(LinkRelation type, String target)
      Get a formatted HTTP Link Relation string in compliance with RFC 8288
      Parameters:
      type - Link relation type
      target - Link relation target
      Returns:
      Formatted Link Relation string
    • checkResponse

      protected static okhttp3.Response checkResponse(okhttp3.Response response)
      Check the provided response for viability
      Parameters:
      response - Response to check
      Returns:
      Checked response
    • checkRdfResponse

      protected static okhttp3.Response checkRdfResponse(okhttp3.Response response) throws SaiHttpException
      Check the provided response for viable for an RDF resource.
      Parameters:
      response - Response to check
      Returns:
      Checked response
      Throws:
      SaiHttpException
    • getContentType

      protected static ContentType getContentType(okhttp3.Response response) throws SaiHttpException
      Get the HTTP Content-Type of the response
      Parameters:
      response - Response to get Content-Type from
      Returns:
      ContentType of response
      Throws:
      SaiHttpException
    • urlToUri

      public static URI urlToUri(URL url)
      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

      public static URI requestUrlToUri(okhttp3.HttpUrl url)
      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

      public static URL urlToBase(URL url) throws SaiHttpException
      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

      public static URL stringToUrl(String urlString) throws SaiHttpException
      Converts a string to a URL
      Parameters:
      urlString - String to convert to URL
      Returns:
      Converted URL
      Throws:
      SaiHttpException
    • uriToUrl

      public static URL uriToUrl(URI uri) throws SaiHttpException
      Coverts a URI to a URL
      Parameters:
      uri - URI to convert
      Returns:
      Converted URL
      Throws:
      SaiHttpException
    • addChildToUriPath

      public static URI addChildToUriPath(URI baseUri, String child) throws SaiHttpException
      Adds a child to the end of the path of baseUrl
      Parameters:
      baseUri - Base URL to append to
      child - Child to add to the path
      Returns:
      URI with child appended
      Throws:
      SaiHttpException
    • getResponseFailureMessage

      public static String getResponseFailureMessage(okhttp3.Response response)
      Generate a failure message based on an OkHttp Response
      Parameters:
      response - OkHttp Response
      Returns:
      Failure message string