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

📄 cmsstaticexport.java

📁 内容管理
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*
* File   : $Source: /usr/local/cvs/opencms/src/com/opencms/file/Attic/CmsStaticExport.java,v $
* Date   : $Date: 2003/04/03 16:55:22 $
* Version: $Revision: 1.44 $
*
* This library is part of OpenCms -
* the Open Source Content Mananagement System
*
* Copyright (C) 2001  The OpenCms Group
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*
* For further information about OpenCms, please see the
* OpenCms Website: http://www.opencms.org
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
package com.opencms.file;

import com.opencms.boot.I_CmsLogChannels;
import com.opencms.core.A_OpenCms;
import com.opencms.core.CmsException;
import com.opencms.core.CmsExportRequest;
import com.opencms.core.CmsExportResponse;
import com.opencms.core.CmsStaticExportProperties;
import com.opencms.core.I_CmsConstants;
import com.opencms.core.OpenCms;
import com.opencms.launcher.I_CmsLauncher;
import com.opencms.report.CmsShellReport;
import com.opencms.report.I_CmsReport;
import com.opencms.util.Utils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Hashtable;
import java.util.StringTokenizer;
import java.util.Vector;

import org.apache.oro.text.perl.Perl5Util;

/**
 * Holds the functionaility to export resources from the cms
 * to the filesystem.
 *
 * @author Hanjo Riege
 * @version $Revision: 1.44 $ $Date: 2003/04/03 16:55:22 $
 */
public class CmsStaticExport implements I_CmsConstants{

    /**
     * The cms-object to do the operations.
     */
    private CmsObject m_cms;

    /**
     * The resources to export.
     */
    private Vector m_startpoints;

    /**
     * indicates that this is called after publish project
     */
    private boolean m_afterPublish = false;

    /**
     * In this vector we store the links that have changed after publish. It is
     * used by the search.
     * It contains compleate links (after linkreplace rules).
     */
    private Vector m_changedLinks = null;

    /**
     * this is used as a return value. it contains all links that where exported.
     * We need it for the clustering of OpenCms. The Slave servers get the vector and
     * can export all changed stuff without creating the vector themself.
     */
    private Vector m_allExportedLinks = null;

    private static Perl5Util c_perlUtil = null;

    /**
     * The export path.
     */
    private String m_exportPath;

    /**
     *
     */
    private String m_webAppUrl="";
    private String m_servletUrl="";

    /**
     * Value for resources with property export = false
     */
    static final String C_EXPORT_FALSE = "export value was set to false";

    /**
     * Additional rules for the export generated dynamical.
     * Used to replace ugly long foldernames with short nice
     * ones set as a property to the folder.
     * one extra version for extern
     */
    public static Vector m_dynamicExportNameRules = null;
    public static Vector m_dynamicExportNameRulesExtern = null;

    /**
     * Additional rules for the export generated dynamical.
     * Used for linking back to OpenCms for dynamic content and
     * linking out to the static stuff. Used for online and for export.
     */
    public static Vector m_dynamicExportRulesOnline = null;

    /**
     * Additional rules for the export generated dynamical.
     * Used for linking back to OpenCms for dynamic content and
     * linking out to the static stuff. Used for extern. It only
     * returns "" so the staticExport dont write something to disk.
     */
    public static Vector m_dynamicExportRulesExtern = null;

    /**
     * This rules are only tho show if a resource is enabled with and without
     * https. they are generated by the export property (values https_enabled
     * for exportable resources(true) and dynamic_https_enabled for dynamic
     * resources (dynamic).
     */
    public static Vector m_rulesForHttpsEnabledResources = null;

    /**
     * The object to report the log-messages.
     */
    private I_CmsReport m_report = null;

