protected boolean isCacheable(HttpRequest request, HttpResponse response) { if (!isCacheable(request)) { return false; } if (request.getCacheTtl() != -1) { // Caching was forced. Ignore what the response wants. return true; } if (response.getHttpStatusCode() == HttpResponse.SC_NOT_MODIFIED) { // Shindig server will serve 304s. Do not cache 304s from the origin server. return false; } // If the HTTP response allows for it, we can cache. return !response.isStrictNoCache(); }
protected boolean isCacheable(HttpRequest request, HttpResponse response) { if (!isCacheable(request)) { return false; } if (request.getCacheTtl() != -1) { // Caching was forced. Ignore what the response wants. return true; } if (response.getHttpStatusCode() == HttpResponse.SC_NOT_MODIFIED) { // Shindig server will serve 304s. Do not cache 304s from the origin server. return false; } // If the HTTP response allows for it, we can cache. return !response.isStrictNoCache(); }
protected boolean isCacheable(HttpRequest request, HttpResponse response, boolean allowStrictNoCacheResponses) { if (!isCacheable(request)) { return false; } if (request.getCacheTtl() != -1) { // Caching was forced. Ignore what the response wants. return true; } if (response.getHttpStatusCode() == HttpResponse.SC_NOT_MODIFIED) { // Shindig server will serve 304s. Do not cache 304s from the origin server. return false; } // If we allow strict no-cache responses or the HTTP response allows for it, we can cache. return allowStrictNoCacheResponses || !response.isStrictNoCache(); }
public boolean addResponse(HttpRequest request, HttpResponse response) { if (isCacheable(request, response)) { // Both are cacheable. Check for forced cache TTL overrides. HttpResponseBuilder responseBuilder = new HttpResponseBuilder(response); int forcedTtl = request.getCacheTtl(); if (forcedTtl != -1) { responseBuilder.setCacheTtl(forcedTtl); } response = responseBuilder.create(); String keyString = createKey(request); addResponseImpl(keyString, response); return true; } return false; }
public boolean addResponse(HttpRequest request, HttpResponse response) { if (isCacheable(request, response)) { // Both are cacheable. Check for forced cache TTL overrides. HttpResponseBuilder responseBuilder = new HttpResponseBuilder(response); int forcedTtl = request.getCacheTtl(); if (forcedTtl != -1) { responseBuilder.setCacheTtl(forcedTtl); } response = responseBuilder.create(); String keyString = createKey(request); addResponseImpl(keyString, response); return true; } return false; }
@Override public boolean equals(Object obj) { if (obj == this) { return true; } if (!(obj instanceof HttpRequest)) { return false; } HttpRequest req = (HttpRequest)obj; return super.equals(obj) && req.getCacheTtl() == getCacheTtl() && req.getIgnoreCache() == getIgnoreCache(); }
@Override public boolean equals(Object obj) { if (obj == this) { return true; } if (!(obj instanceof HttpRequest)) { return false; } HttpRequest req = (HttpRequest)obj; return super.equals(obj) && req.getCacheTtl() == getCacheTtl() && req.getIgnoreCache() == getIgnoreCache(); }
@Override public boolean equals(Object obj) { if (obj == this) { return true; } if (!(obj instanceof HttpRequest)) { return false; } HttpRequest req = (HttpRequest)obj; return super.equals(obj) && req.getCacheTtl() == getCacheTtl() && req.getIgnoreCache() == getIgnoreCache(); }
@Test public void testGetRequestWithRefresh() throws Exception { expect(request.getParameter(Param.REFRESH.getKey())).andReturn("120").anyTimes(); Capture<HttpRequest> requestCapture = new Capture<HttpRequest>(); expect(pipeline.execute(capture(requestCapture))).andReturn(new HttpResponse(RESPONSE_BODY)); replay(); handler.fetch(request, recorder); HttpRequest httpRequest = requestCapture.getValue(); assertEquals("public,max-age=120", recorder.getHeader("Cache-Control")); assertEquals(120, httpRequest.getCacheTtl()); }
@Test public void testGetRequestWithRefresh() throws Exception { expect(request.getParameter(Param.REFRESH.getKey())).andReturn("120").anyTimes(); Capture<HttpRequest> requestCapture = new Capture<HttpRequest>(); expect(pipeline.execute(capture(requestCapture))).andReturn(new HttpResponse(RESPONSE_BODY)); replay(); handler.fetch(request, recorder); HttpRequest httpRequest = requestCapture.getValue(); assertEquals("public,max-age=120", recorder.getHeader("Cache-Control")); assertEquals(120, httpRequest.getCacheTtl()); }
@Test public void testGetRequestWithRefresh() throws Exception { expect(request.getParameter(Param.REFRESH.getKey())).andReturn("120").anyTimes(); Capture<HttpRequest> requestCapture = new Capture<HttpRequest>(); expect(pipeline.execute(capture(requestCapture))).andReturn(new HttpResponse(RESPONSE_BODY)); replay(); handler.fetch(request, recorder); HttpRequest httpRequest = requestCapture.getValue(); assertEquals("public,max-age=120", recorder.getHeader("Cache-Control")); assertEquals(120, httpRequest.getCacheTtl()); }
@Test public void testGetRequestWithBadTtl() throws Exception { expect(request.getParameter(Param.REFRESH.getKey())).andReturn("foo").anyTimes(); Capture<HttpRequest> requestCapture = new Capture<HttpRequest>(); expect(pipeline.execute(capture(requestCapture))).andReturn(new HttpResponse(RESPONSE_BODY)); replay(); try { handler.fetch(request, recorder); } catch (GadgetException e) { // Expected - catch now occurs at the MakeRequestServlet level. } HttpRequest httpRequest = requestCapture.getValue(); assertEquals(null, recorder.getHeader("Cache-Control")); assertEquals(-1, httpRequest.getCacheTtl()); }
@Test public void testGetRequestWithBadTtl() throws Exception { expect(request.getParameter(Param.REFRESH.getKey())).andReturn("foo").anyTimes(); Capture<HttpRequest> requestCapture = new Capture<HttpRequest>(); expect(pipeline.execute(capture(requestCapture))).andReturn(new HttpResponse(RESPONSE_BODY)); replay(); try { handler.fetch(request, recorder); } catch (GadgetException e) { // Expected - catch now occurs at the MakeRequestServlet level. } HttpRequest httpRequest = requestCapture.getValue(); assertEquals(null, recorder.getHeader("Cache-Control")); assertEquals(-1, httpRequest.getCacheTtl()); }
@Test public void testGetRequestWithBadTtl() throws Exception { expect(request.getParameter(Param.REFRESH.getKey())).andReturn("foo").anyTimes(); Capture<HttpRequest> requestCapture = new Capture<HttpRequest>(); expect(pipeline.execute(capture(requestCapture))).andReturn(new HttpResponse(RESPONSE_BODY)); replay(); try { handler.fetch(request, recorder); } catch (GadgetException e) { // Expected - catch now occurs at the MakeRequestServlet level. } HttpRequest httpRequest = requestCapture.getValue(); assertEquals(null, recorder.getHeader("Cache-Control")); assertEquals(-1, httpRequest.getCacheTtl()); }
@Test public void ttlPropagatesToFetcher() throws Exception { CapturingFetcher capturingFetcher = new CapturingFetcher(); MessageBundleFactory factory = new DefaultMessageBundleFactory( new ImmediateExecutorService(), capturingFetcher, cacheProvider, MAX_AGE); factory.getBundle(gadgetSpec, LOCALE, false, ContainerConfig.DEFAULT_CONTAINER, null); assertEquals(MAX_AGE / 1000, capturingFetcher.request.getCacheTtl()); }
@Test public void ttlPropagatesToFetcher() throws Exception { CapturingFetcher capturingFetcher = new CapturingFetcher(); MessageBundleFactory factory = new DefaultMessageBundleFactory( new ImmediateExecutorService(), capturingFetcher, cacheProvider, MAX_AGE); factory.getBundle(gadgetSpec, LOCALE, false, ContainerConfig.DEFAULT_CONTAINER, null); assertEquals(MAX_AGE / 1000, capturingFetcher.request.getCacheTtl()); }
@Test public void ttlPropagatesToFetcher() throws Exception { CapturingFetcher capturingFetcher = new CapturingFetcher(); MessageBundleFactory factory = new DefaultMessageBundleFactory( new TestExecutorService(), capturingFetcher, cacheProvider, MAX_AGE); factory.getBundle(gadgetSpec, LOCALE, false, ContainerConfig.DEFAULT_CONTAINER); assertEquals(MAX_AGE / 1000, capturingFetcher.request.getCacheTtl()); }
@Test public void ttlPropagatesToPipeline() throws Exception { CapturingPipeline capturingPipeline = new CapturingPipeline(); GadgetSpecFactory forcedCacheFactory = new DefaultGadgetSpecFactory( new ImmediateExecutorService(), capturingPipeline, cacheProvider, 10000); forcedCacheFactory.getGadgetSpec(createContext(SPEC_URL, false)); assertEquals(10, capturingPipeline.request.getCacheTtl()); }
@Test public void ttlPropagatesToPipeline() throws Exception { CapturingPipeline capturingPipeline = new CapturingPipeline(); GadgetSpecFactory forcedCacheFactory = new DefaultGadgetSpecFactory( new TestExecutorService(), capturingPipeline, cacheProvider, 10000); forcedCacheFactory.getGadgetSpec(createContext(SPEC_URL, false)); assertEquals(10, capturingPipeline.request.getCacheTtl()); }
@Test public void ttlPropagatesToPipeline() throws Exception { CapturingPipeline capturingPipeline = new CapturingPipeline(); GadgetSpecFactory forcedCacheFactory = new DefaultGadgetSpecFactory( new ImmediateExecutorService(), capturingPipeline, cacheProvider, 10000); forcedCacheFactory.getGadgetSpec(createContext(SPEC_URL, false)); assertEquals(10, capturingPipeline.request.getCacheTtl()); }