Search in sources :

Example 1 with SitePageContext

use of org.ambraproject.wombat.freemarker.SitePageContext in project wombat by PLOS.

the class RenderAssetsDirective method renderAssets.

/**
 * Renders queued asset links as HTML. If in dev mode, the rendered output will be a sequence of plain links to the
 * asset resources. Else, the queued assets will be compiled into a minified form, and the rendered output will be a
 * single link to the result.
 * <p/>
 * Either way, assets will be ordered according to their dependencies, defaulting to the order in which they were
 * enqueued. (That is, in dev mode the links appear in that order, and in production mode the assets are concatenated
 * in that order before they are minified.)
 * <p/>
 * This method pulls asset nodes from the named environment variable. Executing the method clears the queue.
 *
 * @param assetType           defines the type of asset (.js or .css)
 * @param requestVariableName the name of the request variable that uncompiled assets have been stored in by calls to
 *                            a subclass of {@link AssetDirective}
 * @param environment         freemarker execution environment
 * @throws TemplateException
 * @throws IOException
 */
protected void renderAssets(AssetService.AssetType assetType, String requestVariableName, Environment environment) throws TemplateException, IOException {
    HttpServletRequest request = ((HttpRequestHashModel) environment.getDataModel().get("Request")).getRequest();
    Map<String, AssetNode> assetNodes = (Map<String, AssetNode>) request.getAttribute(requestVariableName);
    if (assetNodes == null)
        return;
    List<String> assetPaths = sortNodes(assetNodes.values());
    // Reset in case new assets get put in for a second render
    assetNodes.clear();
    if (assetPaths != null && !assetPaths.isEmpty()) {
        SitePageContext sitePageContext = new SitePageContext(siteResolver, environment);
        if (runtimeConfiguration.getCompiledAssetDir() == null) {
            for (String assetPath : assetPaths) {
                String assetAddress = Link.toLocalSite(sitePageContext.getSite()).toPath(assetPath).get(sitePageContext.getRequest());
                environment.getOut().write(getHtml(assetAddress));
            }
        } else {
            Site site = sitePageContext.getSite();
            String assetLink = assetService.getCompiledAssetLink(assetType, assetPaths, site);
            String path = PathUtil.JOINER.join(AssetService.AssetUrls.RESOURCE_NAMESPACE, assetLink);
            String assetAddress = Link.toLocalSite(site).toPath(path).get(request);
            environment.getOut().write(getHtml(assetAddress));
        }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Site(org.ambraproject.wombat.config.site.Site) HttpRequestHashModel(freemarker.ext.servlet.HttpRequestHashModel) SitePageContext(org.ambraproject.wombat.freemarker.SitePageContext) Map(java.util.Map)

Aggregations

HttpRequestHashModel (freemarker.ext.servlet.HttpRequestHashModel)1 Map (java.util.Map)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 Site (org.ambraproject.wombat.config.site.Site)1 SitePageContext (org.ambraproject.wombat.freemarker.SitePageContext)1