Annotation of edoc-applet/Upload.java, revision 1.1.1.1
1.1 rogo 1: //
2: // Upload.java
3: //
4: //
5: // Created by rogo on Wed Jan 29 2003.
6: // Copyright (c) 2001 __MyCompanyName__. 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;
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 ="141"; // Group or Bundle
29: static String ses ="";
30: static Bundle bundle;
31: static Bundle bundleRef;
32: JButton upload = new JButton("Upload");
33: JRadioButton proxy = new JRadioButton("Proxy");
34: static int fileCount = 0;
35: // static UploadInfo dialog = new UploadInfo();
36: static String mode = "";
37: static String destination = "";
38: static String proxyHost = "";
39: Upload uploader = this;
40: static boolean runningAsApplet=true;
41: static boolean debug=false;
42: static String rootNode = "edoc";
43: static MessageDigest md ;
44: static
45: {
46: try {
47: md = MessageDigest.getInstance("MD5");
48: } catch(Exception e) { e.printStackTrace();}
49: }
50: public Upload()
51: {
52: if(!runningAsApplet)
53: init();
54: }
55:
56: // start applet code
57: public void init()
58: {
59: this.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
60: this.getContentPane().add(upload);
61: this.getContentPane().add(proxy);
62: upload.addActionListener(this);
63: proxy.addActionListener(this);
64: id = System.currentTimeMillis();
65: Properties prop = System.getProperties();
66:
67: // prop.put("proxySet", "true");
68: // prop.put("http.proxyHost","proxy.unibe.ch");
69: // prop.put("proxyPort", "80");
70: // prop.put("java.protocol.handler.pkgs","HTTPClient");
71: fileChooser=null;
72: if(!runningAsApplet)
73: {
74: Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
75: Point p=new Point((screenSize.width-300)/2,(screenSize.height-180)/2);
76: JFrame testFrame=new JFrame();
77: testFrame.setTitle("Archimedes Applet");
78: testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
79: testFrame.getContentPane().add(this);
80: testFrame.setSize(300,180);
81: testFrame.setLocation(p);
82: testFrame.setVisible(true);
83: Console console = new Console(testFrame);
84: console.setVisible(true);
85: try
86: {
87: Connection con= new Connection(new URL("http://core.rz-berlin.mpg.de:80/edoctest/adm.epl"));
88: ses=con.getSessionID("name=root&passwd=edoctest&mode=verifylogin");
89: //("name=dwinter&passwd=e1nste1n&mode=verifylogin");
90: con.socket.close();
91: } catch(Exception exc) { }
92: } else
93: {
94: Console console = new Console(new JFrame());
95: console.setVisible(true);
96: ses= getParameter("ses");
97: col = getParameter("col").trim();
98: grp = getParameter("grp").trim();
99: bundleRef = getBundle(getCodeBase()+"oi.epl?mm=list&grp="+grp+"&col="+col);
100: getGenre();
101: //bundle=getBundle("http://core.rz-berlin.mpg.de/edoctest/oi.epl?mm=list&grp=136&col=11");
102:
103: }
104: }
105: public void actionPerformed(ActionEvent e)
106: {
107: if (e.getSource().equals(proxy))
108: {
109: proxyHost = JOptionPane.showInputDialog("Please enter proxy host (format host:port)!");
110: if (proxyHost != null)
111: {
112: if (proxyHost.length() == 0)
113: {
114: proxyHost = "";
115: Properties prop = System.getProperties();
116: prop.remove("proxySet");
117: prop.remove("http.proxyHost");
118: prop.remove("proxyPort");
119:
120: }
121: else
122: {
123: Properties prop = System.getProperties();
124: int index = proxyHost.indexOf(":");
125: String pHost = proxyHost.substring(0, (index > 0) ? index : proxyHost.length());
126: String pPort = (index > 0) ? proxyHost.substring(index + 1) : "80";
127:
128: prop.put("proxySet", "true");
129: prop.put("http.proxyHost",pHost);
130: prop.put("proxyPort", pPort);
131: }
132: System.out.println("pr "); // proxyHost="";
133: } else
134: proxyHost = "";
135: // proxy.setSelected(false);
136: }
137:
138: if (e.getActionCommand().equals("Upload"))
139: {
140:
141: //url=getParameter("url");
142: if(runningAsApplet)
143: set();
144: //System.out.println("url " + url); //+ " " + getCodeBase());
145: upload.setEnabled(false);
146: makeUpload();
147: upload.setEnabled(true);
148: }
149:
150: }
151: public void set()
152: {
153: col = getParameter("col").trim();
154: grp = getParameter("grp").trim();
155: ses = getParameter("ses").trim();
156: System.out.println("uploading to grp "+grp+" col "+col);
157: destination = "";
158: // destination = getParameter("destinationFolder");
159: if (destination == null)
160: destination = "";
161: // e.g."/rogo/servlet/RWServlet/"
162: URL appletURL = getCodeBase();
163: String serverInfo = appletURL.getHost() + ":" + appletURL.getPort();
164: // this.url = (servletURL.startsWith("http")) ? servletURL + url : "http://" + serverInfo + servletURL + url;
165: this.url=appletURL.toString();
166: Properties prop = System.getProperties();
167:
168: // prop.put("proxySet", "true");
169: // prop.put("http.proxyHost","proxy.unibe.ch");
170: // prop.put("proxyPort", "80");
171: // prop.put("java.protocol.handler.pkgs","HTTPClient");
172: Enumeration en = prop.propertyNames();
173: if(debug)
174: while (en.hasMoreElements())
175: {
176: String key = (String) en.nextElement();
177: System.out.println("key: " + key + " value: " + prop.get(key));
178:
179: }
180: System.out.println(url+"oi.epl?mm=list&grp="+grp+"&col="+col);
181:
182: }
183: public static void makeUpload()
184: {
185: try
186: {
187: if (fileChooser == null)
188: {
189: fileChooser = new JFileChooser();
190: // fileChooser.setFileSelectionMode(fileChooser.FILES_AND_DIRECTORIES);
191: fileChooser.setFileFilter(new XMLFilter());
192: fileChooser.setDialogType(JFileChooser.OPEN_DIALOG);
193: fileChooser.addActionListener(new ActionListener()
194: {
195: public void actionPerformed(ActionEvent e2)
196: {
197: System.out.println(e2.getActionCommand());
198: if (!e2.getActionCommand().toString().equals("ApproveSelection")) return;
199: // {
200:
201: Thread thread = new Thread()
202: {
203: public void run()
204: {
205: UploadInfo dialog = new UploadInfo();
206:
207: File startFile=new File(".");
208: try
209: {
210: dialog.thread=Thread.currentThread();
211: System.out.println("Called run with " + fileChooser.getSelectedFile());
212: dialog.setTitle("Upload Status Window");
213: dialog.setSize(400, 175);
214: dialog.setLocation(new java.awt.Point(150, 100));
215: Update update = new Update();
216: startFile = fileChooser.getSelectedFile(); //new File(fileChooser.getFile());
217: int fileCount = 0;
218: int count = 1;
219: long time = System.currentTimeMillis();
220: if( connections.get(startFile.getAbsolutePath())!=null)
221: throw new Error("Upload already running!");
222:
223: if (startFile.isDirectory())
224: {
225: fileCount = countFiles(startFile, fileCount);
226: System.out.println("Number of files to send: " + fileCount);
227: bundle=getBundle(startFile);
228: writeDirectory(startFile, startFile, fileCount, count, dialog, update);
229:
230: } else
231: {
232: fileCount = 1;
233: //writeStream(startFile, startFile, fileCount, count, dialog, update);
234: bundle = getBundle(startFile);
235: writeStream(startFile,bundle, dialog);
236:
237: }
238:
239: long time2 = System.currentTimeMillis();
240: System.out.println("needed " + (time2 - time) + " ms for " + count--);
241: dialog.setVisible(false);
242: if (startFile.isDirectory())
243: showDialog("Finished upload of " + startFile + " containing " + fileCount + " " + ((fileCount > 1) ? " files" : " file") + " in " + ((time2 - time) / 1000) + " seconds", "Finished Upload");
244: else
245: showDialog("Finished upload of " + startFile + " in " + ((time2 - time) / 1000) + " seconds", "Finished Upload");
246: connections.remove(startFile.getAbsolutePath());
247: } catch (Exception e)
248: {
249: dialog.setVisible(false);
250: ByteArrayOutputStream b = new ByteArrayOutputStream();
251: PrintStream stream = new PrintStream(b);
252: e.printStackTrace(stream);
253: connections.remove(startFile.getAbsolutePath());
254:
255: showErrorDialog(b.toString(), "Upload failed");
256: } catch (Error e8)
257: {
258: dialog.setVisible(false);
259: if( e8.toString().indexOf("Upload already running!")<0)
260: connections.remove(startFile.getAbsolutePath());
261: showErrorDialog(e8.toString(), "Upload failed");
262: }
263:
264: }
265: };
266: thread.setPriority(Thread.MIN_PRIORITY);
267: /* dialog = new UploadInfo();
268: dialog.setTitle("Upload Status Window");
269: dialog.setSize(400, 155);
270: dialog.setLocation(new java.awt.Point(150, 100));
271: */
272: thread.start();
273: // } // else
274: //System.exit(0);
275: }
276: });
277:
278: }
279: count = 1;
280: mode = "";
281: fileChooser.showDialog(new JFrame(), "Upload");
282:
283: } catch (Exception e)
284: {
285: System.out.println("Ha " + e);
286: }
287: }
288: private static int countFiles(File file, int fileCount)
289: {
290: if (file.isDirectory())
291: {
292: String[] files = file.list(new UploadFilter());
293:
294: for (int j = 0; j < files.length; ++j)
295: {
296: File f = new File(file + File.separator + files[j]);
297: if (f.isDirectory())
298: {
299: fileCount = countFiles(f, fileCount);
300: } else
301: fileCount = fileCount + 1;
302: }
303:
304: }
305: return fileCount;
306: }
307:
308: private static int writeDirectory(File file, File startFile, int fileCount, int count, UploadInfo dialog, Update update) throws Exception
309: {
310: if (file.isDirectory())
311: {
312: String[] files = file.list(new UploadFilter());
313: for (int j = 0; j < files.length; ++j)
314: {
315: File f = new File(file + File.separator + files[j]);
316: System.out.println(file + " " + f);
317:
318: if (f.isDirectory())
319: {
320: count = writeDirectory(f, startFile, fileCount, count, dialog, update);
321: } else
322: count = writeStream(f, startFile, fileCount, count, dialog, update);
323: }
324: }
325: return count;
326: }
327: private static int writeStream(File file, File startFile, int fileCount, int count, UploadInfo dialog, Update updateMode) throws Exception
328: {
329: // try
330: //{
331: String response = "", mode = "";
332: String path = file.getAbsolutePath();
333: String name = (startFile.isDirectory()) ? startFile.getName() + "/" + path.substring(startFile.getAbsolutePath().length() + 1) : file.getName();
334: if (destination != "")
335: name = (startFile.isDirectory()) ? destination + "/" + path.substring(startFile.getAbsolutePath().length() + 1) : destination + "/" + file.getName();
336:
337: System.out.println("hi " + name + " " + destination + " " + startFile.getName());
338: FileInputStream f = new FileInputStream(file.getAbsolutePath());
339: int length = f.available();
340: int readCount = 0;
341: byte[] b = (length > 512000) ? new byte[512000] : new byte[length];
342: System.out.println(file.getName() + " " + ((length > 1000000) ? (length / 1048576) + " MByte" : length + " Bytes"));
343: String fileName = file.getName() + " " + ((length > 1000000) ? (length / 1048576) + " MByte" : length + " Bytes");
344: dialog.title.setText("Uploading " + startFile);
345: dialog.progress.setValue(0);
346: dialog.file.setText(fileName);
347: dialog.status.setText(count + " of " + fileCount + ((fileCount > 1) ? " files" : " file"));
348: //dialog.pack();
349: //dialog.validate();
350: if (dialog.isVisible())
351: dialog.repaint();
352: else
353: dialog.show();
354: /*
355: URL httpCon = new URL(url + getSHA((id + "").getBytes()) + "/" + convertUml(name) + "?id=" + getSHA((id + "").getBytes()) + "&count=" + count + "&nf=" + fileCount + "&md5=" + md5 + "&mode=" + mode);
356: Connection con = null;
357: con=(Connection)connections.get(startFile.getAbsolutePath());
358: if(con==null)
359: {
360: con=new Connection(httpCon);
361: connections.put(startFile.getAbsolutePath(),con);
362: } else
363: con.url=httpCon;
364: OutputStream mout = con.out; //con.getOutputStream();
365: */
366: // dialog.progress.setValue((int) (((double) readCount / (double) length) * 100.0));
367: // count = filecount
368: Connection conPost = null;
369: System.out.println("hostURL "+url);
370: if(!runningAsApplet)
371: conPost=new Connection( new URL("http://core.rz-berlin.mpg.de/edoctest/oi.epl"));
372: else
373: conPost=new Connection( new URL(url+"/oi.epl"));
374:
375: // new URL("http://localhost:8080/examples/servlet/rogo/"));
376: conPost.ses=ses;
377: conPost.dialog=dialog;
378: Bundle.Document doc=(Bundle.Document)bundle.documents.get(count);
379: System.out.println(doc.title);
380: conPost.post(file,"new",doc.docID+"",doc.title,doc.locator,doc.comment);
381:
382: // conPost.post(new File("./test2.jpg"),picture.elementAt(i).toString(),artist.elementAt(i).toString(),source.elementAt(i).
383: System.out.println(getMD5(md));
384: dialog.progress.setValue(100);
385: count++;
386: return count;
387: }
388: private static int writeStream(File startFile,Bundle bundle, UploadInfo dialog) throws Exception
389: {
390: int count = 1,fileCount = bundle.documents.size();
391: int index = startFile.getAbsolutePath().lastIndexOf(File.separator);
392:
393:
394: String path =startFile.getAbsolutePath().substring(0,index+1);
395: System.out.println(path);mode = "new";
396: boolean newVersionforAll = false;
397: Vector list=new Vector();
398: for(int k=0;k<fileCount;++k)
399: {
400: Bundle.Document doc = (Bundle.Document)bundle.documents.elementAt(k);
401: if(bundleRef.docsLocator.get(doc.locator)!=null)
402: list.addElement(doc.fileName);
403: }
404:
405: for(int k=0;k<fileCount;++k)
406: {
407: Bundle.Document doc = (Bundle.Document)bundle.documents.elementAt(k);
408:
409: String response = "";
410: dialog.title.setText("Uploading " + path+doc.fileName);
411: dialog.progress.setValue(0);
412: String fileName = doc.fileName+ " " + ((doc.size > 1000000) ? (doc.size / 1048576) + " MByte" : doc.size + " Bytes");
413: dialog.file.setText(fileName);
414: dialog.status.setText(count + " of " + fileCount + ((fileCount > 1) ? " files" : " file"));
415: //dialog.pack();
416: //dialog.validate();
417: if (dialog.isVisible())
418: dialog.repaint();
419: else
420: dialog.show();
421:
422: if(bundleRef.docsLocator.get(doc.locator)!=null)
423: {
424: if(!newVersionforAll)
425: {
426: Object[] options = new Object[] { "New Version for this file","New Version for all files", "Skip file" };
427:
428: int option = showDialog("Warning "+doc.locator+" exists! \n What do you want to do?", "Destination exists!", options,list);
429: if(option == 0) mode ="old";
430: if(option == 1)
431: {
432: mode ="old";
433: newVersionforAll=true;
434: }
435: if(option == 2)
436: {
437: count++;
438: System.out.println("Heh Document exists!"+ doc);
439: System.out.println("Skipping");
440: continue;
441: }
442: }
443: Bundle.Document refDoc= (Bundle.Document) bundleRef.docsLocator.get(doc.locator);
444: doc.docID=refDoc.docID;
445: //count++;
446: // System.out.println("Heh Document exists!"+ doc);
447: // System.out.println("Skipping");
448: // continue;
449: } else mode ="new";
450: Connection conPost = null;
451: if(!runningAsApplet)
452: conPost=new Connection( new URL("http://core.rz-berlin.mpg.de/edoctest/oi.epl"));
453: else
454: conPost=new Connection( new URL(url+"/oi.epl"));
455:
456: // new URL("http://localhost:8080/examples/servlet/rogo/"));
457: conPost.ses=ses;
458: conPost.dialog=dialog;
459: conPost.post(path,doc,mode);
460: String md5=getMD5(md);
461: System.out.println("MD5 is equal ?"+doc.md5cs.equals(md5));
462: if(!doc.md5cs.equals(md5))
463: {
464: Object[] options = { "Retry", "Cancel" };
465: int option = showDialog("Transfer failed! \n What do you want to do?", "Transfer failed ", options);
466: if(option == 0)
467: {
468: if(!runningAsApplet)
469: conPost=new Connection( new URL("http://core.rz-berlin.mpg.de/edoctest/oi.epl"));
470: else
471: conPost=new Connection(new URL(url+"/oi.epl"));
472: conPost.ses=ses;
473: conPost.dialog=dialog;
474: conPost.post(path,doc,mode);
475: md5=getMD5(md);
476: }
477: } // System.out.println(getMD5(md));
478: dialog.progress.setValue(100);
479: count++;
480: }
481: return count;
482: }
483:
484: public static String getMD5(MessageDigest md) throws Exception
485: {
486: byte[] bytes = md.digest();
487: // System.out.println("digest " + bytes.length);
488: StringBuffer sb = new StringBuffer();
489: int decValue;
490: for (int i = 0; i < bytes.length; i++)
491: {
492: String hexVal = Integer.toHexString(bytes[i] & 0xFF);
493: if (hexVal.length() == 1)
494: hexVal = "0" + hexVal; // put a leading zero
495: sb.append(hexVal);
496: }
497: return sb.toString();
498: }
499: public static String getSHA(byte[] b) throws Exception
500: {
501: MessageDigest md = MessageDigest.getInstance("SHA");
502: md.update(b, 0, b.length);
503: //System.out.println("digest");
504: byte[] bytes = md.digest();
505: StringBuffer sb = new StringBuffer();
506: int decValue;
507: for (int i = 0; i < bytes.length; i++)
508: {
509: String hexVal = Integer.toHexString(bytes[i] & 0xFF);
510: if (hexVal.length() == 1)
511: hexVal = "0" + hexVal; // put a leading zero
512: sb.append(hexVal);
513: }
514:
515: return sb.toString();
516: }
517:
518: public static String convertUml(String newName)
519: {
520: StringBuffer alterMe = new StringBuffer(newName.trim());
521: int length = alterMe.length();
522: int j = 0;
523: while (j < length)
524: { //if(Character.isSpaceChar(alterMe.charAt(j)))
525: // alterMe.setCharAt(j,'_');
526: if (alterMe.charAt(j) == ' ')
527: {
528: alterMe.setCharAt(j, '%');
529: alterMe.insert(j + 1, "20");
530: length = length + 2;
531: }
532: /* if(Character.isSpaceChar(alterMe.charAt(j))
533: alterMe.setCharAt(j,'_');
534: */
535: ++j;
536: }
537: return alterMe.toString();
538: }
539: public static class UploadInfo extends JDialog
540: {
541: JLabel file = new JLabel(" ");
542: JLabel status = new JLabel(" ");
543: JLabel title = new JLabel(" ");
544: JPanel content = new JPanel();
545: JPanel titlePanel = new JPanel();
546: JPanel labelPanel = new JPanel();
547: JPanel statusPanel = new JPanel();
548: JPanel buttonPanel = new JPanel();
549:
550: JProgressBar progress = new JProgressBar();
551: JButton cancel = new JButton("Cancel");
552: Thread thread;
553: public UploadInfo()
554: {
555:
556: content = new JPanel(true);
557: //content.setBorder(BorderFactory.createRaisedBevelBorder());
558: content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
559: title = new JLabel("Uploading " + startFile);
560: title.setHorizontalTextPosition(title.CENTER);
561: titlePanel.add(title);
562: buttonPanel.add(cancel);
563: // content.add(titlePanel);
564: content.add(Box.createRigidArea(new java.awt.Dimension(0, 15)));
565: content.add(labelPanel);
566: content.add(new JLabel(""));
567: content.add(progress);
568: content.add(statusPanel);
569: content.add(buttonPanel);
570: content.add(Box.createRigidArea(new java.awt.Dimension(0, 15)));
571: labelPanel.add(file);
572: statusPanel.add(status);
573: getContentPane().add(titlePanel, "North");
574: getContentPane().add(content, "Center");
575: cancel.addActionListener(new ActionListener()
576: {
577: public void actionPerformed(ActionEvent e)
578: {
579: setVisible(false);
580: thread.stop();
581: }
582:
583: });
584: pack();
585:
586: }
587:
588: }
589: static class Update
590: {
591: boolean value = false;
592: public Update()
593: {
594: value = false;
595: }
596: }
597: public static int showDialog(String message, String title, Object[] options)
598: {
599: int option = JOptionPane.showOptionDialog(null, message, title, JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]);
600: return option;
601:
602: }
603: public static int showDialog(String message, String title, Object[] options,Vector fileList)
604: {
605: JOptionPane pane = new JOptionPane( message, JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]);
606: pane.setMessageType(JOptionPane.WARNING_MESSAGE); // Configure
607: JDialog dialog = pane.createDialog(null, title);
608: JList list=null;
609: if(fileList==null)
610: list= new JList(new String[]{"click","here","test","bla","world","hello"});
611: else list = new JList(fileList);
612: JScrollPane scpane = new JScrollPane(list);
613: scpane.setPreferredSize(new Dimension(100,80));
614: JLabel label= new JLabel("Files : ");
615: label.setPreferredSize(new Dimension(50,80));
616: JPanel panel = new JPanel();
617: panel.add(label);
618: panel.add(scpane);
619: dialog.getContentPane().add(panel,"East");
620: dialog.pack();
621: dialog.show();
622: Object selectedValue = pane.getValue();
623: if(selectedValue == null)
624: return 2;
625: //If there is not an array of option buttons:
626: if(options == null) {
627: if(selectedValue instanceof Integer)
628: return ((Integer)selectedValue).intValue();
629: return 2;
630: }
631: //If there is an array of option buttons:
632: for(int counter = 0, maxCounter = options.length;
633: counter < maxCounter; counter++) {
634: if(options[counter].equals(selectedValue))
635: return counter;
636: }
637: return 2;
638:
639: }
640:
641: public static void showDialog(String message, String title)
642: {
643: JOptionPane.showMessageDialog(null, message, title, JOptionPane.PLAIN_MESSAGE);
644:
645: }
646: public static void showErrorDialog(String message, String title)
647: {
648: JOptionPane.showMessageDialog(null, message, title, JOptionPane.ERROR_MESSAGE);
649: }
650:
651:
652:
653: // end applet code
654:
655: public static StringBuffer readChunked(Connection conTest) throws Exception
656: {
657: InputStream r= conTest.in;
658: //InputStreamReader r= new InputStreamReader(new FileInputStream("FMPro.xml"),"UTF-8");
659: //InputStreamReader r=// new InputStreamReader(new URL(fileMakerTest).openStream(),"UTF-8");
660: //new InputStreamReader(conTest.in,"UTF-8");
661: //while(conTest.in.available()==0);
662: //u.openStream();
663: //InputStream r = new InputStream(f);
664: StringBuffer sb = new StringBuffer();
665: int c,countC=0,limit=conTest.in.available();
666: // r.skip(211);
667: String response = conTest.getResponseMessage();
668: String lowerCase= response.toLowerCase();
669: int index =lowerCase.indexOf("charset");
670: int index2 =lowerCase.indexOf("=",index);
671: int index3 =lowerCase.indexOf("\r",index2);
672: String charset=(index>0) ? response.substring(index2+1,index3):"";
673: charset="";//charset.toUpperCase();
674: System.err.println(response);
675: if(lowerCase.indexOf("chunked")<0)
676: {
677: ByteArrayOutputStream local=new ByteArrayOutputStream();
678: while(r.available()==0);
679: System.err.println(r.available());
680: while ((c = r.read()) != -1)
681: {
682: byte b=(byte)c;
683: local.write(b);
684: if(debug)
685: System.out.print((char)c);
686: }
687: if(charset!="")
688: sb.append(local.toString(charset));
689: else
690: sb.append(local.toString());
691: if(debug)
692: System.out.print(sb);
693: return sb;
694: }
695:
696: // read chunk size
697: while ((c = r.read()) != -1)
698: {
699: ++countC;
700: char ch=(char)c;
701:
702: if(Character.digit(ch, 16) != -1)
703: sb.append((char)c);
704: if(ch=='\r')
705: {
706: ch=(char)r.read();
707: if( ch=='\n')
708: break;
709: }
710: // System.out.print((char)c);
711: }
712: int bufSize=Integer.parseInt(sb.toString().trim(), 16);
713:
714: System.err.println("chunk size "+Integer.parseInt(sb.toString().trim(), 16)+" bytes"+" limit "+limit);
715: //sb.append((char)r.read());
716: // sb.append((char)r.read());
717: sb=new StringBuffer();//.delete(0,sb.length());
718: byte[] b=new byte[bufSize];
719: countC=0;
720: StringBuffer localBuff=new StringBuffer();
721: while (true)
722: {
723: // sb.append(" new Chunk "+r.available()+" \r\n ");
724: int readCount = 0;
725: int available = 0;
726: int readSize = b.length; int loop=0;
727: while (readCount < bufSize)
728: {
729: available=r.available();
730:
731: if(available>=bufSize)
732: readSize=bufSize;
733: else
734: readSize=available;
735:
736: if(readCount + readSize > bufSize)
737: {
738: readSize = r.read(b, readCount, bufSize - readCount); // mout.flush();
739:
740: }
741: else
742: {
743: readSize = r.read(b,readCount,readSize);
744: }
745: if(loop>=200) throw new Error("Connection timed out");
746: if(readSize==0)
747: {
748: long time = System.currentTimeMillis();
749: while(System.currentTimeMillis()-time<100);
750: } else loop=0;
751:
752: if(debug)
753: System.out.println(++loop+" read "+readCount+" bytes"+" readSize "+readSize);
754: readCount+=readSize;
755:
756: }
757:
758:
759: //while(r.available()<bufSize); // critical if connection closed
760: // r.read(b,0,bufSize);
761: //System.err.println(r.available());
762: r.read();r.read();
763: if(charset!="")
764: sb.append(new String(b,0,bufSize,charset));
765: else
766: sb.append(new String(b,0,bufSize));
767: if(debug)
768: System.out.println(new String(b,0,bufSize));
769: while ( (c = r.read()) != -1)
770: {
771: // ++countC;
772: char ch=(char)c;
773: //System.err.println("read lf "+(ch=='\r')+" or cr "+( ch=='\n')+" "+(Character.digit(ch, 16) == -1));
774: //if((ch!='\r')&&( ch!='\n')) System.err.println("read ch "+" "+ch+" "+Character.isSpace(ch));
775: if(Character.digit(ch, 16) != -1)
776: localBuff.append(ch);
777: if(ch=='\r')
778: {
779: ch=(char)r.read();
780: if( ch=='\n')
781: break;
782: }
783: }
784: if(debug)
785: System.out.println("chunk size "+localBuff);
786: if(localBuff.length()>0)
787: {
788: // System.err.println("new chunk size "+Integer.parseInt(localBuff.toString().trim(), 16)+" bytes");
789: // System.err.println((char)r.read());
790: bufSize=Integer.parseInt(localBuff.toString().trim(), 16);
791: //localBuff.delete(0,localBuff.length());
792: localBuff=new StringBuffer();
793: } // else bufSize=0;
794: ++countC;
795: // sb.append("new Chunk"+r.available()+"\r\n ");
796: if(bufSize==0)//(bufSize))
797: break;
798: // System.err.println(" in Chunk "+bufSize+" buffers used "+(countC)+" \r\n ");
799: // // System.out.print((char)c);
800: }
801: // System.out.println(sb);
802: return sb;
803: }
804: public static Hashtable getMetaData(String url)
805: {
806: Hashtable data=new Hashtable();
807: Vector artist=new Vector();
808: Vector picture=new Vector();
809: Vector idnr = new Vector();
810: Vector source = new Vector();
811: data.put("artist",artist);
812: data.put("picture",picture);
813: data.put("IDNR",idnr);
814: data.put("source",source);
815: try
816: {
817: String fileMakerTest="http://localhost/FMRes/FMPro?-db=Bildarchiv%20Daston&-layID=69&-token=25&-max=1000&-format=-dso_xml&-mode=browse&-findall";
818: 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";
819: //Connection con= new Connection(new URL("http://core.rz-berlin.mpg.de:80/edoctest/adm.epl"));
820: //String ses=con.getSessionID("name=root&passwd=&mode=verifylogin");
821:
822: Connection conTest=new Connection(new URL(fileMakerTest));//new URL(url));
823: // conTest.ses=ses;
824: conTest.get();
825: StringBuffer sb=readChunked(conTest);
826: if(debug)
827: System.out.println("file "+sb);
828: // parse string and build document tree
829: Node root = new Xparse().parse(sb.toString());
830: Vector contents=root.index.v;
831: for (int i=0;i<contents.size();++i)
832: {
833: Node n=(Node)contents.elementAt(i);
834: if(n.type.equals("element"))
835: {
836: // System.out.println("tag "+n.name+" with contents ");
837: // System.out.println(n.getCharacters());
838: if(n.name.indexOf("artist")>=0) artist.addElement(n.getCharacters());
839: if(n.name.indexOf("picture")>=0) picture.addElement(n.getCharacters());
840: if(n.name.indexOf("IDNR")>=0) idnr.addElement(n.getCharacters());
841: if(n.name.indexOf("source")>=0) source.addElement(n.getCharacters());
842: //contents=n.contents.v;i=0;
843: }
844: }
845: }catch(Exception e8) { e8.printStackTrace(); }
846: return data;
847: }
848:
849: public static Bundle getBundle(File f)
850: {
851: File xmlFile=null;
852: Bundle bundle=new Bundle();
853: if(f.isDirectory())
854: {
855: String[] files=f.list();
856: for(int j=0;j<files.length;++j)
857: {
858: if(files[j].endsWith("xml"))
859: {
860: xmlFile=new File(f.getAbsolutePath()+"/"+files[j]);
861: break;
862: }
863: }
864: } else xmlFile=f;
865: try
866: {
867: // read XML Metadata from a file
868: FileReader r=new FileReader(xmlFile);
869: StringBuffer sb=new StringBuffer();
870: int c=0;
871: while ((c = r.read()) != -1)
872: {
873: char ch=(char)c;
874: sb.append(ch);
875: // System.out.print((char)c);
876: }
877: addToBundle(bundle,sb);
878: } catch(Exception e) { e.printStackTrace();}
879:
880:
881: return bundle;
882: }
883: public static Bundle getBundle(String url)
884: {
885: Bundle bundle=new Bundle();
886:
887: try
888: {
889: if(url=="") url="http://core.rz-berlin.mpg.de:80/edoctest/oi.epl?mm=list&grp=90";
890: // read XML document from URL into a string
891: URL u = new URL(url);
892: // FileReader r=new FileReader();
893: long time = System.currentTimeMillis();
894:
895: //Connection con= new Connection(new URL("http://core.rz-berlin.mpg.de:80/edoctest/adm.epl"));
896: // String ses=con.getSessionID("name=root&passwd=edoctest&mode=verifylogin");
897: // con.socket.close();
898: Connection conTest=new Connection(new URL(url));
899: conTest.ses=ses;
900: conTest.get();
901: StringBuffer sb=readChunked(conTest);
902: // sb.delete( sb.toString().indexOf("<rt>"),sb.length());
903: //System.out.println(sb);
904: String rt =addToBundle(bundle,sb);
905: StringBuffer sb2=null;
906: int index=sb.toString().indexOf("<edoc>",3)+6,index2=0;
907: //for(int i=20;i<260;i=i+20)
908:
909:
910: while(true)
911: {
912: if(rt.equals("finished")) break;
913: conTest.url=new URL(url+"&rt="+rt);
914: conTest.get();
915: sb2=readChunked(conTest);
916: //sb2.delete(0,index);
917: // index2=sb2.toString().indexOf("<rt>",index);
918: // sb2.delete(index2,sb2.length());
919: //sb.append(sb2);
920: rt=addToBundle(bundle,sb2);
921: }
922: //sb.append("</edoc>");
923: //System.out.println(sb);
924: long timeEnd = System.currentTimeMillis();
925: System.err.println("time to get bundle data "+(timeEnd-time));
926:
927: /*
928: // read XML Metadata from a file
929: FileReader r=new FileReader("list7.xml");
930: sb=new StringBuffer();
931: // String response= conTest.getResponseMessage();
932: int c=0;
933: while ((c = r.read()) != -1)
934: {
935: char ch=(char)c;
936: sb.append(ch);
937: // System.out.print((char)c);
938: }
939: */
940: String data=sb.toString().length()+"";
941: // System.out.println(new String(sb.toString().getBytes(),"UTF-8"));
942: // System.out.println(sb.toString());
943: //addToBundle(bundle,sb);
944: // System.out.println(sb.toString());
945: // System.exit(0);
946:
947: } catch(Exception e) {e.printStackTrace();}
948: return bundle;
949: }
950: public static String addToBundle(Bundle bundle,StringBuffer sb)
951: {
952: boolean finished = false;
953: String rtTag="finished";
954: // parse string and build document tree
955: Node root = new Xparse().parse(sb.toString());
956: /*
957: Vector contents=root.index.v;
958: for (int i=0;i<contents.size();++i)
959: {
960: Node n=(Node)contents.elementAt(i);
961: if(n.type.equals("element"))
962: {
963: // System.out.println("tag "+n.name+" with contents ");
964: // System.out.println(n.getCharacters());
965: //contents=n.contents.v;i=0;
966: }
967: // System.out.println(n.type);
968:
969: }
970: */
971: Node tempNode=root.find(rootNode,new int[] {1});
972: if(tempNode==null) return rtTag;
973: int count=1;
974: /*
975: for(int i=0;i<tempNode.contents.v.size();++i)
976: {
977: // System.out.println(((Node)tempNode.contents.v.elementAt(i)).attributes+" "+i);
978: }*/
979: if(tempNode.contents.v.size()==0) return rtTag;
980: Node notNull=null;
981: Node rtNode=root.find(rootNode+"/rt",new int[] {1,1});
982: rtTag=(rtNode==null) ? "finished":rtNode.getCharacters();
983: System.out.println("rt tag is "+ rtTag);
984: int length=0;//(tempNode.contents.length()-1)/2;
985: for(int i=0;i<tempNode.contents.v.size();++i) {
986: Node node=(Node)tempNode.contents.v.elementAt(i);
987: if(node.type.equals("element"))
988: if(node.name.equals("doc"))length++;
989: // System.out.println(((Node)tempNode.contents.v.elementAt(i)).attributes+" "+i);
990: }
991: if(length==0) rtTag="finished";
992: while(count<=length)
993: {
994: //System.out.println(count+" "+length);
995: tempNode=root.find(rootNode+"/doc",new int[] {1,count});
996: if(tempNode==null)
997: {
998: rtTag="finished";
999: break;
1000: }
1001: Bundle.Document doc = new Bundle.Document();
1002: Hashtable attributes=tempNode.attributes;
1003: int docID=0;
1004: int ver=0;
1005: if(attributes != null)
1006: {
1007: docID=Integer.parseInt((String)attributes.get("id"));
1008: ver=Integer.parseInt((String)attributes.get("ver"));
1009: doc.addData("docID",(String)attributes.get("id"));
1010: doc.addData("ver",(String)attributes.get("ver"));
1011: }
1012: bundle.docs.put(new Integer(docID),doc);
1013: bundle.documents.addElement(doc);
1014: // System.out.println("id " +docID+" ver "+ver);
1015: Node fileNode=root.find(rootNode+"/doc/file",new int[] {1,count,1});
1016: attributes=(fileNode==null)? null:fileNode.attributes;
1017: int fileID=0;
1018: int fileVer=0;
1019: if(attributes != null)
1020: {
1021: fileID=Integer.parseInt((String)attributes.get("id"));
1022: fileVer=Integer.parseInt((String)attributes.get("ver"));
1023: doc.addData("fileID",(String)attributes.get("id"));
1024: doc.addData("fileVer",(String)attributes.get("ver"));
1025: // System.out.println("fileid " +fileID+" ver "+fileVer);
1026: }
1027: // add all Document tags to actual Document
1028: for(int i=0;i<tempNode.contents.v.size();++i)
1029: {
1030: Node node=(Node)tempNode.contents.v.elementAt(i);
1031: if(node.type.equals("element"))
1032: {
1033: //System.out.println(node.name+" "+node.getCharacters()+" "+node.contents.v.size());
1034: if(node.contents.v.size()>1)
1035: {
1036: for(int j=0;j<node.contents.v.size();++j)
1037: {
1038: Node node2=(Node)node.contents.v.elementAt(j);
1039: if(node2.type.equals("element"))
1040: {
1041: doc.addData(node2.name,node2.getCharacters());
1042: //System.out.println(node2.name+" "+node2.getCharacters()+" "+node2.contents.v.size());
1043: }
1044: }
1045: } else doc.addData(node.name,node.getCharacters());
1046: }
1047: }
1048: bundle.docsLocator.put(doc.locator,doc);
1049:
1050: //System.out.println( bundle.docsLocator);//doc.extraTags);
1051: /*
1052: notNull=root.find("edoc/doc/title",new int[]{1,count,1});
1053: String title=(notNull!=null) ? notNull.getCharacters():"missing";
1054: // System.out.println("edoc/doc/title "+title);
1055: notNull=root.find("edoc/doc/comment",new int[]{1,count,1});
1056: String comment = (notNull!=null) ? notNull.getCharacters():"missing";
1057: // System.out.println("edoc/doc/comment "+comment);
1058: notNull =root.find("edoc/doc/placeofevent",new int[]{1,count,1});
1059: String placeofevent = (notNull!=null) ? notNull.getCharacters():"missing";
1060: // System.out.println("edoc/doc/placeofevent "+placeofevent);
1061: notNull=root.find("edoc/doc/locater",new int[]{1,count,1});
1062: String locator=(notNull!=null) ? notNull.getCharacters():"missing";
1063: // System.out.println("edoc/doc/locater "+locator);
1064: notNull = root.find("edoc/doc/file/filename",new int[]{1,count,1,1});
1065: String fileName=(notNull!=null) ? notNull.getCharacters():"missing";
1066: // System.out.println("edoc/doc/file/filename "+fileName);
1067: notNull = root.find("edoc/doc/file/mimetype",new int[]{1,count,1,1});
1068: String mimeType=(notNull!=null) ? notNull.getCharacters():"missing";
1069: // System.out.println("edoc/doc/file/mimetype "+mimeType);
1070: notNull = root.find("edoc/doc/file/md5cs",new int[]{1,count,1,1});
1071: String md5cs = (notNull!=null) ? notNull.getCharacters():"missing";
1072: // System.out.println("edoc/doc/file/md5cs "+md5cs);
1073: notNull=root.find("edoc/doc/file/size",new int[]{1,count,1,1});
1074: int size =(notNull!=null) ? Integer.parseInt(notNull.getCharacters()):0;
1075: // System.out.println("edoc/doc/file/size "+size);
1076: notNull = root.find("edoc/doc/file/filelink",new int[]{1,count,1,1});
1077: String fileLink = (notNull!=null) ? notNull.getCharacters():"missing";
1078: // System.out.println("edoc/doc/file/filelink "+fileLink);
1079: Bundle.Document doc=new Bundle. Document(docID,ver,title,locator,comment,placeofevent,fileID,fileVer,fileName,mimeType,md5cs,size,fileLink);
1080: bundle.docs.put(new Integer(docID),doc);
1081: bundle.documents.addElement(doc);
1082: */
1083: count++;
1084: }
1085: // System.out.println(bundle.docs.size());
1086: return rtTag;
1087: }
1088: public static void getGenre()
1089: {
1090:
1091: try
1092: {
1093: String url="http://core.rz-berlin.mpg.de:80/edoctest/oi.epl?mm=help&sm=genres";
1094: // read XML document from URL into a string
1095: URL u = new URL(url);
1096: // FileReader r=new FileReader();
1097: long time = System.currentTimeMillis();
1098: // debug=true;
1099: Connection conTest=new Connection(new URL(url));
1100: conTest.ses=ses;
1101: conTest.get();
1102: StringBuffer sb=readChunked(conTest);
1103: Bundle.genresId.clear();
1104: Bundle.genresLabel.clear();
1105: Bundle.genresName.clear();
1106: Bundle.genVec.removeAllElements();
1107: getGenreList(sb);
1108: long timeEnd = System.currentTimeMillis();
1109: System.err.println("time to get genre data "+(timeEnd-time));
1110: // Enumeration en = Bundle.genres.keys();
1111: //while(en.hasMoreElements()
1112: if(debug)
1113: {
1114: Collections.sort(Bundle.genVec);
1115: // Bundle.Genre.debug=true;
1116:
1117: for(int i=0;i<Bundle.genVec.size();++i)
1118: System.out.println(Bundle.genresId.get(Bundle.genVec.get(i)));
1119: //System.out.println( Bundle.genres);
1120: }
1121: } catch(Exception e) {e.printStackTrace();}
1122: }
1123: public static String getGenreList(StringBuffer sb)
1124: {
1125: boolean finished = false;
1126: String rtTag="finished";
1127: // parse string and build document tree
1128: Node root = new Xparse().parse(sb.toString());
1129: /*
1130: Vector contents=root.index.v;
1131: for (int i=0;i<contents.size();++i)
1132: {
1133: Node n=(Node)contents.elementAt(i);
1134: if(n.type.equals("element"))
1135: {
1136: // System.out.println("tag "+n.name+" with contents ");
1137: // System.out.println(n.getCharacters());
1138: //contents=n.contents.v;i=0;
1139: }
1140: // System.out.println(n.type);
1141:
1142: }
1143: */
1144: Node tempNode=root.find(rootNode,new int[] {1});
1145: if(tempNode==null) return rtTag;
1146: int count=1;
1147: /*
1148: for(int i=0;i<tempNode.contents.v.size();++i)
1149: {
1150: // System.out.println(((Node)tempNode.contents.v.elementAt(i)).attributes+" "+i);
1151: }*/
1152: if(tempNode.contents.v.size()==0) return rtTag;
1153: Node notNull=null;
1154: int length=0;
1155: for(int i=0;i<tempNode.contents.v.size();++i) {
1156: Node node=(Node)tempNode.contents.v.elementAt(i);
1157: if(node.type.equals("element"))
1158: if(node.name.equals("genre"))length++;
1159: // System.out.println(((Node)tempNode.contents.v.elementAt(i)).attributes+" "+i);
1160: }
1161: if(length==0) rtTag="finished";
1162: while(count<=length)
1163: {
1164: //System.out.println(count+" "+length);
1165: tempNode=root.find(rootNode+"/genre",new int[] {1,count});
1166: if(tempNode==null)
1167: {
1168: rtTag="finished";
1169: break;
1170: }
1171: Bundle.Genre genre= new Bundle.Genre();
1172: Hashtable attributes=tempNode.attributes;
1173: Integer id=null;
1174: if(attributes != null)
1175: {
1176: genre.id=new Integer((String)attributes.get("id"));
1177: }
1178: for(int i=0;i<tempNode.contents.v.size();++i)
1179: {
1180: Node node=(Node)tempNode.contents.v.elementAt(i);
1181: if(node.type.equals("element"))
1182: {
1183: //System.out.println(node.name+" "+node.getCharacters()+" "+node.contents.v.size());
1184: if(node.contents.v.size()>1)
1185: {
1186: for(int j=0;j<node.contents.v.size();++j)
1187: {
1188: Node node2=(Node)node.contents.v.elementAt(j);
1189: if(node2.type.equals("element"))
1190: {
1191: //genre.addData(node2.name,node2.getCharacters());
1192: //System.out.println(node2.name+" "+node2.getCharacters()+" "+node2.contents.v.size());
1193: }
1194: }
1195: } else genre.addData(node.name,node.getCharacters());
1196: }
1197: }
1198: Bundle.genresId.put(genre.id,genre);
1199: Bundle.genresLabel.put(genre.label.toLowerCase(),genre.id);
1200: Bundle.genresName.put(genre.name.toLowerCase(),genre.id);
1201: Bundle.genVec.addElement(genre.id);
1202: count++;
1203: }
1204: // System.out.println(bundle.docs.size());
1205: return rtTag;
1206: }
1207:
1208: public static void main(String args[])
1209: {
1210: // System.out.println("Bundle for http://core.rz-berlin.mpg.de/edocmike/oi.epl?mm=list&grp=90");
1211: // getBundle("");
1212: // System.out.println("Bundle for http://core.rz-berlin.mpg.de/edocmike/oi.epl?mm=list&grp=89");
1213: // getBundle("http://core.rz-berlin.mpg.de/edoctest/oi.epl?mm=list&grp=136");
1214: // getBundle("http://core.rz-berlin.mpg.de/edoctest/oi.epl?mm=list&grp=136");
1215: runningAsApplet=false;
1216: // Object[] options = new Object[] { "New Version for this file","New Version for all files", "Skip file" };
1217:
1218: // int option = showDialog("Warning exists! \n What do you want to do?", "Destination exists!", options,null);
1219:
1220: long time = System.currentTimeMillis();
1221: new Upload();
1222: // bundle=getBundle("http://core.rz-berlin.mpg.de/edoctest/oi.epl?mm=list&grp=136&col=11");
1223: url="http://core.rz-berlin.mpg.de/edoctest/";
1224: bundleRef = getBundle(url+"oi.epl?mm=list&grp="+grp+"&col="+col);
1225: getGenre();
1226: //getMetaData("");
1227: // if(true)System.exit(0);
1228: /* Hashtable data=getMetaData("");
1229: //if(true)System.exit(0);
1230: try
1231: {
1232: // old edocmike
1233: Connection con= new Connection(new URL("http://core.rz-berlin.mpg.de:80/edoctest/adm.epl"));
1234: String ses=con.getSessionID("name=root&passwd=&mode=verifylogin");
1235: //("name=dwinter&passwd=e1nste1n&mode=verifylogin");
1236: con.socket.close();
1237: Vector artist=(Vector)data.get("artist");
1238: Vector picture=(Vector)data.get("picture");
1239: Vector idnr=(Vector)data.get("IDNR");
1240: Vector source=(Vector)data.get("source");
1241: Connection conPost=new Connection( new URL("http://core.rz-berlin.mpg.de:80/edoctest/oi.epl"));
1242: // new URL("http://localhost:8080/examples/servlet/rogo/"));
1243: conPost.ses=ses;
1244:
1245: for(int i=135;i<136;++i)//i<artist.size();++i)
1246: {
1247: // System.out.println("artist "+artist.elementAt(i));
1248: // System.out.println("picture nr "+ i +" name "+picture.elementAt(i));
1249: // System.out.println("file" +idnr.elementAt(i));
1250: // System.out.println("source"+source.elementAt(i));
1251: // if(!new File("/Volumes/data/tmp/Bildarchiv-Daston/400/"+idnr.elementAt(i)).exists())
1252: // System.out.println("file "+idnr.elementAt(i) +" does not exist");
1253: // if(idnr.elementAt(i)!=null)
1254: // if(idnr.elementAt(i).toString().indexOf("Ÿ")>=0||idnr.elementAt(i).toString().indexOf("š")>=0||idnr.elementAt(i).toString().indexOf("Š")>=0)
1255: // System.out.println("file "+idnr.elementAt(i) +" contains umlaut ");
1256:
1257: if(new File("/Volumes/data/tmp/Bildarchiv-Daston/400/"+idnr.elementAt(i)).exists())
1258: 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());
1259: // conPost.post(new File("./test2.jpg"),picture.elementAt(i).toString(),artist.elementAt(i).toString(),source.elementAt(i).toString());
1260:
1261: }// to for
1262:
1263: } catch(Exception e)
1264: {
1265: e.printStackTrace();
1266: }
1267: */
1268: long time2 = System.currentTimeMillis();
1269: // System.out.println("time elapsed for upload " +((time2-time)/1000)+" seconds");
1270: }
1271: static class Connection
1272: {
1273: Socket socket;
1274: URL url;
1275: BufferedInputStream in;
1276: PrintStream out;
1277: String pHost;
1278: int pPort = 80;
1279: String proxyHost="";
1280: String ses="";
1281: HttpURLConnection con;
1282: UploadInfo dialog = new UploadInfo();
1283: public Connection(URL url) throws Exception
1284: {
1285: Properties prop = System.getProperties();
1286: pHost = null;
1287: pPort = 80;
1288: Enumeration en = prop.propertyNames();
1289: while (en.hasMoreElements())
1290: {
1291: String key = (String) en.nextElement();
1292: if (key.toString().indexOf("proxyHost") >= 0)
1293: {
1294: Object host = prop.get(key);
1295: if (host != null)
1296: pHost = host.toString();
1297: }
1298: if (key.toString().indexOf("proxyPort") >= 0)
1299: {
1300: Object port = prop.get(key);
1301: if (port != null)
1302: pPort = Integer.parseInt(port.toString());
1303: }
1304: }
1305:
1306: if (proxyHost != "")
1307: {
1308: int index = proxyHost.indexOf(":");
1309: pHost = proxyHost.substring(0, (index > 0) ? index : proxyHost.length());
1310: pPort = (index > 0) ? Integer.parseInt(proxyHost.substring(index + 1)) : 80;
1311: }
1312: int port = (url.getPort()>0) ? url.getPort():80;
1313: socket = new Socket((pHost != null) ? pHost : url.getHost(), (pHost != null) ? pPort :port );
1314: socket.setTcpNoDelay(true);
1315: in = new BufferedInputStream(socket.getInputStream(),2);
1316: out = new PrintStream(new BufferedOutputStream(socket.getOutputStream()),true);
1317: this.url = url;
1318: }
1319: public String getSessionID(String data)
1320: {
1321: try
1322: {
1323: int size=data.length();
1324: // System.out.println("Yeah i called connect");
1325: // socket = new Socket((pHost != null) ? pHost : url.getHost(), (pHost != null) ? pPort : url.getPort());
1326: // socket.setTcpNoDelay(true);
1327: in = new BufferedInputStream(socket.getInputStream());
1328: out = new PrintStream(new BufferedOutputStream(socket.getOutputStream()),true);
1329:
1330: 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());
1331: out.print(data);
1332: out.flush();
1333: String response=getResponseMessage();
1334: int index=response.indexOf("Set-Cookie");
1335: int index2=response.indexOf("\r\n",index);
1336: String cookie=response.substring(index,index2);
1337: index=cookie.indexOf("=");
1338:
1339: // // System.out.print(response);
1340: System.err.println("Session key: "+cookie.substring(index+1));
1341: return cookie.substring(index+1);//sessionID
1342: } catch (Exception e)
1343: {
1344: // System.out.println(e);
1345: }
1346: return "";
1347: }
1348: public void post(File file,String mode,String docID,String title,String author,String source) {
1349: try
1350: {
1351: // System.out.println("Yeah i called post");
1352: in = new BufferedInputStream(socket.getInputStream());
1353: out = new PrintStream(new BufferedOutputStream(socket.getOutputStream()),true);
1354: /*if(in.isClosed())
1355: {
1356: socket = new Socket((pHost != null) ? pHost : url.getHost(), (pHost != null) ? pPort : url.getPort());
1357: socket.setTcpNoDelay(true);
1358: in = new BufferedInputStream(socket.getInputStream());
1359: out = new PrintStream(new BufferedOutputStream(socket.getOutputStream()),true);
1360: }*/
1361: FileInputStream f=new FileInputStream(file);
1362: int length=f.available();
1363: // ses="37b9cec1da340f16f5ef603112e6fe84";
1364: /*
1365: String data = "--xYzZY\r\nContent-Disposition: form-data; name=\"mm\"\r\n\r\ninput\r\n"+
1366: "--xYzZY\r\nContent-Disposition: form-data; name=\"ses\"\r\n\r\n"+ses+"\r\n"
1367: +"--xYzZY\r\nContent-Disposition: form-data; name=\"col\"\r\n\r\n"+"11"+"\r\n"
1368: +"--xYzZY\r\nContent-Disposition: form-data; name=\"grp\"\r\n\r\n"+"78"+"\r\n"
1369: +"--xYzZY\r\nContent-Disposition: form-data; name=\"bun\"\r\n\r\n"+"78"+"\r\n"
1370: +"--xYzZY\r\nContent-Disposition: form-data; name=\"sm\"\r\n\r\n"+"new"+"\r\n"
1371: // +"--xYzZY\r\nContent-Disposition: form-data; name=\"ver\"\r\n\r\n"+"0"+"\r\n"
1372: +"--xYzZY\r\nContent-Disposition: form-data; name=\"docid\"\r\n\r\n"+"10924"+"\r\n"
1373: +"--xYzZY\r\nContent-Disposition: form-data; name=\"gentyp\"\r\n\r\n"+"43"+"\r\n"
1374: +"--xYzZY\r\nContent-Disposition: form-data; name=\"title\"\r\n\r\n"+"sometitlexyz"+"\r\n"
1375: +"--xYzZY\r\nContent-Disposition: form-data; name=\"placeofevent\"\r\n\r\n"+"someplaceofevent"+"\r\n"
1376: +"--xYzZY\r\nContent-Disposition: form-data; name=\"locater\"\r\n\r\n"+"somelocator"+"\r\n"
1377: +"--xYzZY\r\nContent-Disposition: form-data; name=\"authorcomment\"\r\n\r\n"+"somecomment"+"\r\n"
1378: +"--xYzZY\r\nContent-Disposition: form-data; name=\"nfiles\"\r\n\r\n"+"1"+"\r\n"
1379: +"--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
1380: */
1381:
1382: FormData formData=new FormData(file,ses,col,grp,mode,"0",docID);
1383: //format for const File file,String ses,String col,String grp,String sm,String ver,String docid
1384: formData.addEntry("title",title);
1385: formData.addEntry("placeofevent",author);
1386: formData.addEntry("comment",source);
1387: formData.addEntry("locater",source);
1388: formData.addFile(formData.file.getName(),"image/tiff",length+"");
1389: String data=formData.buff.toString();
1390: String ftacc1="--xYzZY\r\nContent-Disposition: form-data; name=\"ftacc1\"\r\n\r\n"+"PUBLIC"+"\r\n";
1391:
1392: System.err.println("cl "+(data.length()+length+ 13 + ftacc1.length()));
1393: 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"
1394: +"Cookie: EMBPERL_UID="+ses+"\r\nHost: " + url.getHost() + "\r\n\r\n"));//.getBytes());
1395: //+"Host: " + url.getHost() + "\r\n\r\n"));
1396: // 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());
1397: out.print(data);
1398: if(debug)
1399: System.out.print(data);
1400: // System.out.println("Finished upload");
1401: // System.out.println("Content-Length "+(data.length()+11+length));
1402: // System.out.print("\r\n--xYzZY--\r\n");// 11 bytes
1403: // System.out.print("\r\n");
1404: sendFile(f,out);
1405: out.print("\r\n");
1406: out.print(ftacc1);
1407: if(debug)
1408: System.out.print(ftacc1);
1409: out.print("--xYzZY--\r\n");
1410: if(debug)
1411: System.out.print("--xYzZY--\r\n");
1412: // System.err.println("Yeah i called connect");
1413: dialog.progress.setValue(99);
1414: String response=getResponseMessage();
1415: System.err.print(response);
1416: if(response.toLowerCase().indexOf("content-length")>=0||(response.toLowerCase().indexOf("content-type")>=0))
1417: {
1418: int c=0;
1419: while ((c = in.read()) != -1)
1420: {
1421: System.err.print((char)c);
1422: }
1423: }
1424: } catch (Exception e)
1425: {
1426: // System.out.println(e);
1427: }
1428: }
1429: public void post(String path,Bundle.Document doc,String mode) throws Exception{
1430: // try
1431: // {
1432: System.out.println("Yeah i called post");
1433: in = new BufferedInputStream(socket.getInputStream());
1434: out = new PrintStream(new BufferedOutputStream(socket.getOutputStream()),true);
1435: /*if(in.isClosed())
1436: {
1437: socket = new Socket((pHost != null) ? pHost : url.getHost(), (pHost != null) ? pPort : url.getPort());
1438: socket.setTcpNoDelay(true);
1439: in = new BufferedInputStream(socket.getInputStream());
1440: out = new PrintStream(new BufferedOutputStream(socket.getOutputStream()),true);
1441: }*/
1442: String name = doc.fileLink.trim().startsWith("file://") ?doc.fileLink.trim().substring(7):doc.fileLink.trim();
1443: System.out.println(name);
1444: String filePath = name.startsWith("/") ? name:path+name;
1445: FileInputStream f=new FileInputStream( filePath);
1446: int length=f.available();
1447:
1448: FormData formData=new FormData(new File(doc.fileName),ses,col,grp,mode,"1",doc.docID+"",doc.type);// "1" means new version if mode == old
1449: //format for const File file,String ses,String col,String grp,String sm,String ver,String docid
1450: Enumeration en = doc.metaTags.keys();
1451: while(en.hasMoreElements())
1452: {
1453: String key=(String)en.nextElement();
1454: formData.addEntry(key,(String)doc.metaTags.get(key));
1455: }
1456: formData.addFile(formData.file.getName(),doc.mimeType,length+"");
1457: String data=formData.buff.toString();
1458: //System.out.print(data);
1459: String ftacc1="--xYzZY\r\nContent-Disposition: form-data; name=\"ftacc1\"\r\n\r\n"+"PUBLIC"+"\r\n";
1460:
1461: System.err.println("cl "+(data.length()+length+ 13 + ftacc1.length()));
1462: 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"
1463: +"Cookie: EMBPERL_UID="+ses+"\r\nHost: " + url.getHost() + "\r\n\r\n"));//.getBytes());
1464: //+"Host: " + url.getHost() + "\r\n\r\n"));
1465: // 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());
1466: out.print(data);
1467: if(out.checkError()) throw new Error("Error while sending form data");
1468: if(debug)
1469: System.out.print(data);
1470: // System.out.println("Finished upload");
1471: // System.out.println("Content-Length "+(data.length()+11+length));
1472: // System.out.print("\r\n--xYzZY--\r\n");// 11 bytes
1473: // System.out.print("\r\n");
1474: sendFile(f,out);
1475: out.print("\r\n");
1476: out.print(ftacc1);
1477: if(debug)
1478: System.out.print(ftacc1);
1479: out.print("--xYzZY--\r\n");
1480: if(debug)
1481: System.out.print("--xYzZY--\r\n");
1482: // System.err.println("Yeah i called connect");
1483: dialog.progress.setValue(99);
1484: // String response=getResponseMessage();
1485: // System.err.print(response);
1486: int c=0;StringBuffer sb = new StringBuffer();
1487: sb=readChunked(this);
1488: if(debug)
1489: System.out.println(sb);
1490: if(sb.length()>0)
1491: addToBundle(bundleRef,sb);
1492: try
1493: {
1494:
1495: Bundle.Document newDoc =(Bundle.Document) bundleRef.docsLocator.get(doc.locator);
1496: doc.md5cs=newDoc.md5cs;
1497: doc.size=newDoc.size;
1498: doc.fileID=newDoc.fileID;
1499: doc.docID=newDoc.docID;
1500: doc.ver=newDoc.ver;
1501: doc.fileVer=newDoc.fileVer;
1502: bundle.exportToXML(new File("./metaout.xml"));
1503: } catch(Exception e7) { e7.printStackTrace();}
1504: }
1505:
1506: public void sendFile(FileInputStream f,PrintStream out) throws Exception
1507: {
1508: int length=f.available();
1509: md.reset();
1510: dialog.progress.setValue(0);
1511: byte[] b=new byte[512];
1512: int readCount =0;
1513: while (readCount < length)
1514: {
1515: if (readCount + b.length > length)
1516: f.read(b, 0, length - readCount); // mout.flush();
1517: else
1518: f.read(b);
1519: if (readCount + b.length > length)
1520: {
1521: out.write(b, 0, length - readCount); // mout.flush();
1522: md.update(b, 0, length - readCount);
1523: if(out.checkError()) throw new Error("Error while sending file data");
1524: //if(debug)
1525: //System.out.write(b, 0, length - readCount); // mout.flush();
1526: }
1527: else
1528: {
1529: // if(debug)
1530: //System.out.write(b); // mout.flush();
1531: out.write(b);
1532: if(out.checkError()) throw new Error("Error while sending file data");
1533: md.update(b, 0, b.length);
1534: }
1535: readCount += b.length;
1536: dialog.progress.setValue((int) (((double) readCount / (double) length) * 100.0));
1537: }
1538:
1539: }
1540: public void get() throws Exception
1541: {
1542: try
1543: {
1544: // System.out.println("Yeah i called connect");
1545: // socket = new Socket((pHost != null) ? pHost : url.getHost(), (pHost != null) ? pPort : url.getPort());
1546: // socket.setTcpNoDelay(true);
1547: in = new BufferedInputStream(socket.getInputStream());
1548: out = new PrintStream(new BufferedOutputStream(socket.getOutputStream()),true);
1549:
1550: // 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());
1551: out.print(("GET " + url + " HTTP/1.1\r\nCookie: EMBPERL_UID="+ses+"\r\nHost: " + url.getHost() + "\r\n\r\n"));
1552: out.flush();
1553: } catch (Exception e)
1554: {
1555: int port = (url.getPort()>0) ? url.getPort():80;
1556:
1557: socket = new Socket((pHost != null) ? pHost : url.getHost(), (pHost != null) ? pPort : url.getPort());
1558: socket.setTcpNoDelay(true);
1559: in = new BufferedInputStream(socket.getInputStream());
1560: out = new PrintStream(new BufferedOutputStream(socket.getOutputStream()),true);
1561: // out.print(("PUT " + url + " HTTP/1.1\r\nContent-Type:text/html\r\nContent-Length:" + size + "\r\nHost: " + url. getHost() + "\r\n\r\n"));
1562: out.flush();
1563: // System.out.println("Error "+e);
1564: }
1565:
1566: /*
1567: con = new HTTPClient.HttpURLConnection(url);//(HttpURLConnection) url.openConnection(); // con.setRequestMethod("GET");
1568: con.setUseCaches(false);
1569: con.setRequestMethod("PUT");
1570: con.setDoOutput(true);
1571: con.setRequestProperty("Content-Length", ""+size);
1572: // con.connect();
1573: // System.out.println(con.getClass());
1574: out = new DataOutputStream(con.getOutputStream());
1575: */
1576: }
1577: public boolean update() throws Exception
1578: {
1579: boolean exists = false;
1580: String response = "";
1581: try
1582: {
1583:
1584: URL url = null;//createURL(this.url.toString(), getSHA((id + "").getBytes()));
1585: // con = new HTTPClient.HttpURLConnection(url);//(HttpURLConnection) url.openConnection(); // con.setRequestMethod("GET");
1586: //con.setUseCaches(false);
1587: // con.setRequestMethod("HEAD");
1588: //con.setDoOutput(true);
1589: // con.connect();
1590:
1591: out.print(("HEAD " + url + " HTTP/1.1\r\nConnection:Keep-Alive\r\nHost: " + url.getHost() + "\r\n\r\n"));//.getBytes());
1592: out.flush();
1593: response = getResponseMessage();
1594: exists = response.indexOf("OK") >= 0;
1595: // System.out.println("Head "+response);
1596:
1597: } catch (Exception e)
1598: {
1599: socket = new Socket((pHost != null) ? pHost : url.getHost(), (pHost != null) ? pPort : url.getPort());
1600: socket.setTcpNoDelay(true);
1601: in = new BufferedInputStream(socket.getInputStream());
1602: out = new PrintStream(new BufferedOutputStream(socket.getOutputStream()),true);
1603: // out.writeBytes("HEAD " + url + " HTTP/1.1\r\n Connection:Keep-Alive\r\n \r\nHost: " + url.getHost() + //"\r\n\r\n");
1604: URL url = null;//createURL(this.url.toString(), getSHA((id + "").getBytes()));
1605: out.print(("HEAD " + url + " HTTP/1.1\r\nConnection:Keep-Alive\r\nHost: " + url.getHost() + "\r\n\r\n"));//.getBytes());
1606: out.flush();
1607: response = getResponseMessage();
1608: // System.out.println("Head " + response);
1609: exists = response.indexOf("OK") >= 0;
1610: //// System.out.println("Error from Head "+e);
1611: // e.printStackTrace();
1612: return exists;
1613:
1614: }
1615: return exists;
1616: }
1617:
1618: public String getResponseMessage()
1619: {
1620:
1621: int b=0;StringBuffer between=new StringBuffer();int count=0;
1622: try
1623: {
1624: int loop =0;int available = 0;
1625: while((available=in.available())==0)
1626: {
1627: if(loop>=3000) throw new Error("Connection timed out");
1628: long time = System.currentTimeMillis();
1629: while(System.currentTimeMillis()-time<100);
1630: if(available==0) loop++; else loop=0;
1631: }
1632: System.out.println("heh there are bytes "+in.available()+" available");
1633: int limit = in.available();
1634: while(true )
1635: {
1636: b=in.read();
1637: if(b=='\r')
1638: {
1639: between.append((char)b);
1640: // System.out.print("CR found ");
1641: b=in.read();
1642: if(b=='\n')
1643: {
1644: // System.out.print("LF found ");
1645: between.append((char)b);
1646: if((b=in.read())=='\r')
1647: {
1648: b=in.read();
1649: // System.out.print("end found ");
1650: break;
1651: }
1652: }
1653: }
1654: between.append((char)b);
1655: // System.out.print((char)b);
1656: // System.out.println("read "+count+" bytes");
1657: count++;
1658: if(count==limit) break;
1659: }
1660:
1661: } catch(Exception e) {
1662: e.printStackTrace();
1663: // System.out.println("Error found");
1664: if(e.toString().indexOf("peer")>=0)
1665: return getResponseMessage();
1666: else throw new Error("Error while reading response");
1667: }
1668:
1669: /*
1670: String response = "";
1671: try
1672: {
1673: response = con.getResponseMessage();
1674: // con.disconnect();
1675: } catch (Exception e)
1676: {
1677: showErrorDialog(e.getMessage(),"Shit happens");
1678: }
1679: */
1680: String response = between.toString();
1681: if(response.indexOf("close") >= 0)
1682: reconnect();
1683: return response;
1684:
1685: }
1686: public void disconnect() throws Exception
1687: {
1688: //socket.close();
1689: // con.disconnect();
1690: }
1691: public void reconnect()
1692: {
1693: try
1694: {
1695: System.out.println("Reconnect called");
1696: int port= (url.getPort()>=0) ? url.getPort():80;
1697: socket = new Socket((pHost != null) ? pHost : url.getHost(), (pHost != null) ? pPort :port);
1698: socket.setTcpNoDelay(true);
1699: in = new BufferedInputStream(socket.getInputStream());
1700: out = new PrintStream(new BufferedOutputStream(socket.getOutputStream()),true);
1701:
1702: } catch(Exception e)
1703: {
1704: showErrorDialog(e.getMessage(),"Shit happens");
1705: }
1706:
1707: }
1708:
1709: }
1710: public static class FormData
1711: {
1712: public StringBuffer buff=new StringBuffer();
1713: File file;
1714: public FormData()
1715: {}
1716: public FormData(File file,String ses,String col,String grp,String sm,String ver,String docid)
1717: {
1718: this(file,ses,col,grp,sm,ver,docid,"Archimedes Image Collection");
1719: }
1720: public FormData(File file,String ses,String col,String grp,String sm,String ver,String docid,String type)
1721: {
1722: this.file=file;
1723: addEntry("mm","input");
1724: addEntry("ses",ses);
1725: addEntry("col",col);
1726: addEntry("grp",grp);
1727: //addEntry("bun",bun);
1728:
1729: if(sm.equals("old"))
1730: {
1731: addEntry("sm","old");
1732: addEntry("ver",ver);
1733: addEntry("docid",docid);
1734:
1735: }
1736: else
1737: addEntry("sm","new");
1738: System.out.println("Type is "+type);
1739: type=type.toLowerCase().trim();
1740: Object genre=null;
1741: if((genre=Bundle.genresLabel.get(type))!=null)
1742: addEntry("gentyp",genre.toString());
1743: else if((genre=Bundle.genresName.get(type))!=null)
1744: addEntry("gentyp",genre.toString());
1745: else
1746: {
1747: Integer id=new Integer("0");
1748: id= new Integer(type);
1749: if((genre=Bundle.genresId.get(id))!=null)
1750: addEntry("gentyp",((Bundle.Genre)genre).id.toString());
1751: }
1752: System.out.println("Numerical Type is "+genre);
1753: }
1754: public void addEntry(String entry,String data)
1755: {
1756: buff.append("--xYzZY\r\nContent-Disposition: form-data; name=\"");
1757: buff.append(entry);
1758: buff.append("\"; charset=utf-8 \r\n\r\n");
1759: buff.append(data);
1760: buff.append("\r\n");
1761: }
1762: public void addFile(String name,String type,String length)
1763: {
1764: addEntry("nfiles","1");
1765: buff.append("--xYzZY\r\nContent-Disposition: form-data; name=\"file1\"");
1766: buff.append("; filename=\""+name+"\""+"\r\nContent-Length: "+length+"\r\n"+"Content-Type:"+type+"\r\n");
1767: buff.append("\r\n");
1768: }
1769:
1770: public void addEnd()
1771: {
1772: buff.append("\r\n--xYzZY--\r\n");
1773: }
1774:
1775: }
1776: static class UploadFilter implements FilenameFilter
1777: {
1778: public UploadFilter() {}
1779: public boolean accept(File dir, String name)
1780: {
1781: if(name.startsWith(".") || name.endsWith(".xml")) return false;
1782: else return true;
1783: }
1784: }
1785:
1786: static class DotFileFilter implements FilenameFilter
1787: {
1788: public DotFileFilter() {}
1789: public boolean accept(File dir, String name)
1790: {
1791: if(name.startsWith(".")) return false;
1792: else return true;
1793: }
1794: }
1795: static class XMLFilter extends javax.swing.filechooser.FileFilter
1796: {
1797: public boolean accept(java.io.File file)
1798: {
1799: if(file.getName().toLowerCase().endsWith(".xml")||file.isDirectory())
1800: return true ;
1801: else return false;
1802: }
1803: public String getDescription()
1804: {
1805: return "Metadata XML File";
1806: }
1807:
1808: }
1809: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>