Package net.targetr.wtm3.cache
Class ResponseCache
java.lang.Object
net.targetr.wtm3.cache.ResponseCache
Builds static HTTP responses from resource loaders backed by a shared
ResourceCache.- Author:
- Dr Michael Gardiner
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classRepresents a named resource loader with optional response headers. -
Constructor Summary
ConstructorsConstructorDescriptionResponseCache(ResourceCache cache, ResponseCache.Source... sources) Constructs a ResponseCache with a shared cache and ordered resource sources. -
Method Summary
Modifier and TypeMethodDescriptionbooleanCheck if a resource is availablebyte[]Gets the byte array content of a resource from the cache sources.getStaticResponse(HttpRequest httpReq, String path) Searches sources in order and builds a static GET or HEAD response.getStaticResponse(HttpRequest httpReq, String path, String contentType) Searches sources in order and builds a static response with source-specific headers.
-
Constructor Details
-
ResponseCache
Constructs a ResponseCache with a shared cache and ordered resource sources.- Parameters:
cache- the cache used to store and retrieve data from the resource loaderssources- the resource loaders and response headers, in search order
-
-
Method Details
-
getStaticResponse
Searches sources in order and builds a static GET or HEAD response.- Parameters:
httpReq- the active network requestpath- the designated internal resource path- Returns:
- an HTTP response encoding the discovered resource
- Throws:
HttpErrorResponseException- for an unsupported method, rejected path or missing resourceIOException- 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 toindex.html.- Parameters:
httpReq- the active network requestpath- the designated internal resource pathcontentType- 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 resourceIOException- if reading a resource fails
-
getBytes
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
Check if a resource is available- Parameters:
path- the path to the resource- Returns:
- true if the resource is accessible via resource cache sources
-