import java.io.*;
import java.net.*;
import java.util.*;
import java.text.*;
//import javax.mail.*;

//import javax.activation.*;
//import javax.mail.internet.*;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.HttpClient;
import org.apache.util.HttpURL;
import org.apache.util.QName;
import org.apache.webdav.lib.Ace;
import org.apache.webdav.lib.Lock;
import org.apache.webdav.lib.Privilege;
import org.apache.webdav.lib.Property;
import org.apache.webdav.lib.PropertyName;
import org.apache.webdav.lib.ResponseEntity;
import org.apache.webdav.lib.WebdavResource;
import org.apache.webdav.lib.methods.DepthSupport;
import org.apache.webdav.lib.properties.AclProperty;
import org.apache.webdav.lib.properties.LockDiscoveryProperty;
import org.apache.webdav.lib.properties.PrincipalCollectionSetProperty;

import org.apache.webdav.lib.WebdavResource;
import org.apache.util.HttpURL;

 /**
  * This class is designed to put a file from the local filesystem to a WebDav Server
  * On the American Redhat Linux Server rented by ella-associates.org it is necessary 
  * to place the following paths on the java classpath
  *   /var/jakarta-slide-1.0.16/client/lib/webdav.jar:/var/jakarta-slide-1.0.16/client/lib/commons-httpclient.jar:
  *   /var/jakarta-slide-1.0.16/client/lib/webdavlib.jar:/var/www/utils/ 
  *
  *-------------------------------                 
  *<pre>
  *  Command line usage:                           
  *  java DebugDavFile TheWebDavLocationPath [TheLocalFile] [user] [password] [server-url]  
  *    The purpose of this program is to put a file from the local file system onto a WebDav server 
  *    A WebDav Server is similar to an HTTP server but it allows the user to edit the files on the 
  *    server rather than just looking at them                      
  *  The program requires two parameters: 
  *    TheLocalFile  This is the file on the local file system which you wish to put on the WebDav Server 
  *    TheWebDavLocationPath  This is the location on the WebDav server where you want to put the file 
  *      which you are uploading. This should be a path using forward slashes (eg /some/directory/)   
  *  
  *  There are other option parameters  
  *    user       
  *    password   
  *    webdav-server-url    
  *  
  *  Examples:              
  *  ---------              
  *    java GetDavFile shopping-list.xml /documents/thingsToDo/     
  *
  * Bugs:
  *   If you finish the remote file path with a forward slash / then the program seems to 
  *   'amalgamate' the two command line variables into one
  *</pre>  
  *  
  *  
  *  
  *  
  * This appears to be working (2 august 2003).  
  * @author mjb
  * @see "http://www.ella-associates.org/utils/GetDavFile.java"
  * @see "http://www.ella-associates.org/utils/PutDavFile.java"
  */
public class DebugDavFile 
{


