static DatanodeInfo chooseDatanode(
      final NameNode namenode,
      final String path,
      final HttpOpParam.Op op,
      final long openOffset,
      final long blocksize,
      Configuration conf)
      throws IOException {
    final BlockManager bm = namenode.getNamesystem().getBlockManager();

    if (op == PutOpParam.Op.CREATE) {
      // choose a datanode near to client
      final DatanodeDescriptor clientNode =
          bm.getDatanodeManager().getDatanodeByHost(getRemoteAddress());
      if (clientNode != null) {
        final DatanodeDescriptor[] datanodes =
            bm.getBlockPlacementPolicy().chooseTarget(path, 1, clientNode, null, blocksize);
        if (datanodes.length > 0) {
          return datanodes[0];
        }
      }
    } else if (op == GetOpParam.Op.OPEN
        || op == GetOpParam.Op.GETFILECHECKSUM
        || op == PostOpParam.Op.APPEND) {
      // choose a datanode containing a replica
      final NamenodeProtocols np = namenode.getRpcServer();
      final HdfsFileStatus status = np.getFileInfo(path);
      if (status == null) {
        throw new FileNotFoundException("File " + path + " not found.");
      }
      final long len = status.getLen();
      if (op == GetOpParam.Op.OPEN) {
        if (openOffset < 0L || (openOffset >= len && len > 0)) {
          throw new IOException(
              "Offset="
                  + openOffset
                  + " out of the range [0, "
                  + len
                  + "); "
                  + op
                  + ", path="
                  + path);
        }
      }

      if (len > 0) {
        final long offset = op == GetOpParam.Op.OPEN ? openOffset : len - 1;
        final LocatedBlocks locations = np.getBlockLocations(path, offset, 1);
        final int count = locations.locatedBlockCount();
        if (count > 0) {
          return JspHelper.bestNode(locations.get(0).getLocations(), false, conf);
        }
      }
    }

    return (DatanodeDescriptor)
        bm.getDatanodeManager().getNetworkTopology().chooseRandom(NodeBase.ROOT);
  }
  private Response get(
      final UserGroupInformation ugi,
      final DelegationParam delegation,
      final UserParam username,
      final DoAsParam doAsUser,
      final String fullpath,
      final GetOpParam op,
      final OffsetParam offset,
      final LengthParam length,
      final RenewerParam renewer,
      final BufferSizeParam bufferSize)
      throws IOException, URISyntaxException {
    final NameNode namenode = (NameNode) context.getAttribute("name.node");
    final NamenodeProtocols np = namenode.getRpcServer();

    switch (op.getValue()) {
      case OPEN:
        {
          final URI uri =
              redirectURI(
                  namenode,
                  ugi,
                  delegation,
                  username,
                  doAsUser,
                  fullpath,
                  op.getValue(),
                  offset.getValue(),
                  -1L,
                  offset,
                  length,
                  bufferSize);
          return Response.temporaryRedirect(uri).type(MediaType.APPLICATION_OCTET_STREAM).build();
        }
      case GET_BLOCK_LOCATIONS:
        {
          final long offsetValue = offset.getValue();
          final Long lengthValue = length.getValue();
          final LocatedBlocks locatedblocks =
              np.getBlockLocations(
                  fullpath, offsetValue, lengthValue != null ? lengthValue : Long.MAX_VALUE);
          final String js = JsonUtil.toJsonString(locatedblocks);
          return Response.ok(js).type(MediaType.APPLICATION_JSON).build();
        }
      case GETFILESTATUS:
        {
          final HdfsFileStatus status = np.getFileInfo(fullpath);
          if (status == null) {
            throw new FileNotFoundException("File does not exist: " + fullpath);
          }

          final String js = JsonUtil.toJsonString(status, true);
          return Response.ok(js).type(MediaType.APPLICATION_JSON).build();
        }
      case LISTSTATUS:
        {
          final StreamingOutput streaming = getListingStream(np, fullpath);
          return Response.ok(streaming).type(MediaType.APPLICATION_JSON).build();
        }
      case GETCONTENTSUMMARY:
        {
          final ContentSummary contentsummary = np.getContentSummary(fullpath);
          final String js = JsonUtil.toJsonString(contentsummary);
          return Response.ok(js).type(MediaType.APPLICATION_JSON).build();
        }
      case GETFILECHECKSUM:
        {
          final URI uri =
              redirectURI(
                  namenode, ugi, delegation, username, doAsUser, fullpath, op.getValue(), -1L, -1L);
          return Response.temporaryRedirect(uri).type(MediaType.APPLICATION_OCTET_STREAM).build();
        }
      case GETDELEGATIONTOKEN:
        {
          if (delegation.getValue() != null) {
            throw new IllegalArgumentException(delegation.getName() + " parameter is not null.");
          }
          final Token<? extends TokenIdentifier> token =
              generateDelegationToken(namenode, ugi, renewer.getValue());
          final String js = JsonUtil.toJsonString(token);
          return Response.ok(js).type(MediaType.APPLICATION_JSON).build();
        }
      case GETHOMEDIRECTORY:
        {
          final String js =
              JsonUtil.toJsonString(
                  org.apache.hadoop.fs.Path.class.getSimpleName(),
                  WebHdfsFileSystem.getHomeDirectoryString(ugi));
          return Response.ok(js).type(MediaType.APPLICATION_JSON).build();
        }
      default:
        throw new UnsupportedOperationException(op + " is not supported");
    }
  }