Example #1
0
  public CmdResult list(String path, int depth, boolean detailed) throws HoneycombTestException {
    CmdResult cr = new CmdResult();
    LinkedList files = new java.util.LinkedList();

    // set up query
    StringBuffer sb = new StringBuffer();
    sb.append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
    sb.append("<D:propfind XMLNS:D=\"DAV:\">");
    if (detailed) sb.append("<D:allprop/>"); // names & values
    else sb.append("<D:propname/>"); // names only
    sb.append("</D:propfind>");
    byte[] qbuf = getUTF8Bytes(sb.toString());

    NVPair[] headers = new NVPair[4];
    headers[0] = CLIENT;

    if (depth < 0) headers[1] = new NVPair("depth", "infinity");
    else headers[1] = new NVPair("depth", Integer.toString(depth));

    headers[2] = new NVPair("Content-type", "text/xml");
    headers[3] = new NVPair("Content-length", Integer.toString(qbuf.length));

    try {
      //
      //  query
      //
      long t1 = System.currentTimeMillis();
      HTTPResponse rsp = conn.ExtensionMethod("PROPFIND", path, qbuf, headers);
      cr.time = System.currentTimeMillis() - t1;

      if (rsp.getStatusCode() >= 300) throw makeTestException("list", path, rsp);

      cr.pass = true;

      //
      //  parse response
      //
      cr.string = new String(rsp.getData());
      SAXBuilder builder = new SAXBuilder();
      Document doc = builder.build(new StringReader(cr.string));
      List l = doc.getContent();
      Iterator it = l.iterator();
      Element root = null;
      int ct = 0;
      while (it.hasNext()) {
        ct++;
        root = (Element) it.next();
      }
      if (ct != 1) throw new HoneycombTestException("Expected 1 content element, got " + ct);
      if (!root.getName().equals("multistatus"))
        throw new HoneycombTestException("Expected multistatus, got " + root.getName());

      l = root.getAttributes();
      if (l.size() != 0) throw new HoneycombTestException("Unexpected: root elt has attributes");

      cr.count = 0;
      cr.list = new java.util.LinkedList();
      l = root.getChildren();
      it = l.iterator();
      while (it.hasNext()) {
        cr.count++;
        Element e = (Element) it.next();
        if (!e.getName().equals("response"))
          throw new HoneycombTestException("Unexpected: child: " + e.getName());
        //
        //  parse this response element
        //
        //  response.href
        Element href = e.getChild("href", XMLNS);
        if (href == null) throw new HoneycombTestException("No href: " + e);
        String epath = href.getValue();
        if (epath == null) throw new HoneycombTestException("empty href: " + e);
        int ix = epath.indexOf("/webdav");
        if (ix == -1) throw new HoneycombTestException("href missing '/webdav': " + e);
        epath = epath.substring(ix);
        // Log.INFO("href.path: " + epath);

        //  response.propstat
        Element propstat = e.getChild("propstat", XMLNS);
        if (propstat == null) throw new HoneycombTestException("No propstat (" + epath + "): " + e);
        //  response.propstat.status
        Element status = propstat.getChild("status", XMLNS);
        if (status == null) throw new HoneycombTestException("No status (" + epath + "): " + e);
        String estatus = status.getValue();
        if (!estatus.equals("HTTP/1.1 200 OK"))
          throw new HoneycombTestException("Bad status (" + estatus + "): " + e);
        //  response.propstat.prop
        Element prop = propstat.getChild("prop", XMLNS);
        if (prop == null) throw new HoneycombTestException("No prop (" + epath + "): " + e);
        boolean isCollection = false;
        Element rtype = prop.getChild("resourcetype", XMLNS);
        if (rtype != null) {
          Element coll = rtype.getChild("collection", XMLNS);
          if (coll == null)
            throw new HoneycombTestException("Resourcetype != collection: " + rtype);
          isCollection = true;
        }
        String ecttype = null;
        Element cttype = prop.getChild("getcontenttype", XMLNS);
        if (cttype == null) Log.WARN("No getcontenttype: " + epath);
        else ecttype = cttype.getValue();
        String emode = null;
        Element mode = prop.getChild("mode", null);
        if (mode != null) emode = mode.getValue();
        Element displayname = prop.getChild("displayname", XMLNS);
        if (displayname == null)
          throw new HoneycombTestException("displayname is null (" + epath + "): " + e);
        String edisplayname = displayname.getValue();
        Element create = prop.getChild("creationdate", XMLNS);
        if (create == null)
          throw new HoneycombTestException("creationdate is null (" + epath + "): " + e);
        String ecreate = create.getValue();
        Element length = prop.getChild("getcontentlength", XMLNS);
        if (length == null)
          throw new HoneycombTestException("getcontentlength is null (" + epath + "): " + e);
        long llength = Long.parseLong(length.getValue());

        // different for dir vs. node
        String eoid = null;
        String lastmod = null;
        int uid = -1;
        int gid = -1;
        if (!isCollection) {
          Element el = prop.getChild("hc-oid", XMLNS);
          if (el != null) eoid = el.getValue();
          el = prop.getChild("getlastmodified", XMLNS);
          if (el == null)
            throw new HoneycombTestException(
                "Not collection but no getlastmodified (" + epath + "): " + e);
          lastmod = el.getValue();
          el = prop.getChild("uid", null);
          if (el != null) uid = Integer.parseInt(el.getValue());
          el = prop.getChild("gid", null);
          if (el != null) gid = Integer.parseInt(el.getValue());
        }

        //
        //  consistency checks
        //
        if (isCollection && (ecttype == null || !ecttype.equals("httpd/unix-directory")))
          throw new HoneycombTestException(
              "Collection but getcontenttype is " + ecttype + " (" + epath + "): " + e);
        //
        //  add to list
        //
        ListResponse lr =
            new ListResponse(
                epath,
                isCollection,
                ecttype,
                edisplayname,
                emode,
                ecreate,
                llength,
                eoid,
                lastmod,
                uid,
                gid);
        if (isCollection) cr.list.add(lr);
        else files.add(lr);
      }
    } catch (HoneycombTestException e) {
      throw e;
    } catch (Exception e) {
      // Log.ERROR("ex: " + e);
      // e.printStackTrace();
      cr.pass = false;
      cr.addException(e);
    }

    //
    //  sort files into directories => note that
    //  directories are not nested
    //
    Iterator it = files.iterator();
    while (it.hasNext()) {
      ListResponse lr = (ListResponse) it.next();
      //
      //  '/' is escaped in attributes, so lastIndexOf('/')
      //  is really the end of the dirs and not e.g. a mimetime
      //  '/' as in "app/xxx".
      //
      String dirPath = lr.path.substring(0, lr.path.lastIndexOf('/') + 1);
      Iterator it2 = cr.list.iterator();
      while (it2.hasNext()) {
        ListResponse lr2 = (ListResponse) it2.next();
        if (lr2.path.equals(dirPath)) {
          it.remove();
          lr2.addFile(lr);
          break;
        }
      }
    }
    // shouldn't be any files left over, but just in case
    if (files.size() > 0) {
      if (cr.list.size() > 0) {
        it = files.iterator();
        while (it.hasNext()) {
          ListResponse lr = (ListResponse) it.next();
          Log.INFO("orphan: " + lr.path);
        }
      }
      cr.list.addAll(files);
    }
    return cr;
  }