KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > staticexport > CmsAfterPublishStaticExportHandler


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/staticexport/CmsAfterPublishStaticExportHandler.java,v $
3  * Date : $Date: 2006/10/18 15:02:25 $
4  * Version: $Revision: 1.22 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

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 JavaDoc;
51 import java.io.IOException JavaDoc;
52 import java.net.HttpURLConnection JavaDoc;
53 import java.net.URL JavaDoc;
54 import java.util.ArrayList JavaDoc;
55 import java.util.Collections JavaDoc;
56 import java.util.HashSet JavaDoc;
57 import java.util.Iterator JavaDoc;
58 import java.util.List JavaDoc;
59 import java.util.Set JavaDoc;
60
61 import javax.servlet.ServletException JavaDoc;
62 import javax.servlet.http.HttpServletResponse JavaDoc;
63
64 import org.apache.commons.logging.Log;
65
66 /**
67  * Implementation for the <code>{@link I_CmsStaticExportHandler}</code> interface.<p>
68  *
69  * This handler exports all changes immediately after something is published.<p>
70  *
71  * @author Michael Moossen
72  *
73  * @version $Revision: 1.22 $
74  *
75  * @since 6.0.0
76  *
77  * @see I_CmsStaticExportHandler
78  */

79 public class CmsAfterPublishStaticExportHandler extends A_CmsStaticExportHandler implements I_CmsStaticExportHandler {
80
81     /** Header field set-cookie constant. */
82     private static final String JavaDoc HEADER_FIELD_SET_COOKIE = "Set-Cookie";
83
84     /** The log object for this class. */
85     private static final Log LOG = CmsLog.getLog(CmsAfterPublishStaticExportHandler.class);
86
87     /** Request method get constant. */
88     private static final String JavaDoc REQUEST_METHOD_GET = "GET";
89
90     /** Request property cookie constant. */
91     private static final String JavaDoc REQUEST_PROPERTY_COOKIE = "Cookie";
92
93     /**
94      * Does the actual static export.<p>
95      *
96      * @param resources a list of CmsPublishedREsources to start the static export with
97      * @param report an <code>{@link I_CmsReport}</code> instance to print output message, or <code>null</code> to write messages to the log file
98      *
99      * @throws CmsException in case of errors accessing the VFS
100      * @throws IOException in case of erros writing to the export output stream
101      * @throws ServletException in case of errors accessing the servlet
102      */

103     public void doExportAfterPublish(List JavaDoc resources, I_CmsReport report)
104     throws CmsException, IOException JavaDoc, ServletException JavaDoc {
105
106         boolean templatesFound;
107
108         // export must be done in the context of the export user
109
CmsObject cmsExportObject = OpenCms.initCmsObject(OpenCms.getDefaultUsers().getUserExport());
110
111         List JavaDoc resourcesToExport = getRelatedResources(cmsExportObject, resources);
112         // first export all non-template resources
113
templatesFound = exportNonTemplateResources(cmsExportObject, resourcesToExport, report);
114
115         // export template resourses (check "plainoptimization" setting)
116
if ((templatesFound) || (!OpenCms.getStaticExportManager().getQuickPlainExport())) {
117             CmsStaticExportManager manager = OpenCms.getStaticExportManager();
118
119             // build resource filter set
120
Set JavaDoc resourceFilter = new HashSet JavaDoc();
121             Iterator JavaDoc itExpRes = resourcesToExport.iterator();
122             while (itExpRes.hasNext()) {
123                 CmsPublishedResource pubResource = (CmsPublishedResource)itExpRes.next();
124                 String JavaDoc rfsName = manager.getRfsName(cmsExportObject, pubResource.getRootPath());
125                 resourceFilter.add(rfsName.substring(manager.getRfsPrefixForRfsName(rfsName).length()));
126             }
127
128             long timestamp = 0;
129             List JavaDoc publishedTemplateResources;
130             boolean newTemplateLinksFound;
131             int linkMode = CmsStaticExportManager.EXPORT_LINK_WITHOUT_PARAMETER;
132             do {
133                 // get all template resources which are potential candidates for a static export
134
publishedTemplateResources = cmsExportObject.readStaticExportResources(linkMode, timestamp);
135                 newTemplateLinksFound = publishedTemplateResources.size() > 0;
136                 if (newTemplateLinksFound) {
137                     if (linkMode == CmsStaticExportManager.EXPORT_LINK_WITHOUT_PARAMETER) {
138                         // first loop, switch mode to parameter links, leave the timestamp unchanged
139
linkMode = CmsStaticExportManager.EXPORT_LINK_WITH_PARAMETER;
140                         // filter without parameter
141
publishedTemplateResources.retainAll(resourceFilter);
142                     } else {
143                         // second and subsequent loops, only look for links not already exported
144
// this can only be the case for a link with parameters
145
// that was present on a page also generated with parameters
146
timestamp = System.currentTimeMillis();
147                         // filter with parameter
148
Iterator JavaDoc itPubTemplates = publishedTemplateResources.iterator();
149                         while (itPubTemplates.hasNext()) {
150                             String JavaDoc rfsName = (String JavaDoc)itPubTemplates.next();
151                             if (!resourceFilter.contains(rfsName.substring(0, rfsName.lastIndexOf('_')))) {
152                                 itPubTemplates.remove();
153                             }
154                         }
155                     }
156                     // leave if no template left
157
if (publishedTemplateResources == null || publishedTemplateResources.isEmpty()) {
158                         break;
159                     }
160                     // export
161
exportTemplateResources(cmsExportObject, publishedTemplateResources, report);
162                 }
163                 // if no new template links where found we are finished
164
} while (newTemplateLinksFound);
165         }
166     }
167
168     /**
169      * Gets all resources within the folder tree.<p>
170      * Since the long min and max value do not work with the sql timestamp function in the driver, we must calculate
171      * some different, but usable start and endtime values first.<p>
172      *
173      * @param cms the cms context
174      *
175      * @return all resources within the folder tree
176      *
177      * @throws CmsException if something goes wrong
178      */

179     public List JavaDoc 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         // read all from the root path, exclude resources flagged as internal
185
List JavaDoc vfsResources = cms.readResources("/", CmsResourceFilter.ALL.addExcludeFlags(CmsResource.FLAG_INTERNAL));
186         // loop through the list and create the list of CmsPublishedResources
187
List JavaDoc resources = new ArrayList JavaDoc(vfsResources.size());
188         Iterator JavaDoc 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 JavaDoc(resources.size())));
199         }
200         return resources;
201     }
202
203     /**
204      * @see org.opencms.staticexport.I_CmsStaticExportHandler#performEventPublishProject(org.opencms.util.CmsUUID, org.opencms.report.I_CmsReport)
205      */

