Search in sources :

Example 1 with TaglibFactory

use of freemarker.ext.jsp.TaglibFactory in project spring-framework by spring-projects.

the class FreeMarkerView method initServletContext.

/**
	 * Invoked on startup. Looks for a single FreeMarkerConfig bean to
	 * find the relevant Configuration for this factory.
	 * <p>Checks that the template for the default Locale can be found:
	 * FreeMarker will check non-Locale-specific templates if a
	 * locale-specific one is not found.
	 * @see freemarker.cache.TemplateCache#getTemplate
	 */
@Override
protected void initServletContext(ServletContext servletContext) throws BeansException {
    if (getConfiguration() != null) {
        this.taglibFactory = new TaglibFactory(servletContext);
    } else {
        FreeMarkerConfig config = autodetectConfiguration();
        setConfiguration(config.getConfiguration());
        this.taglibFactory = config.getTaglibFactory();
    }
    GenericServlet servlet = new GenericServletAdapter();
    try {
        servlet.init(new DelegatingServletConfig());
    } catch (ServletException ex) {
        throw new BeanInitializationException("Initialization of GenericServlet adapter failed", ex);
    }
    this.servletContextHashModel = new ServletContextHashModel(servlet, getObjectWrapper());
}
Also used : ServletException(javax.servlet.ServletException) BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) GenericServlet(javax.servlet.GenericServlet) ServletContextHashModel(freemarker.ext.servlet.ServletContextHashModel) TaglibFactory(freemarker.ext.jsp.TaglibFactory)

Example 2 with TaglibFactory

use of freemarker.ext.jsp.TaglibFactory 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 3 with TaglibFactory

use of freemarker.ext.jsp.TaglibFactory in project freemarker by apache.

the class FreemarkerServlet method createModel.

protected TemplateModel createModel(ObjectWrapper objectWrapper, ServletContext servletContext, final HttpServletRequest request, final HttpServletResponse response) throws TemplateModelException {
    try {
        AllHttpScopesHashModel params = new AllHttpScopesHashModel(objectWrapper, servletContext, request);
        // Create hash model wrapper for servlet context (the application)
        final ServletContextHashModel servletContextModel;
        final TaglibFactory taglibFactory;
        synchronized (lazyInitFieldsLock) {
            if (this.servletContextModel == null) {
                servletContextModel = new ServletContextHashModel(this, objectWrapper);
                taglibFactory = createTaglibFactory(objectWrapper, servletContext);
                // For backward compatibility only. We don't use these:
                servletContext.setAttribute(ATTR_APPLICATION_MODEL, servletContextModel);
                servletContext.setAttribute(ATTR_JSP_TAGLIBS_MODEL, taglibFactory);
                initializeServletContext(request, response);
                this.taglibFactory = taglibFactory;
                this.servletContextModel = servletContextModel;
            } else {
                servletContextModel = this.servletContextModel;
                taglibFactory = this.taglibFactory;
            }
        }
        params.putUnlistedModel(KEY_APPLICATION, servletContextModel);
        params.putUnlistedModel(KEY_APPLICATION_PRIVATE, servletContextModel);
        params.putUnlistedModel(KEY_JSP_TAGLIBS, taglibFactory);
        // Create hash model wrapper for session
        HttpSessionHashModel sessionModel;
        HttpSession session = request.getSession(false);
        if (session != null) {
            sessionModel = (HttpSessionHashModel) session.getAttribute(ATTR_SESSION_MODEL);
            if (sessionModel == null || sessionModel.isOrphaned(session)) {
                sessionModel = new HttpSessionHashModel(session, objectWrapper);
                initializeSessionAndInstallModel(request, response, sessionModel, session);
            }
        } else {
            sessionModel = new HttpSessionHashModel(this, request, response, objectWrapper);
        }
        params.putUnlistedModel(KEY_SESSION, sessionModel);
        // Create hash model wrapper for request
        HttpRequestHashModel requestModel = (HttpRequestHashModel) request.getAttribute(ATTR_REQUEST_MODEL);
        if (requestModel == null || requestModel.getRequest() != request) {
            requestModel = new HttpRequestHashModel(request, response, objectWrapper);
            request.setAttribute(ATTR_REQUEST_MODEL, requestModel);
            request.setAttribute(ATTR_REQUEST_PARAMETERS_MODEL, createRequestParametersHashModel(request));
        }
        params.putUnlistedModel(KEY_REQUEST, requestModel);
        params.putUnlistedModel(KEY_INCLUDE, new IncludePage(request, response));
        params.putUnlistedModel(KEY_REQUEST_PRIVATE, requestModel);
        // Create hash model wrapper for request parameters
        HttpRequestParametersHashModel requestParametersModel = (HttpRequestParametersHashModel) request.getAttribute(ATTR_REQUEST_PARAMETERS_MODEL);
        params.putUnlistedModel(KEY_REQUEST_PARAMETERS, requestParametersModel);
        return params;
    } catch (ServletException e) {
        throw new TemplateModelException(e);
    } catch (IOException e) {
        throw new TemplateModelException(e);
    }
}
Also used : ServletException(javax.servlet.ServletException) TemplateModelException(freemarker.template.TemplateModelException) HttpSession(javax.servlet.http.HttpSession) TaglibFactory(freemarker.ext.jsp.TaglibFactory) IOException(java.io.IOException)

