// Program to upload the file into the repository

import org.apache.slide.webdav.method.*;

import java.security.Principal;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.util.XMLPrinter;
import org.apache.util.WebdavStatus;
import org.apache.slide.common.*;
import org.apache.slide.webdav.*;
import org.apache.slide.macro.*;
import org.apache.slide.lock.*;
import org.apache.slide.content.*;
import org.apache.slide.security.AccessDeniedException;
import org.apache.slide.structure.*;
import org.apache.slide.authenticate.CredentialsToken;
import org.apache.slide.authenticate.SecurityToken;



import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.net.*;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class slidetest extends HttpServlet 
{
    String src;
    String dest;
    PrintWriter out;
    public void doGet(HttpServletRequest req, HttpServletResponse res) 
    throws ServletException, IOException 
    {
        doPost(req,res);
    }        
    public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        
        res.setContentType("text/html");

        out =  res.getWriter();

      NamespaceAccessToken token = null;

	token = Domain.accessNamespace(new SecurityToken(new String()),"webdav");
	GenericServlet servlet;
	try
	{
		new org.apache.slide.webdav.method.PutMethod(this,token,req,res);
	}
	catch(Exception ex)
	{
		out.println(ex);
	}
	
        out.println("<b><font color=red>Helloooooooooooooooooooooooooooo");

/*
            try {  

            src = "c:/temp.xml";
            dest = "http://localhost:8080/slide/files/vijay.txt";
                    
            UploadValidation validity = new UploadValidation(src,dest);

        }    
        catch (Exception e) {
            out.println("<PRE> Here");
            e.printStackTrace(out);
            out.println("</PRE>");
        }
*/
    }
}   //*** End of class - Upload ***//

          

//--- Class to validate the source and destination URLs ---//

class UploadValidation
{

    private File srcFile, srcDir;
    private File destFile,destDir;
    
   
    public UploadValidation(String src,String dest) throws IOException {   
        
        if (src.equals(""))
            throw new IllegalArgumentException("Source Directory cannot be null");
    
        srcFile = new File(src);
        if(!srcFile.exists())
            throw new IllegalArgumentException("Source file not found: " + src);
        
        if (dest.equals(""))
            throw new IllegalArgumentException("Destination Directory cannot be null");
        
        destFile = new File(dest);
        if(destFile.exists())
            throw new IllegalArgumentException("Destination file already exists: " + dest);
        
        // Check destination directory (dest) is truly a directory
        // This try..catch is to avoid null pointer exception 
/*        try { 

          destDir = new File(destFile.getParent());
          if (!destDir.isDirectory())
              throw new IllegalArgumentException("Invalid destination directory: " + dest);

        }
        catch (Exception e) {
            throw new IllegalArgumentException("Invalid destination directory: " + dest);
        }
*/

        // Write the file contents from src to the destination directory (dest)
        try
        {
            UpreadAndSaveFile rsf = new UpreadAndSaveFile(src,dest);
        }
        catch(Exception Ex)
        {
            System.out.println(Ex);
        }
        finally
        {
            System.out.println("Reaches the root");
        }
        
    }
    
}    //*** End of class - UploadValidation ***//
    
    
    
//--- Class to save the file into the destination URL ---//

 class UpreadAndSaveFile 
 {
    private File f1, f2;
    private FileInputStream fi1;
    private FileOutputStream fi2;
    private BufferedInputStream inStream;
    private BufferedOutputStream outStream;
        
    boolean eof = false;
    int byteCount = 0;

    public UpreadAndSaveFile(String src, String dest) throws Exception 
    {

        f1 = new File(src);
        fi1 = new FileInputStream(f1);
        inStream = new BufferedInputStream(fi1);
        
        f2 = new File("vijay.txt");
        fi2 = new FileOutputStream(f2);
        outStream = new BufferedOutputStream(fi2);
        
        URL loc = new URL("http://localhost:8080/vijay/vijay.txt");
        URLConnection Ucon = loc.openConnection();
	Ucon.setDoOutput(true);
        OutputStream os = Ucon.getOutputStream();
        
        while(!eof) 
        {
            int c= inStream.read();
            if(c==-1) eof = true;
            else 
            {
                try
                {
                    os.write(1);
                    os.write((char) c);
                }
                catch(Exception ex)
                {
                    System.out.println(ex);
                }
                
                System.out.print((char) c);
                ++byteCount;
            }
        }

        inStream.close();
        outStream.close();
        fi1.close();
        fi2.close();
    }
}
/*
 class UpreadAndSaveFile 
 {
    private File f1, f2;
    private FileInputStream fi1;
    private FileOutputStream fi2;
    private BufferedInputStream inStream;
    private BufferedOutputStream outStream;
        
    boolean eof = false;
    int byteCount = 0;
    
    public UpreadAndSaveFile(String src, String dest) throws IOException 
    {
        f1 = new File(src);
        fi1 = new FileInputStream(f1);
        inStream = new BufferedInputStream(fi1);
        
        f2 = new File(dest);
        fi2 = new FileOutputStream(f2);
        outStream = new BufferedOutputStream(fi2);
        
        while(!eof) 
        {
            int c= inStream.read();
            if(c==-1) eof = true;
            else 
            {
                outStream.write((char) c);
                ++byteCount;
            }
        }

        inStream.close();
        outStream.close();
        fi1.close();
        fi2.close();
    }
}
 */    
 //*** End of class - UpreadAndSaveFile ***//