Annotation of upload-applet/ServerWriter.java, revision 1.1
1.1 ! rogo 1: /**
! 2: * @author rogo
! 3: *
! 4: * To change this generated comment edit the template variable "typecomment":
! 5: * Window>Preferences>Java>Templates.
! 6: * To enable and disable the creation of type comments go to
! 7: * Window>Preferences>Java>Code Generation.
! 8: */
! 9: import java.net.*;
! 10: import java.io.*;
! 11: import javax.swing.*;
! 12: import java.awt.*;
! 13: import java.awt.event.*;
! 14: import java.security.*;
! 15: import java.util.*;
! 16:
! 17: public class ServerWriter extends JApplet implements ActionListener
! 18: {
! 19: static File startFile;
! 20: static int count = 0;
! 21: static JFileChooser fileChooser;
! 22: static String url;
! 23: static long id = 0L;
! 24: static Hashtable connections=new Hashtable();
! 25: JButton upload = new JButton("Upload");
! 26: JRadioButton proxy = new JRadioButton("Proxy");
! 27: static int fileCount = 0;
! 28: // static UploadInfo dialog = new UploadInfo();
! 29: static String mode = "";
! 30: static String destination = "";
! 31: static String prefix = "";
! 32: static String proxyHost = "";
! 33: ServerWriter uploader = this;
! 34: static final int md5Length = 32;
! 35: public ServerWriter()
! 36: {
! 37:
! 38: }
! 39: public void init()
! 40: {
! 41: this.getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
! 42: this.getContentPane().add(upload);
! 43: this.getContentPane().add(proxy);
! 44: upload.addActionListener(this);
! 45: proxy.addActionListener(this);
! 46: id = System.currentTimeMillis();
! 47: Properties prop = System.getProperties();
! 48:
! 49: // prop.put("proxySet", "true");
! 50: // prop.put("http.proxyHost","proxy.unibe.ch");
! 51: // prop.put("proxyPort", "80");
! 52: prop.put("java.protocol.handler.pkgs","HTTPClient");
! 53: fileChooser=null;
! 54: }
! 55: public void actionPerformed(ActionEvent e)
! 56: {
! 57: if (e.getSource().equals(proxy))
! 58: {
! 59: proxyHost = JOptionPane.showInputDialog("Please enter proxy host (format host:port)!");
! 60: if (proxyHost != null)
! 61: {
! 62: if (proxyHost.length() == 0)
! 63: {
! 64: proxyHost = "";
! 65: Properties prop = System.getProperties();
! 66: prop.remove("proxySet");
! 67: prop.remove("http.proxyHost");
! 68: prop.remove("proxyPort");
! 69:
! 70: }
! 71: else
! 72: {
! 73: Properties prop = System.getProperties();
! 74: int index = proxyHost.indexOf(":");
! 75: String pHost = proxyHost.substring(0, (index > 0) ? index : proxyHost.length());
! 76: String pPort = (index > 0) ? proxyHost.substring(index + 1) : "80";
! 77:
! 78: prop.put("proxySet", "true");
! 79: prop.put("http.proxyHost",pHost);
! 80: prop.put("proxyPort", pPort);
! 81: }
! 82: System.out.println("pr "); // proxyHost="";
! 83: } else
! 84: proxyHost = "";
! 85: // proxy.setSelected(false);
! 86: }
! 87:
! 88: if (e.getActionCommand().equals("Upload"))
! 89: {
! 90:
! 91: //url=getParameter("url");
! 92: set();
! 93: System.out.println("url " + url); //+ " " + getCodeBase());
! 94: upload.setEnabled(false);
! 95: main(new String[] { "", "" });
! 96: upload.setEnabled(true);
! 97: }
! 98:
! 99: }
! 100: public void set()
! 101: {
! 102: String url = getParameter("baseURL");
! 103: String servletURL = getParameter("servletURL");
! 104: destination = "";
! 105: destination = getParameter("destinationFolder");
! 106: if (destination == null)
! 107: destination = "";
! 108: prefix = getParameter("prefix");
! 109: if(prefix == null) prefix="";
! 110: // e.g."/rogo/servlet/RWServlet/"
! 111: URL appletUrl = getCodeBase();
! 112: String serverInfo = appletUrl.getHost() + ":" + appletUrl.getPort();
! 113: this.url = (servletURL.startsWith("http")) ? servletURL + url : "http://" + serverInfo + servletURL + url;
! 114: Properties prop = System.getProperties();
! 115:
! 116: // prop.put("proxySet", "true");
! 117: // prop.put("http.proxyHost","proxy.unibe.ch");
! 118: // prop.put("proxyPort", "80");
! 119: // prop.put("java.protocol.handler.pkgs","HTTPClient");
! 120: Enumeration en = prop.propertyNames();
! 121: while (en.hasMoreElements())
! 122: {
! 123: String key = (String) en.nextElement();
! 124: System.out.println("key: " + key + " value: " + prop.get(key));
! 125:
! 126: }
! 127: }
! 128: public static void main(String[] args)
! 129: {
! 130: try
! 131: {
! 132: if (fileChooser == null)
! 133: {
! 134: fileChooser = new JFileChooser();
! 135: fileChooser.setFileSelectionMode(fileChooser.FILES_AND_DIRECTORIES);
! 136: fileChooser.setFileFilter(new ImageFilter());
! 137: fileChooser.setDialogType(JFileChooser.OPEN_DIALOG);
! 138: fileChooser.addActionListener(new ActionListener()
! 139: {
! 140: public void actionPerformed(ActionEvent e2)
! 141: {
! 142: System.out.println(e2.getActionCommand());
! 143: if (!e2.getActionCommand().toString().equals("ApproveSelection")) return;
! 144: // {
! 145:
! 146: Thread thread = new Thread()
! 147: {
! 148: public void run()
! 149: {
! 150: UploadInfo dialog = new UploadInfo();
! 151:
! 152: File startFile=new File(".");
! 153: try
! 154: {
! 155: dialog.thread=Thread.currentThread();
! 156: System.out.println("Called run with " + fileChooser.getSelectedFile());
! 157: dialog.setTitle("Upload Status Window");
! 158: dialog.setSize(400, 175);
! 159: dialog.setLocation(new java.awt.Point(150, 100));
! 160: Update update = new Update();
! 161: startFile = fileChooser.getSelectedFile(); //new File(fileChooser.getFile());
! 162: int fileCount = 0;
! 163: int count = 1;
! 164: System.out.println(startFile);
! 165: long time = System.currentTimeMillis();
! 166: if( connections.get(startFile.getAbsolutePath())!=null)
! 167: throw new Error("Upload already running!");
! 168:
! 169: if (startFile.isDirectory())
! 170: {
! 171: fileCount = countFiles(startFile, fileCount);
! 172: System.out.println("Number of files to send: " + fileCount);
! 173: writeDirectory(startFile, startFile, fileCount, count, dialog, update);
! 174:
! 175: } else
! 176: {
! 177: fileCount = 1;
! 178: writeStream(startFile, startFile, fileCount, count, dialog, update);
! 179:
! 180: }
! 181:
! 182: long time2 = System.currentTimeMillis();
! 183: System.out.println("needed " + (time2 - time) + " ms for " + count--);
! 184: dialog.setVisible(false);
! 185: if (startFile.isDirectory())
! 186: showDialog("Finished upload of " + startFile + " containing " + fileCount + " " + ((fileCount > 1) ? " files" : " file") + " in " + ((time2 - time) / 1000) + " seconds", "Finished Upload");
! 187: else
! 188: showDialog("Finished upload of " + startFile + " in " + ((time2 - time) / 1000) + " seconds", "Finished Upload");
! 189: connections.remove(startFile.getAbsolutePath());
! 190: } catch (Exception e)
! 191: {
! 192: dialog.setVisible(false);
! 193: ByteArrayOutputStream b = new ByteArrayOutputStream();
! 194: PrintStream stream = new PrintStream(b);
! 195: e.printStackTrace(stream);
! 196: connections.remove(startFile.getAbsolutePath());
! 197:
! 198: showErrorDialog(b.toString(), "Upload failed");
! 199: } catch (Error e8)
! 200: {
! 201: dialog.setVisible(false);
! 202: if( e8.toString().indexOf("Upload already running!")<0)
! 203: connections.remove(startFile.getAbsolutePath());
! 204: showErrorDialog(e8.toString(), "Upload failed");
! 205: }
! 206:
! 207: }
! 208: };
! 209: thread.setPriority(Thread.MIN_PRIORITY);
! 210: /* dialog = new UploadInfo();
! 211: dialog.setTitle("Upload Status Window");
! 212: dialog.setSize(400, 155);
! 213: dialog.setLocation(new java.awt.Point(150, 100));
! 214: */
! 215: thread.start();
! 216: // } // else
! 217: //System.exit(0);
! 218: }
! 219: });
! 220:
! 221: }
! 222: count = 1;
! 223: mode = "";
! 224: fileChooser.showDialog(new JFrame(), "Upload");
! 225:
! 226: } catch (Exception e)
! 227: {
! 228: System.out.println("Ha " + e);
! 229: }
! 230: }
! 231: private static int countFiles(File file, int fileCount)
! 232: {
! 233: if (file.isDirectory())
! 234: {
! 235: String[] files = file.list(new DotFileFilter());
! 236:
! 237: for (int j = 0; j < files.length; ++j)
! 238: {
! 239: File f = new File(file + File.separator + files[j]);
! 240: if (f.isDirectory())
! 241: {
! 242: fileCount = countFiles(f, fileCount);
! 243: } else
! 244: fileCount = fileCount + 1;
! 245: }
! 246:
! 247: }
! 248: return fileCount;
! 249: }
! 250:
! 251: private static int writeDirectory(File file, File startFile, int fileCount, int count, UploadInfo dialog, Update update) throws Exception
! 252: {
! 253: if (file.isDirectory())
! 254: {
! 255: String[] files = file.list(new DotFileFilter());
! 256: for (int j = 0; j < files.length; ++j)
! 257: {
! 258: File f = new File(file + File.separator + files[j]);
! 259: System.out.println(file + " " + f);
! 260:
! 261: if (f.isDirectory())
! 262: {
! 263: count = writeDirectory(f, startFile, fileCount, count, dialog, update);
! 264: } else
! 265: count = writeStream(f, startFile, fileCount, count, dialog, update);
! 266: }
! 267: }
! 268: return count;
! 269: }
! 270: private static int writeStream(File file, File startFile, int fileCount, int count, UploadInfo dialog, Update updateMode) throws Exception
! 271: {
! 272: // try
! 273: //{
! 274: String response = "", mode = "";
! 275: String path = file.getAbsolutePath();
! 276: String fName=file.getName();
! 277: String name = (startFile.isDirectory()) ? startFile.getName() + "/" + path.substring(startFile.getAbsolutePath().length() + 1) : file.getName();
! 278: if (destination != "")
! 279: name = (startFile.isDirectory()) ? destination + "/" + path.substring(startFile.getAbsolutePath().length() + 1) : destination + "/" + file.getName();
! 280: if(prefix!="")
! 281: name=name.substring(0,name.indexOf(fName))+prefix+fName;
! 282: System.out.println("hi " + name + " " + destination + " " + startFile.getName()+" "+file.getName());
! 283: FileInputStream f = new FileInputStream(file.getAbsolutePath());
! 284: int length = f.available();
! 285: int readCount = 0;
! 286: byte[] b = (length > 8192) ? new byte[8192] : new byte[length];
! 287: System.out.println(file.getName() + " " + ((length > 1000000) ? (length / 1048576) + " MByte" : length + " Bytes"));
! 288: String fileName = file.getName() + " " + ((length > 1000000) ? (length / 1048576) + " MByte" : length + " Bytes");
! 289: dialog.title.setText("Uploading " + startFile);
! 290: dialog.progress.setValue(0);
! 291: dialog.file.setText(fileName);
! 292: dialog.status.setText(count + " of " + fileCount + ((fileCount > 1) ? " files" : " file"));
! 293: //dialog.pack();
! 294: //dialog.validate();
! 295: if (dialog.isVisible())
! 296: dialog.repaint();
! 297: else
! 298: dialog.show();
! 299:
! 300: MessageDigest md = MessageDigest.getInstance("MD5");
! 301: FileInputStream f2 = new FileInputStream(file.getAbsolutePath());
! 302:
! 303: readCount = 0;
! 304:
! 305: /* while (readCount < length)
! 306: {
! 307: if (readCount + b.length > length)
! 308: f2.read(b, 0, length - readCount); // mout.flush();
! 309: else
! 310: f2.read(b);
! 311: if (readCount + b.length > length)
! 312: md.update(b, 0, length - readCount);
! 313: else
! 314: md.update(b, 0, b.length);
! 315: readCount += b.length;
! 316: //System.out.println("rock " + readCount);
! 317:
! 318: }*/
! 319: String md5 = "rogo";getMD5(md);
! 320: //md = null;
! 321: f2.close();
! 322: f2 = null;
! 323: if (updateMode.value)
! 324: mode = "replace";
! 325:
! 326: URL httpCon = new URL(url + getSHA((id + "").getBytes()) + "/" + convertUml(name) + "?id=" + getSHA((id + "").getBytes()) + "&count=" + count + "&nf=" + fileCount + "&md5=" + md5 + "&mode=" + mode);
! 327: /* HttpURLConnection con = (HttpURLConnection) httpCon.openConnection();
! 328: con.setUseCaches(false);
! 329: con.setRequestMethod("PUT");
! 330: con.setDoOutput(true);
! 331: con.connect();
! 332: */
! 333: Connection con = null;
! 334: con=(Connection)connections.get(startFile.getAbsolutePath());
! 335: if(con==null)
! 336: {
! 337: con=new Connection(httpCon);
! 338: connections.put(startFile.getAbsolutePath(),con);
! 339: } else
! 340: con.url=httpCon;
! 341: OutputStream mout = con.out; //con.getOutputStream();
! 342: // InputStream min= con.getInputStream();
! 343: // System.out.println(httpCon);
! 344: //System.out.println("Response " + con.getResponseMessage()+" "+con.getContentLength());
! 345: boolean update = con.update();
! 346:
! 347: System.out.println("update is" + update);
! 348:
! 349: if (!update || mode.equals("update") || mode.equals("replace"))
! 350: //if(true)
! 351: {
! 352: con.connect(length+md5Length);
! 353: mout = con.out;
! 354: md.reset();
! 355: /* System.out.println("Free memory "+(Runtime.getRuntime().freeMemory()/ 1048576));
! 356: System.gc();
! 357: System.out.println("Free memory after"+(Runtime.getRuntime().freeMemory()/ 1048576));
! 358: System.out.println("Total memory after"+(Runtime.getRuntime().totalMemory()/ 1048576));
! 359: */
! 360: readCount = 0;
! 361: while (readCount < length)
! 362: {
! 363: if (readCount + b.length > length)
! 364: f.read(b, 0, length - readCount); // mout.flush();
! 365: else
! 366: f.read(b);
! 367: if (readCount + b.length > length)
! 368: mout.write(b, 0, length - readCount); // mout.flush();
! 369: else
! 370: mout.write(b);
! 371: if (readCount + b.length > length)
! 372: md.update(b, 0, length - readCount);
! 373: else
! 374: md.update(b, 0, b.length);
! 375: readCount += b.length;
! 376: if(((PrintStream)mout).checkError()) throw new Error("Error while sending data");
! 377: dialog.progress.setValue((int) (((double) readCount / (double) length) * 100.0));
! 378:
! 379: if (readCount % 1000 == 0)
! 380: {
! 381: //System.out.println("Free memory while writing"+(Runtime.getRuntime().freeMemory()/ 1048576));
! 382: //System.out.println("Total memory while writing"+(Runtime.getRuntime().totalMemory()/ 1048576));
! 383: //mout.flush();
! 384: //System.out.println(con.getClass());
! 385: }
! 386: }
! 387: md5 = getMD5(md);
! 388: mout.write(md5.getBytes());
! 389: if(((PrintStream)mout).checkError()) throw new Error("Error while sending md5");
! 390: //mout.close();
! 391: //con.disconnect();
! 392:
! 393: response = con.getResponseMessage();
! 394: System.out.println(response + " " + md5);
! 395: }
! 396: if (update && !mode.equals("update") && !mode.equals("replace"))
! 397: //(response.indexOf("Conflict")>=0 && !mode.equals("update") && !mode.equals("replace"))
! 398: {
! 399: updateMode.value = true;
! 400: Object[] options = new Object[] { "Replace folder", "Cancel" }, options2 = new Object[] { "Replace file", "Cancel" };
! 401:
! 402: int option = showDialog("Warning destination exists! \n What do you want to do?", "Destination exists!", (startFile.isDirectory()) ? options : options2);
! 403: if (option == 0)
! 404: mode = "replace";
! 405: //System.out.println("performing update");
! 406: else
! 407: throw new Error("Upload cancelled");
! 408: //mode = "cancel"; //System.out.println("performing replace");
! 409: //System.out.println("Watershine" + name + " " + mode);
! 410: // mout.close();
! 411:
! 412: con.disconnect();
! 413: httpCon = new URL(url + getSHA((id + "").getBytes()) + "/" + convertUml(name) + "?id=" + getSHA((id + "").getBytes()) + "&count=" + count + "&nf=" + fileCount + "&md5=" + md5 + "&mode=" + mode);
! 414: /* con = (HttpURLConnection) httpCon.openConnection();
! 415: con.setRequestMethod("PUT");
! 416: con.setDoOutput(true);
! 417: con.connect();
! 418: mout = con.getOutputStream();
! 419: */
! 420: //con=new Connection(httpCon);
! 421: con.url = httpCon;
! 422: con.connect(length+md5Length);
! 423: md.reset();
! 424: mout = con.out;
! 425: readCount = 0;
! 426: FileInputStream f3 = new FileInputStream(file.getAbsolutePath());
! 427: while (readCount < length)
! 428: {
! 429: if (readCount + b.length > length)
! 430: f3.read(b, 0, length - readCount); // mout.flush();
! 431: else
! 432: f3.read(b);
! 433: if (readCount + b.length > length)
! 434: mout.write(b, 0, length - readCount); // mout.flush();
! 435: else
! 436: mout.write(b);
! 437: if (readCount + b.length > length)
! 438: md.update(b, 0, length - readCount);
! 439: else
! 440: md.update(b, 0, b.length);
! 441: readCount += b.length;
! 442:
! 443: if(((PrintStream)mout).checkError()) throw new Error("Error while sending data");
! 444:
! 445: dialog.progress.setValue((int) (((double) readCount / (double) length) * 100.0));
! 446:
! 447: }
! 448: f3.close();
! 449: // mout.flush();
! 450: md5=getMD5(md);
! 451: mout.write(md5.getBytes()); // write md5 code
! 452: if(((PrintStream)mout).checkError()) throw new Error("Error while sending md5");
! 453:
! 454: response = con.getResponseMessage();
! 455: System.out.println("MyResponse " + response);
! 456:
! 457: } else if (response.indexOf(" Checksums not identical!") >= 0)
! 458: {
! 459: Object[] options = { "Retry", "Cancel" };
! 460:
! 461: int option = showDialog("Transfer failed! \n What do you want to do?", "Transfer failed ", options);
! 462: con.disconnect();
! 463: if (option == 0)
! 464: {
! 465: httpCon = new URL(url + getSHA((id + "").getBytes()) + "/" + convertUml(name) + "?id=" + getSHA((id + "").getBytes()) + "&count=" + count + "&nf=" + fileCount + "&md5=" + md5 + "&mode=" + mode);
! 466: /* con = (HttpURLConnection) httpCon.openConnection(); // con.setRequestMethod("GET");
! 467: con.setRequestMethod("PUT");
! 468: con.setDoOutput(true);
! 469: con.connect();
! 470: mout = con.getOutputStream();
! 471: // System.out.println(htt readCount = 0;
! 472: */
! 473:
! 474: con.url = httpCon;
! 475: con.connect(length+md5Length);
! 476: mout = con.out;
! 477: FileInputStream f4 = new FileInputStream(file.getAbsolutePath());
! 478: readCount = 0;
! 479: while (readCount < length)
! 480: {
! 481: if (readCount + b.length > length)
! 482: f4.read(b, 0, length - readCount); // mout.flush();
! 483: else
! 484: f4.read(b);
! 485: if (readCount + b.length > length)
! 486: mout.write(b, 0, length - readCount); // mout.flush();
! 487: else
! 488: mout.write(b);
! 489: readCount += b.length;
! 490: if(((PrintStream)mout).checkError()) throw new Error("Error while sending data");
! 491: dialog.progress.setValue((int) (((double) readCount / (double) length) * 100.0));
! 492:
! 493: }
! 494: f4.close();
! 495: mout.write(md5.getBytes());
! 496: if(((PrintStream)mout).checkError()) throw new Error("Error while sending md5");
! 497: response = con.getResponseMessage();
! 498: System.out.println("MyResponse " + response);
! 499: }
! 500:
! 501: } else
! 502: con.disconnect();
! 503:
! 504: f.close();
! 505: count++;
! 506: /*
! 507: } catch (IOException ioe)
! 508: {
! 509: System.out.println("IOException: " + ioe);
! 510: ioe.printStackTrace();
! 511: throw(ioe);
! 512: } catch (Exception e)
! 513: {
! 514: System.out.println("IOException: " + e);
! 515: showErrorDialog(e.toString(),"Upload failed");
! 516: }*/
! 517: return count;
! 518: }
! 519: public static String getMD5(MessageDigest md) throws Exception
! 520: {
! 521: byte[] bytes = md.digest();
! 522: System.out.println("digest " + bytes.length);
! 523: StringBuffer sb = new StringBuffer();
! 524: int decValue;
! 525: for (int i = 0; i < bytes.length; i++)
! 526: {
! 527: String hexVal = Integer.toHexString(bytes[i] & 0xFF);
! 528: if (hexVal.length() == 1)
! 529: hexVal = "0" + hexVal; // put a leading zero
! 530: sb.append(hexVal);
! 531: }
! 532: return sb.toString();
! 533: }
! 534: public static String getSHA(byte[] b) throws Exception
! 535: {
! 536: MessageDigest md = MessageDigest.getInstance("SHA");
! 537: md.update(b, 0, b.length);
! 538: //System.out.println("digest");
! 539: byte[] bytes = md.digest();
! 540: StringBuffer sb = new StringBuffer();
! 541: int decValue;
! 542: for (int i = 0; i < bytes.length; i++)
! 543: {
! 544: String hexVal = Integer.toHexString(bytes[i] & 0xFF);
! 545: if (hexVal.length() == 1)
! 546: hexVal = "0" + hexVal; // put a leading zero
! 547: sb.append(hexVal);
! 548: }
! 549:
! 550: return sb.toString();
! 551: }
! 552:
! 553: public static String convertUml(String newName)
! 554: {
! 555: StringBuffer alterMe = new StringBuffer(newName.trim());
! 556: int length = alterMe.length();
! 557: int j = 0;
! 558: while (j < length)
! 559: { //if(Character.isSpaceChar(alterMe.charAt(j)))
! 560: // alterMe.setCharAt(j,'_');
! 561: if (alterMe.charAt(j) == ' ')
! 562: {
! 563: alterMe.setCharAt(j, '%');
! 564: alterMe.insert(j + 1, "20");
! 565: length = length + 2;
! 566: }
! 567: /* if(Character.isSpaceChar(alterMe.charAt(j))
! 568: alterMe.setCharAt(j,'_');
! 569: */
! 570: ++j;
! 571: }
! 572: return alterMe.toString();
! 573: }
! 574: public static class UploadInfo extends JDialog
! 575: {
! 576: JLabel file = new JLabel(" ");
! 577: JLabel status = new JLabel(" ");
! 578: JLabel title = new JLabel(" ");
! 579: JPanel content = new JPanel();
! 580: JPanel titlePanel = new JPanel();
! 581: JPanel labelPanel = new JPanel();
! 582: JPanel statusPanel = new JPanel();
! 583: JPanel buttonPanel = new JPanel();
! 584:
! 585: JProgressBar progress = new JProgressBar();
! 586: JButton cancel = new JButton("Cancel");
! 587: Thread thread;
! 588: public UploadInfo()
! 589: {
! 590:
! 591: content = new JPanel(true);
! 592: //content.setBorder(BorderFactory.createRaisedBevelBorder());
! 593: content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
! 594: title = new JLabel("Uploading " + startFile);
! 595: title.setHorizontalTextPosition(title.CENTER);
! 596: titlePanel.add(title);
! 597: buttonPanel.add(cancel);
! 598: // content.add(titlePanel);
! 599: content.add(Box.createRigidArea(new java.awt.Dimension(0, 15)));
! 600: content.add(labelPanel);
! 601: content.add(new JLabel(""));
! 602: content.add(progress);
! 603: content.add(statusPanel);
! 604: content.add(buttonPanel);
! 605: content.add(Box.createRigidArea(new java.awt.Dimension(0, 15)));
! 606: labelPanel.add(file);
! 607: statusPanel.add(status);
! 608: getContentPane().add(titlePanel, "North");
! 609: getContentPane().add(content, "Center");
! 610: cancel.addActionListener(new ActionListener()
! 611: {
! 612: public void actionPerformed(ActionEvent e)
! 613: {
! 614: setVisible(false);
! 615: thread.stop();
! 616: }
! 617:
! 618: });
! 619: pack();
! 620:
! 621: }
! 622:
! 623: }
! 624: public static int showDialog(String message, String title, Object[] options)
! 625: {
! 626: int option = JOptionPane.showOptionDialog(null, message, title, JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]);
! 627: return option;
! 628:
! 629: }
! 630: public static void showDialog(String message, String title)
! 631: {
! 632: JOptionPane.showMessageDialog(null, message, title, JOptionPane.PLAIN_MESSAGE);
! 633:
! 634: }
! 635: public static void showErrorDialog(String message, String title)
! 636: {
! 637: JOptionPane.showMessageDialog(null, message, title, JOptionPane.ERROR_MESSAGE);
! 638: }
! 639:
! 640: static class Connection
! 641: {
! 642: Socket socket;
! 643: URL url;
! 644: BufferedInputStream in;
! 645: PrintStream out;
! 646: String pHost;
! 647: int pPort = 80;
! 648: HttpURLConnection con;
! 649: public Connection(URL url) throws Exception
! 650: {
! 651: Properties prop = System.getProperties();
! 652: pHost = null;
! 653: pPort = 80;
! 654: Enumeration en = prop.propertyNames();
! 655: while (en.hasMoreElements())
! 656: {
! 657: String key = (String) en.nextElement();
! 658: if (key.toString().indexOf("proxyHost") >= 0)
! 659: {
! 660: Object host = prop.get(key);
! 661: if (host != null)
! 662: pHost = host.toString();
! 663: }
! 664: if (key.toString().indexOf("proxyPort") >= 0)
! 665: {
! 666: Object port = prop.get(key);
! 667: if (port != null)
! 668: pPort = Integer.parseInt(port.toString());
! 669: }
! 670: }
! 671:
! 672: if (proxyHost != "")
! 673: {
! 674: int index = proxyHost.indexOf(":");
! 675: pHost = proxyHost.substring(0, (index > 0) ? index : proxyHost.length());
! 676: pPort = (index > 0) ? Integer.parseInt(proxyHost.substring(index + 1)) : 80;
! 677: }
! 678: socket = new Socket((pHost != null) ? pHost : url.getHost(), (pHost != null) ? pPort : url.getPort());
! 679: socket.setTcpNoDelay(true);
! 680: in = new BufferedInputStream(socket.getInputStream());
! 681: out = new PrintStream(new BufferedOutputStream(socket.getOutputStream()),true);
! 682: this.url = url;
! 683: }
! 684: public void connect(int size) throws Exception
! 685: {
! 686: try
! 687: {
! 688: //System.out.println("Yeah i called connect");
! 689: // socket = new Socket((pHost != null) ? pHost : url.getHost(), (pHost != null) ? pPort : url.getPort());
! 690: // socket.setTcpNoDelay(true);
! 691: in = new BufferedInputStream(socket.getInputStream());
! 692: out = new PrintStream(new BufferedOutputStream(socket.getOutputStream()),true);
! 693:
! 694: 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());
! 695: out.flush();
! 696: } catch (Exception e)
! 697: {
! 698: socket = new Socket((pHost != null) ? pHost : url.getHost(), (pHost != null) ? pPort : url.getPort());
! 699: socket.setTcpNoDelay(true);
! 700: in = new BufferedInputStream(socket.getInputStream());
! 701: out = new PrintStream(new BufferedOutputStream(socket.getOutputStream()),true);
! 702: 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());
! 703: out.flush();
! 704: //System.out.println("Error "+e);
! 705: }
! 706:
! 707: /* con = new HTTPClient.HttpURLConnection(url);//(HttpURLConnection) url.openConnection(); // con.setRequestMethod("GET");
! 708: con.setUseCaches(false);
! 709: con.setRequestMethod("PUT");
! 710: con.setDoOutput(true);
! 711: con.setRequestProperty("Content-Length", ""+size);
! 712: // con.connect();
! 713: System.out.println(con.getClass());
! 714: out = new DataOutputStream(con.getOutputStream());
! 715: */
! 716: }
! 717: public boolean update() throws Exception
! 718: {
! 719: boolean exists = false;
! 720: String response = "";
! 721: try
! 722: {
! 723:
! 724: URL url = createURL(this.url.toString(), getSHA((id + "").getBytes()));
! 725: // con = new HTTPClient.HttpURLConnection(url);//(HttpURLConnection) url.openConnection(); // con.setRequestMethod("GET");
! 726: //con.setUseCaches(false);
! 727: // con.setRequestMethod("HEAD");
! 728: //con.setDoOutput(true);
! 729: // con.connect();
! 730:
! 731: out.print(("HEAD " + url + " HTTP/1.1\r\nConnection:Keep-Alive\r\nHost: " + url.getHost() + "\r\n\r\n"));//.getBytes());
! 732: out.flush();
! 733: response = getResponseMessage();
! 734: exists = response.indexOf("OK") >= 0;
! 735: System.out.println("Head "+response);
! 736:
! 737: } catch (Exception e)
! 738: {
! 739: socket = new Socket((pHost != null) ? pHost : url.getHost(), (pHost != null) ? pPort : url.getPort());
! 740: socket.setTcpNoDelay(true);
! 741: in = new BufferedInputStream(socket.getInputStream());
! 742: out = new PrintStream(new BufferedOutputStream(socket.getOutputStream()),true);
! 743: // out.writeBytes("HEAD " + url + " HTTP/1.1\r\n Connection:Keep-Alive\r\n \r\nHost: " + url.getHost() + //"\r\n\r\n");
! 744: URL url = createURL(this.url.toString(), getSHA((id + "").getBytes()));
! 745: out.print(("HEAD " + url + " HTTP/1.1\r\nConnection:Keep-Alive\r\nHost: " + url.getHost() + "\r\n\r\n"));//.getBytes());
! 746: out.flush();
! 747: response = getResponseMessage();
! 748: System.out.println("Head " + response);
! 749: exists = response.indexOf("OK") >= 0;
! 750: //System.out.println("Error from Head "+e);
! 751: // e.printStackTrace();
! 752: return exists;
! 753:
! 754: }
! 755: return exists;
! 756: }
! 757: public URL createURL(String fileToPut, String id) throws Exception
! 758: {
! 759: int index = fileToPut.lastIndexOf("/") + 1;
! 760: String source = fileToPut; //.substring(0, index);
! 761: // String fileName = fileToPut.substring(index);
! 762: index = source.indexOf(id);
! 763: int index2 = index + id.length() + 1;
! 764: int index3 = source.indexOf("/", index2);
! 765:
! 766: // System.out.println(fileToPut+" "+index2+" "+index3+" "+id);
! 767: // String dest = (index3 > 0) ? source.substring(0, index) + source.substring(index2, index3)+"/"+fileName : //source.substring(0, index) + fileName;
! 768: // file check versus folder Check if destination !="" folder check is performed
! 769: String dest = (destination!="") ? source.substring(0, index) + source.substring(index2,index3):source.substring(0, index) + source.substring(index2);
! 770: // System.out.println("dest "+source.substring(0,index)+ source.substring(index2));
! 771: // File f = new File(dest);
! 772: //return f.exists();
! 773: return new URL(dest);
! 774: }
! 775:
! 776: public String getResponseMessage()
! 777: {
! 778:
! 779: int b=0;StringBuffer between=new StringBuffer();int count=0;
! 780: // while(!between.toString().equals("\r"))
! 781: // {
! 782: // between=new StringBuffer(40);
! 783: try
! 784: {
! 785: int loop =0;int available = 0;
! 786: while((available=in.available())==0)
! 787: {
! 788: if(loop>=600) throw new Error("Connection timed out");
! 789: long time = System.currentTimeMillis();
! 790: while(System.currentTimeMillis()-time<100);
! 791: if(available==0) loop++; else loop=0;
! 792: }
! 793: // System.out.println("heh there are bytes "+in.available()+" available");
! 794: int limit = in.available();
! 795: while(true )
! 796: {
! 797: //Thread.sleep(10);
! 798: b=in.read();
! 799: /*
! 800: if(b=='\r')
! 801: {
! 802: b=in.read();
! 803: b=in.read();
! 804: count=count+2;
! 805: // if(b=='\r') break;
! 806: //else
! 807: between.append('\n');
! 808: }
! 809: */
! 810: between.append((char)b);
! 811: // System.out.print((char)b);
! 812: //System.out.println("read "+count+" bytes");
! 813: count++;
! 814: if(count==limit) break;
! 815: }
! 816:
! 817: } catch(Exception e) {
! 818: e.printStackTrace();
! 819: System.out.println("Error found");
! 820: if(e.toString().indexOf("peer")>=0)
! 821: return getResponseMessage();
! 822: else throw new Error("Error while reading response");
! 823: }
! 824: String response = between.toString();
! 825: if(response.indexOf("close") >= 0)
! 826: reconnect();
! 827: return response;
! 828:
! 829: /*
! 830: String response = "";
! 831: try
! 832: {
! 833: response = con.getResponseMessage();
! 834: // con.disconnect();
! 835: } catch (Exception e)
! 836: {
! 837: showErrorDialog(e.getMessage(),"Shit happens");
! 838: }
! 839: */
! 840:
! 841: // return response;
! 842: }
! 843: public void disconnect() throws Exception
! 844: {
! 845: //socket.close();
! 846: // con.disconnect();
! 847: }
! 848: public void reconnect()
! 849: {
! 850: try
! 851: {
! 852: System.out.println("Reconnect called");
! 853: int port= (url.getPort()>=0) ? url.getPort():80;
! 854: socket = new Socket((pHost != null) ? pHost : url.getHost(), (pHost != null) ? pPort :port);
! 855: socket.setTcpNoDelay(true);
! 856: in = new BufferedInputStream(socket.getInputStream());
! 857: out = new PrintStream(new BufferedOutputStream(socket.getOutputStream()),true);
! 858:
! 859: } catch(Exception e)
! 860: {
! 861: showErrorDialog(e.getMessage(),"Shit happens");
! 862: }
! 863:
! 864: }
! 865: }
! 866: static class Update
! 867: {
! 868: boolean value = false;
! 869: public Update()
! 870: {
! 871: value = false;
! 872: }
! 873: }
! 874: static class DotFileFilter implements FilenameFilter
! 875: {
! 876: public DotFileFilter() {}
! 877: public boolean accept(File dir, String name)
! 878: {
! 879: if(name.startsWith(".")) return false;
! 880: else return true;
! 881: }
! 882: }
! 883: static class ImageFilter extends javax.swing.filechooser.FileFilter
! 884: {
! 885: public boolean accept(java.io.File file)
! 886: {
! 887: String name=file.getName().toLowerCase();
! 888: if(name.endsWith(".gif")||name.endsWith(".jpg")||name.endsWith(".tif")||name.endsWith(".png")||name.endsWith(".jpeg")||file.isDirectory())
! 889: return true ;
! 890: else return false;
! 891: }
! 892: public String getDescription()
! 893: {
! 894: return "Image File";
! 895: }
! 896: }
! 897:
! 898: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>