Class ResponseCache

java.lang.Object
net.targetr.wtm3.cache.ResponseCache

public class ResponseCache extends Object
Builds static HTTP responses from resource loaders backed by a shared ResourceCache.
Author:
Dr Michael Gardiner
  • Constructor Details

    • ResponseCache

      public ResponseCache(ResourceCache cache, ResponseCache.Source... sources)
      Constructs a ResponseCache with a shared cache and ordered resource sources.
      Parameters:
      cache - the cache used to store and retrieve data from the resource loaders
      sources - the resource loaders and response headers, in search order
  • Method Details

    • getStaticResponse

      public HttpResponse getStaticResponse(HttpRequest httpReq, String path) throws IOException
      Searches sources in order and builds a static GET or HEAD response.
      Parameters:
      httpReq - the active network request
      path - the designated internal resource path
      Returns:
      an HTTP response encoding the discovered resource
      Throws:
      HttpErrorResponseException - for an unsupported method, rejected path or missing resource
      IOException - if reading a resource fails
    • getStaticResponse

      public HttpResponse getStaticResponse(HttpRequest httpReq, String path, String contentType) throws HttpErrorResponseException, IOException
      Searches sources in order and builds a static response with source-specific headers. Empty paths and paths ending in a slash resolve to index.html.
      Parameters:
      httpReq - the active network request
      path - the designated internal resource path
      contentType - the MIME type override, or null to infer it from the path
      Returns:
      an HTTP response encoding the discovered resource
      Throws:
      HttpErrorResponseException - for an unsupported method, rejected path or missing resource
      IOException - if reading a resource fails
    • getBytes

      public byte[] getBytes(String path) throws IOException
      Gets the byte array content of a resource from the cache sources.
      Parameters:
      path - the path to the resource
      Returns:
      the byte array content
      Throws:
      IOException - if the resource was not found or an I/O error occurred
    • available

      public boolean available(String path)
      Check if a resource is available
      Parameters:
      path - the path to the resource
      Returns:
      true if the resource is accessible via resource cache sources