Search in sources :

Example 1 with HttpCacheObject

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

Example 2 with HttpCacheObject

use of org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheObject in project scout.rt by eclipse.

the class HtmlFileLoader method loadResource.

@Override
public HttpCacheObject loadResource(HttpCacheKey cacheKey) throws IOException {
    String pathInfo = cacheKey.getResourcePath();
    BinaryResource content = loadResource(pathInfo);
    if (content == null) {
        return null;
    }
    // no cache-control, only E-Tag checks to make sure that a session with timeout is correctly
    // forwarded to the login using a GET request BEFORE the first json POST request
    HttpCacheObject httpCacheObject = new HttpCacheObject(cacheKey, content);
    // Suppress automatic "compatibility mode" in IE in intranet zone
    httpCacheObject.addHttpResponseInterceptor(new HttpResponseHeaderContributor("X-UA-Compatible", "IE=edge") {

        private static final long serialVersionUID = 1L;

        @Override
        public void intercept(HttpServletRequest req, HttpServletResponse resp) {
            HttpClientInfo httpClientInfo = HttpClientInfo.get(req);
            if (httpClientInfo.isMshtml()) {
                // Send headers only for IE
                super.intercept(req, resp);
            }
        }
    });
    return httpCacheObject;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) BinaryResource(org.eclipse.scout.rt.platform.resource.BinaryResource) HttpResponseHeaderContributor(org.eclipse.scout.rt.server.commons.servlet.cache.HttpResponseHeaderContributor) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpCacheObject(org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheObject) HttpClientInfo(org.eclipse.scout.rt.server.commons.servlet.HttpClientInfo)

Example 3 with HttpCacheObject

use of org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheObject in project scout.rt by eclipse.

the class PrebuildFiles method buildResources.

/**
 * Pre-builds the HTML files and the referenced scripts (JS, CSS) in the HTML document. Each document is put into the
 * HTTP resource cache, so we don't have to build them again later. However, we have to make a few assumptions here.
 * Since we have no HTTP request or session at this point, we assume:
 * <ul>
 * <li>the default locale</li>
 * <li>the default theme</li>
 * <li>minifying is enabled</li>
 * <li>caching is enabled</li>
 * </ul>
 */
protected void buildResources() {
    long t0 = System.nanoTime();
    List<String> files = CONFIG.getPropertyValue(UiPrebuildFilesProperty.class);
    IHttpResourceCache httpResourceCache = BEANS.get(GlobalHttpResourceCache.class);
    for (String file : files) {
        LOG.info("Pre-building resource '{}'...", file);
        try {
            HttpCacheObject cacheObject = loadResource(file);
            httpResourceCache.put(cacheObject);
        } catch (IOException e) {
            LOG.error("Failed to load resource", e);
        }
    }
    LOG.info("Finished pre-building of {} web resources in {} ms", files.size(), StringUtility.formatNanos(System.nanoTime() - t0));
}
Also used : IOException(java.io.IOException) HttpCacheObject(org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheObject) IHttpResourceCache(org.eclipse.scout.rt.server.commons.servlet.cache.IHttpResourceCache)

Example 4 with HttpCacheObject

use of org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheObject in project scout.rt by eclipse.

the class AbstractResourceLoader method loadResource.

@Override
public HttpCacheObject loadResource(HttpCacheKey cacheKey) throws IOException {
    String pathInfo = cacheKey.getResourcePath();
    BinaryResource content = loadResource(pathInfo);
    if (content == null) {
        return null;
    }
    return new HttpCacheObject(cacheKey, content);
}
Also used : BinaryResource(org.eclipse.scout.rt.platform.resource.BinaryResource) HttpCacheObject(org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheObject)

Example 5 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_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);
}
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