From 5b49a96328349096466b80474e292940d3c08164 Mon Sep 17 00:00:00 2001 From: Yiqun Lin Date: Mon, 6 Mar 2017 19:04:03 +0800 Subject: [PATCH] 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) --- .../hdfs/web/resources/DeleteOpParam.java | 11 ++++- .../hadoop/hdfs/web/resources/GetOpParam.java | 11 ++++- .../hdfs/web/resources/PostOpParam.java | 11 ++++- .../hadoop/hdfs/web/resources/PutOpParam.java | 11 ++++- .../hadoop/hdfs/web/resources/TestParam.java | 41 +++++++++++++++++++ 5 files changed, 81 insertions(+), 4 deletions(-) diff --git a/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/web/resources/DeleteOpParam.java b/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/web/resources/DeleteOpParam.java index 25bed1c6d3e..e765498b5f0 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/web/resources/DeleteOpParam.java +++ b/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 { * @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 diff --git a/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/web/resources/GetOpParam.java b/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/web/resources/GetOpParam.java index 1321bf6fad9..d32af330ae7 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/web/resources/GetOpParam.java +++ b/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 { * @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 diff --git a/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/web/resources/PostOpParam.java b/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/web/resources/PostOpParam.java index 56a14c7cc4a..305db46e9c2 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/web/resources/PostOpParam.java +++ b/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 { * @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 diff --git a/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/web/resources/PutOpParam.java b/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/web/resources/PutOpParam.java index 4bb48a62288..558bb53a429 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/web/resources/PutOpParam.java +++ b/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 { * @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 diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/web/resources/TestParam.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/web/resources/TestParam.java index 70496bfc5b6..a0280199e01 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/web/resources/TestParam.java +++ b/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); + } + } }