From 3536ce031ca780d6de83cf67779f571a0142ccc8 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. --- .../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 25bed1c6d3..e765498b5f 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 String toQueryString() { * @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 1321bf6fad..d32af330ae 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 String toQueryString() { * @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 56a14c7cc4..305db46e9c 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 String toQueryString() { * @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 4bb48a6228..558bb53a42 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 String toQueryString() { * @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 d444cb4360..6c145a4b6c 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.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; @@ -503,4 +504,44 @@ public void testStoragePolicyParam() { 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); + } + } }