Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Email addresses can now be prepended with cc: to denote that the email should be sent to them as a CC - Also fixed spelling of files and classes
- Loading branch information
Showing
5 changed files
with
113 additions
and
73 deletions.
There are no files selected for viewing
49 changes: 0 additions & 49 deletions
49
src/main/java/hudson/plugins/emailext/EmailRecepientUtils.java
This file was deleted.
Oops, something went wrong.
66 changes: 66 additions & 0 deletions
66
src/main/java/hudson/plugins/emailext/EmailRecipientUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package hudson.plugins.emailext; | ||
|
||
import hudson.EnvVars; | ||
import hudson.tasks.Mailer; | ||
import hudson.util.FormValidation; | ||
import org.apache.commons.lang.StringUtils; | ||
|
||
import javax.mail.internet.AddressException; | ||
import javax.mail.internet.InternetAddress; | ||
import java.util.LinkedHashSet; | ||
import java.util.Set; | ||
|
||
public class EmailRecipientUtils { | ||
|
||
public static final String COMMA_SEPARATED_SPLIT_REGEXP = "[,\\s]+"; | ||
|
||
public static final int TO = 0; | ||
public static final int CC = 1; | ||
|
||
public Set<InternetAddress> convertRecipientString(String recipientList, EnvVars envVars) | ||
throws AddressException { | ||
return convertRecipientString(recipientList, envVars, TO); | ||
} | ||
|
||
public Set<InternetAddress> convertRecipientString(String recipientList, EnvVars envVars, int type) | ||
throws AddressException{ | ||
final Set<InternetAddress> internetAddresses = new LinkedHashSet<InternetAddress>(); | ||
if (!StringUtils.isBlank(recipientList)) { | ||
final String expandedRecipientList = envVars.expand(recipientList); | ||
final String[] addresses = StringUtils.trim(expandedRecipientList).split(COMMA_SEPARATED_SPLIT_REGEXP); | ||
final String defaultSuffix = Mailer.descriptor().getDefaultSuffix(); | ||
for (String address : addresses) { | ||
if(!StringUtils.isBlank(address)) { | ||
boolean isCc = false; | ||
address = address.trim(); | ||
|
||
isCc = address.startsWith("cc:"); | ||
// if not a valid address, check if there is a default suffix (@something.com) provided | ||
if (!address.contains("@") && defaultSuffix != null && defaultSuffix.contains("@")) { | ||
address += defaultSuffix; | ||
} | ||
|
||
if(isCc) { | ||
address = address.substring(3); | ||
} | ||
|
||
if((type == TO && !isCc) || (type == CC && isCc)) { | ||
internetAddresses.add(new InternetAddress(address)); | ||
} | ||
} | ||
} | ||
} | ||
return internetAddresses; | ||
} | ||
|
||
public FormValidation validateFormRecipientList(String recipientList) { | ||
// Try and convert the recipient string to a list of InternetAddress. If this fails then the validation fails. | ||
try { | ||
convertRecipientString(recipientList, new EnvVars(), TO); | ||
convertRecipientString(recipientList, new EnvVars(), CC); | ||
return FormValidation.ok(); | ||
} catch (AddressException e) { | ||
return FormValidation.error(e.getMessage() + ": \"" + e.getRef() + "\""); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters