Search in sources :

Example 16 with HttpCacheKey

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

the class RemoteFileServlet method writeResource.

private boolean writeResource(final HttpServletRequest req, final HttpServletResponse resp, final String resourcePath) throws IOException {
    IRemoteFileService rfs = BEANS.get(getConfiguredRemoteFileServiceClass());
    RemoteFile spec = new RemoteFile((resourcePath == null) ? null : StringUtility.join("", m_folder, resourcePath), -1);
    RemoteFile remoteFile = rfs.getRemoteFile(spec);
    if (!remoteFile.exists()) {
        return false;
    }
    HttpCacheObject obj = new HttpCacheObject(new HttpCacheKey(resourcePath), remoteFile.toBinaryResource());
    if (BEANS.get(HttpCacheControl.class).checkAndSetCacheHeaders(req, resp, obj)) {
        return true;
    }
    rfs.streamRemoteFile(remoteFile, resp.getOutputStream());
    return true;
}
Also used : HttpCacheControl(org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheControl) HttpCacheKey(org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheKey) RemoteFile(org.eclipse.scout.rt.shared.services.common.file.RemoteFile) HttpCacheObject(org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheObject) IRemoteFileService(org.eclipse.scout.rt.shared.services.common.file.IRemoteFileService)

Example 17 with HttpCacheKey

use of org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheKey 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 18 with HttpCacheKey

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

the class HtmlFileLoader method createCacheKey.

@Override
public HttpCacheKey createCacheKey(String pathInfo) {
    Map<String, String> attrs = new HashMap<>();
    // Cache key for HTML files include locale and theme, because "<scout:message>" tags are
    // locale-dependent, while CSS files (and therefore their fingerprint) are theme-dependent.
    Locale locale = NlsLocale.getOrElse(null);
    if (locale != null) {
        attrs.put(LOCALE_KEY, locale.toString());
    }
    attrs.put(THEME_KEY, m_theme);
    return new HttpCacheKey(pathInfo, attrs);
}
Also used : NlsLocale(org.eclipse.scout.rt.platform.nls.NlsLocale) Locale(java.util.Locale) HashMap(java.util.HashMap) HttpCacheKey(org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheKey)

Example 19 with HttpCacheKey

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

the class PrebuildFiles method loadResource.

protected HttpCacheObject loadResource(String file) throws IOException {
    String defaultTheme = UiThemeUtility.getConfiguredTheme();
    HtmlFileLoader loader = new HtmlFileLoader(defaultTheme, true, true);
    HttpCacheKey cacheKey = loader.createCacheKey(file);
    return loader.loadResource(cacheKey);
}
Also used : HttpCacheKey(org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheKey) HtmlFileLoader(org.eclipse.scout.rt.ui.html.res.loader.HtmlFileLoader)

Example 20 with HttpCacheKey

use of org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheKey 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)

Aggregations

HttpCacheKey (org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheKey)31 Test (org.junit.Test)24 HttpCacheObject (org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheObject)20 BinaryResource (org.eclipse.scout.rt.platform.resource.BinaryResource)15 HttpCacheControl (org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheControl)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 HashMap (java.util.HashMap)1 Locale (java.util.Locale)1 ServletContext (javax.servlet.ServletContext)1 NlsLocale (org.eclipse.scout.rt.platform.nls.NlsLocale)1 ImmutablePair (org.eclipse.scout.rt.platform.util.ImmutablePair)1 IHttpResourceCache (org.eclipse.scout.rt.server.commons.servlet.cache.IHttpResourceCache)1 IRemoteFileService (org.eclipse.scout.rt.shared.services.common.file.IRemoteFileService)1 RemoteFile (org.eclipse.scout.rt.shared.services.common.file.RemoteFile)1 HtmlFileLoader (org.eclipse.scout.rt.ui.html.res.loader.HtmlFileLoader)1