Search in sources :

Example 1 with HttpRequestHashModel

use of freemarker.ext.servlet.HttpRequestHashModel in project wombat by PLOS.

the class AssetDirective method addAsset.

/**
 * Called when we are adding a new asset file to the page. The asset file will be queued to render by {@link
 * RenderAssetsDirective#renderAssets}.
 *
 * @param assetPath    path to the asset file being added
 * @param dependencies paths to other assets which this asset must come after (may be null for none)
 * @param environment  freemarker execution environment
 * @throws TemplateException
 * @throws IOException
 */
protected void addAsset(String assetPath, Collection<String> dependencies, Environment environment) throws TemplateException, IOException {
    assetPath = assetPath.trim();
    String requestVariableName = getRequestVariableName();
    // Add the asset file to a list that's scoped to the current request. We'll render the asset link(s) later.
    // If not in dev mode, we will minify, concatenate, and render them as a single link then.
    HttpServletRequest request = ((HttpRequestHashModel) environment.getDataModel().get("Request")).getRequest();
    Map<String, AssetNode> assetNodes = (Map<String, AssetNode>) request.getAttribute(requestVariableName);
    if (assetNodes == null) {
        // order is significant
        assetNodes = new LinkedHashMap<>();
    }
    AssetNode node = assetNodes.get(assetPath);
    if (node == null) {
        node = new AssetNode(assetPath, dependencies);
        assetNodes.put(assetPath, node);
    } else if (dependencies != null) {
        node.getDependencies().addAll(dependencies);
    }
    request.setAttribute(requestVariableName, assetNodes);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpRequestHashModel(freemarker.ext.servlet.HttpRequestHashModel) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 2 with HttpRequestHashModel

use of freemarker.ext.servlet.HttpRequestHashModel 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)

Example 3 with HttpRequestHashModel

use of freemarker.ext.servlet.HttpRequestHashModel in project freemarker by apache.

the class TaglibFactory method resolveRelativeUri.

private static String resolveRelativeUri(String uri) throws TaglibGettingException {
    TemplateModel reqHash;
    try {
        reqHash = Environment.getCurrentEnvironment().getVariable(FreemarkerServlet.KEY_REQUEST_PRIVATE);
    } catch (TemplateModelException e) {
        throw new TaglibGettingException("Failed to get FreemarkerServlet request information", e);
    }
    if (reqHash instanceof HttpRequestHashModel) {
        HttpServletRequest req = ((HttpRequestHashModel) reqHash).getRequest();
        String pi = req.getPathInfo();
        String reqPath = req.getServletPath();
        if (reqPath == null) {
            reqPath = "";
        }
        reqPath += (pi == null ? "" : pi);
        // We don't care about paths with ".." in them. If the container
        // wishes to resolve them on its own, let it be.
        int lastSlash = reqPath.lastIndexOf('/');
        if (lastSlash != -1) {
            return reqPath.substring(0, lastSlash + 1) + uri;
        } else {
            return '/' + uri;
        }
    }
    throw new TaglibGettingException("Can't resolve relative URI " + uri + " as request URL information is unavailable.");
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) TemplateModelException(freemarker.template.TemplateModelException) HttpRequestHashModel(freemarker.ext.servlet.HttpRequestHashModel) TemplateModel(freemarker.template.TemplateModel)

Example 4 with HttpRequestHashModel

use of freemarker.ext.servlet.HttpRequestHashModel in project entando-core by entando.

the class AbstractTestExecutorService method createModel.

protected TemplateModel createModel(ObjectWrapper wrapper) throws Throwable {
    HttpServletRequest request = super.getRequestContext().getRequest();
    HttpServletResponse response = super.getRequestContext().getResponse();
    ServletContext servletContext = request.getSession().getServletContext();
    // super.createModel(wrapper, servletContext, request, response);
    AllHttpScopesHashModel hashModel = new AllHttpScopesHashModel(wrapper, servletContext, request);
    ControllerServlet servlet = new ControllerServlet();
    MockServletConfig config = new MockServletConfig(servletContext);
    servlet.init(config);
    ServletContextHashModel newServletContextModel = new ServletContextHashModel(servlet, wrapper);
    ServletContextHashModel servletContextModel = new ServletContextHashModel(servlet, wrapper);
    servletContext.setAttribute(ATTR_APPLICATION_MODEL, servletContextModel);
    TaglibFactory taglibs = new TaglibFactory(servletContext);
    servletContext.setAttribute(ATTR_JSP_TAGLIBS_MODEL, taglibs);
    hashModel.putUnlistedModel(FreemarkerServlet.KEY_APPLICATION, newServletContextModel);
    hashModel.putUnlistedModel(FreemarkerServlet.KEY_APPLICATION_PRIVATE, newServletContextModel);
    hashModel.putUnlistedModel(FreemarkerServlet.KEY_JSP_TAGLIBS, taglibs);
    HttpRequestHashModel requestModel = new HttpRequestHashModel(request, response, wrapper);
    request.setAttribute(ATTR_REQUEST_MODEL, requestModel);
    hashModel.putUnlistedModel(FreemarkerServlet.KEY_REQUEST_PRIVATE, requestModel);
    return hashModel;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) AllHttpScopesHashModel(freemarker.ext.servlet.AllHttpScopesHashModel) HttpRequestHashModel(freemarker.ext.servlet.HttpRequestHashModel) ServletContextHashModel(freemarker.ext.servlet.ServletContextHashModel) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletContext(javax.servlet.ServletContext) MockServletConfig(org.springframework.mock.web.MockServletConfig) TaglibFactory(freemarker.ext.jsp.TaglibFactory) ControllerServlet(org.entando.entando.aps.servlet.ControllerServlet)

Example 5 with HttpRequestHashModel

use of freemarker.ext.servlet.HttpRequestHashModel in project wombat by PLOS.

the class AppLinkDirective method getValue.

@Override
protected String getValue(Environment env, Map params) throws TemplateModelException {
    Object pathObj = params.get("path");
    if (!(pathObj instanceof TemplateScalarModel)) {
        throw new RuntimeException("path parameter required");
    }
    String path = ((TemplateScalarModel) pathObj).getAsString();
    HttpServletRequest request = ((HttpRequestHashModel) env.getDataModel().get("Request")).getRequest();
    return request.getContextPath() + "/" + path;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpRequestHashModel(freemarker.ext.servlet.HttpRequestHashModel) TemplateScalarModel(freemarker.template.TemplateScalarModel)

Aggregations

HttpRequestHashModel (freemarker.ext.servlet.HttpRequestHashModel)8 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 TaglibFactory (freemarker.ext.jsp.TaglibFactory)4 HttpSessionHashModel (freemarker.ext.servlet.HttpSessionHashModel)3 ServletContextHashModel (freemarker.ext.servlet.ServletContextHashModel)3 HttpSession (javax.servlet.http.HttpSession)3 TemplateModel (freemarker.template.TemplateModel)2 Map (java.util.Map)2 ServletContext (javax.servlet.ServletContext)2 AllHttpScopesHashModel (freemarker.ext.servlet.AllHttpScopesHashModel)1 HttpRequestParametersHashModel (freemarker.ext.servlet.HttpRequestParametersHashModel)1 TemplateModelException (freemarker.template.TemplateModelException)1 TemplateScalarModel (freemarker.template.TemplateScalarModel)1 LinkedHashMap (java.util.LinkedHashMap)1 GenericServlet (javax.servlet.GenericServlet)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Site (org.ambraproject.wombat.config.site.Site)1 SitePageContext (org.ambraproject.wombat.freemarker.SitePageContext)1 Delegator (org.apache.ofbiz.entity.Delegator)1 GenericValue (org.apache.ofbiz.entity.GenericValue)1