Example 4 with TaglibFactory

use of freemarker.ext.jsp.TaglibFactory in project nutzboot by nutzam.

the class FreemarkerView method jspTaglibs.

protected void jspTaglibs(ServletContext servletContext, HttpServletRequest request, HttpServletResponse response, Map<String, Object> model, ObjectWrapper wrapper) {
    synchronized (servletContext) {
        ServletContextHashModel servletContextModel = (ServletContextHashModel) servletContext.getAttribute(ATTR_APPLICATION_MODEL);
        if (Lang.isEmpty(servletContextModel)) {
            GenericServlet servlet = JspSupportServlet.jspSupportServlet;
            if (!Lang.isEmpty(servlet)) {
                servletContextModel = new ServletContextHashModel(servlet, wrapper);
                servletContext.setAttribute(ATTR_APPLICATION_MODEL, servletContextModel);
                TaglibFactory taglibs = new TaglibFactory(servletContext);
                servletContext.setAttribute(ATTR_JSP_TAGLIBS_MODEL, taglibs);
            }
        }
        model.put(KEY_APPLICATION, servletContextModel);
        TemplateModel tempModel = (TemplateModel) servletContext.getAttribute(ATTR_JSP_TAGLIBS_MODEL);
        model.put(KEY_JSP_TAGLIBS, tempModel);
    }
    HttpSession session = request.getSession(false);
    if (!Lang.isEmpty(session)) {
        model.put(KEY_SESSION_MODEL, new HttpSessionHashModel(session, wrapper));
    }
    HttpRequestHashModel requestModel = (HttpRequestHashModel) request.getAttribute(ATTR_REQUEST_MODEL);
    if (Lang.isEmpty(requestModel) || !Lang.equals(requestModel.getRequest(), request)) {
        requestModel = new HttpRequestHashModel(request, response, wrapper);
        request.setAttribute(ATTR_REQUEST_MODEL, requestModel);
    }
    model.put(KEY_REQUEST_MODEL, requestModel);
    HttpRequestParametersHashModel reqParametersModel = (HttpRequestParametersHashModel) request.getAttribute(ATTR_REQUEST_PARAMETERS_MODEL);
    if (Lang.isEmpty(reqParametersModel) || !Lang.equals(requestModel.getRequest(), request)) {
        reqParametersModel = new HttpRequestParametersHashModel(request);
        request.setAttribute(ATTR_REQUEST_PARAMETERS_MODEL, reqParametersModel);
    }
    model.put(KEY_REQUEST_PARAMETER_MODEL, reqParametersModel);
    Throwable exception = (Throwable) request.getAttribute("javax.servlet.error.exception");
    if (Lang.isEmpty(exception)) {
        exception = (Throwable) request.getAttribute("javax.servlet.error.JspException");
    }
    if (!Lang.isEmpty(exception)) {
        model.put(KEY_EXCEPTION, exception);
    }
}
Also used : HttpRequestHashModel(freemarker.ext.servlet.HttpRequestHashModel) GenericServlet(javax.servlet.GenericServlet) HttpSessionHashModel(freemarker.ext.servlet.HttpSessionHashModel) HttpSession(javax.servlet.http.HttpSession) ServletContextHashModel(freemarker.ext.servlet.ServletContextHashModel) TaglibFactory(freemarker.ext.jsp.TaglibFactory) TemplateModel(freemarker.template.TemplateModel) HttpRequestParametersHashModel(freemarker.ext.servlet.HttpRequestParametersHashModel)

Example 5 with TaglibFactory

use of freemarker.ext.jsp.TaglibFactory in project freemarker by apache.

the class FreemarkerServlet method createTaglibFactory.

/**
 * Called to create the {@link TaglibFactory} once per servlet context.
 * The default implementation configures it based on the servlet-init parameters and various other environmental
 * settings, so if you override this method, you should call super, then adjust the result.
 *
 * @since 2.3.22
 */
