Class OkHttpHelper

java.lang.Object
com.janeirodigital.shapetrees.client.okhttp.OkHttpHelper

public class OkHttpHelper extends Object
  • Constructor Details

    • OkHttpHelper

      public OkHttpHelper()
  • Method Details

    • getHttpResource

      public static okhttp3.Response getHttpResource(okhttp3.OkHttpClient okHttpClient, URL url, String credentials, okhttp3.Headers headers) throws ShapeTreeException
      Perform an HTTP GET on the resource at url. 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:
      okHttpClient - OkHttpClient to perform the GET with
      url - URL of the resource to GET
      credentials - Optional credential string to include in Authorization Http Header
      headers - Optional OkHttp Headers to include
      Returns:
      OkHttp Response
      Throws:
      ShapeTreeException
      See Also:
    • getHttpResource

      public static okhttp3.Response getHttpResource(okhttp3.OkHttpClient okHttpClient, URL url, String credentials) throws ShapeTreeException
      Calls getHttpResource(OkHttpClient, URL, String, Headers) without any additional headers supplied.
      Parameters:
      okHttpClient - OkHttpClient to perform the GET with
      url - URL of the resource to GET
      credentials - Optional credential string to include in Authorization Http Header
      Returns:
      OkHttp Response
      Throws:
      ShapeTreeException
    • postHttpResource

      public static okhttp3.Response postHttpResource(okhttp3.OkHttpClient okHttpClient, URL url, String credentials, okhttp3.Headers headers, String body, ContentType contentType) throws ShapeTreeException
      Perform an HTTP POST on the resource at url. ResponseBody is closed automatically.
      Parameters:
      okHttpClient - OkHttpClient to perform the POST with
      url - URL of the resource to POST into
      credentials - Optional credential string to include in Authorization Http Header
      headers - Optional OkHttp Headers to include
      body - Body of the POST request
      contentType - ContentType of the POST request
      Returns:
      OkHttp Response
      Throws:
      ShapeTreeException
    • postHttpResource

      public static okhttp3.Response postHttpResource(okhttp3.OkHttpClient okHttpClient, URL url, String credentials, String body, ContentType contentType) throws ShapeTreeException
      Parameters:
      okHttpClient - OkHttpClient to perform the POST with
      url - URL of the resource to POST into
      credentials - Optional credential string to include in Authorization Http Header
      body - Body of the POST request
      contentType - ContentType of the POST request
      Returns:
      OkHttp Response * @return
      Throws:
      ShapeTreeException
    • putHttpResource

      public static okhttp3.Response putHttpResource(okhttp3.OkHttpClient okHttpClient, URL url, String credentials, okhttp3.Headers headers, String body, ContentType contentType) throws ShapeTreeException
      Perform an HTTP PUT on the resource at url. ResponseBody is closed automatically.
      Parameters:
      okHttpClient - OkHttpClient to perform the PUT with
      url - URL of the resource to PUT
      credentials - Optional credential string to include in Authorization Http Header
      headers - Optional OkHttp Headers to include
      body - Body of the PUT request
      contentType - ContentType of the PUT request
      Returns:
      OkHttp Response
      Throws:
      ShapeTreeException
    • putHttpResource

      public static okhttp3.Response putHttpResource(okhttp3.OkHttpClient okHttpClient, URL url, String credentials, String body, ContentType contentType) throws ShapeTreeException
      Parameters:
      okHttpClient - OkHttpClient to perform the PUT with
      url - URL of the resource to PUT
      credentials - Optional credential string to include in Authorization Http Header
      body - Body of the PUT request
      contentType - ContentType of the PUT request
      Returns:
      OkHttp Response
      Throws:
      ShapeTreeException
    • patchHttpResource

      public static okhttp3.Response patchHttpResource(okhttp3.OkHttpClient okHttpClient, URL url, String credentials, okhttp3.Headers headers, String body, ContentType contentType) throws ShapeTreeException
      Perform an HTTP PATCH on the resource at url. ResponseBody is closed automatically.
      Parameters:
      okHttpClient - OkHttpClient to perform the PATCH with
      url - URL of the resource to PATCH
      credentials - Optional credential string to include in Authorization Http Header
      headers - Optional OkHttp Headers to include
      body - Body of the PATCH request
      contentType - ContentType of the PATCH request
      Returns:
      OkHttp Response
      Throws:
      ShapeTreeException
    • patchHttpResource

      public static okhttp3.Response patchHttpResource(okhttp3.OkHttpClient okHttpClient, URL url, String credentials, String body, ContentType contentType) throws ShapeTreeException
      Parameters:
      okHttpClient - OkHttpClient to perform the PATCH with
      url - URL of the resource to PATCH
      credentials - Optional credential string to include in Authorization Http Header
      body - Body of the PATCH request
      contentType - ContentType of the PATCH request
      Returns:
      OkHttp Response
      Throws:
      ShapeTreeException
    • deleteHttpResource

      public static okhttp3.Response deleteHttpResource(okhttp3.OkHttpClient okHttpClient, URL url, String credentials, okhttp3.Headers headers) throws ShapeTreeException
      Perform an HTTP DELETE on the resource at url. ResponseBody is closed automatically.
      Parameters:
      okHttpClient - OkHttpClient to perform the DELETE with
      url - URL of the resource to DELETE
      credentials - Optional credential string to include in Authorization Http Header
      headers - Optional OkHttp Headers to include
      Returns:
      OkHttp Response
      Throws:
      ShapeTreeException
    • deleteHttpResource

      public static okhttp3.Response deleteHttpResource(okhttp3.OkHttpClient okHttpClient, URL url, String credentials) throws ShapeTreeException
      Call deleteHttpResource(OkHttpClient, URL, String, Headers) without any additional headers supplied
      Parameters:
      okHttpClient - OkHttpClient to perform the DELETE with
      url - URL of the resource to DELETE
      credentials - Optional credential string to include in Authorization Http Header
      Returns:
      OkHttp Response
      Throws:
      ShapeTreeException
    • checkResponse

      public static okhttp3.Response checkResponse(okhttp3.Response response) throws ShapeTreeException
      Check the provided response for viability
      Parameters:
      response - Response to check
      Returns:
      Checked response
      Throws:
      ShapeTreeException
    • 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
    • attributesToHeaders

      public static okhttp3.Headers attributesToHeaders(ResourceAttributes attributes)
      Converts "multi map" representation of attributes to the OkHttp Headers class public for OkHttpValidatingShapeTreeInterceptor.createResponse
      Parameters:
      attributes - Multi-map representation of attributes
      Returns:
      OkHttp Headers object
    • 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
    • createInvalidResponse

      public static okhttp3.Response createInvalidResponse(okhttp3.Request nativeRequest, ValidationResult result) throws ShapeTreeException
      Throws:
      ShapeTreeException
    • createInvalidResponse

      public static okhttp3.Response createInvalidResponse(okhttp3.Request nativeRequest, ContainingValidationResult containingResult) throws ShapeTreeException
      Throws:
      ShapeTreeException
    • createErrorResponse

      public static okhttp3.Response createErrorResponse(okhttp3.Request nativeRequest, ShapeTreeException exception)
    • createResponse

      public static okhttp3.Response createResponse(okhttp3.Request nativeRequest, int code)
    • check

      public static okhttp3.Response check(okhttp3.Response response) throws IOException
      Throws:
      IOException