PageRenderTime 36ms CodeModel.GetById 18ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/hudson-core/src/main/java/hudson/tasks/MailAddressResolver.java

http://github.com/hudson/hudson
Java | 163 lines | 52 code | 15 blank | 96 comment | 9 complexity | 4585cb6b2ba452ac8de93cacfa536ad5 MD5 | raw file
  1/*
  2 * The MIT License
  3 * 
  4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Luca Domenico Milanesio
  5 * 
  6 * Permission is hereby granted, free of charge, to any person obtaining a copy
  7 * of this software and associated documentation files (the "Software"), to deal
  8 * in the Software without restriction, including without limitation the rights
  9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 10 * copies of the Software, and to permit persons to whom the Software is
 11 * furnished to do so, subject to the following conditions:
 12 * 
 13 * The above copyright notice and this permission notice shall be included in
 14 * all copies or substantial portions of the Software.
 15 * 
 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 22 * THE SOFTWARE.
 23 */
 24package hudson.tasks;
 25
 26import hudson.Extension;
 27import hudson.ExtensionList;
 28import hudson.ExtensionListView;
 29import hudson.ExtensionPoint;
 30import hudson.model.Hudson;
 31import hudson.model.User;
 32import hudson.model.UserProperty;
 33import hudson.scm.SCM;
 34
 35import java.util.List;
 36import java.util.logging.Logger;
 37import java.util.regex.Matcher;
 38import java.util.regex.Pattern;
 39
 40/**
 41 * Infers e-mail addresses for the user when none is specified.
 42 *
 43 * <p>
 44 * This is an extension point of Hudson. Plugins tha contribute new implementation
 45 * of this class should put {@link Extension} on your implementation class, like this:
 46 *
 47 * <pre>
 48 * &#64;Extension
 49 * class MyMailAddressResolver extends {@link MailAddressResolver} {
 50 *   ...
 51 * }
 52 * </pre>
 53 *
 54 * <h2>Techniques</h2>
 55 * <p>
 56 * User identity in Hudson is global, and not specific to a particular job. As a result, mail address resolution
 57 * only receives {@link User}, which by itself doesn't really have that much information in it.
 58 *
 59 * <p>
 60 * So the common technique for a mail address resolution is to define your own {@link UserProperty} types and
 61 * add it to {@link User} objects where more context is available. For example, an {@link SCM} implementation
 62 * can have a lot more information about a particular user during a check out, so that would be a good place
 63 * to capture information as {@link UserProperty}, which then later used by a {@link MailAddressResolver}. 
 64 *
 65 * @author Kohsuke Kawaguchi
 66 * @since 1.192
 67 */
 68public abstract class MailAddressResolver implements ExtensionPoint {
 69    /**
 70     * Infers e-mail address of the given user.
 71     *
 72     * <p>
 73     * This method is called when a {@link User} without explicitly configured e-mail
 74     * address is used, as an attempt to infer e-mail address.
 75     *
 76     * <p>
 77     * The normal strategy is to look at {@link User#getProjects() the projects that the user
 78     * is participating}, then use the repository information to infer the e-mail address.
 79     *
 80     * <p>
 81     * When multiple resolvers are installed, they are consulted in order and
 82     * the search will be over when an address is inferred by someone.
 83     *
 84     * <p>
 85     * Since {@link MailAddressResolver} is singleton, this method can be invoked concurrently
 86     * from multiple threads.
 87     *
 88     * @return
 89     *      null if the inference failed.
 90     */
 91    public abstract String findMailAddressFor(User u);
 92    
 93    public static String resolve(User u) {
 94        LOGGER.fine("Resolving e-mail address for \""+u+"\" ID="+u.getId());
 95
 96        for (MailAddressResolver r : all()) {
 97            String email = r.findMailAddressFor(u);
 98            if(email!=null) {
 99                LOGGER.fine(r+" resolved "+u.getId()+" to "+email);
100                return email;
101            }
102        }
103
104        // fall back logic
105        String extractedAddress = extractAddressFromId(u.getFullName());
106        if (extractedAddress != null)
107            return extractedAddress;
108
109        if(u.getFullName().contains("@"))
110            // this already looks like an e-mail ID
111            return u.getFullName();
112
113        String ds = Mailer.descriptor().getDefaultSuffix();
114        if(ds!=null) {
115            // another common pattern is "DOMAIN\person" in Windows. Only
116            // do this when this full name is not manually set. see HUDSON-5164
117            Matcher m = WINDOWS_DOMAIN_REGEXP.matcher(u.getFullName());
118            if (m.matches() && u.getFullName().replace('\\','_').equals(u.getId()))
119                return m.group(1)+ds; // user+defaultSuffix
120
121            return u.getId()+ds;
122        } else
123            return null;
124    }
125
126    /**
127     * Tries to extract an email address from the user id, or returns null
128     */
129    private static String extractAddressFromId(String id) {
130        Matcher m = EMAIL_ADDRESS_REGEXP.matcher(id);
131        if(m.matches())
132    		return m.group(1);
133    	return null;
134    }
135
136    /**
137     * Matches strings like "Kohsuke Kawaguchi &lt;kohsuke.kawaguchi@sun.com>"
138     * @see #extractAddressFromId(String)
139     */
140    private static final Pattern EMAIL_ADDRESS_REGEXP = Pattern.compile("^.*<([^>]+)>.*$");
141
142    /**
143     * Matches something like "DOMAIN\person"
144     */
145    private static final Pattern WINDOWS_DOMAIN_REGEXP = Pattern.compile("[^\\\\ ]+\\\\([^\\\\ ]+)");
146
147    /**
148     * All registered {@link MailAddressResolver} implementations.
149     *
150     * @deprecated as of 1.286
151     *      Use {@link #all()} for read access and {@link Extension} for registration.
152     */
153    public static final List<MailAddressResolver> LIST = ExtensionListView.createList(MailAddressResolver.class);
154
155    /**
156     * Returns all the registered {@link MailAddressResolver} descriptors.
157     */
158    public static ExtensionList<MailAddressResolver> all() {
159        return Hudson.getInstance().getExtensionList(MailAddressResolver.class);
160    }
161
162    private static final Logger LOGGER = Logger.getLogger(MailAddressResolver.class.getName());
163}