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