Extract root URL check in own method

Change-Id: I77132bd0ba76d51c6ee1903ab0834311473961f1
diff --git a/src/main/java/com/googlesource/gerrit/plugins/oauth/InitOAuth.java b/src/main/java/com/googlesource/gerrit/plugins/oauth/InitOAuth.java
index c0a6b3c..0da32ce 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/oauth/InitOAuth.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/oauth/InitOAuth.java
@@ -110,11 +110,7 @@
         ui.yesno(
             isConfigured(casOAuthProviderSection), "Use CAS OAuth provider for Gerrit login ?");
     if (configureCasOAuthProvider && configureOAuth(casOAuthProviderSection)) {
-      String rootUrl = casOAuthProviderSection.string("CAS Root URL", ROOT_URL, null);
-      requireNonNull(rootUrl);
-      if (!URI.create(rootUrl).isAbsolute()) {
-        throw new ProvisionException("Root URL must be absolute URL");
-      }
+      checkRootUrl(casOAuthProviderSection.string("CAS Root URL", ROOT_URL, null));
       casOAuthProviderSection.string(FIX_LEGACY_USER_ID_QUESTION, FIX_LEGACY_USER_ID, "false");
     }
 
@@ -131,21 +127,13 @@
             isConfigured(gitlabOAuthProviderSection),
             "Use GitLab OAuth provider for Gerrit login ?");
     if (configureGitLabOAuthProvider && configureOAuth(gitlabOAuthProviderSection)) {
-      String rootUrl = gitlabOAuthProviderSection.string("GitLab Root URL", ROOT_URL, null);
-      requireNonNull(rootUrl);
-      if (!URI.create(rootUrl).isAbsolute()) {
-        throw new ProvisionException("Root URL must be absolute URL");
-      }
+      checkRootUrl(gitlabOAuthProviderSection.string("GitLab Root URL", ROOT_URL, null));
     }
 
     boolean configureLemonLDAPOAuthProvider =
         ui.yesno(true, "Use LemonLDAP OAuth provider for Gerrit login ?");
     if (configureLemonLDAPOAuthProvider) {
-      String rootUrl = lemonldapOAuthProviderSection.string("LemonLDAP Root URL", ROOT_URL, null);
-      requireNonNull(rootUrl);
-      if (!URI.create(rootUrl).isAbsolute()) {
-        throw new ProvisionException("Root URL must be absolute URL");
-      }
+      checkRootUrl(lemonldapOAuthProviderSection.string("LemonLDAP Root URL", ROOT_URL, null));
       configureOAuth(lemonldapOAuthProviderSection);
     }
 
@@ -153,11 +141,7 @@
         ui.yesno(
             isConfigured(dexOAuthProviderSection), "Use Dex OAuth provider for Gerrit login ?");
     if (configureDexOAuthProvider && configureOAuth(dexOAuthProviderSection)) {
-      String rootUrl = dexOAuthProviderSection.string("Dex Root URL", ROOT_URL, null);
-      requireNonNull(rootUrl);
-      if (!URI.create(rootUrl).isAbsolute()) {
-        throw new ProvisionException("Root URL must be absolute URL");
-      }
+      checkRootUrl(dexOAuthProviderSection.string("Dex Root URL", ROOT_URL, null));
     }
 
     boolean configureKeycloakOAuthProvider =
@@ -165,11 +149,7 @@
             isConfigured(keycloakOAuthProviderSection),
             "Use Keycloak OAuth provider for Gerrit login ?");
     if (configureKeycloakOAuthProvider && configureOAuth(keycloakOAuthProviderSection)) {
-      String rootUrl = keycloakOAuthProviderSection.string("Keycloak Root URL", ROOT_URL, null);
-      requireNonNull(rootUrl);
-      if (!URI.create(rootUrl).isAbsolute()) {
-        throw new ProvisionException("Root URL must be absolute URL");
-      }
+      checkRootUrl(keycloakOAuthProviderSection.string("Keycloak Root URL", ROOT_URL, null));
       keycloakOAuthProviderSection.string("Keycloak Realm", REALM, null);
     }
 
@@ -214,6 +194,19 @@
     return false;
   }
 
+  /**
+   * Check root URL parameter. It must be not null and it must be an absolute URI.
+   *
+   * @param rootUrl root URL
+   * @throws ProvisionException if rootUrl wasn't provided or is not absolute URI.
+   */
+  private static void checkRootUrl(String rootUrl) {
+    requireNonNull(rootUrl);
+    if (!URI.create(rootUrl).isAbsolute()) {
+      throw new ProvisionException("Root URL must be absolute URL");
+    }
+  }
+
   @Override
   public void postRun() throws Exception {}
 }