From afaab55bf4f309382b55793db44033233dcfb64d Mon Sep 17 00:00:00 2001 From: James Moger Date: Tue, 26 Nov 2013 16:26:44 -0500 Subject: [PATCH] Support customizing the "from" display name for generated emails Change-Id: Ibad9e2b1c12a24ad9c671a0d96aafb3365daa529 --- .../java/com/gitblit/FederationClient.java | 8 +++++ .../gitblit/authority/GitblitAuthority.java | 3 +- .../com/gitblit/manager/GitblitManager.java | 10 ++++++ .../gitblit/manager/INotificationManager.java | 20 ++++++++++++ .../gitblit/manager/NotificationManager.java | 32 +++++++++++++++++-- .../java/com/gitblit/service/MailService.java | 27 ++++++++++++++-- 6 files changed, 93 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/gitblit/FederationClient.java b/src/main/java/com/gitblit/FederationClient.java index c2757463..4f4b00bc 100644 --- a/src/main/java/com/gitblit/FederationClient.java +++ b/src/main/java/com/gitblit/FederationClient.java @@ -188,5 +188,13 @@ public class FederationClient { @Override public void sendHtmlMail(String subject, String message, String... toAddresses) { } + + @Override + public void sendHtmlMail(String from, String subject, String message, Collection toAddresses) { + } + + @Override + public void sendHtmlMail(String from, String subject, String message, String... toAddresses) { + } } } diff --git a/src/main/java/com/gitblit/authority/GitblitAuthority.java b/src/main/java/com/gitblit/authority/GitblitAuthority.java index 36c016de..51626e64 100644 --- a/src/main/java/com/gitblit/authority/GitblitAuthority.java +++ b/src/main/java/com/gitblit/authority/GitblitAuthority.java @@ -43,6 +43,7 @@ import java.security.cert.CertificateFactory; import java.security.cert.X509Certificate; import java.text.MessageFormat; import java.util.ArrayList; +import java.util.Arrays; import java.util.Calendar; import java.util.Collections; import java.util.Date; @@ -853,7 +854,7 @@ public class GitblitAuthority extends JFrame implements X509Log { // send email try { if (mail.isReady()) { - Message message = mail.createMessage(user.emailAddress); + Message message = mail.createMessage(Arrays.asList(user.emailAddress)); message.setSubject("Your Gitblit client certificate for " + metadata.serverHostname); // body of email diff --git a/src/main/java/com/gitblit/manager/GitblitManager.java b/src/main/java/com/gitblit/manager/GitblitManager.java index b3911814..75b258a5 100644 --- a/src/main/java/com/gitblit/manager/GitblitManager.java +++ b/src/main/java/com/gitblit/manager/GitblitManager.java @@ -593,6 +593,16 @@ public class GitblitManager implements IGitblit { notificationManager.sendHtmlMail(subject, message, toAddresses); } + @Override + public void sendHtmlMail(String from, String subject, String message, Collection toAddresses) { + notificationManager.sendHtmlMail(from, subject, message, toAddresses); + } + + @Override + public void sendHtmlMail(String from, String subject, String message, String... toAddresses) { + notificationManager.sendHtmlMail(from, subject, message, toAddresses); + } + /* * SESSION MANAGER */ diff --git a/src/main/java/com/gitblit/manager/INotificationManager.java b/src/main/java/com/gitblit/manager/INotificationManager.java index 29d8f549..ce5d3f71 100644 --- a/src/main/java/com/gitblit/manager/INotificationManager.java +++ b/src/main/java/com/gitblit/manager/INotificationManager.java @@ -63,4 +63,24 @@ public interface INotificationManager extends IManager { */ void sendHtmlMail(String subject, String message, String... toAddresses); + /** + * Notify users by email of something. + * + * @param from + * @param subject + * @param message + * @param toAddresses + */ + void sendHtmlMail(String from, String subject, String message, Collection toAddresses); + + /** + * Notify users by email of something. + * + * @param from + * @param subject + * @param message + * @param toAddresses + */ + void sendHtmlMail(String from, String subject, String message, String... toAddresses); + } \ No newline at end of file diff --git a/src/main/java/com/gitblit/manager/NotificationManager.java b/src/main/java/com/gitblit/manager/NotificationManager.java index a226d1a0..22ae5517 100644 --- a/src/main/java/com/gitblit/manager/NotificationManager.java +++ b/src/main/java/com/gitblit/manager/NotificationManager.java @@ -142,7 +142,20 @@ public class NotificationManager implements INotificationManager { */ @Override public void sendHtmlMail(String subject, String message, Collection toAddresses) { - this.sendHtmlMail(subject, message, toAddresses.toArray(new String[0])); + this.sendHtmlMail(null, subject, message, toAddresses.toArray(new String[0])); + } + + /** + * Notify users by email of something. + * + * @param from + * @param subject + * @param message + * @param toAddresses + */ + @Override + public void sendHtmlMail(String from, String subject, String message, Collection toAddresses) { + this.sendHtmlMail(from, subject, message, toAddresses.toArray(new String[0])); } /** @@ -154,12 +167,25 @@ public class NotificationManager implements INotificationManager { */ @Override public void sendHtmlMail(String subject, String message, String... toAddresses) { + this.sendHtmlMail(null, message, toAddresses); + } + + /** + * Notify users by email of something. + * + * @param from + * @param subject + * @param message + * @param toAddresses + */ + @Override + public void sendHtmlMail(String from, String subject, String message, String... toAddresses) { if (toAddresses == null || toAddresses.length == 0) { - logger.debug(MessageFormat.format("Dropping message {0} because there are no recipients", subject)); + logger.debug("Dropping message {} because there are no recipients", subject); return; } try { - Message mail = mailExecutor.createMessage(toAddresses); + Message mail = mailExecutor.createMessage(from, toAddresses); if (mail != null) { mail.setSubject(subject); diff --git a/src/main/java/com/gitblit/service/MailService.java b/src/main/java/com/gitblit/service/MailService.java index 1d5e91f5..54b6c911 100644 --- a/src/main/java/com/gitblit/service/MailService.java +++ b/src/main/java/com/gitblit/service/MailService.java @@ -114,7 +114,6 @@ public class MailService implements Runnable { return session != null; } - /** * Create a message. * @@ -122,7 +121,7 @@ public class MailService implements Runnable { * @return a message */ public Message createMessage(String... toAddresses) { - return createMessage(Arrays.asList(toAddresses)); + return createMessage(null, Arrays.asList(toAddresses)); } /** @@ -132,13 +131,35 @@ public class MailService implements Runnable { * @return a message */ public Message createMessage(List toAddresses) { + return createMessage(null, toAddresses); + } + + /** + * Create a message. + * + * @param fromDisplayName + * @param toAddresses + * @return a message + */ + public Message createMessage(String fromDisplayName, String... toAddresses) { + return createMessage(fromDisplayName, Arrays.asList(toAddresses)); + } + + /** + * Create a message. + * + * @param fromDisplayName + * @param toAddresses + * @return a message + */ + public Message createMessage(String fromDisplayName, List toAddresses) { MimeMessage message = new MimeMessage(session); try { String fromAddress = settings.getString(Keys.mail.fromAddress, null); if (StringUtils.isEmpty(fromAddress)) { fromAddress = "gitblit@gitblit.com"; } - InternetAddress from = new InternetAddress(fromAddress, "Gitblit"); + InternetAddress from = new InternetAddress(fromAddress, fromDisplayName == null ? "Gitblit" : fromDisplayName); message.setFrom(from); // determine unique set of addresses -- 2.39.5