Class ResponseCache

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

public class ResponseCache extends Object
Quickly builds responses using a resource cache.
Author:
Dr Michael Gardiner
  • Constructor Details

    • ResponseCache

      public ResponseCache(ResponseCache.Source... sources)
      Constructs a ResponseCache with the given resource cache sources.
      Parameters:
      sources - The ResourceCaches to access for the sources of the response.
  • Method Details

    • getStaticResponse

      public HttpResponse getStaticResponse(HttpRequest httpReq, String path) throws IOException
      Interrogates the generalized application ResourceCache for a specific network path.
      Parameters:
      httpReq - the active network request
      path - the designated internal resource path
      Returns:
      an HTTP response encoding the discovered resource
      Throws:
      IOException - on an underlying cache access block
    • getStaticResponse

      public HttpResponse getStaticResponse(HttpRequest httpReq, String path, String contentType) throws HttpErrorResponseException, IOException
      Interrogates the generalized application ResourceCache for a specific network path.
      Parameters:
      httpReq - the active network request
      path - the designated internal resource path
      contentType - the forced static content MIME mapping
      Returns:
      an HTTP response encoding the discovered resource
      Throws:
      HttpErrorResponseException - on a general error accessing the static response
      IOException - on a more serious I/O error
    • 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
    • invalidate

      public void invalidate()
      Invalidate the cache. All items in the cache will be removed.
    • getType

      public static String getType(String path)
      Guesses the MIME type from a given request path extension.
      Parameters:
      path - the resource's network path
      Returns:
      the matching predefined string describing its content type