📄 cmsstaticexportmanager.java
字号:
* Caches a calculated online link.<p>
*
* @param linkName the link
* @param vfsName the name of the VFS resource
*/
public void cacheOnlineLink(Object linkName, Object vfsName) {
m_cacheOnlineLinks.put(linkName, vfsName);
}
/**
* Implements the CmsEvent interface,
* the static export properties uses the events to clear
* the list of cached keys in case a project is published.<p>
*
* @param event CmsEvent that has occurred
*/
public void cmsEvent(CmsEvent event) {
switch (event.getType()) {
case I_CmsEventListener.EVENT_UPDATE_EXPORTS:
scrubExportFolders(null);
clearCaches(event);
break;
case I_CmsEventListener.EVENT_PUBLISH_PROJECT:
// event data contains a list of the published resources
CmsUUID publishHistoryId = new CmsUUID((String)event.getData().get(I_CmsEventListener.KEY_PUBLISHID));
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.get().getBundle().key(Messages.LOG_EVENT_PUBLISH_PROJECT_1, publishHistoryId));
}
I_CmsReport report = (I_CmsReport)event.getData().get(I_CmsEventListener.KEY_REPORT);
if (report == null) {
report = new CmsLogReport(CmsLocaleManager.getDefaultLocale(), getClass());
}
synchronized (m_lockCmsEvent) {
getHandler().performEventPublishProject(publishHistoryId, report);
}
clearCaches(event);
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.get().getBundle().key(
Messages.LOG_EVENT_PUBLISH_PROJECT_FINISHED_1,
publishHistoryId));
}
break;
case I_CmsEventListener.EVENT_CLEAR_CACHES:
clearCaches(event);
break;
default:
// no operation
}
}
/**
* Exports the requested uri and at the same time writes the uri to the response output stream
* if required.<p>
*
* @param req the current request
* @param res the current response
* @param cms an initialized cms context (should be initialized with the "Guest" user only)
* @param data the static export data set
* @return status code of the export operation, status codes are the same as http status codes (200,303,304)
* @throws CmsException in case of errors accessing the VFS
* @throws ServletException in case of errors accessing the servlet
* @throws IOException in case of erros writing to the export output stream
* @throws CmsStaticExportException if static export is disabled
*/
public int export(HttpServletRequest req, HttpServletResponse res, CmsObject cms, CmsStaticExportData data)
throws CmsException, IOException, ServletException, CmsStaticExportException {
String vfsName = data.getVfsName();
String rfsName = data.getRfsName();
CmsResource resource = data.getResource();
// cut the site root from the vfsName and switch to the correct site
String siteRoot = CmsSiteManager.getSiteRoot(vfsName);
CmsI18nInfo i18nInfo = OpenCms.getLocaleManager().getI18nInfo(
req,
cms.getRequestContext().currentUser(),
cms.getRequestContext().currentProject(),
vfsName);
String remoteAddr = m_remoteAddr;
if (remoteAddr == null) {
remoteAddr = CmsContextInfo.LOCALHOST;
}
CmsContextInfo contextInfo = new CmsContextInfo(
cms.getRequestContext().currentUser(),
cms.getRequestContext().currentProject(),
vfsName,
"/",
i18nInfo.getLocale(),
i18nInfo.getEncoding(),
remoteAddr);
cms = OpenCms.initCmsObject(null, contextInfo);
if (siteRoot != null) {
vfsName = vfsName.substring(siteRoot.length());
} else {
siteRoot = "/";
}
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.get().getBundle().key(Messages.LOG_STATIC_EXPORT_SITE_ROOT_2, siteRoot, vfsName));
}
cms.getRequestContext().setSiteRoot(siteRoot);
String oldUri = null;
// this flag signals if the export method is used for "on demand" or "after publish".
// if no request and result stream are available, it was called during "export on publish"
boolean exportOnDemand = ((req != null) && (res != null));
CmsStaticExportResponseWrapper wrapRes = null;
if (res != null) {
wrapRes = new CmsStaticExportResponseWrapper(res);
}
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.get().getBundle().key(Messages.LOG_SE_RESOURCE_START_1, data));
}
CmsFile file;
// read vfs resource
if (resource.isFile()) {
file = cms.readFile(vfsName);
} else {
file = CmsFile.upgrade(OpenCms.initResource(cms, vfsName, req, wrapRes), cms);
if (cms.existsResource(vfsName + file.getName())) {
vfsName = vfsName + file.getName();
}
rfsName += EXPORT_DEFAULT_FILE;
}
// check loader id for resource
I_CmsResourceLoader loader = OpenCms.getResourceManager().getLoader(file);
if ((loader == null) || (!loader.isStaticExportEnabled())) {
Object[] arguments = new Object[] {vfsName, new Integer(file.getTypeId())};
throw new CmsStaticExportException(Messages.get().container(Messages.ERR_EXPORT_NOT_SUPPORTED_2, arguments));
}
// ensure we have exactly the same setup as if called "the usual way"
// we only have to do this in case of the static export on demand
if (exportOnDemand) {
String mimetype = OpenCms.getResourceManager().getMimeType(
file.getName(),
cms.getRequestContext().getEncoding());
if (wrapRes != null) {
wrapRes.setContentType(mimetype);
}
oldUri = cms.getRequestContext().getUri();
cms.getRequestContext().setUri(vfsName);
}
// do the export
byte[] result = loader.export(cms, file, req, wrapRes);
// release unused resources
file = null;
int status = -1;
if (result != null) {
// normal case
String exportPath = getExportPath(siteRoot + vfsName);
// only export those resource where the export property is set
if (isExportLink(cms, vfsName)) {
writeResource(req, exportPath, rfsName, vfsName, resource, result);
// system folder case
if (vfsName.startsWith(CmsWorkplace.VFS_PATH_SYSTEM)) {
// iterate over all rules
Iterator it = getRfsRules().iterator();
while (it.hasNext()) {
CmsStaticExportRfsRule rule = (CmsStaticExportRfsRule)it.next();
if (rule.match(vfsName)) {
writeResource(req, rule.getExportPath(), rfsName, vfsName, resource, result);
}
}
}
// get the wrapper status that was set
status = (wrapRes != null) ? wrapRes.getStatus() : -1;
if (status < 0) {
// the status was not set, assume everything is o.k.
status = HttpServletResponse.SC_OK;
}
} else {
// the resource was not used for export, so return HttpServletResponse.SC_SEE_OTHER
// as a signal for not exported resource
status = HttpServletResponse.SC_SEE_OTHER;
}
} else {
// the resource was not written because it was not modified.
// set the status to not modified
status = HttpServletResponse.SC_NOT_MODIFIED;
}
// restore context
// we only have to do this in case of the static export on demand
if (exportOnDemand) {
cms.getRequestContext().setUri(oldUri);
}
return status;
}
/**
* Starts a complete static export of all resources.<p>
*
* @param purgeFirst flag to delete all resources in the export folder of the rfs
* @param report an I_CmsReport instance to print output message, or null to write messages to the log file
*
* @throws CmsException in case of errors accessing the VFS
* @throws IOException in case of erros writing to the export output stream
* @throws ServletException in case of errors accessing the servlet
*/
public synchronized void exportFullStaticRender(boolean purgeFirst, I_CmsReport report)
throws CmsException, IOException, ServletException {
// delete all old exports if the purgeFirst flag is set
if (purgeFirst) {
Map eventData = new HashMap();
eventData.put(I_CmsEventListener.KEY_REPORT, report);
CmsEvent clearCacheEvent = new CmsEvent(I_CmsEventListener.EVENT_CLEAR_CACHES, eventData);
OpenCms.fireCmsEvent(clearCacheEvent);
scrubExportFolders(report);
CmsObject cms = OpenCms.initCmsObject(OpenCms.getDefaultUsers().getUserExport());
cms.deleteAllStaticExportPublishedResources(EXPORT_LINK_WITHOUT_PARAMETER);
cms.deleteAllStaticExportPublishedResources(EXPORT_LINK_WITH_PARAMETER);
}
// do the export
CmsAfterPublishStaticExportHandler handler = new CmsAfterPublishStaticExportHandler();
// export everything
handler.doExportAfterPublish(null, report);
}
/**
* Returns the accept-charset header used for internal requests.<p>
*
* @return the accept-charset header
*/
public String getAcceptCharsetHeader() {
return m_acceptCharsetHeader;
}
/**
* Returns the accept-language header used for internal requests.<p>
*
* @return the accept-language header
*/
public String getAcceptLanguageHeader() {
return m_acceptLanguageHeader;
}
/**
* Returns a cached export data for the given rfs name.<p>
*
* Please note that this export data contains only the vfs name and the parameters,
* not the resource itself. It can therefore not directly be used as a return
* value for the static export.<p>
*
* @param rfsName the name of the ref resource to get the cached vfs resource name for
* @return a cached vfs resource name for the given rfs name, or null
*/
public CmsStaticExportData getCachedExportUri(String rfsName) {
return (CmsStaticExportData)m_cacheExportUris.get(rfsName);
}
/**
* Returns a cached link for the given vfs name.<p>
*
* @param vfsName the name of the vfs resource to get the cached link for
* @return a cached link for the given vfs name, or null
*/
public String getCachedOnlineLink(String vfsName) {
return (String)m_cacheOnlineLinks.get(vfsName);
}
/**
* Returns the key for the online, export and secure cache.<p>
*
* @param siteRoot the site root of the resource
* @param uri the URI of the resource
* @return a key for the cache
*/
public String getCacheKey(String siteRoot, String uri) {
return new StringBuffer(siteRoot).append(uri).toString();
}
/**
* Gets the default property value as a string representation.<p>
*
* @return <code>"true"</code> or <code>"false"</code>
*/
public String getDefault() {
return String.valueOf(m_exportPropertyDefault);
}
/**
* Returns the current default charset header.<p>
*
* @return the current default charset header
*/
public String getDefaultAcceptCharsetHeader() {
return m_defaultAcceptCharsetHeader;
}
/**
* Returns the current default locale header.<p>
*
* @return the current default locale header
*/
public String getDefaultAcceptLanguageHeader() {
return m_defaultAcceptLanguageHeader;
}
/**
* Returns the default prefix for exported links in the "real" file system.<p>
*
* @return the default prefix for exported links in the "real" file system
*/
public String getDefaultRfsPrefix() {
return m_rfsPrefix;
}
/**
* Returns the export data for the request, if null is returned no export is required.<p>
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -