Search in sources :

Example 16 with SimpleHash

use of freemarker.template.SimpleHash in project spring-framework by spring-projects.

the class FreeMarkerView method renderInternal.

@Override
protected Mono<Void> renderInternal(Map<String, Object> renderAttributes, MediaType contentType, ServerWebExchange exchange) {
    // Expose all standard FreeMarker hash models.
    SimpleHash freeMarkerModel = getTemplateModel(renderAttributes, exchange);
    if (logger.isDebugEnabled()) {
        logger.debug("Rendering FreeMarker template [" + getUrl() + "].");
    }
    Locale acceptLocale = exchange.getRequest().getHeaders().getAcceptLanguageAsLocale();
    Locale locale = acceptLocale != null ? acceptLocale : Locale.getDefault();
    DataBuffer dataBuffer = exchange.getResponse().bufferFactory().allocateBuffer();
    try {
        Charset charset = getCharset(contentType).orElse(getDefaultCharset());
        Writer writer = new OutputStreamWriter(dataBuffer.asOutputStream(), charset);
        getTemplate(locale).process(freeMarkerModel, writer);
    } catch (IOException ex) {
        String message = "Could not load FreeMarker template for URL [" + getUrl() + "]";
        return Mono.error(new IllegalStateException(message, ex));
    } catch (Throwable ex) {
        return Mono.error(ex);
    }
    return exchange.getResponse().writeWith(Flux.just(dataBuffer));
}
Also used : Locale(java.util.Locale) SimpleHash(freemarker.template.SimpleHash) Charset(java.nio.charset.Charset) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Example 17 with SimpleHash

use of freemarker.template.SimpleHash in project engine by craftercms.

the class CrafterFreeMarkerView method buildTemplateModel.

@Override
protected SimpleHash buildTemplateModel(final Map<String, Object> model, final HttpServletRequest request, final HttpServletResponse response) {
    AllHttpScopesAndAppContextHashModel templateModel = new AllHttpScopesAndAppContextHashModel(getObjectWrapper(), applicationContextAccessor, getServletContext(), request);
    HttpSessionHashModel sessionModel = createSessionModel(request, response);
    HttpRequestHashModel requestModel = new HttpRequestHashModel(request, response, getObjectWrapper());
    HttpRequestParametersHashModel requestParamsModel = new HttpRequestParametersHashModel(request);
    Map<String, String> cookies = createCookieMap(request);
    templateModel.put(KEY_APPLICATION_CAP, servletContextHashModel);
    templateModel.put(KEY_APPLICATION, servletContextHashModel);
    templateModel.put(KEY_SESSION_CAP, sessionModel);
    templateModel.put(KEY_SESSION, sessionModel);
    templateModel.put(KEY_REQUEST_CAP, requestModel);
    templateModel.put(KEY_REQUEST, requestModel);
    templateModel.put(KEY_REQUEST_PARAMS_CAP, requestParamsModel);
    templateModel.put(KEY_REQUEST_PARAMS, requestParamsModel);
    templateModel.put(KEY_APP_CONTEXT_CAP, applicationContextAccessor);
    templateModel.put(KEY_APP_CONTEXT, applicationContextAccessor);
    templateModel.put(KEY_COOKIES_CAP, cookies);
    templateModel.put(KEY_COOKIES, cookies);
    Authentication auth = SecurityUtils.getAuthentication(request);
    if (auth != null) {
        templateModel.put(KEY_AUTH_CAP, auth);
        templateModel.put(KEY_AUTH, auth);
        templateModel.put(KEY_PROFILE_CAP, auth.getProfile());
        templateModel.put(KEY_PROFILE, auth.getProfile());
    }
    SiteContext siteContext = SiteContext.getCurrent();
    Configuration siteConfig = siteContext.getConfig();
    Locale locale = LocaleContextHolder.getLocale();
    TemplateHashModel staticModels = BeansWrapper.getDefaultInstance().getStaticModels();
    TemplateHashModel enumModels = BeansWrapper.getDefaultInstance().getEnumModels();
    templateModel.put(KEY_STATICS_CAP, staticModels);
    templateModel.put(KEY_STATICS, staticModels);
    templateModel.put(KEY_ENUMS_CAP, enumModels);
    templateModel.put(KEY_ENUMS, enumModels);
    templateModel.put(KEY_SITE_CONTEXT_CAP, siteContext);
    templateModel.put(KEY_SITE_CONTEXT, siteContext);
    templateModel.put(KEY_LOCALE_CAP, locale);
    templateModel.put(KEY_LOCALE, locale);
    if (siteConfig != null) {
        templateModel.put(KEY_SITE_CONFIG, siteConfig);
        templateModel.put(KEY_SITE_CONFIG_CAP, siteConfig);
    }
    templateModel.putAll(model);
    ObjectFactory<SimpleHash> componentModelFactory = new ObjectFactory<SimpleHash>() {

        public SimpleHash getObject() {
            return buildTemplateModel(model, request, response);
        }
    };
    RenderComponentDirective renderComponentDirective = new RenderComponentDirective();
    renderComponentDirective.setSiteItemService(siteItemService);
    renderComponentDirective.setModelFactory(componentModelFactory);
    renderComponentDirective.setTemplateXPathQuery(componentTemplateXPathQuery);
    renderComponentDirective.setTemplateNamePrefix(componentTemplateNamePrefix);
    renderComponentDirective.setTemplateNameSuffix(componentTemplateNameSuffix);
    renderComponentDirective.setIncludeElementName(componentIncludeElementName);
    renderComponentDirective.setScriptResolver(componentScriptResolver);
    renderComponentDirective.setServletContext(getServletContext());
    ExecuteControllerDirective executeControllerDirective = new ExecuteControllerDirective();
    executeControllerDirective.setServletContext(getServletContext());
    templateModel.put(RENDER_COMPONENT_DIRECTIVE_NAME, renderComponentDirective);
    templateModel.put(EXECUTE_CONTROLLER_DIRECTIVE_NAME, executeControllerDirective);
    return templateModel;
}
Also used : Locale(java.util.Locale) HttpRequestHashModel(org.craftercms.engine.util.freemarker.HttpRequestHashModel) Configuration(org.apache.commons.configuration.Configuration) SiteContext(org.craftercms.engine.service.context.SiteContext) ExecuteControllerDirective(org.craftercms.engine.freemarker.ExecuteControllerDirective) RenderComponentDirective(org.craftercms.engine.freemarker.RenderComponentDirective) HttpSessionHashModel(freemarker.ext.servlet.HttpSessionHashModel) TemplateHashModel(freemarker.template.TemplateHashModel) ObjectFactory(org.springframework.beans.factory.ObjectFactory) Authentication(org.craftercms.security.authentication.Authentication) SimpleHash(freemarker.template.SimpleHash) HttpRequestParametersHashModel(freemarker.ext.servlet.HttpRequestParametersHashModel)

