//
// Upload.java
//
//
// Created by rogo on Wed Jan 29 2003.
// Copyright (c) 2003 Rogo Soft. 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,downloadChooser;
static String url;
static long id = 0L;
static Hashtable connections=new Hashtable();
static String col = "11"; // Collection /Institute
static String grp ="136"; // Group or Bundle
static String ses ="";
static Bundle bundle;
static Bundle bundleRef;
JButton upload = new JButton("Upload");
JButton download = new JButton("Download");
JRadioButton proxy = new JRadioButton("Proxy");
static int fileCount = 0;
// static UploadInfo dialog = new UploadInfo();
static String mode = "";
static String destination = "";
static String proxyHost = "";
Upload uploader = this;
static boolean runningAsApplet=true;
static boolean debug=false;
static boolean uploadMode = true;
static String rootNode = "edoc";
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(download);
this.getContentPane().add(proxy);
upload.addActionListener(this);
download.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;download=null;
if(!runningAsApplet)
{
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
Point p=new Point((screenSize.width-700)/2,(screenSize.height-80)/2);
JFrame testFrame=new JFrame();
testFrame.setTitle("Archimedes Applet");
testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
testFrame.getContentPane().add(this);
testFrame.setSize(300,80);
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("Download"))
{
if (downloadChooser == null)
{
downloadChooser = new JFileChooser();
downloadChooser.setFileSelectionMode(fileChooser.FILES_AND_DIRECTORIES);
downloadChooser.setFileFilter(new XMLFilter());
downloadChooser.setDialogType(JFileChooser.SAVE_DIALOG);
downloadChooser.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e2)
{
System.out.println(e2.getActionCommand());
if (!e2.getActionCommand().toString().equals("ApproveSelection")) return;
// {
Thread thread = new Thread()
{
public void run()
{
if(runningAsApplet)
set();
UploadInfo dialog = new UploadInfo();
File startFile=downloadChooser.getSelectedFile();
dialog.thread=Thread.currentThread();
//System.out.println("Called run with " + fileChooser.getSelectedFile());
dialog.setTitle("Download Status Window");
dialog.setSize(400, 175);
dialog.setLocation(new java.awt.Point(150, 100));
long time = System.currentTimeMillis();
try
{
Connection conGet = null;
if(!runningAsApplet)
conGet=new Connection( new URL("http://core.rz-berlin.mpg.de/edoctest/oi.epl"));
else
conGet=new Connection( new URL(url+"/oi.epl"));
conGet.ses=ses;
conGet.dialog=dialog;
Bundle bundle = bundleRef;
int fileCount = bundle.documents.size();
new File(startFile.getAbsolutePath()+"/images").mkdirs();
File indexFile = new File(startFile.getAbsolutePath()+"/index.xml");
if(indexFile.exists())
bundle = getBundle(indexFile);
else
bundleRef.exportToXMLDownload(new File(startFile.getAbsolutePath()+"/index.xml"),startFile);
boolean update = (bundle != bundleRef);
// System.out.println("update "+update);
Vector docs = new Vector(); Bundle.Document refDoc = null; Vector list = new Vector();
if(update)
for(int k = 0;k <fileCount; ++k)
{
Bundle.Document doc = (Bundle.Document)bundleRef.documents.elementAt(k);
if((refDoc=(Bundle.Document)bundle.docs.get(new Integer(doc.docID)))!=null)
{
if(checkForFileDownloaded(startFile.getAbsolutePath(),refDoc,doc))
{
if(!doc.downloadAgain)
list.addElement(doc.fileName);
docs.addElement(doc);//empty
}
} else docs.addElement(doc);
}
if(!update)
docs=bundleRef.documents;
System.out.println(docs.size());
fileCount=docs.size();boolean keepAllFiles=false,newVersionforAll=false;
for(int i=0;i< fileCount;++i)
{
Bundle.Document doc=(Bundle.Document)docs.get(i);
if(update&&(refDoc=(Bundle.Document)bundle.docs.get(new Integer(doc.docID)))!=null&&!doc.downloadAgain)
{
System.out.println(refDoc+" "+doc.fileName);
if(!newVersionforAll&&!keepAllFiles)
{
Object[] options = new Object[] { "Cancel","Replace existing files", "Keep existing files" };
Object[] options2 = new Object[] { "Cancel","Replace existing file", "Keep existing file" };
int option = (list.size()>1) ? showDialog("Warning : several files exist on the client \n Press show list to see the list! \n What do you want to do?", "several Files exist!", options,list):showDialog("Warning : file "+refDoc.fileName+" exists on the client \n What do you want to do?", " File "+doc.fileName+" exists!", options2);
;
if(option == 0) break;// mode = "old";
if(option == 1)
{
mode ="old";
newVersionforAll = true;
keepAllFiles = false;
}
if(option == 2)
{
count++;
System.out.println("Heh Document exists!"+ doc);
System.out.println("Skipping");
newVersionforAll = false;
keepAllFiles = true;
//continue;
}
}
if(keepAllFiles)
{
if(!doc.downloadAgain)
continue;
}
}// to bundle if
else
if(update)
{
bundle.docs.put(new Integer(doc.docID),doc);
bundle.documents.addElement(doc);
bundle.docsLocator.put(doc.locator,doc);
}
if(doc.fileLink=="") continue;
System.out.println(doc.title);
conGet.url=new URL(doc.fileLink);
conGet.get();
writeChunked(conGet,new File(startFile.getAbsolutePath()+"/images/"+doc.fileName),i+1, fileCount,doc.size, dialog);
String md5 =getMD5(conGet.md);
if(md5.equals(doc.md5cs)) System.out.println("download sucess");
else
{
System.out.println(" md5 "+md5 +" "+doc.md5cs+ " "+doc.fileName);
break;
}
if(debug)
System.out.println(" md5 "+md5 +" "+doc.md5cs+ " "+doc.fileName);
}
// close connection
conGet.socket.close();
dialog.setVisible(false);
long time2 =System.currentTimeMillis();
showDialog("Finished download of " + fileCount + " files in " + ((time2 - time) / 1000) + " seconds", "Finished Download");
if(update)
bundle.exportToXMLDownload(new File(startFile.getAbsolutePath()+"/index.xml"),startFile);
System.out.println("Time for download needed "+(time2-time));
} catch(Exception e5)
{
dialog.setVisible(false);
ByteArrayOutputStream b = new ByteArrayOutputStream();
PrintStream stream = new PrintStream(b);
e5.printStackTrace(stream);
//connections.remove(startFile.getAbsolutePath());
showErrorDialog(b.toString(), "Download failed");
}
catch(Error err)
{
showErrorDialog(err.toString(), "Download failed");
System.out.println("Error caught "+ err);
}
}
};
thread.start();
}
});
}
downloadChooser.showDialog(new JFrame()," Download ");
}
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).toString());
System.out.println(getMD5(conPost.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, keepAllFiles = false;
Vector list=new Vector();Vector docs=new Vector();Bundle.Document refDoc=null;
for(int k=0;k<fileCount;++k)
{
Bundle.Document doc = (Bundle.Document)bundle.documents.elementAt(k);
if((refDoc=(Bundle.Document)bundleRef.docsLocator.get(doc.locator))!=null)
{
if(checkForNewFile(path,doc,refDoc))
{
list.addElement(doc.fileName);
docs.addElement(doc);
}
} else docs.addElement(doc);
}
for(int k=0;k<docs.size();++k)
{
Bundle.Document doc = (Bundle.Document)docs.elementAt(k);
String response = "";
dialog.title.setText("Uploading " + path+doc.fileName);
dialog.progress.setValue(0);
String fileName = doc.fileName+ " " + ((doc.size > 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&&!keepAllFiles)
{
Object[] options = new Object[] { "Cancel","Replace existing files", "Keep existing files" };
Object[] options2 = new Object[] { "Cancel","Replace existing file", "Keep existing file" };
int option = (list.size()>1) ? showDialog("Warning : several files exist on the server \n Press show list to see the list! \n What do you want to do?", "several Files exist!", options,list):showDialog("Warning : file "+doc.fileName+" exists on the server \n What do you want to do?", " File "+doc.fileName+" exists!", options2);
;
if(option == 0) break;// mode = "old";
if(option == 1)
{
mode ="old";
newVersionforAll = true;
keepAllFiles = false;
}
if(option == 2)
{
count++;
System.out.println("Heh Document exists!"+ doc);
System.out.println("Skipping");
newVersionforAll = false;
keepAllFiles = true;
continue;
}
}
refDoc= (Bundle.Document) bundleRef.docsLocator.get(doc.locator);
doc.docID=refDoc.docID;
if(keepAllFiles)
{
count++;
continue;
}
//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(conPost.md);
System.out.println("MD5 is equal ?"+doc.md5cs.equals(md5)+" doc_md5 "+doc.md5cs+" md5 "+md5);
if(!doc.md5cs.equals(md5))
{
Object[] options = { "Retry", "Cancel" };
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(conPost.md);
if(!doc.md5cs.equals(md5)) showDialog("Transfer failed","Transfer failed again! ");
}
} // System.out.println(getMD5(md));
dialog.progress.setValue(100);
count++;
}
bundle.exportToXML(new File(path+"/temp.xml"));
return count;
}
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 boolean checkForNewFile(String path,Bundle.Document doc,Bundle.Document refDoc) throws Exception
{
String name = doc.fileLink.trim().startsWith("file://") ?doc.fileLink.trim().substring(7):doc.fileLink.trim();
//System.out.println(name);
boolean isNewFile=false;
String filePath = name.startsWith("/") ? name:path+name;
File file = new File(filePath);
if(!file.exists())
{
showErrorDialog("Warning \n"+filePath +" does not exist !","Warning file does not exist !");
return false;
}
if(file.isDirectory()) return false;
FileInputStream f=new FileInputStream( filePath);
int length=f.available();
if(length!=refDoc.size) isNewFile=true;
else
{
//new File(filePath).setLastModified(System.currentTimeMillis()-690000000L);
Date actualTime = new Date(new File(filePath).lastModified());
Date modified = new Date(doc.modified);//new File(filePath).lastModified());
// System.out.println("actual Date "+actualTime);
// System.out.println("Date file modified "+modified);
// System.out.println("Time difference in minutes "+((actualTime.getTime()-modified.getTime())/60000L));
// if file has been modified longer than 10 hours do not compare md5 --> file is not newer
if(actualTime.getTime()==modified.getTime())
{
f.close();
return false;
}
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] b=new byte[512000];
int readCount =0;
while (readCount < length)
{
if (readCount + b.length > length)
f.read(b, 0, length - readCount); // mout.flush();
else
f.read(b);
if (readCount + b.length > length)
{
md.update(b, 0, length - readCount);
}
else
{
md.update(b, 0, b.length);
}
readCount += b.length;
}
isNewFile = !getMD5(md).equals(refDoc.md5cs);
}
// close file
f.close();
if(isNewFile)
System.out.println(name+" "+refDoc.fileName);
// System.out.println("is new file ? "+isNewFile+" length is equal ?"+(length==refDoc.size));
return isNewFile;
}
public static boolean checkForFileDownloaded(String path,Bundle.Document doc,Bundle.Document refDoc) throws Exception
{
String name = doc.fileLink.trim().startsWith("file://") ?doc.fileLink.trim().substring(7):doc.fileLink.trim();
//System.out.println(name);
boolean isNewFile=false;
String filePath = name.startsWith("/") ? name:path+name;
File file = new File(filePath);
if(!file.exists())
{
refDoc.downloadAgain = true;
return true;
}
if(file.isDirectory()) return false;
FileInputStream f=new FileInputStream( filePath);
int length=f.available();
if(length!=refDoc.size)
{
// System.out.println(doc.fileName+" "+refDoc.fileName+" "+doc.size+" "
// +refDoc.size+" "+length);
refDoc.downloadAgain = doc.size>length;
isNewFile = true;
}
else
{
isNewFile = !doc.md5cs.equals(refDoc.md5cs);
}
f.close();
if(isNewFile)
System.out.println(name+" "+refDoc.fileName);
// System.out.println("is new file ? "+isNewFile+" length is equal ?"+(length==refDoc.size));
return isNewFile;
}
public static String convertUml(String newName)
{
StringBuffer alterMe = new StringBuffer(newName.trim());
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
final JDialog dialog = pane.createDialog(null, title);
JList list=null;
if(fileList==null)
list= new JList(new String[]{"click","here","test","bla","world","hello"});
else list = new JList(fileList);
JScrollPane scpane = new JScrollPane(list);
scpane.setPreferredSize(new Dimension(150,150));
JLabel label = new JLabel("Files to be updated : ");
label.setPreferredSize(new Dimension(150,30));
final JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(label,"North");
panel.add(scpane,"Center");
JButton showList= new JButton("show List");
showList.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JDialog window = new JDialog(dialog);
window.getContentPane().add(panel);
window.pack();
window.show();
}
});
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
buttonPanel.add(Box.createRigidArea(new Dimension(0,30)));
buttonPanel.add(showList);
dialog.getContentPane().add(buttonPanel,"East");
dialog.pack();
dialog.show();
Object selectedValue = pane.getValue();
if(selectedValue == null)
return 2;
//If there is not an array of option buttons:
if(options == null) {
if(selectedValue instanceof Integer)
return ((Integer)selectedValue).intValue();
return 2;
}
//If there is an array of option buttons:
for(int counter = 0, maxCounter = options.length;counter < maxCounter; counter++)
{
if(options[counter].equals(selectedValue))
return counter;
}
return 2;
}
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()<bufSize); // critical if connection closed
// r.read(b,0,bufSize);
//System.err.println(r.available());
r.read();r.read();
if(charset!="")
sb.append(new String(b,0,bufSize,charset));
else
sb.append(new String(b,0,bufSize));
if(debug)
System.out.println(new String(b,0,bufSize));
while ( (c = r.read()) != -1)
{
// ++countC;
char ch=(char)c;
//System.err.println("read lf "+(ch=='\r')+" or cr "+( ch=='\n')+" "+(Character.digit(ch, 16) == -1));
//if((ch!='\r')&&( ch!='\n')) System.err.println("read ch "+" "+ch+" "+Character.isSpace(ch));
if(Character.digit(ch, 16) != -1)
localBuff.append(ch);
if(ch=='\r')
{
ch=(char)r.read();
if( ch=='\n')
break;
}
}
if(debug)
System.out.println("chunk size "+localBuff);
if(localBuff.length()>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 void writeChunked(Connection conTest,File f,int count,int fileCount,int fileSize,UploadInfo dialog) throws Exception
{
InputStream r= conTest.in;
FileOutputStream file_out = new FileOutputStream(f);
BufferedOutputStream fout = new BufferedOutputStream(file_out);
int c,countC=0,limit=conTest.in.available();
// r.skip(211);
String response = conTest.getResponseMessage();
String lowerCase= response.toLowerCase();
int index =lowerCase.indexOf("charset");
int index2 =lowerCase.indexOf("=",index);
int index3 =lowerCase.indexOf("\r",index2);
String charset=(index>0) ? response.substring(index2+1,index3):"";
charset="";//charset.toUpperCase();
System.err.println(response);
dialog.title.setText("Downloading to " + f.toString());
dialog.progress.setValue(0);
MessageDigest md =conTest.md;
md.reset();
String fileName = f.getName()+ " " + ((fileSize > 1000000) ? (fileSize / 1048576) + " MByte" : fileSize+ " Bytes");
dialog.file.setText(fileName);
dialog.status.setText(count + " of " + fileCount + ((fileCount > 1) ? " files" : " file"));
//dialog.pack();
//dialog.validate();
if (dialog.isVisible())
dialog.repaint();
else
dialog.show();
if(lowerCase.indexOf("chunked")<0)
{
while(r.available()==0);
System.err.println(r.available());
while ((c = r.read()) != -1)
{
byte b=(byte)c;
fout.write(b);
//md.update(b, 0, b.length);
if(debug)
System.out.print((char)c);
}
/*
if(charset!="")
fout.write(local.toString(charset));
else
fout.write(local.toString());
*/
//if(debug)
// System.out.print(sb);
//return sb;
}
StringBuffer sb = new StringBuffer();
// read chunk size
while ((c = r.read()) != -1)
{
++countC;
char ch=(char)c;
if(Character.digit(ch, 16) != -1)
sb.append((char)c);
if(ch=='\r')
{
ch=(char)r.read();
if( ch=='\n')
break;
}
//System.out.print((char)c);
}
int bufSize=Integer.parseInt(sb.toString().trim(), 16),newBufSize=0;
System.err.println("chunk size "+Integer.parseInt(sb.toString().trim(), 16)+" bytes"+" limit "+limit);
// sb.append((char)r.read());
// sb.append((char)r.read());
sb = new StringBuffer();//.delete(0,sb.length());
byte[] b=new byte[bufSize];
countC=0;int size = 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);
}
else
{
readSize = r.read(b,readCount,readSize);
}
if(loop>=60)
{
loop = 0;
conTest.socket.close();
conTest.reconnect();
conTest.get();
r= conTest.in;
file_out = new FileOutputStream(f);
fout = new BufferedOutputStream(file_out);
countC=0;size=0;readCount= 0;conTest.md.reset();
response = conTest.getResponseMessage();
System.out.println(response);
while ((c = r.read()) != -1)
{
++countC;
char ch=(char)c;
if(Character.digit(ch, 16) != -1)
sb.append((char)c);
if(ch=='\r')
{
ch=(char)r.read();
if( ch=='\n')
break;
}
// System.out.print((char)c);
}
bufSize=Integer.parseInt(sb.toString().trim(), 16);
}
//throw new Error("Connection timed out");
if(readSize==0)
{
long time = System.currentTimeMillis();
while(System.currentTimeMillis()-time<100);
} else loop=0;
//if(debug)
// System.out.println(++loop+" read "+readCount+" bytes"+" readSize "+readSize);
readCount+=readSize;
}
//while(r.available()<bufSize); // critical if connection closed
// r.read(b,0,bufSize);
//System.err.println(r.available());
char ch2=(char)r.read();
// if(ch2=='\r') System.out.print("CR");
ch2=(char)r.read();
// if(ch2=='\n') System.out.println("LF");
// if(debug)
// System.out.println(new String(b,0,bufSize));
while ( (c = r.read()) != -1)
{
// ++countC;
char ch=(char)c;
//System.err.println("read lf "+(ch=='\r')+" or cr "+( ch=='\n')+" "+(Character.digit(ch, 16) == -1));
//if((ch!='\r')&&( ch!='\n')) System.err.println("read ch "+" "+ch+" "+Character.isSpace(ch));
// System.out.print(ch);
if(Character.digit(ch, 16) != -1)
localBuff.append(ch);
if(ch=='\r')
{
ch=(char)r.read();
if( ch=='\n')
break;
}
}
if(localBuff.length()>0)
{
// System.err.println("new chunk size "+Integer.parseInt(localBuff.toString().trim(), 16)+" bytes");
// System.err.println((char)r.read());
newBufSize=Integer.parseInt(localBuff.toString().trim(), 16);
//if(bufSize==3041) bufSize=3040;
if(debug)
System.out.println("chunk size "+Integer.parseInt(localBuff.toString().trim(), 16)+" "+size);
//localBuff.delete(0,localBuff.length());
localBuff=new StringBuffer();
} // else bufSize=0;
if((char)b[bufSize-1]=='\n')
{
if(debug)
System.out.println("LF found "+((char)b[bufSize-1]=='\n'));
if(newBufSize==0) bufSize=bufSize-1;
}
fout.write(b,0,bufSize);
md.update(b, 0, bufSize);
size += bufSize;
bufSize=newBufSize;
dialog.progress.setValue((int) (((double) size / (double) fileSize) * 100.0));
++countC;
if(bufSize==0)
break;
// System.err.println(" in Chunk "+bufSize+" buffers used "+(countC)+" \r\n ");
// // System.out.print((char)c);
}
// System.out.println(sb);
fout.flush();
fout.close();
file_out.close();
//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<contents.size();++i)
{
Node n=(Node)contents.elementAt(i);
if(n.type.equals("element"))
{
// System.out.println("tag "+n.name+" with contents ");
// System.out.println(n.getCharacters());
if(n.name.indexOf("artist")>=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<files.length;++j)
{
if(files[j].endsWith("xml"))
{
xmlFile=new File(f.getAbsolutePath()+"/"+files[j]);
break;
}
}
} else xmlFile=f;
try
{
// read XML Metadata from a file
FileReader r=new FileReader(xmlFile);
BufferedReader buffr = new BufferedReader(r);
StringBuffer sb=new StringBuffer();
int c=0;
while ((c = buffr.read()) != -1)
{
char ch=(char)c;
sb.append(ch);
// System.out.print((char)c);
}
addToBundle(bundle,sb);
} catch(Exception e) { e.printStackTrace();}
return bundle;
}
public static Bundle getBundle(String url)
{
Bundle bundle=new Bundle();
try
{
if(url=="") url="http://core.rz-berlin.mpg.de:80/edoctest/oi.epl?mm=list&grp=90";
// read XML document from URL into a string
URL u = new URL(url);
// FileReader r=new FileReader();
long time = System.currentTimeMillis();
//Connection con= new Connection(new URL("http://core.rz-berlin.mpg.de:80/edoctest/adm.epl"));
// String ses=con.getSessionID("name=root&passwd=edoctest&mode=verifylogin");
// con.socket.close();
Connection conTest=new Connection(new URL(url));
conTest.ses=ses;
conTest.get();
StringBuffer sb=readChunked(conTest);
// sb.delete( sb.toString().indexOf("<rt>"),sb.length());
//System.out.println(sb);
String rt =addToBundle(bundle,sb);
StringBuffer sb2=null;
int index=sb.toString().indexOf("<edoc>",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("<rt>",index);
// sb2.delete(index2,sb2.length());
//sb.append(sb2);
rt=addToBundle(bundle,sb2);
}
//sb.append("</edoc>");
//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);
conTest.socket.close();
} 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;i<contents.size();++i)
{
Node n=(Node)contents.elementAt(i);
if(n.type.equals("element"))
{
// System.out.println("tag "+n.name+" with contents ");
// System.out.println(n.getCharacters());
//contents=n.contents.v;i=0;
}
// System.out.println(n.type);
}
*/
Node tempNode=root.find(rootNode,new int[] {1});
if(tempNode==null) return rtTag;
int count=1;
/*
for(int i=0;i<tempNode.contents.v.size();++i)
{
// System.out.println(((Node)tempNode.contents.v.elementAt(i)).attributes+" "+i);
}*/
if(tempNode.contents.v.size()==0) return rtTag;
Node notNull=null;
Node rtNode=root.find(rootNode+"/rt",new int[] {1,1});
rtTag=(rtNode==null) ? "finished":rtNode.getCharacters();
System.out.println("rt tag is "+ rtTag);
int length=0;//(tempNode.contents.length()-1)/2;
for(int i=0;i<tempNode.contents.v.size();++i) {
Node node=(Node)tempNode.contents.v.elementAt(i);
if(node.type.equals("element"))
if(node.name.equals("doc"))length++;
// System.out.println(((Node)tempNode.contents.v.elementAt(i)).attributes+" "+i);
}
if(length==0) rtTag="finished";
while(count<=length)
{
//System.out.println(count+" "+length);
tempNode=root.find(rootNode+"/doc",new int[] {1,count});
if(tempNode==null)
{
rtTag="finished";
break;
}
Bundle.Document doc = new Bundle.Document();
Hashtable attributes=tempNode.attributes;
int docID=0;
int ver=0;
if(attributes != null)
{
docID=Integer.parseInt((String)attributes.get("id"));
ver=Integer.parseInt((String)attributes.get("ver"));
doc.addData("docID",(String)attributes.get("id"));
doc.addData("ver",(String)attributes.get("ver"));
}
bundle.docs.put(new Integer(docID),doc);
bundle.documents.addElement(doc);
// System.out.println("id " +docID+" ver "+ver);
Node fileNode=root.find(rootNode+"/doc/file",new int[] {1,count,1});
attributes=(fileNode==null)? null:fileNode.attributes;
int fileID=0;
int fileVer=0;
if(attributes != null)
{
fileID=Integer.parseInt((String)attributes.get("id"));
fileVer=Integer.parseInt((String)attributes.get("ver"));
doc.addData("fileID",(String)attributes.get("id"));
doc.addData("fileVer",(String)attributes.get("ver"));
// System.out.println("fileid " +fileID+" ver "+fileVer);
}
// add all Document tags to actual Document
for(int i=0;i<tempNode.contents.v.size();++i)
{
Node node=(Node)tempNode.contents.v.elementAt(i);
if(node.type.equals("element"))
{
//System.out.println(node.name+" "+node.getCharacters()+" "+node.contents.v.size());
if(node.contents.v.size()>1)
{
for(int j=0;j<node.contents.v.size();++j)
{
Node node2=(Node)node.contents.v.elementAt(j);
if(node2.type.equals("element"))
{
doc.addData(node2.name,node2.getCharacters());
//System.out.println(node2.name+" "+node2.getCharacters()+" "+node2.contents.v.size());
}
}
} else doc.addData(node.name,node.getCharacters());
}
}
bundle.docsLocator.put(doc.locator,doc);
//System.out.println( bundle.docsLocator);//doc.extraTags);
/*
notNull=root.find("edoc/doc/title",new int[]{1,count,1});
String title=(notNull!=null) ? notNull.getCharacters():"missing";
// System.out.println("edoc/doc/title "+title);
notNull=root.find("edoc/doc/comment",new int[]{1,count,1});
String comment = (notNull!=null) ? notNull.getCharacters():"missing";
// System.out.println("edoc/doc/comment "+comment);
notNull =root.find("edoc/doc/placeofevent",new int[]{1,count,1});
String placeofevent = (notNull!=null) ? notNull.getCharacters():"missing";
// System.out.println("edoc/doc/placeofevent "+placeofevent);
notNull=root.find("edoc/doc/locater",new int[]{1,count,1});
String locator=(notNull!=null) ? notNull.getCharacters():"missing";
// System.out.println("edoc/doc/locater "+locator);
notNull = root.find("edoc/doc/file/filename",new int[]{1,count,1,1});
String fileName=(notNull!=null) ? notNull.getCharacters():"missing";
// System.out.println("edoc/doc/file/filename "+fileName);
notNull = root.find("edoc/doc/file/mimetype",new int[]{1,count,1,1});
String mimeType=(notNull!=null) ? notNull.getCharacters():"missing";
// System.out.println("edoc/doc/file/mimetype "+mimeType);
notNull = root.find("edoc/doc/file/md5cs",new int[]{1,count,1,1});
String md5cs = (notNull!=null) ? notNull.getCharacters():"missing";
// System.out.println("edoc/doc/file/md5cs "+md5cs);
notNull=root.find("edoc/doc/file/size",new int[]{1,count,1,1});
int size =(notNull!=null) ? Integer.parseInt(notNull.getCharacters()):0;
// System.out.println("edoc/doc/file/size "+size);
notNull = root.find("edoc/doc/file/filelink",new int[]{1,count,1,1});
String fileLink = (notNull!=null) ? notNull.getCharacters():"missing";
// System.out.println("edoc/doc/file/filelink "+fileLink);
Bundle.Document doc=new Bundle. Document(docID,ver,title,locator,comment,placeofevent,fileID,fileVer,fileName,mimeType,md5cs,size,fileLink);
bundle.docs.put(new Integer(docID),doc);
bundle.documents.addElement(doc);
*/
count++;
}
// System.out.println(bundle.docs.size());
return rtTag;
}
public static void getGenre()
{
try
{
String url="http://core.rz-berlin.mpg.de:80/edoctest/oi.epl?mm=help&sm=genres";
// read XML document from URL into a string
URL u = new URL(url);
// FileReader r=new FileReader();
long time = System.currentTimeMillis();
// debug=true;
Connection conTest=new Connection(new URL(url));
conTest.ses=ses;
conTest.get();
StringBuffer sb=readChunked(conTest);
Bundle.genresId.clear();
Bundle.genresLabel.clear();
Bundle.genresName.clear();
Bundle.genVec.removeAllElements();
getGenreList(sb);
long timeEnd = System.currentTimeMillis();
System.err.println("time to get genre data "+(timeEnd-time));
// Enumeration en = Bundle.genres.keys();
//while(en.hasMoreElements()
if(debug)
{
Collections.sort(Bundle.genVec);
// Bundle.Genre.debug=true;
for(int i=0;i<Bundle.genVec.size();++i)
System.out.println(Bundle.genresId.get(Bundle.genVec.get(i)));
//System.out.println( Bundle.genres);
}
conTest.socket.close();
} catch(Exception e) {e.printStackTrace();}
}
public static String getGenreList(StringBuffer sb)
{
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;i<contents.size();++i)
{
Node n=(Node)contents.elementAt(i);
if(n.type.equals("element"))
{
// System.out.println("tag "+n.name+" with contents ");
// System.out.println(n.getCharacters());
//contents=n.contents.v;i=0;
}
// System.out.println(n.type);
}
*/
Node tempNode=root.find(rootNode,new int[] {1});
if(tempNode==null) return rtTag;
int count=1;
/*
for(int i=0;i<tempNode.contents.v.size();++i)
{
// System.out.println(((Node)tempNode.contents.v.elementAt(i)).attributes+" "+i);
}*/
if(tempNode.contents.v.size()==0) return rtTag;
Node notNull=null;
int length=0;
for(int i=0;i<tempNode.contents.v.size();++i) {
Node node=(Node)tempNode.contents.v.elementAt(i);
if(node.type.equals("element"))
if(node.name.equals("genre"))length++;
// System.out.println(((Node)tempNode.contents.v.elementAt(i)).attributes+" "+i);
}
if(length==0) rtTag="finished";
while(count<=length)
{
//System.out.println(count+" "+length);
tempNode=root.find(rootNode+"/genre",new int[] {1,count});
if(tempNode==null)
{
rtTag="finished";
break;
}
Bundle.Genre genre= new Bundle.Genre();
Hashtable attributes=tempNode.attributes;
Integer id=null;
if(attributes != null)
{
genre.id=new Integer((String)attributes.get("id"));
}
for(int i=0;i<tempNode.contents.v.size();++i)
{
Node node=(Node)tempNode.contents.v.elementAt(i);
if(node.type.equals("element"))
{
//System.out.println(node.name+" "+node.getCharacters()+" "+node.contents.v.size());
if(node.contents.v.size()>1)
{
for(int j=0;j<node.contents.v.size();++j)
{
Node node2=(Node)node.contents.v.elementAt(j);
if(node2.type.equals("element"))
{
//genre.addData(node2.name,node2.getCharacters());
//System.out.println(node2.name+" "+node2.getCharacters()+" "+node2.contents.v.size());
}
}
} else genre.addData(node.name,node.getCharacters());
}
}
Bundle.genresId.put(genre.id,genre);
Bundle.genresLabel.put(genre.label.toLowerCase(),genre.id);
Bundle.genresName.put(genre.name.toLowerCase(),genre.id);
Bundle.genVec.addElement(genre.id);
count++;
}
// System.out.println(bundle.docs.size());
return rtTag;
}
public static void main(String args[])
{
// System.out.println("Bundle for http://core.rz-berlin.mpg.de/edocmike/oi.epl?mm=list&grp=90");
// getBundle("");
// System.out.println("Bundle for http://core.rz-berlin.mpg.de/edocmike/oi.epl?mm=list&grp=89");
// getBundle("http://core.rz-berlin.mpg.de/edoctest/oi.epl?mm=list&grp=136");
// getBundle("http://core.rz-berlin.mpg.de/edoctest/oi.epl?mm=list&grp=136");
runningAsApplet=false;
//Object[] options = new Object[] { "Cancel","Replace existing files", "Keep existing files" };
// int option = showDialog("Warning : several files exist on the server \n Press show list to see the list! \n What do you want to do?", "Files exist!", options,null);
//System.out.println("User selected "+options[option]);
// System.exit(0); // due to now internet
long time = System.currentTimeMillis();
new Upload();
// bundle=getBundle("http://core.rz-berlin.mpg.de/edoctest/oi.epl?mm=list&grp=136&col=11");
url="http://core.rz-berlin.mpg.de/edoctest/";
bundleRef = getBundle(url+"oi.epl?mm=list&grp="+grp+"&col="+col);
getGenre();
//getMetaData("");
// if(true)System.exit(0);
/* Hashtable data=getMetaData("");
//if(true)System.exit(0);
try
{
// old edocmike
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");
//("name=dwinter&passwd=e1nste1n&mode=verifylogin");
con.socket.close();
Vector artist=(Vector)data.get("artist");
Vector picture=(Vector)data.get("picture");
Vector idnr=(Vector)data.get("IDNR");
Vector source=(Vector)data.get("source");
Connection conPost=new Connection( new URL("http://core.rz-berlin.mpg.de:80/edoctest/oi.epl"));
// new URL("http://localhost:8080/examples/servlet/rogo/"));
conPost.ses=ses;
for(int i=135;i<136;++i)//i<artist.size();++i)
{
// System.out.println("artist "+artist.elementAt(i));
// System.out.println("picture nr "+ i +" name "+picture.elementAt(i));
// System.out.println("file" +idnr.elementAt(i));
// System.out.println("source"+source.elementAt(i));
// if(!new File("/Volumes/data/tmp/Bildarchiv-Daston/400/"+idnr.elementAt(i)).exists())
// System.out.println("file "+idnr.elementAt(i) +" does not exist");
// if(idnr.elementAt(i)!=null)
// if(idnr.elementAt(i).toString().indexOf("Ÿ")>=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();
MessageDigest md ;
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;
try
{
md = MessageDigest.getInstance("MD5");
} catch(Exception e) { e.printStackTrace();}
}
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);
// updating document with edoc Data
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;
doc.modified = new File(filePath).lastModified();
}
public void sendFile(FileInputStream f,PrintStream out) throws Exception
{
int length=f.available();
md.reset();
dialog.progress.setValue(0);
byte[] b=new byte[16204];
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 get with "+url );
// 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";
}
}
}
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>