ThymeleafV3EmailBuilder.java

  1. package fr.sii.ogham.template.thymeleaf.v3.buider;

  2. import org.thymeleaf.TemplateEngine;
  3. import org.thymeleaf.templateresolver.ITemplateResolver;

  4. import fr.sii.ogham.core.builder.context.BuildContext;
  5. import fr.sii.ogham.core.builder.context.DefaultBuildContext;
  6. import fr.sii.ogham.core.builder.env.EnvironmentBuilder;
  7. import fr.sii.ogham.core.template.detector.TemplateEngineDetector;
  8. import fr.sii.ogham.email.builder.EmailBuilder;
  9. import fr.sii.ogham.template.thymeleaf.common.adapter.ClassPathResolverAdapter;
  10. import fr.sii.ogham.template.thymeleaf.common.adapter.FileResolverAdapter;
  11. import fr.sii.ogham.template.thymeleaf.common.adapter.FirstSupportingResolverAdapter;
  12. import fr.sii.ogham.template.thymeleaf.common.adapter.TemplateResolverAdapter;
  13. import fr.sii.ogham.template.thymeleaf.common.buider.AbstractThymeleafMultiContentBuilder;
  14. import fr.sii.ogham.template.thymeleaf.v3.ThymeLeafV3FirstSupportingTemplateResolver;
  15. import fr.sii.ogham.template.thymeleaf.v3.ThymeleafV3TemplateDetector;
  16. import fr.sii.ogham.template.thymeleaf.v3.adapter.StringResolverAdapter;
  17. import fr.sii.ogham.template.thymeleaf.v3.adapter.ThymeleafV3TemplateOptionsApplier;

  18. /**
  19.  * Configures parsing of templates using Thymeleaf.
  20.  *
  21.  * Specific resource resolution can be configured to use template prefix/suffix
  22.  * paths:
  23.  *
  24.  * <pre>
  25.  * <code>
  26.  * .classpath()
  27.  *   .pathPrefix("email/")
  28.  *   .pathSuffix(".html")
  29.  *   .and()
  30.  * .file()
  31.  *   .pathPrefix("/data/myapplication/templates/email")
  32.  *   .pathSuffix(".html")
  33.  * </code>
  34.  * </pre>
  35.  *
  36.  * You can customize default Thymeleaf {@link TemplateEngine}:
  37.  *
  38.  * <pre>
  39.  * <code>
  40.  * .engine()
  41.  *   .addDialect("foo", myDialect)
  42.  *   .addMessageResolver(myMessageResolver)
  43.  * </code>
  44.  * </pre>
  45.  *
  46.  * Or you can use a particular Thymeleaf {@link TemplateEngine}:
  47.  *
  48.  * <pre>
  49.  * <code>
  50.  * .engine(new MyTemplateEngine())
  51.  * </code>
  52.  * </pre>
  53.  *
  54.  *
  55.  * Email protocol supports several contents (main and alternative). The main
  56.  * content is often an HTML email to display a beautiful email to users. The
  57.  * alternative content is often a textual fallback (when email client can't
  58.  * display HTML version like mobile phones that tries to display a summary of
  59.  * the email). You can configure which file extensions are supported by
  60.  * Thymeleaf to automatically load variants (HTML: main, TEXT: alternative):
  61.  *
  62.  * <pre>
  63.  * <code>
  64.  * .variant(EmailVariant.HTML, "html")
  65.  * .variant(EmailVariant.TEXT, "txt")
  66.  * </code>
  67.  * </pre>
  68.  *
  69.  * Thanks to that configuration, you can send an email without specifying the
  70.  * extension:
  71.  *
  72.  * <pre>
  73.  * <code>
  74.  * service.send(new Email()
  75.  *   .content(new MultiTemplateContent("email/sample", new SampleBean("foo", 42)))
  76.  *   .to("foo.bar@sii.fr"))
  77.  * </code>
  78.  * </pre>
  79.  *
  80.  * Ogham will then be able to detect which files exist and choose the right
  81.  * behavior:
  82.  * <ul>
  83.  * <li>If you provide an ".html" file (either in classpath or on filesytem), the
  84.  * HTML template is used as main content</li>
  85.  * <li>If you provide an ".txt" file (either in classpath or on filesytem), the
  86.  * text template is used as main content</li>
  87.  * <li>If you provide both files, the HTML template is used as main content and
  88.  * text template as alternative</li>
  89.  * </ul>
  90.  *
  91.  *
  92.  * @author AurĂ©lien Baudet
  93.  *
  94.  */
  95. public class ThymeleafV3EmailBuilder extends AbstractThymeleafMultiContentBuilder<ThymeleafV3EmailBuilder, EmailBuilder, ThymeleafV3EngineConfigBuilder<ThymeleafV3EmailBuilder>> {
  96.     /**
  97.      * Default constructor when using Thymeleaf without all Ogham work.
  98.      *
  99.      * <strong>WARNING: use is only if you know what you are doing !</strong>
  100.      */
  101.     public ThymeleafV3EmailBuilder() {
  102.         super(ThymeleafV3EmailBuilder.class, null, new DefaultBuildContext());
  103.     }

  104.     /**
  105.      * Initializes the builder with a parent builder. The parent builder is used
  106.      * when calling {@link #and()} method. The {@link EnvironmentBuilder} is
  107.      * used to evaluate properties when {@link #build()} method is called.
  108.      *
  109.      * @param parent
  110.      *            the parent builder
  111.      * @param buildContext
  112.      *            for registering instances and property evaluation
  113.      */
  114.     public ThymeleafV3EmailBuilder(EmailBuilder parent, BuildContext buildContext) {
  115.         super(ThymeleafV3EmailBuilder.class, parent, buildContext);
  116.     }

  117.     @Override
  118.     protected TemplateEngineDetector createTemplateDetector() {
  119.         return buildContext.register(new ThymeleafV3TemplateDetector(buildResolver()));
  120.     }

  121.     @Override
  122.     protected ITemplateResolver buildTemplateResolver(TemplateEngine builtEngine) {
  123.         return buildContext.register(new ThymeLeafV3FirstSupportingTemplateResolver(buildResolver(), buildAdapters()));
  124.     }

  125.     @Override
  126.     protected ThymeleafV3EngineConfigBuilder<ThymeleafV3EmailBuilder> getThymeleafEngineConfigBuilder() {
  127.         return buildContext.register(new ThymeleafV3EngineConfigBuilder<>(myself, buildContext));
  128.     }

  129.     @Override
  130.     protected FirstSupportingResolverAdapter buildAdapters() {
  131.         FirstSupportingResolverAdapter adapter = buildContext.register(new FirstSupportingResolverAdapter());
  132.         for (TemplateResolverAdapter custom : customAdapters) {
  133.             adapter.addAdapter(custom);
  134.         }
  135.         ThymeleafV3TemplateOptionsApplier applier = buildContext.register(new ThymeleafV3TemplateOptionsApplier());
  136.         adapter.addAdapter(buildContext.register(new ClassPathResolverAdapter(applier)));
  137.         adapter.addAdapter(buildContext.register(new FileResolverAdapter(applier)));
  138.         adapter.addAdapter(buildContext.register(new StringResolverAdapter(applier)));
  139.         adapter.setOptions(buildTemplateResolverOptions());
  140.         return adapter;
  141.     }
  142. }