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(ResourceCache cache)
      Constructs a ResponseCache with the given resource cache underneath.
      Parameters:
      cache - The ResourceCache to use for lookups.
  • Method Details

    • 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
    • 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 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:
      IOException - on an underlying cache access block
    • get

      public HttpByteArrayResponse get(String path, String contentType) throws IOException
      Get a resource quickly by using cache if possible.
      Parameters:
      path - the path to the resource in the cache.
      contentType - the type to set in the response header.
      Returns:
      response containing data from the resource.
      Throws:
      IOException - An exception occurred while reading the resource from the cache.
    • invalidate

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