Search in sources :

Example 16 with HttpCacheObject

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;
}
Also used : HttpCacheKey(org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheKey) File(java.io.File) HttpCacheObject(org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheObject)

Example 17 with HttpCacheObject

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;
}
Also used : HttpCacheKey(org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheKey) HttpCacheObject(org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheObject) IHttpResourceCache(org.eclipse.scout.rt.server.commons.servlet.cache.IHttpResourceCache)

Example 18 with HttpCacheObject

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;
}
Also used : BinaryResource(org.eclipse.scout.rt.platform.resource.BinaryResource) IResourceLoader(org.eclipse.scout.rt.ui.html.res.loader.IResourceLoader) ResourceLoaders(org.eclipse.scout.rt.ui.html.res.loader.ResourceLoaders) HttpCacheObject(org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheObject)

Example 19 with HttpCacheObject

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);
}
Also used : BinaryResource(org.eclipse.scout.rt.platform.resource.BinaryResource) HttpCacheKey(org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheKey) HttpCacheObject(org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheObject) Test(org.junit.Test)

Example 20 with HttpCacheObject

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);
}
Also used : BinaryResource(org.eclipse.scout.rt.platform.resource.BinaryResource) HttpCacheKey(org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheKey) HttpCacheObject(org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheObject) Test(org.junit.Test)

Aggregations

HttpCacheObject (org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheObject)25 HttpCacheKey (org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheKey)20 BinaryResource (org.eclipse.scout.rt.platform.resource.BinaryResource)19 Test (org.junit.Test)15 IOException (java.io.IOException)2 HttpCacheControl (org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheControl)2 IHttpResourceCache (org.eclipse.scout.rt.server.commons.servlet.cache.IHttpResourceCache)2 File (java.io.File)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 URL (java.net.URL)1 URLConnection (java.net.URLConnection)1 ServletContext (javax.servlet.ServletContext)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 ImmutablePair (org.eclipse.scout.rt.platform.util.ImmutablePair)1 HttpClientInfo (org.eclipse.scout.rt.server.commons.servlet.HttpClientInfo)1 HttpResponseHeaderContributor (org.eclipse.scout.rt.server.commons.servlet.cache.HttpResponseHeaderContributor)1 IHttpResponseInterceptor (org.eclipse.scout.rt.server.commons.servlet.cache.IHttpResponseInterceptor)1 IRemoteFileService (org.eclipse.scout.rt.shared.services.common.file.IRemoteFileService)1