1 31 32 package org.opencms.staticexport; 33 34 import org.opencms.db.CmsPublishedResource; 35 import org.opencms.file.CmsObject; 36 import org.opencms.file.CmsPropertyDefinition; 37 import org.opencms.file.CmsResource; 38 import org.opencms.file.CmsResourceFilter; 39 import org.opencms.loader.I_CmsResourceLoader; 40 import org.opencms.main.CmsException; 41 import org.opencms.main.CmsLog; 42 import org.opencms.main.OpenCms; 43 import org.opencms.report.I_CmsReport; 44 import org.opencms.util.CmsFileUtil; 45 import org.opencms.util.CmsRequestUtil; 46 import org.opencms.util.CmsStringUtil; 47 import org.opencms.util.CmsUUID; 48 import org.opencms.workplace.CmsWorkplace; 49 50 import java.io.File ; 51 import java.io.IOException ; 52 import java.net.HttpURLConnection ; 53 import java.net.URL ; 54 import java.util.ArrayList ; 55 import java.util.Collections ; 56 import java.util.HashSet ; 57 import java.util.Iterator ; 58 import java.util.List ; 59 import java.util.Set ; 60 61 import javax.servlet.ServletException ; 62 import javax.servlet.http.HttpServletResponse ; 63 64 import org.apache.commons.logging.Log; 65 66 79 public class CmsAfterPublishStaticExportHandler extends A_CmsStaticExportHandler implements I_CmsStaticExportHandler { 80 81 82 private static final String HEADER_FIELD_SET_COOKIE = "Set-Cookie"; 83 84 85 private static final Log LOG = CmsLog.getLog(CmsAfterPublishStaticExportHandler.class); 86 87 88 private static final String REQUEST_METHOD_GET = "GET"; 89 90 91 private static final String REQUEST_PROPERTY_COOKIE = "Cookie"; 92 93 103 public void doExportAfterPublish(List resources, I_CmsReport report) 104 throws CmsException, IOException , ServletException { 105 106 boolean templatesFound; 107 108 CmsObject cmsExportObject = OpenCms.initCmsObject(OpenCms.getDefaultUsers().getUserExport()); 110 111 List resourcesToExport = getRelatedResources(cmsExportObject, resources); 112 templatesFound = exportNonTemplateResources(cmsExportObject, resourcesToExport, report); 114 115 if ((templatesFound) || (!OpenCms.getStaticExportManager().getQuickPlainExport())) { 117 CmsStaticExportManager manager = OpenCms.getStaticExportManager(); 118 119 Set resourceFilter = new HashSet (); 121 Iterator itExpRes = resourcesToExport.iterator(); 122 while (itExpRes.hasNext()) { 123 CmsPublishedResource pubResource = (CmsPublishedResource)itExpRes.next(); 124 String rfsName = manager.getRfsName(cmsExportObject, pubResource.getRootPath()); 125 resourceFilter.add(rfsName.substring(manager.getRfsPrefixForRfsName(rfsName).length())); 126 } 127 128 long timestamp = 0; 129 List publishedTemplateResources; 130 boolean newTemplateLinksFound; 131 int linkMode = CmsStaticExportManager.EXPORT_LINK_WITHOUT_PARAMETER; 132 do { 133 publishedTemplateResources = cmsExportObject.readStaticExportResources(linkMode, timestamp); 135 newTemplateLinksFound = publishedTemplateResources.size() > 0; 136 if (newTemplateLinksFound) { 137 if (linkMode == CmsStaticExportManager.EXPORT_LINK_WITHOUT_PARAMETER) { 138 linkMode = CmsStaticExportManager.EXPORT_LINK_WITH_PARAMETER; 140 publishedTemplateResources.retainAll(resourceFilter); 142 } else { 143 timestamp = System.currentTimeMillis(); 147 Iterator itPubTemplates = publishedTemplateResources.iterator(); 149 while (itPubTemplates.hasNext()) { 150 String rfsName = (String )itPubTemplates.next(); 151 if (!resourceFilter.contains(rfsName.substring(0, rfsName.lastIndexOf('_')))) { 152 itPubTemplates.remove(); 153 } 154 } 155 } 156 if (publishedTemplateResources == null || publishedTemplateResources.isEmpty()) { 158 break; 159 } 160 exportTemplateResources(cmsExportObject, publishedTemplateResources, report); 162 } 163 } while (newTemplateLinksFound); 165 } 166 } 167 168 179 public List getAllResources(CmsObject cms) throws CmsException { 180 181 if (LOG.isDebugEnabled()) { 182 LOG.debug(Messages.get().getBundle().key(Messages.LOG_GET_ALL_RESOURCES_0)); 183 } 184 List vfsResources = cms.readResources("/", CmsResourceFilter.ALL.addExcludeFlags(CmsResource.FLAG_INTERNAL)); 186 List resources = new ArrayList (vfsResources.size()); 188 Iterator i = vfsResources.iterator(); 189 while (i.hasNext()) { 190 CmsPublishedResource resource = new CmsPublishedResource((CmsResource)i.next()); 191 if (LOG.isDebugEnabled()) { 192 LOG.debug(Messages.get().getBundle().key(Messages.LOG_PROCESSING_1, resource.getRootPath())); 193 } 194 resources.add(resource); 195 } 196 197 if (LOG.isDebugEnabled()) { 198 LOG.debug(Messages.get().getBundle().key(Messages.LOG_NUM_RESOURCES_1, new Integer (resources.size()))); 199 } 200 return resources; 201 } 202 203 206 public void performEventPublishProject(CmsUUID publishHistoryId, I_CmsReport report) { 207 208 try { 209 m_busy = true; 210 exportAfterPublish(publishHistoryId, report); 211 } catch (Throwable t) { 212 LOG.error(Messages.get().getBundle().key(Messages.LOG_STATIC_EXPORT_ERROR_0), t); 213 } finally { 214 m_busy = false; 215 } 216 } 217 218 221 protected List getRelatedFilesToPurge(String exportFileName, String vfsName) { 222 223 return Collections.EMPTY_LIST; 224 } 225 226 238 private void exportAfterPublish(CmsUUID publishHistoryId, I_CmsReport report) 239 throws CmsException, IOException , ServletException { 240 241 String rfsName = CmsFileUtil.normalizePath(OpenCms.getStaticExportManager().getExportPath( 244 OpenCms.getStaticExportManager().getTestResource()) 245 + OpenCms.getStaticExportManager().getTestResource()); 246 247 if (LOG.isDebugEnabled()) { 248 LOG.debug(Messages.get().getBundle().key(Messages.LOG_CHECKING_TEST_RESOURCE_1, rfsName)); 249 } 250 File file = new File (rfsName); 251 if (!file.exists()) { 252 if (LOG.isDebugEnabled()) { 253 LOG.debug(Messages.get().getBundle().key(Messages.LOG_TEST_RESOURCE_NOT_EXISTANT_0)); 254 } 255 OpenCms.getStaticExportManager().exportFullStaticRender(true, report); 257 } else { 258 if (LOG.isDebugEnabled()) { 259 LOG.debug(Messages.get().getBundle().key(Messages.LOG_TEST_RESOURCE_EXISTS_0)); 260 } 261 262 scrubExportFolders(publishHistoryId); 264 265 CmsObject cms = OpenCms.initCmsObject(OpenCms.getDefaultUsers().getUserExport()); 267 List publishedResources = cms.readPublishedResources(publishHistoryId); 268 269 doExportAfterPublish(publishedResources, report); 271 } 272 273 } 274 275 288 private boolean exportNonTemplateResources(CmsObject cms, List publishedResources, I_CmsReport report) 289 throws CmsException, IOException , ServletException { 290 291 CmsStaticExportManager manager = OpenCms.getStaticExportManager(); 292 String vfsName = null; 293 List resourcesToExport = new ArrayList (); 294 boolean templatesFound = false; 295 296 int count = 1; 297 298 report.println( 299 Messages.get().container(Messages.RPT_STATICEXPORT_NONTEMPLATE_RESOURCES_BEGIN_0), 300 I_CmsReport.FORMAT_HEADLINE); 301 302 Iterator i = publishedResources.iterator(); 304 305 if (LOG.isDebugEnabled()) { 306 LOG.debug(Messages.get().getBundle().key( 307 Messages.LOG_EXPORTING_NON_TEMPLATE_1, 308 new Integer (publishedResources.size()))); 309 } 310 311 while (i.hasNext()) { 312 CmsPublishedResource pupRes = (CmsPublishedResource)i.next(); 313 314 vfsName = pupRes.getRootPath(); 315 316 if (manager.getExportFolderMatcher().match(vfsName)) { 318 319 if (pupRes.isVfsResource()) { 321 CmsStaticExportData exportData = manager.getExportData(vfsName, cms); 323 if (exportData != null) { 324 CmsResource resource = null; 325 if (pupRes.isFile()) { 326 resource = exportData.getResource(); 327 } else { 328 try { 330 String defaultFileName = cms.readPropertyObject( 331 vfsName, 332 CmsPropertyDefinition.PROPERTY_DEFAULT_FILE, 333 false).getValue(); 334 if (defaultFileName != null) { 335 resource = cms.readResource(vfsName + defaultFileName); 336 } 337 } catch (CmsException e) { 338 for (int j = 0; j < OpenCms.getDefaultFiles().size(); j++) { 340 String tmpResourceName = vfsName + OpenCms.getDefaultFiles().get(j); 341 try { 342 resource = cms.readResource(tmpResourceName); 343 break; 344 } catch (CmsException e1) { 345 } 347 } 348 } 349 } 350 if (resource != null) { 351 I_CmsResourceLoader loader = OpenCms.getResourceManager().getLoader(resource); 353 if (!loader.isStaticExportProcessable()) { 354 if (pupRes.getState() != CmsResource.STATE_DELETED) { 356 resourcesToExport.add(exportData); 358 } 359 } else { 360 templatesFound = true; 362 cms.writeStaticExportPublishedResource( 363 exportData.getRfsName(), 364 CmsStaticExportManager.EXPORT_LINK_WITHOUT_PARAMETER, 365 "", 366 System.currentTimeMillis()); 367 } 368 } 369 } 370 } 371 } 372 } 373 374 i = resourcesToExport.iterator(); 376 int size = resourcesToExport.size(); 377 378 if (LOG.isDebugEnabled()) { 379 LOG.debug(Messages.get().getBundle().key(Messages.LOG_NUM_EXPORT_1, new Integer (size))); 380 } 381 while (i.hasNext()) { 382 CmsStaticExportData exportData = (CmsStaticExportData)i.next(); 383 if (LOG.isDebugEnabled()) { 384 LOG.debug(Messages.get().getBundle().key( 385 Messages.LOG_EXPORT_FILE_2, 386 exportData.getVfsName(), 387 exportData.getRfsName())); 388 } 389 390 report.print(org.opencms.report.Messages.get().container( 391 org.opencms.report.Messages.RPT_SUCCESSION_2, 392 new Integer (count++), 393 new Integer (size)), I_CmsReport.FORMAT_NOTE); 394 report.print(Messages.get().container(Messages.RPT_EXPORTING_0), I_CmsReport.FORMAT_NOTE); 395 report.print(org.opencms.report.Messages.get().container( 396 org.opencms.report.Messages.RPT_ARGUMENT_1, 397 exportData.getVfsName())); 398 report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0)); 399 int status = manager.export(null, null, cms, exportData); 400 if (status == HttpServletResponse.SC_OK) { 401 report.println( 402 org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0), 403 I_CmsReport.FORMAT_OK); 404 } else { 405 report.println( 406 org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_IGNORED_0), 407 I_CmsReport.FORMAT_NOTE); 408 } 409 410 if (LOG.isInfoEnabled()) { 411 Object [] arguments = new Object [] { 412 exportData.getVfsName(), 413 exportData.getRfsName(), 414 new Integer (status)}; 415 LOG.info(Messages.get().getBundle().key(Messages.LOG_EXPORT_FILE_STATUS_3, arguments)); 416 } 417 } 418 419 resourcesToExport = null; 420 421 report.println( 422 Messages.get().container(Messages.RPT_STATICEXPORT_NONTEMPLATE_RESOURCES_END_0), 423 I_CmsReport.FORMAT_HEADLINE); 424 425 return templatesFound; 426 } 427 428 435 private void exportTemplateResources(CmsObject cms, List publishedTemplateResources, I_CmsReport report) { 436 437 CmsStaticExportManager manager = OpenCms.getStaticExportManager(); 438 int size = publishedTemplateResources.size(); 439 int count = 1; 440 441 if (LOG.isDebugEnabled()) { 442 LOG.debug(Messages.get().getBundle().key(Messages.LOG_EXPORT_TEMPLATES_1, new Integer (size))); 443 } 444 report.println( 445 Messages.get().container(Messages.RPT_STATICEXPORT_TEMPLATE_RESOURCES_BEGIN_0), 446 I_CmsReport.FORMAT_HEADLINE); 447 448 Iterator i = publishedTemplateResources.iterator(); 450 451 String cookies = null; 452 while (i.hasNext()) { 453 String rfsName = (String )i.next(); 454 String vfsName = manager.getVfsNameInternal(cms, rfsName); 455 if (vfsName == null) { 456 String rfsBaseName = rfsName.substring(0, rfsName.lastIndexOf('_')); 457 vfsName = manager.getVfsNameInternal(cms, rfsBaseName); 458 } 459 460 report.print(org.opencms.report.Messages.get().container( 461 org.opencms.report.Messages.RPT_SUCCESSION_2, 462 new Integer (count++), 463 new Integer (size)), I_CmsReport.FORMAT_NOTE); 464 report.print(Messages.get().container(Messages.RPT_EXPORTING_0), I_CmsReport.FORMAT_NOTE); 465 report.print(org.opencms.report.Messages.get().container( 466 org.opencms.report.Messages.RPT_ARGUMENT_1, 467 rfsName)); 468 report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0)); 469 470 String exportUrlStr = manager.getExportUrl() + manager.getRfsPrefix(vfsName) + rfsName; 471 472 if (LOG.isDebugEnabled()) { 473 LOG.debug(Messages.get().getBundle().key(Messages.LOG_SENDING_REQUEST_2, rfsName, exportUrlStr)); 474 } 475 476 try { 477 URL exportUrl = new URL (exportUrlStr); 479 HttpURLConnection.setFollowRedirects(false); 480 HttpURLConnection urlcon = (HttpURLConnection )exportUrl.openConnection(); 481 urlcon.setRequestMethod(REQUEST_METHOD_GET); 483 urlcon.setRequestProperty(CmsRequestUtil.HEADER_OPENCMS_EXPORT, CmsStringUtil.TRUE); 485 if (manager.getAcceptLanguageHeader() != null) { 487 urlcon.setRequestProperty(CmsRequestUtil.HEADER_ACCEPT_LANGUAGE, manager.getAcceptLanguageHeader()); 488 } else { 489 urlcon.setRequestProperty( 490 CmsRequestUtil.HEADER_ACCEPT_LANGUAGE, 491 manager.getDefaultAcceptLanguageHeader()); 492 } 493 if (manager.getAcceptCharsetHeader() != null) { 494 urlcon.setRequestProperty(CmsRequestUtil.HEADER_ACCEPT_CHARSET, manager.getAcceptCharsetHeader()); 495 } else { 496 urlcon.setRequestProperty( 497 CmsRequestUtil.HEADER_ACCEPT_CHARSET, 498 manager.getDefaultAcceptCharsetHeader()); 499 } 500 501 String exportFileName = CmsFileUtil.normalizePath(manager.getExportPath(vfsName) + rfsName); 503 File exportFile = new File (exportFileName); 504 long dateLastModified = exportFile.lastModified(); 505 if (vfsName.startsWith(CmsWorkplace.VFS_PATH_SYSTEM)) { 507 Iterator it = manager.getRfsRules().iterator(); 509 while (it.hasNext()) { 510 CmsStaticExportRfsRule rule = (CmsStaticExportRfsRule)it.next(); 511 if (rule.match(vfsName)) { 512 exportFileName = CmsFileUtil.normalizePath(rule.getExportPath() + rfsName); 513 exportFile = new File (exportFileName); 514 if (dateLastModified > exportFile.lastModified()) { 515 dateLastModified = exportFile.lastModified(); 516 } 517 } 518 } 519 } 520 urlcon.setIfModifiedSince(dateLastModified); 521 if (LOG.isDebugEnabled()) { 522 LOG.debug(Messages.get().getBundle().key( 523 Messages.LOG_IF_MODIFIED_SINCE_SET_2, 524 exportFile.getName(), 525 new Long ((dateLastModified / 1000) * 1000))); 526 } 527 if (cookies != null) { 528 urlcon.setRequestProperty(REQUEST_PROPERTY_COOKIE, cookies); 530 } 531 532 urlcon.connect(); 534 int status = urlcon.getResponseCode(); 535 536 if (cookies == null) { 537 cookies = urlcon.getHeaderField(HEADER_FIELD_SET_COOKIE); 539 if (LOG.isDebugEnabled()) { 540 LOG.debug(Messages.get().getBundle().key(Messages.LOG_STATICEXPORT_COOKIES_1, cookies)); 541 } 542 } 543 urlcon.disconnect(); 544 if (LOG.isInfoEnabled()) { 545 LOG.info(Messages.get().getBundle().key( 546 Messages.LOG_REQUEST_RESULT_3, 547 rfsName, 548 exportUrlStr, 549 new Integer (status))); 550 } 551 552 if (status == HttpServletResponse.SC_OK) { 554 report.println( 555 org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0), 556 I_CmsReport.FORMAT_OK); 557 } else if (status == HttpServletResponse.SC_NOT_MODIFIED) { 558 report.println(org.opencms.report.Messages.get().container( 559 org.opencms.report.Messages.RPT_SKIPPED_0), I_CmsReport.FORMAT_NOTE); 560 } else if (status == HttpServletResponse.SC_SEE_OTHER) { 561 report.println(org.opencms.report.Messages.get().container( 562 org.opencms.report.Messages.RPT_IGNORED_0), I_CmsReport.FORMAT_NOTE); 563 } else { 564 report.println(org.opencms.report.Messages.get().container( 565 org.opencms.report.Messages.RPT_ARGUMENT_1, 566 new Integer (status)), I_CmsReport.FORMAT_OK); 567 } 568 } catch (IOException e) { 569 report.println(e); 570 } 571 } 572 report.println( 573 Messages.get().container(Messages.RPT_STATICEXPORT_TEMPLATE_RESOURCES_END_0), 574 I_CmsReport.FORMAT_HEADLINE); 575 } 576 577 590 private List getRelatedResources(CmsObject cms, List publishedResources) throws CmsException { 591 592 try { 593 cms.getRequestContext().saveSiteRoot(); 595 cms.getRequestContext().setSiteRoot("/"); 596 if (publishedResources == null) { 597 return getAllResources(cms); 599 } else { 600 Set resourceSet = new HashSet (); 602 Iterator itPubRes = publishedResources.iterator(); 603 while (itPubRes.hasNext()) { 604 CmsPublishedResource pubResource = (CmsPublishedResource)itPubRes.next(); 605 if (cms.existsResource(pubResource.getRootPath())) { 608 CmsResource vfsResource = cms.readResource(pubResource.getRootPath()); 609 if ((vfsResource.getFlags() & CmsResource.FLAG_INTERNAL) != CmsResource.FLAG_INTERNAL) { 610 resourceSet.add(pubResource); 612 } 613 } else { 614 resourceSet.add(pubResource); 616 } 617 boolean match = false; 618 Iterator itExportRules = OpenCms.getStaticExportManager().getExportRules().iterator(); 619 while (itExportRules.hasNext()) { 620 CmsStaticExportExportRule rule = (CmsStaticExportExportRule)itExportRules.next(); 621 Set relatedResources = rule.getRelatedResources(cms, pubResource); 622 if (relatedResources != null) { 623 resourceSet.addAll(relatedResources); 624 match = true; 625 } 626 } 627 if (!match) { 629 return getAllResources(cms); 630 } 631 } 632 return new ArrayList (resourceSet); 633 } 634 } finally { 635 cms.getRequestContext().restoreSiteRoot(); 636 } 637 } 638 } | Popular Tags |