206     public void performEventPublishProject(CmsUUID publishHistoryId, I_CmsReport report) {
207
208         try {
209             m_busy = true;
210             exportAfterPublish(publishHistoryId, report);
211         } catch (Throwable JavaDoc 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     /**
219      * @see org.opencms.staticexport.A_CmsOnDemandStaticExportHandler#getRelatedFilesToPurge(java.lang.String, java.lang.String)
220      */

221     protected List JavaDoc getRelatedFilesToPurge(String JavaDoc exportFileName, String JavaDoc vfsName) {
222
223         return Collections.EMPTY_LIST;
224     }
225
226     /**
227      * Starts the static export on publish.<p>
228      *
229      * Exports all modified resources after a publish process into the real FS.<p>
230      *
231      * @param publishHistoryId the publichHistoryId of the published project
232      * @param report an <code>{@link I_CmsReport}</code> instance to print output message, or <code>null</code> to write messages to the log file
233      *
234      * @throws CmsException in case of errors accessing the VFS
235      * @throws IOException in case of erros writing to the export output stream
236      * @throws ServletException in case of errors accessing the servlet
237      */

238     private void exportAfterPublish(CmsUUID publishHistoryId, I_CmsReport report)
239     throws CmsException, IOException JavaDoc, ServletException JavaDoc {
240
241         // first check if the test resource was published already
242
// if not, we must do a complete export of all static resources
243
String JavaDoc 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 JavaDoc file = new File JavaDoc(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             // the file is not there, so export everything
256
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             // delete all resources deleted during the publish process
263
scrubExportFolders(publishHistoryId);
264
265             // get the list of published resources from the publish history table
266
CmsObject cms = OpenCms.initCmsObject(OpenCms.getDefaultUsers().getUserExport());
267             List JavaDoc publishedResources = cms.readPublishedResources(publishHistoryId);
268
269             // do the export
270
doExportAfterPublish(publishedResources, report);
271         }
272
273     }
274
275     /**
276      * Exports all non template resources found in a list of published resources.<p>
277      *
278      * @param cms the current cms object
279      * @param publishedResources the list of published resources
280      * @param report an I_CmsReport instance to print output message, or null to write messages to the log file
281      *
282      * @return true if some template resources were found whil looping the list of published resources
283      *
284      * @throws CmsException in case of errors accessing the VFS
285      * @throws IOException in case of erros writing to the export output stream
286      * @throws ServletException in case of errors accessing the servlet
287      */

288     private boolean exportNonTemplateResources(CmsObject cms, List JavaDoc publishedResources, I_CmsReport report)
289     throws CmsException, IOException JavaDoc, ServletException JavaDoc {
290
291         CmsStaticExportManager manager = OpenCms.getStaticExportManager();
292         String JavaDoc vfsName = null;
293         List JavaDoc resourcesToExport = new ArrayList JavaDoc();
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         // loop through all resources
303
Iterator JavaDoc 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 JavaDoc(publishedResources.size())));
309         }
310
311         while (i.hasNext()) {
312             CmsPublishedResource pupRes = (CmsPublishedResource)i.next();
313
314             vfsName = pupRes.getRootPath();
315
316             // only process this resource, if it is within the tree of allowed folders for static export
317
if (manager.getExportFolderMatcher().match(vfsName)) {
318
319                 // only export VFS files, other data is handled elsewhere
320
if (pupRes.isVfsResource()) {
321                     // get the export data object, if null is returned, this resource cannot be exported
322
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                             // the resource is a folder, check if PROPERTY_DEFAULT_FILE is set on folder
329
try {
330                                 String JavaDoc 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                                 // resource is (still) a folder, check default files specified in configuration
339
for (int j = 0; j < OpenCms.getDefaultFiles().size(); j++) {
340                                     String JavaDoc tmpResourceName = vfsName + OpenCms.getDefaultFiles().get(j);
341                                     try {
342                                         resource = cms.readResource(tmpResourceName);
343                                         break;
344                                     } catch (CmsException e1) {
345                                         // ignore all other exceptions and continue the lookup process
346
}
347                                 }
348                             }
349                         }
350                         if (resource != null) {
351                             // check loader for current resource if it must be processed before exported
352
I_CmsResourceLoader loader = OpenCms.getResourceManager().getLoader(resource);
353                             if (!loader.isStaticExportProcessable()) {
354                                 // this resource must not be processed, so export it (if it's not marked as deleted)
355
if (pupRes.getState() != CmsResource.STATE_DELETED) {
356                                     // mark the resource for export to the real file system
357
resourcesToExport.add(exportData);
358                                 }
359                             } else {
360                                 // the resource is a template resource or a folder, so store the name of it in the DB for further use
361
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         // now do the export
375
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 JavaDoc(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 JavaDoc(count++),
393                 new Integer JavaDoc(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 JavaDoc[] arguments = new Object JavaDoc[] {
412                     exportData.getVfsName(),
413                     exportData.getRfsName(),
414                     new Integer JavaDoc(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     /**
429      * Exports all template resources found in a list of published resources.<p>
430      *
431      * @param cms the cms context
432      * @param publishedTemplateResources list of potential candidates to export
433      * @param report an I_CmsReport instance to print output message, or null to write messages to the log file
434      */

435     private void exportTemplateResources(CmsObject cms, List JavaDoc 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 JavaDoc(size)));
443         }
444         report.println(
445             Messages.get().container(Messages.RPT_STATICEXPORT_TEMPLATE_RESOURCES_BEGIN_0),
446             I_CmsReport.FORMAT_HEADLINE);
447
448         // now loop through all of them and request them from the server
449
Iterator JavaDoc i = publishedTemplateResources.iterator();
450
451         String JavaDoc cookies = null;
452         while (i.hasNext()) {
453             String JavaDoc rfsName = (String JavaDoc)i.next();
454             String JavaDoc vfsName = manager.getVfsNameInternal(cms, rfsName);
455             if (vfsName == null) {
456                 String JavaDoc 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 JavaDoc(count++),
463                 new Integer JavaDoc(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 JavaDoc 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                 // setup the connection and request the resource
478
URL JavaDoc exportUrl = new URL JavaDoc(exportUrlStr);
479                 HttpURLConnection.setFollowRedirects(false);
480                 HttpURLConnection JavaDoc urlcon = (HttpURLConnection JavaDoc)exportUrl.openConnection();
481                 // set request type to GET
482
urlcon.setRequestMethod(REQUEST_METHOD_GET);
483                 // add special export header
484
urlcon.setRequestProperty(CmsRequestUtil.HEADER_OPENCMS_EXPORT, CmsStringUtil.TRUE);
485                 // add additional headers if available
486
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                 // get the last modified date and add it to the request
502
String JavaDoc exportFileName = CmsFileUtil.normalizePath(manager.getExportPath(vfsName) + rfsName);
503                 File JavaDoc exportFile = new File JavaDoc(exportFileName);
504                 long dateLastModified = exportFile.lastModified();
505                 // system folder case
506
if (vfsName.startsWith(CmsWorkplace.VFS_PATH_SYSTEM)) {
507                     // iterate over all rules
508
Iterator JavaDoc 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 JavaDoc(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 JavaDoc((dateLastModified / 1000) * 1000)));
526                 }
527                 if (cookies != null) {
528                     // set the cookies, included the session id to keep the same session
529
urlcon.setRequestProperty(REQUEST_PROPERTY_COOKIE, cookies);
530                 }
531
532                 // now perform the request
533
urlcon.connect();
534                 int status = urlcon.getResponseCode();
535
536                 if (cookies == null) {
537                     //Now retrieve the cookies. The jsessionid is here
538
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 JavaDoc(status)));
550                 }
551
552                 // write the report
553
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 JavaDoc(status)), I_CmsReport.FORMAT_OK);
567                 }
568             } catch (IOException JavaDoc 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     /**
578      * Creates a list of <code>{@link CmsPulishedResource}</code> objects containing all related resources of the VFS tree.<p>
579      *
580      * If the static export has been triggered by the OpenCms workplace, publishedResources is null and all resources in the VFS tree are returned.<p>
581      * If really an after publish static export is triggered, then only the related resources are returned.<p>
582      *
583      * @param cms the current cms object
584      * @param publishedResources the list of published resources
585      *
586      * @return list of CmsPulishedResource objects containing all resources of the VFS tree
587      *
588      * @throws CmsException in case of errors accessing the VFS
589      */

590     private List JavaDoc getRelatedResources(CmsObject cms, List JavaDoc publishedResources) throws CmsException {
591
592         try {
593             // switch to root site
594
cms.getRequestContext().saveSiteRoot();
595             cms.getRequestContext().setSiteRoot("/");
596             if (publishedResources == null) {
597                 // full static export
598
return getAllResources(cms);
599             } else {
600                 // after publish export
601
Set JavaDoc resourceSet = new HashSet JavaDoc();
602                 Iterator JavaDoc itPubRes = publishedResources.iterator();
603                 while (itPubRes.hasNext()) {
604                     CmsPublishedResource pubResource = (CmsPublishedResource)itPubRes.next();
605                     // check the internal flag if the resource does still exist
606
// we cannot export with an internal flag
607
if (cms.existsResource(pubResource.getRootPath())) {
608                         CmsResource vfsResource = cms.readResource(pubResource.getRootPath());
609                         if ((vfsResource.getFlags() & CmsResource.FLAG_INTERNAL) != CmsResource.FLAG_INTERNAL) {
610                             // add only if not internal
611
resourceSet.add(pubResource);
612                         }
613                     } else {
614                         // the resource does not exist, so add them for deletion in the static export
615
resourceSet.add(pubResource);
616                     }
617                     boolean match = false;
618                     Iterator JavaDoc itExportRules = OpenCms.getStaticExportManager().getExportRules().iterator();
619                     while (itExportRules.hasNext()) {
620                         CmsStaticExportExportRule rule = (CmsStaticExportExportRule)itExportRules.next();
621                         Set JavaDoc relatedResources = rule.getRelatedResources(cms, pubResource);
622                         if (relatedResources != null) {
623                             resourceSet.addAll(relatedResources);
624                             match = true;
625                         }
626                     }
627                     // if one res does not match any rule, then export all files
628
if (!match) {
629                         return getAllResources(cms);
630                     }
631                 }
632                 return new ArrayList JavaDoc(resourceSet);
633             }
634         } finally {
635             cms.getRequestContext().restoreSiteRoot();
636         }
637     }
638 }
Popular Tags