use of org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheKey in project scout.rt by eclipse.
the class HtmlDocumentParser method getScriptAndFingerprint.
/**
* Returns the external script name including a content-based fingerprint.
*/
protected Pair<HttpCacheObject, String> getScriptAndFingerprint(String internalPath) throws IOException {
ScriptFileLoader scriptLoader = createScriptFileLoader();
HttpCacheKey cacheKey = scriptLoader.createCacheKey(internalPath);
HttpCacheObject script = m_cache.get(cacheKey);
if (script == null) {
// cache miss: try to load script
script = scriptLoader.loadResource(cacheKey);
}
if (script == null) {
// script not found -> no fingerprint
LOG.warn("Failed to locate script referenced in html file '{}': {}", m_params.getHtmlPath(), internalPath);
return null;
}
String fingerprint = Long.toHexString(script.getResource().getFingerprint());
return new ImmutablePair<>(script, fingerprint);
}
use of org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheKey in project scout.rt by eclipse.
the class HttpCacheControlTest method testCheckAndSet_EnableCaching_IfModifiedSince_NotModifiedAtFidelity.
@Test
public void testCheckAndSet_EnableCaching_IfModifiedSince_NotModifiedAtFidelity() throws Exception {
Mockito.when(req.getPathInfo()).thenReturn("/");
Mockito.when(req.getHeader(HttpCacheControl.ETAG)).thenReturn(null);
Mockito.when(req.getHeader(HttpCacheControl.IF_NONE_MATCH)).thenReturn(null);
Mockito.when(req.getDateHeader(HttpCacheControl.IF_MODIFIED_SINCE)).thenReturn(1000000L);
BinaryResource res = BinaryResources.create().withFilename("a.html").withContent("<html></html>".getBytes("UTF-8")).withCachingAllowed(true).withLastModified(1000000L + HttpCacheControl.IF_MODIFIED_SINCE_FIDELITY).build();
HttpCacheObject obj = new HttpCacheObject(new HttpCacheKey("/"), res);
boolean b = cc.checkAndSetCacheHeaders(req, resp, obj);
Assert.assertTrue(b);
Mockito.verify(req, ANY_TIMES).getPathInfo();
Mockito.verify(req, ANY_TIMES).getAttribute("javax.servlet.forward.path_info");
Mockito.verify(req, ANY_TIMES).getHeader(HttpCacheControl.ETAG);
Mockito.verify(req, ANY_TIMES).getHeader(HttpCacheControl.IF_NONE_MATCH);
Mockito.verify(req, ANY_TIMES).getDateHeader(HttpCacheControl.IF_MODIFIED_SINCE);
Mockito.verify(resp, ONCE).setHeader(HttpCacheControl.CACHE_CONTROL, "private, max-age=0, must-revalidate");
Mockito.verify(resp, ONCE).setStatus(HttpServletResponse.SC_NOT_MODIFIED);
}
use of org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheKey in project scout.rt by eclipse.
the class HttpCacheControlTest method testCheckAndSet_EnableCaching_MaxAge3.
@Test
public void testCheckAndSet_EnableCaching_MaxAge3() throws Exception {
Mockito.when(req.getPathInfo()).thenReturn("/");
Mockito.when(req.getHeader(HttpCacheControl.ETAG)).thenReturn(null);
Mockito.when(req.getHeader(HttpCacheControl.IF_NONE_MATCH)).thenReturn(null);
Mockito.when(req.getDateHeader(HttpCacheControl.IF_MODIFIED_SINCE)).thenReturn(0L);
BinaryResource res = BinaryResources.create().withFilename("a.html").withContent("<html></html>".getBytes("UTF-8")).withCachingAllowed(true).withCacheMaxAge(3).withLastModified(0L).build();
HttpCacheObject obj = new HttpCacheObject(new HttpCacheKey("/"), res);
boolean b = cc.checkAndSetCacheHeaders(req, resp, obj);
Assert.assertFalse(b);
Mockito.verify(req, ANY_TIMES).getPathInfo();
Mockito.verify(req, ANY_TIMES).getAttribute("javax.servlet.forward.path_info");
Mockito.verify(req, ANY_TIMES).getHeader(HttpCacheControl.ETAG);
Mockito.verify(req, ANY_TIMES).getHeader(HttpCacheControl.IF_NONE_MATCH);
Mockito.verify(req, ANY_TIMES).getDateHeader(HttpCacheControl.IF_MODIFIED_SINCE);
Mockito.verify(resp, ONCE).setHeader(HttpCacheControl.CACHE_CONTROL, "private, max-age=3, s-maxage=3");
Mockito.verify(resp, ONCE).setHeader(HttpCacheControl.ETAG, obj.createETag());
}
use of org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheKey in project scout.rt by eclipse.
the class HttpCacheControlTest method testCheckAndSet_EnableCaching_LastModified.
@Test
public void testCheckAndSet_EnableCaching_LastModified() throws Exception {
Mockito.when(req.getPathInfo()).thenReturn("/");
Mockito.when(req.getHeader(HttpCacheControl.ETAG)).thenReturn(null);
Mockito.when(req.getHeader(HttpCacheControl.IF_NONE_MATCH)).thenReturn(null);
Mockito.when(req.getDateHeader(HttpCacheControl.IF_MODIFIED_SINCE)).thenReturn(0L);
BinaryResource res = BinaryResources.create().withFilename("a.html").withContent("<html></html>".getBytes("UTF-8")).withCachingAllowed(true).withLastModifiedNow().build();
HttpCacheObject obj = new HttpCacheObject(new HttpCacheKey("/"), res);
boolean b = cc.checkAndSetCacheHeaders(req, resp, obj);
Assert.assertFalse(b);
Mockito.verify(req, ANY_TIMES).getPathInfo();
Mockito.verify(req, ANY_TIMES).getAttribute("javax.servlet.forward.path_info");
Mockito.verify(req, ANY_TIMES).getHeader(HttpCacheControl.ETAG);
Mockito.verify(req, ANY_TIMES).getHeader(HttpCacheControl.IF_NONE_MATCH);
Mockito.verify(req, ANY_TIMES).getDateHeader(HttpCacheControl.IF_MODIFIED_SINCE);
Mockito.verify(resp, ONCE).setHeader(HttpCacheControl.CACHE_CONTROL, "private, max-age=0, must-revalidate");
Mockito.verify(resp, ONCE).setHeader(HttpCacheControl.ETAG, obj.createETag());
Mockito.verify(resp, ONCE).setDateHeader(HttpCacheControl.LAST_MODIFIED, obj.getResource().getLastModified());
}
use of org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheKey in project scout.rt by eclipse.
the class HttpCacheControlTest method testCheckAndSet_EnableCaching_IfNoneMatch_false.
@Test
public void testCheckAndSet_EnableCaching_IfNoneMatch_false() throws Exception {
Mockito.when(req.getPathInfo()).thenReturn("/");
Mockito.when(req.getHeader(HttpCacheControl.ETAG)).thenReturn(null);
// non-matching E-Tag
Mockito.when(req.getHeader(HttpCacheControl.IF_NONE_MATCH)).thenReturn("W/\"FooBar\"");
Mockito.when(req.getDateHeader(HttpCacheControl.IF_MODIFIED_SINCE)).thenReturn(0L);
BinaryResource res = BinaryResources.create().withFilename("a.html").withContent("<html></html>".getBytes("UTF-8")).withCachingAllowed(true).withLastModifiedNow().build();
HttpCacheObject obj = new HttpCacheObject(new HttpCacheKey("/"), res);
boolean b = cc.checkAndSetCacheHeaders(req, resp, obj);
Assert.assertFalse(b);
Mockito.verify(req, ANY_TIMES).getPathInfo();
Mockito.verify(req, ANY_TIMES).getAttribute("javax.servlet.forward.path_info");
Mockito.verify(req, ANY_TIMES).getHeader(HttpCacheControl.ETAG);
Mockito.verify(req, ANY_TIMES).getHeader(HttpCacheControl.IF_NONE_MATCH);
Mockito.verify(req, ANY_TIMES).getDateHeader(HttpCacheControl.IF_MODIFIED_SINCE);
Mockito.verify(resp, ONCE).setHeader(HttpCacheControl.CACHE_CONTROL, "private, max-age=0, must-revalidate");
Mockito.verify(resp, ONCE).setHeader(HttpCacheControl.ETAG, obj.createETag());
Mockito.verify(resp, ONCE).setDateHeader(HttpCacheControl.LAST_MODIFIED, obj.getResource().getLastModified());
}
Aggregations