From 285ed0a8491c82ba1b7d1e6ebe8da753f293d35e Mon Sep 17 00:00:00 2001 From: Viraj Jasani Date: Wed, 18 Sep 2019 16:43:24 +0200 Subject: [PATCH] HDDS-2137. HddsClientUtils and OzoneUtils have duplicate verifyResourceName() Closes #1455 --- .../hdds/scm/client/HddsClientUtils.java | 40 ++++------ .../hadoop/ozone/web/utils/OzoneUtils.java | 73 +------------------ 2 files changed, 17 insertions(+), 96 deletions(-) diff --git a/hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/client/HddsClientUtils.java b/hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/client/HddsClientUtils.java index 1fa2665d07c..78213ed447d 100644 --- a/hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/client/HddsClientUtils.java +++ b/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"); } diff --git a/hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/web/utils/OzoneUtils.java b/hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/web/utils/OzoneUtils.java index 954cab1e62b..4adb0066c0d 100644 --- a/hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/web/utils/OzoneUtils.java +++ b/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); } /**