示例#1
0
  /**
   * Contact the remote WebDAV server and do a PROPFIND lookup, returning a {@link List} of all
   * scavenged resources.
   */
  private List propfind(Location location) throws IOException {
    /* Create the new HttpClient instance associated with the location */
    final HttpClient client = new HttpClient(location);
    client.addRequestHeader("Depth", "1");
    client.setAcceptableStatus(207).connect("PROPFIND", true);

    /* Get the XML SAX Parser and parse the output of the PROPFIND */
    try {
      final SAXParserFactory factory = SAXParserFactory.newInstance();
      factory.setValidating(false);
      factory.setNamespaceAware(true);
      final SAXParser parser = factory.newSAXParser();
      final String systemId = location.toString();
      final InputSource source = new InputSource(systemId);
      final Handler handler = new Handler(location);
      source.setByteStream(client.getResponseStream());
      parser.parse(source, handler);
      return handler.list;

    } catch (ParserConfigurationException exception) {
      Exception throwable = new IOException("Error creating XML parser");
      throw (IOException) throwable.initCause(exception);
    } catch (SAXException exception) {
      Exception throwable = new IOException("Error creating XML parser");
      throw (IOException) throwable.initCause(exception);
    } finally {
      client.disconnect();
    }
  }
示例#2
0
  /** Contact the remote WebDAV server and fetch all properties. */
  private void reload(Location location) throws IOException {

    /* Do an OPTIONS over onto the location */
    location = this.options(location);

    /* Do a PROPFIND to figure out the properties and the children */
    final Iterator iterator = this.propfind(location).iterator();
    final Map children = new HashMap();
    while (iterator.hasNext()) {
      final Resource resource = (Resource) iterator.next();
      final Path path = resource.location.getPath();
      if (path.size() == 0) {
        resource.location = location.resolve(resource.location);
        this.resource = resource;
      } else if (path.size() == 1) {
        final Path.Element element = (Path.Element) path.get(0);
        if ("..".equals(element.getName())) continue;
        children.put(element.toString(), resource);
      }
    }

    /* Check if the current resource was discovered */
    if (this.resource == null) throw new IOException("Current resource not returned in PROOPFIND");

    /* Don't actually allow resources to be modified */
    this.children = Collections.unmodifiableMap(children);
  }
示例#3
0
  /**
   * A command-line interface to a WebDAV repository.
   *
   * <p>When invoked from the command line, this class requires one only argument, the URL location
   * of the WebDAV repository to connect to.
   *
   * <p>After connection this method will interact with the user using an extremely simple
   * console-based interface.
   */
  public static void main(String args[]) throws IOException {
    final InputStreamReader r = new InputStreamReader(System.in);
    final BufferedReader in = new BufferedReader(r);
    WebDavClient client = new WebDavClient(Location.parse(args[0]));

    while (true)
      try {
        System.out.print("[" + client.getLocation() + "] -> ");
        args = parse(in.readLine());
        if (args == null) break;
        if (args[0].equals("list")) {
          if (args[1] == null) list(client, System.out);
          else list(client.open(args[1]), System.out);

        } else if (args[0].equals("refresh")) {
          client = client.refresh();

        } else if (args[0].equals("get")) {
          if (args[1] != null) {
            final InputStream input = client.get(args[1]);
            final File file = new File(args[2]).getCanonicalFile();
            final OutputStream output = new FileOutputStream(file);
            final long bytes = StreamTools.copy(input, output);
            System.out.println(
                "Fetched child \"" + args[1] + "\" to file \"" + file + "\" (" + bytes + " bytes)");
          } else System.out.print("Can't \"get\" null");

        } else if (args[0].equals("put")) {
          if (args[1] != null) {
            final File file = new File(args[1]).getCanonicalFile();
            final InputStream input = new FileInputStream(file);
            final OutputStream output = client.put(args[2], file.length());
            final long bytes = StreamTools.copy(input, output);
            System.out.println(
                "Uploaded file \""
                    + file
                    + "\" to child \""
                    + args[2]
                    + "\" ("
                    + bytes
                    + " bytes)");
          } else System.out.print("Can't \"put\" null");

        } else if (args[0].equals("mkcol")) {
          if (args[1] != null) {
            client.mkcol(args[1]);
            System.out.println("Created \"" + args[1] + "\"");
          } else System.out.print("Can't \"mkcol\" null");

        } else if (args[0].equals("delete")) {
          if (args[1] != null) {
            client.delete(args[1]);
            System.out.println("Deleted \"" + args[1] + "\"");
          } else System.out.print("Can't \"delete\" null");

        } else if (args[0].equals("cd")) {
          if (args[1] != null) client = client.open(args[1]);
          else System.out.print("Can't \"cd\" to null");

        } else if (args[0].equals("quit")) {
          break;

        } else {
          System.out.print("Invalid command \"" + args[0] + "\". ");
          System.out.println("Valid commands are:");
          System.out.println(" - \"list\"    list the children child");
          System.out.println(" - \"get\"     fetch the specified child");
          System.out.println(" - \"put\"     put the specified child");
          System.out.println(" - \"mkcol\"   create a collection");
          System.out.println(" - \"delete\"  delete a child");
          System.out.println(" - \"put\"     put the specified resource");
          System.out.println(" - \"cd\"      change the location");
          System.out.println(" - \"refresh\" refresh this location");
          System.out.println(" - \"quit\"    quit this application");
        }
      } catch (Exception exception) {
        exception.printStackTrace(System.err);
      }
    System.err.println();
  }