1 31 32 package org.opencms.applet.upload; 33 34 import java.awt.Color ; 35 import java.awt.Font ; 36 import java.awt.FontMetrics ; 37 import java.awt.Graphics ; 38 import java.awt.Image ; 39 import java.io.File ; 40 import java.io.FileInputStream ; 41 import java.io.FileOutputStream ; 42 import java.io.IOException ; 43 import java.net.MalformedURLException ; 44 import java.net.URL ; 45 import java.util.HashMap ; 46 import java.util.StringTokenizer ; 47 import java.util.zip.ZipEntry ; 48 import java.util.zip.ZipOutputStream ; 49 50 import javax.swing.JApplet ; 51 import javax.swing.JFileChooser ; 52 import javax.swing.JOptionPane ; 53 54 import org.apache.commons.httpclient.Cookie; 55 import org.apache.commons.httpclient.HttpClient; 56 import org.apache.commons.httpclient.HttpState; 57 import org.apache.commons.httpclient.HttpStatus; 58 import org.apache.commons.httpclient.URI; 59 import org.apache.commons.httpclient.cookie.CookiePolicy; 60 import org.apache.commons.httpclient.methods.PostMethod; 61 import org.apache.commons.httpclient.methods.multipart.FilePart; 62 import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; 63 import org.apache.commons.httpclient.methods.multipart.Part; 64 import org.apache.commons.httpclient.methods.multipart.StringPart; 65 import org.apache.commons.httpclient.params.HttpConnectionParams; 66 import org.apache.commons.httpclient.params.HttpMethodParams; 67 68 77 public class FileUploadApplet extends JApplet implements Runnable { 78 79 80 public static final String C_JSESSIONID = "JSESSIONID"; 81 82 83 private static final long serialVersionUID = -3710093915699772778L; 84 85 86 Thread m_runner; 87 88 89 private String m_opencms = ""; 90 91 92 private String m_targetUrl = ""; 93 94 95 private String m_redirectUrl = ""; 96 97 98 private String m_redirectTargetFrame = ""; 99 100 101 private String m_errorUrl = ""; 102 103 104 private String m_uploadFolder = ""; 105 106 107 private long m_maxsize = -1; 108 109 110 private int m_resources; 111 112 113 private String m_fileExtensions = ""; 114 115 116 private HashMap m_colors = new HashMap (); 117 118 119 private String m_action = ""; 120 121 122 private String m_message = ""; 123 124 125 private int m_outputMode; 126 127 128 private int m_step; 129 130 131 private Image m_source; 132 private Image m_target; 133 private Image m_floater; 134 135 136 private int m_floaterPos = 50; 137 138 139 private String m_actionOutputSelect = "Seleting files for upload...."; 140 private String m_actionOutputCount = "Counting resources ...."; 141 private String m_actionOutputCreate = "Creating Zip-File..."; 142 private String m_actionOutputUpload = "Upload Zip-File"; 143 private String m_actionOutputError = "Error"; 144 private String m_messageNoPreview = "no preview available"; 145 private String m_errorLine1 = "An error has occurred on the server:"; 146 private String m_messageOutputUpload = "Please wait, uploading data..."; 147 private String m_messageOutputAdding = "Adding "; 148 private String m_messageOutputErrorSize = "Zip file too big:"; 149 private String m_messageOutputErrorZip = "Error creating Zip-File, see Java Console."; 150 private String m_certificateErrorTitle = "Error initializing the OpenCms Upload Applet"; 151 private String m_certificateErrorMessage = "The required Applet certificate has not been accepted!"; 152 153 154 private Font m_font; 155 private FontMetrics m_metrics; 156 private Image m_offscreen; 157 private Graphics m_offgraphics; 158 159 160 private JFileChooser m_fileSelector; 161 162 163 private boolean m_certificateAccepted; 164 165 168 public void destroy() { 169 170 } 172 173 176 public void displayError() { 177 178 m_outputMode = 5; 179 m_action = m_certificateErrorTitle; 180 m_message = m_certificateErrorMessage; 181 182 JOptionPane.showMessageDialog(this, m_message, m_action, JOptionPane.ERROR_MESSAGE); 183 184 try { 185 getAppletContext().showDocument(new URL (m_redirectUrl), m_redirectTargetFrame); 187 } catch (MalformedURLException e) { 188 e.printStackTrace(); 190 } 191 192 stop(); 193 } 194 195 198 public void init() { 199 200 m_opencms = getParameter("opencms"); 201 m_targetUrl = getParameter("target"); 202 m_redirectUrl = getParameter("redirect"); 203 m_redirectTargetFrame = getParameter("targetframe"); 204 if ((m_redirectTargetFrame == null) || m_redirectTargetFrame.equals("")) { 205 m_redirectTargetFrame = "explorer_files"; 206 } 207 m_errorUrl = getParameter("error"); 208 m_uploadFolder = getParameter("filelist"); 209 String tmpSize = getParameter("maxsize"); 210 if ((tmpSize != null) && (tmpSize.length() > 0)) { 211 m_maxsize = Long.parseLong(tmpSize); 212 } 213 m_fileExtensions = getParameter("fileExtensions"); 214 m_colors = extractColors(getParameter("colors")); 215 216 m_font = new java.awt.Font (null, Font.BOLD, 12); 218 m_metrics = getFontMetrics(m_font); 219 m_source = getImage(getCodeBase(), "org/opencms/applet/upload/applet_source.png"); 220 m_target = getImage(getCodeBase(), "org/opencms/applet/upload/applet_target.png"); 221 m_floater = getImage(getCodeBase(), "org/opencms/applet/upload/floater.gif"); 222 223 if (getParameter("actionOutputSelect") != null) { 225 m_actionOutputSelect = getParameter("actionOutputSelect"); 226 } 227 if (getParameter("actionOutputCount") != null) { 228 m_actionOutputCount = getParameter("actionOutputCount"); 229 } 230 if (getParameter("actionOutputCreate") != null) { 231 m_actionOutputCreate = getParameter("actionOutputCreate"); 232 } 233 if (getParameter("actionOutputUpload") != null) { 234 m_actionOutputUpload = getParameter("actionOutputUpload"); 235 } 236 if (getParameter("actionOutputError") != null) { 237 m_actionOutputError = getParameter("actionOutputError"); 238 } 239 if (getParameter("messageOutputUpload") != null) { 240 m_messageOutputUpload = getParameter("messageOutputUpload"); 241 } 242 if (getParameter("messageOutputAdding") != null) { 243 m_messageOutputAdding = getParameter("messageOutputAdding"); 244 } 245 if (getParameter("messageOutputErrorZip") != null) { 246 m_messageOutputErrorZip = getParameter("messageOutputErrorZip"); 247 } 248 if (getParameter("messageOutputErrorSize") != null) { 249 m_messageOutputErrorSize = getParameter("messageOutputErrorSize"); 250 } 251 if (getParameter("messageNoPreview") != null) { 252 m_messageNoPreview = getParameter("messageNoPreview"); 253 } 254 if (getParameter("errorLine1") != null) { 255 m_errorLine1 = getParameter("errorLine1"); 256 } 257 if (getParameter("certificateErrorTitle") != null) { 258 m_certificateErrorTitle = getParameter("certificateErrorTitle"); 259 } 260 if (getParameter("certificateErrorMessage") != null) { 261 m_certificateErrorMessage = getParameter("certificateErrorMessage"); 262 } 263 264 m_certificateAccepted = true; 265 try { 266 System.setProperty( 268 org.apache.commons.logging.LogFactory.FACTORY_PROPERTY, 269 org.apache.commons.logging.LogFactory.FACTORY_DEFAULT); 270 } catch (SecurityException e) { 271 m_certificateAccepted = false; 273 e.printStackTrace(); 274 } 275 } 276 277 280 public void moveFloater() { 281 282 m_floaterPos += 10; 283 if ((m_floaterPos) > 430) { 284 m_floaterPos = 50; 285 } 286 repaint(); 287 } 288 289 292 public void paint(Graphics g) { 293 294 m_offscreen = createImage(getSize().width, getSize().height); 296 m_offgraphics = m_offscreen.getGraphics(); 297 m_offgraphics.setColor(getColor("bgColor")); 298 m_offgraphics.fillRect(0, 0, getSize().width, getSize().height); 299 m_offgraphics.setColor(getColor("outerBorderRightBottom")); 300 m_offgraphics.drawLine(0, getSize().height - 1, getSize().width - 1, getSize().height - 1); 301 m_offgraphics.drawLine(getSize().width - 1, 0, getSize().width - 1, getSize().height - 1); 302 m_offgraphics.setColor(getColor("outerBorderLeftTop")); 303 m_offgraphics.drawLine(0, 0, getSize().width - 1, 0); 304 m_offgraphics.drawLine(0, 0, 0, getSize().height - 1); 305 m_offgraphics.setColor(getColor("innerBorderRightBottom")); 306 m_offgraphics.drawLine(1, getSize().height - 2, getSize().width - 2, getSize().height - 2); 307 m_offgraphics.drawLine(getSize().width - 2, 1, getSize().width - 2, getSize().height - 2); 308 m_offgraphics.setColor(getColor("innerBorderLeftTop")); 309 m_offgraphics.drawLine(1, 1, getSize().width - 2, 1); 310 m_offgraphics.drawLine(1, 1, 1, getSize().height - 2); 311 m_offgraphics.setColor(getColor("bgHeadline")); 312 m_offgraphics.fillRect(4, 4, getSize().width - 5, 18); 313 314 m_offgraphics.setColor(getColor("innerBorderRightBottom")); 315 m_offgraphics.drawLine(10, getSize().height - 11, getSize().width - 11, getSize().height - 11); 316 m_offgraphics.drawLine(getSize().width - 11, 25, getSize().width - 11, getSize().height - 11); 317 m_offgraphics.setColor(getColor("innerBorderLeftTop")); 318 m_offgraphics.drawLine(10, 25, getSize().width - 11, 25); 319 m_offgraphics.drawLine(10, 25, 10, getSize().height - 11); 320 321 int cx = 10; 323 int cy = 17; 324 m_offgraphics.setFont(m_font); 325 m_offgraphics.setColor(getColor("colorHeadline")); 326 m_offgraphics.drawString(m_action, cx, cy); 327 328 m_offgraphics.setColor(getColor("colorText")); 329 if (m_outputMode >= 3) { 331 cx = Math.max((getSize().width - m_metrics.stringWidth(m_message)) / 2, 0); 332 } else { 333 cx = 25; 334 } 335 cy = 41; 336 m_offgraphics.drawString(m_message, cx, cy); 337 338 if (m_outputMode == 2) { 340 float bar = new Float (m_step).floatValue() / new Float (m_resources).floatValue(); 341 String barText = "(" + m_step + " / " + m_resources + ")"; 342 m_offgraphics.drawRect(25, 50, 450, 20); 343 m_offgraphics.setColor(Color.white); 344 m_offgraphics.fillRect(26, 51, 449, 19); 345 m_offgraphics.setColor(getColor("progessBar")); 346 m_offgraphics.fillRect(26, 51, new Float (bar * 449).intValue(), 19); 347 int progressWith = m_metrics.stringWidth(barText); 348 cx = Math.max((getSize().width - progressWith) / 2, 0); 349 cy = 64; 350 m_offgraphics.setColor(Color.black); 351 m_offgraphics.drawString(barText, cx, cy); 352 } 353 354 if (m_outputMode == 3) { 356 m_offgraphics.drawImage(m_floater, m_floaterPos, 57, this); 357 m_offgraphics.drawImage(m_source, 30, 47, this); 358 m_offgraphics.drawImage(m_target, 440, 47, this); 359 } 360 361 g.drawImage(m_offscreen, 0, 0, null); 363 } 364 365 368 public void run() { 369 370 try { 371 boolean ok = true; 372 while (ok) { 373 ok = true; 374 375 m_message = ""; 376 m_resources = 0; 377 m_step = 0; 378 380 if (m_fileSelector == null) { 381 m_fileSelector = new JFileChooser (); 382 } 383 384 m_fileSelector.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); 386 387 m_fileSelector.setDialogTitle(m_actionOutputSelect); 388 389 m_fileSelector.addChoosableFileFilter(new ImageFilter()); 391 m_fileSelector.addChoosableFileFilter(new OfficeFilter()); 392 m_fileSelector.setAcceptAllFileFilterUsed(true); 393 m_fileSelector.setMultiSelectionEnabled(true); 395 m_fileSelector.setFileView(new ImageFileView(m_opencms, m_fileExtensions)); 397 m_fileSelector.setAccessory(new ImagePreview(m_fileSelector, m_messageNoPreview)); 399 400 m_action = m_actionOutputSelect; 401 repaint(); 402 403 int returnVal = m_fileSelector.showDialog(this, "OK"); 405 406 if (returnVal == JFileChooser.APPROVE_OPTION) { 408 m_outputMode = 1; 410 m_action = m_actionOutputCount; 411 repaint(); 412 m_resources = countResources(m_fileSelector.getSelectedFiles()); 413 m_outputMode = 2; 415 File targetFile = createZipFile(m_fileSelector.getSelectedFiles()); 416 if ((targetFile == null) || ((m_maxsize > 0) && (targetFile.length() > m_maxsize))) { 418 m_outputMode = 4; 420 if (targetFile == null) { 421 m_message = m_messageOutputErrorZip; 422 } else { 423 m_message = m_messageOutputErrorSize + " " + targetFile.length() + " > " + m_maxsize; 424 } 425 m_action = m_actionOutputError; 426 repaint(); 427 JOptionPane.showMessageDialog(this, m_message, m_action, JOptionPane.ERROR_MESSAGE); 429 } else { 430 m_outputMode = 3; 431 m_message = m_messageOutputUpload + " (" + targetFile.length() / 1024 + " kb)"; 432 repaint(); 433 FileUploadThread uploadThreat = new FileUploadThread(); 435 436 uploadThreat.init(this); 437 uploadThreat.start(); 438 439 uploadZipFile(targetFile); 440 ok = false; 441 } 442 443 } else { 444 ok = false; 446 getAppletContext().showDocument(new URL (m_redirectUrl), m_redirectTargetFrame); 447 } 448 } 449 } catch (RuntimeException e) { 450 e.printStackTrace(); 451 } catch (Exception e) { 452 e.printStackTrace(); 453 } 454 } 455 456 459 public void start() { 460 461 if (m_certificateAccepted) { 462 m_runner = new Thread (this); 464 m_runner.start(); 465 } else { 466 displayError(); 468 } 469 } 470 471 474 public void stop() { 475 476 m_runner = null; 477 } 478 479 482 public void update(Graphics g) { 483 484 paint(g); 485 } 486 487 495 private void addFileToZip(ZipOutputStream zipStream, File file, String filename) throws Exception { 496 497 String name = filename; 499 if (name.length() > 40) { 500 name = "..." + name.substring(name.length() - 40, name.length()); 501 } 502 m_message = m_messageOutputAdding + " " + name + ".."; 503 m_step++; 504 repaint(); 505 ZipEntry entry = new ZipEntry (filename); 506 zipStream.putNextEntry(entry); 507 zipStream.write(getFileBytes(file)); 508 zipStream.closeEntry(); 509 } 510 511 519 private void addFolderToZip(ZipOutputStream zipStream, File file, String prefix) throws Exception { 520 521 String foldername = file.getName(); 522 523 prefix += "/" + foldername; 524 File [] subresources = file.listFiles(); 526 for (int i = 0; i < subresources.length; i++) { 528 if (subresources[i].isFile()) { 530 531 addFileToZip(zipStream, subresources[i], prefix + "/" + subresources[i].getName()); 533 } else { 534 addFolderToZip(zipStream, subresources[i], prefix); 536 } 537 } 538 } 539 540 546 private int countResources(File [] files) { 547 548 int count = 0; 549 for (int i = 0; i < files.length; i++) { 551 if (files[i].isFile()) { 552 count++; 554 } else { 555 count += countSubresources(files[i]); 557 } 558 } 559 return count; 560 } 561 562 568 private int countSubresources(File folder) { 569 570 int count = 0; 571 if (folder.isFile()) { 572 count = 1; 574 } else { 575 count = countResources(folder.listFiles()); 577 } 578 return count; 579 } 580 581 587 private File createZipFile(File [] files) { 588 589 File targetFile = null; 590 m_action = m_actionOutputCreate; 591 try { 592 String zipFileName = ".opencms_upload.zip"; 594 String userHome = System.getProperty("user.home"); 595 if (userHome != null) { 597 if (!userHome.endsWith(File.separator)) { 598 userHome = userHome + File.separator; 599 } 600 zipFileName = userHome + zipFileName; 601 } 602 ZipOutputStream zipStream = new ZipOutputStream (new FileOutputStream (zipFileName)); 603 for (int i = 0; i < files.length; i++) { 605 606 if (files[i].isFile()) { 608 addFileToZip(zipStream, files[i], files[i].getName()); 609 } else { 610 addFolderToZip(zipStream, files[i], ""); 611 } 612 repaint(); 613 files[i] = null; 615 } 616 zipStream.close(); 617 targetFile = new File (zipFileName); 619 } catch (Exception e) { 620 System.err.println("Error creating zipfile " + e); 621 } 622 return targetFile; 623 624 } 625 626 632 private HashMap extractColors(String colors) { 633 634 HashMap colorStorage = new HashMap (); 635 636 if (colors != null) { 637 StringTokenizer tok = new StringTokenizer (colors, ","); 638 while (tok.hasMoreElements()) { 641 String token = tok.nextToken(); 642 String colorName = token.substring(0, token.indexOf("=")); 644 String colorValue = token.substring(token.indexOf("=") + 1); 645 colorStorage.put(colorName, colorValue); 646 } 647 } 648 return colorStorage; 649 } 650 651 657 private Color getColor(String colorName) { 658 659 Color col = Color.black; 660 try { 661 col = Color.decode((String )m_colors.get(colorName)); 662 } catch (Exception e) { 663 System.err.println("Error reading " + colorName + ":" + e); 664 } 665 return col; 666 } 667 668 675 private byte[] getFileBytes(File file) throws Exception { 676 677 byte[] buffer = null; 678 FileInputStream fileStream = null; 679 int charsRead; 680 int size; 681 try { 682 fileStream = new FileInputStream (file); 683 charsRead = 0; 684 size = new Long (file.length()).intValue(); 685 buffer = new byte[size]; 686 while (charsRead < size) { 687 charsRead += fileStream.read(buffer, charsRead, size - charsRead); 688 } 689 return buffer; 690 } catch (IOException e) { 691 throw e; 692 } finally { 693 try { 694 if (fileStream != null) { 695 fileStream.close(); 696 } 697 } catch (IOException e) { 698 } 700 } 701 } 702 703 708 private void uploadZipFile(File uploadFile) { 709 710 m_action = m_actionOutputUpload; 711 repaint(); 712 713 PostMethod post = new PostMethod(m_targetUrl); 714 715 try { 716 Part[] parts = new Part[4]; 717 parts[0] = new FilePart(uploadFile.getName(), uploadFile); 718 parts[1] = new StringPart("action", "submitform"); 719 parts[2] = new StringPart("unzipfile", "true"); 720 parts[3] = new StringPart("uploadfolder", m_uploadFolder); 721 722 HttpMethodParams methodParams = post.getParams(); 723 methodParams.setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); 724 MultipartRequestEntity request = new MultipartRequestEntity(parts, methodParams); 725 post.setRequestEntity(request); 726 727 String sessionId = getParameter("sessionId"); 729 String query = ";" + C_JSESSIONID.toLowerCase() + "=" + sessionId; 730 post.setQueryString(query); 731 post.addRequestHeader(C_JSESSIONID, sessionId); 732 733 HttpClient client = new HttpClient(); 734 HttpConnectionParams connectionParams = client.getHttpConnectionManager().getParams(); 735 connectionParams.setConnectionTimeout(5000); 736 737 client.getState(); 739 client.getHostConfiguration().getHost(); 740 741 HttpState initialState = new HttpState(); 742 URI uri = new URI(m_targetUrl, false); 743 Cookie sessionCookie = new Cookie(uri.getHost(), C_JSESSIONID, sessionId, "/", null, false); 744 initialState.addCookie(sessionCookie); 745 client.setState(initialState); 746 747 int status = client.executeMethod(post); 749 750 if (status == HttpStatus.SC_OK) { 751 getAppletContext().showDocument(new URL (m_redirectUrl), m_redirectTargetFrame); 753 } else { 754 String error = m_errorLine1 + "\n" + post.getStatusLine(); 756 getAppletContext().showDocument( 758 new URL (m_errorUrl + "?action=showerror&uploaderror=" + error), 759 "explorer_files"); 760 } 761 } catch (RuntimeException e) { 762 e.printStackTrace(); 763 } catch (Exception e) { 764 e.printStackTrace(); 765 } finally { 766 post.releaseConnection(); 767 uploadFile.delete(); 769 } 770 } 771 } | Popular Tags |