mirror of
https://github.com/google/nomulus.git
synced 2025-07-04 18:23:26 +02:00
Refactor common email sending utility
The main thrust of this is to create a common POJO that contains email content in a simple way, then have one class that converts that to an email and sends it. Any class that uses email should only have to deal with creating that POJO. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=237883643
This commit is contained in:
parent
792cd21da7
commit
058c12c343
35 changed files with 773 additions and 614 deletions
|
@ -20,23 +20,17 @@ import static java.nio.charset.StandardCharsets.UTF_8;
|
|||
import com.google.appengine.tools.cloudstorage.GcsFilename;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.io.CharStreams;
|
||||
import com.google.common.net.MediaType;
|
||||
import google.registry.config.RegistryConfig.Config;
|
||||
import google.registry.gcs.GcsUtils;
|
||||
import google.registry.reporting.billing.BillingModule.InvoiceDirectoryPrefix;
|
||||
import google.registry.util.Retrier;
|
||||
import google.registry.util.EmailMessage;
|
||||
import google.registry.util.EmailMessage.Attachment;
|
||||
import google.registry.util.SendEmailService;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import javax.inject.Inject;
|
||||
import javax.mail.BodyPart;
|
||||
import javax.mail.Message;
|
||||
import javax.mail.Message.RecipientType;
|
||||
import javax.mail.MessagingException;
|
||||
import javax.mail.Multipart;
|
||||
import javax.mail.internet.InternetAddress;
|
||||
import javax.mail.internet.MimeBodyPart;
|
||||
import javax.mail.internet.MimeMultipart;
|
||||
import org.joda.time.YearMonth;
|
||||
|
||||
/** Utility functions for sending emails involving monthly invoices. */
|
||||
|
@ -44,27 +38,25 @@ class BillingEmailUtils {
|
|||
|
||||
private final SendEmailService emailService;
|
||||
private final YearMonth yearMonth;
|
||||
private final String outgoingEmailAddress;
|
||||
private final String alertRecipientAddress;
|
||||
private final ImmutableList<String> invoiceEmailRecipients;
|
||||
private final InternetAddress outgoingEmailAddress;
|
||||
private final InternetAddress alertRecipientAddress;
|
||||
private final ImmutableList<InternetAddress> invoiceEmailRecipients;
|
||||
private final String billingBucket;
|
||||
private final String invoiceFilePrefix;
|
||||
private final String invoiceDirectoryPrefix;
|
||||
private final GcsUtils gcsUtils;
|
||||
private final Retrier retrier;
|
||||
|
||||
@Inject
|
||||
BillingEmailUtils(
|
||||
SendEmailService emailService,
|
||||
YearMonth yearMonth,
|
||||
@Config("gSuiteOutgoingEmailAddress") String outgoingEmailAddress,
|
||||
@Config("alertRecipientEmailAddress") String alertRecipientAddress,
|
||||
@Config("invoiceEmailRecipients") ImmutableList<String> invoiceEmailRecipients,
|
||||
@Config("gSuiteOutgoingEmailAddress") InternetAddress outgoingEmailAddress,
|
||||
@Config("alertRecipientEmailAddress") InternetAddress alertRecipientAddress,
|
||||
@Config("invoiceEmailRecipients") ImmutableList<InternetAddress> invoiceEmailRecipients,
|
||||
@Config("billingBucket") String billingBucket,
|
||||
@Config("invoiceFilePrefix") String invoiceFilePrefix,
|
||||
@InvoiceDirectoryPrefix String invoiceDirectoryPrefix,
|
||||
GcsUtils gcsUtils,
|
||||
Retrier retrier) {
|
||||
GcsUtils gcsUtils) {
|
||||
this.emailService = emailService;
|
||||
this.yearMonth = yearMonth;
|
||||
this.outgoingEmailAddress = outgoingEmailAddress;
|
||||
|
@ -74,50 +66,34 @@ class BillingEmailUtils {
|
|||
this.invoiceFilePrefix = invoiceFilePrefix;
|
||||
this.invoiceDirectoryPrefix = invoiceDirectoryPrefix;
|
||||
this.gcsUtils = gcsUtils;
|
||||
this.retrier = retrier;
|
||||
}
|
||||
|
||||
/** Sends an e-mail to all expected recipients with an attached overall invoice from GCS. */
|
||||
void emailOverallInvoice() {
|
||||
try {
|
||||
retrier.callWithRetry(
|
||||
() -> {
|
||||
String invoiceFile =
|
||||
String.format("%s-%s.csv", invoiceFilePrefix, yearMonth);
|
||||
GcsFilename invoiceFilename =
|
||||
new GcsFilename(billingBucket, invoiceDirectoryPrefix + invoiceFile);
|
||||
try (InputStream in = gcsUtils.openInputStream(invoiceFilename)) {
|
||||
Message msg = emailService.createMessage();
|
||||
msg.setFrom(new InternetAddress(outgoingEmailAddress));
|
||||
for (String recipient : invoiceEmailRecipients) {
|
||||
msg.addRecipient(RecipientType.TO, new InternetAddress(recipient));
|
||||
}
|
||||
msg.setSubject(
|
||||
String.format("Domain Registry invoice data %s", yearMonth));
|
||||
Multipart multipart = new MimeMultipart();
|
||||
BodyPart textPart = new MimeBodyPart();
|
||||
textPart.setText(
|
||||
String.format(
|
||||
"Attached is the %s invoice for the domain registry.", yearMonth));
|
||||
multipart.addBodyPart(textPart);
|
||||
BodyPart invoicePart = new MimeBodyPart();
|
||||
String invoiceData = CharStreams.toString(new InputStreamReader(in, UTF_8));
|
||||
invoicePart.setContent(invoiceData, "text/csv; charset=utf-8");
|
||||
invoicePart.setFileName(invoiceFile);
|
||||
multipart.addBodyPart(invoicePart);
|
||||
msg.setContent(multipart);
|
||||
msg.saveChanges();
|
||||
emailService.sendMessage(msg);
|
||||
}
|
||||
},
|
||||
IOException.class,
|
||||
MessagingException.class);
|
||||
String invoiceFile = String.format("%s-%s.csv", invoiceFilePrefix, yearMonth);
|
||||
GcsFilename invoiceFilename =
|
||||
new GcsFilename(billingBucket, invoiceDirectoryPrefix + invoiceFile);
|
||||
try (InputStream in = gcsUtils.openInputStream(invoiceFilename)) {
|
||||
emailService.sendEmail(
|
||||
EmailMessage.newBuilder()
|
||||
.setSubject(String.format("Domain Registry invoice data %s", yearMonth))
|
||||
.setBody(
|
||||
String.format("Attached is the %s invoice for the domain registry.", yearMonth))
|
||||
.setFrom(outgoingEmailAddress)
|
||||
.setRecipients(invoiceEmailRecipients)
|
||||
.setAttachment(
|
||||
Attachment.newBuilder()
|
||||
.setContent(CharStreams.toString(new InputStreamReader(in, UTF_8)))
|
||||
.setContentType(MediaType.CSV_UTF_8)
|
||||
.setFilename(invoiceFile)
|
||||
.build())
|
||||
.build());
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
// Strip one layer, because callWithRetry wraps in a RuntimeException
|
||||
sendAlertEmail(
|
||||
String.format(
|
||||
"Emailing invoice failed due to %s",
|
||||
getRootCause(e).getMessage()));
|
||||
String.format("Emailing invoice failed due to %s", getRootCause(e).getMessage()));
|
||||
throw new RuntimeException("Emailing invoice failed", e);
|
||||
}
|
||||
}
|
||||
|
@ -125,17 +101,13 @@ class BillingEmailUtils {
|
|||
/** Sends an e-mail to the provided alert e-mail address indicating a billing failure. */
|
||||
void sendAlertEmail(String body) {
|
||||
try {
|
||||
retrier.callWithRetry(
|
||||
() -> {
|
||||
Message msg = emailService.createMessage();
|
||||
msg.setFrom(new InternetAddress(outgoingEmailAddress));
|
||||
msg.addRecipient(RecipientType.TO, new InternetAddress(alertRecipientAddress));
|
||||
msg.setSubject(String.format("Billing Pipeline Alert: %s", yearMonth));
|
||||
msg.setText(body);
|
||||
emailService.sendMessage(msg);
|
||||
return null;
|
||||
},
|
||||
MessagingException.class);
|
||||
emailService.sendEmail(
|
||||
EmailMessage.newBuilder()
|
||||
.setSubject(String.format("Billing Pipeline Alert: %s", yearMonth))
|
||||
.setBody(body)
|
||||
.addRecipient(alertRecipientAddress)
|
||||
.setFrom(outgoingEmailAddress)
|
||||
.build());
|
||||
} catch (Throwable e) {
|
||||
throw new RuntimeException("The alert e-mail system failed.", e);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue