Skip to content

Commit

Permalink
Merge pull request #12 from jenkinsci/JENKINS_19433_fix
Browse files Browse the repository at this point in the history
[JENKINS-19433] - Added handling of user-defined e-mail addresses in FastEmailResolver.
  • Loading branch information
oleg-nenashev committed Mar 10, 2014
2 parents 1dfed4a + 2b59658 commit 17c289f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/main/java/hudson/tasks/MailAddressResolver.java
Expand Up @@ -38,6 +38,7 @@
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import hudson.tasks.Mailer;

/**
* Infers e-mail addresses for the user when none is specified.
Expand Down Expand Up @@ -101,12 +102,20 @@ public abstract class MailAddressResolver implements ExtensionPoint {

/**
* Try to resolve email address using resolvers.
* If a user specifies a Mail {@link UserProperty}, then it will be used with
* the highest priority.
* @return User address or null if resolution failed
*/
public static String resolve(User u) {
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.fine("Resolving e-mail address for \""+u+"\" ID="+u.getId());
}

// Use User Property with a highest priority
String userPropertyAddress = extractAddressFromUserProperty(u);
if (userPropertyAddress != null) {
return userPropertyAddress;
}

for (MailAddressResolver r : all()) {
String email = r.findMailAddressFor(u);
Expand All @@ -126,10 +135,16 @@ public static String resolve(User u) {
* Try to resolve user email address fast enough to be used from UI
* <p>
* This implementation does not trigger {@link MailAddressResolver} extension point.
* @param u A user, for whom the email should be resolved
* @return User address or null if resolution failed
*/
public static String resolveFast(User u) {

String userPropertyAddress = extractAddressFromUserProperty(u);
if (userPropertyAddress != null) {
return userPropertyAddress;
}

String extractedAddress = extractAddressFromId(u.getFullName());
if (extractedAddress != null)
return extractedAddress;
Expand All @@ -151,6 +166,22 @@ public static String resolveFast(User u) {

return null;
}

/**
* Try to resolve user email using his {@link UserProperty}.
* @param u A user, for whom the email should be resolved
* @return User address or null if the resolution fails
*/
private static String extractAddressFromUserProperty (User u) {
Mailer.UserProperty emailProperty = u.getProperty(Mailer.UserProperty.class);
if (emailProperty != null) {
String explicitAddress = emailProperty.getExplicitlyConfiguredAddress();
if (explicitAddress != null) // A final check to prevent concurrency issues
return explicitAddress;
}

return null; // Return nothing by default
}

/**
* Tries to extract an email address from the user id, or returns null
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/hudson/tasks/Mailer.java
Expand Up @@ -57,6 +57,7 @@
import java.util.logging.Logger;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import javax.annotation.CheckForNull;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
Expand Down Expand Up @@ -555,6 +556,20 @@ public String getConfiguredAddress() {
// try the inference logic
return MailAddressResolver.resolveFast(user);
}

/**
* Gets an email address, which have been explicitly configured on the
* user's configuration page.
* This method also truncates spaces. It is highly recommended to
* use {@link #hasExplicitlyConfiguredAddress()} method to check the
* option's existence.
* @return A trimmed email address. It can be null
* @since TODO
*/
@CheckForNull
public String getExplicitlyConfiguredAddress() {
return Util.fixEmptyAndTrim(emailAddress);
}

/**
* Has the user configured a value explicitly (true), or is it inferred (false)?
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/hudson/tasks/MailAddressResolverTest.java
Expand Up @@ -140,6 +140,17 @@ public void doResolveWhenNotUsingFastResolution() throws Exception {
assertEquals("a@b.c", address);
}

@Test
public void doResolveWhenUsingExplicitlUserEmail() {
final String testEmail = "very_strange_email@test.case";

when(user.getProperty(Mailer.UserProperty.class)).thenReturn(
new Mailer.UserProperty(testEmail));

final String address = MailAddressResolver.resolveFast(user);
assertEquals(testEmail, address);
}

private MailAddressResolver mockResolver() {

return PowerMockito.mock(MailAddressResolver.class);
Expand Down

0 comments on commit 17c289f

Please sign in to comment.