소스 검색

HDFS-8741. Proper error msg to be printed when invalid operation type is given to WebHDFS operations. Contributed by Surendra Singh Lilhore.

(cherry picked from commit 3536ce031ca780d6de83cf67779f571a0142ccc8)
Yiqun Lin 8 년 전
부모
커밋
5b49a96328

+ 10 - 1
hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/web/resources/DeleteOpParam.java

@@ -72,7 +72,16 @@ public class DeleteOpParam extends HttpOpParam<DeleteOpParam.Op> {
    * @param str a string representation of the parameter value.
    */
   public DeleteOpParam(final String str) {
-    super(DOMAIN, DOMAIN.parse(str));
+    super(DOMAIN, getOp(str));
+  }
+
+  private static Op getOp(String str) {
+    try {
+      return DOMAIN.parse(str);
+    } catch (IllegalArgumentException e) {
+      throw new IllegalArgumentException(str + " is not a valid " + Type.DELETE
+          + " operation.");
+    }
   }
 
   @Override

+ 10 - 1
hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/web/resources/GetOpParam.java

@@ -111,7 +111,16 @@ public class GetOpParam extends HttpOpParam<GetOpParam.Op> {
    * @param str a string representation of the parameter value.
    */
   public GetOpParam(final String str) {
-    super(DOMAIN, DOMAIN.parse(str));
+    super(DOMAIN, getOp(str));
+  }
+
+  private static Op getOp(String str) {
+    try {
+      return DOMAIN.parse(str);
+    } catch (IllegalArgumentException e) {
+      throw new IllegalArgumentException(str + " is not a valid " + Type.GET
+          + " operation.");
+    }
   }
 
   @Override

+ 10 - 1
hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/web/resources/PostOpParam.java

@@ -80,7 +80,16 @@ public class PostOpParam extends HttpOpParam<PostOpParam.Op> {
    * @param str a string representation of the parameter value.
    */
   public PostOpParam(final String str) {
-    super(DOMAIN, DOMAIN.parse(str));
+    super(DOMAIN, getOp(str));
+  }
+
+  private static Op getOp(String str) {
+    try {
+      return DOMAIN.parse(str);
+    } catch (IllegalArgumentException e) {
+      throw new IllegalArgumentException(str + " is not a valid " + Type.POST
+          + " operation.");
+    }
   }
 
   @Override

+ 10 - 1
hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/web/resources/PutOpParam.java

@@ -107,7 +107,16 @@ public class PutOpParam extends HttpOpParam<PutOpParam.Op> {
    * @param str a string representation of the parameter value.
    */
   public PutOpParam(final String str) {
-    super(DOMAIN, DOMAIN.parse(str));
+    super(DOMAIN, getOp(str));
+  }
+
+  private static Op getOp(String str) {
+    try {
+      return DOMAIN.parse(str);
+    } catch (IllegalArgumentException e) {
+      throw new IllegalArgumentException(str + " is not a valid " + Type.PUT
+          + " operation.");
+    }
   }
 
   @Override

+ 41 - 0
hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/web/resources/TestParam.java

@@ -36,6 +36,7 @@ import org.apache.hadoop.fs.XAttrSetFlag;
 import org.apache.hadoop.fs.permission.AclEntry;
 import org.apache.hadoop.fs.permission.FsPermission;
 import org.apache.hadoop.hdfs.DFSConfigKeys;
+import org.apache.hadoop.test.GenericTestUtils;
 import org.apache.hadoop.util.StringUtils;
 import org.junit.Assert;
 import org.junit.Test;
@@ -468,4 +469,44 @@ public class TestParam {
     p = new StoragePolicyParam("COLD");
     Assert.assertEquals("COLD", p.getValue());
   }
+
+  @Test
+  public void testHttpOpParams() {
+    try {
+      new PostOpParam("TEST");
+      Assert
+          .fail("Construct the PostOpParam with param value 'TEST' should be"
+              + " failed.");
+    } catch (IllegalArgumentException e) {
+      GenericTestUtils.assertExceptionContains(
+          "TEST is not a valid POST operation.", e);
+    }
+    try {
+      new PutOpParam("TEST");
+      Assert
+          .fail("Construct the PutOpParam with param value 'TEST' should be"
+              + " failed.");
+    } catch (IllegalArgumentException e) {
+      GenericTestUtils.assertExceptionContains(
+          "TEST is not a valid PUT operation.", e);
+    }
+    try {
+      new DeleteOpParam("TEST");
+      Assert
+          .fail("Construct the DeleteOpParam with param value 'TEST' should be"
+              + " failed.");
+    } catch (IllegalArgumentException e) {
+      GenericTestUtils.assertExceptionContains(
+          "TEST is not a valid DELETE operation.", e);
+    }
+    try {
+      new GetOpParam("TEST");
+      Assert
+          .fail("Construct the GetOpParam with param value 'TEST' should be"
+              + " failed.");
+    } catch (IllegalArgumentException e) {
+      GenericTestUtils.assertExceptionContains(
+          "TEST is not a valid GET operation.", e);
+    }
+  }
 }