Feature: User's Email in External Avatar URL

- Allow the user's email address to be used in external avatar's email
address
- Also update configuration document with addition and fix change url to
notify user they can do replacements there also

Change-Id: Iad4b9a06c92300827dae960e235cfe7cb6970d5d
diff --git a/src/main/java/com/googlesource/gerrit/plugins/avatars/external/ExternalUrlAvatarProvider.java b/src/main/java/com/googlesource/gerrit/plugins/avatars/external/ExternalUrlAvatarProvider.java
index c022427..c293ba7 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/avatars/external/ExternalUrlAvatarProvider.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/avatars/external/ExternalUrlAvatarProvider.java
@@ -34,6 +34,7 @@
 public class ExternalUrlAvatarProvider implements AvatarProvider {
 
   private static final String USER_PLACEHOLDER = "${user}";
+  private static final String EMAIL_PLACEHOLDER = "${email}";
 
   private final String pluginName;
   private final boolean ssl;
@@ -64,7 +65,8 @@
 
     // it is unrealistic that all users share the same avatar image, thus we're
     // warning if we can't find our marker
-    if (!externalAvatarUrl.contains(USER_PLACEHOLDER)) {
+    if (!externalAvatarUrl.contains(USER_PLACEHOLDER)
+        && !externalAvatarUrl.contains(EMAIL_PLACEHOLDER)) {
       Logger log = LoggerFactory.getLogger(ExternalUrlAvatarProvider.class);
       log.warn("Avatar provider url '" + externalAvatarUrl
           + "' does not contain " + USER_PLACEHOLDER
@@ -78,7 +80,10 @@
       externalAvatarUrl = externalAvatarUrl.replace("http://", "https://");
     }
     StringBuilder avatarUrl = new StringBuilder();
-    avatarUrl.append(replaceInUrl(externalAvatarUrl, forUser.getUserName()));
+    String userReplacedAvatarURL = replaceInUrl(USER_PLACEHOLDER,
+        externalAvatarUrl, forUser.getUserName());
+    avatarUrl.append(replaceInUrl(EMAIL_PLACEHOLDER, userReplacedAvatarURL,
+        forUser.getAccount().getPreferredEmail()));
     if (imageSize > 0 && sizeParameter != null) {
       avatarUrl.append("?");
       avatarUrl.append(sizeParameter.replaceAll("\\$\\{size\\}",
@@ -89,25 +94,27 @@
 
   @Override
   public String getChangeAvatarUrl(IdentifiedUser forUser) {
-
-    return replaceInUrl(avatarChangeUrl, forUser.getUserName());
+    String userReplacedAvatarChangeURL = replaceInUrl(USER_PLACEHOLDER,
+        avatarChangeUrl, forUser.getUserName());
+    return replaceInUrl(EMAIL_PLACEHOLDER, userReplacedAvatarChangeURL,
+        forUser.getAccount().getPreferredEmail());
   }
 
   /**
-   * Takes #{replacement} and substitutes the marker USER_PLACEHOLDER in #{url}
+   * Takes #{replacement} and substitutes the marker #{placeholder} in #{url}
    * after it has been URL encoded
-   *
-   * @param url The URL, usually containing #{USER_PLACEHOLDER}
+   * @param placeholder The placeholder to be replaced
+   * @param url The URL, usually containing #{placeholder}
    * @param replacement String to be put inside
    * @return new URL
    */
-  private String replaceInUrl(String url, String replacement) {
-    if (replacement == null || url == null
-        || url.contains(USER_PLACEHOLDER) == false) {
+  private String replaceInUrl(String placeholder, String url,
+      String replacement) {
+    if (!url.contains(placeholder)) {
       return url;
     }
 
     // as we can't assume anything of 'replacement', we're URL encoding it
-    return url.replace(USER_PLACEHOLDER, Url.encode(replacement));
+    return url.replace(placeholder, Url.encode(replacement));
   }
 }
diff --git a/src/main/resources/Documentation/config.md b/src/main/resources/Documentation/config.md
index f76d7a9..83ab4e7 100644
--- a/src/main/resources/Documentation/config.md
+++ b/src/main/resources/Documentation/config.md
@@ -13,13 +13,16 @@
 
 <a id="url">
 `plugin.@PLUGIN@.url`
-:	The location of avatar images containing `${user}`, which will then
-	be replaced by the `username`. Required.
+:	The location of avatar images. The placeholder `${user}` will
+	be replaced by the `username` and `${email}` will be replaced with
+	the user's `email address`. Required.
 
 <a id="changeUrl">
 `plugin.@PLUGIN@.changeUrl`
 :	The URL shown in Gerrit's user settings to tell the user, where the
-	avatar can be changed. Optional.
+	avatar can be changed. The placeholder `${user}` will
+	be replaced by the `username` and `${email}` will be replaced with
+	the user's `email address`. Optional.
 
 <a id="sizeParameter">
 `plugin.@PLUGIN@.sizeParameter`