| 1 31 32 package org.opencms.workplace.explorer; 33 34 import org.opencms.db.CmsDbSqlException; 35 import org.opencms.db.CmsImportFolder; 36 import org.opencms.file.CmsFile; 37 import org.opencms.file.CmsProject; 38 import org.opencms.file.CmsProperty; 39 import org.opencms.file.CmsPropertyDefinition; 40 import org.opencms.file.CmsResource; 41 import org.opencms.file.CmsResourceFilter; 42 import org.opencms.file.types.I_CmsResourceType; 43 import org.opencms.jsp.CmsJspActionElement; 44 import org.opencms.main.OpenCms; 45 import org.opencms.util.CmsStringUtil; 46 import org.opencms.workplace.CmsWorkplace; 47 import org.opencms.workplace.CmsWorkplaceException; 48 import org.opencms.workplace.CmsWorkplaceSettings; 49 import org.opencms.workplace.commons.CmsChtype; 50 51 import java.util.ArrayList ; 52 import java.util.Iterator ; 53 import java.util.List ; 54 import java.util.Map ; 55 56 import javax.servlet.http.HttpServletRequest ; 57 import javax.servlet.http.HttpServletResponse ; 58 import javax.servlet.http.HttpSession ; 59 import javax.servlet.jsp.JspException ; 60 import javax.servlet.jsp.PageContext ; 61 62 import org.apache.commons.fileupload.FileItem; 63 64 79 public class CmsNewResourceUpload extends CmsNewResource { 80 81 82 public static final int ACTION_APPLET = 140; 83 84 85 public static final int ACTION_NEWFORM2 = 120; 86 87 88 public static final int ACTION_SHOWERROR = 150; 89 90 91 public static final int ACTION_SUBMITFORM2 = 130; 92 93 94 public static final String DIALOG_SHOWERROR = "showerror"; 95 96 97 public static final String DIALOG_SUBMITFORM2 = "submitform2"; 98 99 100 public static final String PARAM_NEWRESOURCENAME = "newresourcename"; 101 102 103 public static final String PARAM_REDIRECTURL = "redirecturl"; 104 105 106 public static final String PARAM_TARGETFRAME = "targetframe"; 107 108 109 public static final String PARAM_UNZIPFILE = "unzipfile"; 110 111 112 public static final String PARAM_UPLOADERROR = "uploaderror"; 113 114 115 public static final String PARAM_UPLOADFILE = "uploadfile"; 116 117 118 public static final String PARAM_UPLOADFOLDER = "uploadfolder"; 119 120 private String m_paramNewResourceName; 121 private String m_paramRedirectUrl; 122 private String m_paramTargetFrame; 123 private String m_paramUnzipFile; 124 private String m_paramUploadError; 125 private String m_paramUploadFile; 126 private String m_paramUploadFolder; 127 128 133 public CmsNewResourceUpload(CmsJspActionElement jsp) { 134 135 super(jsp); 136 } 137 138 145 public CmsNewResourceUpload(PageContext context, HttpServletRequest req, HttpServletResponse res) { 146 147 this(new CmsJspActionElement(context, req, res)); 148 } 149 150 163 public void actionCloseDialog() throws JspException { 164 165 if (getAction() == ACTION_CANCEL) { 166 try { 167 CmsResource res = getCms().readResource(getParamResource(), CmsResourceFilter.IGNORE_EXPIRATION); 168 if (res.getState() == CmsResource.STATE_NEW) { 169 getCms().deleteResource(getParamResource(), CmsResource.DELETE_PRESERVE_SIBLINGS); 171 } 172 if (res.getState() == CmsResource.STATE_CHANGED) { 173 CmsProject currentProject = getCms().getRequestContext().currentProject(); 175 byte[] onlineContents = null; 176 try { 177 getCms().getRequestContext().setCurrentProject( 179 getCms().readProject(CmsProject.ONLINE_PROJECT_ID)); 180 CmsFile onlineFile = getCms().readFile(getParamResource(), CmsResourceFilter.IGNORE_EXPIRATION); 181 onlineContents = onlineFile.getContents(); 182 183 } finally { 184 getCms().getRequestContext().setCurrentProject(currentProject); 186 } 187 if (onlineContents != null) { 188 CmsFile modFile = getCms().readFile(getParamResource(), CmsResourceFilter.IGNORE_EXPIRATION); 190 modFile.setContents(onlineContents); 191 getCms().writeFile(modFile); 192 } 193 } 194 } catch (RuntimeException e) { 195 } catch (Exception e) { 197 } 199 } 200 super.actionCloseDialog(); 201 } 202 203 208 public void actionUpdateFile() throws JspException { 209 210 try { 211 CmsResource res = getCms().readResource(getParamResource(), CmsResourceFilter.ALL); 212 I_CmsResourceType oldType = OpenCms.getResourceManager().getResourceType(res.getTypeId()); 213 if (!oldType.getTypeName().equals(getParamNewResourceType())) { 214 int newType = OpenCms.getResourceManager().getResourceType(getParamNewResourceType()).getTypeId(); 216 getCms().chtype(getParamResource(), newType); 217 } 218 if ((getParamNewResourceName() != null) && !getParamResource().endsWith(getParamNewResourceName())) { 219 String newResourceName = CmsResource.getFolderPath(getParamResource()) + getParamNewResourceName(); 220 getCms().renameResource(getParamResource(), newResourceName); 222 setParamResource(newResourceName); 223 } 224 } catch (Throwable e) { 225 setParamMessage(Messages.get().getBundle(getLocale()).key(Messages.ERR_UPLOAD_FILE_0)); 227 includeErrorpage(this, e); 228 } 229 } 230 231 236 public void actionUpload() throws JspException { 237 238 boolean unzipFile = Boolean.valueOf(getParamUnzipFile()).booleanValue(); 240 242 try { 243 Iterator i = getMultiPartFileItems().iterator(); 245 FileItem fi = null; 246 while (i.hasNext()) { 247 fi = (FileItem)i.next(); 248 if (fi.getName() != null) { 249 break; 251 } else { 252 continue; 254 } 255 } 256 257 if (fi != null) { 258 String fileName = fi.getName(); 259 long size = fi.getSize(); 260 long maxFileSizeBytes = OpenCms.getWorkplaceManager().getFileBytesMaxUploadSize(getCms()); 261 if ((maxFileSizeBytes > 0) && (size > maxFileSizeBytes)) { 263 throw new CmsWorkplaceException(Messages.get().container( 265 Messages.ERR_UPLOAD_FILE_SIZE_TOO_HIGH_1, 266 new Long (maxFileSizeBytes / 1024))); 267 } 268 byte[] content = fi.get(); 269 fi.delete(); 270 271 if (unzipFile) { 272 String currentFolder = getParamUploadFolder(); 274 if (CmsStringUtil.isEmpty(currentFolder)) { 275 currentFolder = getParamCurrentFolder(); 277 } 278 if (CmsStringUtil.isEmpty(currentFolder) || !currentFolder.startsWith("/")) { 279 currentFolder = computeCurrentFolder(); 281 } 282 new CmsImportFolder(content, currentFolder, getCms(), false); 284 285 } else { 286 String newResname = CmsResource.getName(fileName.replace('\\', '/')); 288 String title = newResname; 290 if (title.lastIndexOf('.') != -1) { 291 title = title.substring(0, title.lastIndexOf('.')); 292 } 293 List properties = new ArrayList (1); 294 CmsProperty titleProp = new CmsProperty(); 295 titleProp.setName(CmsPropertyDefinition.PROPERTY_TITLE); 296 if (OpenCms.getWorkplaceManager().isDefaultPropertiesOnStructure()) { 297 titleProp.setStructureValue(title); 298 } else { 299 titleProp.setResourceValue(title); 300 } 301 properties.add(titleProp); 302 newResname = getCms().getRequestContext().getFileTranslator().translateResource(newResname); 303 setParamNewResourceName(newResname); 304 setParamResource(newResname); 305 setParamResource(computeFullResourceName()); 306 int resTypeId = OpenCms.getResourceManager().getDefaultTypeForName(newResname).getTypeId(); 308 if (! getCms().existsResource(getParamResource(), CmsResourceFilter.IGNORE_EXPIRATION)) { 309 try { 310 getCms().createResource(getParamResource(), resTypeId, content, properties); 312 } catch (CmsDbSqlException sqlExc) { 313 getCms().lockResource(getParamResource()); 315 getCms().deleteResource(getParamResource(), CmsResource.DELETE_PRESERVE_SIBLINGS); 316 throw sqlExc; 317 } 318 } else { 319 checkLock(getParamResource()); 320 CmsFile file = getCms().readFile(getParamResource(), CmsResourceFilter.IGNORE_EXPIRATION); 321 byte[] contents = file.getContents(); 322 try { 323 getCms().replaceResource(getParamResource(), resTypeId, content, null); 324 } catch (CmsDbSqlException sqlExc) { 325 file.setContents(contents); 327 getCms().writeFile(file); 328 throw sqlExc; 329 } 330 } 331 } 332 } else { 333 throw new CmsWorkplaceException(Messages.get().container(Messages.ERR_UPLOAD_FILE_NOT_FOUND_0)); 334 } 335 } catch (Throwable e) { 336 setParamMessage(Messages.get().getBundle(getLocale()).key(Messages.ERR_UPLOAD_FILE_0)); 338 setAction(ACTION_SHOWERROR); 339 includeErrorpage(this, e); 340 } 341 } 342 343 348 public String buildTypeList() { 349 350 return CmsChtype.buildTypeList(this, false); 351 } 352 353 358 public String createAppletCode() { 359 360 StringBuffer applet = new StringBuffer (2048); 361 362 String scheme = getJsp().getRequest().getScheme(); 364 String host = getJsp().getRequest().getServerName(); 365 String path = OpenCms.getSystemInfo().getContextPath() + OpenCms.getSystemInfo().getServletPath(); 366 int port = getJsp().getRequest().getServerPort(); 367 String webapp = scheme + "://" + host + ":" + port + OpenCms.getSystemInfo().getContextPath(); 368 369 String fileExtensions = ""; 371 Map extensions = OpenCms.getResourceManager().getExtensionMapping(); 372 Iterator keys = extensions.entrySet().iterator(); 373 while (keys.hasNext()) { 374 Map.Entry entry = (Map.Entry )keys.next(); 375 String key = (String )entry.getKey(); 376 String value = (String )entry.getValue(); 377 fileExtensions += key + "=" + value + ","; 378 } 379 fileExtensions = fileExtensions.substring(0, fileExtensions.length() - 1); 380 381 long maxFileSize = OpenCms.getWorkplaceManager().getFileBytesMaxUploadSize(getCms()); 383 384 String currentFolder = getParamCurrentFolder(); 386 387 HttpSession session = getJsp().getRequest().getSession(false); 389 String sessionId = session.getId(); 391 392 String colors = "bgColor=#C0C0C0,outerBorderRightBottom=#333333,outerBorderLeftTop=#C0C0C0"; 395 colors += ",innerBorderRightBottom=#777777,innerBorderLeftTop=#F0F0F0"; 396 colors += ",bgHeadline=#000066,colorHeadline=#FFFFFF"; 397 colors += ",colorText=#000000,progessBar=#E10050"; 398 399 applet.append("<applet code=\"org.opencms.applet.upload.FileUploadApplet.class\" archive=\""); 401 applet.append(webapp); 402 applet.append("/resources/components/upload_applet/upload.jar\" width=\"500\" height=\"100\">\n"); 403 applet.append("<param name=\"opencms\" value=\""); 404 applet.append(scheme); 405 applet.append("://"); 406 applet.append(host); 407 applet.append(":"); 408 applet.append(port); 409 applet.append(getSkinUri()); 410 applet.append("filetypes/\">\n"); 411 applet.append("<param name=\"target\" value=\""); 412 applet.append(scheme); 413 applet.append("://"); 414 applet.append(host); 415 applet.append(":"); 416 applet.append(port); 417 applet.append(path); 418 applet.append("/system/workplace/commons/newresource_upload.jsp\">\n"); 419 applet.append("<param name=\"redirect\" value=\""); 420 applet.append(scheme); 421 applet.append("://"); 422 applet.append(host); 423 applet.append(":"); 424 applet.append(port); 425 applet.append(path); 426 if (CmsStringUtil.isEmpty(getParamRedirectUrl())) { 428 applet.append(CmsWorkplace.FILE_EXPLORER_FILELIST); 429 } else { 430 applet.append(getParamRedirectUrl()); 431 } 432 applet.append("?time=" + System.currentTimeMillis()); 434 applet.append("\">\n"); 435 applet.append("<param name=\"targetframe\" value=\""); 436 applet.append(getParamTargetFrame()); 437 applet.append("\">\n"); 438 applet.append("<param name=error value=\""); 439 applet.append(scheme); 440 applet.append("://"); 441 applet.append(host); 442 applet.append(":"); 443 applet.append(port); 444 applet.append(path); 445 applet.append("/system/workplace/action/explorer_files_new_upload.html\">\n"); 446 applet.append("<param name=\"sessionId\" value=\""); 447 applet.append(sessionId); 448 applet.append("\">\n"); 449 applet.append("<param name=\"filelist\" value=\""); 450 applet.append(currentFolder); 451 applet.append("\">\n"); 452 applet.append("<param name=\"colors\" value=\""); 453 applet.append(colors); 454 applet.append("\">\n"); 455 applet.append("<param name=\"fileExtensions\" value=\""); 456 applet.append(fileExtensions); 457 applet.append("\">\n\n"); 458 applet.append("<param name=\"maxsize\" value=\""); 459 applet.append(maxFileSize); 460 applet.append("\">\n"); 461 applet.append("<param name=\"actionOutputSelect\" value=\""); 462 applet.append(Messages.get().getBundle(getLocale()).key(Messages.GUI_UPLOADAPPLET_ACTION_SELECT_0)); 463 applet.append("\">\n"); 464 applet.append("<param name=\"actionOutputCount\"value=\""); 465 applet.append(Messages.get().getBundle(getLocale()).key(Messages.GUI_UPLOADAPPLET_ACTION_COUNT_0)); 466 applet.append("\">\n"); 467 applet.append("<param name=\"actionOutputCreate\" value=\""); 468 applet.append(Messages.get().getBundle(getLocale()).key(Messages.GUI_UPLOADAPPLET_ACTION_CREATE_0)); 469 applet.append("\">\n"); 470 applet.append("<param name=\"actionOutputUpload\" value=\""); 471 applet.append(Messages.get().getBundle(getLocale()).key(Messages.GUI_UPLOADAPPLET_ACTION_UPLOAD_0)); 472 applet.append("\">\n"); 473 applet.append("<param name=\"messageOutputUpload\" value=\""); 474 applet.append(Messages.get().getBundle(getLocale()).key(Messages.GUI_UPLOADAPPLET_MESSAGE_UPLOAD_0)); 475 applet.append("\">\n"); 476 applet.append("<param name=\"messageOutputErrorZip\" value=\""); 477 applet.append(Messages.get().getBundle(getLocale()).key(Messages.GUI_UPLOADAPPLET_MESSAGE_ERROR_ZIP_0)); 478 applet.append("\">\n"); 479 applet.append("<param name=\"messageOutputErrorSize\" value=\""); 480 applet.append(Messages.get().getBundle(getLocale()).key(Messages.GUI_UPLOADAPPLET_MESSAGE_ERROR_SIZE_0)); 481 applet.append("\">\n"); 482 applet.append("<param name=\"messageNoPreview\" value=\""); 483 applet.append(Messages.get().getBundle(getLocale()).key(Messages.GUI_UPLOADAPPLET_MESSAGE_NOPREVIEW_0)); 484 applet.append("\">\n"); 485 applet.append("<param name=\"messageOutputAdding\" value=\""); 486 applet.append(Messages.get().getBundle(getLocale()).key(Messages.GUI_UPLOADAPPLET_MESSAGE_ADDING_0)); 487 applet.append(" \">\n"); 488 applet.append("<param name=\"errorTitle\" value=\""); 489 applet.append(Messages.get().getBundle(getLocale()).key(Messages.GUI_UPLOADAPPLET_ERROR_TITLE_0)); 490 applet.append(" \">\n"); 491 applet.append("<param name=\"errorLine1\" value=\""); 492 applet.append(Messages.get().getBundle(getLocale()).key(Messages.GUI_UPLOADAPPLET_ERROR_LINE1_0)); 493 applet.append(" \">\n"); 494 applet.append("</applet>\n"); 495 496 return applet.toString(); 497 498 } 499 500 505 public String getParamNewResourceName() { 506 507 return m_paramNewResourceName; 508 } 509 510 515 public String getParamRedirectUrl() { 516 517 return m_paramRedirectUrl; 518 } 519 520 525 public String getParamTargetFrame() { 526 527 if (CmsStringUtil.isEmpty(m_paramTargetFrame)) { 528 return "explorer_files"; 529 } 530 531 return m_paramTargetFrame; 532 } 533 534 539 public String getParamUnzipFile() { 540 541 return m_paramUnzipFile; 542 } 543 544 549 public String getParamUploadError() { 550 551 return m_paramUploadError; 552 } 553 554 559 public String getParamUploadFile() { 560 561 return m_paramUploadFile; 562 } 563 564 569 public String getParamUploadFolder() { 570 571 return m_paramUploadFolder; 572 } 573 574 579 public void setParamNewResourceName(String newResourceName) { 580 581 m_paramNewResourceName = newResourceName; 582 } 583 584 589 public void setParamRedirectUrl(String paramRedirectUrl) { 590 591 m_paramRedirectUrl = paramRedirectUrl; 592 } 593 594 599 public void setParamTargetFrame(String paramTargetFrame) { 600 601 m_paramTargetFrame = paramTargetFrame; 602 } 603 604 609 public void setParamUnzipFile(String unzipFile) { 610 611 m_paramUnzipFile = unzipFile; 612 } 613 614 619 public void setParamUploadError(String uploadError) { 620 621 m_paramUploadError = uploadError; 622 } 623 624 629 public void setParamUploadFile(String uploadFile) { 630 631 m_paramUploadFile = uploadFile; 632 } 633 634 639 public void setParamUploadFolder(String uploadFolder) { 640 641 m_paramUploadFolder = uploadFolder; 642 } 643 644 649 public boolean unzipUpload() { 650 651 return Boolean.valueOf(getParamUnzipFile()).booleanValue(); 652 } 653 654 657 protected void initWorkplaceMembers(CmsJspActionElement jsp) { 658 659 String siteRoot = jsp.getRequestContext().getSiteRoot(); 660 super.initWorkplaceMembers(jsp); 666 if (!siteRoot.equals(getSettings().getSite())) { 667 getSettings().setSite(siteRoot); 668 jsp.getRequestContext().setSiteRoot(siteRoot); 669 } 670 } 671 672 675 protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) { 676 677 fillParamValues(request); 679 setParamDialogtype(DIALOG_TYPE); 681 if (DIALOG_OK.equals(getParamAction())) { 683 setAction(ACTION_OK); 684 } else if (DIALOG_SUBMITFORM.equals(getParamAction())) { 685 setAction(ACTION_SUBMITFORM); 686 } else if (DIALOG_SUBMITFORM2.equals(getParamAction())) { 687 setAction(ACTION_SUBMITFORM2); 688 } else if (DIALOG_CANCEL.equals(getParamAction())) { 689 setAction(ACTION_CANCEL); 690 } else { 691 if (getSettings().getUserSettings().useUploadApplet()) { 692 setAction(ACTION_APPLET); 693 } else { 694 setAction(ACTION_DEFAULT); 695 } 696 setParamTitle(key(Messages.GUI_NEWRESOURCE_UPLOAD_0)); 698 } 699 } 700 701 } 702 | Popular Tags |