// // Upload.java // // // Created by rogo on Wed Jan 29 2003. // Copyright (c) 2001 __MyCompanyName__. All rights reserved. // import com.exploringxml.xml.*; import java.io.*; import java.util.*; import java.net.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.security.*; public class Upload extends JApplet implements ActionListener { static File startFile; static int count = 0; static JFileChooser fileChooser; 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 ses =""; static Bundle bundle; static Bundle bundleRef; JButton upload = new JButton("Upload"); JRadioButton proxy = new JRadioButton("Proxy"); static int fileCount = 0; // static UploadInfo dialog = new UploadInfo(); static String mode = ""; static String destination = ""; static String proxyHost = ""; Upload uploader = this; 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) init(); } // start applet code public void init() { this.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0)); this.getContentPane().add(upload); this.getContentPane().add(proxy); upload.addActionListener(this); proxy.addActionListener(this); id = System.currentTimeMillis(); Properties prop = System.getProperties(); // prop.put("proxySet", "true"); // prop.put("http.proxyHost","proxy.unibe.ch"); // prop.put("proxyPort", "80"); // prop.put("java.protocol.handler.pkgs","HTTPClient"); fileChooser=null; if(!runningAsApplet) { Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize(); Point p=new Point((screenSize.width-300)/2,(screenSize.height-180)/2); JFrame testFrame=new JFrame(); testFrame.setTitle("Archimedes Applet"); testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); testFrame.getContentPane().add(this); testFrame.setSize(300,180); testFrame.setLocation(p); testFrame.setVisible(true); Console console = new Console(testFrame); console.setVisible(true); try { Connection con= new Connection(new URL("http://core.rz-berlin.mpg.de:80/edoctest/adm.epl")); ses=con.getSessionID("name=root&passwd=edoctest&mode=verifylogin"); //("name=dwinter&passwd=e1nste1n&mode=verifylogin"); con.socket.close(); } catch(Exception exc) { } } else { Console console = new Console(new JFrame()); console.setVisible(true); ses= getParameter("ses"); col = getParameter("col").trim(); grp = getParameter("grp").trim(); bundleRef = getBundle(getCodeBase()+"oi.epl?mm=list&grp="+grp+"&col="+col); getGenre(); //bundle=getBundle("http://core.rz-berlin.mpg.de/edoctest/oi.epl?mm=list&grp=136&col=11"); } } public void actionPerformed(ActionEvent e) { if (e.getSource().equals(proxy)) { proxyHost = JOptionPane.showInputDialog("Please enter proxy host (format host:port)!"); if (proxyHost != null) { if (proxyHost.length() == 0) { proxyHost = ""; Properties prop = System.getProperties(); prop.remove("proxySet"); prop.remove("http.proxyHost"); prop.remove("proxyPort"); } else { Properties prop = System.getProperties(); int index = proxyHost.indexOf(":"); String pHost = proxyHost.substring(0, (index > 0) ? index : proxyHost.length()); String pPort = (index > 0) ? proxyHost.substring(index + 1) : "80"; prop.put("proxySet", "true"); prop.put("http.proxyHost",pHost); prop.put("proxyPort", pPort); } System.out.println("pr "); // proxyHost=""; } else proxyHost = ""; // proxy.setSelected(false); } if (e.getActionCommand().equals("Upload")) { //url=getParameter("url"); if(runningAsApplet) set(); //System.out.println("url " + url); //+ " " + getCodeBase()); upload.setEnabled(false); makeUpload(); upload.setEnabled(true); } } public void set() { col = getParameter("col").trim(); grp = getParameter("grp").trim(); ses = getParameter("ses").trim(); System.out.println("uploading to grp "+grp+" col "+col); destination = ""; // destination = getParameter("destinationFolder"); if (destination == null) destination = ""; // e.g."/rogo/servlet/RWServlet/" URL appletURL = getCodeBase(); String serverInfo = appletURL.getHost() + ":" + appletURL.getPort(); // this.url = (servletURL.startsWith("http")) ? servletURL + url : "http://" + serverInfo + servletURL + url; this.url=appletURL.toString(); Properties prop = System.getProperties(); // prop.put("proxySet", "true"); // prop.put("http.proxyHost","proxy.unibe.ch"); // prop.put("proxyPort", "80"); // prop.put("java.protocol.handler.pkgs","HTTPClient"); Enumeration en = prop.propertyNames(); if(debug) while (en.hasMoreElements()) { String key = (String) en.nextElement(); System.out.println("key: " + key + " value: " + prop.get(key)); } System.out.println(url+"oi.epl?mm=list&grp="+grp+"&col="+col); } public static void makeUpload() { try { if (fileChooser == null) { fileChooser = new JFileChooser(); // fileChooser.setFileSelectionMode(fileChooser.FILES_AND_DIRECTORIES); fileChooser.setFileFilter(new XMLFilter()); fileChooser.setDialogType(JFileChooser.OPEN_DIALOG); fileChooser.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() { UploadInfo dialog = new UploadInfo(); File startFile=new File("."); try { dialog.thread=Thread.currentThread(); System.out.println("Called run with " + fileChooser.getSelectedFile()); dialog.setTitle("Upload Status Window"); dialog.setSize(400, 175); dialog.setLocation(new java.awt.Point(150, 100)); Update update = new Update(); startFile = fileChooser.getSelectedFile(); //new File(fileChooser.getFile()); int fileCount = 0; int count = 1; long time = System.currentTimeMillis(); if( connections.get(startFile.getAbsolutePath())!=null) throw new Error("Upload already running!"); if (startFile.isDirectory()) { fileCount = countFiles(startFile, fileCount); System.out.println("Number of files to send: " + fileCount); bundle=getBundle(startFile); writeDirectory(startFile, startFile, fileCount, count, dialog, update); } else { fileCount = 1; //writeStream(startFile, startFile, fileCount, count, dialog, update); bundle = getBundle(startFile); writeStream(startFile,bundle, dialog); } long time2 = System.currentTimeMillis(); System.out.println("needed " + (time2 - time) + " ms for " + count--); dialog.setVisible(false); if (startFile.isDirectory()) showDialog("Finished upload of " + startFile + " containing " + fileCount + " " + ((fileCount > 1) ? " files" : " file") + " in " + ((time2 - time) / 1000) + " seconds", "Finished Upload"); else showDialog("Finished upload of " + startFile + " in " + ((time2 - time) / 1000) + " seconds", "Finished Upload"); connections.remove(startFile.getAbsolutePath()); } catch (Exception e) { dialog.setVisible(false); ByteArrayOutputStream b = new ByteArrayOutputStream(); PrintStream stream = new PrintStream(b); e.printStackTrace(stream); connections.remove(startFile.getAbsolutePath()); showErrorDialog(b.toString(), "Upload failed"); } catch (Error e8) { dialog.setVisible(false); if( e8.toString().indexOf("Upload already running!")<0) connections.remove(startFile.getAbsolutePath()); showErrorDialog(e8.toString(), "Upload failed"); } } }; thread.setPriority(Thread.MIN_PRIORITY); /* dialog = new UploadInfo(); dialog.setTitle("Upload Status Window"); dialog.setSize(400, 155); dialog.setLocation(new java.awt.Point(150, 100)); */ thread.start(); // } // else //System.exit(0); } }); } count = 1; mode = ""; fileChooser.showDialog(new JFrame(), "Upload"); } catch (Exception e) { System.out.println("Ha " + e); } } private static int countFiles(File file, int fileCount) { if (file.isDirectory()) { String[] files = file.list(new UploadFilter()); for (int j = 0; j < files.length; ++j) { File f = new File(file + File.separator + files[j]); if (f.isDirectory()) { fileCount = countFiles(f, fileCount); } else fileCount = fileCount + 1; } } return fileCount; } private static int writeDirectory(File file, File startFile, int fileCount, int count, UploadInfo dialog, Update update) throws Exception { if (file.isDirectory()) { String[] files = file.list(new UploadFilter()); for (int j = 0; j < files.length; ++j) { File f = new File(file + File.separator + files[j]); System.out.println(file + " " + f); if (f.isDirectory()) { count = writeDirectory(f, startFile, fileCount, count, dialog, update); } else count = writeStream(f, startFile, fileCount, count, dialog, update); } } return count; } private static int writeStream(File file, File startFile, int fileCount, int count, UploadInfo dialog, Update updateMode) throws Exception { // try //{ String response = "", mode = ""; String path = file.getAbsolutePath(); String name = (startFile.isDirectory()) ? startFile.getName() + "/" + path.substring(startFile.getAbsolutePath().length() + 1) : file.getName(); if (destination != "") name = (startFile.isDirectory()) ? destination + "/" + path.substring(startFile.getAbsolutePath().length() + 1) : destination + "/" + file.getName(); System.out.println("hi " + name + " " + destination + " " + startFile.getName()); FileInputStream f = new FileInputStream(file.getAbsolutePath()); int length = f.available(); int readCount = 0; byte[] b = (length > 512000) ? new byte[512000] : new byte[length]; System.out.println(file.getName() + " " + ((length > 1000000) ? (length / 1048576) + " MByte" : length + " Bytes")); String fileName = file.getName() + " " + ((length > 1000000) ? (length / 1048576) + " MByte" : length + " Bytes"); dialog.title.setText("Uploading " + startFile); dialog.progress.setValue(0); 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(); /* URL httpCon = new URL(url + getSHA((id + "").getBytes()) + "/" + convertUml(name) + "?id=" + getSHA((id + "").getBytes()) + "&count=" + count + "&nf=" + fileCount + "&md5=" + md5 + "&mode=" + mode); Connection con = null; con=(Connection)connections.get(startFile.getAbsolutePath()); if(con==null) { con=new Connection(httpCon); connections.put(startFile.getAbsolutePath(),con); } else con.url=httpCon; OutputStream mout = con.out; //con.getOutputStream(); */ // dialog.progress.setValue((int) (((double) readCount / (double) length) * 100.0)); // count = filecount Connection conPost = null; System.out.println("hostURL "+url); if(!runningAsApplet) conPost=new Connection( new URL("http://core.rz-berlin.mpg.de/edoctest/oi.epl")); else conPost=new Connection( new URL(url+"/oi.epl")); // new URL("http://localhost:8080/examples/servlet/rogo/")); conPost.ses=ses; conPost.dialog=dialog; Bundle.Document doc=(Bundle.Document)bundle.documents.get(count); 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)); dialog.progress.setValue(100); count++; return count; } private static int writeStream(File startFile,Bundle bundle, UploadInfo dialog) throws Exception { int count = 1,fileCount = bundle.documents.size(); 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(); for(int k=0;k 1000000) ? (doc.size / 1048576) + " MByte" : doc.size + " 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(bundleRef.docsLocator.get(doc.locator)!=null) { if(!newVersionforAll) { Object[] options = new Object[] { "New Version for this file","New Version for all files", "Skip file" }; int option = showDialog("Warning "+doc.locator+" exists! \n What do you want to do?", "Destination exists!", options,list); if(option == 0) mode ="old"; if(option == 1) { mode ="old"; newVersionforAll=true; } if(option == 2) { count++; System.out.println("Heh Document exists!"+ doc); System.out.println("Skipping"); continue; } } Bundle.Document refDoc= (Bundle.Document) bundleRef.docsLocator.get(doc.locator); doc.docID=refDoc.docID; //count++; // System.out.println("Heh Document exists!"+ doc); // System.out.println("Skipping"); // continue; } else mode ="new"; Connection conPost = null; if(!runningAsApplet) conPost=new Connection( new URL("http://core.rz-berlin.mpg.de/edoctest/oi.epl")); else conPost=new Connection( new URL(url+"/oi.epl")); // new URL("http://localhost:8080/examples/servlet/rogo/")); 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)); if(!doc.md5cs.equals(md5)) { Object[] options = { "Retry", "Cancel" }; int option = showDialog("Transfer failed! \n What do you want to do?", "Transfer failed ", options); if(option == 0) { if(!runningAsApplet) conPost=new Connection( new URL("http://core.rz-berlin.mpg.de/edoctest/oi.epl")); else conPost=new Connection(new URL(url+"/oi.epl")); conPost.ses=ses; conPost.dialog=dialog; conPost.post(path,doc,mode); md5=getMD5(md); } } // System.out.println(getMD5(md)); dialog.progress.setValue(100); count++; } return count; } public static String getMD5(MessageDigest md) throws Exception { byte[] bytes = md.digest(); // System.out.println("digest " + bytes.length); StringBuffer sb = new StringBuffer(); int decValue; for (int i = 0; i < bytes.length; i++) { String hexVal = Integer.toHexString(bytes[i] & 0xFF); if (hexVal.length() == 1) hexVal = "0" + hexVal; // put a leading zero sb.append(hexVal); } return sb.toString(); } public static String getSHA(byte[] b) throws Exception { MessageDigest md = MessageDigest.getInstance("SHA"); md.update(b, 0, b.length); //System.out.println("digest"); byte[] bytes = md.digest(); StringBuffer sb = new StringBuffer(); int decValue; for (int i = 0; i < bytes.length; i++) { String hexVal = Integer.toHexString(bytes[i] & 0xFF); if (hexVal.length() == 1) hexVal = "0" + hexVal; // put a leading zero sb.append(hexVal); } return sb.toString(); } public static String convertUml(String newName) { StringBuffer alterMe = new StringBuffer(newName.trim()); int length = alterMe.length(); int j = 0; while (j < length) { //if(Character.isSpaceChar(alterMe.charAt(j))) // alterMe.setCharAt(j,'_'); if (alterMe.charAt(j) == ' ') { alterMe.setCharAt(j, '%'); alterMe.insert(j + 1, "20"); length = length + 2; } /* if(Character.isSpaceChar(alterMe.charAt(j)) alterMe.setCharAt(j,'_'); */ ++j; } return alterMe.toString(); } public static class UploadInfo extends JDialog { JLabel file = new JLabel(" "); JLabel status = new JLabel(" "); JLabel title = new JLabel(" "); JPanel content = new JPanel(); JPanel titlePanel = new JPanel(); JPanel labelPanel = new JPanel(); JPanel statusPanel = new JPanel(); JPanel buttonPanel = new JPanel(); JProgressBar progress = new JProgressBar(); JButton cancel = new JButton("Cancel"); Thread thread; public UploadInfo() { content = new JPanel(true); //content.setBorder(BorderFactory.createRaisedBevelBorder()); content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS)); title = new JLabel("Uploading " + startFile); title.setHorizontalTextPosition(title.CENTER); titlePanel.add(title); buttonPanel.add(cancel); // content.add(titlePanel); content.add(Box.createRigidArea(new java.awt.Dimension(0, 15))); content.add(labelPanel); content.add(new JLabel("")); content.add(progress); content.add(statusPanel); content.add(buttonPanel); content.add(Box.createRigidArea(new java.awt.Dimension(0, 15))); labelPanel.add(file); statusPanel.add(status); getContentPane().add(titlePanel, "North"); getContentPane().add(content, "Center"); cancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { setVisible(false); thread.stop(); } }); pack(); } } static class Update { boolean value = false; public Update() { value = false; } } public static int showDialog(String message, String title, Object[] options) { int option = JOptionPane.showOptionDialog(null, message, title, JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]); return option; } 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; 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(); 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; } public static void showDialog(String message, String title) { JOptionPane.showMessageDialog(null, message, title, JOptionPane.PLAIN_MESSAGE); } public static void showErrorDialog(String message, String title) { JOptionPane.showMessageDialog(null, message, title, JOptionPane.ERROR_MESSAGE); } // end applet code public static StringBuffer readChunked(Connection conTest) throws Exception { InputStream r= conTest.in; //InputStreamReader r= new InputStreamReader(new FileInputStream("FMPro.xml"),"UTF-8"); //InputStreamReader r=// new InputStreamReader(new URL(fileMakerTest).openStream(),"UTF-8"); //new InputStreamReader(conTest.in,"UTF-8"); //while(conTest.in.available()==0); //u.openStream(); //InputStream r = new InputStream(f); StringBuffer sb = new StringBuffer(); 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); if(lowerCase.indexOf("chunked")<0) { ByteArrayOutputStream local=new ByteArrayOutputStream(); while(r.available()==0); System.err.println(r.available()); while ((c = r.read()) != -1) { byte b=(byte)c; local.write(b); if(debug) System.out.print((char)c); } if(charset!="") sb.append(local.toString(charset)); else sb.append(local.toString()); if(debug) System.out.print(sb); return sb; } // 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); return sb; } public static Hashtable getMetaData(String url) { Hashtable data=new Hashtable(); Vector artist=new Vector(); Vector picture=new Vector(); Vector idnr = new Vector(); Vector source = new Vector(); data.put("artist",artist); data.put("picture",picture); data.put("IDNR",idnr); data.put("source",source); try { String fileMakerTest="http://localhost/FMRes/FMPro?-db=Bildarchiv%20Daston&-layID=69&-token=25&-max=1000&-format=-dso_xml&-mode=browse&-findall"; 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"; //Connection con= new Connection(new URL("http://core.rz-berlin.mpg.de:80/edoctest/adm.epl")); //String ses=con.getSessionID("name=root&passwd=&mode=verifylogin"); Connection conTest=new Connection(new URL(fileMakerTest));//new URL(url)); // conTest.ses=ses; conTest.get(); StringBuffer sb=readChunked(conTest); if(debug) System.out.println("file "+sb); // parse string and build document tree Node root = new Xparse().parse(sb.toString()); Vector contents=root.index.v; for (int i=0;i=0) artist.addElement(n.getCharacters()); if(n.name.indexOf("picture")>=0) picture.addElement(n.getCharacters()); if(n.name.indexOf("IDNR")>=0) idnr.addElement(n.getCharacters()); if(n.name.indexOf("source")>=0) source.addElement(n.getCharacters()); //contents=n.contents.v;i=0; } } }catch(Exception e8) { e8.printStackTrace(); } return data; } public static Bundle getBundle(File f) { File xmlFile=null; Bundle bundle=new Bundle(); if(f.isDirectory()) { String[] files=f.list(); for(int j=0;j"),sb.length()); //System.out.println(sb); String rt =addToBundle(bundle,sb); StringBuffer sb2=null; int index=sb.toString().indexOf("",3)+6,index2=0; //for(int i=20;i<260;i=i+20) while(true) { if(rt.equals("finished")) break; conTest.url=new URL(url+"&rt="+rt); conTest.get(); sb2=readChunked(conTest); //sb2.delete(0,index); // index2=sb2.toString().indexOf("",index); // sb2.delete(index2,sb2.length()); //sb.append(sb2); rt=addToBundle(bundle,sb2); } //sb.append(""); //System.out.println(sb); long timeEnd = System.currentTimeMillis(); System.err.println("time to get bundle data "+(timeEnd-time)); /* // read XML Metadata from a file FileReader r=new FileReader("list7.xml"); sb=new StringBuffer(); // String response= conTest.getResponseMessage(); int c=0; while ((c = r.read()) != -1) { char ch=(char)c; sb.append(ch); // System.out.print((char)c); } */ String data=sb.toString().length()+""; // System.out.println(new String(sb.toString().getBytes(),"UTF-8")); // System.out.println(sb.toString()); //addToBundle(bundle,sb); // System.out.println(sb.toString()); // System.exit(0); } catch(Exception e) {e.printStackTrace();} return bundle; } public static String addToBundle(Bundle bundle,StringBuffer sb) { boolean finished = false; String rtTag="finished"; // parse string and build document tree Node root = new Xparse().parse(sb.toString()); /* Vector contents=root.index.v; for (int i=0;i1) { for(int j=0;j1) { for(int j=0;j=0||idnr.elementAt(i).toString().indexOf("š")>=0||idnr.elementAt(i).toString().indexOf("Š")>=0) // System.out.println("file "+idnr.elementAt(i) +" contains umlaut "); if(new File("/Volumes/data/tmp/Bildarchiv-Daston/400/"+idnr.elementAt(i)).exists()) 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()); // conPost.post(new File("./test2.jpg"),picture.elementAt(i).toString(),artist.elementAt(i).toString(),source.elementAt(i).toString()); }// to for } catch(Exception e) { e.printStackTrace(); } */ long time2 = System.currentTimeMillis(); // System.out.println("time elapsed for upload " +((time2-time)/1000)+" seconds"); } static class Connection { Socket socket; URL url; BufferedInputStream in; PrintStream out; String pHost; int pPort = 80; String proxyHost=""; String ses=""; HttpURLConnection con; UploadInfo dialog = new UploadInfo(); public Connection(URL url) throws Exception { Properties prop = System.getProperties(); pHost = null; pPort = 80; Enumeration en = prop.propertyNames(); while (en.hasMoreElements()) { String key = (String) en.nextElement(); if (key.toString().indexOf("proxyHost") >= 0) { Object host = prop.get(key); if (host != null) pHost = host.toString(); } if (key.toString().indexOf("proxyPort") >= 0) { Object port = prop.get(key); if (port != null) pPort = Integer.parseInt(port.toString()); } } if (proxyHost != "") { int index = proxyHost.indexOf(":"); pHost = proxyHost.substring(0, (index > 0) ? index : proxyHost.length()); pPort = (index > 0) ? Integer.parseInt(proxyHost.substring(index + 1)) : 80; } int port = (url.getPort()>0) ? url.getPort():80; socket = new Socket((pHost != null) ? pHost : url.getHost(), (pHost != null) ? pPort :port ); socket.setTcpNoDelay(true); in = new BufferedInputStream(socket.getInputStream(),2); out = new PrintStream(new BufferedOutputStream(socket.getOutputStream()),true); this.url = url; } public String getSessionID(String data) { try { int size=data.length(); // System.out.println("Yeah i called connect"); // socket = new Socket((pHost != null) ? pHost : url.getHost(), (pHost != null) ? pPort : url.getPort()); // socket.setTcpNoDelay(true); in = new BufferedInputStream(socket.getInputStream()); out = new PrintStream(new BufferedOutputStream(socket.getOutputStream()),true); 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()); out.print(data); out.flush(); String response=getResponseMessage(); int index=response.indexOf("Set-Cookie"); int index2=response.indexOf("\r\n",index); String cookie=response.substring(index,index2); index=cookie.indexOf("="); // // System.out.print(response); System.err.println("Session key: "+cookie.substring(index+1)); return cookie.substring(index+1);//sessionID } catch (Exception e) { // System.out.println(e); } return ""; } public void post(File file,String mode,String docID,String title,String author,String source) { try { // System.out.println("Yeah i called post"); in = new BufferedInputStream(socket.getInputStream()); out = new PrintStream(new BufferedOutputStream(socket.getOutputStream()),true); /*if(in.isClosed()) { socket = new Socket((pHost != null) ? pHost : url.getHost(), (pHost != null) ? pPort : url.getPort()); socket.setTcpNoDelay(true); in = new BufferedInputStream(socket.getInputStream()); out = new PrintStream(new BufferedOutputStream(socket.getOutputStream()),true); }*/ FileInputStream f=new FileInputStream(file); int length=f.available(); // ses="37b9cec1da340f16f5ef603112e6fe84"; /* String data = "--xYzZY\r\nContent-Disposition: form-data; name=\"mm\"\r\n\r\ninput\r\n"+ "--xYzZY\r\nContent-Disposition: form-data; name=\"ses\"\r\n\r\n"+ses+"\r\n" +"--xYzZY\r\nContent-Disposition: form-data; name=\"col\"\r\n\r\n"+"11"+"\r\n" +"--xYzZY\r\nContent-Disposition: form-data; name=\"grp\"\r\n\r\n"+"78"+"\r\n" +"--xYzZY\r\nContent-Disposition: form-data; name=\"bun\"\r\n\r\n"+"78"+"\r\n" +"--xYzZY\r\nContent-Disposition: form-data; name=\"sm\"\r\n\r\n"+"new"+"\r\n" // +"--xYzZY\r\nContent-Disposition: form-data; name=\"ver\"\r\n\r\n"+"0"+"\r\n" +"--xYzZY\r\nContent-Disposition: form-data; name=\"docid\"\r\n\r\n"+"10924"+"\r\n" +"--xYzZY\r\nContent-Disposition: form-data; name=\"gentyp\"\r\n\r\n"+"43"+"\r\n" +"--xYzZY\r\nContent-Disposition: form-data; name=\"title\"\r\n\r\n"+"sometitlexyz"+"\r\n" +"--xYzZY\r\nContent-Disposition: form-data; name=\"placeofevent\"\r\n\r\n"+"someplaceofevent"+"\r\n" +"--xYzZY\r\nContent-Disposition: form-data; name=\"locater\"\r\n\r\n"+"somelocator"+"\r\n" +"--xYzZY\r\nContent-Disposition: form-data; name=\"authorcomment\"\r\n\r\n"+"somecomment"+"\r\n" +"--xYzZY\r\nContent-Disposition: form-data; name=\"nfiles\"\r\n\r\n"+"1"+"\r\n" +"--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 */ FormData formData=new FormData(file,ses,col,grp,mode,"0",docID); //format for const File file,String ses,String col,String grp,String sm,String ver,String docid formData.addEntry("title",title); formData.addEntry("placeofevent",author); formData.addEntry("comment",source); formData.addEntry("locater",source); formData.addFile(formData.file.getName(),"image/tiff",length+""); String data=formData.buff.toString(); String ftacc1="--xYzZY\r\nContent-Disposition: form-data; name=\"ftacc1\"\r\n\r\n"+"PUBLIC"+"\r\n"; System.err.println("cl "+(data.length()+length+ 13 + ftacc1.length())); 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" +"Cookie: EMBPERL_UID="+ses+"\r\nHost: " + url.getHost() + "\r\n\r\n"));//.getBytes()); //+"Host: " + url.getHost() + "\r\n\r\n")); // 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()); out.print(data); if(debug) System.out.print(data); // System.out.println("Finished upload"); // System.out.println("Content-Length "+(data.length()+11+length)); // System.out.print("\r\n--xYzZY--\r\n");// 11 bytes // System.out.print("\r\n"); sendFile(f,out); out.print("\r\n"); out.print(ftacc1); if(debug) System.out.print(ftacc1); out.print("--xYzZY--\r\n"); if(debug) System.out.print("--xYzZY--\r\n"); // System.err.println("Yeah i called connect"); dialog.progress.setValue(99); String response=getResponseMessage(); System.err.print(response); if(response.toLowerCase().indexOf("content-length")>=0||(response.toLowerCase().indexOf("content-type")>=0)) { int c=0; while ((c = in.read()) != -1) { System.err.print((char)c); } } } catch (Exception e) { // System.out.println(e); } } public void post(String path,Bundle.Document doc,String mode) throws Exception{ // try // { System.out.println("Yeah i called post"); in = new BufferedInputStream(socket.getInputStream()); out = new PrintStream(new BufferedOutputStream(socket.getOutputStream()),true); /*if(in.isClosed()) { socket = new Socket((pHost != null) ? pHost : url.getHost(), (pHost != null) ? pPort : url.getPort()); socket.setTcpNoDelay(true); in = new BufferedInputStream(socket.getInputStream()); out = new PrintStream(new BufferedOutputStream(socket.getOutputStream()),true); }*/ String name = doc.fileLink.trim().startsWith("file://") ?doc.fileLink.trim().substring(7):doc.fileLink.trim(); System.out.println(name); String filePath = name.startsWith("/") ? name:path+name; FileInputStream f=new FileInputStream( filePath); int length=f.available(); FormData formData=new FormData(new File(doc.fileName),ses,col,grp,mode,"1",doc.docID+"",doc.type);// "1" means new version if mode == old //format for const File file,String ses,String col,String grp,String sm,String ver,String docid Enumeration en = doc.metaTags.keys(); while(en.hasMoreElements()) { String key=(String)en.nextElement(); formData.addEntry(key,(String)doc.metaTags.get(key)); } formData.addFile(formData.file.getName(),doc.mimeType,length+""); String data=formData.buff.toString(); //System.out.print(data); String ftacc1="--xYzZY\r\nContent-Disposition: form-data; name=\"ftacc1\"\r\n\r\n"+"PUBLIC"+"\r\n"; System.err.println("cl "+(data.length()+length+ 13 + ftacc1.length())); 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" +"Cookie: EMBPERL_UID="+ses+"\r\nHost: " + url.getHost() + "\r\n\r\n"));//.getBytes()); //+"Host: " + url.getHost() + "\r\n\r\n")); // 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()); out.print(data); if(out.checkError()) throw new Error("Error while sending form data"); if(debug) System.out.print(data); // System.out.println("Finished upload"); // System.out.println("Content-Length "+(data.length()+11+length)); // System.out.print("\r\n--xYzZY--\r\n");// 11 bytes // System.out.print("\r\n"); sendFile(f,out); out.print("\r\n"); out.print(ftacc1); if(debug) System.out.print(ftacc1); out.print("--xYzZY--\r\n"); if(debug) System.out.print("--xYzZY--\r\n"); // System.err.println("Yeah i called connect"); dialog.progress.setValue(99); // String response=getResponseMessage(); // System.err.print(response); int c=0;StringBuffer sb = new StringBuffer(); sb=readChunked(this); if(debug) System.out.println(sb); if(sb.length()>0) addToBundle(bundleRef,sb); try { Bundle.Document newDoc =(Bundle.Document) bundleRef.docsLocator.get(doc.locator); doc.md5cs=newDoc.md5cs; doc.size=newDoc.size; doc.fileID=newDoc.fileID; doc.docID=newDoc.docID; doc.ver=newDoc.ver; doc.fileVer=newDoc.fileVer; bundle.exportToXML(new File("./metaout.xml")); } catch(Exception e7) { e7.printStackTrace();} } public void sendFile(FileInputStream f,PrintStream out) throws Exception { int length=f.available(); md.reset(); dialog.progress.setValue(0); byte[] b=new byte[512]; 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) { out.write(b, 0, length - readCount); // mout.flush(); md.update(b, 0, length - readCount); if(out.checkError()) throw new Error("Error while sending file data"); //if(debug) //System.out.write(b, 0, length - readCount); // mout.flush(); } else { // if(debug) //System.out.write(b); // mout.flush(); out.write(b); if(out.checkError()) throw new Error("Error while sending file data"); md.update(b, 0, b.length); } readCount += b.length; dialog.progress.setValue((int) (((double) readCount / (double) length) * 100.0)); } } public void get() throws Exception { try { // System.out.println("Yeah i called connect"); // socket = new Socket((pHost != null) ? pHost : url.getHost(), (pHost != null) ? pPort : url.getPort()); // socket.setTcpNoDelay(true); in = new BufferedInputStream(socket.getInputStream()); out = new PrintStream(new BufferedOutputStream(socket.getOutputStream()),true); // 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()); out.print(("GET " + url + " HTTP/1.1\r\nCookie: EMBPERL_UID="+ses+"\r\nHost: " + url.getHost() + "\r\n\r\n")); out.flush(); } catch (Exception e) { int port = (url.getPort()>0) ? url.getPort():80; socket = new Socket((pHost != null) ? pHost : url.getHost(), (pHost != null) ? pPort : url.getPort()); socket.setTcpNoDelay(true); in = new BufferedInputStream(socket.getInputStream()); out = new PrintStream(new BufferedOutputStream(socket.getOutputStream()),true); // out.print(("PUT " + url + " HTTP/1.1\r\nContent-Type:text/html\r\nContent-Length:" + size + "\r\nHost: " + url. getHost() + "\r\n\r\n")); out.flush(); // System.out.println("Error "+e); } /* con = new HTTPClient.HttpURLConnection(url);//(HttpURLConnection) url.openConnection(); // con.setRequestMethod("GET"); con.setUseCaches(false); con.setRequestMethod("PUT"); con.setDoOutput(true); con.setRequestProperty("Content-Length", ""+size); // con.connect(); // System.out.println(con.getClass()); out = new DataOutputStream(con.getOutputStream()); */ } public boolean update() throws Exception { boolean exists = false; String response = ""; try { URL url = null;//createURL(this.url.toString(), getSHA((id + "").getBytes())); // con = new HTTPClient.HttpURLConnection(url);//(HttpURLConnection) url.openConnection(); // con.setRequestMethod("GET"); //con.setUseCaches(false); // con.setRequestMethod("HEAD"); //con.setDoOutput(true); // con.connect(); out.print(("HEAD " + url + " HTTP/1.1\r\nConnection:Keep-Alive\r\nHost: " + url.getHost() + "\r\n\r\n"));//.getBytes()); out.flush(); response = getResponseMessage(); exists = response.indexOf("OK") >= 0; // System.out.println("Head "+response); } catch (Exception e) { socket = new Socket((pHost != null) ? pHost : url.getHost(), (pHost != null) ? pPort : url.getPort()); socket.setTcpNoDelay(true); in = new BufferedInputStream(socket.getInputStream()); out = new PrintStream(new BufferedOutputStream(socket.getOutputStream()),true); // out.writeBytes("HEAD " + url + " HTTP/1.1\r\n Connection:Keep-Alive\r\n \r\nHost: " + url.getHost() + //"\r\n\r\n"); URL url = null;//createURL(this.url.toString(), getSHA((id + "").getBytes())); out.print(("HEAD " + url + " HTTP/1.1\r\nConnection:Keep-Alive\r\nHost: " + url.getHost() + "\r\n\r\n"));//.getBytes()); out.flush(); response = getResponseMessage(); // System.out.println("Head " + response); exists = response.indexOf("OK") >= 0; //// System.out.println("Error from Head "+e); // e.printStackTrace(); return exists; } return exists; } public String getResponseMessage() { int b=0;StringBuffer between=new StringBuffer();int count=0; try { int loop =0;int available = 0; while((available=in.available())==0) { if(loop>=3000) throw new Error("Connection timed out"); long time = System.currentTimeMillis(); while(System.currentTimeMillis()-time<100); if(available==0) loop++; else loop=0; } System.out.println("heh there are bytes "+in.available()+" available"); int limit = in.available(); while(true ) { b=in.read(); if(b=='\r') { between.append((char)b); // System.out.print("CR found "); b=in.read(); if(b=='\n') { // System.out.print("LF found "); between.append((char)b); if((b=in.read())=='\r') { b=in.read(); // System.out.print("end found "); break; } } } between.append((char)b); // System.out.print((char)b); // System.out.println("read "+count+" bytes"); count++; if(count==limit) break; } } catch(Exception e) { e.printStackTrace(); // System.out.println("Error found"); if(e.toString().indexOf("peer")>=0) return getResponseMessage(); else throw new Error("Error while reading response"); } /* String response = ""; try { response = con.getResponseMessage(); // con.disconnect(); } catch (Exception e) { showErrorDialog(e.getMessage(),"Shit happens"); } */ String response = between.toString(); if(response.indexOf("close") >= 0) reconnect(); return response; } public void disconnect() throws Exception { //socket.close(); // con.disconnect(); } public void reconnect() { try { System.out.println("Reconnect called"); int port= (url.getPort()>=0) ? url.getPort():80; socket = new Socket((pHost != null) ? pHost : url.getHost(), (pHost != null) ? pPort :port); socket.setTcpNoDelay(true); in = new BufferedInputStream(socket.getInputStream()); out = new PrintStream(new BufferedOutputStream(socket.getOutputStream()),true); } catch(Exception e) { showErrorDialog(e.getMessage(),"Shit happens"); } } } public static class FormData { public StringBuffer buff=new StringBuffer(); File file; public FormData() {} public FormData(File file,String ses,String col,String grp,String sm,String ver,String docid) { this(file,ses,col,grp,sm,ver,docid,"Archimedes Image Collection"); } public FormData(File file,String ses,String col,String grp,String sm,String ver,String docid,String type) { this.file=file; addEntry("mm","input"); addEntry("ses",ses); addEntry("col",col); addEntry("grp",grp); //addEntry("bun",bun); if(sm.equals("old")) { addEntry("sm","old"); addEntry("ver",ver); addEntry("docid",docid); } else addEntry("sm","new"); System.out.println("Type is "+type); type=type.toLowerCase().trim(); Object genre=null; if((genre=Bundle.genresLabel.get(type))!=null) addEntry("gentyp",genre.toString()); else if((genre=Bundle.genresName.get(type))!=null) addEntry("gentyp",genre.toString()); else { Integer id=new Integer("0"); id= new Integer(type); if((genre=Bundle.genresId.get(id))!=null) addEntry("gentyp",((Bundle.Genre)genre).id.toString()); } System.out.println("Numerical Type is "+genre); } public void addEntry(String entry,String data) { buff.append("--xYzZY\r\nContent-Disposition: form-data; name=\""); buff.append(entry); buff.append("\"; charset=utf-8 \r\n\r\n"); buff.append(data); buff.append("\r\n"); } public void addFile(String name,String type,String length) { addEntry("nfiles","1"); buff.append("--xYzZY\r\nContent-Disposition: form-data; name=\"file1\""); buff.append("; filename=\""+name+"\""+"\r\nContent-Length: "+length+"\r\n"+"Content-Type:"+type+"\r\n"); buff.append("\r\n"); } public void addEnd() { buff.append("\r\n--xYzZY--\r\n"); } } static class UploadFilter implements FilenameFilter { public UploadFilter() {} public boolean accept(File dir, String name) { if(name.startsWith(".") || name.endsWith(".xml")) return false; else return true; } } static class DotFileFilter implements FilenameFilter { public DotFileFilter() {} public boolean accept(File dir, String name) { if(name.startsWith(".")) return false; else return true; } } static class XMLFilter extends javax.swing.filechooser.FileFilter { public boolean accept(java.io.File file) { if(file.getName().toLowerCase().endsWith(".xml")||file.isDirectory()) return true ; else return false; } public String getDescription() { return "Metadata XML File"; } } }