Example 18 with SimpleHash

use of freemarker.template.SimpleHash in project engine by craftercms.

the class RenderComponentDirective method execute.

@SuppressWarnings("unchecked")
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException {
    TemplateModel componentParam = (TemplateModel) params.get(COMPONENT_PARAM_NAME);
    TemplateModel componentPathParam = (TemplateModel) params.get(COMPONENT_PATH_PARAM_NAME);
    TemplateModel additionalModelParam = (TemplateModel) params.get(ADDITIONAL_MODEL_PARAM_NAME);
    Map<String, Object> additionalModel = null;
    SiteItem component;
    if (componentParam == null && componentPathParam == null) {
        throw new TemplateException("No '" + COMPONENT_PARAM_NAME + "' or '" + COMPONENT_PATH_PARAM_NAME + "' param specified", env);
    } else if (componentParam != null) {
        component = getComponentFromNode(componentParam, env);
    } else {
        component = getComponentFromPath(componentPathParam, env);
    }
    if (additionalModelParam != null) {
        additionalModel = unwrap(ADDITIONAL_MODEL_PARAM_NAME, additionalModelParam, Map.class, env);
    }
    Map<String, Object> templateModel = executeScripts(component, additionalModel, env);
    SimpleHash model = getFullModel(component, templateModel, additionalModel);
    Template template = getTemplate(component, env);
    Writer output = env.getOut();
    processComponentTemplate(template, model, output, env);
}
Also used : TemplateException(freemarker.template.TemplateException) SimpleHash(freemarker.template.SimpleHash) TemplateModel(freemarker.template.TemplateModel) HashMap(java.util.HashMap) Map(java.util.Map) SiteItem(org.craftercms.engine.model.SiteItem) Writer(java.io.Writer) Template(freemarker.template.Template)

Aggregations

SimpleHash (freemarker.template.SimpleHash)18 Template (freemarker.template.Template)5 IOException (java.io.IOException)3 OutputStreamWriter (java.io.OutputStreamWriter)3 Writer (java.io.Writer)3 Locale (java.util.Locale)3 BufferedWriter (java.io.BufferedWriter)2 StringWriter (java.io.StringWriter)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 ServletException (javax.servlet.ServletException)2 JForumContext (net.jforum.context.JForumContext)2 RequestContext (net.jforum.context.RequestContext)2 ResponseContext (net.jforum.context.ResponseContext)2 WebRequestContext (net.jforum.context.web.WebRequestContext)2 WebResponseContext (net.jforum.context.web.WebResponseContext)2 ExceptionWriter (net.jforum.exceptions.ExceptionWriter)2