--- edoc-applet/Upload.java 2003/06/17 10:38:55 1.1 +++ edoc-applet/Upload.java 2003/06/25 11:50:09 1.3 @@ -30,6 +30,7 @@ public class Upload extends JApplet impl static Bundle bundle; static Bundle bundleRef; JButton upload = new JButton("Upload"); + JButton download = new JButton("Download"); JRadioButton proxy = new JRadioButton("Proxy"); static int fileCount = 0; // static UploadInfo dialog = new UploadInfo(); @@ -40,13 +41,6 @@ public class Upload extends JApplet impl static boolean runningAsApplet=true; static boolean debug=false; static String rootNode = "edoc"; - static MessageDigest md ; - static - { - try { - md = MessageDigest.getInstance("MD5"); - } catch(Exception e) { e.printStackTrace();} - } public Upload() { if(!runningAsApplet) @@ -58,8 +52,10 @@ public class Upload extends JApplet impl { this.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0)); this.getContentPane().add(upload); + this.getContentPane().add(download); this.getContentPane().add(proxy); upload.addActionListener(this); + download.addActionListener(this); proxy.addActionListener(this); id = System.currentTimeMillis(); Properties prop = System.getProperties(); @@ -135,7 +131,55 @@ public class Upload extends JApplet impl // proxy.setSelected(false); } - if (e.getActionCommand().equals("Upload")) + if (e.getActionCommand().equals("Download")) + { + + Thread thread = new Thread() + { + public void run() + { + UploadInfo dialog = new UploadInfo(); + + File startFile=new File("."); + dialog.thread=Thread.currentThread(); + //System.out.println("Called run with " + fileChooser.getSelectedFile()); + dialog.setTitle("Download Status Window"); + dialog.setSize(400, 175); + dialog.setLocation(new java.awt.Point(150, 100)); + + long time = System.currentTimeMillis(); + try + { + Connection conGet = null; + if(!runningAsApplet) + conGet=new Connection( new URL("http://core.rz-berlin.mpg.de/edoctest/oi.epl")); + else + conGet=new Connection( new URL(url+"/oi.epl")); + + conGet.ses=ses; + conGet.dialog=dialog; + Bundle bundle = bundleRef; + int fileCount = bundle.documents.size(); + for(int i=0;i< fileCount;++i) + { + Bundle.Document doc=(Bundle.Document)bundle.documents.get(i); + if(doc.fileLink=="") continue; + System.out.println(doc.title); + conGet.url=new URL(doc.fileLink); + conGet.get(); + writeChunked(conGet,new File("./download/"+doc.fileName),i+1, fileCount,doc.size, dialog); + //if(getMD5(conGet.md).equals(doc.md5cs)) System.out.println("download sucess"); + //else + // break; + } + } catch(Exception e5) { e5.printStackTrace();} + long time2 =System.currentTimeMillis(); + System.out.println("Time for download needed "+(time2-time)); + } + }; + thread.start(); + } + if (e.getActionCommand().equals("Upload")) { //url=getParameter("url"); @@ -379,8 +423,8 @@ public class Upload extends JApplet impl System.out.println(doc.title); conPost.post(file,"new",doc.docID+"",doc.title,doc.locator,doc.comment); - // conPost.post(new File("./test2.jpg"),picture.elementAt(i).toString(),artist.elementAt(i).toString(),source.elementAt(i). - System.out.println(getMD5(md)); + // conPost.post(new File("./test2.jpg"),picture.elementAt(i).toString(),artist.elementAt(i).toString(),source.elementAt(i).toString()); + System.out.println(getMD5(conPost.md)); dialog.progress.setValue(100); count++; return count; @@ -391,20 +435,27 @@ public class Upload extends JApplet impl int index = startFile.getAbsolutePath().lastIndexOf(File.separator); - String path =startFile.getAbsolutePath().substring(0,index+1); - System.out.println(path);mode = "new"; - boolean newVersionforAll = false; - Vector list=new Vector(); + String path = startFile.getAbsolutePath().substring(0,index+1); + System.out.println(path); + mode = "new"; + boolean newVersionforAll = false, keepAllFiles = false; + Vector list=new Vector();Vector docs=new Vector();Bundle.Document refDoc=null; for(int k=0;k1) ? 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); +; + if(option == 0) break;// mode = "old"; if(option == 1) { mode ="old"; - newVersionforAll=true; + newVersionforAll = true; + keepAllFiles = false; } if(option == 2) { count++; System.out.println("Heh Document exists!"+ doc); System.out.println("Skipping"); + newVersionforAll = false; + keepAllFiles = true; continue; } } - Bundle.Document refDoc= (Bundle.Document) bundleRef.docsLocator.get(doc.locator); + refDoc= (Bundle.Document) bundleRef.docsLocator.get(doc.locator); doc.docID=refDoc.docID; - //count++; + if(keepAllFiles) + { + count++; + continue; + } + //count++; // System.out.println("Heh Document exists!"+ doc); // System.out.println("Skipping"); // continue; @@ -457,8 +517,8 @@ public class Upload extends JApplet impl conPost.ses=ses; conPost.dialog=dialog; conPost.post(path,doc,mode); - String md5=getMD5(md); - System.out.println("MD5 is equal ?"+doc.md5cs.equals(md5)); + String md5=getMD5(conPost.md); + System.out.println("MD5 is equal ?"+doc.md5cs.equals(md5)+" doc_md5 "+doc.md5cs+" md5 "+md5); if(!doc.md5cs.equals(md5)) { Object[] options = { "Retry", "Cancel" }; @@ -472,12 +532,14 @@ public class Upload extends JApplet impl conPost.ses=ses; conPost.dialog=dialog; conPost.post(path,doc,mode); - md5=getMD5(md); + md5=getMD5(conPost.md); + if(!doc.md5cs.equals(md5)) showDialog("Transfer failed","Transfer failed again! "); } } // System.out.println(getMD5(md)); dialog.progress.setValue(100); count++; } + bundle.exportToXML(new File(path+"/temp.xml")); return count; } @@ -514,7 +576,51 @@ public class Upload extends JApplet impl return sb.toString(); } - + public static boolean checkForNewFile(String path,Bundle.Document doc,Bundle.Document refDoc) throws Exception + { + String name = doc.fileLink.trim().startsWith("file://") ?doc.fileLink.trim().substring(7):doc.fileLink.trim(); + System.out.println(name); + boolean isNewFile=false; + String filePath = name.startsWith("/") ? name:path+name; + FileInputStream f=new FileInputStream( filePath); + int length=f.available(); + if(length!=refDoc.size) isNewFile=true; + else + { + + //new File(filePath).setLastModified(System.currentTimeMillis()-690000000L); + Date actualTime = new Date(new File(filePath).lastModified()); + Date modified = new Date(doc.modified);//new File(filePath).lastModified()); + // System.out.println("actual Date "+actualTime); + // System.out.println("Date file modified "+modified); + // System.out.println("Time difference in minutes "+((actualTime.getTime()-modified.getTime())/60000L)); + // if file has been modified longer than 10 hours do not compare md5 --> file is not newer + if(actualTime.getTime()==modified.getTime()) return false; + MessageDigest md = MessageDigest.getInstance("MD5"); + byte[] b=new byte[512000]; + int readCount =0; + while (readCount < length) + { + if (readCount + b.length > length) + f.read(b, 0, length - readCount); // mout.flush(); + else + f.read(b); + if (readCount + b.length > length) + { + md.update(b, 0, length - readCount); + } + else + { + md.update(b, 0, b.length); + } + readCount += b.length; + } + isNewFile = !getMD5(md).equals(refDoc.md5cs); + + } + System.out.println("is new file ? "+isNewFile+" length is equal ?"+(length==refDoc.size)); + return isNewFile; + } public static String convertUml(String newName) { StringBuffer alterMe = new StringBuffer(newName.trim()); @@ -602,40 +708,58 @@ public class Upload extends JApplet impl } public static int showDialog(String message, String title, Object[] options,Vector fileList) { - JOptionPane pane = new JOptionPane( message, JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]); - pane.setMessageType(JOptionPane.WARNING_MESSAGE); // Configure - JDialog dialog = pane.createDialog(null, title); - JList list=null; + JOptionPane pane = new JOptionPane( message, JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]); + pane.setMessageType(JOptionPane.WARNING_MESSAGE); // Configure + final JDialog dialog = pane.createDialog(null, title); + JList list=null; if(fileList==null) list= new JList(new String[]{"click","here","test","bla","world","hello"}); - else list = new JList(fileList); - JScrollPane scpane = new JScrollPane(list); - scpane.setPreferredSize(new Dimension(100,80)); - JLabel label= new JLabel("Files : "); - label.setPreferredSize(new Dimension(50,80)); - JPanel panel = new JPanel(); - panel.add(label); - panel.add(scpane); - dialog.getContentPane().add(panel,"East"); - dialog.pack(); - dialog.show(); + else list = new JList(fileList); + + JScrollPane scpane = new JScrollPane(list); + scpane.setPreferredSize(new Dimension(150,150)); + + JLabel label = new JLabel("Files to be updated : "); + label.setPreferredSize(new Dimension(150,30)); + final JPanel panel = new JPanel(); + panel.setLayout(new BorderLayout()); + panel.add(label,"North"); + panel.add(scpane,"Center"); + JButton showList= new JButton("show List"); + showList.addActionListener(new ActionListener() + { + public void actionPerformed(ActionEvent e) + { + JDialog window = new JDialog(dialog); + window.getContentPane().add(panel); + window.pack(); + window.show(); + } + }); + JPanel buttonPanel = new JPanel(); + buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS)); + buttonPanel.add(Box.createRigidArea(new Dimension(0,30))); + buttonPanel.add(showList); + dialog.getContentPane().add(buttonPanel,"East"); + dialog.pack(); + dialog.show(); Object selectedValue = pane.getValue(); - if(selectedValue == null) - return 2; - //If there is not an array of option buttons: - if(options == null) { - if(selectedValue instanceof Integer) - return ((Integer)selectedValue).intValue(); - return 2; - } - //If there is an array of option buttons: - for(int counter = 0, maxCounter = options.length; - counter < maxCounter; counter++) { - if(options[counter].equals(selectedValue)) - return counter; - } - return 2; - + if(selectedValue == null) + return 2; + //If there is not an array of option buttons: + if(options == null) { + if(selectedValue instanceof Integer) + return ((Integer)selectedValue).intValue(); + return 2; + } + //If there is an array of option buttons: + for(int counter = 0, maxCounter = options.length;counter < maxCounter; counter++) + { + if(options[counter].equals(selectedValue)) + return counter; + } + return 2; + } public static void showDialog(String message, String title) @@ -801,6 +925,175 @@ public class Upload extends JApplet impl // System.out.println(sb); return sb; } + public static void writeChunked(Connection conTest,File f,int count,int fileCount,int fileSize,UploadInfo dialog) throws Exception + { + InputStream r= conTest.in; + FileOutputStream file_out = new FileOutputStream(f); + BufferedOutputStream fout = new BufferedOutputStream(file_out); + int c,countC=0,limit=conTest.in.available(); + // r.skip(211); + String response = conTest.getResponseMessage(); + String lowerCase= response.toLowerCase(); + int index =lowerCase.indexOf("charset"); + int index2 =lowerCase.indexOf("=",index); + int index3 =lowerCase.indexOf("\r",index2); + String charset=(index>0) ? response.substring(index2+1,index3):""; + charset="";//charset.toUpperCase(); + System.err.println(response); + dialog.title.setText("Downloading to " + f.toString()); + dialog.progress.setValue(0); + MessageDigest md =conTest.md; + md.reset(); + String fileName = f.getName()+ " " + ((fileSize > 1000000) ? (fileSize / 1048576) + " MByte" : fileSize+ " Bytes"); + dialog.file.setText(fileName); + dialog.status.setText(count + " of " + fileCount + ((fileCount > 1) ? " files" : " file")); + //dialog.pack(); + //dialog.validate(); + if (dialog.isVisible()) + dialog.repaint(); + else + dialog.show(); + + if(lowerCase.indexOf("chunked")<0) + { + while(r.available()==0); + System.err.println(r.available()); + while ((c = r.read()) != -1) + { + byte b=(byte)c; + fout.write(b); + //md.update(b, 0, b.length); + if(debug) + System.out.print((char)c); + } + /* + if(charset!="") + fout.write(local.toString(charset)); + else + fout.write(local.toString()); + */ + //if(debug) + // System.out.print(sb); + //return sb; + } + StringBuffer sb = new StringBuffer(); + // read chunk size + while ((c = r.read()) != -1) + { + ++countC; + char ch=(char)c; + + if(Character.digit(ch, 16) != -1) + sb.append((char)c); + if(ch=='\r') + { + ch=(char)r.read(); + if( ch=='\n') + break; + } + // System.out.print((char)c); + } + int bufSize=Integer.parseInt(sb.toString().trim(), 16); + + System.err.println("chunk size "+Integer.parseInt(sb.toString().trim(), 16)+" bytes"+" limit "+limit); + //sb.append((char)r.read()); + // sb.append((char)r.read()); + sb=new StringBuffer();//.delete(0,sb.length()); + byte[] b=new byte[bufSize]; + countC=0; + StringBuffer localBuff=new StringBuffer(); + while (true) + { + // sb.append(" new Chunk "+r.available()+" \r\n "); + int readCount = 0; + int available = 0; + int readSize = b.length; int loop=0; + while (readCount < bufSize) + { + available=r.available(); + + if(available>=bufSize) + readSize=bufSize; + else + readSize=available; + + if(readCount + readSize > bufSize) + { + readSize = r.read(b, readCount, bufSize - readCount); // mout.flush(); + + } + else + { + readSize = r.read(b,readCount,readSize); + } + if(loop>=200) throw new Error("Connection timed out"); + if(readSize==0) + { + long time = System.currentTimeMillis(); + while(System.currentTimeMillis()-time<100); + } else loop=0; + + if(debug) + System.out.println(++loop+" read "+readCount+" bytes"+" readSize "+readSize); + readCount+=readSize; + + } + + + //while(r.available()0) + { + // System.err.println("new chunk size "+Integer.parseInt(localBuff.toString().trim(), 16)+" bytes"); + // System.err.println((char)r.read()); + bufSize=Integer.parseInt(localBuff.toString().trim(), 16); + //localBuff.delete(0,localBuff.length()); + localBuff=new StringBuffer(); + } // else bufSize=0; + ++countC; + // sb.append("new Chunk"+r.available()+"\r\n "); + if(bufSize==0)//(bufSize)) + break; + // System.err.println(" in Chunk "+bufSize+" buffers used "+(countC)+" \r\n "); + // // System.out.print((char)c); + } + // System.out.println(sb); + fout.flush(); + fout.close(); + + file_out.close(); + //return sb; + } + public static Hashtable getMetaData(String url) { Hashtable data=new Hashtable(); @@ -1213,10 +1506,11 @@ public class Upload extends JApplet impl // getBundle("http://core.rz-berlin.mpg.de/edoctest/oi.epl?mm=list&grp=136"); // getBundle("http://core.rz-berlin.mpg.de/edoctest/oi.epl?mm=list&grp=136"); runningAsApplet=false; - // Object[] options = new Object[] { "New Version for this file","New Version for all files", "Skip file" }; + //Object[] options = new Object[] { "Cancel","Replace existing files", "Keep existing files" }; - // int option = showDialog("Warning exists! \n What do you want to do?", "Destination exists!", options,null); - + // 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); + //System.out.println("User selected "+options[option]); + // System.exit(0); // due to now internet long time = System.currentTimeMillis(); new Upload(); // bundle=getBundle("http://core.rz-berlin.mpg.de/edoctest/oi.epl?mm=list&grp=136&col=11"); @@ -1280,6 +1574,8 @@ public class Upload extends JApplet impl String ses=""; HttpURLConnection con; UploadInfo dialog = new UploadInfo(); + MessageDigest md ; + public Connection(URL url) throws Exception { Properties prop = System.getProperties(); @@ -1315,6 +1611,12 @@ public class Upload extends JApplet impl in = new BufferedInputStream(socket.getInputStream(),2); out = new PrintStream(new BufferedOutputStream(socket.getOutputStream()),true); this.url = url; + try + { + md = MessageDigest.getInstance("MD5"); + + } catch(Exception e) { e.printStackTrace();} + } public String getSessionID(String data) { @@ -1489,9 +1791,7 @@ public class Upload extends JApplet impl System.out.println(sb); if(sb.length()>0) addToBundle(bundleRef,sb); - try - { - + // updating document with edoc Data Bundle.Document newDoc =(Bundle.Document) bundleRef.docsLocator.get(doc.locator); doc.md5cs=newDoc.md5cs; doc.size=newDoc.size; @@ -1499,8 +1799,8 @@ public class Upload extends JApplet impl doc.docID=newDoc.docID; doc.ver=newDoc.ver; doc.fileVer=newDoc.fileVer; - bundle.exportToXML(new File("./metaout.xml")); - } catch(Exception e7) { e7.printStackTrace();} + doc.modified = new File(filePath).lastModified(); + } public void sendFile(FileInputStream f,PrintStream out) throws Exception @@ -1508,7 +1808,7 @@ public class Upload extends JApplet impl int length=f.available(); md.reset(); dialog.progress.setValue(0); - byte[] b=new byte[512]; + byte[] b=new byte[16204]; int readCount =0; while (readCount < length) {