use of org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheObject in project scout.rt by eclipse.
the class HtmlDocumentParser method createExternalPath.
/**
* Creates the external path of the given resource, including fingerprint and '.min' extensions. This method also
* deals with caching, since we must build a script file first, before we can calculate its fingerprint.
*/
protected String createExternalPath(String internalPath) throws IOException {
File srcFile = new File(internalPath);
String[] filenameParts = FileUtility.getFilenameParts(srcFile);
Pair<HttpCacheObject, String> scriptAndFingerprint = null;
// When caching is enabled we must calculate the fingerprint for the script file
if (m_params.isCacheEnabled()) {
scriptAndFingerprint = getScriptAndFingerprint(internalPath);
}
// append path to file
StringBuilder externalPathSb = new StringBuilder();
if (srcFile.getParent() != null) {
externalPathSb.append(srcFile.getParent()).append("/");
}
// append file name without file-extension
externalPathSb.append(getScriptFileName(filenameParts[0]));
// append fingerprint
if (scriptAndFingerprint != null) {
String fingerprint = scriptAndFingerprint.getRight();
externalPathSb.append("-").append(fingerprint);
}
// append 'min'
if (m_params.isMinify()) {
externalPathSb.append(".min");
}
// append file-extension
externalPathSb.append(".").append(getScriptFileExtension(filenameParts[1]));
String externalPath = externalPathSb.toString();
// want to access the cached file by its internal path.
if (scriptAndFingerprint != null) {
HttpCacheObject internalCacheObject = scriptAndFingerprint.getLeft();
HttpCacheKey externalCacheKey = createScriptFileLoader().createCacheKey("/" + externalPath);
HttpCacheObject externalCacheObject = new HttpCacheObject(externalCacheKey, internalCacheObject.getResource());
m_cache.put(internalCacheObject);
m_cache.put(externalCacheObject);
}
return externalPath;
}
use of org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheObject in project scout.rt by eclipse.
the class ResourceRequestHandler method resolveResourceFromCache.
protected HttpCacheObject resolveResourceFromCache(HttpServletRequest req, String pathInfoEx, IResourceLoader resourceLoader) throws IOException {
// Create cache key for resource and check if resource exists in cache
HttpCacheKey cacheKey = resourceLoader.createCacheKey(pathInfoEx);
// When caching is disabled, always load resource
if (!UrlHints.isCacheHint(req)) {
LOG.debug("Requested resource with cacheKey={}. Caching is disabled by URL hint", cacheKey);
return resourceLoader.loadResource(cacheKey);
}
IHttpResourceCache resourceCache = resourceLoader.getCache(cacheKey);
if (resourceCache == null) {
LOG.debug("Loader for resource with cacheKey={} does not support caching.", cacheKey);
return resourceLoader.loadResource(cacheKey);
}
String cacheResultMsg;
HttpCacheObject resource = resourceCache.get(cacheKey);
if (resource == null) {
// Cache miss: resource not found in cache --> load it
resource = resourceLoader.loadResource(cacheKey);
if (resource == null) {
cacheResultMsg = "Resource is not cached (cache miss), could not load resource (not added to the cache)";
} else {
resourceCache.put(resource);
cacheResultMsg = "Resource is not cached (cache miss), resource loaded and added to the cache";
}
} else {
// Cache hit
cacheResultMsg = "Resource found in cache (cache hit), using cached resource";
}
LOG.debug("Requested resource with cacheKey={}. {}", cacheKey, cacheResultMsg);
return resource;
}
use of org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheObject in project scout.rt by eclipse.
the class ResourceRequestHandler method handleGet.
@Override
public boolean handleGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String pathInfoEx = resolvePathInfoEx(req);
// Create loader for the requested resource type
IResourceLoader resourceLoader = null;
for (ResourceLoaders f : m_resourceLoaders) {
resourceLoader = f.create(req, pathInfoEx);
if (resourceLoader != null) {
break;
}
}
if (resourceLoader == null) {
return false;
}
HttpCacheObject resource = resolveResourceFromCache(req, pathInfoEx, resourceLoader);
// check resource existence (also ignore resources without content, to prevent invalid "content-length" header and NPE in write() method)
if (resource == null || resource.getResource() == null || resource.getResource().getContent() == null) {
return false;
}
// cached in browser? -> returns 304 if the resource has not been modified
if (m_httpCacheControl.checkAndSetCacheHeaders(req, resp, resource)) {
return true;
}
BinaryResource binaryResource = resource.getResource();
// set the resp headers only if no 304 (according to spec: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.5)
setHttpResponseHeaders(resp, binaryResource);
// Apply response interceptors
resource.applyHttpResponseInterceptors(req, resp);
if (!"HEAD".equals(req.getMethod())) {
resp.getOutputStream().write(binaryResource.getContent());
}
return true;
}
use of org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheObject in project scout.rt by eclipse.
the class HttpCacheControlTest method testCheckAndSet_EnableCaching_IfNoneMatch_true.
@Test
public void testCheckAndSet_EnableCaching_IfNoneMatch_true() throws Exception {
Mockito.when(req.getPathInfo()).thenReturn("/");
Mockito.when(req.getHeader(HttpCacheControl.ETAG)).thenReturn(null);
// matching E-Tag
Mockito.when(req.getHeader(HttpCacheControl.IF_NONE_MATCH)).thenReturn("W/\"FooBar\", W/\"13-535168142\"");
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.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.HttpCacheObject in project scout.rt by eclipse.
the class HttpResourceCacheTest method testPutNotCachable.
@Test
public void testPutNotCachable() throws Exception {
BinaryResource res = BinaryResources.create().withFilename("a.html").withContent("<html></html>".getBytes("UTF-8")).build();
HttpCacheKey key = new HttpCacheKey("/");
HttpCacheObject obj = new HttpCacheObject(key, res);
boolean b = rc.put(obj);
Assert.assertFalse(b);
}
Aggregations