Class CachingProxy

java.lang.Object
net.targetr.wtm3.net.http.proxy.Proxy
net.targetr.wtm3.net.http.proxy.cache.CachingProxy

public class CachingProxy extends Proxy
A proxy that caches responses from target servers to avoid repeated network requests. Entries are keyed only by target URL, ignoring request method, body and headers. Callers must use targets whose responses can be shared on that basis.
  • Constructor Details

    • CachingProxy

      public CachingProxy(HttpRequest httpReq, String target, long maxAgeMillis, long maxObjectSize, ProxyCache cache)
      Creates a new caching proxy.
      Parameters:
      httpReq - The incoming HTTP request.
      target - The target URL to proxy to.
      maxAgeMillis - The maximum time in milliseconds to keep the response in the cache.
      maxObjectSize - The maximum size of an object to cache.
      cache - The cache used to look up and store proxied responses.
  • Method Details

    • getResponse

      public HttpResponse getResponse() throws Exception
      Returns a cached body if available; otherwise forwards the request upstream. Cache hits use status 200 and retain content type and encoding, not all upstream headers.
      Overrides:
      getResponse in class Proxy
      Returns:
      Response for proxying data from the target address. Or ErrorResponse.
      Throws:
      Exception - An exception occured while connecting to the target or sending request.
    • makeResponse

      protected ProxyResponse makeResponse() throws Exception
      Description copied from class: Proxy
      Creates a ProxyResponse to stream the response back.
      Overrides:
      makeResponse in class Proxy
      Returns:
      A new ProxyResponse instance.
      Throws:
      Exception - If an error occurs creating the response.