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

version 1.2, 2003/06/17 10:59:07 version 1.3, 2003/06/25 11:50:09
Line 30  public class Upload extends JApplet impl Line 30  public class Upload extends JApplet impl
   static Bundle bundle;    static Bundle bundle;
   static Bundle bundleRef;    static Bundle bundleRef;
   JButton upload = new JButton("Upload");    JButton upload = new JButton("Upload");
     JButton download = new JButton("Download");
   JRadioButton proxy = new JRadioButton("Proxy");    JRadioButton proxy = new JRadioButton("Proxy");
   static int fileCount = 0;    static int fileCount = 0;
   // static UploadInfo dialog = new UploadInfo();    // static UploadInfo dialog = new UploadInfo();
Line 40  public class Upload extends JApplet impl Line 41  public class Upload extends JApplet impl
   static boolean runningAsApplet=true;    static boolean runningAsApplet=true;
   static boolean debug=false;    static boolean debug=false;
   static String  rootNode = "edoc";    static String  rootNode = "edoc";
   static MessageDigest md ;  
   static  
   {  
       try {  
         md = MessageDigest.getInstance("MD5");  
     } catch(Exception e) { e.printStackTrace();}  
   }  
   public Upload()     public Upload() 
   {    {
     if(!runningAsApplet)      if(!runningAsApplet)
Line 58  public class Upload extends JApplet impl Line 52  public class Upload extends JApplet impl
   {    {
     this.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));      this.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
     this.getContentPane().add(upload);      this.getContentPane().add(upload);
       this.getContentPane().add(download);
     this.getContentPane().add(proxy);      this.getContentPane().add(proxy);
     upload.addActionListener(this);      upload.addActionListener(this);
       download.addActionListener(this);
     proxy.addActionListener(this);      proxy.addActionListener(this);
     id = System.currentTimeMillis();      id = System.currentTimeMillis();
        Properties prop = System.getProperties();         Properties prop = System.getProperties();
Line 135  public class Upload extends JApplet impl Line 131  public class Upload extends JApplet impl
       // proxy.setSelected(false);        // proxy.setSelected(false);
     }      }
   
       if (e.getActionCommand().equals("Download"))
       {
   
        Thread thread = new Thread()
        {
           public void run()
           {
               UploadInfo dialog = new UploadInfo();
           
               File startFile=new File(".");
               dialog.thread=Thread.currentThread();
               //System.out.println("Called run with " + fileChooser.getSelectedFile());
               dialog.setTitle("Download Status Window");
               dialog.setSize(400, 175);
               dialog.setLocation(new java.awt.Point(150, 100));
             
                 long time = System.currentTimeMillis();
                 try 
                 {
                   Connection conGet = null;
                   if(!runningAsApplet)
                   conGet=new Connection( new URL("http://core.rz-berlin.mpg.de/edoctest/oi.epl"));
                   else
                   conGet=new Connection( new URL(url+"/oi.epl"));
                   
                   conGet.ses=ses;
                   conGet.dialog=dialog;
                   Bundle bundle = bundleRef;
                   int fileCount = bundle.documents.size();
                   for(int i=0;i< fileCount;++i)
                   {
                     Bundle.Document doc=(Bundle.Document)bundle.documents.get(i);
                     if(doc.fileLink=="") continue;
                     System.out.println(doc.title);
                     conGet.url=new URL(doc.fileLink);
                     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");
                     //else 
                     // break;
                   }
                 } catch(Exception e5) { e5.printStackTrace();}      
                 long time2 =System.currentTimeMillis();
                 System.out.println("Time for download needed "+(time2-time));
           }
        };
        thread.start();
      }
     if (e.getActionCommand().equals("Upload"))      if (e.getActionCommand().equals("Upload"))
     {      {
   
Line 380  public class Upload extends JApplet impl Line 424  public class Upload extends JApplet impl
       conPost.post(file,"new",doc.docID+"",doc.title,doc.locator,doc.comment);        conPost.post(file,"new",doc.docID+"",doc.title,doc.locator,doc.comment);
       
     // conPost.post(new File("./test2.jpg"),picture.elementAt(i).toString(),artist.elementAt(i).toString(),source.elementAt(i).toString());      // conPost.post(new File("./test2.jpg"),picture.elementAt(i).toString(),artist.elementAt(i).toString(),source.elementAt(i).toString());
      System.out.println(getMD5(md));       System.out.println(getMD5(conPost.md));
      dialog.progress.setValue(100);       dialog.progress.setValue(100);
      count++;       count++;
     return count;      return count;
Line 395  public class Upload extends JApplet impl Line 439  public class Upload extends JApplet impl
     System.out.println(path);       System.out.println(path); 
     mode = "new";      mode = "new";
     boolean newVersionforAll = false, keepAllFiles = false;       boolean newVersionforAll = false, keepAllFiles = false; 
     Vector list=new Vector();      Vector list=new Vector();Vector docs=new Vector();Bundle.Document refDoc=null;
     for(int k=0;k<fileCount;++k)      for(int k=0;k<fileCount;++k)
     {      {
        Bundle.Document doc = (Bundle.Document)bundle.documents.elementAt(k);         Bundle.Document doc = (Bundle.Document)bundle.documents.elementAt(k);
        if(bundleRef.docsLocator.get(doc.locator)!=null)          if((refDoc=(Bundle.Document)bundleRef.docsLocator.get(doc.locator))!=null) 
          {
            if(checkForNewFile(path,doc,refDoc))
            {
        list.addElement(doc.fileName);         list.addElement(doc.fileName);
              docs.addElement(doc);
            }
          } else docs.addElement(doc);
     }      }
   
     for(int k=0;k<fileCount;++k)      for(int k=0;k<docs.size();++k)
     {      {
       Bundle.Document doc = (Bundle.Document)bundle.documents.elementAt(k);        Bundle.Document doc = (Bundle.Document)docs.elementAt(k);
             
       String response = "";        String response = "";
       dialog.title.setText("Uploading " + path+doc.fileName);        dialog.title.setText("Uploading " + path+doc.fileName);
Line 419  public class Upload extends JApplet impl Line 469  public class Upload extends JApplet impl
       dialog.repaint();        dialog.repaint();
       else        else
       dialog.show();        dialog.show();
         
       if(bundleRef.docsLocator.get(doc.locator)!=null)         if(bundleRef.docsLocator.get(doc.locator)!=null) 
       {        {
         if(!newVersionforAll&&!keepAllFiles)          if(!newVersionforAll&&!keepAllFiles)
Line 446  public class Upload extends JApplet impl Line 495  public class Upload extends JApplet impl
            continue;             continue;
          }             }  
       }        }
       Bundle.Document refDoc= (Bundle.Document) bundleRef.docsLocator.get(doc.locator);        refDoc= (Bundle.Document) bundleRef.docsLocator.get(doc.locator);
       doc.docID=refDoc.docID;        doc.docID=refDoc.docID;
       if(keepAllFiles)        if(keepAllFiles)
       {        {
Line 468  public class Upload extends JApplet impl Line 517  public class Upload extends JApplet impl
       conPost.ses=ses;        conPost.ses=ses;
       conPost.dialog=dialog;        conPost.dialog=dialog;
       conPost.post(path,doc,mode);        conPost.post(path,doc,mode);
       String md5=getMD5(md);        String md5=getMD5(conPost.md);
       System.out.println("MD5 is equal ?"+doc.md5cs.equals(md5)+" doc_md5 "+doc.md5cs+" md5 "+md5);        System.out.println("MD5 is equal ?"+doc.md5cs.equals(md5)+" doc_md5 "+doc.md5cs+" md5 "+md5);
       if(!doc.md5cs.equals(md5))        if(!doc.md5cs.equals(md5))
       {        {
Line 483  public class Upload extends JApplet impl Line 532  public class Upload extends JApplet impl
             conPost.ses=ses;              conPost.ses=ses;
             conPost.dialog=dialog;              conPost.dialog=dialog;
             conPost.post(path,doc,mode);                    conPost.post(path,doc,mode);      
             md5=getMD5(md);              md5=getMD5(conPost.md);
               if(!doc.md5cs.equals(md5)) showDialog("Transfer failed","Transfer failed again! ");
          }           }
       }      // System.out.println(getMD5(md));        }      // System.out.println(getMD5(md));
       dialog.progress.setValue(100);        dialog.progress.setValue(100);
       count++;        count++;
      }       }
        bundle.exportToXML(new File(path+"/temp.xml"));
      return count;       return count;
   }    }
     
