📄 cmsafterpublishstaticexporthandler.java
字号:
/**
* Exports all template resources found in a list of published resources.<p>
*
* @param cms the cms context
* @param publishedTemplateResources list of potential candidates to export
* @param report an I_CmsReport instance to print output message, or null to write messages to the log file
*/
private void exportTemplateResources(CmsObject cms, List publishedTemplateResources, I_CmsReport report) {
CmsStaticExportManager manager = OpenCms.getStaticExportManager();
int size = publishedTemplateResources.size();
int count = 1;
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.get().getBundle().key(Messages.LOG_EXPORT_TEMPLATES_1, new Integer(size)));
}
report.println(
Messages.get().container(Messages.RPT_STATICEXPORT_TEMPLATE_RESOURCES_BEGIN_0),
I_CmsReport.FORMAT_HEADLINE);
// now loop through all of them and request them from the server
Iterator i = publishedTemplateResources.iterator();
String cookies = null;
while (i.hasNext()) {
String rfsName = (String)i.next();
String vfsName = manager.getVfsNameInternal(cms, rfsName);
if (vfsName == null) {
String rfsBaseName = rfsName.substring(0, rfsName.lastIndexOf('_'));
vfsName = manager.getVfsNameInternal(cms, rfsBaseName);
}
report.print(org.opencms.report.Messages.get().container(
org.opencms.report.Messages.RPT_SUCCESSION_2,
new Integer(count++),
new Integer(size)), I_CmsReport.FORMAT_NOTE);
report.print(Messages.get().container(Messages.RPT_EXPORTING_0), I_CmsReport.FORMAT_NOTE);
report.print(org.opencms.report.Messages.get().container(
org.opencms.report.Messages.RPT_ARGUMENT_1,
rfsName));
report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));
String exportUrlStr = manager.getExportUrl() + manager.getRfsPrefix(vfsName) + rfsName;
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.get().getBundle().key(Messages.LOG_SENDING_REQUEST_2, rfsName, exportUrlStr));
}
try {
// setup the connection and request the resource
URL exportUrl = new URL(exportUrlStr);
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection urlcon = (HttpURLConnection)exportUrl.openConnection();
// set request type to GET
urlcon.setRequestMethod(REQUEST_METHOD_GET);
// add special export header
urlcon.setRequestProperty(CmsRequestUtil.HEADER_OPENCMS_EXPORT, CmsStringUtil.TRUE);
// add additional headers if available
if (manager.getAcceptLanguageHeader() != null) {
urlcon.setRequestProperty(CmsRequestUtil.HEADER_ACCEPT_LANGUAGE, manager.getAcceptLanguageHeader());
} else {
urlcon.setRequestProperty(
CmsRequestUtil.HEADER_ACCEPT_LANGUAGE,
manager.getDefaultAcceptLanguageHeader());
}
if (manager.getAcceptCharsetHeader() != null) {
urlcon.setRequestProperty(CmsRequestUtil.HEADER_ACCEPT_CHARSET, manager.getAcceptCharsetHeader());
} else {
urlcon.setRequestProperty(
CmsRequestUtil.HEADER_ACCEPT_CHARSET,
manager.getDefaultAcceptCharsetHeader());
}
// get the last modified date and add it to the request
String exportFileName = CmsFileUtil.normalizePath(manager.getExportPath(vfsName) + rfsName);
File exportFile = new File(exportFileName);
long dateLastModified = exportFile.lastModified();
// system folder case
if (vfsName.startsWith(CmsWorkplace.VFS_PATH_SYSTEM)) {
// iterate over all rules
Iterator it = manager.getRfsRules().iterator();
while (it.hasNext()) {
CmsStaticExportRfsRule rule = (CmsStaticExportRfsRule)it.next();
if (rule.match(vfsName)) {
exportFileName = CmsFileUtil.normalizePath(rule.getExportPath() + rfsName);
exportFile = new File(exportFileName);
if (dateLastModified > exportFile.lastModified()) {
dateLastModified = exportFile.lastModified();
}
}
}
}
urlcon.setIfModifiedSince(dateLastModified);
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.get().getBundle().key(
Messages.LOG_IF_MODIFIED_SINCE_SET_2,
exportFile.getName(),
new Long((dateLastModified / 1000) * 1000)));
}
if (cookies != null) {
// set the cookies, included the session id to keep the same session
urlcon.setRequestProperty(REQUEST_PROPERTY_COOKIE, cookies);
}
// now perform the request
urlcon.connect();
int status = urlcon.getResponseCode();
if (cookies == null) {
//Now retrieve the cookies. The jsessionid is here
cookies = urlcon.getHeaderField(HEADER_FIELD_SET_COOKIE);
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.get().getBundle().key(Messages.LOG_STATICEXPORT_COOKIES_1, cookies));
}
}
urlcon.disconnect();
if (LOG.isInfoEnabled()) {
LOG.info(Messages.get().getBundle().key(
Messages.LOG_REQUEST_RESULT_3,
rfsName,
exportUrlStr,
new Integer(status)));
}
// write the report
if (status == HttpServletResponse.SC_OK) {
report.println(
org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
I_CmsReport.FORMAT_OK);
} else if (status == HttpServletResponse.SC_NOT_MODIFIED) {
report.println(org.opencms.report.Messages.get().container(
org.opencms.report.Messages.RPT_SKIPPED_0), I_CmsReport.FORMAT_NOTE);
} else if (status == HttpServletResponse.SC_SEE_OTHER) {
report.println(org.opencms.report.Messages.get().container(
org.opencms.report.Messages.RPT_IGNORED_0), I_CmsReport.FORMAT_NOTE);
} else {
report.println(org.opencms.report.Messages.get().container(
org.opencms.report.Messages.RPT_ARGUMENT_1,
new Integer(status)), I_CmsReport.FORMAT_OK);
}
} catch (IOException e) {
report.println(e);
}
}
report.println(
Messages.get().container(Messages.RPT_STATICEXPORT_TEMPLATE_RESOURCES_END_0),
I_CmsReport.FORMAT_HEADLINE);
}
/**
* Creates a list of <code>{@link CmsPulishedResource}</code> objects containing all related resources of the VFS tree.<p>
*
* If the static export has been triggered by the OpenCms workplace, publishedResources is null and all resources in the VFS tree are returned.<p>
* If really an after publish static export is triggered, then only the related resources are returned.<p>
*
* @param cms the current cms object
* @param publishedResources the list of published resources
*
* @return list of CmsPulishedResource objects containing all resources of the VFS tree
*
* @throws CmsException in case of errors accessing the VFS
*/
private List getRelatedResources(CmsObject cms, List publishedResources) throws CmsException {
try {
// switch to root site
cms.getRequestContext().saveSiteRoot();
cms.getRequestContext().setSiteRoot("/");
if (publishedResources == null) {
// full static export
return getAllResources(cms);
} else {
// after publish export
Set resourceSet = new HashSet();
Iterator itPubRes = publishedResources.iterator();
while (itPubRes.hasNext()) {
CmsPublishedResource pubResource = (CmsPublishedResource)itPubRes.next();
// check the internal flag if the resource does still exist
// we cannot export with an internal flag
if (cms.existsResource(pubResource.getRootPath())) {
CmsResource vfsResource = cms.readResource(pubResource.getRootPath());
if ((vfsResource.getFlags() & CmsResource.FLAG_INTERNAL) != CmsResource.FLAG_INTERNAL) {
// add only if not internal
resourceSet.add(pubResource);
}
} else {
// the resource does not exist, so add them for deletion in the static export
resourceSet.add(pubResource);
}
boolean match = false;
Iterator itExportRules = OpenCms.getStaticExportManager().getExportRules().iterator();
while (itExportRules.hasNext()) {
CmsStaticExportExportRule rule = (CmsStaticExportExportRule)itExportRules.next();
Set relatedResources = rule.getRelatedResources(cms, pubResource);
if (relatedResources != null) {
resourceSet.addAll(relatedResources);
match = true;
}
}
// if one res does not match any rule, then export all files
if (!match) {
return getAllResources(cms);
}
}
return new ArrayList(resourceSet);
}
} finally {
cms.getRequestContext().restoreSiteRoot();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -