hadoop-common-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From szets...@apache.org
Subject svn commit: r1179172 - in /hadoop/common/branches/branch-0.20-security-205: ./ src/hdfs/org/apache/hadoop/hdfs/server/namenode/web/resources/ src/hdfs/org/apache/hadoop/hdfs/web/ src/test/org/apache/hadoop/hdfs/web/
Date Wed, 05 Oct 2011 11:37:04 GMT
Author: szetszwo
Date: Wed Oct  5 11:37:04 2011
New Revision: 1179172

URL: http://svn.apache.org/viewvc?rev=1179172&view=rev
Log:
svn merge -c 1179171 from branch-0.20-security for HDFS-2395.

Modified:
    hadoop/common/branches/branch-0.20-security-205/   (props changed)
    hadoop/common/branches/branch-0.20-security-205/CHANGES.txt   (contents, props changed)
    hadoop/common/branches/branch-0.20-security-205/src/hdfs/org/apache/hadoop/hdfs/server/namenode/web/resources/NamenodeWebHdfsMethods.java
    hadoop/common/branches/branch-0.20-security-205/src/hdfs/org/apache/hadoop/hdfs/web/JsonUtil.java
    hadoop/common/branches/branch-0.20-security-205/src/hdfs/org/apache/hadoop/hdfs/web/WebHdfsFileSystem.java
    hadoop/common/branches/branch-0.20-security-205/src/test/org/apache/hadoop/hdfs/web/TestJsonUtil.java

Propchange: hadoop/common/branches/branch-0.20-security-205/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Wed Oct  5 11:37:04 2011
@@ -1,6 +1,6 @@
 /hadoop/common/branches/branch-0.20:826138,826568,829987,831184,833001,880632,898713,909245,909723,960946,990003,1044225
 /hadoop/common/branches/branch-0.20-append:955380,955398,955448,956329
-/hadoop/common/branches/branch-0.20-security:1170042,1170087,1170997,1171137,1171380,1171613,1171891,1171905,1172184,1172188,1172190,1172192,1173470,1174471,1175114,1176179,1176720,1177907,1179036
+/hadoop/common/branches/branch-0.20-security:1170042,1170087,1170997,1171137,1171380,1171613,1171891,1171905,1172184,1172188,1172190,1172192,1173470,1174471,1175114,1176179,1176720,1177907,1179036,1179171
 /hadoop/common/branches/branch-0.20-security-203:1096071,1097011,1097249,1097269,1097281,1097966,1098816,1098819,1098823,1098827,1098832,1098839,1098854,1098863,1099088,1099191,1099324,1099330,1099333,1102071,1128115
 /hadoop/common/branches/branch-0.20-security-204:1128390,1147228,1148069,1149316,1154413
 /hadoop/core/branches/branch-0.19:713112

Modified: hadoop/common/branches/branch-0.20-security-205/CHANGES.txt
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-0.20-security-205/CHANGES.txt?rev=1179172&r1=1179171&r2=1179172&view=diff
==============================================================================
--- hadoop/common/branches/branch-0.20-security-205/CHANGES.txt (original)
+++ hadoop/common/branches/branch-0.20-security-205/CHANGES.txt Wed Oct  5 11:37:04 2011
@@ -362,6 +362,9 @@ Release 0.20.205.0 - 2011.09.28
     HADOOP-7720. Added parameter for HBase user to setup config script.
     (Arpit Gupta via Eric Yang)
 
+    HDFS-2395. Add a root element in the JSON responses of webhdfs.
+    (szetszwo)
+
 Release 0.20.204.0 - 2011-8-25
 
   NEW FEATURES

Propchange: hadoop/common/branches/branch-0.20-security-205/CHANGES.txt
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Wed Oct  5 11:37:04 2011
@@ -1,6 +1,6 @@
 /hadoop/common/branches/branch-0.20/CHANGES.txt:826138,826568,829987,831184,833001,880632,898713,909245,909723,960946,990003,1044225
 /hadoop/common/branches/branch-0.20-append/CHANGES.txt:955380,955398,955448,956329
-/hadoop/common/branches/branch-0.20-security/CHANGES.txt:1170042,1170087,1170997,1171137,1171181,1171380,1171613,1171891,1171905,1172184,1172188,1172190,1172192,1173470,1173843,1174326,1174471,1174476,1174482,1175114,1176179,1176182,1176270,1176276,1176675,1176720,1177031,1177036,1177098,1177101,1177907,1178074,1179036
+/hadoop/common/branches/branch-0.20-security/CHANGES.txt:1170042,1170087,1170997,1171137,1171181,1171380,1171613,1171891,1171905,1172184,1172188,1172190,1172192,1173470,1173843,1174326,1174471,1174476,1174482,1175114,1176179,1176182,1176270,1176276,1176675,1176720,1177031,1177036,1177098,1177101,1177907,1178074,1179036,1179171
 /hadoop/common/branches/branch-0.20-security-203/CHANGES.txt:1096071,1097011,1097249,1097269,1097281,1097966,1098816,1098819,1098823,1098827,1098832,1098839,1098854,1098863,1099088,1099191,1099324,1099330,1099333,1102071,1128115
 /hadoop/common/branches/branch-0.20-security-204/CHANGES.txt:1128390,1147228,1148069,1149316,1154413,1159730,1161741
 /hadoop/core/branches/branch-0.18/CHANGES.txt:727226

Modified: hadoop/common/branches/branch-0.20-security-205/src/hdfs/org/apache/hadoop/hdfs/server/namenode/web/resources/NamenodeWebHdfsMethods.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-0.20-security-205/src/hdfs/org/apache/hadoop/hdfs/server/namenode/web/resources/NamenodeWebHdfsMethods.java?rev=1179172&r1=1179171&r2=1179172&view=diff
==============================================================================
--- hadoop/common/branches/branch-0.20-security-205/src/hdfs/org/apache/hadoop/hdfs/server/namenode/web/resources/NamenodeWebHdfsMethods.java
(original)
+++ hadoop/common/branches/branch-0.20-security-205/src/hdfs/org/apache/hadoop/hdfs/server/namenode/web/resources/NamenodeWebHdfsMethods.java
Wed Oct  5 11:37:04 2011
@@ -236,19 +236,19 @@ public class NamenodeWebHdfsMethods {
     case MKDIRS:
     {
       final boolean b = namenode.mkdirs(fullpath, permission.getFsPermission());
-      final String js = JsonUtil.toJsonString(PutOpParam.Op.MKDIRS, b);
+      final String js = JsonUtil.toJsonString("boolean", b);
       return Response.ok(js).type(MediaType.APPLICATION_JSON).build();
     }
     case RENAME:
     {
       final boolean b = namenode.rename(fullpath, dstPath.getValue());
-      final String js = JsonUtil.toJsonString(PutOpParam.Op.RENAME, b);
+      final String js = JsonUtil.toJsonString("boolean", b);
       return Response.ok(js).type(MediaType.APPLICATION_JSON).build();
     }
     case SETREPLICATION:
     {
       final boolean b = namenode.setReplication(fullpath, replication.getValue());
-      final String js = JsonUtil.toJsonString(PutOpParam.Op.SETREPLICATION, b);
+      final String js = JsonUtil.toJsonString("boolean", b);
       return Response.ok(js).type(MediaType.APPLICATION_JSON).build();
     }
     case SETOWNER:
@@ -271,7 +271,7 @@ public class NamenodeWebHdfsMethods {
       final Token<DelegationTokenIdentifier> token = new Token<DelegationTokenIdentifier>();
       token.decodeFromUrlString(delegation.getValue());
       final long expiryTime = namenode.renewDelegationToken(token);
-      final String js = JsonUtil.toJsonString(PutOpParam.Op.RENEWDELEGATIONTOKEN, expiryTime);
+      final String js = JsonUtil.toJsonString("long", expiryTime);
       return Response.ok(js).type(MediaType.APPLICATION_JSON).build();
     }
     case CANCELDELEGATIONTOKEN:
@@ -476,7 +476,7 @@ public class NamenodeWebHdfsMethods {
       @Override
       public void write(final OutputStream outstream) throws IOException {
         final PrintStream out = new PrintStream(outstream);
-        out.print('[');
+        out.println("{\"" + HdfsFileStatus[].class.getSimpleName() + "\":[");
 
         final HdfsFileStatus[] partial = first.getPartialListing();
         if (partial.length > 0) {
@@ -495,7 +495,7 @@ public class NamenodeWebHdfsMethods {
           }
         }
         
-        out.println(']');
+        out.println("]}");
       }
     };
   }
@@ -531,7 +531,7 @@ public class NamenodeWebHdfsMethods {
         case DELETE:
         {
           final boolean b = namenode.delete(fullpath, recursive.getValue());
-          final String js = JsonUtil.toJsonString(DeleteOpParam.Op.DELETE, b);
+          final String js = JsonUtil.toJsonString("boolean", b);
           return Response.ok(js).type(MediaType.APPLICATION_JSON).build();
         }
         default:

Modified: hadoop/common/branches/branch-0.20-security-205/src/hdfs/org/apache/hadoop/hdfs/web/JsonUtil.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-0.20-security-205/src/hdfs/org/apache/hadoop/hdfs/web/JsonUtil.java?rev=1179172&r1=1179171&r2=1179172&view=diff
==============================================================================
--- hadoop/common/branches/branch-0.20-security-205/src/hdfs/org/apache/hadoop/hdfs/web/JsonUtil.java
(original)
+++ hadoop/common/branches/branch-0.20-security-205/src/hdfs/org/apache/hadoop/hdfs/web/JsonUtil.java
Wed Oct  5 11:37:04 2011
@@ -42,42 +42,29 @@ import org.apache.hadoop.io.MD5Hash;
 import org.apache.hadoop.ipc.RemoteException;
 import org.apache.hadoop.security.token.Token;
 import org.apache.hadoop.security.token.TokenIdentifier;
+import org.apache.hadoop.util.StringUtils;
 import org.mortbay.util.ajax.JSON;
 
 /** JSON Utilities */
 public class JsonUtil {
-  private static class ThreadLocalMap extends ThreadLocal<Map<String, Object>>
{
-    @Override
-    protected Map<String, Object> initialValue() {
-      return new TreeMap<String, Object>();
-    }
-
-    @Override
-    public Map<String, Object> get() {
-      final Map<String, Object> m = super.get();
-      m.clear();
-      return m;
-    }
-  }
-
-  private static final ThreadLocalMap jsonMap = new ThreadLocalMap();
-  private static final ThreadLocalMap tokenMap = new ThreadLocalMap();
-  private static final ThreadLocalMap datanodeInfoMap = new ThreadLocalMap();
-  private static final ThreadLocalMap BlockMap = new ThreadLocalMap();
-  private static final ThreadLocalMap locatedBlockMap = new ThreadLocalMap();
-
+  private static final Object[] EMPTY_OBJECT_ARRAY = {};
   private static final DatanodeInfo[] EMPTY_DATANODE_INFO_ARRAY = {};
 
   /** Convert a token object to a Json string. */
   public static String toJsonString(final Token<? extends TokenIdentifier> token
       ) throws IOException {
+    return toJsonString(Token.class, toJsonMap(token));
+  }
+
+  private static Map<String, Object> toJsonMap(
+      final Token<? extends TokenIdentifier> token) throws IOException {
     if (token == null) {
       return null;
     }
 
-    final Map<String, Object> m = tokenMap.get();
+    final Map<String, Object> m = new TreeMap<String, Object>();
     m.put("urlString", token.encodeToUrlString());
-    return JSON.toString(m);
+    return m;
   }
 
   /** Convert a Json map to a Token. */
@@ -96,46 +83,52 @@ public class JsonUtil {
   /** Convert a Json map to a Token of DelegationTokenIdentifier. */
   @SuppressWarnings("unchecked")
   public static Token<DelegationTokenIdentifier> toDelegationToken(
-      final Map<?, ?> m) throws IOException {
+      final Map<?, ?> json) throws IOException {
+    final Map<?, ?> m = (Map<?, ?>)json.get(Token.class.getSimpleName());
     return (Token<DelegationTokenIdentifier>)toToken(m);
   }
 
   /** Convert a Json map to a Token of BlockTokenIdentifier. */
   @SuppressWarnings("unchecked")
-  public static Token<BlockTokenIdentifier> toBlockToken(
+  private static Token<BlockTokenIdentifier> toBlockToken(
       final Map<?, ?> m) throws IOException {
     return (Token<BlockTokenIdentifier>)toToken(m);
   }
 
   /** Convert an exception object to a Json string. */
   public static String toJsonString(final Exception e) {
-    final Map<String, Object> m = jsonMap.get();
+    final Map<String, Object> m = new TreeMap<String, Object>();
     m.put("className", e.getClass().getName());
     m.put("message", e.getMessage());
-    return JSON.toString(m);
+    return toJsonString(RemoteException.class, m);
   }
 
   /** Convert a Json map to a RemoteException. */
-  public static RemoteException toRemoteException(final Map<String, Object> m) {
+  public static RemoteException toRemoteException(final Map<?, ?> json) {
+    final Map<?, ?> m = (Map<?, ?>)json.get(RemoteException.class.getSimpleName());
     final String className = (String)m.get("className");
     final String message = (String)m.get("message");
     return new RemoteException(className, message);
   }
 
+  private static String toJsonString(final Class<?> clazz, final Object value) {
+    return toJsonString(clazz.getSimpleName(), value);
+  }
+
   /** Convert a key-value pair to a Json string. */
-  public static String toJsonString(final Object key, final Object value) {
-    final Map<String, Object> m = jsonMap.get();
-    m.put(key instanceof String ? (String) key : key.toString(), value);
+  public static String toJsonString(final String key, final Object value) {
+    final Map<String, Object> m = new TreeMap<String, Object>();
+    m.put(key, value);
     return JSON.toString(m);
   }
 
   /** Convert a FsPermission object to a string. */
-  public static String toString(final FsPermission permission) {
+  private static String toString(final FsPermission permission) {
     return String.format("%o", permission.toShort());
   }
 
   /** Convert a string to a FsPermission object. */
-  public static FsPermission toFsPermission(final String s) {
+  private static FsPermission toFsPermission(final String s) {
     return new FsPermission(Short.parseShort(s, 8));
   }
 
@@ -144,7 +137,7 @@ public class JsonUtil {
     if (status == null) {
       return null;
     } else {
-      final Map<String, Object> m = jsonMap.get();
+      final Map<String, Object> m = new TreeMap<String, Object>();
       m.put("localName", status.getLocalName());
       m.put("isDir", status.isDir());
       m.put("len", status.getLen());
@@ -155,21 +148,17 @@ public class JsonUtil {
       m.put("modificationTime", status.getModificationTime());
       m.put("blockSize", status.getBlockSize());
       m.put("replication", status.getReplication());
-      return JSON.toString(m);
+      return toJsonString(HdfsFileStatus.class, m);
     }
   }
 
-  @SuppressWarnings("unchecked")
-  static Map<String, Object> parse(String jsonString) {
-    return (Map<String, Object>) JSON.parse(jsonString);
-  }
-
   /** Convert a Json map to a HdfsFileStatus object. */
-  public static HdfsFileStatus toFileStatus(final Map<String, Object> m) {
-    if (m == null) {
+  public static HdfsFileStatus toFileStatus(final Map<?, ?> json) {
+    if (json == null) {
       return null;
     }
 
+    final Map<?, ?> m = (Map<?, ?>)json.get(HdfsFileStatus.class.getSimpleName());
     final String localName = (String) m.get("localName");
     final boolean isDir = (Boolean) m.get("isDir");
     final long len = (Long) m.get("len");
@@ -184,21 +173,21 @@ public class JsonUtil {
         permission, owner, group, DFSUtil.string2Bytes(localName));
   }
 
-  /** Convert a LocatedBlock to a Json string. */
-  public static String toJsonString(final Block Block) {
-    if (Block == null) {
+  /** Convert a Block to a Json map. */
+  private static Map<String, Object> toJsonMap(final Block block) {
+    if (block == null) {
       return null;
     }
 
-    final Map<String, Object> m = BlockMap.get();
-    m.put("blockId", Block.getBlockId());
-    m.put("numBytes", Block.getNumBytes());
-    m.put("generationStamp", Block.getGenerationStamp());
-    return JSON.toString(m);
+    final Map<String, Object> m = new TreeMap<String, Object>();
+    m.put("blockId", block.getBlockId());
+    m.put("numBytes", block.getNumBytes());
+    m.put("generationStamp", block.getGenerationStamp());
+    return m;
   }
 
   /** Convert a Json map to an Block object. */
-  public static Block toBlock(final Map<?, ?> m) {
+  private static Block toBlock(final Map<?, ?> m) {
     if (m == null) {
       return null;
     }
@@ -209,13 +198,13 @@ public class JsonUtil {
     return new Block(blockId, numBytes, generationStamp);
   }
   
-  /** Convert a DatanodeInfo to a Json string. */
-  public static String toJsonString(final DatanodeInfo datanodeinfo) {
+  /** Convert a DatanodeInfo to a Json map. */
+  private static Map<String, Object> toJsonMap(final DatanodeInfo datanodeinfo) {
     if (datanodeinfo == null) {
       return null;
     }
 
-    final Map<String, Object> m = datanodeInfoMap.get();
+    final Map<String, Object> m = new TreeMap<String, Object>();
     m.put("name", datanodeinfo.getName());
     m.put("storageID", datanodeinfo.getStorageID());
     m.put("infoPort", datanodeinfo.getInfoPort());
@@ -230,11 +219,11 @@ public class JsonUtil {
     m.put("networkLocation", datanodeinfo.getNetworkLocation());
     m.put("hostName", datanodeinfo.getHostName());
     m.put("adminState", datanodeinfo.getAdminState().name());
-    return JSON.toString(m);
+    return m;
   }
 
   /** Convert a Json map to an DatanodeInfo object. */
-  public static DatanodeInfo toDatanodeInfo(final Map<?, ?> m) {
+  private static DatanodeInfo toDatanodeInfo(final Map<?, ?> m) {
     if (m == null) {
       return null;
     }
@@ -255,25 +244,23 @@ public class JsonUtil {
         AdminStates.valueOf((String)m.get("adminState")));
   }
 
-  /** Convert a DatanodeInfo[] to a Json string. */
-  public static String toJsonString(final DatanodeInfo[] array
-      ) throws IOException {
+  /** Convert a DatanodeInfo[] to a Json array. */
+  private static Object[] toJsonArray(final DatanodeInfo[] array) {
     if (array == null) {
       return null;
     } else if (array.length == 0) {
-      return "[]";
+      return EMPTY_OBJECT_ARRAY;
     } else {
-      final StringBuilder b = new StringBuilder().append('[').append(
-          toJsonString(array[0]));
-      for(int i = 1; i < array.length; i++) {
-        b.append(", ").append(toJsonString(array[i]));
+      final Object[] a = new Object[array.length];
+      for(int i = 0; i < array.length; i++) {
+        a[i] = toJsonMap(array[i]);
       }
-      return b.append(']').toString();
+      return a;
     }
   }
 
   /** Convert an Object[] to a DatanodeInfo[]. */
-  public static DatanodeInfo[] toDatanodeInfoArray(final Object[] objects) {
+  private static DatanodeInfo[] toDatanodeInfoArray(final Object[] objects) {
     if (objects == null) {
       return null;
     } else if (objects.length == 0) {
@@ -281,66 +268,64 @@ public class JsonUtil {
     } else {
       final DatanodeInfo[] array = new DatanodeInfo[objects.length];
       for(int i = 0; i < array.length; i++) {
-        array[i] = (DatanodeInfo)toDatanodeInfo((Map<?, ?>) objects[i]);
+        array[i] = toDatanodeInfo((Map<?, ?>) objects[i]);
       }
       return array;
     }
   }
 
-  /** Convert a LocatedBlock to a Json string. */
-  public static String toJsonString(final LocatedBlock locatedblock
+  /** Convert a LocatedBlock to a Json map. */
+  private static Map<String, Object> toJsonMap(final LocatedBlock locatedblock
       ) throws IOException {
     if (locatedblock == null) {
       return null;
     }
  
-    final Map<String, Object> m = locatedBlockMap.get();
-    m.put("blockToken", toJsonString(locatedblock.getBlockToken()));
+    final Map<String, Object> m = new TreeMap<String, Object>();
+    m.put("blockToken", toJsonMap(locatedblock.getBlockToken()));
     m.put("isCorrupt", locatedblock.isCorrupt());
     m.put("startOffset", locatedblock.getStartOffset());
-    m.put("block", toJsonString(locatedblock.getBlock()));
+    m.put("block", toJsonMap(locatedblock.getBlock()));
 
-    m.put("locations", toJsonString(locatedblock.getLocations()));
-    return JSON.toString(m);
+    m.put("locations", toJsonArray(locatedblock.getLocations()));
+    return m;
   }
 
   /** Convert a Json map to LocatedBlock. */
-  public static LocatedBlock toLocatedBlock(final Map<?, ?> m) throws IOException {
+  private static LocatedBlock toLocatedBlock(final Map<?, ?> m) throws IOException
{
     if (m == null) {
       return null;
     }
 
-    final Block b = toBlock((Map<?, ?>)JSON.parse((String)m.get("block")));
+    final Block b = toBlock((Map<?, ?>)m.get("block"));
     final DatanodeInfo[] locations = toDatanodeInfoArray(
-        (Object[])JSON.parse((String)m.get("locations")));
+        (Object[])m.get("locations"));
     final long startOffset = (Long)m.get("startOffset");
     final boolean isCorrupt = (Boolean)m.get("isCorrupt");
 
     final LocatedBlock locatedblock = new LocatedBlock(b, locations, startOffset, isCorrupt);
-    locatedblock.setBlockToken(toBlockToken((Map<?, ?>)JSON.parse((String)m.get("blockToken"))));
+    locatedblock.setBlockToken(toBlockToken((Map<?, ?>)m.get("blockToken")));
     return locatedblock;
   }
 
-  /** Convert a LocatedBlock[] to a Json string. */
-  public static String toJsonString(final List<LocatedBlock> array
+  /** Convert a LocatedBlock[] to a Json array. */
+  private static Object[] toJsonArray(final List<LocatedBlock> array
       ) throws IOException {
     if (array == null) {
       return null;
     } else if (array.size() == 0) {
-      return "[]";
+      return EMPTY_OBJECT_ARRAY;
     } else {
-      final StringBuilder b = new StringBuilder().append('[').append(
-          toJsonString(array.get(0)));
-      for(int i = 1; i < array.size(); i++) {
-        b.append(",\n  ").append(toJsonString(array.get(i)));
+      final Object[] a = new Object[array.size()];
+      for(int i = 0; i < array.size(); i++) {
+        a[i] = toJsonMap(array.get(0));
       }
-      return b.append(']').toString();
+      return a;
     }
   }
 
-  /** Convert an Object[] to a List of LocatedBlock. 
-   * @throws IOException */
-  public static List<LocatedBlock> toLocatedBlockList(final Object[] objects
+  /** Convert an Object[] to a List of LocatedBlock. */
+  private static List<LocatedBlock> toLocatedBlockList(final Object[] objects
       ) throws IOException {
     if (objects == null) {
       return null;
@@ -349,7 +334,7 @@ public class JsonUtil {
     } else {
       final List<LocatedBlock> list = new ArrayList<LocatedBlock>(objects.length);
       for(int i = 0; i < objects.length; i++) {
-        list.add((LocatedBlock)toLocatedBlock((Map<?, ?>)objects[i]));
+        list.add(toLocatedBlock((Map<?, ?>)objects[i]));
       }
       return list;
     }
@@ -362,52 +347,52 @@ public class JsonUtil {
       return null;
     }
 
-    final Map<String, Object> m = jsonMap.get();
+    final Map<String, Object> m = new TreeMap<String, Object>();
     m.put("fileLength", locatedblocks.getFileLength());
     m.put("isUnderConstruction", locatedblocks.isUnderConstruction());
 
-    m.put("locatedBlocks", toJsonString(locatedblocks.getLocatedBlocks()));
-    return JSON.toString(m);
+    m.put("locatedBlocks", toJsonArray(locatedblocks.getLocatedBlocks()));
+    return toJsonString(LocatedBlocks.class, m);
   }
 
   /** Convert a Json map to LocatedBlock. */
-  public static LocatedBlocks toLocatedBlocks(final Map<String, Object> m
+  public static LocatedBlocks toLocatedBlocks(final Map<?, ?> json
       ) throws IOException {
-    if (m == null) {
+    if (json == null) {
       return null;
     }
-    
+
+    final Map<?, ?> m = (Map<?, ?>)json.get(LocatedBlocks.class.getSimpleName());
     final long fileLength = (Long)m.get("fileLength");
     final boolean isUnderConstruction = (Boolean)m.get("isUnderConstruction");
     final List<LocatedBlock> locatedBlocks = toLocatedBlockList(
-        (Object[])JSON.parse((String) m.get("locatedBlocks")));
+        (Object[])m.get("locatedBlocks"));
     return new LocatedBlocks(fileLength, locatedBlocks, isUnderConstruction);
   }
 
   /** Convert a ContentSummary to a Json string. */
-  public static String toJsonString(final ContentSummary contentsummary
-      ) throws IOException {
+  public static String toJsonString(final ContentSummary contentsummary) {
     if (contentsummary == null) {
       return null;
     }
 
-    final Map<String, Object> m = jsonMap.get();
+    final Map<String, Object> m = new TreeMap<String, Object>();
     m.put("length", contentsummary.getLength());
     m.put("fileCount", contentsummary.getFileCount());
     m.put("directoryCount", contentsummary.getDirectoryCount());
     m.put("quota", contentsummary.getQuota());
     m.put("spaceConsumed", contentsummary.getSpaceConsumed());
     m.put("spaceQuota", contentsummary.getSpaceQuota());
-    return JSON.toString(m);
+    return toJsonString(ContentSummary.class, m);
   }
 
   /** Convert a Json map to a ContentSummary. */
-  public static ContentSummary toContentSummary(final Map<String, Object> m
-      ) throws IOException {
-    if (m == null) {
+  public static ContentSummary toContentSummary(final Map<?, ?> json) {
+    if (json == null) {
       return null;
     }
 
+    final Map<?, ?> m = (Map<?, ?>)json.get(ContentSummary.class.getSimpleName());
     final long length = (Long)m.get("length");
     final long fileCount = (Long)m.get("fileCount");
     final long directoryCount = (Long)m.get("directoryCount");
@@ -420,36 +405,51 @@ public class JsonUtil {
   }
 
   /** Convert a MD5MD5CRC32FileChecksum to a Json string. */
-  public static String toJsonString(final MD5MD5CRC32FileChecksum checksum
-      ) throws IOException {
+  public static String toJsonString(final MD5MD5CRC32FileChecksum checksum) {
     if (checksum == null) {
       return null;
     }
 
-    final Map<String, Object> m = jsonMap.get();
-    final byte[] bytes = checksum.getBytes();
-    final DataInputStream in = new DataInputStream(new ByteArrayInputStream(bytes));
-    final int bytesPerCRC = in.readInt();
-    final long crcPerBlock = in.readLong();
-    final MD5Hash md5 = MD5Hash.read(in);
-    m.put("bytesPerCRC", bytesPerCRC);
-    m.put("crcPerBlock", crcPerBlock);
-    m.put("md5", "" + md5);
-    return JSON.toString(m);
+    final Map<String, Object> m = new TreeMap<String, Object>();
+    m.put("algorithm", checksum.getAlgorithmName());
+    m.put("length", checksum.getLength());
+    m.put("bytes", StringUtils.byteToHexString(checksum.getBytes()));
+    return toJsonString(MD5MD5CRC32FileChecksum.class, m);
   }
 
   /** Convert a Json map to a MD5MD5CRC32FileChecksum. */
   public static MD5MD5CRC32FileChecksum toMD5MD5CRC32FileChecksum(
-      final Map<String, Object> m) throws IOException {
-    if (m == null) {
+      final Map<?, ?> json) throws IOException {
+    if (json == null) {
       return null;
     }
 
-    final int bytesPerCRC = (int)(long)(Long)m.get("bytesPerCRC");
-    final long crcPerBlock = (Long)m.get("crcPerBlock");
-    final String md5 = (String)m.get("md5");
+    final Map<?, ?> m = (Map<?, ?>)json.get(
+        MD5MD5CRC32FileChecksum.class.getSimpleName());
+    final String algorithm = (String)m.get("algorithm");
+    final int length = (int)(long)(Long)m.get("length");
+    final byte[] bytes = StringUtils.hexStringToByte((String)m.get("bytes"));
+
+    final DataInputStream in = new DataInputStream(new ByteArrayInputStream(bytes));
+    final int bytesPerCRC = in.readInt();
+    final long crcPerBlock = in.readLong();
+    final MD5Hash md5 = MD5Hash.read(in);
+    final MD5MD5CRC32FileChecksum checksum = new MD5MD5CRC32FileChecksum(
+        bytesPerCRC, crcPerBlock, md5);
+
+    //check algorithm name
+    final String alg = "MD5-of-" + crcPerBlock + "MD5-of-" + bytesPerCRC + "CRC32";
+    if (!alg.equals(algorithm)) {
+      throw new IOException("Algorithm not matched: algorithm=" + algorithm
+          + ", crcPerBlock=" + crcPerBlock
+          + ", bytesPerCRC=" + bytesPerCRC);
+    }
+    //check length
+    if (length != checksum.getLength()) {
+      throw new IOException("Length not matched: length=" + length
+          + ", checksum.getLength()=" + checksum.getLength());
+    }
 
-    return new MD5MD5CRC32FileChecksum(bytesPerCRC, crcPerBlock,
-        new MD5Hash(md5));
+    return checksum;
   }
 }
\ No newline at end of file

Modified: hadoop/common/branches/branch-0.20-security-205/src/hdfs/org/apache/hadoop/hdfs/web/WebHdfsFileSystem.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-0.20-security-205/src/hdfs/org/apache/hadoop/hdfs/web/WebHdfsFileSystem.java?rev=1179172&r1=1179171&r2=1179172&view=diff
==============================================================================
--- hadoop/common/branches/branch-0.20-security-205/src/hdfs/org/apache/hadoop/hdfs/web/WebHdfsFileSystem.java
(original)
+++ hadoop/common/branches/branch-0.20-security-205/src/hdfs/org/apache/hadoop/hdfs/web/WebHdfsFileSystem.java
Wed Oct  5 11:37:04 2011
@@ -363,7 +363,7 @@ public class WebHdfsFileSystem extends F
     final HttpOpParam.Op op = PutOpParam.Op.MKDIRS;
     final Map<String, Object> json = run(op, f,
         new PermissionParam(applyUMask(permission)));
-    return (Boolean)json.get(op.toString());
+    return (Boolean)json.get("boolean");
   }
 
   @Override
@@ -372,7 +372,7 @@ public class WebHdfsFileSystem extends F
     final HttpOpParam.Op op = PutOpParam.Op.RENAME;
     final Map<String, Object> json = run(op, src,
         new DstPathParam(makeQualified(dst).toUri().getPath()));
-    return (Boolean)json.get(op.toString());
+    return (Boolean)json.get("boolean");
   }
 
   @Override
@@ -402,7 +402,7 @@ public class WebHdfsFileSystem extends F
     final HttpOpParam.Op op = PutOpParam.Op.SETREPLICATION;
     final Map<String, Object> json = run(op, p,
         new ReplicationParam(replication));
-    return (Boolean)json.get(op.toString());
+    return (Boolean)json.get("boolean");
   }
 
   @Override
@@ -464,7 +464,7 @@ public class WebHdfsFileSystem extends F
   public boolean delete(Path f, boolean recursive) throws IOException {
     final HttpOpParam.Op op = DeleteOpParam.Op.DELETE;
     final Map<String, Object> json = run(op, f, new RecursiveParam(recursive));
-    return (Boolean)json.get(op.toString());
+    return (Boolean)json.get("boolean");
   }
 
   @Override
@@ -481,7 +481,9 @@ public class WebHdfsFileSystem extends F
     statistics.incrementReadOps(1);
 
     final HttpOpParam.Op op = GetOpParam.Op.LISTSTATUS;
-    final Object[] array = run(op, f);
+    final Map<?, ?> json  = run(op, f);
+    final Object[] array = (Object[])json.get(
+        HdfsFileStatus[].class.getSimpleName());
 
     //convert FileStatus
     final FileStatus[] statuses = new FileStatus[array.length];
@@ -528,7 +530,7 @@ public class WebHdfsFileSystem extends F
     delegationToken = token;
     final HttpOpParam.Op op = PutOpParam.Op.RENEWDELEGATIONTOKEN;
     final Map<String, Object> m = run(op, null);
-    return (Long)m.get(op.toString());
+    return (Long)m.get("long");
   }
 
   private synchronized void cancelDelegationToken(final Token<?> token

Modified: hadoop/common/branches/branch-0.20-security-205/src/test/org/apache/hadoop/hdfs/web/TestJsonUtil.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-0.20-security-205/src/test/org/apache/hadoop/hdfs/web/TestJsonUtil.java?rev=1179172&r1=1179171&r2=1179172&view=diff
==============================================================================
--- hadoop/common/branches/branch-0.20-security-205/src/test/org/apache/hadoop/hdfs/web/TestJsonUtil.java
(original)
+++ hadoop/common/branches/branch-0.20-security-205/src/test/org/apache/hadoop/hdfs/web/TestJsonUtil.java
Wed Oct  5 11:37:04 2011
@@ -17,14 +17,16 @@
  */
 package org.apache.hadoop.hdfs.web;
 
+import java.util.Map;
+
 import org.apache.hadoop.fs.FileStatus;
 import org.apache.hadoop.fs.Path;
 import org.apache.hadoop.fs.permission.FsPermission;
 import org.apache.hadoop.hdfs.DFSUtil;
 import org.apache.hadoop.hdfs.protocol.HdfsFileStatus;
-import org.apache.hadoop.hdfs.web.JsonUtil;
 import org.junit.Assert;
 import org.junit.Test;
+import org.mortbay.util.ajax.JSON;
 
 public class TestJsonUtil {
   static FileStatus toFileStatus(HdfsFileStatus f, String parent) {
@@ -46,7 +48,7 @@ public class TestJsonUtil {
     System.out.println("fstatus = " + fstatus);
     final String json = JsonUtil.toJsonString(status);
     System.out.println("json    = " + json.replace(",", ",\n  "));
-    final HdfsFileStatus s2 = JsonUtil.toFileStatus(JsonUtil.parse(json));
+    final HdfsFileStatus s2 = JsonUtil.toFileStatus((Map<?, ?>)JSON.parse(json));
     final FileStatus fs2 = toFileStatus(s2, parent);
     System.out.println("s2      = " + s2);
     System.out.println("fs2     = " + fs2);



Mime
View raw message