Line 525  public class Upload extends JApplet impl Line 576  public class Upload extends JApplet impl
   
     return sb.toString();      return sb.toString();
   }    }
     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();
        System.out.println(name);
       boolean isNewFile=false;
        String filePath = name.startsWith("/") ? name:path+name;
        FileInputStream f=new FileInputStream( filePath);
        int length=f.available();
        if(length!=refDoc.size) isNewFile=true;
         else
         {
            
             //new File(filePath).setLastModified(System.currentTimeMillis()-690000000L);
            Date actualTime = new Date(new File(filePath).lastModified());
            Date modified = new Date(doc.modified);//new File(filePath).lastModified());
           // System.out.println("actual Date "+actualTime);
           // System.out.println("Date file modified "+modified);
           // 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(actualTime.getTime()==modified.getTime()) return false; 
            MessageDigest md = MessageDigest.getInstance("MD5");
           byte[] b=new byte[512000];
           int   readCount =0;
           while (readCount < length)
           {
             if (readCount + b.length > length)
               f.read(b, 0, length - readCount); //  mout.flush();
             else
               f.read(b);
             if (readCount + b.length > length)
             {
               md.update(b, 0, length - readCount);
             }
             else
            {
               md.update(b, 0, b.length);
            }
           readCount += b.length;
           }
        isNewFile = !getMD5(md).equals(refDoc.md5cs);
   
        }
      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 620  public class Upload extends JApplet impl Line 715  public class Upload extends JApplet impl
     if(fileList==null)      if(fileList==null)
     list= new JList(new String[]{"click","here","test","bla","world","hello"});      list= new JList(new String[]{"click","here","test","bla","world","hello"});
     else list = new JList(fileList);      else list = new JList(fileList);
     
     JScrollPane scpane = new JScrollPane(list);      JScrollPane scpane = new JScrollPane(list);
     scpane.setPreferredSize(new Dimension(150,150));      scpane.setPreferredSize(new Dimension(150,150));
           
Line 829  public class Upload extends JApplet impl Line 925  public class Upload extends JApplet impl
     // System.out.println(sb);      // System.out.println(sb);
     return sb;      return sb;
   }    }
     public static void writeChunked(Connection conTest,File f,int count,int fileCount,int fileSize,UploadInfo dialog) throws Exception
     {
       InputStream r= conTest.in;
       FileOutputStream file_out = new  FileOutputStream(f);
       BufferedOutputStream fout = new BufferedOutputStream(file_out);
       int c,countC=0,limit=conTest.in.available();
       // r.skip(211);
       String response = conTest.getResponseMessage();
       String lowerCase= response.toLowerCase();
       int index =lowerCase.indexOf("charset");
       int index2 =lowerCase.indexOf("=",index);
       int index3 =lowerCase.indexOf("\r",index2);
       String charset=(index>0) ? response.substring(index2+1,index3):"";
       charset="";//charset.toUpperCase();
       System.err.println(response);
       dialog.title.setText("Downloading to " + f.toString());
       dialog.progress.setValue(0);
        MessageDigest md =conTest.md;
        md.reset();
       String fileName = f.getName()+ " " + ((fileSize > 1000000) ? (fileSize / 1048576) + " MByte" : fileSize+ " Bytes");
         dialog.file.setText(fileName);
         dialog.status.setText(count + " of " + fileCount + ((fileCount > 1) ? " files" : " file"));
         //dialog.pack();
         //dialog.validate();
         if (dialog.isVisible())
         dialog.repaint();
         else
         dialog.show();
     
       if(lowerCase.indexOf("chunked")<0)
       {
         while(r.available()==0); 
         System.err.println(r.available());
         while ((c = r.read()) != -1) 
         {
           byte b=(byte)c;
           fout.write(b);             
           //md.update(b, 0, b.length);
           if(debug)
           System.out.print((char)c);
         }
       /* 
        if(charset!="")
         fout.write(local.toString(charset));
         else
         fout.write(local.toString());
         */
         //if(debug)
        // System.out.print(sb);
         //return sb;
       }
        StringBuffer sb = new StringBuffer(); 
       // read chunk size
       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);
       }   
       int bufSize=Integer.parseInt(sb.toString().trim(), 16);
       
       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=new StringBuffer();//.delete(0,sb.length());
       byte[] b=new byte[bufSize];
       countC=0;
       StringBuffer localBuff=new StringBuffer();
       while (true) 
       {
         // sb.append(" new  Chunk "+r.available()+" \r\n ");
          int  readCount = 0;
         int available  = 0;
         int readSize    = b.length; int loop=0;
          while (readCount < bufSize)
         {
           available=r.available();
   
           if(available>=bufSize)
           readSize=bufSize;
           else
           readSize=available;
   
         if(readCount + readSize > bufSize)
         {
           readSize = r.read(b, readCount, bufSize - readCount); //  mout.flush();
       
         }
         else
         {
           readSize = r.read(b,readCount,readSize);
         }
       if(loop>=200) throw new Error("Connection timed out");
       if(readSize==0) 
       {
         long time = System.currentTimeMillis();
         while(System.currentTimeMillis()-time<100);
       } else loop=0;
       
       if(debug)
       System.out.println(++loop+" read "+readCount+" bytes"+" readSize "+readSize);
       readCount+=readSize;
       
       }
         
         
         //while(r.available()<bufSize); // critical if connection closed
       //  r.read(b,0,bufSize); 
         //System.err.println(r.available());
         r.read();r.read();
         if(charset!="")
        fout.write(b,0,bufSize);
       //   sb.append(new String(b,0,bufSize,charset));
         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)
        // System.out.println(new String(b,0,bufSize));
         while ( (c = r.read()) != -1) 
         {
           //      ++countC;
           char ch=(char)c;
           //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(Character.digit(ch, 16) != -1)
           localBuff.append(ch);
           if(ch=='\r')
           {
             ch=(char)r.read();
             if( ch=='\n') 
             break;
           }
         }
          if(debug)
          System.out.println("chunk size "+localBuff);
         if(localBuff.length()>0)
         {
           // System.err.println("new chunk size "+Integer.parseInt(localBuff.toString().trim(), 16)+" bytes");
           // System.err.println((char)r.read());
           bufSize=Integer.parseInt(localBuff.toString().trim(), 16);
           //localBuff.delete(0,localBuff.length());
            localBuff=new StringBuffer();
         } // else bufSize=0;
         ++countC;
        // sb.append("new  Chunk"+r.available()+"\r\n ");
        if(bufSize==0)//(bufSize)) 
         break;
         // System.err.println(" in  Chunk "+bufSize+"   buffers used "+(countC)+" \r\n ");
         // // System.out.print((char)c);
       }
       // System.out.println(sb);
       fout.flush();
       fout.close();
     
       file_out.close();
       //return sb;
     }
   
   public static Hashtable getMetaData(String url)     public static Hashtable getMetaData(String url) 
   {    {
     Hashtable data=new Hashtable();      Hashtable data=new Hashtable();
Line 1309  public class Upload extends JApplet impl Line 1574  public class Upload extends JApplet impl
     String ses="";      String ses="";
     HttpURLConnection con;      HttpURLConnection con;
     UploadInfo dialog = new UploadInfo();      UploadInfo dialog = new UploadInfo();
       MessageDigest md ;
   
     public Connection(URL url) throws Exception      public Connection(URL url) throws Exception
     {      {
       Properties prop = System.getProperties();        Properties prop = System.getProperties();
Line 1344  public class Upload extends JApplet impl Line 1611  public class Upload extends JApplet impl
       in = new BufferedInputStream(socket.getInputStream(),2);        in = new BufferedInputStream(socket.getInputStream(),2);
       out = new PrintStream(new BufferedOutputStream(socket.getOutputStream()),true);        out = new PrintStream(new BufferedOutputStream(socket.getOutputStream()),true);
       this.url = url;        this.url = url;
         try 
         {
            md = MessageDigest.getInstance("MD5");
         
         } catch(Exception e) { e.printStackTrace();}
   
     }      }
    public String getSessionID(String data)      public String getSessionID(String data) 
    {     {
Line 1518  public class Upload extends JApplet impl Line 1791  public class Upload extends JApplet impl
      System.out.println(sb);       System.out.println(sb);
      if(sb.length()>0)       if(sb.length()>0)
      addToBundle(bundleRef,sb);       addToBundle(bundleRef,sb);
         try        // updating document with edoc Data
         {  
             
           Bundle.Document newDoc =(Bundle.Document) bundleRef.docsLocator.get(doc.locator);            Bundle.Document newDoc =(Bundle.Document) bundleRef.docsLocator.get(doc.locator);
           doc.md5cs=newDoc.md5cs;            doc.md5cs=newDoc.md5cs;
           doc.size=newDoc.size;            doc.size=newDoc.size;
Line 1528  public class Upload extends JApplet impl Line 1799  public class Upload extends JApplet impl
           doc.docID=newDoc.docID;            doc.docID=newDoc.docID;
           doc.ver=newDoc.ver;            doc.ver=newDoc.ver;
           doc.fileVer=newDoc.fileVer;            doc.fileVer=newDoc.fileVer;
           bundle.exportToXML(new File("./meta.xml"));            doc.modified = new File(filePath).lastModified();
         } catch(Exception e7) { e7.printStackTrace();}         
     }      }
       
     public void sendFile(FileInputStream f,PrintStream out) throws Exception      public void sendFile(FileInputStream f,PrintStream out) throws Exception
Line 1537  public class Upload extends JApplet impl Line 1808  public class Upload extends JApplet impl
         int length=f.available();          int length=f.available();
         md.reset();          md.reset();
         dialog.progress.setValue(0);          dialog.progress.setValue(0);
         byte[] b=new byte[512];          byte[] b=new byte[16204];
         int   readCount =0;          int   readCount =0;
         while (readCount < length)          while (readCount < length)
         {          {

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


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