File:  [Repository] / edoc-applet / Upload.java
Revision 1.4: download - view: text, annotated - select for diffs - revision graph
Mon Jul 21 10:14:15 2003 UTC (20 years, 11 months ago) by rogo
Branches: MAIN
CVS tags: HEAD
Download working

    1: //
    2: //  Upload.java
    3: //  
    4: //
    5: //  Created by rogo on Wed Jan 29 2003.
    6: //  Copyright (c) 2003 Rogo Soft. All rights reserved.
    7: //
    8:  import com.exploringxml.xml.*;
    9:  import java.io.*;
   10:  import java.util.*;
   11:  import java.net.*;
   12:  import javax.swing.*;
   13:  import java.awt.*;
   14:  import java.awt.event.*;
   15:  import java.security.*;
   16: 
   17: 
   18: 
   19: public class Upload extends JApplet implements ActionListener
   20: {
   21:   static File startFile;
   22:   static int count = 0;
   23:   static JFileChooser fileChooser,downloadChooser;
   24:   static String url;
   25:   static long id = 0L;
   26:   static Hashtable connections=new Hashtable();
   27:   static String col = "11"; // Collection /Institute
   28:   static String grp ="136"; // Group or Bundle
   29:   static String ses ="";
   30:   static Bundle bundle;
   31:   static Bundle bundleRef;
   32:   JButton upload = new JButton("Upload");
   33:   JButton download = new JButton("Download");
   34:   JRadioButton proxy = new JRadioButton("Proxy");
   35:   static int fileCount = 0;
   36:   // static UploadInfo dialog = new UploadInfo();
   37:   static String mode = "";
   38:   static String destination = "";
   39:   static String proxyHost = "";
   40:   Upload uploader = this;
   41:   static boolean runningAsApplet=true;
   42:   static boolean debug=false;
   43:   static boolean uploadMode = true;
   44:   static String  rootNode = "edoc";
   45:   public Upload() 
   46:   {
   47:     if(!runningAsApplet)
   48:     init();
   49:   }
   50:  
   51:  // start applet code
   52:    public void init()
   53:   {
   54:     this.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
   55:     this.getContentPane().add(upload);
   56:     this.getContentPane().add(download);
   57:     this.getContentPane().add(proxy);
   58:     upload.addActionListener(this);
   59:     download.addActionListener(this);
   60:     proxy.addActionListener(this);
   61:     id = System.currentTimeMillis();
   62:        Properties prop = System.getProperties();
   63: 
   64:     // prop.put("proxySet", "true"); 
   65:     // prop.put("http.proxyHost","proxy.unibe.ch");
   66:     // prop.put("proxyPort", "80"); 
   67:      // prop.put("java.protocol.handler.pkgs","HTTPClient");
   68:      fileChooser=null;download=null;
   69:     if(!runningAsApplet)
   70:     {
   71:       Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
   72:       Point p=new Point((screenSize.width-700)/2,(screenSize.height-80)/2);
   73:       JFrame testFrame=new JFrame();
   74:       testFrame.setTitle("Archimedes Applet");
   75:       testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   76:       testFrame.getContentPane().add(this);
   77:       testFrame.setSize(300,80);
   78:       testFrame.setLocation(p);
   79:       testFrame.setVisible(true);
   80:       Console console = new Console(testFrame);
   81:       console.setVisible(true);
   82:       try 
   83:       {
   84:         Connection con= new Connection(new URL("http://core.rz-berlin.mpg.de:80/edoctest/adm.epl"));
   85:         ses=con.getSessionID("name=root&passwd=edoctest&mode=verifylogin");
   86:         //("name=dwinter&passwd=e1nste1n&mode=verifylogin");
   87:         con.socket.close();
   88:        } catch(Exception exc) { }
   89:     } else
   90:     {
   91:       Console console = new Console(new JFrame());
   92:       console.setVisible(true);
   93:       ses= getParameter("ses");
   94:       col = getParameter("col").trim();
   95:       grp = getParameter("grp").trim();
   96:       bundleRef = getBundle(getCodeBase()+"oi.epl?mm=list&grp="+grp+"&col="+col);
   97:       getGenre(); 
   98:       //bundle=getBundle("http://core.rz-berlin.mpg.de/edoctest/oi.epl?mm=list&grp=136&col=11");
   99:      
  100:     }
  101:   }
  102:   public void actionPerformed(ActionEvent e)
  103:   {
  104:     if (e.getSource().equals(proxy))
  105:     {
  106:       proxyHost = JOptionPane.showInputDialog("Please enter proxy host (format host:port)!");
  107:       if (proxyHost != null)
  108:       {
  109:         if (proxyHost.length() == 0)
  110:           {
  111:           proxyHost = "";
  112:           Properties prop = System.getProperties();
  113:           prop.remove("proxySet"); 
  114:           prop.remove("http.proxyHost");
  115:           prop.remove("proxyPort");   
  116:         
  117:           }
  118:          else
  119:          {
  120:            Properties prop = System.getProperties();
  121:         int index = proxyHost.indexOf(":");
  122:         String pHost = proxyHost.substring(0, (index > 0) ? index : proxyHost.length());
  123:         String pPort = (index > 0) ? proxyHost.substring(index + 1) : "80";
  124: 
  125:        prop.put("proxySet", "true"); 
  126:        prop.put("http.proxyHost",pHost);
  127:        prop.put("proxyPort", pPort);   
  128:          }
  129:         System.out.println("pr "); // proxyHost="";
  130:       } else
  131:         proxyHost = "";
  132:       // proxy.setSelected(false);
  133:     }
  134: 
  135:     if (e.getActionCommand().equals("Download"))
  136:     {
  137: 
  138:       if (downloadChooser == null)
  139:       {
  140:         downloadChooser = new JFileChooser();
  141:         downloadChooser.setFileSelectionMode(fileChooser.FILES_AND_DIRECTORIES);
  142:         downloadChooser.setFileFilter(new XMLFilter());
  143:         downloadChooser.setDialogType(JFileChooser.SAVE_DIALOG);
  144:         downloadChooser.addActionListener(new ActionListener()
  145:         {
  146:           public void actionPerformed(ActionEvent e2)
  147:           {
  148:             System.out.println(e2.getActionCommand());
  149:             if (!e2.getActionCommand().toString().equals("ApproveSelection")) return;
  150:             // {
  151: 
  152:      Thread thread = new Thread()
  153:      {
  154:         public void run()
  155:         {
  156:                if(runningAsApplet)
  157:                 set();
  158:             UploadInfo dialog = new UploadInfo();
  159:         
  160:             File startFile=downloadChooser.getSelectedFile();
  161:             dialog.thread=Thread.currentThread();
  162:             //System.out.println("Called run with " + fileChooser.getSelectedFile());
  163:             dialog.setTitle("Download Status Window");
  164:             dialog.setSize(400, 175);
  165:             dialog.setLocation(new java.awt.Point(150, 100));
  166:           
  167:               long time = System.currentTimeMillis();
  168:               try 
  169:               {
  170:                 Connection conGet = null;
  171:                 if(!runningAsApplet)
  172:                 conGet=new Connection( new URL("http://core.rz-berlin.mpg.de/edoctest/oi.epl"));
  173:                 else
  174:                 conGet=new Connection( new URL(url+"/oi.epl"));
  175:                 
  176:                 conGet.ses=ses;
  177:                 conGet.dialog=dialog;
  178:                 Bundle bundle = bundleRef;
  179:                 int fileCount = bundle.documents.size();
  180:                 new File(startFile.getAbsolutePath()+"/images").mkdirs();
  181:                 File indexFile = new File(startFile.getAbsolutePath()+"/index.xml");
  182:                 if(indexFile.exists())
  183:                 bundle = getBundle(indexFile);
  184:                 else
  185:                 bundleRef.exportToXMLDownload(new File(startFile.getAbsolutePath()+"/index.xml"),startFile);
  186:     
  187:                 boolean update = (bundle != bundleRef);
  188:                 // System.out.println("update "+update);
  189:                 Vector docs = new Vector(); Bundle.Document refDoc = null; Vector list = new Vector();
  190:                 if(update)
  191:                 for(int k = 0;k <fileCount; ++k)
  192:                 {
  193:                       Bundle.Document doc = (Bundle.Document)bundleRef.documents.elementAt(k);
  194:                       if((refDoc=(Bundle.Document)bundle.docs.get(new Integer(doc.docID)))!=null) 
  195:                       {
  196:                         if(checkForFileDownloaded(startFile.getAbsolutePath(),refDoc,doc))
  197:                         {
  198:                           if(!doc.downloadAgain)
  199:                           list.addElement(doc.fileName);
  200:                           docs.addElement(doc);//empty
  201:                         } 
  202:                        } else docs.addElement(doc);
  203:                   }
  204:                  if(!update)
  205:                  docs=bundleRef.documents;
  206:                 System.out.println(docs.size());
  207:                 fileCount=docs.size();boolean keepAllFiles=false,newVersionforAll=false;
  208:                 for(int i=0;i< fileCount;++i)
  209:                 {
  210:                   Bundle.Document doc=(Bundle.Document)docs.get(i);
  211:                   
  212:                   if(update&&(refDoc=(Bundle.Document)bundle.docs.get(new Integer(doc.docID)))!=null&&!doc.downloadAgain) 
  213:                   {
  214:                      System.out.println(refDoc+" "+doc.fileName);
  215: 
  216:                       if(!newVersionforAll&&!keepAllFiles)
  217:                       {
  218:                         Object[] options = new Object[] { "Cancel","Replace existing files", "Keep existing files" }; 
  219:                         Object[] options2 = new Object[] { "Cancel","Replace existing file", "Keep existing file" }; 
  220:               
  221:                         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);
  222:               ;
  223:                         if(option == 0) break;// mode = "old";
  224:                       if(option == 1) 
  225:                       { 
  226:                         mode ="old";
  227:                         newVersionforAll = true;
  228:                         keepAllFiles = false;
  229:                       }
  230:                       if(option == 2) 
  231:                       {  
  232:                         count++;
  233:                         System.out.println("Heh Document exists!"+ doc);
  234:                         System.out.println("Skipping");
  235:                         newVersionforAll = false;
  236:                         keepAllFiles = true;
  237:                         //continue;
  238:                       }  
  239:                     }
  240:               if(keepAllFiles) 
  241:               {
  242:                 if(!doc.downloadAgain)
  243:                 continue;
  244:               }
  245:               }// to bundle if
  246:               else
  247:               if(update) 
  248:               {
  249:                  bundle.docs.put(new Integer(doc.docID),doc);
  250:                  bundle.documents.addElement(doc);
  251:                  bundle.docsLocator.put(doc.locator,doc); 
  252:               }   
  253:                   if(doc.fileLink=="") continue;
  254:                   System.out.println(doc.title);
  255:                   conGet.url=new URL(doc.fileLink);
  256:                   conGet.get();
  257:                 
  258:                   writeChunked(conGet,new File(startFile.getAbsolutePath()+"/images/"+doc.fileName),i+1, fileCount,doc.size, dialog);
  259:                   String md5 =getMD5(conGet.md);
  260:                   if(md5.equals(doc.md5cs)) System.out.println("download sucess");
  261:                   else 
  262:                    {
  263:                      System.out.println(" md5 "+md5 +" "+doc.md5cs+ " "+doc.fileName);
  264:                     break;
  265:                    }
  266:                    
  267:                     if(debug)
  268:                     System.out.println(" md5 "+md5 +" "+doc.md5cs+ " "+doc.fileName);
  269:             
  270:                 }
  271:               // close connection
  272:               conGet.socket.close();
  273:               dialog.setVisible(false);
  274:               long time2 =System.currentTimeMillis();
  275:                showDialog("Finished download of " + fileCount + " files in " + ((time2 - time) / 1000) + " seconds", "Finished Download");
  276:               if(update)
  277:               bundle.exportToXMLDownload(new File(startFile.getAbsolutePath()+"/index.xml"),startFile);
  278:               System.out.println("Time for download needed "+(time2-time));
  279:   
  280:               } catch(Exception e5) 
  281:               {  
  282:                   dialog.setVisible(false);
  283:                   ByteArrayOutputStream b = new ByteArrayOutputStream();
  284:                   PrintStream stream = new PrintStream(b);
  285:                   e5.printStackTrace(stream);
  286:                   //connections.remove(startFile.getAbsolutePath());
  287:                   showErrorDialog(b.toString(), "Download failed");
  288:                   }      
  289:                catch(Error err)
  290:               {
  291:                  
  292:                  showErrorDialog(err.toString(), "Download failed");
  293:                 System.out.println("Error caught "+ err);
  294:               }
  295:           }
  296:      };
  297:      thread.start();
  298:     }
  299:    });
  300:    }
  301:    downloadChooser.showDialog(new JFrame()," Download ");
  302:  } 
  303:     if (e.getActionCommand().equals("Upload"))
  304:     {
  305: 
  306:       //url=getParameter("url");
  307:      if(runningAsApplet)
  308:       set();
  309:       //System.out.println("url " + url); //+ " " + getCodeBase());
  310:       upload.setEnabled(false);
  311:       makeUpload();
  312:       upload.setEnabled(true);
  313:     }
  314: 
  315:   }
  316:   public void set()
  317:   {
  318:     col = getParameter("col").trim();
  319:     grp = getParameter("grp").trim();
  320:     ses = getParameter("ses").trim();
  321:     System.out.println("uploading to grp "+grp+" col "+col);
  322:     destination = "";
  323:    // destination = getParameter("destinationFolder");
  324:     if (destination == null)
  325:       destination = "";
  326:     // e.g."/rogo/servlet/RWServlet/"
  327:     URL appletURL = getCodeBase();
  328:     String serverInfo = appletURL.getHost() + ":" + appletURL.getPort();
  329:   //  this.url = (servletURL.startsWith("http")) ? servletURL + url : "http://" + serverInfo + servletURL + url;
  330:     this.url=appletURL.toString();
  331:     Properties prop = System.getProperties();
  332:      
  333:     // prop.put("proxySet", "true"); 
  334:     // prop.put("http.proxyHost","proxy.unibe.ch");
  335:     // prop.put("proxyPort", "80"); 
  336:    //   prop.put("java.protocol.handler.pkgs","HTTPClient");
  337:     Enumeration en = prop.propertyNames();
  338:     if(debug)
  339:     while (en.hasMoreElements())
  340:     {
  341:       String key = (String) en.nextElement();
  342:       System.out.println("key: " + key + " value: " + prop.get(key));
  343: 
  344:     }
  345:      System.out.println(url+"oi.epl?mm=list&grp="+grp+"&col="+col);
  346:     
  347:   }
  348:   public static void makeUpload()
  349:   {
  350:     try
  351:     {
  352:       if (fileChooser == null)
  353:       {
  354:         fileChooser = new JFileChooser();
  355:        // fileChooser.setFileSelectionMode(fileChooser.FILES_AND_DIRECTORIES);
  356:         fileChooser.setFileFilter(new XMLFilter());
  357:         fileChooser.setDialogType(JFileChooser.OPEN_DIALOG);
  358:         fileChooser.addActionListener(new ActionListener()
  359:         {
  360:           public void actionPerformed(ActionEvent e2)
  361:           {
  362:             System.out.println(e2.getActionCommand());
  363:             if (!e2.getActionCommand().toString().equals("ApproveSelection")) return;
  364:             // {
  365: 
  366:             Thread thread = new Thread()
  367:             {
  368:               public void run()
  369:               {
  370:                 UploadInfo dialog = new UploadInfo();
  371:                
  372:                 File startFile=new File(".");
  373:                 try
  374:                 {
  375:                   dialog.thread=Thread.currentThread();
  376:                   System.out.println("Called run with " + fileChooser.getSelectedFile());
  377:                   dialog.setTitle("Upload Status Window");
  378:                   dialog.setSize(400, 175);
  379:                   dialog.setLocation(new java.awt.Point(150, 100));
  380:                   Update update = new Update();
  381:                   startFile = fileChooser.getSelectedFile(); //new File(fileChooser.getFile());
  382:                   int fileCount = 0;
  383:                   int count = 1;
  384:                   long time = System.currentTimeMillis();
  385:                    if( connections.get(startFile.getAbsolutePath())!=null) 
  386:                    throw new Error("Upload already running!");
  387:   
  388:                   if (startFile.isDirectory())
  389:                   {
  390:                     fileCount = countFiles(startFile, fileCount);
  391:                     System.out.println("Number of files to send: " + fileCount);
  392:                     bundle=getBundle(startFile);
  393:                     writeDirectory(startFile, startFile, fileCount, count, dialog, update);
  394: 
  395:                   } else
  396:                   {
  397:                     fileCount = 1;
  398:                     //writeStream(startFile, startFile, fileCount, count, dialog, update);
  399:                      bundle = getBundle(startFile);
  400:                      writeStream(startFile,bundle, dialog);
  401: 
  402:                   }
  403: 
  404:                   long time2 = System.currentTimeMillis();
  405:                   System.out.println("needed " + (time2 - time) + " ms for " + count--);
  406:                   dialog.setVisible(false);
  407:                   if (startFile.isDirectory())
  408:                     showDialog("Finished upload of " + startFile + " containing " + fileCount + " " + ((fileCount > 1) ? " files" : " file") + " in " + ((time2 - time) / 1000) + " seconds", "Finished Upload");
  409:                   else
  410:                     showDialog("Finished upload of " + startFile + " in " + ((time2 - time) / 1000) + " seconds", "Finished Upload");
  411:                  connections.remove(startFile.getAbsolutePath());
  412:                 } catch (Exception e)
  413:                 {
  414:                   dialog.setVisible(false);
  415:                   ByteArrayOutputStream b = new ByteArrayOutputStream();
  416:                   PrintStream stream = new PrintStream(b);
  417:                   e.printStackTrace(stream);
  418:                   connections.remove(startFile.getAbsolutePath());
  419:                  
  420:                   showErrorDialog(b.toString(), "Upload failed");
  421:                 } catch (Error e8)
  422:                 {
  423:                    dialog.setVisible(false);
  424:                    if( e8.toString().indexOf("Upload already running!")<0)
  425:                    connections.remove(startFile.getAbsolutePath());
  426:                    showErrorDialog(e8.toString(), "Upload failed");
  427:                 }
  428: 
  429:               }
  430:             };
  431:             thread.setPriority(Thread.MIN_PRIORITY);
  432:             /* dialog = new UploadInfo();
  433:              dialog.setTitle("Upload Status Window");
  434:              dialog.setSize(400, 155);
  435:              dialog.setLocation(new java.awt.Point(150, 100));
  436:              */
  437:             thread.start();
  438:             //          } // else
  439:             //System.exit(0);
  440:           }
  441:         });
  442: 
  443:       }
  444:       count = 1;
  445:       mode = "";
  446:       fileChooser.showDialog(new JFrame(), "Upload");
  447: 
  448:     } catch (Exception e)
  449:     {
  450:       System.out.println("Ha " + e);
  451:     }
  452:   }
  453:   private static int countFiles(File file, int fileCount)
  454:   {
  455:     if (file.isDirectory())
  456:     {
  457:       String[] files = file.list(new UploadFilter());
  458: 
  459:       for (int j = 0; j < files.length; ++j)
  460:       {
  461:         File f = new File(file + File.separator + files[j]);
  462:         if (f.isDirectory())
  463:         {
  464:           fileCount = countFiles(f, fileCount);
  465:         } else
  466:           fileCount = fileCount + 1;
  467:       }
  468: 
  469:     }
  470:     return fileCount;
  471:   }
  472: 
  473:   private static int writeDirectory(File file, File startFile, int fileCount, int count, UploadInfo dialog, Update update) throws Exception
  474:   {
  475:     if (file.isDirectory())
  476:     {
  477:       String[] files = file.list(new UploadFilter());
  478:       for (int j = 0; j < files.length; ++j)
  479:       {
  480:         File f = new File(file + File.separator + files[j]);
  481:         System.out.println(file + " " + f);
  482: 
  483:         if (f.isDirectory())
  484:         {
  485:           count = writeDirectory(f, startFile, fileCount, count, dialog, update);
  486:         } else
  487:           count = writeStream(f, startFile, fileCount, count, dialog, update);
  488:       }
  489:     }
  490:     return count;
  491:   }
  492:   private static int writeStream(File file, File startFile, int fileCount, int count, UploadInfo dialog, Update updateMode) throws Exception
  493:   {
  494:     // try
  495:     //{
  496:     String response = "", mode = "";
  497:     String path = file.getAbsolutePath();
  498:     String name = (startFile.isDirectory()) ? startFile.getName() + "/" + path.substring(startFile.getAbsolutePath().length() + 1) : file.getName();
  499:     if (destination != "")
  500:       name = (startFile.isDirectory()) ? destination + "/" + path.substring(startFile.getAbsolutePath().length() + 1) : destination + "/" + file.getName();
  501: 
  502:     System.out.println("hi " + name + " " + destination + " " + startFile.getName());
  503:     FileInputStream f = new FileInputStream(file.getAbsolutePath());
  504:     int length = f.available();
  505:     int readCount = 0;
  506:     byte[] b = (length > 512000) ? new byte[512000] : new byte[length];
  507:     System.out.println(file.getName() + " " + ((length > 1000000) ? (length / 1048576) + " MByte" : length + " Bytes"));
  508:     String fileName = file.getName() + " " + ((length > 1000000) ? (length / 1048576) + " MByte" : length + " Bytes");
  509:     dialog.title.setText("Uploading " + startFile);
  510:     dialog.progress.setValue(0);
  511:     dialog.file.setText(fileName);
  512:     dialog.status.setText(count + " of " + fileCount + ((fileCount > 1) ? " files" : " file"));
  513:     //dialog.pack();
  514:     //dialog.validate();
  515:     if (dialog.isVisible())
  516:       dialog.repaint();
  517:     else
  518:       dialog.show();
  519: /*
  520:     URL httpCon = new URL(url + getSHA((id + "").getBytes()) + "/" + convertUml(name) + "?id=" + getSHA((id + "").getBytes()) + "&count=" + count + "&nf=" + fileCount + "&md5=" + md5 + "&mode=" + mode);
  521:        Connection con = null;
  522:     con=(Connection)connections.get(startFile.getAbsolutePath());
  523:     if(con==null)
  524:     {
  525:       con=new Connection(httpCon);
  526:       connections.put(startFile.getAbsolutePath(),con);
  527:     } else
  528:     con.url=httpCon;
  529:     OutputStream mout = con.out; //con.getOutputStream();
  530:   */  
  531:   //   dialog.progress.setValue((int) (((double) readCount / (double) length) * 100.0));
  532:   // count = filecount 
  533:      Connection conPost = null;
  534:          System.out.println("hostURL "+url);
  535:      if(!runningAsApplet)
  536:      conPost=new Connection( new URL("http://core.rz-berlin.mpg.de/edoctest/oi.epl"));
  537:       else
  538:      conPost=new Connection( new URL(url+"/oi.epl"));
  539:      
  540:        // new    URL("http://localhost:8080/examples/servlet/rogo/"));
  541:       conPost.ses=ses;
  542:       conPost.dialog=dialog;
  543:       Bundle.Document doc=(Bundle.Document)bundle.documents.get(count);
  544:       System.out.println(doc.title);
  545:       conPost.post(file,"new",doc.docID+"",doc.title,doc.locator,doc.comment);
  546:   
  547:     // conPost.post(new File("./test2.jpg"),picture.elementAt(i).toString(),artist.elementAt(i).toString(),source.elementAt(i).toString());
  548:      System.out.println(getMD5(conPost.md));
  549:      dialog.progress.setValue(100);
  550:      count++;
  551:     return count;
  552:   }
  553:    private static int writeStream(File startFile,Bundle bundle, UploadInfo dialog) throws Exception
  554:   {
  555:     int count = 1,fileCount = bundle.documents.size();
  556:     int index = startFile.getAbsolutePath().lastIndexOf(File.separator);
  557:                 
  558: 
  559:     String path = startFile.getAbsolutePath().substring(0,index+1);
  560:     System.out.println(path); 
  561:     mode = "new";
  562:     boolean newVersionforAll = false, keepAllFiles = false; 
  563:     Vector list=new Vector();Vector docs=new Vector();Bundle.Document refDoc=null;
  564:     for(int k=0;k<fileCount;++k)
  565:     {
  566:        Bundle.Document doc = (Bundle.Document)bundle.documents.elementAt(k);
  567:        if((refDoc=(Bundle.Document)bundleRef.docsLocator.get(doc.locator))!=null) 
  568:        {
  569:          if(checkForNewFile(path,doc,refDoc))
  570:          {
  571:            list.addElement(doc.fileName);
  572:            docs.addElement(doc);
  573:          }
  574:        } else docs.addElement(doc);
  575:     }
  576: 
  577:     for(int k=0;k<docs.size();++k)
  578:     {
  579:       Bundle.Document doc = (Bundle.Document)docs.elementAt(k);
  580:      
  581:       String response = "";
  582:       dialog.title.setText("Uploading " + path+doc.fileName);
  583:       dialog.progress.setValue(0);
  584:       String fileName = doc.fileName+ " " + ((doc.size > 1000000) ? (doc.size / 1048576) + " MByte" : doc.size + " Bytes");
  585:       dialog.file.setText(fileName);
  586:       dialog.status.setText(count + " of " + fileCount + ((fileCount > 1) ? " files" : " file"));
  587:       //dialog.pack();
  588:       //dialog.validate();
  589:       if (dialog.isVisible())
  590:       dialog.repaint();
  591:       else
  592:       dialog.show();
  593:          if(bundleRef.docsLocator.get(doc.locator)!=null) 
  594:       {
  595:         if(!newVersionforAll&&!keepAllFiles)
  596:         {
  597:            Object[] options = new Object[] { "Cancel","Replace existing files", "Keep existing files" }; 
  598:            Object[] options2 = new Object[] { "Cancel","Replace existing file", "Keep existing file" }; 
  599: 
  600:            int option =  (list.size()>1) ? showDialog("Warning : several files  exist on the server \n Press show list to see the list! \n What do you want to do?", "several Files exist!", options,list):showDialog("Warning :  file "+doc.fileName+" exists on the server \n What do you want to do?", " File "+doc.fileName+" exists!", options2);
  601: ;
  602:            if(option == 0) break;// mode = "old";
  603:          if(option == 1) 
  604:          { 
  605:            mode ="old";
  606:            newVersionforAll = true;
  607:            keepAllFiles = false;
  608:          }
  609:          if(option == 2) 
  610:          {  
  611:            count++;
  612:            System.out.println("Heh Document exists!"+ doc);
  613:            System.out.println("Skipping");
  614:            newVersionforAll = false;
  615:            keepAllFiles = true;
  616:            continue;
  617:          }  
  618:       }
  619:       refDoc= (Bundle.Document) bundleRef.docsLocator.get(doc.locator);
  620:       doc.docID=refDoc.docID;
  621:       if(keepAllFiles)
  622:       {
  623:          count++;
  624:          continue;
  625:       }
  626:           //count++;
  627:       // System.out.println("Heh Document exists!"+ doc);
  628:       // System.out.println("Skipping");
  629:       // continue;
  630:       } else mode ="new";
  631:       Connection conPost = null;
  632:       if(!runningAsApplet)
  633:       conPost=new Connection( new URL("http://core.rz-berlin.mpg.de/edoctest/oi.epl"));
  634:       else
  635:       conPost=new Connection( new URL(url+"/oi.epl"));
  636:       
  637:       // new    URL("http://localhost:8080/examples/servlet/rogo/"));
  638:       conPost.ses=ses;
  639:       conPost.dialog=dialog;
  640:       conPost.post(path,doc,mode);
  641:       String md5=getMD5(conPost.md);
  642:       System.out.println("MD5 is equal ?"+doc.md5cs.equals(md5)+" doc_md5 "+doc.md5cs+" md5 "+md5);
  643:       if(!doc.md5cs.equals(md5))
  644:       {
  645:          Object[] options = { "Retry", "Cancel" };  
  646:          int option = showDialog("Transfer failed! \n What do you want to do?", "Transfer failed ", options);
  647:          if(option == 0)
  648:          {
  649:             if(!runningAsApplet)
  650:             conPost=new Connection( new URL("http://core.rz-berlin.mpg.de/edoctest/oi.epl"));
  651:             else
  652:             conPost=new Connection(new URL(url+"/oi.epl"));
  653:             conPost.ses=ses;
  654:             conPost.dialog=dialog;
  655:             conPost.post(path,doc,mode);      
  656:             md5=getMD5(conPost.md);
  657:             if(!doc.md5cs.equals(md5)) showDialog("Transfer failed","Transfer failed again! ");
  658:          }
  659:       }      // System.out.println(getMD5(md));
  660:       dialog.progress.setValue(100);
  661:       count++;
  662:      }
  663:      bundle.exportToXML(new File(path+"/temp.xml"));
  664:      return count;
  665:   }
  666:  
  667:   public static String getMD5(MessageDigest md) throws Exception
  668:   {
  669:     byte[] bytes = md.digest();
  670:    // System.out.println("digest " + bytes.length);
  671:     StringBuffer sb = new StringBuffer();
  672:     int decValue;
  673:     for (int i = 0; i < bytes.length; i++)
  674:     {
  675:       String hexVal = Integer.toHexString(bytes[i] & 0xFF);
  676:       if (hexVal.length() == 1)
  677:         hexVal = "0" + hexVal; // put a leading zero
  678:       sb.append(hexVal);
  679:     }
  680:     return sb.toString();
  681:   }
  682:   public static String getSHA(byte[] b) throws Exception
  683:   {
  684:     MessageDigest md = MessageDigest.getInstance("SHA");
  685:     md.update(b, 0, b.length);
  686:     //System.out.println("digest");
  687:     byte[] bytes = md.digest();
  688:     StringBuffer sb = new StringBuffer();
  689:     int decValue;
  690:     for (int i = 0; i < bytes.length; i++)
  691:     {
  692:       String hexVal = Integer.toHexString(bytes[i] & 0xFF);
  693:       if (hexVal.length() == 1)
  694:         hexVal = "0" + hexVal; // put a leading zero
  695:       sb.append(hexVal);
  696:     }
  697: 
  698:     return sb.toString();
  699:   }
  700:   public static boolean checkForNewFile(String path,Bundle.Document doc,Bundle.Document refDoc) throws Exception
  701:   {
  702:      String name = doc.fileLink.trim().startsWith("file://") ?doc.fileLink.trim().substring(7):doc.fileLink.trim();
  703:      //System.out.println(name);
  704:     boolean isNewFile=false;
  705:      String filePath = name.startsWith("/") ? name:path+name;
  706:      File file = new File(filePath);
  707:      if(!file.exists()) 
  708:      {
  709:       
  710:       showErrorDialog("Warning \n"+filePath +" does not exist !","Warning file does not exist !");
  711:       return false;
  712:      }
  713:      if(file.isDirectory()) return false;
  714:      FileInputStream f=new FileInputStream( filePath);
  715:      int length=f.available();
  716:      if(length!=refDoc.size) isNewFile=true;
  717:       else
  718:       {
  719:          
  720:           //new File(filePath).setLastModified(System.currentTimeMillis()-690000000L);
  721:          Date actualTime = new Date(new File(filePath).lastModified());
  722:          Date modified = new Date(doc.modified);//new File(filePath).lastModified());
  723:         // System.out.println("actual Date "+actualTime);
  724:         // System.out.println("Date file modified "+modified);
  725:         // System.out.println("Time difference in minutes "+((actualTime.getTime()-modified.getTime())/60000L));
  726:          // if file has been modified longer than 10 hours do not compare md5 --> file is not newer
  727:          if(actualTime.getTime()==modified.getTime()) 
  728:          {
  729:           f.close();
  730:           return false; 
  731:          }
  732:         
  733:          MessageDigest md = MessageDigest.getInstance("MD5");
  734:         byte[] b=new byte[512000];
  735:         int   readCount =0;
  736:         while (readCount < length)
  737:         {
  738:           if (readCount + b.length > length)
  739:             f.read(b, 0, length - readCount); //  mout.flush();
  740:           else
  741:             f.read(b);
  742:           if (readCount + b.length > length)
  743:           {
  744:             md.update(b, 0, length - readCount);
  745:           }
  746:           else
  747:          {
  748:             md.update(b, 0, b.length);
  749:          }
  750:         readCount += b.length;
  751:         }
  752:      isNewFile = !getMD5(md).equals(refDoc.md5cs);
  753:      
  754:      }
  755:       // close file
  756:       f.close();
  757:    if(isNewFile)
  758:      System.out.println(name+" "+refDoc.fileName);
  759:  //  System.out.println("is new file ? "+isNewFile+" length is equal ?"+(length==refDoc.size));
  760:    return isNewFile;
  761:    }     
  762:   public static boolean checkForFileDownloaded(String path,Bundle.Document doc,Bundle.Document refDoc) throws Exception
  763:   {
  764:      String name = doc.fileLink.trim().startsWith("file://") ?doc.fileLink.trim().substring(7):doc.fileLink.trim();
  765:      //System.out.println(name);
  766:      boolean isNewFile=false;
  767:      String filePath = name.startsWith("/") ? name:path+name;
  768:      File file = new File(filePath);
  769:      if(!file.exists()) 
  770:      {
  771:        refDoc.downloadAgain = true;
  772:        return true;
  773:      }
  774:      if(file.isDirectory()) return false;
  775:       FileInputStream f=new FileInputStream( filePath);
  776:      int length=f.available();
  777:      if(length!=refDoc.size) 
  778:      {
  779:         // System.out.println(doc.fileName+" "+refDoc.fileName+" "+doc.size+" "
  780:        // +refDoc.size+" "+length);
  781:        refDoc.downloadAgain = doc.size>length;
  782:        isNewFile = true;
  783:      }
  784:      else
  785:      {
  786:        isNewFile = !doc.md5cs.equals(refDoc.md5cs);
  787:      }
  788:     
  789:    f.close();
  790:    if(isNewFile)
  791:      System.out.println(name+" "+refDoc.fileName);
  792:  //  System.out.println("is new file ? "+isNewFile+" length is equal ?"+(length==refDoc.size));
  793:    
  794:    return isNewFile;
  795:    }     
  796:   
  797:   public static String convertUml(String newName)
  798:   {
  799:     StringBuffer alterMe = new StringBuffer(newName.trim());
  800:     int length = alterMe.length();
  801:     int j = 0;
  802:     while (j < length)
  803:     { //if(Character.isSpaceChar(alterMe.charAt(j)))
  804:       //  alterMe.setCharAt(j,'_');
  805:       if (alterMe.charAt(j) == ' ')
  806:       {
  807:         alterMe.setCharAt(j, '%');
  808:         alterMe.insert(j + 1, "20");
  809:         length = length + 2;
  810:       }
  811:       /* if(Character.isSpaceChar(alterMe.charAt(j))
  812:        alterMe.setCharAt(j,'_');
  813:       */
  814:       ++j;
  815:     }
  816:     return alterMe.toString();
  817:   }
  818:   public static class UploadInfo extends JDialog
  819:   {
  820:     JLabel file = new JLabel("    ");
  821:     JLabel status = new JLabel("    ");
  822:     JLabel title = new JLabel("     ");
  823:     JPanel content = new JPanel();
  824:     JPanel titlePanel = new JPanel();
  825:     JPanel labelPanel = new JPanel();
  826:     JPanel statusPanel = new JPanel();
  827:     JPanel buttonPanel = new JPanel();
  828: 
  829:     JProgressBar progress = new JProgressBar();
  830:     JButton cancel = new JButton("Cancel");
  831:     Thread thread;
  832:     public UploadInfo()
  833:     {
  834: 
  835:       content = new JPanel(true);
  836:       //content.setBorder(BorderFactory.createRaisedBevelBorder());
  837:       content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
  838:       title = new JLabel("Uploading " + startFile);
  839:       title.setHorizontalTextPosition(title.CENTER);
  840:       titlePanel.add(title);
  841:       buttonPanel.add(cancel);
  842:       // content.add(titlePanel);
  843:       content.add(Box.createRigidArea(new java.awt.Dimension(0, 15)));
  844:       content.add(labelPanel);
  845:       content.add(new JLabel(""));
  846:       content.add(progress);
  847:       content.add(statusPanel);
  848:       content.add(buttonPanel);
  849:       content.add(Box.createRigidArea(new java.awt.Dimension(0, 15)));
  850:       labelPanel.add(file);
  851:       statusPanel.add(status);
  852:       getContentPane().add(titlePanel, "North");
  853:       getContentPane().add(content, "Center");
  854:       cancel.addActionListener(new ActionListener()
  855:       {
  856:         public void actionPerformed(ActionEvent e)
  857:         {
  858:           setVisible(false);
  859:           thread.stop();
  860:         }
  861: 
  862:       });
  863:       pack();
  864: 
  865:     }
  866: 
  867:   }
  868:  static class Update
  869:   {
  870:     boolean value = false;
  871:     public Update()
  872:     {
  873:       value = false;
  874:     }
  875:   }
  876:   public static int showDialog(String message, String title, Object[] options)
  877:   {
  878:     int option = JOptionPane.showOptionDialog(null, message, title, JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]);
  879:     return option;
  880: 
  881:   }
  882:   public static int showDialog(String message, String title, Object[] options,Vector fileList)
  883:   {
  884:     JOptionPane pane = new JOptionPane( message, JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]);
  885:     pane.setMessageType(JOptionPane.WARNING_MESSAGE); // Configure
  886:     final JDialog dialog = pane.createDialog(null, title);
  887:     JList list=null;
  888:     if(fileList==null)
  889:     list= new JList(new String[]{"click","here","test","bla","world","hello"});
  890:     else list = new JList(fileList);
  891:   
  892:     JScrollPane scpane = new JScrollPane(list);
  893:     scpane.setPreferredSize(new Dimension(150,150));
  894:     
  895:     JLabel label =  new JLabel("Files to be updated : ");
  896:     label.setPreferredSize(new Dimension(150,30));
  897:     final JPanel panel = new JPanel();
  898:     panel.setLayout(new BorderLayout());
  899:     panel.add(label,"North");
  900:     panel.add(scpane,"Center");
  901:     JButton showList= new JButton("show List");
  902:     showList.addActionListener(new ActionListener() 
  903:     {
  904:       public void actionPerformed(ActionEvent e) 
  905:       {
  906:         JDialog window = new JDialog(dialog);
  907:         window.getContentPane().add(panel);
  908:         window.pack();
  909:         window.show();
  910:       }
  911:     });
  912:     JPanel buttonPanel = new JPanel();
  913:     buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS)); 
  914:     buttonPanel.add(Box.createRigidArea(new Dimension(0,30)));
  915:     buttonPanel.add(showList);  
  916:     dialog.getContentPane().add(buttonPanel,"East");
  917:     dialog.pack();
  918:     dialog.show();
  919:     Object selectedValue = pane.getValue();
  920:     if(selectedValue == null)
  921:     return 2;
  922:     //If there is not an array of option buttons:
  923:     if(options == null) {
  924:     if(selectedValue instanceof Integer)
  925:     return ((Integer)selectedValue).intValue();
  926:     return   2;
  927:     }
  928:     //If there is an array of option buttons:
  929:     for(int counter = 0, maxCounter = options.length;counter < maxCounter; counter++) 
  930:     {
  931:       if(options[counter].equals(selectedValue))
  932:       return counter;
  933:     }
  934:     return 2;
  935:     
  936:   }
  937: 
  938:   public static void showDialog(String message, String title)
  939:   {
  940:     JOptionPane.showMessageDialog(null, message, title, JOptionPane.PLAIN_MESSAGE);
  941: 
  942:   }
  943:   public static void showErrorDialog(String message, String title)
  944:   {
  945:     JOptionPane.showMessageDialog(null, message, title, JOptionPane.ERROR_MESSAGE);
  946:   }
  947: 
  948: 
  949:  
  950:  // end applet code
  951:  
  952:   public static StringBuffer readChunked(Connection conTest) throws Exception
  953:   {
  954:     InputStream r= conTest.in;
  955:     //InputStreamReader r= new InputStreamReader(new FileInputStream("FMPro.xml"),"UTF-8"); 
  956:     //InputStreamReader r=// new InputStreamReader(new URL(fileMakerTest).openStream(),"UTF-8");
  957:     //new InputStreamReader(conTest.in,"UTF-8");
  958:     //while(conTest.in.available()==0);
  959:     //u.openStream();
  960:     //InputStream r = new InputStream(f);
  961:     StringBuffer sb = new StringBuffer();
  962:     int c,countC=0,limit=conTest.in.available();
  963:     // r.skip(211);
  964:     String response = conTest.getResponseMessage();
  965:     String lowerCase= response.toLowerCase();
  966:     int index =lowerCase.indexOf("charset");
  967:     int index2 =lowerCase.indexOf("=",index);
  968:     int index3 =lowerCase.indexOf("\r",index2);
  969:     String charset=(index>0) ? response.substring(index2+1,index3):"";
  970:     charset="";//charset.toUpperCase();
  971:     System.err.println(response);
  972:     if(lowerCase.indexOf("chunked")<0)
  973:     {
  974:       ByteArrayOutputStream local=new ByteArrayOutputStream();
  975:       while(r.available()==0); 
  976:       System.err.println(r.available());
  977:       while ((c = r.read()) != -1) 
  978:       {
  979:         byte b=(byte)c;
  980:         local.write(b);             
  981:         if(debug)
  982:         System.out.print((char)c);
  983:       }
  984:       if(charset!="")
  985:       sb.append(local.toString(charset));
  986:       else
  987:       sb.append(local.toString());
  988:       if(debug)
  989:       System.out.print(sb);
  990:       return sb;
  991:     }
  992:       
  993:     // read chunk size
  994:     while ((c = r.read()) != -1) 
  995:     {
  996:       ++countC;
  997:       char ch=(char)c;
  998:       
  999:       if(Character.digit(ch, 16) != -1)
 1000:       sb.append((char)c);
 1001:       if(ch=='\r')
 1002:       {
 1003:         ch=(char)r.read();
 1004:         if( ch=='\n') 
 1005:         break;
 1006:       }
 1007:       // System.out.print((char)c);
 1008:     }   
 1009:     int bufSize=Integer.parseInt(sb.toString().trim(), 16);
 1010:     
 1011:     System.err.println("chunk size "+Integer.parseInt(sb.toString().trim(), 16)+" bytes"+" limit "+limit);
 1012:     //sb.append((char)r.read());
 1013:     // sb.append((char)r.read());
 1014:     sb=new StringBuffer();//.delete(0,sb.length());
 1015:     byte[] b=new byte[bufSize];
 1016:     countC=0;
 1017:     StringBuffer localBuff=new StringBuffer();
 1018:     while (true) 
 1019:     {
 1020:       // sb.append(" new  Chunk "+r.available()+" \r\n ");
 1021:        int  readCount = 0;
 1022:       int available  = 0;
 1023:       int readSize    = b.length; int loop=0;
 1024:        while (readCount < bufSize)
 1025:       {
 1026:         available=r.available();
 1027: 
 1028:         if(available>=bufSize)
 1029:         readSize=bufSize;
 1030:         else
 1031:         readSize=available;
 1032: 
 1033:       if(readCount + readSize > bufSize)
 1034:       {
 1035:         readSize = r.read(b, readCount, bufSize - readCount); //  mout.flush();
 1036: 	
 1037:       }
 1038:       else
 1039:       {
 1040:         readSize = r.read(b,readCount,readSize);
 1041:       }
 1042:     if(loop>=200) throw new Error("Connection timed out");
 1043:     if(readSize==0) 
 1044:     {
 1045:       long time = System.currentTimeMillis();
 1046:       while(System.currentTimeMillis()-time<100);
 1047:     } else loop=0;
 1048:     
 1049:     if(debug)
 1050:     System.out.println(++loop+" read "+readCount+" bytes"+" readSize "+readSize);
 1051:     readCount+=readSize;
 1052:     
 1053:     }
 1054:       
 1055:       
 1056:       //while(r.available()<bufSize); // critical if connection closed
 1057:     //  r.read(b,0,bufSize); 
 1058:       //System.err.println(r.available());
 1059:       r.read();r.read();
 1060:       if(charset!="")
 1061:       sb.append(new String(b,0,bufSize,charset));
 1062:       else
 1063:       sb.append(new String(b,0,bufSize));
 1064:       if(debug)
 1065:       System.out.println(new String(b,0,bufSize));
 1066:       while ( (c = r.read()) != -1) 
 1067:       {
 1068:         //      ++countC;
 1069:         char ch=(char)c;
 1070:         //System.err.println("read lf "+(ch=='\r')+" or cr "+( ch=='\n')+" "+(Character.digit(ch, 16) == -1));
 1071:         //if((ch!='\r')&&( ch!='\n')) System.err.println("read ch "+" "+ch+" "+Character.isSpace(ch));
 1072:         if(Character.digit(ch, 16) != -1)
 1073:         localBuff.append(ch);
 1074:         if(ch=='\r')
 1075:         {
 1076:           ch=(char)r.read();
 1077:           if( ch=='\n') 
 1078:           break;
 1079:         }
 1080:       }
 1081:        if(debug)
 1082:        System.out.println("chunk size "+localBuff);
 1083:       if(localBuff.length()>0)
 1084:       {
 1085:         // System.err.println("new chunk size "+Integer.parseInt(localBuff.toString().trim(), 16)+" bytes");
 1086:         // System.err.println((char)r.read());
 1087:         bufSize=Integer.parseInt(localBuff.toString().trim(), 16);
 1088:         //localBuff.delete(0,localBuff.length());
 1089:          localBuff=new StringBuffer();
 1090:       } // else bufSize=0;
 1091:       ++countC;
 1092:      // sb.append("new  Chunk"+r.available()+"\r\n ");
 1093:      if(bufSize==0)//(bufSize)) 
 1094:       break;
 1095:       // System.err.println(" in  Chunk "+bufSize+"   buffers used "+(countC)+" \r\n ");
 1096:       // // System.out.print((char)c);
 1097:     }
 1098:     // System.out.println(sb);
 1099:     return sb;
 1100:   }
 1101:   public static void writeChunked(Connection conTest,File f,int count,int fileCount,int fileSize,UploadInfo dialog) throws Exception
 1102:   {
 1103:     InputStream r= conTest.in;
 1104:     FileOutputStream file_out = new  FileOutputStream(f);
 1105:     BufferedOutputStream fout = new BufferedOutputStream(file_out);
 1106:     int c,countC=0,limit=conTest.in.available();
 1107:     // r.skip(211);
 1108:     String response = conTest.getResponseMessage();
 1109:     String lowerCase= response.toLowerCase();
 1110:     int index =lowerCase.indexOf("charset");
 1111:     int index2 =lowerCase.indexOf("=",index);
 1112:     int index3 =lowerCase.indexOf("\r",index2);
 1113:     String charset=(index>0) ? response.substring(index2+1,index3):"";
 1114:     charset="";//charset.toUpperCase();
 1115:     System.err.println(response);
 1116:     dialog.title.setText("Downloading to " + f.toString());
 1117:     dialog.progress.setValue(0);
 1118:      MessageDigest md =conTest.md;
 1119:      md.reset();
 1120:     String fileName = f.getName()+ " " + ((fileSize > 1000000) ? (fileSize / 1048576) + " MByte" : fileSize+ " Bytes");
 1121:       dialog.file.setText(fileName);
 1122:       dialog.status.setText(count + " of " + fileCount + ((fileCount > 1) ? " files" : " file"));
 1123:       //dialog.pack();
 1124:       //dialog.validate();
 1125:       if (dialog.isVisible())
 1126:       dialog.repaint();
 1127:       else
 1128:       dialog.show();
 1129:   
 1130:     if(lowerCase.indexOf("chunked")<0)
 1131:     {
 1132:       while(r.available()==0); 
 1133:       System.err.println(r.available());
 1134:       while ((c = r.read()) != -1) 
 1135:       {
 1136:         byte b=(byte)c;
 1137:         fout.write(b);             
 1138:         //md.update(b, 0, b.length);
 1139:         if(debug)
 1140:         System.out.print((char)c);
 1141:       }
 1142:     /* 
 1143:      if(charset!="")
 1144:       fout.write(local.toString(charset));
 1145:       else
 1146:       fout.write(local.toString());
 1147:       */
 1148:       //if(debug)
 1149:      // System.out.print(sb);
 1150:       //return sb;
 1151:     }
 1152:      StringBuffer sb = new StringBuffer(); 
 1153:     // read chunk size
 1154:     while ((c = r.read()) != -1) 
 1155:     {
 1156:       ++countC;
 1157:       char ch=(char)c;
 1158:       
 1159:       if(Character.digit(ch, 16) != -1)
 1160:       sb.append((char)c);
 1161:       if(ch=='\r')
 1162:       {
 1163:         ch=(char)r.read();
 1164:         if( ch=='\n') 
 1165:         break;
 1166:       }
 1167:        //System.out.print((char)c);
 1168:     }   
 1169:     int bufSize=Integer.parseInt(sb.toString().trim(), 16),newBufSize=0;
 1170:     
 1171:     System.err.println("chunk size "+Integer.parseInt(sb.toString().trim(), 16)+" bytes"+" limit "+limit);
 1172:     // sb.append((char)r.read());
 1173:     // sb.append((char)r.read());
 1174:     sb = new StringBuffer();//.delete(0,sb.length());
 1175:     byte[] b=new byte[bufSize];
 1176:     countC=0;int size = 0;
 1177:     StringBuffer localBuff=new StringBuffer();
 1178:     while (true) 
 1179:     {
 1180:       // sb.append(" new  Chunk "+r.available()+" \r\n ");
 1181:       int  readCount = 0;
 1182:       int available  = 0;
 1183:       int readSize    = b.length; int loop=0;
 1184:       while (readCount < bufSize)
 1185:       {
 1186:         available=r.available();
 1187: 
 1188:         if(available>=bufSize)
 1189:         readSize=bufSize;
 1190:         else
 1191:         readSize=available;
 1192: 
 1193:       if(readCount + readSize > bufSize)
 1194:       {
 1195:          readSize = r.read(b, readCount, bufSize - readCount); 
 1196:       }
 1197:       else
 1198:       {
 1199:         readSize = r.read(b,readCount,readSize);
 1200:       }
 1201:       if(loop>=60) 
 1202:       {
 1203:         loop = 0;
 1204:         conTest.socket.close();
 1205:         conTest.reconnect();
 1206:         conTest.get();
 1207:         r= conTest.in;
 1208:         file_out = new  FileOutputStream(f);
 1209:         fout = new BufferedOutputStream(file_out);
 1210:         countC=0;size=0;readCount= 0;conTest.md.reset();
 1211:         response = conTest.getResponseMessage();
 1212:         System.out.println(response);
 1213:         while ((c = r.read()) != -1) 
 1214:         {
 1215:           ++countC;
 1216:           char ch=(char)c;
 1217:           
 1218:           if(Character.digit(ch, 16) != -1)
 1219:           sb.append((char)c);
 1220:           if(ch=='\r')
 1221:           {
 1222:             ch=(char)r.read();
 1223:             if( ch=='\n') 
 1224:             break;
 1225:           }
 1226:           // System.out.print((char)c);
 1227:         }   
 1228:        bufSize=Integer.parseInt(sb.toString().trim(), 16);
 1229:      }
 1230:       //throw new Error("Connection timed out");
 1231:       if(readSize==0) 
 1232:       {
 1233:         long time = System.currentTimeMillis();
 1234:         while(System.currentTimeMillis()-time<100);
 1235:       } else loop=0;
 1236:       
 1237:     //if(debug)
 1238:    // System.out.println(++loop+" read "+readCount+" bytes"+" readSize "+readSize);
 1239:     readCount+=readSize;
 1240:     
 1241:   }
 1242:       
 1243:       //while(r.available()<bufSize); // critical if connection closed
 1244:     //  r.read(b,0,bufSize); 
 1245:       //System.err.println(r.available());
 1246:       char ch2=(char)r.read();
 1247:      // if(ch2=='\r') System.out.print("CR");
 1248:       ch2=(char)r.read();
 1249:      // if(ch2=='\n') System.out.println("LF");
 1250:      // if(debug)
 1251:      // System.out.println(new String(b,0,bufSize));
 1252:       while ( (c = r.read()) != -1) 
 1253:       {
 1254:         //      ++countC;
 1255:         char ch=(char)c;
 1256:         //System.err.println("read lf "+(ch=='\r')+" or cr "+( ch=='\n')+" "+(Character.digit(ch, 16) == -1));
 1257:         //if((ch!='\r')&&( ch!='\n')) System.err.println("read ch "+" "+ch+" "+Character.isSpace(ch));
 1258:        // System.out.print(ch);
 1259:         if(Character.digit(ch, 16) != -1)
 1260:         localBuff.append(ch);
 1261:         if(ch=='\r')
 1262:         {
 1263:           ch=(char)r.read();
 1264:           if( ch=='\n') 
 1265:           break;
 1266:         }
 1267:       }
 1268:       if(localBuff.length()>0)
 1269:       {
 1270:         // System.err.println("new chunk size "+Integer.parseInt(localBuff.toString().trim(), 16)+" bytes");
 1271:         // System.err.println((char)r.read());
 1272:         newBufSize=Integer.parseInt(localBuff.toString().trim(), 16);
 1273:         //if(bufSize==3041) bufSize=3040;
 1274:         if(debug)
 1275:        System.out.println("chunk size "+Integer.parseInt(localBuff.toString().trim(), 16)+" "+size);
 1276:   
 1277:         //localBuff.delete(0,localBuff.length());
 1278:          localBuff=new StringBuffer();
 1279:       } // else bufSize=0;
 1280:        if((char)b[bufSize-1]=='\n')
 1281:     {
 1282:         if(debug)
 1283:         System.out.println("LF found "+((char)b[bufSize-1]=='\n'));
 1284:         if(newBufSize==0) bufSize=bufSize-1;
 1285:         
 1286:     }
 1287:      fout.write(b,0,bufSize);
 1288:      md.update(b, 0, bufSize);
 1289:      size += bufSize;
 1290:      bufSize=newBufSize;
 1291:      dialog.progress.setValue((int) (((double) size / (double) fileSize) * 100.0));     
 1292:        ++countC;
 1293:      if(bufSize==0)
 1294:       break;
 1295:       // System.err.println(" in  Chunk "+bufSize+"   buffers used "+(countC)+" \r\n ");
 1296:       // // System.out.print((char)c);
 1297:     }
 1298:     // System.out.println(sb);
 1299:     fout.flush();
 1300:     fout.close();
 1301:   
 1302:     file_out.close();
 1303:     //return sb;
 1304:   }
 1305: 
 1306:   public static Hashtable getMetaData(String url) 
 1307:   {
 1308:     Hashtable data=new Hashtable();
 1309:     Vector artist=new Vector();
 1310:     Vector picture=new Vector();
 1311:     Vector idnr   = new Vector();
 1312:     Vector source   = new Vector();
 1313:     data.put("artist",artist);
 1314:     data.put("picture",picture);
 1315:     data.put("IDNR",idnr);
 1316:     data.put("source",source);
 1317:     try
 1318:     {
 1319:       String   fileMakerTest="http://localhost/FMRes/FMPro?-db=Bildarchiv%20Daston&-layID=69&-token=25&-max=1000&-format=-dso_xml&-mode=browse&-findall";
 1320:       String fileMakerTest2="http://localhost/FMRes/FMPro?-db=bildarchiv%20daston&-layid=1&-format=-dso_xml&-max=899&-skip=6&-token.0=25&-mode=browse&-lop=and&-find";
 1321:       //Connection con= new Connection(new URL("http://core.rz-berlin.mpg.de:80/edoctest/adm.epl"));
 1322:       //String ses=con.getSessionID("name=root&passwd=&mode=verifylogin");
 1323:       
 1324:       Connection conTest=new Connection(new URL(fileMakerTest));//new URL(url));
 1325:       //  conTest.ses=ses;
 1326:       conTest.get();
 1327:       StringBuffer sb=readChunked(conTest);
 1328:       if(debug)
 1329:       System.out.println("file "+sb);
 1330:       // parse string and build document tree
 1331:       Node  root = new Xparse().parse(sb.toString());
 1332:       Vector contents=root.index.v;
 1333:       for (int i=0;i<contents.size();++i)
 1334:       {
 1335:         Node n=(Node)contents.elementAt(i);
 1336:         if(n.type.equals("element"))
 1337:         {
 1338:           // System.out.println("tag "+n.name+" with contents ");
 1339:           // System.out.println(n.getCharacters());
 1340:           if(n.name.indexOf("artist")>=0) artist.addElement(n.getCharacters());
 1341:           if(n.name.indexOf("picture")>=0) picture.addElement(n.getCharacters());
 1342:           if(n.name.indexOf("IDNR")>=0) idnr.addElement(n.getCharacters());
 1343:           if(n.name.indexOf("source")>=0) source.addElement(n.getCharacters());
 1344:           //contents=n.contents.v;i=0;
 1345:         }
 1346:       }
 1347:     }catch(Exception e8) { e8.printStackTrace(); }
 1348:     return data;
 1349:   }
 1350: 
 1351:   public static Bundle getBundle(File f) 
 1352:   {
 1353:      File xmlFile=null;
 1354:      Bundle bundle=new Bundle();
 1355:      if(f.isDirectory())
 1356:      {
 1357:        String[] files=f.list();
 1358:        for(int j=0;j<files.length;++j)
 1359:        {
 1360:          if(files[j].endsWith("xml"))
 1361:          {
 1362:            xmlFile=new File(f.getAbsolutePath()+"/"+files[j]);
 1363:            break;
 1364:          }
 1365:        } 
 1366:     } else xmlFile=f;
 1367:        try 
 1368:        {
 1369:           // read XML Metadata from a file
 1370:           FileReader r=new FileReader(xmlFile);
 1371:           BufferedReader buffr = new BufferedReader(r);
 1372:           StringBuffer sb=new StringBuffer();
 1373:           int c=0;
 1374:           while ((c = buffr.read()) != -1) 
 1375:           {
 1376:             char ch=(char)c;
 1377:             sb.append(ch);
 1378:             // System.out.print((char)c);
 1379:           }
 1380:           addToBundle(bundle,sb);
 1381:       } catch(Exception e) { e.printStackTrace();} 
 1382:         
 1383:     
 1384:     return bundle;
 1385:   }
 1386:   public static Bundle getBundle(String url) 
 1387:   {
 1388:     Bundle bundle=new Bundle();
 1389:     
 1390:     try 
 1391:     {
 1392:       if(url=="") url="http://core.rz-berlin.mpg.de:80/edoctest/oi.epl?mm=list&grp=90";
 1393:       // read XML document from URL into a string
 1394:       URL u = new URL(url);
 1395:       //  FileReader r=new FileReader();
 1396:       long time = System.currentTimeMillis();
 1397: 
 1398:       //Connection con= new Connection(new URL("http://core.rz-berlin.mpg.de:80/edoctest/adm.epl"));
 1399:      // String ses=con.getSessionID("name=root&passwd=edoctest&mode=verifylogin");
 1400:     //  con.socket.close();
 1401:       Connection conTest=new Connection(new URL(url));
 1402:       conTest.ses=ses;
 1403:       conTest.get();
 1404:       StringBuffer sb=readChunked(conTest);
 1405:        // sb.delete( sb.toString().indexOf("<rt>"),sb.length());
 1406:       //System.out.println(sb);
 1407:       String rt =addToBundle(bundle,sb);
 1408:       StringBuffer  sb2=null;
 1409:       int index=sb.toString().indexOf("<edoc>",3)+6,index2=0;
 1410:       //for(int i=20;i<260;i=i+20)
 1411:       
 1412:       
 1413:       while(true)
 1414:       {
 1415:         if(rt.equals("finished")) break;
 1416:         conTest.url=new URL(url+"&rt="+rt);
 1417:         conTest.get();
 1418:         sb2=readChunked(conTest);
 1419:         //sb2.delete(0,index);
 1420:         // index2=sb2.toString().indexOf("<rt>",index);
 1421:         // sb2.delete(index2,sb2.length());
 1422:          //sb.append(sb2);
 1423:          rt=addToBundle(bundle,sb2);
 1424:       }
 1425:        //sb.append("</edoc>");
 1426:       //System.out.println(sb);
 1427:       long timeEnd = System.currentTimeMillis();
 1428:       System.err.println("time to get bundle data "+(timeEnd-time));
 1429:       
 1430:       /*   
 1431:       // read XML Metadata from a file
 1432:       FileReader r=new FileReader("list7.xml");
 1433:       sb=new StringBuffer();
 1434:       // String response= conTest.getResponseMessage();
 1435:       int c=0;
 1436:       while ((c = r.read()) != -1) 
 1437:       {
 1438:         char ch=(char)c;
 1439:         sb.append(ch);
 1440:         // System.out.print((char)c);
 1441:       }
 1442:       */
 1443:       String data=sb.toString().length()+"";
 1444:       // System.out.println(new String(sb.toString().getBytes(),"UTF-8"));
 1445:       // System.out.println(sb.toString());
 1446:       //addToBundle(bundle,sb);
 1447:       // System.out.println(sb.toString());
 1448:       // System.exit(0);
 1449:       conTest.socket.close();
 1450:     } catch(Exception e) {e.printStackTrace();} 
 1451:     return bundle;
 1452:   }
 1453:   public static String  addToBundle(Bundle bundle,StringBuffer sb)
 1454:   {
 1455:     boolean finished = false;
 1456:     String  rtTag="finished";
 1457:     // parse string and build document tree
 1458:     Node  root = new Xparse().parse(sb.toString());
 1459:     /*  
 1460:     Vector contents=root.index.v;
 1461:     for (int i=0;i<contents.size();++i)
 1462:     {
 1463:       Node n=(Node)contents.elementAt(i);
 1464:       if(n.type.equals("element"))
 1465:       {
 1466:         // System.out.println("tag "+n.name+" with contents ");
 1467:         // System.out.println(n.getCharacters());
 1468:         //contents=n.contents.v;i=0;
 1469:       }
 1470:       // System.out.println(n.type);
 1471:     
 1472:     }
 1473:     */
 1474:     Node tempNode=root.find(rootNode,new int[] {1});
 1475:     if(tempNode==null) return rtTag;
 1476:     int count=1;
 1477:     /* 
 1478:     for(int i=0;i<tempNode.contents.v.size();++i)
 1479:     {
 1480:       // System.out.println(((Node)tempNode.contents.v.elementAt(i)).attributes+" "+i);
 1481:     }*/
 1482:     if(tempNode.contents.v.size()==0) return rtTag;
 1483:     Node notNull=null;
 1484:     Node rtNode=root.find(rootNode+"/rt",new int[] {1,1});
 1485:     rtTag=(rtNode==null) ? "finished":rtNode.getCharacters();
 1486:     System.out.println("rt tag is "+ rtTag);
 1487:     int length=0;//(tempNode.contents.length()-1)/2;
 1488:     for(int i=0;i<tempNode.contents.v.size();++i)    {
 1489:       Node node=(Node)tempNode.contents.v.elementAt(i);
 1490:       if(node.type.equals("element"))
 1491:       if(node.name.equals("doc"))length++;
 1492:       // System.out.println(((Node)tempNode.contents.v.elementAt(i)).attributes+" "+i);
 1493:     }
 1494:    if(length==0) rtTag="finished";        
 1495:     while(count<=length)
 1496:     {
 1497:        //System.out.println(count+" "+length);
 1498:       tempNode=root.find(rootNode+"/doc",new int[] {1,count});
 1499:       if(tempNode==null)
 1500:       {
 1501:         rtTag="finished";
 1502:         break;
 1503:       }    
 1504:       Bundle.Document doc = new Bundle.Document();
 1505:       Hashtable attributes=tempNode.attributes;
 1506:       int docID=0;
 1507:       int ver=0;
 1508:        if(attributes != null)
 1509:       {
 1510:         docID=Integer.parseInt((String)attributes.get("id"));
 1511:         ver=Integer.parseInt((String)attributes.get("ver"));
 1512:         doc.addData("docID",(String)attributes.get("id"));
 1513:         doc.addData("ver",(String)attributes.get("ver"));
 1514:       }
 1515:       bundle.docs.put(new Integer(docID),doc);
 1516:       bundle.documents.addElement(doc);
 1517:       // System.out.println("id " +docID+" ver "+ver);
 1518:       Node fileNode=root.find(rootNode+"/doc/file",new int[] {1,count,1});
 1519:       attributes=(fileNode==null)? null:fileNode.attributes;
 1520:       int fileID=0;
 1521:       int fileVer=0;
 1522:       if(attributes != null)
 1523:       {
 1524:         fileID=Integer.parseInt((String)attributes.get("id"));
 1525:         fileVer=Integer.parseInt((String)attributes.get("ver"));
 1526:         doc.addData("fileID",(String)attributes.get("id"));
 1527:         doc.addData("fileVer",(String)attributes.get("ver"));
 1528:         // System.out.println("fileid " +fileID+" ver "+fileVer);
 1529:       }
 1530:    // add all Document tags to actual Document
 1531:       for(int i=0;i<tempNode.contents.v.size();++i)
 1532:       {   
 1533:         Node node=(Node)tempNode.contents.v.elementAt(i);
 1534:         if(node.type.equals("element"))
 1535:          {
 1536:            //System.out.println(node.name+" "+node.getCharacters()+" "+node.contents.v.size());
 1537:            if(node.contents.v.size()>1) 
 1538:            {
 1539:              for(int j=0;j<node.contents.v.size();++j)
 1540:              {  
 1541:                Node node2=(Node)node.contents.v.elementAt(j);
 1542:                if(node2.type.equals("element"))
 1543:                 {
 1544:                   doc.addData(node2.name,node2.getCharacters());
 1545:                   //System.out.println(node2.name+" "+node2.getCharacters()+" "+node2.contents.v.size());
 1546:                 }
 1547:              }
 1548:            } else doc.addData(node.name,node.getCharacters());
 1549:         }
 1550:       }
 1551:       bundle.docsLocator.put(doc.locator,doc); 
 1552:    
 1553:       //System.out.println( bundle.docsLocator);//doc.extraTags);
 1554:       /*
 1555:       notNull=root.find("edoc/doc/title",new int[]{1,count,1});
 1556:       String title=(notNull!=null) ? notNull.getCharacters():"missing";
 1557:       // System.out.println("edoc/doc/title "+title);
 1558:       notNull=root.find("edoc/doc/comment",new int[]{1,count,1});
 1559:       String comment = (notNull!=null) ? notNull.getCharacters():"missing";
 1560:       // System.out.println("edoc/doc/comment "+comment);
 1561:       notNull =root.find("edoc/doc/placeofevent",new int[]{1,count,1});
 1562:       String placeofevent = (notNull!=null) ? notNull.getCharacters():"missing";
 1563:       // System.out.println("edoc/doc/placeofevent "+placeofevent);
 1564:       notNull=root.find("edoc/doc/locater",new int[]{1,count,1});
 1565:       String locator=(notNull!=null) ? notNull.getCharacters():"missing";
 1566:       // System.out.println("edoc/doc/locater "+locator);
 1567:       notNull = root.find("edoc/doc/file/filename",new int[]{1,count,1,1});
 1568:       String fileName=(notNull!=null) ? notNull.getCharacters():"missing";
 1569:       // System.out.println("edoc/doc/file/filename "+fileName);
 1570:       notNull = root.find("edoc/doc/file/mimetype",new int[]{1,count,1,1});
 1571:       String mimeType=(notNull!=null) ? notNull.getCharacters():"missing";
 1572:       // System.out.println("edoc/doc/file/mimetype "+mimeType);
 1573:       notNull = root.find("edoc/doc/file/md5cs",new int[]{1,count,1,1});
 1574:       String md5cs = (notNull!=null) ? notNull.getCharacters():"missing";
 1575:       // System.out.println("edoc/doc/file/md5cs "+md5cs);
 1576:         notNull=root.find("edoc/doc/file/size",new int[]{1,count,1,1});
 1577:         int size  =(notNull!=null) ? Integer.parseInt(notNull.getCharacters()):0;
 1578:       // System.out.println("edoc/doc/file/size "+size);
 1579:       notNull = root.find("edoc/doc/file/filelink",new int[]{1,count,1,1});
 1580:       String fileLink = (notNull!=null) ? notNull.getCharacters():"missing";
 1581:       // System.out.println("edoc/doc/file/filelink "+fileLink);
 1582:       Bundle.Document doc=new Bundle.  Document(docID,ver,title,locator,comment,placeofevent,fileID,fileVer,fileName,mimeType,md5cs,size,fileLink);
 1583:       bundle.docs.put(new Integer(docID),doc);
 1584:       bundle.documents.addElement(doc);
 1585:       */
 1586:       count++;
 1587:     }
 1588:     // System.out.println(bundle.docs.size());
 1589:   return rtTag;
 1590:   }
 1591:   public static void getGenre() 
 1592:   {
 1593:     
 1594:     try 
 1595:     {
 1596:       String  url="http://core.rz-berlin.mpg.de:80/edoctest/oi.epl?mm=help&sm=genres";
 1597:       // read XML document from URL into a string
 1598:       URL u = new URL(url);
 1599:       //  FileReader r=new FileReader();
 1600:       long time = System.currentTimeMillis();
 1601:       // debug=true;
 1602:       Connection conTest=new Connection(new URL(url));
 1603:       conTest.ses=ses;
 1604:       conTest.get();
 1605:       StringBuffer sb=readChunked(conTest);
 1606:       Bundle.genresId.clear();
 1607:       Bundle.genresLabel.clear();
 1608:       Bundle.genresName.clear();
 1609:       Bundle.genVec.removeAllElements();
 1610:       getGenreList(sb); 
 1611:       long timeEnd = System.currentTimeMillis();
 1612:       System.err.println("time to get genre data "+(timeEnd-time));
 1613:      // Enumeration en = Bundle.genres.keys();
 1614:       //while(en.hasMoreElements()
 1615:        if(debug)
 1616:        {
 1617:          Collections.sort(Bundle.genVec);
 1618:          //  Bundle.Genre.debug=true;
 1619:      
 1620:          for(int i=0;i<Bundle.genVec.size();++i)
 1621:          System.out.println(Bundle.genresId.get(Bundle.genVec.get(i)));
 1622:         //System.out.println( Bundle.genres);
 1623:        } 
 1624:      conTest.socket.close();
 1625:     } catch(Exception e) {e.printStackTrace();} 
 1626:   }
 1627:  public static String  getGenreList(StringBuffer sb)
 1628:   {
 1629:     boolean finished = false;
 1630:     String  rtTag="finished";
 1631:     // parse string and build document tree
 1632:     Node root = new Xparse().parse(sb.toString());
 1633:     /*  
 1634:     Vector contents=root.index.v;
 1635:     for (int i=0;i<contents.size();++i)
 1636:     {
 1637:       Node n=(Node)contents.elementAt(i);
 1638:       if(n.type.equals("element"))
 1639:       {
 1640:         // System.out.println("tag "+n.name+" with contents ");
 1641:         // System.out.println(n.getCharacters());
 1642:         //contents=n.contents.v;i=0;
 1643:       }
 1644:       // System.out.println(n.type);
 1645:     
 1646:     }
 1647:     */
 1648:     Node tempNode=root.find(rootNode,new int[] {1});
 1649:     if(tempNode==null) return rtTag;
 1650:     int count=1;
 1651:     /* 
 1652:     for(int i=0;i<tempNode.contents.v.size();++i)
 1653:     {
 1654:       // System.out.println(((Node)tempNode.contents.v.elementAt(i)).attributes+" "+i);
 1655:     }*/
 1656:     if(tempNode.contents.v.size()==0) return rtTag;
 1657:     Node notNull=null;
 1658:     int length=0;
 1659:     for(int i=0;i<tempNode.contents.v.size();++i)    {
 1660:       Node node=(Node)tempNode.contents.v.elementAt(i);
 1661:       if(node.type.equals("element"))
 1662:       if(node.name.equals("genre"))length++;
 1663:       // System.out.println(((Node)tempNode.contents.v.elementAt(i)).attributes+" "+i);
 1664:     }
 1665:    if(length==0) rtTag="finished";        
 1666:     while(count<=length)
 1667:     {
 1668:        //System.out.println(count+" "+length);
 1669:       tempNode=root.find(rootNode+"/genre",new int[] {1,count});
 1670:       if(tempNode==null)
 1671:       {
 1672:         rtTag="finished";
 1673:         break;
 1674:       }    
 1675:         Bundle.Genre genre= new Bundle.Genre();
 1676:         Hashtable attributes=tempNode.attributes;
 1677:         Integer id=null;
 1678:        if(attributes != null)
 1679:       {
 1680:         genre.id=new Integer((String)attributes.get("id"));
 1681:       }
 1682:       for(int i=0;i<tempNode.contents.v.size();++i)
 1683:       {   
 1684:         Node node=(Node)tempNode.contents.v.elementAt(i);
 1685:         if(node.type.equals("element"))
 1686:          {
 1687:            //System.out.println(node.name+" "+node.getCharacters()+" "+node.contents.v.size());
 1688:            if(node.contents.v.size()>1) 
 1689:            {
 1690:              for(int j=0;j<node.contents.v.size();++j)
 1691:              {  
 1692:                Node node2=(Node)node.contents.v.elementAt(j);
 1693:                if(node2.type.equals("element"))
 1694:                 {
 1695:                   //genre.addData(node2.name,node2.getCharacters());
 1696:                   //System.out.println(node2.name+" "+node2.getCharacters()+" "+node2.contents.v.size());
 1697:                 }
 1698:              }
 1699:            } else genre.addData(node.name,node.getCharacters());
 1700:         }
 1701:       }
 1702:       Bundle.genresId.put(genre.id,genre);
 1703:       Bundle.genresLabel.put(genre.label.toLowerCase(),genre.id);
 1704:       Bundle.genresName.put(genre.name.toLowerCase(),genre.id);
 1705:       Bundle.genVec.addElement(genre.id);
 1706:       count++;
 1707:     }
 1708:     // System.out.println(bundle.docs.size());
 1709:   return rtTag;
 1710:   }
 1711:  
 1712:   public static void main(String args[])
 1713:   {
 1714:     // System.out.println("Bundle for http://core.rz-berlin.mpg.de/edocmike/oi.epl?mm=list&grp=90");
 1715:     // getBundle("");
 1716:     // System.out.println("Bundle for http://core.rz-berlin.mpg.de/edocmike/oi.epl?mm=list&grp=89");
 1717:    // getBundle("http://core.rz-berlin.mpg.de/edoctest/oi.epl?mm=list&grp=136");
 1718:     // getBundle("http://core.rz-berlin.mpg.de/edoctest/oi.epl?mm=list&grp=136");
 1719:     runningAsApplet=false;
 1720:     //Object[] options = new Object[] { "Cancel","Replace existing files", "Keep existing files" }; 
 1721: 
 1722:    // int option = showDialog("Warning : several files  exist on the server \n Press show list to see the list! \n What do you want to do?", "Files exist!", options,null);
 1723:     //System.out.println("User selected "+options[option]);
 1724:     // System.exit(0); // due to now internet
 1725:     long time = System.currentTimeMillis();
 1726:     new Upload();
 1727:   // bundle=getBundle("http://core.rz-berlin.mpg.de/edoctest/oi.epl?mm=list&grp=136&col=11");
 1728:      url="http://core.rz-berlin.mpg.de/edoctest/";
 1729:      bundleRef = getBundle(url+"oi.epl?mm=list&grp="+grp+"&col="+col);
 1730:      getGenre();
 1731:    //getMetaData(""); 
 1732:         // if(true)System.exit(0);
 1733:    /* Hashtable data=getMetaData("");
 1734:     //if(true)System.exit(0);
 1735:     try 
 1736:     {
 1737:       // old edocmike
 1738:       Connection con= new Connection(new URL("http://core.rz-berlin.mpg.de:80/edoctest/adm.epl"));
 1739:       String ses=con.getSessionID("name=root&passwd=&mode=verifylogin");
 1740:       //("name=dwinter&passwd=e1nste1n&mode=verifylogin");
 1741:       con.socket.close();
 1742:       Vector artist=(Vector)data.get("artist");
 1743:       Vector picture=(Vector)data.get("picture");
 1744:       Vector idnr=(Vector)data.get("IDNR");
 1745:       Vector source=(Vector)data.get("source");
 1746:       Connection conPost=new Connection( new URL("http://core.rz-berlin.mpg.de:80/edoctest/oi.epl"));
 1747:       // new    URL("http://localhost:8080/examples/servlet/rogo/"));
 1748:       conPost.ses=ses;
 1749:   
 1750:       for(int i=135;i<136;++i)//i<artist.size();++i)
 1751:       {
 1752:         // System.out.println("artist "+artist.elementAt(i));
 1753:         // System.out.println("picture nr "+ i +" name "+picture.elementAt(i));
 1754:         // System.out.println("file" +idnr.elementAt(i));
 1755:         // System.out.println("source"+source.elementAt(i));
 1756:         // if(!new File("/Volumes/data/tmp/Bildarchiv-Daston/400/"+idnr.elementAt(i)).exists())
 1757:         // System.out.println("file "+idnr.elementAt(i) +" does not exist");
 1758:         // if(idnr.elementAt(i)!=null)
 1759:         // if(idnr.elementAt(i).toString().indexOf("Ÿ")>=0||idnr.elementAt(i).toString().indexOf("š")>=0||idnr.elementAt(i).toString().indexOf("Š")>=0)
 1760:       // System.out.println("file "+idnr.elementAt(i) +" contains umlaut ");
 1761:       
 1762:       if(new File("/Volumes/data/tmp/Bildarchiv-Daston/400/"+idnr.elementAt(i)).exists())
 1763:       conPost.post(new File("/Volumes/data/tmp/Bildarchiv-Daston/400/"+idnr.elementAt(i)),picture.elementAt(i).toString(),artist.elementAt(i). toString(),source.elementAt(i).toString());
 1764:       // conPost.post(new File("./test2.jpg"),picture.elementAt(i).toString(),artist.elementAt(i).toString(),source.elementAt(i).toString());
 1765:       
 1766:         }// to for
 1767:     
 1768:     } catch(Exception e) 
 1769:      {
 1770:         e.printStackTrace();
 1771:      }
 1772:    */
 1773:    long time2 = System.currentTimeMillis(); 
 1774:    // System.out.println("time elapsed for upload " +((time2-time)/1000)+" seconds");
 1775:   }
 1776:  static class Connection
 1777:   {
 1778:     Socket socket;
 1779:     URL url;
 1780:     BufferedInputStream in;
 1781:     PrintStream out;
 1782:     String pHost;
 1783:     int pPort = 80;
 1784:     String proxyHost="";
 1785:     String ses="";
 1786:     HttpURLConnection con;
 1787:     UploadInfo dialog = new UploadInfo();
 1788:     MessageDigest md ;
 1789: 
 1790:     public Connection(URL url) throws Exception
 1791:     {
 1792:       Properties prop = System.getProperties();
 1793:       pHost = null;
 1794:       pPort = 80;
 1795:       Enumeration en = prop.propertyNames();
 1796:       while (en.hasMoreElements())
 1797:       {
 1798:         String key = (String) en.nextElement();
 1799:         if (key.toString().indexOf("proxyHost") >= 0)
 1800:         {
 1801:           Object host = prop.get(key);
 1802:           if (host != null)
 1803:             pHost = host.toString();
 1804:         }
 1805:         if (key.toString().indexOf("proxyPort") >= 0)
 1806:         {
 1807:           Object port = prop.get(key);
 1808:           if (port != null)
 1809:             pPort = Integer.parseInt(port.toString());
 1810:         }
 1811:       }
 1812: 
 1813:       if (proxyHost != "")
 1814:       {
 1815:         int index = proxyHost.indexOf(":");
 1816:         pHost = proxyHost.substring(0, (index > 0) ? index : proxyHost.length());
 1817:         pPort = (index > 0) ? Integer.parseInt(proxyHost.substring(index + 1)) : 80;
 1818:       }
 1819:       int port = (url.getPort()>0) ? url.getPort():80;
 1820:       socket = new Socket((pHost != null) ? pHost : url.getHost(), (pHost != null) ? pPort :port );
 1821:       socket.setTcpNoDelay(true);
 1822:       in = new BufferedInputStream(socket.getInputStream(),2);
 1823:       out = new PrintStream(new BufferedOutputStream(socket.getOutputStream()),true);
 1824:       this.url = url;
 1825:       try 
 1826:       {
 1827:          md = MessageDigest.getInstance("MD5");
 1828:       
 1829:       } catch(Exception e) { e.printStackTrace();}
 1830: 
 1831:     }
 1832:    public String getSessionID(String data) 
 1833:    {
 1834:      try
 1835:       {
 1836:         int size=data.length();
 1837:         // System.out.println("Yeah i called connect");
 1838:         //  socket = new Socket((pHost != null) ? pHost : url.getHost(), (pHost != null) ? pPort : url.getPort());
 1839:         // socket.setTcpNoDelay(true);
 1840:         in = new BufferedInputStream(socket.getInputStream());
 1841:         out = new PrintStream(new BufferedOutputStream(socket.getOutputStream()),true);
 1842:     
 1843:           out.print(("POST " + url + " HTTP/1.1\r\nContent-Type: application/x-www-form-urlencoded'\r\nContent-Length:" + size + "\r\nHost: " + url.getHost() + "\r\n\r\n"));//.getBytes());
 1844:         out.print(data);
 1845:         out.flush();
 1846:         String response=getResponseMessage();
 1847:         int index=response.indexOf("Set-Cookie");
 1848:         int index2=response.indexOf("\r\n",index);
 1849:         String cookie=response.substring(index,index2);
 1850:         index=cookie.indexOf("=");
 1851:         
 1852:        // // System.out.print(response);
 1853:         System.err.println("Session key: "+cookie.substring(index+1));
 1854:         return cookie.substring(index+1);//sessionID
 1855:       } catch (Exception e)
 1856:       {
 1857:         // System.out.println(e);
 1858:       }
 1859:    return "";
 1860:    }
 1861:    public void post(File file,String mode,String docID,String title,String author,String source) {
 1862:      try
 1863:       {
 1864:         // System.out.println("Yeah i called post");
 1865:         in = new BufferedInputStream(socket.getInputStream());
 1866:         out = new PrintStream(new BufferedOutputStream(socket.getOutputStream()),true);
 1867:         /*if(in.isClosed())
 1868:         {
 1869:           socket = new Socket((pHost != null) ? pHost : url.getHost(), (pHost != null) ? pPort : url.getPort());
 1870:           socket.setTcpNoDelay(true);
 1871:           in = new BufferedInputStream(socket.getInputStream());
 1872:           out = new PrintStream(new BufferedOutputStream(socket.getOutputStream()),true);
 1873:         }*/
 1874:         FileInputStream f=new FileInputStream(file);
 1875:         int length=f.available();
 1876:         // ses="37b9cec1da340f16f5ef603112e6fe84";
 1877:         /*
 1878:         String data = "--xYzZY\r\nContent-Disposition: form-data; name=\"mm\"\r\n\r\ninput\r\n"+
 1879:         "--xYzZY\r\nContent-Disposition: form-data; name=\"ses\"\r\n\r\n"+ses+"\r\n"
 1880:         +"--xYzZY\r\nContent-Disposition: form-data; name=\"col\"\r\n\r\n"+"11"+"\r\n"
 1881:         +"--xYzZY\r\nContent-Disposition: form-data; name=\"grp\"\r\n\r\n"+"78"+"\r\n"
 1882:           +"--xYzZY\r\nContent-Disposition: form-data; name=\"bun\"\r\n\r\n"+"78"+"\r\n"
 1883:         +"--xYzZY\r\nContent-Disposition: form-data; name=\"sm\"\r\n\r\n"+"new"+"\r\n"
 1884:         // +"--xYzZY\r\nContent-Disposition: form-data; name=\"ver\"\r\n\r\n"+"0"+"\r\n"
 1885:           +"--xYzZY\r\nContent-Disposition: form-data; name=\"docid\"\r\n\r\n"+"10924"+"\r\n"
 1886:         +"--xYzZY\r\nContent-Disposition: form-data; name=\"gentyp\"\r\n\r\n"+"43"+"\r\n"
 1887:         +"--xYzZY\r\nContent-Disposition: form-data; name=\"title\"\r\n\r\n"+"sometitlexyz"+"\r\n"
 1888:         +"--xYzZY\r\nContent-Disposition: form-data; name=\"placeofevent\"\r\n\r\n"+"someplaceofevent"+"\r\n"
 1889:         +"--xYzZY\r\nContent-Disposition: form-data; name=\"locater\"\r\n\r\n"+"somelocator"+"\r\n"
 1890:         +"--xYzZY\r\nContent-Disposition: form-data; name=\"authorcomment\"\r\n\r\n"+"somecomment"+"\r\n"
 1891:         +"--xYzZY\r\nContent-Disposition: form-data; name=\"nfiles\"\r\n\r\n"+"1"+"\r\n"
 1892:         +"--xYzZY\r\nContent-Disposition: form-data; name=\"file1\"; filename=\"test1.jpg\"\r\n"+"Content-Length: "+length+"\r\n"+"Content-Type: image/jpeg"+"\r\n\r\n"; // file data+end data
 1893:         */
 1894:           
 1895:       FormData formData=new FormData(file,ses,col,grp,mode,"0",docID);
 1896:       //format for const File file,String ses,String col,String grp,String sm,String ver,String docid
 1897:       formData.addEntry("title",title);
 1898:       formData.addEntry("placeofevent",author);
 1899:       formData.addEntry("comment",source);
 1900:       formData.addEntry("locater",source);  
 1901:       formData.addFile(formData.file.getName(),"image/tiff",length+"");
 1902:       String data=formData.buff.toString();
 1903:       String ftacc1="--xYzZY\r\nContent-Disposition: form-data; name=\"ftacc1\"\r\n\r\n"+"PUBLIC"+"\r\n";
 1904: 
 1905:       System.err.println("cl "+(data.length()+length+ 13 + ftacc1.length()));
 1906:       out.print(("POST " + url + " HTTP/1.1\r\n"+"Content-Length:"+(data.length()+length+13+ftacc1.length())+"\r\n"+"Content-Type: multipart/form-data, boundary=xYzZY\r\n" 
 1907:       +"Cookie: EMBPERL_UID="+ses+"\r\nHost: " + url.getHost() + "\r\n\r\n"));//.getBytes());
 1908:     //+"Host: " + url.getHost() + "\r\n\r\n"));
 1909:     //        out.print(("POST " + url + " HTTP/1.1\r\nContent-Type: application/x-www-form-//urlencoded'\r\nContent-//Length:" + size + "\r\nCookie: EMBPERL_UID="+ses+"\r\nHost: " + url.getHost() + "\r\n\r\n"));//.getBytes());
 1910:     out.print(data);
 1911:      if(debug)   
 1912:      System.out.print(data);
 1913:     // System.out.println("Finished upload");
 1914:     // System.out.println("Content-Length "+(data.length()+11+length));
 1915:     // System.out.print("\r\n--xYzZY--\r\n");// 11 bytes
 1916:     // System.out.print("\r\n");
 1917:     sendFile(f,out);
 1918:     out.print("\r\n");
 1919:     out.print(ftacc1);
 1920:      if(debug)  
 1921:      System.out.print(ftacc1);
 1922:     out.print("--xYzZY--\r\n");
 1923:      if(debug)  
 1924:      System.out.print("--xYzZY--\r\n");
 1925:     //  System.err.println("Yeah i called connect");
 1926:      dialog.progress.setValue(99);
 1927:     String response=getResponseMessage();
 1928:     System.err.print(response);
 1929:     if(response.toLowerCase().indexOf("content-length")>=0||(response.toLowerCase().indexOf("content-type")>=0))
 1930:     {
 1931:       int c=0;
 1932:       while ((c = in.read()) != -1) 
 1933:       {
 1934:           System.err.print((char)c);
 1935:       }
 1936:     }
 1937:       } catch (Exception e)
 1938:       {
 1939:         // System.out.println(e);
 1940:       }
 1941:    }
 1942:    public void post(String path,Bundle.Document doc,String mode) throws Exception{
 1943:    //  try
 1944:      // {
 1945:          System.out.println("Yeah i called post");
 1946:         in = new BufferedInputStream(socket.getInputStream());
 1947:         out = new PrintStream(new BufferedOutputStream(socket.getOutputStream()),true);
 1948:         /*if(in.isClosed())
 1949:         {
 1950:           socket = new Socket((pHost != null) ? pHost : url.getHost(), (pHost != null) ? pPort : url.getPort());
 1951:           socket.setTcpNoDelay(true);
 1952:           in = new BufferedInputStream(socket.getInputStream());
 1953:           out = new PrintStream(new BufferedOutputStream(socket.getOutputStream()),true);
 1954:         }*/
 1955:         String name = doc.fileLink.trim().startsWith("file://") ?doc.fileLink.trim().substring(7):doc.fileLink.trim();
 1956:         System.out.println(name);
 1957:         String filePath = name.startsWith("/") ? name:path+name;
 1958:         FileInputStream f=new FileInputStream( filePath);
 1959:         int length=f.available();
 1960:           
 1961:         FormData formData=new FormData(new File(doc.fileName),ses,col,grp,mode,"1",doc.docID+"",doc.type);// "1" means new version if mode == old
 1962:         //format for const File file,String ses,String col,String grp,String sm,String ver,String docid
 1963:         Enumeration en = doc.metaTags.keys();
 1964:       while(en.hasMoreElements())
 1965:       {
 1966:         String key=(String)en.nextElement();
 1967:         formData.addEntry(key,(String)doc.metaTags.get(key));
 1968:       }
 1969:       formData.addFile(formData.file.getName(),doc.mimeType,length+"");
 1970:       String data=formData.buff.toString();
 1971:       //System.out.print(data);
 1972:       String ftacc1="--xYzZY\r\nContent-Disposition: form-data; name=\"ftacc1\"\r\n\r\n"+"PUBLIC"+"\r\n";
 1973: 
 1974:       System.err.println("cl "+(data.length()+length+ 13 + ftacc1.length()));
 1975:       out.print(("POST " + url + " HTTP/1.1\r\n"+"Content-Length:"+(data.length()+length+13+ftacc1.length())+"\r\n"+"Content-Type: multipart/form-data, boundary=xYzZY\r\n" 
 1976:       +"Cookie: EMBPERL_UID="+ses+"\r\nHost: " + url.getHost() + "\r\n\r\n"));//.getBytes());
 1977:     //+"Host: " + url.getHost() + "\r\n\r\n"));
 1978:     //        out.print(("POST " + url + " HTTP/1.1\r\nContent-Type: application/x-www-form-//urlencoded'\r\nContent-//Length:" + size + "\r\nCookie: EMBPERL_UID="+ses+"\r\nHost: " + url.getHost() + "\r\n\r\n"));//.getBytes());
 1979:     out.print(data);
 1980:       if(out.checkError()) throw new Error("Error while sending form data");
 1981:      if(debug)   
 1982:      System.out.print(data);
 1983:     // System.out.println("Finished upload");
 1984:     // System.out.println("Content-Length "+(data.length()+11+length));
 1985:     // System.out.print("\r\n--xYzZY--\r\n");// 11 bytes
 1986:     // System.out.print("\r\n");
 1987:     sendFile(f,out);
 1988:     out.print("\r\n");
 1989:     out.print(ftacc1);
 1990:      if(debug)  
 1991:      System.out.print(ftacc1);
 1992:     out.print("--xYzZY--\r\n");
 1993:      if(debug)  
 1994:      System.out.print("--xYzZY--\r\n");
 1995:     //  System.err.println("Yeah i called connect");
 1996:      dialog.progress.setValue(99);
 1997:    // String response=getResponseMessage();
 1998:    // System.err.print(response);
 1999:       int c=0;StringBuffer sb = new StringBuffer();
 2000:       sb=readChunked(this);
 2001:      if(debug)
 2002:      System.out.println(sb);
 2003:      if(sb.length()>0)
 2004:      addToBundle(bundleRef,sb);
 2005:      // updating document with edoc Data
 2006:           Bundle.Document newDoc =(Bundle.Document) bundleRef.docsLocator.get(doc.locator);
 2007:           doc.md5cs=newDoc.md5cs;
 2008:           doc.size=newDoc.size;
 2009:           doc.fileID=newDoc.fileID;
 2010:           doc.docID=newDoc.docID;
 2011:           doc.ver=newDoc.ver;
 2012:           doc.fileVer=newDoc.fileVer;
 2013:           doc.modified = new File(filePath).lastModified();
 2014:        
 2015:     }
 2016:   
 2017:     public void sendFile(FileInputStream f,PrintStream out) throws Exception
 2018:     {
 2019:         int length=f.available();
 2020:         md.reset();
 2021:         dialog.progress.setValue(0);
 2022:         byte[] b=new byte[16204];
 2023:         int   readCount =0;
 2024:         while (readCount < length)
 2025:         {
 2026:           if (readCount + b.length > length)
 2027:             f.read(b, 0, length - readCount); //  mout.flush();
 2028:           else
 2029:             f.read(b);
 2030:           if (readCount + b.length > length)
 2031:           {
 2032:           out.write(b, 0, length - readCount); //  mout.flush();
 2033:           md.update(b, 0, length - readCount);
 2034:             if(out.checkError()) throw new Error("Error while sending file data");
 2035:           //if(debug)  
 2036:           //System.out.write(b, 0, length - readCount); //  mout.flush();
 2037:           }
 2038:           else
 2039:         {
 2040:          //  if(debug)  
 2041:            //System.out.write(b); //  mout.flush();
 2042:           out.write(b);
 2043:           if(out.checkError()) throw new Error("Error while sending file data");
 2044:           md.update(b, 0, b.length);
 2045:         }
 2046:         readCount += b.length;
 2047:         dialog.progress.setValue((int) (((double) readCount / (double) length) * 100.0));        
 2048:         }
 2049:      
 2050:     }
 2051:     public void get() throws Exception
 2052:     {
 2053:       try
 2054:       {
 2055:          // System.out.println("Yeah i called get with "+url );
 2056:         //  socket = new Socket((pHost != null) ? pHost : url.getHost(), (pHost != null) ? pPort : url.getPort());
 2057:         // socket.setTcpNoDelay(true);
 2058:         in = new BufferedInputStream(socket.getInputStream());
 2059:         out = new PrintStream(new BufferedOutputStream(socket.getOutputStream()),true);
 2060:     
 2061:          // out.print(("PUT " + url + " HTTP/1.1\r\nContent-Type: text/html\r\nContent-Length:" + size + "\r\nHost: " + url.getHost() + "\r\n\r\n"));//.getBytes());
 2062:           out.print(("GET " + url + " HTTP/1.1\r\nCookie: EMBPERL_UID="+ses+"\r\nHost: " + url.getHost() + "\r\n\r\n"));
 2063:         out.flush();
 2064:       } catch (Exception e)
 2065:       {
 2066:         int port = (url.getPort()>0) ? url.getPort():80;
 2067:         
 2068:         socket = new Socket((pHost != null) ? pHost : url.getHost(), (pHost != null) ? pPort : url.getPort());
 2069:         socket.setTcpNoDelay(true);
 2070:         in = new BufferedInputStream(socket.getInputStream());
 2071:         out = new PrintStream(new BufferedOutputStream(socket.getOutputStream()),true);
 2072:         // out.print(("PUT " + url + " HTTP/1.1\r\nContent-Type:text/html\r\nContent-Length:" + size + "\r\nHost: " + url.       getHost() + "\r\n\r\n"));
 2073:         out.flush();
 2074:         // System.out.println("Error "+e);
 2075:       }
 2076:       
 2077:       /*  
 2078:       con = new HTTPClient.HttpURLConnection(url);//(HttpURLConnection) url.openConnection(); // con.setRequestMethod("GET");
 2079:       con.setUseCaches(false);
 2080:       con.setRequestMethod("PUT");
 2081:       con.setDoOutput(true);
 2082:       con.setRequestProperty("Content-Length", ""+size);
 2083:       // con.connect();      
 2084:       // System.out.println(con.getClass());
 2085:       out = new DataOutputStream(con.getOutputStream());
 2086:      */
 2087:     }
 2088:     public boolean update() throws Exception
 2089:     {
 2090:       boolean exists = false;
 2091:       String response = "";
 2092:       try
 2093:       {
 2094: 
 2095:         URL url = null;//createURL(this.url.toString(), getSHA((id + "").getBytes()));
 2096:       //  con = new HTTPClient.HttpURLConnection(url);//(HttpURLConnection) url.openConnection(); // con.setRequestMethod("GET");
 2097:         //con.setUseCaches(false);
 2098:        // con.setRequestMethod("HEAD");
 2099:         //con.setDoOutput(true);
 2100:        // con.connect();
 2101: 
 2102:         out.print(("HEAD " + url + " HTTP/1.1\r\nConnection:Keep-Alive\r\nHost: " + url.getHost() + "\r\n\r\n"));//.getBytes());
 2103:           out.flush();
 2104:         response = getResponseMessage();
 2105:         exists = response.indexOf("OK") >= 0;
 2106:         // System.out.println("Head "+response);
 2107:       
 2108:          } catch (Exception e)
 2109:       {
 2110:         socket = new Socket((pHost != null) ? pHost : url.getHost(), (pHost != null) ? pPort : url.getPort());
 2111:           socket.setTcpNoDelay(true);
 2112:         in = new BufferedInputStream(socket.getInputStream());
 2113:         out = new PrintStream(new BufferedOutputStream(socket.getOutputStream()),true);
 2114:     //    out.writeBytes("HEAD " + url + " HTTP/1.1\r\n Connection:Keep-Alive\r\n \r\nHost: " + url.getHost() + //"\r\n\r\n");
 2115:         URL url = null;//createURL(this.url.toString(), getSHA((id + "").getBytes()));
 2116:         out.print(("HEAD " + url + " HTTP/1.1\r\nConnection:Keep-Alive\r\nHost: " + url.getHost() + "\r\n\r\n"));//.getBytes());
 2117:         out.flush();
 2118:         response = getResponseMessage();
 2119:         // System.out.println("Head " + response);
 2120:         exists = response.indexOf("OK") >= 0;
 2121:         //// System.out.println("Error from Head "+e);
 2122:         // e.printStackTrace();
 2123:              return exists;
 2124:       
 2125:       }
 2126:       return exists;
 2127:     }
 2128:   
 2129:       public String getResponseMessage()
 2130:     {
 2131:       
 2132:       int b=0;StringBuffer between=new StringBuffer();int count=0;
 2133:     try 
 2134:     {
 2135:        int loop =0;int available = 0;
 2136:        while((available=in.available())==0) 
 2137:        {
 2138:          if(loop>=3000) throw new Error("Connection timed out");
 2139:          long time = System.currentTimeMillis();
 2140:          while(System.currentTimeMillis()-time<100);
 2141:          if(available==0) loop++; else loop=0;
 2142:        }
 2143:        System.out.println("heh there are bytes "+in.available()+" available");
 2144:       int limit = in.available();
 2145:       while(true )
 2146:       {
 2147:         b=in.read();
 2148:         if(b=='\r') 
 2149:         {
 2150:           between.append((char)b);
 2151:           // System.out.print("CR found ");
 2152:           b=in.read();
 2153:           if(b=='\n')
 2154:           {                        
 2155:              // System.out.print("LF found ");
 2156:              between.append((char)b);
 2157:              if((b=in.read())=='\r')
 2158:              {
 2159:                 b=in.read();
 2160:                 // System.out.print("end found ");
 2161:                 break;
 2162:              }
 2163:           }
 2164:        }
 2165:        between.append((char)b);
 2166:        // System.out.print((char)b);
 2167:        // System.out.println("read "+count+" bytes");
 2168:        count++;            
 2169:        if(count==limit) break;
 2170:       }
 2171: 
 2172:       } catch(Exception e) { 
 2173:       e.printStackTrace();
 2174:       // System.out.println("Error found");
 2175:       if(e.toString().indexOf("peer")>=0)
 2176:       return getResponseMessage();
 2177:       else throw new Error("Error while reading response");
 2178:       }
 2179:           
 2180:       /*
 2181:       String response = "";
 2182:       try
 2183:       {
 2184:         response = con.getResponseMessage();
 2185:       // con.disconnect();
 2186:       } catch (Exception e)
 2187:       {
 2188:         showErrorDialog(e.getMessage(),"Shit happens");
 2189:       }
 2190:       */
 2191:       String response = between.toString();
 2192:       if(response.indexOf("close") >= 0)
 2193:        reconnect();
 2194:       return response; 
 2195:      
 2196:     }
 2197:     public void disconnect() throws Exception
 2198:     {
 2199:      //socket.close();
 2200:      // con.disconnect();
 2201:     }
 2202:     public void reconnect() 
 2203:     {
 2204:        try 
 2205:        {
 2206:           System.out.println("Reconnect called");
 2207:           int port= (url.getPort()>=0) ? url.getPort():80;
 2208:           socket = new Socket((pHost != null) ? pHost : url.getHost(), (pHost != null) ? pPort :port);
 2209:           socket.setTcpNoDelay(true);
 2210:           in = new BufferedInputStream(socket.getInputStream());
 2211:           out = new PrintStream(new BufferedOutputStream(socket.getOutputStream()),true);
 2212: 
 2213:        } catch(Exception e) 
 2214:          {
 2215:            showErrorDialog(e.getMessage(),"Shit happens");
 2216:          }
 2217:        
 2218:     }
 2219:  
 2220:   }
 2221:   public static class FormData 
 2222:   {
 2223:     public StringBuffer buff=new StringBuffer();
 2224:     File file;
 2225:     public FormData() 
 2226:     {}
 2227:     public FormData(File file,String ses,String col,String grp,String sm,String ver,String docid) 
 2228:     {
 2229:      this(file,ses,col,grp,sm,ver,docid,"Archimedes Image Collection");
 2230:     }
 2231:      public FormData(File file,String ses,String col,String grp,String sm,String ver,String docid,String type) 
 2232:     {
 2233:       this.file=file;
 2234:       addEntry("mm","input");
 2235:       addEntry("ses",ses);
 2236:       addEntry("col",col);
 2237:       addEntry("grp",grp);
 2238:       //addEntry("bun",bun);
 2239:         
 2240:       if(sm.equals("old"))
 2241:       {
 2242:         addEntry("sm","old");  
 2243:         addEntry("ver",ver);  
 2244:         addEntry("docid",docid);  
 2245:       
 2246:       }
 2247:       else 
 2248:         addEntry("sm","new");  
 2249:         System.out.println("Type is "+type);
 2250:         type=type.toLowerCase().trim();
 2251:         Object genre=null;
 2252:         if((genre=Bundle.genresLabel.get(type))!=null)
 2253:         addEntry("gentyp",genre.toString());
 2254:         else  if((genre=Bundle.genresName.get(type))!=null)
 2255:          addEntry("gentyp",genre.toString());
 2256:         else
 2257:         {
 2258:           Integer id=new Integer("0");
 2259:           id= new Integer(type);
 2260:           if((genre=Bundle.genresId.get(id))!=null)
 2261:           addEntry("gentyp",((Bundle.Genre)genre).id.toString());
 2262:         }
 2263:    System.out.println("Numerical Type is "+genre);
 2264:   } 
 2265:   public void addEntry(String entry,String data)
 2266:   {
 2267:     buff.append("--xYzZY\r\nContent-Disposition: form-data; name=\"");
 2268:     buff.append(entry);
 2269:     buff.append("\"; charset=utf-8 \r\n\r\n");
 2270:     buff.append(data);
 2271:     buff.append("\r\n"); 
 2272:   }
 2273:   public void addFile(String name,String type,String length)
 2274:   {
 2275:     addEntry("nfiles","1");
 2276:     buff.append("--xYzZY\r\nContent-Disposition: form-data; name=\"file1\"");
 2277:     buff.append("; filename=\""+name+"\""+"\r\nContent-Length: "+length+"\r\n"+"Content-Type:"+type+"\r\n");
 2278:     buff.append("\r\n"); 
 2279:   }
 2280:     
 2281:   public void addEnd() 
 2282:   {
 2283:     buff.append("\r\n--xYzZY--\r\n");
 2284:   }
 2285:   
 2286:   }
 2287:  static class UploadFilter implements FilenameFilter
 2288:   {
 2289:     public UploadFilter() {}
 2290:     public boolean accept(File dir, String name) 
 2291:     {
 2292:       if(name.startsWith(".") || name.endsWith(".xml")) return false;
 2293:       else return true;
 2294:     }
 2295:   }
 2296:  
 2297: static class DotFileFilter implements FilenameFilter
 2298: {
 2299:      public DotFileFilter() {}
 2300:     public boolean accept(File dir, String name) 
 2301:     {
 2302:       if(name.startsWith(".")) return false;
 2303:       else return true;
 2304:     }
 2305:   }
 2306:   static class XMLFilter extends javax.swing.filechooser.FileFilter
 2307:   {
 2308:     public boolean accept(java.io.File file)
 2309:     {
 2310:      if(file.getName().toLowerCase().endsWith(".xml")||file.isDirectory())
 2311:      return true ; 
 2312:      else return false;
 2313:     }
 2314:      public String getDescription()
 2315:      {
 2316:      return "Metadata XML File";
 2317:      }
 2318: 
 2319: }
 2320: }

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