All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.peterphi.std.guice.web.rest.templating.thymeleaf.ThymeleafTemplater Maven / Gradle / Ivy

There is a newer version: 10.1.5
Show newest version
package com.peterphi.std.guice.web.rest.templating.thymeleaf;

import com.codahale.metrics.Meter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import com.peterphi.std.guice.common.auth.iface.CurrentUser;
import com.peterphi.std.guice.common.metrics.GuiceMetricNames;
import com.peterphi.std.guice.common.serviceprops.composite.GuiceConfig;
import com.peterphi.std.guice.web.HttpCallContext;
import com.peterphi.std.guice.web.rest.templating.Templater;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.context.IContext;
import org.thymeleaf.context.WebContext;

import java.util.HashMap;
import java.util.Map;

/**
 * Light abstraction over a ThymeLeaf TemplateEngine allowing cleaner construction of the current web context (when the template
 * engine is being used inside an http call).
* Exposes the following special variables: *
  • currentUser - of type {@link ThymeleafCurrentUserUtils}
  • *
    • config - of type {@link GuiceConfig}
    • *
    */ @Singleton public class ThymeleafTemplater implements Templater { private final TemplateEngine engine; private final GuiceConfig configuration; private Map data = new HashMap(); private final Timer calls; private final Meter failures; @Inject public ThymeleafTemplater(final TemplateEngine engine, final GuiceConfig configuration, MetricRegistry metrics, Provider userProvider) { if (engine.getTemplateResolvers().isEmpty()) { throw new IllegalArgumentException("No Template Resolvers have been configured for thymeleaf (missing import?)"); } this.engine = engine; this.configuration = configuration; this.calls = metrics.timer(GuiceMetricNames.THYMELEAF_CALL_TIMER); this.failures = metrics.meter(GuiceMetricNames.THYMELEAF_RENDER_EXCEPTION_METER); data.put("currentUser", new ThymeleafCurrentUserUtils(userProvider)); } public ThymeleafTemplater set(String key, Object value) { data.put(key, value); return this; } public ThymeleafCall template(final String name) { final IContext ctx = makeContext(); // Expose the service configuration ctx.getVariables().put("config", configuration); ctx.getVariables().putAll(data); return new ThymeleafCall(engine, ctx, name, calls, failures); } /** * Build a new IContext (exposing the HttpCallContext, where possible) * * @return */ private IContext makeContext() { final HttpCallContext http = HttpCallContext.peek(); if (http != null) { return new WebContext(http.getRequest(), http.getResponse(), http.getServletContext(), http.getRequest().getLocale()); } else { return new Context(); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy