1 31 32 package org.opencms.staticexport; 33 34 import org.opencms.file.CmsFile; 35 import org.opencms.file.CmsObject; 36 import org.opencms.file.CmsProperty; 37 import org.opencms.file.CmsPropertyDefinition; 38 import org.opencms.file.CmsResource; 39 import org.opencms.file.CmsVfsResourceNotFoundException; 40 import org.opencms.file.types.CmsResourceTypeJsp; 41 import org.opencms.i18n.CmsAcceptLanguageHeaderParser; 42 import org.opencms.i18n.CmsI18nInfo; 43 import org.opencms.i18n.CmsLocaleManager; 44 import org.opencms.loader.I_CmsResourceLoader; 45 import org.opencms.main.CmsContextInfo; 46 import org.opencms.main.CmsEvent; 47 import org.opencms.main.CmsException; 48 import org.opencms.main.CmsIllegalArgumentException; 49 import org.opencms.main.CmsLog; 50 import org.opencms.main.I_CmsEventListener; 51 import org.opencms.main.OpenCms; 52 import org.opencms.report.CmsLogReport; 53 import org.opencms.report.I_CmsReport; 54 import org.opencms.security.CmsSecurityException; 55 import org.opencms.site.CmsSiteManager; 56 import org.opencms.util.CmsFileUtil; 57 import org.opencms.util.CmsMacroResolver; 58 import org.opencms.util.CmsRequestUtil; 59 import org.opencms.util.CmsStringUtil; 60 import org.opencms.util.CmsUUID; 61 import org.opencms.workplace.CmsWorkplace; 62 63 import java.io.File ; 64 import java.io.FileOutputStream ; 65 import java.io.IOException ; 66 import java.util.ArrayList ; 67 import java.util.Collections ; 68 import java.util.HashMap ; 69 import java.util.Iterator ; 70 import java.util.List ; 71 import java.util.Map ; 72 73 import javax.servlet.ServletException ; 74 import javax.servlet.http.HttpServletRequest ; 75 import javax.servlet.http.HttpServletResponse ; 76 77 import org.apache.commons.collections.map.LRUMap; 78 import org.apache.commons.logging.Log; 79 80 91 public class CmsStaticExportManager implements I_CmsEventListener { 92 93 94 public static final String EXPORT_ATTRIBUTE_ERROR_MESSAGE = "javax.servlet.error.message"; 95 96 97 public static final String EXPORT_ATTRIBUTE_ERROR_REQUEST_URI = "javax.servlet.error.request_uri"; 98 99 100 public static final String EXPORT_ATTRIBUTE_ERROR_SERVLET_NAME = "javax.servlet.error.servlet_name"; 101 102 103 public static final String EXPORT_ATTRIBUTE_ERROR_STATUS_CODE = "javax.servlet.error.status_code"; 104 105 106 public static final String EXPORT_DEFAULT_FILE = "index_export.html"; 107 108 109 public static final int EXPORT_LINK_WITH_PARAMETER = 2; 110 111 112 public static final int EXPORT_LINK_WITHOUT_PARAMETER = 1; 113 114 115 public static final String EXPORT_MARKER = "exporturi"; 116 117 118 public static final int HANDLER_FINISH_TIME = 60; 119 120 121 private static final String CACHEVALUE_404 = "?404"; 122 123 124 private static final Log LOG = CmsLog.getLog(CmsStaticExportManager.class); 125 126 127 private static CmsExportFolderMatcher m_exportFolderMatcher; 128 129 130 private String m_acceptCharsetHeader; 131 132 133 private String m_acceptLanguageHeader; 134 135 136 private Map m_cacheExportLinks; 137 138 139 private Map m_cacheExportUris; 140 141 142 private Map m_cacheOnlineLinks; 143 144 145 private Map m_cacheSecureLinks; 146 147 148 private String m_defaultAcceptCharsetHeader; 149 150 151 private String m_defaultAcceptLanguageHeader; 152 153 154 private List m_exportFolders; 155 156 157 private List m_exportHeaders; 158 159 160 private Map m_exportnameResources; 161 162 163 private boolean m_exportPropertyDefault; 164 165 166 private boolean m_exportRelativeLinks; 167 168 169 private List m_exportRules; 170 171 172 private List m_exportSuffixes; 173 174 175 private CmsStaticExportExportRule m_exportTmpRule; 176 177 178 private String m_exportUrl; 179 180 181 private String m_exportUrlConfigured; 182 183 184 private String m_exportUrlPrefix; 185 186 187 private I_CmsStaticExportHandler m_handler; 188 189 190 private Object m_lockCmsEvent; 191 192 193 private Object m_lockScrubExportFolders; 194 195 196 private Object m_lockSetExportnames; 197 198 199 private boolean m_quickPlainExport; 200 201 202 private String m_remoteAddr; 203 204 205 private String m_rfsPrefix; 206 207 208 private String m_rfsPrefixConfigured; 209 210 211 private List m_rfsRules; 212 213 214 private CmsStaticExportRfsRule m_rfsTmpRule; 215 216 217 private boolean m_staticExportEnabled; 218 219 220 private String m_staticExportPath; 221 222 223 private String m_staticExportPathConfigured; 224 225 226 private String m_testResource; 227 228 229 private String m_vfsPrefix; 230 231 232 private String m_vfsPrefixConfigured; 233 234 238 public CmsStaticExportManager() { 239 240 m_lockCmsEvent = new Object (); 241 m_lockScrubExportFolders = new Object (); 242 m_lockSetExportnames = new Object (); 243 m_exportSuffixes = new ArrayList (); 244 m_exportFolders = new ArrayList (); 245 m_exportHeaders = new ArrayList (); 246 m_rfsRules = new ArrayList (); 247 m_exportRules = new ArrayList (); 248 m_exportTmpRule = new CmsStaticExportExportRule("", ""); 249 m_rfsTmpRule = new CmsStaticExportRfsRule("", "", "", "", "", null); 250 } 251 252 258 public void addExportRule(String name, String description) { 259 260 m_exportRules.add(new CmsStaticExportExportRule( 261 name, 262 description, 263 m_exportTmpRule.getModifiedResources(), 264 m_exportTmpRule.getExportResourcePatterns())); 265 m_exportTmpRule = new CmsStaticExportExportRule("", ""); 266 } 267 268 273 public void addExportRuleRegex(String regex) { 274 275 m_exportTmpRule.addModifiedResource(regex); 276 } 277 278 283 public void addExportRuleUri(String exportUri) { 284 285 m_exportTmpRule.addExportResourcePattern(exportUri); 286 } 287 288 298 public void addRfsRule( 299 String name, 300 String description, 301 String source, 302 String rfsPrefix, 303 String exportPath, 304 String useRelativeLinks) { 305 306 Boolean relativeLinks = (useRelativeLinks == null ? null : Boolean.valueOf(useRelativeLinks)); 307 m_rfsRules.add(new CmsStaticExportRfsRule( 308 name, 309 description, 310 source, 311 rfsPrefix, 312 exportPath, 313 relativeLinks, 314 m_rfsTmpRule.getRelatedSystemResources())); 315 m_rfsTmpRule = new CmsStaticExportRfsRule("", "", "", "", "", null); 316 } 317 318 323 public void addRfsRuleSystemRes(String regex) { 324 325 m_rfsTmpRule.addRelatedSystemRes(regex); 326 } 327 328 335 public void cacheExportUri(String rfsName, String vfsName, String parameters) { 336 337 m_cacheExportUris.put(rfsName, new CmsStaticExportData(vfsName, parameters)); 338 } 339 340 346 public void cacheOnlineLink(Object linkName, Object vfsName) { 347 348 m_cacheOnlineLinks.put(linkName, vfsName); 349 } 350 351 358 public void cmsEvent(CmsEvent event) { 359 360 switch (event.getType()) { 361 case I_CmsEventListener.EVENT_UPDATE_EXPORTS: 362 scrubExportFolders(null); 363 clearCaches(event); 364 break; 365 case I_CmsEventListener.EVENT_PUBLISH_PROJECT: 366 CmsUUID publishHistoryId = new CmsUUID((String )event.getData().get(I_CmsEventListener.KEY_PUBLISHID)); 368 if (LOG.isDebugEnabled()) { 369 LOG.debug(Messages.get().getBundle().key(Messages.LOG_EVENT_PUBLISH_PROJECT_1, publishHistoryId)); 370 } 371 I_CmsReport report = (I_CmsReport)event.getData().get(I_CmsEventListener.KEY_REPORT); 372 if (report == null) { 373 report = new CmsLogReport(CmsLocaleManager.getDefaultLocale(), getClass()); 374 } 375 synchronized (m_lockCmsEvent) { 376 getHandler().performEventPublishProject(publishHistoryId, report); 377 } 378 clearCaches(event); 379 380 if (LOG.isDebugEnabled()) { 381 LOG.debug(Messages.get().getBundle().key( 382 Messages.LOG_EVENT_PUBLISH_PROJECT_FINISHED_1, 383 publishHistoryId)); 384 } 385 386 break; 387 case I_CmsEventListener.EVENT_CLEAR_CACHES: 388 clearCaches(event); 389 break; 390 default: 391 } 393 } 394 395 409 public int export(HttpServletRequest req, HttpServletResponse res, CmsObject cms, CmsStaticExportData data) 410 throws CmsException, IOException , ServletException , CmsStaticExportException { 411 412 String vfsName = data.getVfsName(); 413 String rfsName = data.getRfsName(); 414 CmsResource resource = data.getResource(); 415 416 String siteRoot = CmsSiteManager.getSiteRoot(vfsName); 418 419 CmsI18nInfo i18nInfo = OpenCms.getLocaleManager().getI18nInfo( 420 req, 421 cms.getRequestContext().currentUser(), 422 cms.getRequestContext().currentProject(), 423 vfsName); 424 425 String remoteAddr = m_remoteAddr; 426 if (remoteAddr == null) { 427 remoteAddr = CmsContextInfo.LOCALHOST; 428 } 429 CmsContextInfo contextInfo = new CmsContextInfo( 430 cms.getRequestContext().currentUser(), 431 cms.getRequestContext().currentProject(), 432 vfsName, 433 "/", 434 i18nInfo.getLocale(), 435 i18nInfo.getEncoding(), 436 remoteAddr); 437 438 cms = OpenCms.initCmsObject(null, contextInfo); 439 440 if (siteRoot != null) { 441 vfsName = vfsName.substring(siteRoot.length()); 442 } else { 443 siteRoot = "/"; 444 } 445 446 if (LOG.isDebugEnabled()) { 447 LOG.debug(Messages.get().getBundle().key(Messages.LOG_STATIC_EXPORT_SITE_ROOT_2, siteRoot, vfsName)); 448 } 449 cms.getRequestContext().setSiteRoot(siteRoot); 450 451 String oldUri = null; 452 453 boolean exportOnDemand = ((req != null) && (res != null)); 456 457 CmsStaticExportResponseWrapper wrapRes = null; 458 if (res != null) { 459 wrapRes = new CmsStaticExportResponseWrapper(res); 460 } 461 if (LOG.isDebugEnabled()) { 462 LOG.debug(Messages.get().getBundle().key(Messages.LOG_SE_RESOURCE_START_1, data)); 463 } 464 465 CmsFile file; 466 if (resource.isFile()) { 468 file = cms.readFile(vfsName); 469 } else { 470 file = CmsFile.upgrade(OpenCms.initResource(cms, vfsName, req, wrapRes), cms); 471 if (cms.existsResource(vfsName + file.getName())) { 472 vfsName = vfsName + file.getName(); 473 } 474 rfsName += EXPORT_DEFAULT_FILE; 475 } 476 477 I_CmsResourceLoader loader = OpenCms.getResourceManager().getLoader(file); 479 if ((loader == null) || (!loader.isStaticExportEnabled())) { 480 Object [] arguments = new Object [] {vfsName, new Integer (file.getTypeId())}; 481 throw new CmsStaticExportException(Messages.get().container(Messages.ERR_EXPORT_NOT_SUPPORTED_2, arguments)); 482 } 483 484 if (exportOnDemand) { 487 String mimetype = OpenCms.getResourceManager().getMimeType( 488 file.getName(), 489 cms.getRequestContext().getEncoding()); 490 if (wrapRes != null) { 491 wrapRes.setContentType(mimetype); 492 } 493 oldUri = cms.getRequestContext().getUri(); 494 cms.getRequestContext().setUri(vfsName); 495 } 496 497 byte[] result = loader.export(cms, file, req, wrapRes); 499 500 file = null; 502 503 int status = -1; 504 if (result != null) { 505 String exportPath = getExportPath(siteRoot + vfsName); 507 if (isExportLink(cms, vfsName)) { 509 writeResource(req, exportPath, rfsName, vfsName, resource, result); 510 if (vfsName.startsWith(CmsWorkplace.VFS_PATH_SYSTEM)) { 512 Iterator it = getRfsRules().iterator(); 514 while (it.hasNext()) { 515 CmsStaticExportRfsRule rule = (CmsStaticExportRfsRule)it.next(); 516 if (rule.match(vfsName)) { 517 writeResource(req, rule.getExportPath(), rfsName, vfsName, resource, result); 518 } 519 } 520 } 521 status = (wrapRes != null) ? wrapRes.getStatus() : -1; 523 if (status < 0) { 524 status = HttpServletResponse.SC_OK; 526 } 527 } else { 528 status = HttpServletResponse.SC_SEE_OTHER; 531 } 532 } else { 533 status = HttpServletResponse.SC_NOT_MODIFIED; 536 } 537 538 if (exportOnDemand) { 541 cms.getRequestContext().setUri(oldUri); 542 } 543 544 return status; 545 } 546 547 557 public synchronized void exportFullStaticRender(boolean purgeFirst, I_CmsReport report) 558 throws CmsException, IOException , ServletException { 559 560 if (purgeFirst) { 562 563 Map eventData = new HashMap (); 564 eventData.put(I_CmsEventListener.KEY_REPORT, report); 565 CmsEvent clearCacheEvent = new CmsEvent(I_CmsEventListener.EVENT_CLEAR_CACHES, eventData); 566 OpenCms.fireCmsEvent(clearCacheEvent); 567 568 scrubExportFolders(report); 569 CmsObject cms = OpenCms.initCmsObject(OpenCms.getDefaultUsers().getUserExport()); 570 cms.deleteAllStaticExportPublishedResources(EXPORT_LINK_WITHOUT_PARAMETER); 571 cms.deleteAllStaticExportPublishedResources(EXPORT_LINK_WITH_PARAMETER); 572 } 573 574 CmsAfterPublishStaticExportHandler handler = new CmsAfterPublishStaticExportHandler(); 576 handler.doExportAfterPublish(null, report); 578 } 579 580 585 public String getAcceptCharsetHeader() { 586 587 return m_acceptCharsetHeader; 588 } 589 590 595 public String getAcceptLanguageHeader() { 596 597 return m_acceptLanguageHeader; 598 } 599 600 610 public CmsStaticExportData getCachedExportUri(String rfsName) { 611 612 return (CmsStaticExportData)m_cacheExportUris.get(rfsName); 613 } 614 615 621 public String getCachedOnlineLink(String vfsName) { 622 623 return (String )m_cacheOnlineLinks.get(vfsName); 624 } 625 626 633 public String getCacheKey(String siteRoot, String uri) { 634 635 return new StringBuffer (siteRoot).append(uri).toString(); 636 } 637 638 643 public String getDefault() { 644 645 return String.valueOf(m_exportPropertyDefault); 646 } 647 648 653 public String getDefaultAcceptCharsetHeader() { 654 655 return m_defaultAcceptCharsetHeader; 656 } 657 658 663 public String getDefaultAcceptLanguageHeader() { 664 665 return m_defaultAcceptLanguageHeader; 666 } 667 668 673 public String getDefaultRfsPrefix() { 674 675 return m_rfsPrefix; 676 } 677 678 685 public CmsStaticExportData getExportData(HttpServletRequest request, CmsObject cms) { 686 687 if (!isStaticExportEnabled()) { 688 return null; 690 } 691 692 String rfsName = request.getParameter(EXPORT_MARKER); 694 if ((rfsName == null)) { 695 rfsName = (String )request.getAttribute(EXPORT_ATTRIBUTE_ERROR_REQUEST_URI); 696 } 697 698 if (request.getHeader(CmsRequestUtil.HEADER_OPENCMS_EXPORT) != null) { 699 int prefix = rfsName.startsWith(getExportUrlPrefix()) ? getExportUrlPrefix().length() : 0; 702 if (prefix > 0) { 703 rfsName = rfsName.substring(prefix); 704 } else { 705 return null; 706 } 707 } 708 709 if (!isValidRfsName(rfsName)) { 710 return null; 712 } 713 714 return getExportData(rfsName, null, cms); 715 } 716 717 724 public CmsStaticExportData getExportData(String vfsName, CmsObject cms) { 725 726 return getExportData(getRfsName(cms, vfsName), vfsName, cms); 727 } 728 729 734 public String getExportEnabled() { 735 736 return String.valueOf(m_staticExportEnabled); 737 } 738 739 744 public CmsExportFolderMatcher getExportFolderMatcher() { 745 746 return m_exportFolderMatcher; 747 } 748 749 754 public List getExportFolderPatterns() { 755 756 return Collections.unmodifiableList(m_exportFolders); 757 } 758 759 766 public List getExportHeaders() { 767 768 return Collections.unmodifiableList(m_exportHeaders); 769 } 770 771 776 public Map getExportnames() { 777 778 return m_exportnameResources; 779 } 780 781 802 public String getExportPath(String vfsName) { 803 804 if (vfsName != null) { 805 Iterator it = m_rfsRules.iterator(); 806 while (it.hasNext()) { 807 CmsStaticExportRfsRule rule = (CmsStaticExportRfsRule)it.next(); 808 if (rule.getSource().matcher(vfsName).matches()) { 809 return rule.getExportPath(); 810 } 811 } 812 } 813 return m_staticExportPath; 814 } 815 816 826 public String getExportPathForConfiguration() { 827 828 return m_staticExportPathConfigured; 829 } 830 831 836 public boolean getExportPropertyDefault() { 837 838 return m_exportPropertyDefault; 839 } 840 841 846 public List getExportRules() { 847 848 return Collections.unmodifiableList(m_exportRules); 849 } 850 851 856 public List getExportSuffixes() { 857 858 return m_exportSuffixes; 859 } 860 861 867 public String getExportUrl() { 868 869 return m_exportUrl; 870 } 871 872 882 public String getExportUrlForConfiguration() { 883 884 return m_exportUrlConfigured; 885 } 886 887 893 public String getExportUrlPrefix() { 894 895 return m_exportUrlPrefix; 896 } 897 898 905 public I_CmsStaticExportHandler getHandler() { 906 907 if (m_handler == null) { 908 setHandler(CmsOnDemandStaticExportHandler.class.getName()); 909 } 910 return m_handler; 911 } 912 913 918 public String getPlainExportOptimization() { 919 920 return String.valueOf(m_quickPlainExport); 921 } 922 923 928 public boolean getQuickPlainExport() { 929 930 return m_quickPlainExport; 931 } 932 933 938 public String getRelativeLinks() { 939 940 return String.valueOf(m_exportRelativeLinks); 941 } 942 943 948 public String getRemoteAddr() { 949 950 return m_remoteAddr; 951 } 952 953 958 public String getRemoteAddress() { 959 960 return m_remoteAddr; 961 } 962 963 974 public String getRfsName(CmsObject cms, String vfsName) { 975 976 return getRfsName(cms, vfsName, null); 977 } 978 979 989 public String getRfsName(CmsObject cms, String vfsName, String parameters) { 990 991 String rfsName = vfsName; 992 try { 993 CmsProperty exportNameProperty = cms.readPropertyObject( 995 CmsResource.getFolderPath(rfsName), 996 CmsPropertyDefinition.PROPERTY_EXPORTNAME, 997 true); 998 999 if (exportNameProperty.isNullProperty()) { 1000 rfsName = cms.getRequestContext().addSiteRoot(rfsName); 1002 } else { 1003 String exportname = exportNameProperty.getValue(); 1005 if (exportname.charAt(0) != '/') { 1006 exportname = '/' + exportname; 1007 } 1008 if (exportname.charAt(exportname.length() - 1) != '/') { 1009 exportname = exportname + '/'; 1010 } 1011 String value = null; 1012 boolean cont; 1013 String resourceName = rfsName; 1014 do { 1015 try { 1017 CmsProperty prop = cms.readPropertyObject( 1018 resourceName, 1019 CmsPropertyDefinition.PROPERTY_EXPORTNAME, 1020 false); 1021 if (prop.isIdentical(exportNameProperty)) { 1022 value = prop.getValue(); 1024 } 1025 cont = (value == null) && (resourceName.length() > 1); 1026 } catch (CmsVfsResourceNotFoundException e) { 1027 cont = (resourceName.length() > 1); 1029 } catch (CmsSecurityException se) { 1030 cont = false; 1032 } 1033 if (cont) { 1034 resourceName = CmsResource.getParentFolder(resourceName); 1035 } 1036 } while (cont); 1037 rfsName = exportname + rfsName.substring(resourceName.length()); 1038 } 1039 1040 String extension = CmsFileUtil.getFileExtension(rfsName); 1041 boolean isJsp = extension.equals(".jsp"); 1045 if (isJsp) { 1046 CmsResource res = cms.readResource(vfsName); 1047 isJsp = res.getTypeId() == CmsResourceTypeJsp.getStaticTypeId(); 1048 if (isJsp) { 1050 String suffix = cms.readPropertyObject(vfsName, CmsPropertyDefinition.PROPERTY_EXPORTSUFFIX, true).getValue( 1051 ".html"); 1052 if (!extension.equals(suffix.toLowerCase())) { 1053 rfsName += suffix; 1054 extension = suffix; 1055 } 1056 } 1057 } 1058 if (parameters != null) { 1059 rfsName = CmsFileUtil.getRfsPath(rfsName, extension, parameters); 1061 try { 1063 cms.writeStaticExportPublishedResource( 1064 rfsName, 1065 EXPORT_LINK_WITH_PARAMETER, 1066 parameters, 1067 System.currentTimeMillis()); 1068 } catch (CmsException e) { 1069 LOG.error(Messages.get().getBundle().key(Messages.LOG_WRITE_FAILED_1, rfsName), e); 1070 } 1071 } 1072 } catch (CmsException e) { 1073 rfsName = vfsName; 1075 } 1076 1077 if (!vfsName.startsWith(CmsWorkplace.VFS_PATH_SYSTEM)) { 1079 return getRfsPrefix(cms.getRequestContext().addSiteRoot(vfsName)).concat(rfsName); 1080 } else { 1081 return getRfsPrefix(cms.getRequestContext().getSiteRoot() + "/").concat(rfsName); 1082 } 1083 } 1084 1085 1105 public String getRfsPrefix(String vfsName) { 1106 1107 if (vfsName != null) { 1108 Iterator it = m_rfsRules.iterator(); 1109 while (it.hasNext()) { 1110 CmsStaticExportRfsRule rule = (CmsStaticExportRfsRule)it.next(); 1111 if (rule.getSource().matcher(vfsName).matches()) { 1112 return rule.getRfsPrefix(); 1113 } 1114 } 1115 } 1116 return m_rfsPrefix; 1117 } 1118 1119 1129 public String getRfsPrefixForConfiguration() { 1130 1131 return m_rfsPrefixConfigured; 1132 } 1133 1134 1139 public List getRfsRules() { 1140 1141 return Collections.unmodifiableList(m_rfsRules); 1142 } 1143 1144 1149 public String getTestResource() { 1150 1151 return m_testResource; 1152 } 1153 1154 1166 public String getVfsName(CmsObject cms, String rfsName) { 1167 1168 CmsStaticExportData data = getExportData(rfsName, null, cms); 1169 if (data != null) { 1170 String result = data.getVfsName(); 1171 if ((result != null) && result.startsWith(cms.getRequestContext().getSiteRoot())) { 1172 result = result.substring(cms.getRequestContext().getSiteRoot().length()); 1173 } 1174 return result; 1175 } 1176 return null; 1177 } 1178 1179 1194 public String getVfsPrefix() { 1195 1196 return m_vfsPrefix; 1197 } 1198 1199 1209 public String getVfsPrefixForConfiguration() { 1210 1211 return m_vfsPrefixConfigured; 1212 } 1213 1214 1219 public void initialize(CmsObject cms) { 1220 1221 m_staticExportPath = normalizeExportPath(m_staticExportPathConfigured); 1223 if (m_staticExportPath.equals(OpenCms.getSystemInfo().getWebApplicationRfsPath())) { 1224 throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_INVALID_EXPORT_PATH_0)); 1225 } 1226 m_rfsPrefix = normalizeRfsPrefix(m_rfsPrefixConfigured); 1228 Iterator itRfsRules = m_rfsRules.iterator(); 1229 while (itRfsRules.hasNext()) { 1230 CmsStaticExportRfsRule rule = (CmsStaticExportRfsRule)itRfsRules.next(); 1231 try { 1232 rule.setExportPath(normalizeExportPath(rule.getExportPathConfigured())); 1233 } catch (CmsIllegalArgumentException e) { 1234 CmsLog.INIT.info(e.getMessageContainer()); 1235 rule.setExportPath(m_staticExportPath); 1236 } 1237 rule.setRfsPrefix(normalizeRfsPrefix(rule.getRfsPrefixConfigured())); 1238 } 1239 m_vfsPrefix = insertContextStrings(m_vfsPrefixConfigured); 1240 m_vfsPrefix = CmsFileUtil.normalizePath(m_vfsPrefix, '/'); 1241 if (CmsResource.isFolder(m_vfsPrefix)) { 1242 m_vfsPrefix = m_vfsPrefix.substring(0, m_vfsPrefix.length() - 1); 1244 } 1245 if (CmsLog.INIT.isDebugEnabled()) { 1246 if (cms != null) { 1247 CmsLog.INIT.debug(Messages.get().getBundle().key(Messages.INIT_SE_MANAGER_CREATED_1, cms)); 1248 } else { 1249 CmsLog.INIT.debug(Messages.get().getBundle().key(Messages.INIT_SE_MANAGER_CREATED_0)); 1250 } 1251 } 1252 1253 LRUMap lruMap1 = new LRUMap(2048); 1254 m_cacheOnlineLinks = Collections.synchronizedMap(lruMap1); 1255 if (OpenCms.getMemoryMonitor().enabled()) { 1256 OpenCms.getMemoryMonitor().register(this.getClass().getName() + ".m_cacheOnlineLinks", lruMap1); 1258 } 1259 1260 LRUMap lruMap2 = new LRUMap(2048); 1261 m_cacheExportUris = Collections.synchronizedMap(lruMap2); 1262 if (OpenCms.getMemoryMonitor().enabled()) { 1263 OpenCms.getMemoryMonitor().register(this.getClass().getName() + ".m_cacheExportUris", lruMap2); 1265 } 1266 1267 LRUMap lruMap3 = new LRUMap(2048); 1268 m_cacheSecureLinks = Collections.synchronizedMap(lruMap3); 1269 if (OpenCms.getMemoryMonitor().enabled()) { 1270 OpenCms.getMemoryMonitor().register(this.getClass().getName() + ".m_cacheSecureLinks", lruMap3); 1272 } 1273 1274 LRUMap lruMap4 = new LRUMap(2048); 1275 m_cacheExportLinks = Collections.synchronizedMap(lruMap4); 1276 if (OpenCms.getMemoryMonitor().enabled()) { 1277 OpenCms.getMemoryMonitor().register(this.getClass().getName() + ".m_cacheExportLinks", lruMap4); 1279 } 1280 1281 OpenCms.addCmsEventListener(this, new int[] { 1283 I_CmsEventListener.EVENT_PUBLISH_PROJECT, 1284 I_CmsEventListener.EVENT_CLEAR_CACHES, 1285 I_CmsEventListener.EVENT_UPDATE_EXPORTS}); 1286 1287 m_exportFolderMatcher = new CmsExportFolderMatcher(m_exportFolders, m_testResource); 1288 1289 setExportnames(); 1291 1292 m_defaultAcceptLanguageHeader = CmsAcceptLanguageHeaderParser.createLanguageHeader(); 1294 1295 m_defaultAcceptCharsetHeader = OpenCms.getSystemInfo().getDefaultEncoding(); 1297 1298 int pos = m_exportUrl.indexOf("://"); 1300 if (pos > 0) { 1301 int pos2 = m_exportUrl.indexOf('/', pos + 3); 1303 if (pos2 > 0) { 1304 m_exportUrlPrefix = m_exportUrl.substring(pos2); 1305 } else { 1306 m_exportUrlPrefix = ""; 1308 } 1309 } else { 1310 m_exportUrlPrefix = m_exportUrl; 1311 } 1312 if (CmsLog.INIT.isInfoEnabled()) { 1313 if (isStaticExportEnabled()) { 1314 CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_STATIC_EXPORT_ENABLED_0)); 1315 CmsLog.INIT.info(Messages.get().getBundle().key( 1316 Messages.INIT_EXPORT_DEFAULT_1, 1317 new Boolean (getExportPropertyDefault()))); 1318 itRfsRules = m_rfsRules.iterator(); 1319 while (itRfsRules.hasNext()) { 1320 CmsStaticExportRfsRule rfsRule = (CmsStaticExportRfsRule)itRfsRules.next(); 1321 CmsLog.INIT.info(Messages.get().getBundle().key( 1322 Messages.INIT_EXPORT_RFS_RULE_EXPORT_PATH_2, 1323 rfsRule.getSource(), 1324 rfsRule.getExportPath())); 1325 CmsLog.INIT.info(Messages.get().getBundle().key( 1326 Messages.INIT_EXPORT_RFS_RULE_RFS_PREFIX_2, 1327 rfsRule.getSource(), 1328 rfsRule.getRfsPrefix())); 1329 if (rfsRule.getUseRelativeLinks() != null) { 1330 if (rfsRule.getUseRelativeLinks().booleanValue()) { 1331 CmsLog.INIT.info(Messages.get().getBundle().key( 1332 Messages.INIT_EXPORT_RFS_RULE_RELATIVE_LINKS_1, 1333 rfsRule.getSource())); 1334 } else { 1335 CmsLog.INIT.info(Messages.get().getBundle().key( 1336 Messages.INIT_EXPORT_RFS_RULE_ABSOLUTE_LINKS_1, 1337 rfsRule.getSource())); 1338 } 1339 } 1340 } 1341 CmsLog.INIT.info(Messages.get().getBundle().key( 1343 Messages.INIT_EXPORT_RFS_RULE_EXPORT_PATH_2, 1344 "/", 1345 m_staticExportPath)); 1346 CmsLog.INIT.info(Messages.get().getBundle().key( 1347 Messages.INIT_EXPORT_RFS_RULE_RFS_PREFIX_2, 1348 "/", 1349 m_rfsPrefix)); 1350 if (m_exportRelativeLinks) { 1351 CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_EXPORT_RFS_RULE_RELATIVE_LINKS_1, "/")); 1352 } else { 1353 CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_EXPORT_RFS_RULE_ABSOLUTE_LINKS_1, "/")); 1354 } 1355 CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_EXPORT_VFS_PREFIX_1, getVfsPrefix())); 1356 CmsLog.INIT.info(Messages.get().getBundle().key( 1357 Messages.INIT_EXPORT_EXPORT_HANDLER_1, 1358 getHandler().getClass().getName())); 1359 CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_EXPORT_URL_1, getExportUrl())); 1360 CmsLog.INIT.info(Messages.get().getBundle().key( 1361 Messages.INIT_EXPORT_OPTIMIZATION_1, 1362 getPlainExportOptimization())); 1363 CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_EXPORT_TESTRESOURCE_1, getTestResource())); 1364 } else { 1365 CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_STATIC_EXPORT_DISABLED_0)); 1366 } 1367 } 1368 } 1369 1370 1386 public boolean isExportLink(CmsObject cms, String vfsName) { 1387 1388 boolean result = false; 1389 if (isStaticExportEnabled()) { 1390 Boolean exportResource = (Boolean )m_cacheExportLinks.get(getCacheKey( 1391 cms.getRequestContext().getSiteRoot(), 1392 vfsName)); 1393 if (exportResource == null) { 1394 try { 1395 CmsObject exportCms = OpenCms.initCmsObject(OpenCms.getDefaultUsers().getUserExport()); 1398 exportCms.getRequestContext().setSiteRoot(cms.getRequestContext().getSiteRoot()); 1399 String exportValue = exportCms.readPropertyObject( 1401 vfsName, 1402 CmsPropertyDefinition.PROPERTY_EXPORT, 1403 true).getValue(); 1404 if (exportValue == null) { 1405 if (getExportPropertyDefault()) { 1407 result = true; 1409 } else { 1410 result = isSuffixExportable(vfsName); 1412 } 1413 } else { 1414 result = Boolean.valueOf(exportValue).booleanValue(); 1416 } 1417 } catch (Exception e) { 1418 } 1420 m_cacheExportLinks.put(getCacheKey(cms.getRequestContext().getSiteRoot(), vfsName), new Boolean (result)); 1421 } else { 1422 result = exportResource.booleanValue(); 1423 } 1424 } 1425 return result; 1426 } 1427 1428 1444 public boolean isSecureLink(CmsObject cms, String vfsName) { 1445 1446 if (!cms.getRequestContext().currentProject().isOnlineProject()) { 1447 return false; 1448 } 1449 Boolean secureResource = (Boolean )m_cacheSecureLinks.get(getCacheKey( 1450 cms.getRequestContext().getSiteRoot(), 1451 vfsName)); 1452 if (secureResource == null) { 1453 try { 1454 String secureProp = cms.readPropertyObject(vfsName, CmsPropertyDefinition.PROPERTY_SECURE, true).getValue(); 1455 secureResource = Boolean.valueOf(secureProp); 1456 m_cacheSecureLinks.put(getCacheKey(cms.getRequestContext().getSiteRoot(), vfsName), secureResource); 1458 } catch (CmsVfsResourceNotFoundException e) { 1459 secureResource = Boolean.FALSE; 1460 m_cacheSecureLinks.put(getCacheKey(cms.getRequestContext().getSiteRoot(), vfsName), secureResource); 1462 } catch (Exception e) { 1463 secureResource = Boolean.FALSE; 1466 } 1467 } 1468 return secureResource.booleanValue(); 1469 } 1470 1471 1483 public boolean isSecureLink(CmsObject cms, String vfsName, String siteRoot) { 1484 1485 if (siteRoot == null) { 1486 return isSecureLink(cms, vfsName); 1487 } 1488 1489 try { 1491 cms.getRequestContext().saveSiteRoot(); 1492 cms.getRequestContext().setSiteRoot(siteRoot); 1493 return isSecureLink(cms, vfsName); 1494 } finally { 1495 cms.getRequestContext().restoreSiteRoot(); 1496 } 1497 } 1498 1499 1504 public boolean isStaticExportEnabled() { 1505 1506 return m_staticExportEnabled; 1507 } 1508 1509 1515 public boolean isSuffixExportable(String resourceName) { 1516 1517 if (resourceName == null) { 1518 return false; 1519 } 1520 int pos = resourceName.lastIndexOf('.'); 1521 if (pos >= 0) { 1522 String suffix = resourceName.substring(pos).toLowerCase(); 1523 return m_exportSuffixes.contains(suffix); 1524 } 1525 return false; 1526 } 1527 1528 1535 public boolean relativeLinksInExport(String vfsName) { 1536 1537 if (vfsName != null) { 1538 Iterator it = m_rfsRules.iterator(); 1539 while (it.hasNext()) { 1540 CmsStaticExportRfsRule rule = (CmsStaticExportRfsRule)it.next(); 1541 if (rule.getSource().matcher(vfsName).matches()) { 1542 return rule.getUseRelativeLinks() != null ? rule.getUseRelativeLinks().booleanValue() 1543 : m_exportRelativeLinks; 1544 } 1545 } 1546 } 1547 return m_exportRelativeLinks; 1548 } 1549 1550 1555 public void setAcceptCharsetHeader(String value) { 1556 1557 m_acceptCharsetHeader = value; 1558 } 1559 1560 1565 public void setAcceptLanguageHeader(String value) { 1566 1567 m_acceptLanguageHeader = value; 1568 } 1569 1570 1575 public void setDefault(String value) { 1576 1577 m_exportPropertyDefault = Boolean.valueOf(value).booleanValue(); 1578 } 1579 1580 1585 public void setExportEnabled(String value) { 1586 1587 m_staticExportEnabled = Boolean.valueOf(value).booleanValue(); 1588 } 1589 1590 1595 public void setExportFolderPattern(String folder) { 1596 1597 m_exportFolders.add(folder); 1598 } 1599 1600 1607 public void setExportHeader(String exportHeader) { 1608 1609 if (CmsStringUtil.splitAsArray(exportHeader, ':').length == 2) { 1610 if (CmsLog.INIT.isInfoEnabled()) { 1611 CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_EXPORT_HEADERS_1, exportHeader)); 1612 } 1613 m_exportHeaders.add(exportHeader); 1614 } else { 1615 if (CmsLog.INIT.isWarnEnabled()) { 1616 CmsLog.INIT.warn(Messages.get().getBundle().key(Messages.INIT_INVALID_HEADER_1, exportHeader)); 1617 } 1618 } 1619 } 1620 1621 1626 public void setExportPath(String path) { 1627 1628 m_staticExportPathConfigured = path; 1629 } 1630 1631 1636 public void setExportSuffix(String suffix) { 1637 1638 m_exportSuffixes.add(suffix.toLowerCase()); 1639 } 1640 1641 1646 public void setExportUrl(String url) { 1647 1648 m_exportUrl = insertContextStrings(url); 1649 m_exportUrlConfigured = url; 1650 } 1651 1652 1657 public void setHandler(String handlerClassName) { 1658 1659 try { 1660 m_handler = (I_CmsStaticExportHandler)Class.forName(handlerClassName).newInstance(); 1661 } catch (Exception e) { 1662 LOG.error(e); 1664 } 1665 } 1666 1667 1672 public void setPlainExportOptimization(String value) { 1673 1674 m_quickPlainExport = Boolean.valueOf(value).booleanValue(); 1675 } 1676 1677 1682 public void setRelativeLinks(String value) { 1683 1684 m_exportRelativeLinks = Boolean.valueOf(value).booleanValue(); 1685 } 1686 1687 1692 public void setRemoteAddr(String addr) { 1693 1694 m_remoteAddr = addr; 1695 } 1696 1697 1702 public void setRfsPrefix(String rfsPrefix) { 1703 1704 m_rfsPrefixConfigured = rfsPrefix; 1705 } 1706 1707 1712 public void setTestResource(String testResource) { 1713 1714 m_testResource = testResource; 1715 } 1716 1717 1722 public void setVfsPrefix(String vfsPrefix) { 1723 1724 m_vfsPrefixConfigured = vfsPrefix; 1725 } 1726 1727 1732 public synchronized void shutDown() { 1733 1734 int count = 0; 1735 while ((count < HANDLER_FINISH_TIME) && m_handler.isBusy()) { 1737 count++; 1738 try { 1739 if (CmsLog.INIT.isInfoEnabled()) { 1740 CmsLog.INIT.info(Messages.get().getBundle().key( 1741 Messages.INIT_STATIC_EXPORT_SHUTDOWN_3, 1742 m_handler.getClass().getName(), 1743 String.valueOf(count), 1744 String.valueOf(HANDLER_FINISH_TIME))); 1745 } 1746 Thread.sleep(1000); 1747 } catch (InterruptedException e) { 1748 count = HANDLER_FINISH_TIME; 1750 } 1751 } 1752 1753 if (CmsLog.INIT.isInfoEnabled()) { 1754 CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_SHUTDOWN_1, this.getClass().getName())); 1755 } 1756 1757 } 1758 1759 1768 protected String getRfsPrefixForRfsName(String rfsName) { 1769 1770 String retVal = ""; 1771 if (rfsName.startsWith(m_rfsPrefix + "/")) { 1773 retVal = m_rfsPrefix; 1774 } 1775 Iterator it = m_rfsRules.iterator(); 1777 while (it.hasNext()) { 1778 CmsStaticExportRfsRule rule = (CmsStaticExportRfsRule)it.next(); 1779 String rfsPrefix = rule.getRfsPrefix(); 1780 if (rfsName.startsWith(rfsPrefix + "/") && (retVal.length() < rfsPrefix.length())) { 1781 retVal = rfsPrefix; 1782 } 1783 } 1784 return retVal; 1785 } 1786 1787 1796 String getVfsNameInternal(CmsObject cms, String rfsName) { 1797 1798 String vfsName = null; 1799 CmsResource resource; 1800 1801 boolean match = false; 1802 1803 try { 1804 resource = cms.readResource(cms.getRequestContext().removeSiteRoot(rfsName)); 1805 if (resource.isFolder() && !CmsResource.isFolder(rfsName)) { 1806 rfsName += '/'; 1807 } 1808 vfsName = rfsName; 1809 match = true; 1810 } catch (Throwable t) { 1811 } 1813 1814 if (!match) { 1815 Map exportnameFolders = getExportnames(); 1818 Iterator i = exportnameFolders.keySet().iterator(); 1819 while (i.hasNext()) { 1820 String exportName = (String )i.next(); 1821 if (rfsName.startsWith(exportName)) { 1822 match = true; 1824 vfsName = exportnameFolders.get(exportName) + rfsName.substring(exportName.length()); 1825 try { 1826 resource = cms.readResource(vfsName); 1827 if (resource.isFolder()) { 1828 if (!CmsResource.isFolder(rfsName)) { 1829 rfsName += '/'; 1830 } 1831 if (!CmsResource.isFolder(vfsName)) { 1832 vfsName += '/'; 1833 } 1834 } 1835 break; 1836 } catch (CmsVfsResourceNotFoundException e) { 1837 match = false; 1839 continue; 1840 } catch (CmsException e) { 1841 rfsName = null; 1842 break; 1843 } 1844 } 1845 } 1846 } 1847 1848 if (!match) { 1850 int extPos = rfsName.lastIndexOf('.'); 1852 if (extPos >= 0) { 1853 String cutName = rfsName.substring(0, extPos); 1854 int pos = cutName.lastIndexOf('.'); 1855 if (pos >= 0) { 1856 String extension = cutName.substring(pos).toLowerCase(); 1858 if (".jsp".equals(extension)) { 1859 return getVfsNameInternal(cms, cutName); 1860 } 1861 } 1862 } 1863 } 1864 if (match) { 1865 return vfsName; 1866 } else { 1867 return null; 1868 } 1869 } 1870 1871 1876 private void clearCaches(CmsEvent event) { 1877 1878 1881 m_cacheOnlineLinks.clear(); 1883 m_cacheExportUris.clear(); 1884 m_cacheSecureLinks.clear(); 1885 m_cacheExportLinks.clear(); 1886 setExportnames(); 1887 if (LOG.isDebugEnabled()) { 1888 String eventType = "EVENT_CLEAR_CACHES"; 1889 if (event.getType() != I_CmsEventListener.EVENT_CLEAR_CACHES) { 1890 eventType = "EVENT_PUBLISH_PROJECT"; 1891 } 1892 LOG.debug(Messages.get().getBundle().key(Messages.LOG_FLUSHED_CACHES_1, eventType)); 1893 } 1894 } 1895 1896 1904 private void createExportFolder(String exportPath, String rfsName) throws CmsException { 1905 1906 String exportFolderName = CmsFileUtil.normalizePath(exportPath + CmsResource.getFolderPath(rfsName)); 1907 File exportFolder = new File(exportFolderName); 1908 if (!exportFolder.exists()) { 1909 if (!exportFolder.mkdirs()) { 1910 throw new CmsStaticExportException(Messages.get().container(Messages.ERR_CREATE_FOLDER_1, rfsName)); 1911 1912 } 1913 } 1914 } 1915 1916 1924 private CmsStaticExportData getExportData(String rfsName, String vfsName, CmsObject cms) { 1925 1926 CmsResource resource = null; 1927 cms.getRequestContext().saveSiteRoot(); 1928 try { 1929 cms.getRequestContext().setSiteRoot("/"); 1930 1931 rfsName = rfsName.substring(getRfsPrefixForRfsName(rfsName).length()); 1933 1934 String parameters = null; 1935 if (vfsName == null) { 1936 CmsStaticExportData data = getCachedExportUri(rfsName); 1938 if (data != null) { 1939 vfsName = data.getVfsName(); 1940 parameters = data.getParameters(); 1941 } 1942 } 1943 1944 if (vfsName != null) { 1945 if (CACHEVALUE_404 != vfsName) { 1947 try { 1949 resource = cms.readResource(vfsName); 1950 } catch (CmsException e) { 1951 return null; 1953 } 1954 return new CmsStaticExportData(vfsName, rfsName, resource, parameters); 1956 } else { 1957 return null; 1959 } 1960 } else { 1961 boolean match = false; 1963 1964 vfsName = getVfsNameInternal(cms, rfsName); 1965 if (vfsName != null) { 1966 match = true; 1967 try { 1968 resource = cms.readResource(vfsName); 1969 } catch (CmsException e) { 1970 rfsName = null; 1971 } 1972 } 1973 1974 if (!match) { 1975 try { 1978 parameters = cms.readStaticExportPublishedResourceParameters(rfsName); 1979 if (CmsStringUtil.isNotEmpty(parameters)) { 1981 1982 String rfsBaseName = rfsName.substring(0, rfsName.lastIndexOf('_')); 1984 1985 String vfsBaseName = getVfsNameInternal(cms, rfsBaseName); 1987 1988 if (vfsBaseName != null) { 1990 resource = cms.readResource(vfsBaseName); 1991 CmsStaticExportData exportData = new CmsStaticExportData( 1992 vfsBaseName, 1993 rfsName, 1994 resource, 1995 parameters); 1996 cacheExportUri(rfsName, exportData.getVfsName(), parameters); 1997 return exportData; 1998 } 1999 } 2000 } catch (CmsException e) { 2001 } 2003 cacheExportUri(rfsName, CACHEVALUE_404, null); 2005 return null; 2006 } else { 2007 cacheExportUri(rfsName, vfsName, null); 2009 return new CmsStaticExportData(vfsName, rfsName, resource); 2010 } 2011 } 2012 } finally { 2013 cms.getRequestContext().restoreSiteRoot(); 2014 } 2015 } 2016 2017 2023 private String insertContextStrings(String path) { 2024 2025 CmsMacroResolver resolver = CmsMacroResolver.newInstance(); 2027 2028 resolver.addMacro("CONTEXT_NAME", OpenCms.getSystemInfo().getContextPath()); 2030 resolver.addMacro("SERVLET_NAME", OpenCms.getSystemInfo().getServletPath()); 2031 2032 return resolver.resolveMacros(path); 2034 } 2035 2036 2042 private boolean isValidRfsName(String rfsName) { 2043 2044 if (rfsName != null) { 2045 if (rfsName.startsWith(m_rfsPrefix + "/")) { 2047 return true; 2048 } 2049 Iterator it = m_rfsRules.iterator(); 2051 while (it.hasNext()) { 2052 CmsStaticExportRfsRule rule = (CmsStaticExportRfsRule)it.next(); 2053 String rfsPrefix = rule.getRfsPrefix() + "/"; 2054 if (rfsName.startsWith(rfsPrefix)) { 2055 return true; 2056 } 2057 } 2058 } 2059 return false; 2060 } 2061 2062 2071 private String normalizeExportPath(String exportPath) { 2072 2073 String result = insertContextStrings(exportPath); 2074 result = OpenCms.getSystemInfo().getAbsoluteRfsPathRelativeToWebApplication(result); 2075 if (result.endsWith(File.separator)) { 2076 result = result.substring(0, result.length() - 1); 2078 } 2079 return result; 2080 } 2081 2082 2091 private String normalizeRfsPrefix(String rfsPrefix) { 2092 2093 String result = insertContextStrings(rfsPrefix); 2094 result = CmsFileUtil.normalizePath(result, '/'); 2095 if (CmsResource.isFolder(result)) { 2096 result = result.substring(0, result.length() - 1); 2098 } 2099 return result; 2100 } 2101 2102 2107 private void scrubExportFolders(I_CmsReport report) { 2108 2109 if (report != null) { 2110 report.println( 2111 Messages.get().container(Messages.RPT_DELETING_EXPORT_FOLDERS_BEGIN_0), 2112 I_CmsReport.FORMAT_HEADLINE); 2113 } 2114 synchronized (m_lockScrubExportFolders) { 2115 int count = 0; 2116 Integer size = new Integer (m_rfsRules.size() + 1); 2117 String exportFolderName = CmsFileUtil.normalizePath(m_staticExportPath + '/'); 2119 try { 2120 File exportFolder = new File(exportFolderName); 2121 if (exportFolder.exists() && exportFolder.canWrite()) { 2123 CmsFileUtil.purgeDirectory(exportFolder); 2124 } 2125 count++; 2126 if (report != null) { 2127 report.println(Messages.get().container( 2128 Messages.RPT_DELETE_EXPORT_FOLDER_3, 2129 new Integer (count), 2130 size, 2131 exportFolderName), I_CmsReport.FORMAT_NOTE); 2132 } else { 2133 if (LOG.isInfoEnabled()) { 2135 LOG.info(Messages.get().getBundle().key(Messages.LOG_DEL_MAIN_SE_FOLDER_1, exportFolderName)); 2136 } 2137 } 2138 } catch (Throwable t) { 2139 if (LOG.isWarnEnabled()) { 2141 LOG.warn(Messages.get().getBundle().key(Messages.LOG_FOLDER_DELETION_FAILED_1, exportFolderName), t); 2142 } 2143 } 2144 Iterator it = m_rfsRules.iterator(); 2146 while (it.hasNext()) { 2147 CmsStaticExportRfsRule rule = (CmsStaticExportRfsRule)it.next(); 2148 exportFolderName = CmsFileUtil.normalizePath(rule.getExportPath() + '/'); 2149 try { 2150 File exportFolder = new File(exportFolderName); 2151 if (exportFolder.exists() && exportFolder.canWrite()) { 2153 CmsFileUtil.purgeDirectory(exportFolder); 2154 } 2155 count++; 2156 if (report != null) { 2157 report.println(Messages.get().container( 2158 Messages.RPT_DELETE_EXPORT_FOLDER_3, 2159 new Integer (count), 2160 size, 2161 exportFolderName), I_CmsReport.FORMAT_NOTE); 2162 } else { 2163 if (LOG.isInfoEnabled()) { 2165 LOG.info(Messages.get().getBundle().key(Messages.LOG_DEL_MAIN_SE_FOLDER_1, exportFolderName)); 2166 } 2167 } 2168 } catch (Throwable t) { 2169 if (LOG.isWarnEnabled()) { 2171 LOG.warn( 2172 Messages.get().getBundle().key(Messages.LOG_FOLDER_DELETION_FAILED_1, exportFolderName), 2173 t); 2174 } 2175 } 2176 } 2177 } 2178 if (report != null) { 2179 report.println( 2180 Messages.get().container(Messages.RPT_DELETING_EXPORT_FOLDERS_END_0), 2181 I_CmsReport.FORMAT_HEADLINE); 2182 } 2183 } 2184 2185 2188 private void setExportnames() { 2189 2190 if (LOG.isDebugEnabled()) { 2191 LOG.debug(Messages.get().getBundle().key(Messages.LOG_UPDATE_EXPORTNAME_PROP_START_0)); 2192 } 2193 2194 List resources; 2195 CmsObject cms = null; 2196 try { 2197 cms = OpenCms.initCmsObject(OpenCms.getDefaultUsers().getUserExport()); 2198 resources = cms.readResourcesWithProperty(CmsPropertyDefinition.PROPERTY_EXPORTNAME); 2199 } catch (CmsException e) { 2200 resources = Collections.EMPTY_LIST; 2201 } 2202 2203 synchronized (m_lockSetExportnames) { 2204 m_exportnameResources = new HashMap (resources.size()); 2205 for (int i = 0, n = resources.size(); i < n; i++) { 2206 CmsResource res = (CmsResource)resources.get(i); 2207 try { 2208 String foldername = cms.getSitePath(res); 2209 String exportname = cms.readPropertyObject( 2210 foldername, 2211 CmsPropertyDefinition.PROPERTY_EXPORTNAME, 2212 false).getValue(); 2213 if (exportname != null) { 2214 if (exportname.charAt(exportname.length() - 1) != '/') { 2215 exportname = exportname + "/"; 2216 } 2217 if (exportname.charAt(0) != '/') { 2218 exportname = "/" + exportname; 2219 } 2220 m_exportnameResources.put(exportname, foldername); 2221 } 2222 } catch (CmsException e) { 2223 } 2225 } 2226 m_exportnameResources = Collections.unmodifiableMap(m_exportnameResources); 2227 } 2228 if (LOG.isDebugEnabled()) { 2229 LOG.debug(Messages.get().getBundle().key(Messages.LOG_UPDATE_EXPORTNAME_PROP_FINISHED_0)); 2230 } 2231 } 2232 2233 2245 private void writeResource( 2246 HttpServletRequest req, 2247 String exportPath, 2248 String rfsName, 2249 String vfsName, 2250 CmsResource resource, 2251 byte[] content) throws CmsException { 2252 2253 File exportFile = null; 2254 String exportFileName = CmsFileUtil.normalizePath(exportPath + rfsName); 2255 2256 createExportFolder(exportPath, rfsName); 2258 exportFile = new File(exportFileName); 2260 2261 if (exportFile != null) { 2262 try { 2264 FileOutputStream exportStream = new FileOutputStream (exportFile); 2265 exportStream.write(content); 2266 exportStream.close(); 2267 2268 if (LOG.isInfoEnabled()) { 2270 LOG.info(Messages.get().getBundle().key(Messages.LOG_STATIC_EXPORTED_2, vfsName, exportFileName)); 2271 } 2272 2273 } catch (Throwable t) { 2274 throw new CmsStaticExportException(Messages.get().container( 2275 Messages.ERR_OUTPUT_STREAM_1, 2276 exportFileName), t); 2277 } 2278 if (req != null) { 2280 Long dateLastModified = (Long )req.getAttribute(CmsRequestUtil.HEADER_OPENCMS_EXPORT); 2281 if ((dateLastModified != null) && (dateLastModified.longValue() != -1)) { 2282 exportFile.setLastModified((dateLastModified.longValue() / 1000) * 1000); 2283 if (LOG.isDebugEnabled()) { 2284 LOG.debug(Messages.get().getBundle().key( 2285 Messages.LOG_SET_LAST_MODIFIED_2, 2286 exportFile.getName(), 2287 new Long ((dateLastModified.longValue() / 1000) * 1000))); 2288 } 2289 } 2290 } else { 2291 exportFile.setLastModified((resource.getDateLastModified() / 1000) * 1000); 2293 } 2294 } 2295 } 2296} | Popular Tags |