⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmsafterpublishstaticexporthandler.java

📁 OpenCms 是一个J2EE的产品
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

            vfsName = pupRes.getRootPath();

            // only process this resource, if it is within the tree of allowed folders for static export
            if (manager.getExportFolderMatcher().match(vfsName)) {

                // only export VFS files, other data is handled elsewhere 
                if (pupRes.isVfsResource()) {
                    // get the export data object, if null is returned, this resource cannot be exported
                    CmsStaticExportData exportData = manager.getExportData(vfsName, cms);
                    if (exportData != null) {
                        CmsResource resource = null;
                        if (pupRes.isFile()) {
                            resource = exportData.getResource();
                        } else {
                            // the resource is a folder, check if PROPERTY_DEFAULT_FILE is set on folder
                            try {
                                String defaultFileName = cms.readPropertyObject(
                                    vfsName,
                                    CmsPropertyDefinition.PROPERTY_DEFAULT_FILE,
                                    false).getValue();
                                if (defaultFileName != null) {
                                    resource = cms.readResource(vfsName + defaultFileName);
                                }
                            } catch (CmsException e) {
                                // resource is (still) a folder, check default files specified in configuration
                                for (int j = 0; j < OpenCms.getDefaultFiles().size(); j++) {
                                    String tmpResourceName = vfsName + OpenCms.getDefaultFiles().get(j);
                                    try {
                                        resource = cms.readResource(tmpResourceName);
                                        break;
                                    } catch (CmsException e1) {
                                        // ignore all other exceptions and continue the lookup process
                                    }
                                }
                            }
                        }
                        if (resource != null) {
                            // check loader for current resource if it must be processed before exported
                            I_CmsResourceLoader loader = OpenCms.getResourceManager().getLoader(resource);
                            if (!loader.isStaticExportProcessable()) {
                                // this resource must not be processed, so export it (if it's not marked as deleted)
                                if (pupRes.getState() != CmsResource.STATE_DELETED) {
                                    // mark the resource for export to the real file system                  
                                    resourcesToExport.add(exportData);
                                }
                            } else {
                                // the resource is a template resource or a folder, so store the name of it in the DB for further use                  
                                templatesFound = true;
                                cms.writeStaticExportPublishedResource(
                                    exportData.getRfsName(),
                                    CmsStaticExportManager.EXPORT_LINK_WITHOUT_PARAMETER,
                                    "",
                                    System.currentTimeMillis());
                            }
                        }
                    }
                }
            }
        }

        // now do the export
        i = resourcesToExport.iterator();
        int size = resourcesToExport.size();

        if (LOG.isDebugEnabled()) {
            LOG.debug(Messages.get().getBundle().key(Messages.LOG_NUM_EXPORT_1, new Integer(size)));
        }
        while (i.hasNext()) {
            CmsStaticExportData exportData = (CmsStaticExportData)i.next();
            if (LOG.isDebugEnabled()) {
                LOG.debug(Messages.get().getBundle().key(
                    Messages.LOG_EXPORT_FILE_2,
                    exportData.getVfsName(),
                    exportData.getRfsName()));
            }

            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,
                exportData.getVfsName()));
            report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));
            int status = manager.export(null, null, cms, exportData);
            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 {
                report.println(
                    org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_IGNORED_0),
                    I_CmsReport.FORMAT_NOTE);
            }

            if (LOG.isInfoEnabled()) {
                Object[] arguments = new Object[] {
                    exportData.getVfsName(),
                    exportData.getRfsName(),
                    new Integer(status)};
                LOG.info(Messages.get().getBundle().key(Messages.LOG_EXPORT_FILE_STATUS_3, arguments));
            }
        }

        resourcesToExport = null;

        report.println(
            Messages.get().container(Messages.RPT_STATICEXPORT_NONTEMPLATE_RESOURCES_END_0),
            I_CmsReport.FORMAT_HEADLINE);

        return templatesFound;
    }

    /**
     * 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();

        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("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)));
                }

                // now perform the request
                urlcon.connect();
                int status = urlcon.getResponseCode();
                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 + -