Java Code Examples for de.neuland.jade4j.Jade4J#render()

The following examples show how to use de.neuland.jade4j.Jade4J#render() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SmtpMailer.java    From Singularity with Apache License 2.0 5 votes vote down vote up
private void prepareDisasterMail(final SingularityDisastersData disastersData) {
  final List<SingularityEmailDestination> emailDestination = configuration
    .getSmtpConfigurationOptional()
    .get()
    .getEmailConfiguration()
    .get(SingularityEmailType.DISASTER_DETECTED);
  if (
    emailDestination.isEmpty() ||
    !emailDestination.contains(SingularityEmailDestination.ADMINS) ||
    smtpConfiguration.getAdmins().isEmpty()
  ) {
    LOG.info("Not configured to send disaster detected mail");
    return;
  }

  final List<String> toList = smtpConfiguration.getAdmins();
  final List<String> ccList = Lists.newArrayList();
  final String subject = String.format(
    "Disaster(s) Detected %s",
    disastersData.getDisasters()
  );

  final Map<String, Object> templateProperties = Maps.newHashMap();

  templateProperties.put("disasterTypes", disastersData.getDisasters());
  templateProperties.put(
    "stats",
    mailTemplateHelpers.getJadeDisasterStats(disastersData.getStats())
  );

  final String body = Jade4J.render(disastersTemplate, templateProperties);

  smtpSender.queueMail(toList, ccList, subject, body);
}
 
Example 2
Source File: SmtpMailer.java    From Singularity with Apache License 2.0 4 votes vote down vote up
private void prepareTaskMail(
  Optional<SingularityTask> task,
  SingularityTaskId taskId,
  SingularityRequest request,
  SingularityEmailType emailType,
  Map<String, Object> extraProperties,
  Collection<SingularityTaskHistoryUpdate> taskHistory,
  ExtendedTaskState taskState,
  List<SingularityTaskMetadata> taskMetadata
) {
  final Collection<SingularityEmailDestination> emailDestination = getDestination(
    request,
    emailType
  );

  final Map<String, Object> templateProperties = Maps.newHashMap();
  populateRequestEmailProperties(templateProperties, request, emailType);
  populateTaskEmailProperties(
    templateProperties,
    taskId,
    taskHistory,
    taskState,
    taskMetadata,
    emailType
  );
  templateProperties.putAll(extraProperties);

  final String subject = mailTemplateHelpers.getSubjectForTaskHistory(
    taskId,
    taskState,
    emailType,
    taskHistory
  );

  final String adminEmails = adminJoiner.join(smtpConfiguration.getAdmins());
  templateProperties.put("adminEmails", adminEmails);

  final String body = Jade4J.render(taskTemplate, templateProperties);

  final Optional<String> user = task.isPresent()
    ? task.get().getTaskRequest().getPendingTask().getUser()
    : Optional.<String>empty();

  queueMail(emailDestination, request, emailType, user, subject, body);
}
 
Example 3
Source File: SmtpMailer.java    From Singularity with Apache License 2.0 4 votes vote down vote up
private void prepareRequestMail(
  SingularityRequest request,
  RequestMailType type,
  Optional<String> user,
  Optional<String> message,
  Optional<Map<String, Object>> additionalProperties
) {
  final List<SingularityEmailDestination> emailDestination = getDestination(
    request,
    type.getEmailType()
  );

  if (emailDestination.isEmpty()) {
    LOG.debug("Not configured to send request mail for {}", request);
    return;
  }

  final String subject = String.format(
    "Request %s has been %s — Singularity",
    request.getId(),
    type.name().toLowerCase()
  );
  final Map<String, Object> templateProperties = Maps.newHashMap();
  populateRequestEmailProperties(templateProperties, request, type.getEmailType());

  templateProperties.put("expiring", Boolean.FALSE);
  templateProperties.put("requestPaused", type == RequestMailType.PAUSED);
  templateProperties.put("requestUnpaused", type == RequestMailType.UNPAUSED);
  templateProperties.put("requestScaled", type == RequestMailType.SCALED);
  templateProperties.put("action", type.name().toLowerCase());
  templateProperties.put("hasUser", user.isPresent());
  templateProperties.put("hasMessage", message.isPresent());

  if (user.isPresent()) {
    templateProperties.put("user", user.get());
  }

  if (message.isPresent()) {
    templateProperties.put("message", message.get());
  }

  if (additionalProperties.isPresent()) {
    templateProperties.putAll(additionalProperties.get());
  }

  final String body = Jade4J.render(requestModifiedTemplate, templateProperties);

  queueMail(emailDestination, request, type.getEmailType(), user, subject, body);
}
 