protected TaglibFactory createTaglibFactory(ObjectWrapper objectWrapper, ServletContext servletContext) throws TemplateModelException {
    TaglibFactory taglibFactory = new TaglibFactory(servletContext);
    taglibFactory.setObjectWrapper(objectWrapper);
    {
        List /*<MetaInfTldSource>*/
        mergedMetaInfTldSources = new ArrayList();
        if (metaInfTldSources != null) {
            mergedMetaInfTldSources.addAll(metaInfTldSources);
        }
        String sysPropVal = SecurityUtilities.getSystemProperty(SYSTEM_PROPERTY_META_INF_TLD_SOURCES, null);
        if (sysPropVal != null) {
            try {
                List metaInfTldSourcesSysProp = parseAsMetaInfTldLocations(sysPropVal);
                if (metaInfTldSourcesSysProp != null) {
                    mergedMetaInfTldSources.addAll(metaInfTldSourcesSysProp);
                }
            } catch (ParseException e) {
                throw new TemplateModelException("Failed to parse system property \"" + SYSTEM_PROPERTY_META_INF_TLD_SOURCES + "\"", e);
            }
        }
        List /*<Pattern>*/
        jettyTaglibJarPatterns = null;
        try {
            final String attrVal = (String) servletContext.getAttribute(ATTR_JETTY_CP_TAGLIB_JAR_PATTERNS);
            jettyTaglibJarPatterns = attrVal != null ? InitParamParser.parseCommaSeparatedPatterns(attrVal) : null;
        } catch (Exception e) {
            LOG.error("Failed to parse application context attribute \"" + ATTR_JETTY_CP_TAGLIB_JAR_PATTERNS + "\" - it will be ignored", e);
        }
        if (jettyTaglibJarPatterns != null) {
            for (Iterator /*<Pattern>*/
            it = jettyTaglibJarPatterns.iterator(); it.hasNext(); ) {
                Pattern pattern = (Pattern) it.next();
                mergedMetaInfTldSources.add(new ClasspathMetaInfTldSource(pattern));
            }
        }
        taglibFactory.setMetaInfTldSources(mergedMetaInfTldSources);
    }
    {
        List /*<String>*/
        mergedClassPathTlds = new ArrayList();
        if (classpathTlds != null) {
            mergedClassPathTlds.addAll(classpathTlds);
        }
        String sysPropVal = SecurityUtilities.getSystemProperty(SYSTEM_PROPERTY_CLASSPATH_TLDS, null);
        if (sysPropVal != null) {
            try {
                List /*<String>*/
                classpathTldsSysProp = InitParamParser.parseCommaSeparatedList(sysPropVal);
                if (classpathTldsSysProp != null) {
                    mergedClassPathTlds.addAll(classpathTldsSysProp);
                }
            } catch (ParseException e) {
                throw new TemplateModelException("Failed to parse system property \"" + SYSTEM_PROPERTY_CLASSPATH_TLDS + "\"", e);
            }
        }
        taglibFactory.setClasspathTlds(mergedClassPathTlds);
    }
    return taglibFactory;
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) Pattern(java.util.regex.Pattern) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) TaglibFactory(freemarker.ext.jsp.TaglibFactory) ArrayList(java.util.ArrayList) List(java.util.List) ParseException(java.text.ParseException) ServletException(javax.servlet.ServletException) TemplateModelException(freemarker.template.TemplateModelException) TemplateException(freemarker.template.TemplateException) TemplateNotFoundException(freemarker.template.TemplateNotFoundException) ParseException(java.text.ParseException) IOException(java.io.IOException) ClasspathMetaInfTldSource(freemarker.ext.jsp.TaglibFactory.ClasspathMetaInfTldSource)

Aggregations

TaglibFactory (freemarker.ext.jsp.TaglibFactory)8 ServletContextHashModel (freemarker.ext.servlet.ServletContextHashModel)5 HttpRequestHashModel (freemarker.ext.servlet.HttpRequestHashModel)4 HttpSession (javax.servlet.http.HttpSession)4 HttpSessionHashModel (freemarker.ext.servlet.HttpSessionHashModel)3 ServletException (javax.servlet.ServletException)3 AllHttpScopesHashModel (freemarker.ext.servlet.AllHttpScopesHashModel)2 TemplateModel (freemarker.template.TemplateModel)2 TemplateModelException (freemarker.template.TemplateModelException)2 IOException (java.io.IOException)2 GenericServlet (javax.servlet.GenericServlet)2 ServletContext (javax.servlet.ServletContext)2 ClasspathMetaInfTldSource (freemarker.ext.jsp.TaglibFactory.ClasspathMetaInfTldSource)1 HttpRequestParametersHashModel (freemarker.ext.servlet.HttpRequestParametersHashModel)1 TemplateException (freemarker.template.TemplateException)1 TemplateNotFoundException (freemarker.template.TemplateNotFoundException)1 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 List (java.util.List)1