Do not get HttpServletRequest in SSH and SendMail threads

When asking for the canonical web url in a SSH or SendMail
thread it isn't useful to request the HttpServletRequest and
cause Guice exceptions: it is way better to just give up and
accept the default behaviour in Gerrit.

Also, when the HttpServletRequest cannot be obtained
the Guice exception to manage is ProvisionException.

Change-Id: Ife5f71285a91c95b161aeb2ce4684ba2261d1aa6
diff --git a/src/main/java/com/gerritforge/gerrit/modules/virtualhost/VirtualHostHttpCanonicalWebUrlProvider.java b/src/main/java/com/gerritforge/gerrit/modules/virtualhost/VirtualHostHttpCanonicalWebUrlProvider.java
index 77e5995..96477c6 100644
--- a/src/main/java/com/gerritforge/gerrit/modules/virtualhost/VirtualHostHttpCanonicalWebUrlProvider.java
+++ b/src/main/java/com/gerritforge/gerrit/modules/virtualhost/VirtualHostHttpCanonicalWebUrlProvider.java
@@ -19,8 +19,8 @@
 import com.google.gerrit.httpd.HttpCanonicalWebUrlProvider;
 import com.google.gerrit.server.config.GerritServerConfig;
 import com.google.inject.Inject;
-import com.google.inject.OutOfScopeException;
 import com.google.inject.Provider;
+import com.google.inject.ProvisionException;
 import com.google.inject.Singleton;
 import java.net.URI;
 import java.util.Optional;
@@ -70,12 +70,21 @@
 
   @Nullable
   private static String extractServerNameWhenInScope(Provider<HttpServletRequest> provider) {
-    try {
-      return provider.get().getServerName();
-    } catch (OutOfScopeException e) {
-      logger.atWarning().withCause(e).log(
-          "Unable to determine the virtual-host servername: current thread is out of an HTTP request scope or outside a call stack coming from a GuiceServlet filter");
+    String currentThreadName = Thread.currentThread().getName();
+    // SSH threads and associated delegate (e.g. ReceiveCommits-*-for-SSH) and SendMail would
+    // never have an incoming HTTP servlet request context, therefore avoid to invoke Guice as
+    // it would surely result in a ProvisionException
+    if (currentThreadName.contains("SSH") || currentThreadName.contains("SendEmail")) {
       return null;
     }
+
+    try {
+      return provider.get().getServerName();
+    } catch (ProvisionException e) {
+      logger.atWarning().withCause(e).log(
+          "Unable to determine the virtual-host servername: current thread is out of an HTTP request scope or outside a call stack coming from a GuiceServlet filter");
+    }
+
+    return null;
   }
 }