  public static void main (String[]args) throws Exception
  {

    String sLocalFileName = "";
    String sLocalFileFullName = "";
    String sRemoteLocation = "";
    String sWebdavUser = "matthew";
    String sWebdavPassword = "bish";
    String sWebdavServer = "http://ella-associates.org:8081/";

    if (args.length == 1)
    {
      sRemoteLocation = args[0];
    }
    else if (args.length == 2)
    {
      sRemoteLocation = args[0];
      sLocalFileName = args[1];
    }
    else if (args.length == 4)
    {
      sRemoteLocation = args[0];
      sLocalFileName = args[1];
      sWebdavUser = args[2];
      sWebdavPassword = args[3];
    }
    else if (args.length == 5)
    {
      sRemoteLocation = args[0];
      sLocalFileName = args[1];
      sWebdavUser = args[2];
      sWebdavPassword = args[3];
      sWebdavServer = args[4];
    }
    else 
    {
      //-- The following attempts to display the javadoc from this file
      //--
      FileReader firTheFile = new FileReader ("/var/www/utils/GetDavFile.java");
      BufferedReader buffrTextFile = new BufferedReader (firTheFile);

      String sTextLine;

      sTextLine = buffrTextFile.readLine();
      while (sTextLine != null)
      {
	if (sTextLine.matches("\\s*\\*.*"))
	 { System.out.println(sTextLine); }

        sTextLine = buffrTextFile.readLine();
      }
      System.exit (-1);
       
    }  //-- if, else [Command Line Arguments]
  
     
   HttpURL httpURL = new HttpURL(sWebdavServer);
   httpURL.setUserInfo(sWebdavUser, sWebdavPassword);
   WebdavResource webdavResource = new WebdavResource(httpURL, sRemoteLocation);

   if (!webdavResource.exists())
   {
     System.out.println(
       "The resource [" + sRemoteLocation + "] which you specified to get from the \n" +
       "WebDav server [" + sWebdavServer + "] does not appear to exist. If you enter a directory \n" +
       "name you should see a list of resources in that directory. This may help you locate what \n" +
       "you are looking for");
     System.exit(-1);
     
   } //-- if 

   
   if (sLocalFileName == "") 
   {
     sLocalFileName = webdavResource.getDisplayName();
   } //-- if 
   
    

   // Used for debugging
   if (0 == 0)
   {
     System.out.println(
      "PROGRAM VARIABLES     \n" +
      "  sRemoteLocation=" + sRemoteLocation  + "\n" +
      "  sWebdavUser=" + sWebdavUser + "\n" +
      "  sWebdavPassword=" + sWebdavPassword + "\n" +
      "  sWebdavServer=" + sWebdavServer  + "\n" +
      "  \n"); 
   }

   //-- for debugging the webdav resource
   if (0 == 0)
   {
     StringBuffer sbInformation = new StringBuffer("");

     System.out.println("   webdavResource.exists()=" + webdavResource.exists());
     System.out.println("   webdavResource.getExistence()=" + webdavResource.getExistence());

     System.out.println("   webdavResource.getGetLastModified()=" + webdavResource.getGetLastModified() +
	"[" +  new Date(webdavResource.getGetLastModified()) + "]");
     Date dtLastModification = new Date(webdavResource.getGetLastModified());
     //SimpleDateFormat df = (SimpleDateFormat)SimpleDateFormat.getDateInstance();
     //System.out.println(df.format(dtLastModification));
     //System.out.println(dtLastModification);
   
     System.out.println("   webdavResource.getGetContentLength()=" + webdavResource.getGetContentLength());
     System.out.println("");

     System.out.println("PATH AND LOCATION INFORMATION");
     System.out.println("   webdavResource.getHost()=" + webdavResource.getHost());
     System.out.println("   webdavResource.getHttpURL()=" + webdavResource.getHttpURL());
     System.out.println("   webdavResource.getHttpURLExceptForUserInfo()=" + webdavResource.getHttpURLExceptForUserInfo());
     System.out.println("   webdavResource.getDisplayName()=" + webdavResource.getDisplayName());
     System.out.println("   webdavResource.getIsHidden()=" + webdavResource.getIsHidden());
     System.out.println("   webdavResource.getPath()=" + webdavResource.getPath());
     System.out.println("");

     System.out.println("LOCKING INFORMATION");
     System.out.println("   webdavResource.isLocked()=" + webdavResource.isLocked());
     Enumeration enActiveLockOwners = webdavResource.getActiveLockOwners();

     if (webdavResource.isLocked())
     {
       System.out.println("   webdavResource.getActiveLockOwners()=");
       while (enActiveLockOwners.hasMoreElements())
       {
         System.out.println(enActiveLockOwners.nextElement());
       }
     }	

     System.out.println("");
     System.out.println("COLLECTION INFORMATION");
     System.out.println("   webdavResource.getIsCollection()=" + webdavResource.getIsCollection());
     System.out.println("   webdavResource.isCollection()=" + webdavResource.isCollection());
     if (webdavResource.isCollection())
     {

       StringBuffer sbFolder = new StringBuffer();
       String[] aaFiles = webdavResource.list();
       for (int ii = 0; ii < aaFiles.length; ii++)
       {
	 sbFolder.append(aaFiles[ii] + "\n");
       }
       System.out.println("Children of this collection: " + sRemoteLocation);
       System.out.println(sbFolder);
       
     } //-- if 

     System.out.println("   webdavResource.getHost()=" + webdavResource.getHost());
     System.out.println("   webdavResource.getHost()=" + webdavResource.getHost());
     System.out.println("   webdavResource.getHost()=" + webdavResource.getHost());
     
   }


   try
   {

      //--The ella associates doc directory = /slide/files/documents/alexis_docs/
      //--
      System.out.print("Debugging WebDav Resource: " + sRemoteLocation);
      webdavResource.unlockMethod();
      if (webdavResource.unlockMethod())
      {
        System.out.println("The file unlock succeeded.");
      }
      else 
      {
	String sServerResponse = webdavResource.getStatusMessage();
	System.out.println("The file unlock failed. The following message was provided:");
        System.out.println(sServerResponse);
	if (sServerResponse.matches(".*Conflict.*409.*"))
	{
	  System.out.println(
	    "This message means that the location you specified was probably invalid");
	} //-- if is 'conflict'
      } //-- if, else
    }
    catch (Exception ex)
    {
      System.out.println(
	"An unspecified problem ocurred while unlocking. Sorry");
    }

   webdavResource.close();

   System.out.println("disconnected from the WebDav Server");
  }   // main
  
    
} // class: GetDavFile 

