You are viewing a plain text version of this content. The canonical link for it is here.
Posted to slide-dev@jakarta.apache.org by re...@locus.apache.org on 2000/11/29 08:00:04 UTC
cvs commit: jakarta-slide/src/webdav/client/src/org/apache/webdav/cmd Main.java
remm 00/11/28 23:00:04
Modified: src/webdav/client/src/org/apache/webdav/cmd Main.java
Log:
- Replace the client sample with a prototype of the command line client.
It does OPTIONS, MKCOL, GET and PUT. You can't browse the server
tree yet (using a "cd" command or something similar).
Contributed by Dave Bryson.
Revision Changes Path
1.2 +322 -59 jakarta-slide/src/webdav/client/src/org/apache/webdav/cmd/Main.java
Index: Main.java
===================================================================
RCS file: /home/cvs/jakarta-slide/src/webdav/client/src/org/apache/webdav/cmd/Main.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- Main.java 2000/11/22 06:19:08 1.1
+++ Main.java 2000/11/29 07:00:03 1.2
@@ -1,7 +1,7 @@
/*
- * $Header: /home/cvs/jakarta-slide/src/webdav/client/src/org/apache/webdav/cmd/Main.java,v 1.1 2000/11/22 06:19:08 remm Exp $
- * $Revision: 1.1 $
- * $Date: 2000/11/22 06:19:08 $
+ * $Header: /home/cvs/jakarta-slide/src/webdav/client/src/org/apache/webdav/cmd/Main.java,v 1.2 2000/11/29 07:00:03 remm Exp $
+ * $Revision: 1.2 $
+ * $Date: 2000/11/29 07:00:03 $
*
* ====================================================================
*
@@ -76,85 +76,348 @@
/**
* Command line WebDAV client.
*
+ *
* @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
+ * @author <a href="mailto:daveb@miceda-data.com">Dave Bryson</a>
+ * @version $Revision: 1.2 $
*/
public class Main {
+
+
+ // ----------------------------------------------------- Instance Variables
+
+
+ /**
+ * Table for startup parameters
+ */
+ Hashtable startUp = null;
+
+
+ /**
+ * Table for command actions
+ */
+ Hashtable actions = null;
+
+
+ /**
+ * Client
+ */
+ WebdavClient client = null;
+
+
+ // ------------------------------------------------------------ Constructor
+ /**
+ * Constructor
+ */
+ public Main()
+ {
+ // Set some defaults
+ startUp = new Hashtable();
+ startUp.put("host", "127.0.0.1");
+ startUp.put("port", new Integer("8080"));
+ startUp.put("user", "guest");
+ startUp.put("password", "guest" );
+
+ client = new WebdavClient();
+ client.setDebug(1);
+ }
+
+
// --------------------------------------------------------- Public Methods
/**
* Main function.
*/
- public static void main(String args[]) {
+ public static void main( String args[] )
+ {
+ boolean ready = false;
- try {
-
- String host = "127.0.0.1";
- int port = 80;
-
- WebdavClient client = new WebdavClient();
-
- // Parsing host and port from command line
- if (args.length >= 2) {
- host = args[0];
- port = Integer.parseInt(args[1]);
+ Main m = new Main();
+ ready = m.processStartUp( args );
+ final WebdavClient cl = m.getClient();
+ if ( ready )
+ {
+ // Process command line requests
+ // Once the client has started
+ Thread inner =
+ new Thread(
+ new Runnable()
+ {
+ public void run()
+ {
+ Main.processCommands( cl );
+ }
+
+ }
+ );
+ inner.start();
+ }
+ }
+
+
+ // -------------------------------------------------------- Private Methods
+
+
+ private static void processCommands( final WebdavClient client )
+ {
+ try
+ {
+ BufferedReader in =
+ new BufferedReader(new InputStreamReader(System.in));
+ String command = null;
+ String commandPrompt = "[Slide]$ ";
+
+ do
+ {
+ System.out.print( commandPrompt );
+ command = in.readLine();
+
+ if( command == null || command.length() == 0 )
+ {
+ // Print commandLine Usage
+ printCommandUsage();
+ continue;
+ }
+
+ command = command.trim();
+ StringTokenizer st = new StringTokenizer(command);
+ String todo = st.nextToken();
+ String params = "";
+
+ if( st.hasMoreTokens() )
+ params = st.nextToken();
+
+ System.out.println( todo );
+
+ if( todo.equalsIgnoreCase("quit") )
+ {
+ break;
+ }
+ else if( todo.equalsIgnoreCase("-help") )
+ {
+ printCommandUsage();
+ }
+ else if( todo.equalsIgnoreCase("get") )
+ {
+ GetMethod gm = new GetMethod();
+
+ if( params == null || params.length() == 0 )
+ {
+ System.out.println("get <uri to file to fetch>");
+ }
+ else
+ {
+ gm.setPath( params );
+ client.executeMethod( gm );
+ System.out.println( "Status: " + gm.getStatusCode() );
+ System.out.println();
+ System.out.println( gm.getDataAsString() );
+ }
+ }
+ else if( todo.equalsIgnoreCase("put") )
+ {
+ PutMethod pm = new PutMethod();
+
+ if( params == null || params.length() == 0 )
+ {
+ System.out.println("put <uri to file to upload>");
+ }
+ else
+ {
+ pm.setPath( "/slide/files" );
+ pm.sendData( new File( params ) );
+ client.executeMethod( pm );
+ System.out.println( "Status: " + pm.getStatusCode() );
+ System.out.println();
+ }
+ }
+ else if( todo.equalsIgnoreCase("mkcol") )
+ {
+ MkcolMethod mc = new MkcolMethod();
+
+ if( params == null || params.length() == 0 )
+ {
+ System.out.println("mkcol <uri to make>");
+ }
+ else
+ {
+ mc.setPath( params );
+ client.executeMethod( mc );
+ System.out.println( "Status: " + mc.getStatusCode() );
+ System.out.println();
+ }
+ }
+ else if( todo.equalsIgnoreCase("options") )
+ {
+ OptionsMethod mc = new OptionsMethod();
+
+ if( params == null || params.length() == 0 )
+ {
+ System.out.println("options <uri>");
+ }
+ else
+ {
+ mc.setPath( params );
+ client.executeMethod( mc );
+ System.out.println( "Status: " + mc.getStatusCode() );
+ // Allowed methods
+ Enumeration enum = mc.getAllowedMethods();
+ System.out.print("Allowed methods: ");
+ while (enum.hasMoreElements()) {
+ System.out.print((String) enum.nextElement());
+ if (enum.hasMoreElements())
+ System.out.print(", ");
+ }
+ System.out.println();
+ // DAV capabilities
+ enum = mc.getDavCapabilities();
+ System.out.print("DAV capabilities: ");
+ while (enum.hasMoreElements()) {
+ System.out.print((String) enum.nextElement());
+ if (enum.hasMoreElements())
+ System.out.print(", ");
+ }
+ System.out.println();
+ }
+ }
+ }
+ while (true );
+
+ }
+ catch( Exception pce )
+ {
+ System.out.println("Error processing a command: " + pce );
+ }
+ }
+
+
+ /**
+ * Process the initial args passed to the Class.<br>
+ * Currently this should consist of:<br>
+ *
+ * -host [the host to connect to]</br>
+ * -port [the port the host is running webdav on]<br>
+ * -u [username]<br>
+ * -p [password]<br>
+ *
+ * There are default set if anything is missing.<br>
+ *
+ * If all goes well, a session will be created and credentials set.
+ */
+ private boolean processStartUp( String[] args )
+ {
+ int argsIndex = 0;
+ boolean ready = false;
+ try
+ {
+ while ( argsIndex < args.length )
+ {
+ String input = args[ argsIndex ++ ];
+
+ if ( input.equalsIgnoreCase( "-help" ) )
+ {
+ printStartUsage();
+ return false;
+ }
+ else if ( input.equalsIgnoreCase( "-host" ) )
+ {
+ startUp.put("host", args[ argsIndex++ ]);
+ }
+ else if ( input.equalsIgnoreCase( "-port" ) )
+ {
+ int val;
+ try
+ {
+ val = Integer.parseInt( args[ argsIndex++ ] );
+ }
+ catch ( NumberFormatException ne )
+ {
+ printStartUsage();
+ return false;
+ }
+ startUp.put("port", new Integer(val) );
+ }
+ else if ( input.equalsIgnoreCase( "-u" ) )
+ {
+ startUp.put("user", args[ argsIndex++ ] );
+ }
+ else if (input.equalsIgnoreCase( "-p" ) )
+ {
+ startUp.put("password", args[ argsIndex++ ] );
+ }
+ else
+ {
+ printStartUsage();
+ return false;
+ }
}
- client.startSession(host, port);
+ // Start session with client
+ String h = (String)startUp.get("host");
+ int p = ( (Integer)startUp.get("port") ).intValue();
+ client.startSession(h, p);
// Set the credentials to use
- Credentials credentials = new Credentials("root", "root");
+ String un = (String)startUp.get("user");
+ String pn = (String)startUp.get("password");
+ Credentials credentials = new Credentials(un,pn);
client.setCredentials(credentials);
-
- WebdavMethod method = new OptionsMethod();
- method.setPath("/");
- client.executeMethod(method);
-
- System.out.println("Response :");
- System.out.println("Status code : " + method.getStatusCode());
- System.out.println("Status text : " + method.getStatusText());
-
-/* GetMethod method2 = new GetMethod();
- method2.setPath("/examples/servlet/HelloWorldExample");
- method2.setUseDisk(false);
- client.executeMethod(method2);
-
- System.out.println("Response :");
- System.out.println("Status code : " + method2.getStatusCode());
- System.out.println("Status text : " + method2.getStatusText());
-
- System.out.println("Contents :");
- /*
- is = method2.getData();
- nb = 0;
- while (true) {
- nb = is.read(buffer);
- if (nb == -1)
- break;
- System.out.println(new String(buffer, 0, nb));
- }
- is.close();
- */
- //System.out.println(method2.getDataAsString());
-
-
- PropPatchMethod method3 = new PropPatchMethod();
- method3.setPath("/test");
- method3.addPropertyToSet("password", "foo");
- method3.addPropertyToSet("getlastmodified", "foo");
- method3.addPropertyToRemove("getetag");
- client.executeMethod(method3);
- System.out.println("Response :");
- System.out.println("Status code : " + method3.getStatusCode());
- System.out.println("Status text : " + method3.getStatusText());
- } catch (Throwable t) {
- t.printStackTrace();
+ // Ready for interactive commandline
+ ready = true;
}
+ catch( Exception se )
+ {
+ System.out.println("Error on startup: " + se + "\n");
+ printStartUsage();
+ ready = false;
+ }
+ return ready;
+ }
+
+
+ private static void printStartUsage()
+ {
+ System.out.println("WebDAV Client Startup args:");
+ System.out.println(" -host <host to connect to>");
+ System.out.println(" -port <post to conect on>");
+ System.out.println(" -u <username>");
+ System.out.println(" -p <password>");
+ System.out.println(" -help");
+ }
+
+
+ private static void printCommandUsage()
+ {
+ System.out.println("WebDAV Client commands:");
+ System.out.println(" quit");
+ System.out.println(" get <uri of file to fetch>");
+ System.out.println(" put <uri of file to upload>");
+ System.out.println(" options <uri>");
+ }
+
+
+ private WebdavClient getClient()
+ {
+ return client;
+ }
+
+
+ private static void pause(int secs)
+ {
+ try
+ {
+ Thread.sleep( secs * 1000 );
+ }
+ catch( InterruptedException ex )
+ {
+ }
}
}
+