    /**
     * This constructor is used to export the files on a slave server in clustering mode.
     * The list of linksToExport is created by the master server and is ready to use.
     *
     * @param cms The cms-object to work with.
     * @param linksToExport A vector of links that were exported on the master server.
     */
    public CmsStaticExport(CmsObject cms, Vector linksToExport) throws CmsException{

        m_cms = cms;
        m_exportPath = CmsObject.getStaticExportProperties().getExportPath();
        c_perlUtil = new Perl5Util();
        m_afterPublish = true;
        m_report = new CmsShellReport();

        try{
            m_servletUrl = cms.getRequestContext().getRequest().getServletUrl();
            m_webAppUrl = cms.getRequestContext().getRequest().getWebAppUrl();
            if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging()) {
                A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_STATICEXPORT,
                                    "[CmsStaticExport] Starting the static export on slave server.");
            }
            createDynamicRules();
            checkExportPath();
            if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging()) {
                A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_STATICEXPORT,
                        "[CmsStaticExport] got "+linksToExport.size()+" links to export.");
            }

            for(int i=0; i < linksToExport.size(); i++){
                String aktLink = (String)linksToExport.elementAt(i);
                exportLink(aktLink, linksToExport, false);
            }
            if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging()) {
                A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_STATICEXPORT,
                        "[CmsStaticExport] all done.");
            }
        }catch(NullPointerException e){
            // no original request
            if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging()) {
                A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_STATICEXPORT,
                                    "[CmsStaticExport] Nothing found to export.");
            }
        }

    }

    /**
     * This constructs a new CmsStaticExport-object which generates static html pages in the filesystem.
     *
     * @param cms the cms-object to work with.
     * @param startpoints. The resources to export (Vector of Strings)
     * @param doTheExport. must be set to true to export something, otherwise only the linkrules are generated.
     * @param changedLinks here we return the changed links for the search module (worked with the getlinksubstitution() method.
     * @param allExportedLinks here we return the links that was exported
     * @param changedResources. Contains the changed resources belonging to the project, if started after publishProject.
     * @param report the cmsReport to handle the log messages.
     *
     * @throws CmsException the CmsException is thrown if something goes wrong.
     */
    public CmsStaticExport(CmsObject cms, Vector startpoints, boolean doTheExport,
                     Vector changedLinks, Vector allExportedLinks, CmsPublishedResources changedResources, I_CmsReport report)
                     throws CmsException{
        m_cms = cms;
        m_startpoints = startpoints;
        m_changedLinks = changedLinks;
        m_allExportedLinks = allExportedLinks;
        m_exportPath = CmsObject.getStaticExportProperties().getExportPath();
        c_perlUtil = new Perl5Util();
        m_report = report;
        if(changedResources != null){
            m_afterPublish = true;
        }

        if(!doTheExport){
            // this is just to generate the dynamic rulesets
            if(I_CmsLogChannels.C_LOGGING && A_OpenCms.isLogging(I_CmsLogChannels.C_OPENCMS_INIT)) {
                A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_INIT,
                                ". Generating rulesets  : ok");                                
            }
            createDynamicRules();
        }else if (cms.getRequestContext().getRequest() != null) {
            // this will be false if OpenCms is running from the CmsShell
            try{
                m_servletUrl = cms.getRequestContext().getRequest().getServletUrl();
                m_webAppUrl = cms.getRequestContext().getRequest().getWebAppUrl();
                if(I_CmsLogChannels.C_LOGGING && A_OpenCms.isLogging(I_CmsLogChannels.C_OPENCMS_STATICEXPORT)) {
                    A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_STATICEXPORT,
                                        "[CmsStaticExport] Starting the static export.");
                }
                createDynamicRules();
                checkExportPath();
                Vector exportLinks = null;
                if(m_afterPublish){
                    exportLinks = getChangedLinks(changedResources);

                }else{
                    exportLinks = getStartLinks();
                }

                if(I_CmsLogChannels.C_LOGGING && A_OpenCms.isLogging(I_CmsLogChannels.C_OPENCMS_STATICEXPORT)) {
                    A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_STATICEXPORT,
                            "[CmsStaticExport] got "+exportLinks.size()+" links to start with.");
                }
                
                boolean doExport = (exportLinks.size() > 0);
                if (doExport) {
                    m_report.print(m_report.key("report.static_export_begin"), I_CmsReport.C_FORMAT_HEADLINE);
                    m_report.println(" " + exportLinks.size(), I_CmsReport.C_FORMAT_HEADLINE);                                
                    for(int i=0; i < exportLinks.size(); i++) {
                        String aktLink = (String)exportLinks.elementAt(i);
                        exportLink(aktLink, exportLinks, true);
                    }
                }
                setChangedLinkVector(exportLinks);
                if(I_CmsLogChannels.C_LOGGING && A_OpenCms.isLogging(I_CmsLogChannels.C_OPENCMS_STATICEXPORT)) {
                    A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_STATICEXPORT,
                            "[CmsStaticExport] all done.");
                }   
                if (doExport) {             
                    m_report.println(m_report.key("report.static_export_end"), I_CmsReport.C_FORMAT_HEADLINE);
                } else {
                    m_report.println(m_report.key("report.static_export_none"), I_CmsReport.C_FORMAT_HEADLINE);                    
                }
            }catch(NullPointerException e){
                m_report.println(e);
                // no original request
                if(I_CmsLogChannels.C_LOGGING && A_OpenCms.isLogging(I_CmsLogChannels.C_OPENCMS_STATICEXPORT)) {
                    A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_STATICEXPORT,
                                        "[CmsStaticExport] Nothing found to export.");
                }
            }
        }
    }

    /**
     * Checks if the export path exists. If not it is created.
     */
    private void checkExportPath()throws CmsException{

        if(m_exportPath == null){
            if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging()) {
                A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_STATICEXPORT, "[CmsStaticExport] no export folder given (null).");
            }
            throw new CmsException("[" + this.getClass().getName() + "] " + "no export folder given (null)", CmsException.C_BAD_NAME);
        }
        // we don't need the last slash
        if(m_exportPath.endsWith("/")){
            m_exportPath = m_exportPath.substring(0, m_exportPath.length()-1);
        }
        File discFolder = new File(m_exportPath + "/");
        if (!discFolder.exists()){
            if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging()) {
                A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_STATICEXPORT, "[CmsStaticExport] the export folder does not exist.");
            }
            throw new CmsException("[" + this.getClass().getName() + "] " + "the export folder does not exist", CmsException.C_BAD_NAME);
        }
    }

    /**
     * Generates the dynamic rules for the export. It looks for all folders that
     * have the property exportname. For each folder found it generates a rule that
     * replaces the folder name (incl. path) with the exportname.
     * In addition it looks for the property dynamic. and creates rules for linking
     * between static and dynamic pages.
     * These rules are used for export and for extern links.
     */
    private void createDynamicRules(){
        // first the rules for namereplacing
        try{
            // get the resources with the property exportname
            Vector resWithProp = null;
            if(!"false_ssl".equalsIgnoreCase(CmsObject.getStaticExportProperties().getStaticExportEnabledValue())){
                resWithProp = m_cms.getResourcesWithProperty(C_PROPERTY_EXPORTNAME);
                // generate the dynamic rules for the nice exportnames

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -