Allow to provide protocol in gravatar service url

Gravatar service protocol (http/https) can be defined in provided url.
If the protocol is not present, it will be taken from canonical web url.

Change-Id: Ie9960445e26bd1274e0c3aabb9e95fdc48c18907
diff --git a/src/main/java/com/googlesource/gerrit/plugins/avatars/gravatar/GravatarAvatarProvider.java b/src/main/java/com/googlesource/gerrit/plugins/avatars/gravatar/GravatarAvatarProvider.java
index f53464a..b481a99 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/avatars/gravatar/GravatarAvatarProvider.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/avatars/gravatar/GravatarAvatarProvider.java
@@ -42,7 +42,6 @@
     return r.toString();
   }
 
-  private final boolean ssl;
   private final String avatarType;
   private final String avatarRating;
   private final String gravatarUrl;
@@ -53,17 +52,23 @@
       @CanonicalWebUrl String canonicalUrl,
       @PluginName String pluginName,
       PluginConfigFactory cfgFactory) {
-    ssl = canonicalUrl.startsWith("https://");
     this.avatarType = cfgFactory.getFromGerritConfig(pluginName).getString("type", "identicon");
     this.avatarRating = cfgFactory.getFromGerritConfig(pluginName).getString("rating", "pg");
-    this.gravatarUrl =
-        cfgFactory
-            .getFromGerritConfig(pluginName)
-            .getString("gravatarUrl", "www.gravatar.com/avatar/");
     this.changeAvatarUrl =
         cfgFactory
             .getFromGerritConfig(pluginName)
             .getString("changeAvatarUrl", "http://www.gravatar.com");
+
+    String gravatarUrlCfg =
+        cfgFactory
+            .getFromGerritConfig(pluginName)
+            .getString("gravatarUrl", "www.gravatar.com/avatar/");
+    if (gravatarUrlCfg.matches("^https?://.+")) {
+      this.gravatarUrl = gravatarUrlCfg;
+    } else {
+      this.gravatarUrl =
+          (canonicalUrl.startsWith("https://") ? "https://" : "http://") + gravatarUrlCfg;
+    }
   }
 
   @Override
@@ -81,13 +86,7 @@
     } catch (NoSuchAlgorithmException e) {
       throw new RuntimeException("MD5 digest not supported - required for Gravatar");
     }
-    StringBuilder url = new StringBuilder();
-    if (ssl) {
-      url.append("https://");
-    } else {
-      url.append("http://");
-    }
-    url.append(gravatarUrl);
+    StringBuilder url = new StringBuilder(gravatarUrl);
     url.append(hex(emailMd5));
     url.append(".jpg");
     url.append("?d=" + avatarType + "&r=" + avatarRating);
diff --git a/src/main/resources/Documentation/config.md b/src/main/resources/Documentation/config.md
index 465ae8d..d9a0f67 100644
--- a/src/main/resources/Documentation/config.md
+++ b/src/main/resources/Documentation/config.md
@@ -31,7 +31,7 @@
 `plugin.@PLUGIN@.gravatarUrl`
 :	URL that provides the Gravatar API.
 
-	Uses the same protocol (http, https) as gerrit.canonicalWebUrl.
+	Uses provided protocol (http, https) or the same as gerrit.canonicalWebUrl.
 
 	Default: www.gravatar.com/avatar/