DefaultSendGridV2Configurer.java

  1. package fr.sii.ogham.email.sendgrid.v2.builder.sendgrid;

  2. import static fr.sii.ogham.email.sendgrid.SendGridConstants.DEFAULT_SENDGRID_CONFIGURER_PRIORITY;

  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;

  5. import fr.sii.ogham.core.builder.MessagingBuilder;
  6. import fr.sii.ogham.core.builder.configurer.ConfigurerFor;
  7. import fr.sii.ogham.core.builder.configurer.MessagingConfigurer;
  8. import fr.sii.ogham.core.builder.context.BuildContext;
  9. import fr.sii.ogham.core.builder.mimetype.MimetypeDetectionBuilder;
  10. import fr.sii.ogham.core.util.ClasspathUtils;

  11. /**
  12.  * Default SendGrid configurer that is automatically applied every time a
  13.  * {@link MessagingBuilder} instance is created through
  14.  * {@link MessagingBuilder#standard()}.
  15.  *
  16.  * <p>
  17.  * The configurer has a priority of 30000 in order to be applied after
  18.  * templating configurers and JavaMail configurer.
  19.  * </p>
  20.  *
  21.  * This configurer is applied only if {@code com.sendgrid.SendGrid} is present
  22.  * in the classpath. If not present, SendGrid implementation is not registered
  23.  * at all.
  24.  *
  25.  * <p>
  26.  * This configurer inherits environment configuration (see
  27.  * {@link BuildContext}).
  28.  * </p>
  29.  * <p>
  30.  * This configurer inherits mimetype configuration (see
  31.  * {@link MimetypeDetectionBuilder} and
  32.  * {@link SendGridV2Builder#mimetype(MimetypeDetectionBuilder)}).
  33.  * </p>
  34.  *
  35.  * <p>
  36.  * This configurer applies the following configuration:
  37.  * <ul>
  38.  * <li>Configures authentication:
  39.  * <ul>
  40.  * <li>Either by providing an <a href=
  41.  * "https://sendgrid.com/docs/Classroom/Send/How_Emails_Are_Sent/api_keys.html">API
  42.  * key</a>: using the property "ogham.email.sendgrid.api-key"</li>
  43.  * <li>Or using username/password: using the properties
  44.  * "ogham.email.sendgrid.username" and "ogham.email.sendgrid.password"</li>
  45.  * </ul>
  46.  * </li>
  47.  * </ul>
  48.  *
  49.  * @author AurĂ©lien Baudet
  50.  *
  51.  */
  52. public final class DefaultSendGridV2Configurer {
  53.     private static final Logger LOG = LoggerFactory.getLogger(DefaultSendGridV2Configurer.class);

  54.     @ConfigurerFor(targetedBuilder = "standard", priority = DEFAULT_SENDGRID_CONFIGURER_PRIORITY)
  55.     public static class SendGridV2Configurer implements MessagingConfigurer {
  56.         @Override
  57.         public void configure(MessagingBuilder msgBuilder) {
  58.             if (!canUseSendGrid()) {
  59.                 LOG.debug("[{}] skip configuration", this);
  60.                 return;
  61.             }
  62.             LOG.debug("[{}] apply configuration", this);
  63.             // @formatter:off
  64.             SendGridV2Builder builder = msgBuilder.email().sender(SendGridV2Builder.class);
  65.             // inherit mimetype configuration as parent builder
  66.             builder.mimetype(msgBuilder.mimetype());
  67.             builder
  68.                 .apiKey().properties("${ogham.email.sendgrid.api-key}").and()
  69.                 .username().properties("${ogham.email.sendgrid.username}").and()
  70.                 .password().properties("${ogham.email.sendgrid.password}").and()
  71.                 .url().properties("${ogham.email.sendgrid.url}");
  72.             // @formatter:on
  73.         }

  74.         private static boolean canUseSendGrid() {
  75.             return ClasspathUtils.exists("com.sendgrid.SendGrid") && ClasspathUtils.exists("com.sendgrid.SendGrid$Email");
  76.         }
  77.     }

  78.     private DefaultSendGridV2Configurer() {
  79.         super();
  80.     }
  81. }