Browse Source

HDFS-7818. OffsetParam should return the default value instead of throwing NPE when the value is unspecified. Contributed by Eric Payne.

Haohui Mai 10 years ago
parent
commit
ab251fd355

+ 3 - 0
hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt

@@ -801,6 +801,9 @@ Release 2.7.0 - UNRELEASED
     HDFS-7885. Datanode should not trust the generation stamp provided by
     client. (Tsz Wo Nicholas Sze via jing9)
 
+    HDFS-7818. OffsetParam should return the default value instead of throwing
+    NPE when the value is unspecified. (Eric Payne via wheat9)
+
     BREAKDOWN OF HDFS-7584 SUBTASKS AND RELATED JIRAS
 
       HDFS-7720. Quota by Storage Type API, tools and ClientNameNode

+ 1 - 1
hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/web/webhdfs/ParameterParser.java

@@ -62,7 +62,7 @@ class ParameterParser {
   }
 
   long offset() {
-    return new OffsetParam(param(OffsetParam.NAME)).getValue();
+    return new OffsetParam(param(OffsetParam.NAME)).getOffset();
   }
 
   String namenodeId() {

+ 5 - 0
hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/resources/OffsetParam.java

@@ -46,4 +46,9 @@ public class OffsetParam extends LongParam {
   public String getName() {
     return NAME;
   }
+
+  public Long getOffset() {
+    Long offset = getValue();
+    return (offset == null) ? Long.valueOf(0) : offset;
+  }
 }

+ 19 - 0
hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/web/webhdfs/TestParameterParser.java

@@ -23,6 +23,7 @@ import org.apache.hadoop.hdfs.HAUtil;
 import org.apache.hadoop.hdfs.security.token.delegation.DelegationTokenIdentifier;
 import org.apache.hadoop.hdfs.web.resources.DelegationParam;
 import org.apache.hadoop.hdfs.web.resources.NamenodeAddressParam;
+import org.apache.hadoop.hdfs.web.resources.OffsetParam;
 import org.apache.hadoop.security.token.Token;
 import org.junit.Assert;
 import org.junit.Test;
@@ -65,4 +66,22 @@ public class TestParameterParser {
     ParameterParser testParser = new ParameterParser(decoder, conf);
     Assert.assertEquals(EXPECTED_PATH, testParser.path());
   }
+
+  @Test
+  public void testOffset() throws IOException {
+    final long X = 42;
+
+    long offset = new OffsetParam(Long.toString(X)).getOffset();
+    Assert.assertEquals("OffsetParam: ", X, offset);
+
+    offset = new OffsetParam((String) null).getOffset();
+    Assert.assertEquals("OffsetParam with null should have defaulted to 0", 0, offset);
+
+    try {
+      offset = new OffsetParam("abc").getValue();
+      Assert.fail("OffsetParam with nondigit value should have thrown IllegalArgumentException");
+    } catch (IllegalArgumentException iae) {
+      // Ignore
+    }
+  }
 }