SmsRunner.java

  1. package fr.sii.ogham.runtime.runner;

  2. import fr.sii.ogham.core.exception.MessagingException;
  3. import fr.sii.ogham.core.service.MessagingService;
  4. import fr.sii.ogham.core.util.ClasspathUtils;
  5. import fr.sii.ogham.sms.message.Sms;
  6. import mock.context.SimpleBean;

  7. public class SmsRunner {
  8.     private final MessagingService messagingService;
  9.    
  10.     public SmsRunner(MessagingService messagingService) {
  11.         super();
  12.         this.messagingService = messagingService;
  13.     }

  14.     public void sendSmsWithoutTemplate() throws MessagingException {
  15.         messagingService.send(new Sms()
  16.                 .message().string("Hello world !!")
  17.                 .from("+33601020304")
  18.                 .to("0709080706"));
  19.     }

  20.     public void sendSmsWithThymeleaf() throws MessagingException {
  21.         messagingService.send(new Sms()
  22.                 .message().template("classpath:/sms/thymeleaf/source/simple-"+detectThymeleafEngineVersion()+".txt", new SimpleBean("foo", 42))
  23.                 .from("+33601020304")
  24.                 .to("0709080706"));
  25.     }

  26.     public void sendSmsWithFreemarker() throws MessagingException {
  27.         messagingService.send(new Sms()
  28.                 .message().template("classpath:/sms/freemarker/source/simple.txt.ftl", new SimpleBean("foo", 42))
  29.                 .from("+33601020304")
  30.                 .to("0709080706"));
  31.     }

  32.     private String detectThymeleafEngineVersion() {
  33.         if (ClasspathUtils.exists("org.thymeleaf.TemplateEngine") && ClasspathUtils.exists("org.thymeleaf.IEngineConfiguration")) {
  34.             return "v3";
  35.         }
  36.         if (ClasspathUtils.exists("org.thymeleaf.TemplateEngine") && !ClasspathUtils.exists("org.thymeleaf.IEngineConfiguration")) {
  37.             return "v2";
  38.         }
  39.         throw new IllegalStateException("Unknown Thymeleaf engine version");
  40.     }
  41. }