Beispiel #1
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();
  }