Example 4
Source File: SmtpMailer.java    From Singularity with Apache License 2.0 4 votes vote down vote up
private void prepareRequestInCooldownMail(SingularityRequest request) {
  final List<SingularityEmailDestination> emailDestination = getDestination(
    request,
    SingularityEmailType.REQUEST_IN_COOLDOWN
  );

  if (emailDestination.isEmpty()) {
    LOG.debug("Not configured to send request cooldown mail for {}", request);
    return;
  }

  final Map<String, Object> templateProperties = Maps.newHashMap();
  populateRequestEmailProperties(
    templateProperties,
    request,
    SingularityEmailType.REQUEST_IN_COOLDOWN
  );

  final String subject = String.format(
    "Request %s has entered system cooldown — Singularity",
    request.getId()
  );

  templateProperties.put(
    "cooldownDelayFormat",
    DurationFormatUtils.formatDurationHMS(
      TimeUnit.SECONDS.toMillis(configuration.getCooldownMinScheduleSeconds())
    )
  );

  final String body = Jade4J.render(requestInCooldownTemplate, templateProperties);

  queueMail(
    emailDestination,
    request,
    SingularityEmailType.REQUEST_IN_COOLDOWN,
    Optional.<String>empty(),
    subject,
    body
  );
}
 
Example 5
Source File: SmtpMailer.java    From Singularity with Apache License 2.0 4 votes vote down vote up
/**
 * Check to see if email should be rate limited, and if so, send a rate limit
 * email notification. Next attempt to email will immediately return.
 *
 * @param destination collection of enum values used to specify who will receive this email.
 * @param request SingularityRequest this email is about.
 * @param emailType what the email is about (e.g. TASK_FAILED).
 * @param actionTaker the user taking the action
 * @param subject the subject line of the email.
 * @param body the body of the email.
 */
private void queueMail(
  final Collection<SingularityEmailDestination> destination,
  final SingularityRequest request,
  final SingularityEmailType emailType,
  final Optional<String> actionTaker,
  String subject,
  String body
) {
  RateLimitResult result = checkRateLimitForMail(request, emailType);

  if (result == RateLimitResult.DONT_SEND_MAIL_IN_COOLDOWN) {
    return;
  }

  if (result == RateLimitResult.SEND_COOLDOWN_STARTED_MAIL) {
    subject =
      String.format(
        "%s notifications for %s are being rate limited",
        emailType.name(),
        request.getId()
      );
    body =
      Jade4J.render(
        rateLimitedTemplate,
        getRateLimitTemplateProperties(request, emailType)
      );
  }

  final Set<String> toList = new HashSet<>();
  final Set<String> ccList = new HashSet<>();

  // Decide where to send this email.
  if (
    destination.contains(SingularityEmailDestination.OWNERS) &&
    request.getOwners().isPresent() &&
    !request.getOwners().get().isEmpty()
  ) {
    toList.addAll(request.getOwners().get());
  }

  if (
    destination.contains(SingularityEmailDestination.ADMINS) &&
    !smtpConfiguration.getAdmins().isEmpty()
  ) {
    if (toList.isEmpty()) {
      toList.addAll(smtpConfiguration.getAdmins());
    } else {
      ccList.addAll(smtpConfiguration.getAdmins());
    }
  }

  if (actionTaker.isPresent() && !Strings.isNullOrEmpty(actionTaker.get())) {
    if (destination.contains(SingularityEmailDestination.ACTION_TAKER)) {
      toList.add(actionTaker.get());
    } else {
      final Iterator<String> i = toList.iterator();
      while (i.hasNext()) {
        if (actionTaker.get().equalsIgnoreCase(i.next())) {
          i.remove();
        }
      }
    }
  }

  Set<String> emailBlacklist = Sets.newHashSet(notificationsManager.getBlacklist());
  toList.removeAll(emailBlacklist);
  ccList.removeAll(emailBlacklist);

  smtpSender.queueMail(
    Lists.newArrayList(toList),
    Lists.newArrayList(ccList),
    subject,
    body
  );
}