Diff for /edoc-applet/Upload.java between versions 1.3 and 1.4

version 1.3, 2003/06/25 11:50:09 version 1.4, 2003/07/21 10:14:15
Line 3 Line 3
 //    //  
 //  //
 //  Created by rogo on Wed Jan 29 2003.  //  Created by rogo on Wed Jan 29 2003.
 //  Copyright (c) 2001 __MyCompanyName__. All rights reserved.  //  Copyright (c) 2003 Rogo Soft. All rights reserved.
 //  //
  import com.exploringxml.xml.*;   import com.exploringxml.xml.*;
  import java.io.*;   import java.io.*;
Line 20  public class Upload extends JApplet impl Line 20  public class Upload extends JApplet impl
 {  {
   static File startFile;    static File startFile;
   static int count = 0;    static int count = 0;
   static JFileChooser fileChooser;    static JFileChooser fileChooser,downloadChooser;
   static String url;    static String url;
   static long id = 0L;    static long id = 0L;
   static Hashtable connections=new Hashtable();    static Hashtable connections=new Hashtable();
   static String col = "11"; // Collection /Institute    static String col = "11"; // Collection /Institute
   static String grp ="141"; // Group or Bundle    static String grp ="136"; // Group or Bundle
   static String ses ="";    static String ses ="";
   static Bundle bundle;    static Bundle bundle;
   static Bundle bundleRef;    static Bundle bundleRef;
Line 40  public class Upload extends JApplet impl Line 40  public class Upload extends JApplet impl
   Upload uploader = this;    Upload uploader = this;
   static boolean runningAsApplet=true;    static boolean runningAsApplet=true;
   static boolean debug=false;    static boolean debug=false;
     static boolean uploadMode = true;
   static String  rootNode = "edoc";    static String  rootNode = "edoc";
   public Upload()     public Upload() 
   {    {
Line 64  public class Upload extends JApplet impl Line 65  public class Upload extends JApplet impl
     // prop.put("http.proxyHost","proxy.unibe.ch");      // prop.put("http.proxyHost","proxy.unibe.ch");
     // prop.put("proxyPort", "80");       // prop.put("proxyPort", "80"); 
      // prop.put("java.protocol.handler.pkgs","HTTPClient");       // prop.put("java.protocol.handler.pkgs","HTTPClient");
      fileChooser=null;       fileChooser=null;download=null;
     if(!runningAsApplet)      if(!runningAsApplet)
     {      {
       Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();        Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
       Point p=new Point((screenSize.width-300)/2,(screenSize.height-180)/2);        Point p=new Point((screenSize.width-700)/2,(screenSize.height-80)/2);
       JFrame testFrame=new JFrame();        JFrame testFrame=new JFrame();
       testFrame.setTitle("Archimedes Applet");        testFrame.setTitle("Archimedes Applet");
       testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       testFrame.getContentPane().add(this);        testFrame.getContentPane().add(this);
       testFrame.setSize(300,180);        testFrame.setSize(300,80);
       testFrame.setLocation(p);        testFrame.setLocation(p);
       testFrame.setVisible(true);        testFrame.setVisible(true);
       Console console = new Console(testFrame);        Console console = new Console(testFrame);
Line 134  public class Upload extends JApplet impl Line 135  public class Upload extends JApplet impl
     if (e.getActionCommand().equals("Download"))      if (e.getActionCommand().equals("Download"))
     {      {
   
         if (downloadChooser == null)
         {
           downloadChooser = new JFileChooser();
           downloadChooser.setFileSelectionMode(fileChooser.FILES_AND_DIRECTORIES);
           downloadChooser.setFileFilter(new XMLFilter());
           downloadChooser.setDialogType(JFileChooser.SAVE_DIALOG);
           downloadChooser.addActionListener(new ActionListener()
           {
             public void actionPerformed(ActionEvent e2)
             {
               System.out.println(e2.getActionCommand());
               if (!e2.getActionCommand().toString().equals("ApproveSelection")) return;
               // {
   
      Thread thread = new Thread()       Thread thread = new Thread()
      {       {
         public void run()          public void run()
         {          {
                  if(runningAsApplet)
                   set();
             UploadInfo dialog = new UploadInfo();              UploadInfo dialog = new UploadInfo();
                   
             File startFile=new File(".");              File startFile=downloadChooser.getSelectedFile();
             dialog.thread=Thread.currentThread();              dialog.thread=Thread.currentThread();
             //System.out.println("Called run with " + fileChooser.getSelectedFile());              //System.out.println("Called run with " + fileChooser.getSelectedFile());
             dialog.setTitle("Download Status Window");              dialog.setTitle("Download Status Window");
Line 160  public class Upload extends JApplet impl Line 177  public class Upload extends JApplet impl
                 conGet.dialog=dialog;                  conGet.dialog=dialog;
                 Bundle bundle = bundleRef;                  Bundle bundle = bundleRef;
                 int fileCount = bundle.documents.size();                  int fileCount = bundle.documents.size();
                   new File(startFile.getAbsolutePath()+"/images").mkdirs();
                   File indexFile = new File(startFile.getAbsolutePath()+"/index.xml");
                   if(indexFile.exists())
                   bundle = getBundle(indexFile);
                   else
                   bundleRef.exportToXMLDownload(new File(startFile.getAbsolutePath()+"/index.xml"),startFile);
       
                   boolean update = (bundle != bundleRef);
                   // System.out.println("update "+update);
                   Vector docs = new Vector(); Bundle.Document refDoc = null; Vector list = new Vector();
                   if(update)
                   for(int k = 0;k <fileCount; ++k)
                   {
                         Bundle.Document doc = (Bundle.Document)bundleRef.documents.elementAt(k);
                         if((refDoc=(Bundle.Document)bundle.docs.get(new Integer(doc.docID)))!=null) 
                         {
                           if(checkForFileDownloaded(startFile.getAbsolutePath(),refDoc,doc))
                           {
                             if(!doc.downloadAgain)
                             list.addElement(doc.fileName);
                             docs.addElement(doc);//empty
                           } 
                          } else docs.addElement(doc);
                     }
                    if(!update)
                    docs=bundleRef.documents;
                   System.out.println(docs.size());
                   fileCount=docs.size();boolean keepAllFiles=false,newVersionforAll=false;
                 for(int i=0;i< fileCount;++i)                  for(int i=0;i< fileCount;++i)
                 {                  {
                   Bundle.Document doc=(Bundle.Document)bundle.documents.get(i);                    Bundle.Document doc=(Bundle.Document)docs.get(i);
                     
                     if(update&&(refDoc=(Bundle.Document)bundle.docs.get(new Integer(doc.docID)))!=null&&!doc.downloadAgain) 
                     {
                        System.out.println(refDoc+" "+doc.fileName);
   
                         if(!newVersionforAll&&!keepAllFiles)
                         {
                           Object[] options = new Object[] { "Cancel","Replace existing files", "Keep existing files" }; 
                           Object[] options2 = new Object[] { "Cancel","Replace existing file", "Keep existing file" }; 
                 
                           int option =  (list.size()>1) ? showDialog("Warning : several files  exist on the client \n Press show list to see the list! \n What do you want to do?", "several Files exist!", options,list):showDialog("Warning :  file "+refDoc.fileName+" exists on the client \n What do you want to do?", " File "+doc.fileName+" exists!", options2);
                 ;
                           if(option == 0) break;// mode = "old";
                         if(option == 1) 
                         { 
                           mode ="old";
                           newVersionforAll = true;
                           keepAllFiles = false;
                         }
                         if(option == 2) 
                         {  
                           count++;
                           System.out.println("Heh Document exists!"+ doc);
                           System.out.println("Skipping");
                           newVersionforAll = false;
                           keepAllFiles = true;
                           //continue;
                         }  
                       }
                 if(keepAllFiles) 
                 {
                   if(!doc.downloadAgain)
                   continue;
                 }
                 }// to bundle if
                 else
                 if(update) 
                 {
                    bundle.docs.put(new Integer(doc.docID),doc);
                    bundle.documents.addElement(doc);
                    bundle.docsLocator.put(doc.locator,doc); 
                 }   
                   if(doc.fileLink=="") continue;                    if(doc.fileLink=="") continue;
                   System.out.println(doc.title);                    System.out.println(doc.title);
                   conGet.url=new URL(doc.fileLink);                    conGet.url=new URL(doc.fileLink);
                   conGet.get();                    conGet.get();
                   writeChunked(conGet,new File("./download/"+doc.fileName),i+1, fileCount,doc.size, dialog);                  
                   //if(getMD5(conGet.md).equals(doc.md5cs)) System.out.println("download sucess");                    writeChunked(conGet,new File(startFile.getAbsolutePath()+"/images/"+doc.fileName),i+1, fileCount,doc.size, dialog);
                   //else                     String md5 =getMD5(conGet.md);
                   // break;                    if(md5.equals(doc.md5cs)) System.out.println("download sucess");
                     else 
                      {
                        System.out.println(" md5 "+md5 +" "+doc.md5cs+ " "+doc.fileName);
                       break;
                      }
                      
                       if(debug)
                       System.out.println(" md5 "+md5 +" "+doc.md5cs+ " "+doc.fileName);
               
                 }                  }
               } catch(Exception e5) { e5.printStackTrace();}                      // close connection
                 conGet.socket.close();
                 dialog.setVisible(false);
               long time2 =System.currentTimeMillis();                long time2 =System.currentTimeMillis();
                  showDialog("Finished download of " + fileCount + " files in " + ((time2 - time) / 1000) + " seconds", "Finished Download");
                 if(update)
                 bundle.exportToXMLDownload(new File(startFile.getAbsolutePath()+"/index.xml"),startFile);
               System.out.println("Time for download needed "+(time2-time));                System.out.println("Time for download needed "+(time2-time));
     
                 } catch(Exception e5) 
                 {  
                     dialog.setVisible(false);
                     ByteArrayOutputStream b = new ByteArrayOutputStream();
                     PrintStream stream = new PrintStream(b);
                     e5.printStackTrace(stream);
                     //connections.remove(startFile.getAbsolutePath());
                     showErrorDialog(b.toString(), "Download failed");
                     }      
                  catch(Error err)
                 {
                    
                    showErrorDialog(err.toString(), "Download failed");
                   System.out.println("Error caught "+ err);
                 }
         }          }
      };       };
      thread.start();       thread.start();
    }     }
      });
      }
      downloadChooser.showDialog(new JFrame()," Download ");
    } 
    if (e.getActionCommand().equals("Upload"))     if (e.getActionCommand().equals("Upload"))
     {      {
   
Line 579  public class Upload extends JApplet impl Line 700  public class Upload extends JApplet impl
   public static boolean checkForNewFile(String path,Bundle.Document doc,Bundle.Document refDoc) throws Exception    public static boolean checkForNewFile(String path,Bundle.Document doc,Bundle.Document refDoc) throws Exception
   {    {
      String name = doc.fileLink.trim().startsWith("file://") ?doc.fileLink.trim().substring(7):doc.fileLink.trim();       String name = doc.fileLink.trim().startsWith("file://") ?doc.fileLink.trim().substring(7):doc.fileLink.trim();
      System.out.println(name);       //System.out.println(name);
     boolean isNewFile=false;      boolean isNewFile=false;
      String filePath = name.startsWith("/") ? name:path+name;       String filePath = name.startsWith("/") ? name:path+name;
        File file = new File(filePath);
        if(!file.exists()) 
        {
         
         showErrorDialog("Warning \n"+filePath +" does not exist !","Warning file does not exist !");
         return false;
        }
        if(file.isDirectory()) return false;
      FileInputStream f=new FileInputStream( filePath);       FileInputStream f=new FileInputStream( filePath);
      int length=f.available();       int length=f.available();
      if(length!=refDoc.size) isNewFile=true;       if(length!=refDoc.size) isNewFile=true;
Line 595  public class Upload extends JApplet impl Line 724  public class Upload extends JApplet impl
         // System.out.println("Date file modified "+modified);          // System.out.println("Date file modified "+modified);
         // System.out.println("Time difference in minutes "+((actualTime.getTime()-modified.getTime())/60000L));          // System.out.println("Time difference in minutes "+((actualTime.getTime()-modified.getTime())/60000L));
          // if file has been modified longer than 10 hours do not compare md5 --> file is not newer           // if file has been modified longer than 10 hours do not compare md5 --> file is not newer
          if(actualTime.getTime()==modified.getTime()) return false;            if(actualTime.getTime()==modified.getTime()) 
            {
             f.close();
             return false; 
            }
           
          MessageDigest md = MessageDigest.getInstance("MD5");           MessageDigest md = MessageDigest.getInstance("MD5");
         byte[] b=new byte[512000];          byte[] b=new byte[512000];
         int   readCount =0;          int   readCount =0;
Line 618  public class Upload extends JApplet impl Line 752  public class Upload extends JApplet impl
      isNewFile = !getMD5(md).equals(refDoc.md5cs);       isNewFile = !getMD5(md).equals(refDoc.md5cs);
             
      }       }
    System.out.println("is new file ? "+isNewFile+" length is equal ?"+(length==refDoc.size));        // close file
         f.close();
      if(isNewFile)
        System.out.println(name+" "+refDoc.fileName);
    //  System.out.println("is new file ? "+isNewFile+" length is equal ?"+(length==refDoc.size));
    return isNewFile;     return isNewFile;
    }          }     
     public static boolean checkForFileDownloaded(String path,Bundle.Document doc,Bundle.Document refDoc) throws Exception
     {
        String name = doc.fileLink.trim().startsWith("file://") ?doc.fileLink.trim().substring(7):doc.fileLink.trim();
        //System.out.println(name);
        boolean isNewFile=false;
        String filePath = name.startsWith("/") ? name:path+name;
        File file = new File(filePath);
        if(!file.exists()) 
        {
          refDoc.downloadAgain = true;
          return true;
        }
        if(file.isDirectory()) return false;
         FileInputStream f=new FileInputStream( filePath);
        int length=f.available();
        if(length!=refDoc.size) 
        {
           // System.out.println(doc.fileName+" "+refDoc.fileName+" "+doc.size+" "
          // +refDoc.size+" "+length);
          refDoc.downloadAgain = doc.size>length;
          isNewFile = true;
        }
        else
        {
          isNewFile = !doc.md5cs.equals(refDoc.md5cs);
        }
       
      f.close();
      if(isNewFile)
        System.out.println(name+" "+refDoc.fileName);
    //  System.out.println("is new file ? "+isNewFile+" length is equal ?"+(length==refDoc.size));
      
      return isNewFile;
      }     
     
   public static String convertUml(String newName)    public static String convertUml(String newName)
   {    {
     StringBuffer alterMe = new StringBuffer(newName.trim());      StringBuffer alterMe = new StringBuffer(newName.trim());
Line 993  public class Upload extends JApplet impl Line 1166  public class Upload extends JApplet impl
       }        }
       // System.out.print((char)c);        // System.out.print((char)c);
     }         }   
     int bufSize=Integer.parseInt(sb.toString().trim(), 16);      int bufSize=Integer.parseInt(sb.toString().trim(), 16),newBufSize=0;
           
     System.err.println("chunk size "+Integer.parseInt(sb.toString().trim(), 16)+" bytes"+" limit "+limit);      System.err.println("chunk size "+Integer.parseInt(sb.toString().trim(), 16)+" bytes"+" limit "+limit);
     //sb.append((char)r.read());      //sb.append((char)r.read());
     // sb.append((char)r.read());      // sb.append((char)r.read());
     sb=new StringBuffer();//.delete(0,sb.length());      sb=new StringBuffer();//.delete(0,sb.length());
     byte[] b=new byte[bufSize];      byte[] b=new byte[bufSize];
     countC=0;      countC=0;int size = 0;
     StringBuffer localBuff=new StringBuffer();      StringBuffer localBuff=new StringBuffer();
     while (true)       while (true) 
     {      {
Line 1019  public class Upload extends JApplet impl Line 1192  public class Upload extends JApplet impl
   
       if(readCount + readSize > bufSize)        if(readCount + readSize > bufSize)
       {        {
         readSize = r.read(b, readCount, bufSize - readCount); //  mout.flush();           readSize = r.read(b, readCount, bufSize - readCount); 
       
       }        }
       else        else
       {        {
         readSize = r.read(b,readCount,readSize);          readSize = r.read(b,readCount,readSize);
       }        }
     if(loop>=200) throw new Error("Connection timed out");        if(loop>=60) 
         {
           loop = 0;
           conTest.socket.close();
           conTest.reconnect();
           conTest.get();
           r= conTest.in;
           file_out = new  FileOutputStream(f);
           fout = new BufferedOutputStream(file_out);
           countC=0;size=0;readCount= 0;conTest.md.reset();
           response = conTest.getResponseMessage();
           System.out.println(response);
           while ((c = r.read()) != -1) 
           {
             ++countC;
             char ch=(char)c;
             
             if(Character.digit(ch, 16) != -1)
             sb.append((char)c);
             if(ch=='\r')
             {
               ch=(char)r.read();
               if( ch=='\n') 
               break;
             }
             // System.out.print((char)c);
           }   
          bufSize=Integer.parseInt(sb.toString().trim(), 16);
        }
         //throw new Error("Connection timed out");
     if(readSize==0)       if(readSize==0) 
     {      {
       long time = System.currentTimeMillis();        long time = System.currentTimeMillis();
       while(System.currentTimeMillis()-time<100);        while(System.currentTimeMillis()-time<100);
     } else loop=0;      } else loop=0;
           
     if(debug)      //if(debug)
     System.out.println(++loop+" read "+readCount+" bytes"+" readSize "+readSize);     // System.out.println(++loop+" read "+readCount+" bytes"+" readSize "+readSize);
     readCount+=readSize;      readCount+=readSize;
           
     }      }
               
         
       //while(r.available()<bufSize); // critical if connection closed        //while(r.available()<bufSize); // critical if connection closed
     //  r.read(b,0,bufSize);       //  r.read(b,0,bufSize); 
       //System.err.println(r.available());        //System.err.println(r.available());
       r.read();r.read();        char ch2=(char)r.read();
       if(charset!="")       // if(ch2=='\r') System.out.print("CR");
      fout.write(b,0,bufSize);        ch2=(char)r.read();
     //   sb.append(new String(b,0,bufSize,charset));       // if(ch2=='\n') System.out.println("LF");
       else  
      fout.write(b,0,bufSize);  
       md.update(b, 0, bufSize);  
      countC+=bufSize;  
      dialog.progress.setValue((int) (((double) countC / (double) fileSize) * 100.0));       
      // if(debug)       // if(debug)
      // System.out.println(new String(b,0,bufSize));       // System.out.println(new String(b,0,bufSize));
       while ( (c = r.read()) != -1)         while ( (c = r.read()) != -1) 
Line 1060  public class Upload extends JApplet impl Line 1255  public class Upload extends JApplet impl
         char ch=(char)c;          char ch=(char)c;
         //System.err.println("read lf "+(ch=='\r')+" or cr "+( ch=='\n')+" "+(Character.digit(ch, 16) == -1));          //System.err.println("read lf "+(ch=='\r')+" or cr "+( ch=='\n')+" "+(Character.digit(ch, 16) == -1));
         //if((ch!='\r')&&( ch!='\n')) System.err.println("read ch "+" "+ch+" "+Character.isSpace(ch));          //if((ch!='\r')&&( ch!='\n')) System.err.println("read ch "+" "+ch+" "+Character.isSpace(ch));
          // System.out.print(ch);
         if(Character.digit(ch, 16) != -1)          if(Character.digit(ch, 16) != -1)
         localBuff.append(ch);          localBuff.append(ch);
         if(ch=='\r')          if(ch=='\r')
Line 1069  public class Upload extends JApplet impl Line 1265  public class Upload extends JApplet impl
           break;            break;
         }          }
       }        }
        if(debug)  
        System.out.println("chunk size "+localBuff);  
       if(localBuff.length()>0)        if(localBuff.length()>0)
       {        {
         // System.err.println("new chunk size "+Integer.parseInt(localBuff.toString().trim(), 16)+" bytes");          // System.err.println("new chunk size "+Integer.parseInt(localBuff.toString().trim(), 16)+" bytes");
         // System.err.println((char)r.read());          // System.err.println((char)r.read());
         bufSize=Integer.parseInt(localBuff.toString().trim(), 16);          newBufSize=Integer.parseInt(localBuff.toString().trim(), 16);
           //if(bufSize==3041) bufSize=3040;
           if(debug)
          System.out.println("chunk size "+Integer.parseInt(localBuff.toString().trim(), 16)+" "+size);
     
         //localBuff.delete(0,localBuff.length());          //localBuff.delete(0,localBuff.length());
          localBuff=new StringBuffer();           localBuff=new StringBuffer();
       } // else bufSize=0;        } // else bufSize=0;
          if((char)b[bufSize-1]=='\n')
       {
           if(debug)
           System.out.println("LF found "+((char)b[bufSize-1]=='\n'));
           if(newBufSize==0) bufSize=bufSize-1;
           
       }
        fout.write(b,0,bufSize);
        md.update(b, 0, bufSize);
        size += bufSize;
        bufSize=newBufSize;
        dialog.progress.setValue((int) (((double) size / (double) fileSize) * 100.0));     
       ++countC;        ++countC;
      // sb.append("new  Chunk"+r.available()+"\r\n ");       if(bufSize==0)
      if(bufSize==0)//(bufSize))   
       break;        break;
       // System.err.println(" in  Chunk "+bufSize+"   buffers used "+(countC)+" \r\n ");        // System.err.println(" in  Chunk "+bufSize+"   buffers used "+(countC)+" \r\n ");
       // // System.out.print((char)c);        // // System.out.print((char)c);
Line 1159  public class Upload extends JApplet impl Line 1368  public class Upload extends JApplet impl
        {         {
           // read XML Metadata from a file            // read XML Metadata from a file
           FileReader r=new FileReader(xmlFile);            FileReader r=new FileReader(xmlFile);
             BufferedReader buffr = new BufferedReader(r);
           StringBuffer sb=new StringBuffer();            StringBuffer sb=new StringBuffer();
           int c=0;            int c=0;
           while ((c = r.read()) != -1)             while ((c = buffr.read()) != -1) 
           {            {
             char ch=(char)c;              char ch=(char)c;
             sb.append(ch);              sb.append(ch);
Line 1236  public class Upload extends JApplet impl Line 1446  public class Upload extends JApplet impl
       //addToBundle(bundle,sb);        //addToBundle(bundle,sb);
       // System.out.println(sb.toString());        // System.out.println(sb.toString());
       // System.exit(0);        // System.exit(0);
         conTest.socket.close();
     } catch(Exception e) {e.printStackTrace();}       } catch(Exception e) {e.printStackTrace();} 
     return bundle;      return bundle;
   }    }
Line 1411  public class Upload extends JApplet impl Line 1621  public class Upload extends JApplet impl
          System.out.println(Bundle.genresId.get(Bundle.genVec.get(i)));           System.out.println(Bundle.genresId.get(Bundle.genVec.get(i)));
         //System.out.println( Bundle.genres);          //System.out.println( Bundle.genres);
        }          } 
        conTest.socket.close();
     } catch(Exception e) {e.printStackTrace();}       } catch(Exception e) {e.printStackTrace();} 
   }    }
  public static String  getGenreList(StringBuffer sb)   public static String  getGenreList(StringBuffer sb)
Line 1841  public class Upload extends JApplet impl Line 2052  public class Upload extends JApplet impl
     {      {
       try        try
       {        {
         // System.out.println("Yeah i called connect");           // System.out.println("Yeah i called get with "+url );
         //  socket = new Socket((pHost != null) ? pHost : url.getHost(), (pHost != null) ? pPort : url.getPort());          //  socket = new Socket((pHost != null) ? pHost : url.getHost(), (pHost != null) ? pPort : url.getPort());
         // socket.setTcpNoDelay(true);          // socket.setTcpNoDelay(true);
         in = new BufferedInputStream(socket.getInputStream());          in = new BufferedInputStream(socket.getInputStream());

Removed from v.1.3  
changed lines
  Added in v.1.4


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>