浏览代码

HDDS-2137. HddsClientUtils and OzoneUtils have duplicate verifyResourceName()

Closes #1455
Viraj Jasani 5 年之前
父节点
当前提交
285ed0a849

+ 14 - 26
hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/client/HddsClientUtils.java

@@ -126,8 +126,6 @@ public final class HddsClientUtils {
         .toInstant().toEpochMilli();
   }
 
-
-
   /**
    * verifies that bucket name / volume name is a valid DNS name.
    *
@@ -135,29 +133,25 @@ public final class HddsClientUtils {
    *
    * @throws IllegalArgumentException
    */
-  public static void verifyResourceName(String resName)
-      throws IllegalArgumentException {
-
+  public static void verifyResourceName(String resName) throws IllegalArgumentException {
     if (resName == null) {
       throw new IllegalArgumentException("Bucket or Volume name is null");
     }
 
-    if ((resName.length() < OzoneConsts.OZONE_MIN_BUCKET_NAME_LENGTH) ||
-        (resName.length() > OzoneConsts.OZONE_MAX_BUCKET_NAME_LENGTH)) {
+    if (resName.length() < OzoneConsts.OZONE_MIN_BUCKET_NAME_LENGTH ||
+        resName.length() > OzoneConsts.OZONE_MAX_BUCKET_NAME_LENGTH) {
       throw new IllegalArgumentException(
-          "Bucket or Volume length is illegal, " +
-              "valid length is 3-63 characters");
+          "Bucket or Volume length is illegal, valid length is 3-63 characters");
     }
 
-    if ((resName.charAt(0) == '.') || (resName.charAt(0) == '-')) {
+    if (resName.charAt(0) == '.' || resName.charAt(0) == '-') {
       throw new IllegalArgumentException(
           "Bucket or Volume name cannot start with a period or dash");
     }
 
-    if ((resName.charAt(resName.length() - 1) == '.') ||
-        (resName.charAt(resName.length() - 1) == '-')) {
-      throw new IllegalArgumentException(
-          "Bucket or Volume name cannot end with a period or dash");
+    if (resName.charAt(resName.length() - 1) == '.' ||
+        resName.charAt(resName.length() - 1) == '-') {
+      throw new IllegalArgumentException("Bucket or Volume name cannot end with a period or dash");
     }
 
     boolean isIPv4 = true;
@@ -165,36 +159,30 @@ public final class HddsClientUtils {
 
     for (int index = 0; index < resName.length(); index++) {
       char currChar = resName.charAt(index);
-
       if (currChar != '.') {
         isIPv4 = ((currChar >= '0') && (currChar <= '9')) && isIPv4;
       }
-
       if (currChar > 'A' && currChar < 'Z') {
         throw new IllegalArgumentException(
             "Bucket or Volume name does not support uppercase characters");
       }
-
-      if ((currChar != '.') && (currChar != '-')) {
-        if ((currChar < '0') || (currChar > '9' && currChar < 'a') ||
-            (currChar > 'z')) {
+      if (currChar != '.' && currChar != '-') {
+        if (currChar < '0' || (currChar > '9' && currChar < 'a') ||
+            currChar > 'z') {
           throw new IllegalArgumentException("Bucket or Volume name has an " +
               "unsupported character : " +
               currChar);
         }
       }
-
-      if ((prev == '.') && (currChar == '.')) {
+      if (prev == '.' && currChar == '.') {
         throw new IllegalArgumentException("Bucket or Volume name should not " +
             "have two contiguous periods");
       }
-
-      if ((prev == '-') && (currChar == '.')) {
+      if (prev == '-' && currChar == '.') {
         throw new IllegalArgumentException(
             "Bucket or Volume name should not have period after dash");
       }
-
-      if ((prev == '.') && (currChar == '-')) {
+      if (prev == '.' && currChar == '-') {
         throw new IllegalArgumentException(
             "Bucket or Volume name should not have dash after period");
       }

+ 3 - 70
hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/web/utils/OzoneUtils.java

@@ -31,6 +31,7 @@ import java.util.concurrent.TimeUnit;
 import org.apache.hadoop.classification.InterfaceAudience;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.hdds.HddsUtils;
+import org.apache.hadoop.hdds.scm.client.HddsClientUtils;
 import org.apache.hadoop.ozone.OzoneConsts;
 
 import com.google.common.base.Preconditions;
@@ -144,76 +145,8 @@ public final class OzoneUtils {
    *
    * @throws IllegalArgumentException
    */
-  public static void verifyResourceName(String resName)
-      throws IllegalArgumentException {
-
-    if (resName == null) {
-      throw new IllegalArgumentException("Bucket or Volume name is null");
-    }
-
-    if ((resName.length() < OzoneConsts.OZONE_MIN_BUCKET_NAME_LENGTH) ||
-        (resName.length() > OzoneConsts.OZONE_MAX_BUCKET_NAME_LENGTH)) {
-      throw new IllegalArgumentException(
-          "Bucket or Volume length is illegal, " +
-              "valid length is 3-63 characters");
-    }
-
-    if ((resName.charAt(0) == '.') || (resName.charAt(0) == '-')) {
-      throw new IllegalArgumentException(
-          "Bucket or Volume name cannot start with a period or dash");
-    }
-
-    if ((resName.charAt(resName.length() - 1) == '.') ||
-        (resName.charAt(resName.length() - 1) == '-')) {
-      throw new IllegalArgumentException(
-          "Bucket or Volume name cannot end with a period or dash");
-    }
-
-    boolean isIPv4 = true;
-    char prev = (char) 0;
-
-    for (int index = 0; index < resName.length(); index++) {
-      char currChar = resName.charAt(index);
-
-      if (currChar != '.') {
-        isIPv4 = ((currChar >= '0') && (currChar <= '9')) && isIPv4;
-      }
-
-      if (currChar > 'A' && currChar < 'Z') {
-        throw new IllegalArgumentException(
-            "Bucket or Volume name does not support uppercase characters");
-      }
-
-      if ((currChar != '.') && (currChar != '-')) {
-        if ((currChar < '0') || (currChar > '9' && currChar < 'a') ||
-            (currChar > 'z')) {
-          throw new IllegalArgumentException("Bucket or Volume name has an " +
-              "unsupported character : " +
-              currChar);
-        }
-      }
-
-      if ((prev == '.') && (currChar == '.')) {
-        throw new IllegalArgumentException("Bucket or Volume name should not " +
-            "have two contiguous periods");
-      }
-
-      if ((prev == '-') && (currChar == '.')) {
-        throw new IllegalArgumentException(
-            "Bucket or Volume name should not have period after dash");
-      }
-
-      if ((prev == '.') && (currChar == '-')) {
-        throw new IllegalArgumentException(
-            "Bucket or Volume name should not have dash after period");
-      }
-      prev = currChar;
-    }
-
-    if (isIPv4) {
-      throw new IllegalArgumentException(
-          "Bucket or Volume name cannot be an IPv4 address or all numeric");
-    }
+  public static void verifyResourceName(String resName) throws IllegalArgumentException {
+    HddsClientUtils.verifyResourceName(resName);
   }
 
   /**