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

📄 cmsstaticexport.java

📁 java 编写的程序
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                    for(int i=0; i < resWithProp.size(); i++){
                        CmsResource resource = (CmsResource)resWithProp.elementAt(i);
                        String oldName = resource .getAbsolutePath();
                        String newName = m_cms.readProperty(oldName, C_PROPERTY_EXPORTNAME);
                        if(m_cms.getStaticExportProperties().isExportDefault()){
                            m_dynamicExportNameRules.addElement("s#^"+oldName+"#"+m_cms.getStaticExportProperties().getUrlPrefixArray()[0]+newName+"#");
                        }else{
                            m_dynamicExportNameRules.addElement("s#^("+m_cms.getStaticExportProperties().getUrlPrefixArray()[0]+")"+oldName+"#$1"+newName+"#");
                        }
                        m_dynamicExportNameRulesExtern.addElement("s#^"+oldName+"#"+newName+"#");
                    }
                }
            }
        }catch(CmsException e){
            if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging()) {
                A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_STATICEXPORT, "[CmsStaticExport] "
                    +"couldnt create dynamic export rules for the nice names. Will use the original names instead."
                    + e.toString() );
            }
        }
        // now the rules for linking between static and dynamic pages
        try{
            // get the resources with the property "export"
            Vector resWithProp = m_cms.getResourcesWithProperty(C_PROPERTY_EXPORT);
            // generate the rules
            if(resWithProp != null && resWithProp.size() != 0){
                m_dynamicExportRulesExtern = new Vector();
                m_dynamicExportRulesOnline = new Vector();
                m_rulesForHttpsEnabledResources = new Vector();
                for(int i=0; i < resWithProp.size(); i++){
                    CmsResource resource = (CmsResource)resWithProp.elementAt(i);
                    String resName = resource.getAbsolutePath();
                    String propertyValue = m_cms.readProperty(resName, C_PROPERTY_EXPORT);
                    if(propertyValue != null){
                        if(propertyValue.equalsIgnoreCase("dynamic")){
                            m_dynamicExportRulesExtern.addElement("s#^"+resName+".*##");//1
                            m_dynamicExportRulesOnline.addElement("s#^("+resName+")#"+ m_cms.getStaticExportProperties().getUrlPrefixArray()[1] +"$1#");//2
                        }
                        if(propertyValue.equalsIgnoreCase("https")){
                            m_dynamicExportRulesExtern.addElement("s#^"+resName+".*##");//1
                            m_dynamicExportRulesOnline.addElement("s#^("+resName+")#"+ m_cms.getStaticExportProperties().getUrlPrefixArray()[2] +"$1#");
                        }
                        if(propertyValue.equalsIgnoreCase("true")){
                            m_dynamicExportRulesExtern.addElement("s#^("+resName+")#$1#");
                            m_dynamicExportRulesOnline.addElement("s#^("+resName+")#"+ m_cms.getStaticExportProperties().getUrlPrefixArray()[0] +"$1#");
                        }
                        if(propertyValue.equalsIgnoreCase("false")){
                            m_dynamicExportRulesExtern.addElement("s#^"+resName+".*#export value was set to false#");
                            m_dynamicExportRulesOnline.addElement("s#^("+resName+")#"+ m_cms.getStaticExportProperties().getUrlPrefixArray()[1] +"$1#");//2
                        }
                        if(propertyValue.equalsIgnoreCase("https_enabled")){
                            m_rulesForHttpsEnabledResources.addElement("s#^"+resName+".*##");//3
                        }
                        if(propertyValue.equalsIgnoreCase("dynamic_https_enabled")){
                            m_dynamicExportRulesExtern.addElement("s#^"+resName+".*##");//1
                            m_dynamicExportRulesOnline.addElement("s#^("+resName+")#"+ m_cms.getStaticExportProperties().getUrlPrefixArray()[1] +"$1#");//2
                            m_rulesForHttpsEnabledResources.addElement("s#^"+resName+".*##");//3
                        }
                    }
                }
            }
        }catch(CmsException e){
            if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging()) {
                A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_STATICEXPORT, "[CmsStaticExport] "
                    +"couldnt create dynamic export rules. " + e.toString() );
            }
        }
    }
    private void createDynamicRules2(){
        // first the rules for namereplacing
            // later!!

        // now the rules for linking between static and dynamic pages
        try{
            // get the resources with the property "export"
            Vector resWithProp = m_cms.getResourcesWithProperty(C_PROPERTY_EXPORT);
            // generate the rules
            if(resWithProp != null && resWithProp.size() != 0){
                m_dynamicExportRulesExtern = new Vector();
                m_dynamicExportRulesOnline = new Vector();
                m_rulesForHttpsEnabledResources = new Vector();
                for(int i=0; i < resWithProp.size(); i++){
                    CmsResource resource = (CmsResource)resWithProp.elementAt(i);
                    String resName = resource.getAbsolutePath();
                    String propertyValue = m_cms.readProperty(resName, C_PROPERTY_EXPORT);
                    if(propertyValue != null){
                        if(propertyValue.equalsIgnoreCase("dynamic")){
                            m_dynamicExportRulesExtern.addElement("s#^"+resName+".*##");
                            m_dynamicExportRulesOnline.addElement("s#^("+resName+")#"+ m_cms.getStaticExportProperties().getUrlPrefixArray()[1] +"$1#");
                        }
                        if(propertyValue.equalsIgnoreCase("https")){
                            m_dynamicExportRulesExtern.addElement("s#^"+resName+".*##");
                            m_dynamicExportRulesOnline.addElement("s#^("+resName+")#"+ m_cms.getStaticExportProperties().getUrlPrefixArray()[2] +"$1#");
                        }
                        if(propertyValue.equalsIgnoreCase("true")){

                        }
                        if(propertyValue.equalsIgnoreCase("false")){
                            m_dynamicExportRulesExtern.addElement("s#^"+resName+".*#export value was set to false#");
                            m_dynamicExportRulesOnline.addElement("s#^("+resName+")#"+ m_cms.getStaticExportProperties().getUrlPrefixArray()[1] +"$1#");
                        }
                        if(propertyValue.equalsIgnoreCase("https_enabled")){
                            m_rulesForHttpsEnabledResources.addElement("s#^"+resName+".*##");
                        }
                        if(propertyValue.equalsIgnoreCase("dynamic_https_enabled")){
                            m_dynamicExportRulesExtern.addElement("s#^"+resName+".*##");
                            m_dynamicExportRulesOnline.addElement("s#^("+resName+")#"+ m_cms.getStaticExportProperties().getUrlPrefixArray()[1] +"$1#");
                            m_rulesForHttpsEnabledResources.addElement("s#^"+resName+".*##");
                        }
                    }
                }
            }
        }catch(CmsException e){
            if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging()) {
                A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_STATICEXPORT, "[CmsStaticExport] "
                    +"couldnt create dynamic export rules. " + e.toString() );
            }
        }
    }

    /**
     * checks if a link on a https page needs the http prefix.
     * Therefor it uses the property value https_enabled and dynamic_https_enabled on
     * the property export.
     * @param link the link to test
     */
    public static boolean needsScheme(String link){

        String retValue = link;
        if(m_rulesForHttpsEnabledResources != null){
            for(int i=0; i<m_rulesForHttpsEnabledResources.size(); i++){
                retValue = c_perlUtil.substitute((String)m_rulesForHttpsEnabledResources.elementAt(i), link);
                if(!retValue.equals(link)) {
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * exports one single link and adds sublinks to the allLinks vector.
     *
     * @param link The link to export may have html parameters
     * @param allLinks The vector with all links that have to be exported.
     */
    private void exportLink(String link, Vector allLinks) throws CmsException{

        String deleteFileOnError = null;
        OutputStream outStream = null;
        CmsExportLink dbLink = new CmsExportLink(link, System.currentTimeMillis(), null);
        // remember the original link for later
        String remLink = link;

        try{
            // first lets create our request and response objects for the export
            CmsExportRequest dReq = new CmsExportRequest(m_webAppUrl, m_servletUrl);
            // test if there are parameters for the request
            int paraStart = link.indexOf("?");

            // get the name for the filesystem
            String externLink = getExternLinkName(link);
            boolean writeFile = true;
            if(externLink == null || externLink.equals("")){
                writeFile = false;
            }
            if(C_EXPORT_FALSE.equals(externLink)){
                // this should not be exported and has no links on it that must be exported
                return;
            }
            if(paraStart >= 0){
                Hashtable parameter = javax.servlet.http.HttpUtils.parseQueryString(
                                        link.substring(paraStart +1));
                link = link.substring(0, paraStart);
                dReq.setParameters(parameter);
            }
            CmsExportResponse dRes = new CmsExportResponse();

            if(writeFile){
                // now create the necesary folders
                String folder = "";
                int folderIndex = externLink.lastIndexOf('/');
                if(folderIndex != -1){
                    folder = externLink.substring(0, externLink.lastIndexOf('/'));
                }
                String correctur = "";
                if(!externLink.startsWith("/")){
                    correctur = "/";
                    // this is only for old versions where the editor may have added linktags containing
                    // extern links. Such a link can not be exported and we dont want to create strange
                    // folders like '"http:'
                    boolean linkIsExtern = true;
                    try{
                        URL test = new URL(externLink);
                    }catch(MalformedURLException e){
                        linkIsExtern = false;
                    }
                    if(linkIsExtern){
                        throw new CmsException(" This is a extern link.");
                    }
                }
                File discFolder = new File(m_exportPath + correctur + folder);
                if(!discFolder.exists()){
                    if(!discFolder.mkdirs()){
                        throw new CmsException("["+this.getClass().getName() + "] " +
                                "could't create all folders for "+folder+".");
                    }
                }
                // all folders exist now create the file
                File discFile = new File(m_exportPath + correctur + externLink);
                deleteFileOnError = m_exportPath + correctur + externLink;
                try{
                    // link the File to the request
                    outStream = new FileOutputStream(discFile);
                    dRes.putOutputStream(outStream);
                }catch(Exception e){
                    throw new CmsException("["+this.getClass().getName() + "] " + "could't open file "
                                + m_exportPath + correctur + externLink + ": " + e.getMessage());
                }
            }else{
                // we dont want to write this file but we have to generate it to get links in it.
                dRes.putOutputStream(new ByteArrayOutputStream());
            }

            // everthing is prepared now start the template mechanism
            CmsObject cmsForStaticExport = m_cms.getCmsObjectForStaticExport(dReq, dRes);
            cmsForStaticExport.setMode(C_MODUS_EXPORT);
            CmsFile file = m_cms.readFile(link);
            int launcherId = file.getLauncherType();
            String startTemplateClass = file.getLauncherClassname();
            I_CmsLauncher launcher = cmsForStaticExport.getLauncherManager().getLauncher(launcherId);
            if(launcher == null){
                throw new CmsException("Could not launch file " + link + ". Launcher for requested launcher ID "
                            + launcherId + " could not be found.");
            }
            ((CmsExportRequest)cmsForStaticExport.getRequestContext().getRequest()).setRequestedResource(link);
            cmsForStaticExport.getRequestContext().addDependency(file.getResourceName());
            launcher.initlaunch(cmsForStaticExport, file, startTemplateClass, null);

            // we need the links on the page for the further export
            Vector linksToAdd = cmsForStaticExport.getRequestContext().getLinkVector();
            for(int i=0; i<linksToAdd.size(); i++){
                if(!allLinks.contains(linksToAdd.elementAt(i))){
                    CmsExportLink lookup = m_cms.readExportLinkHeader((String)linksToAdd.elementAt(i));
                    if(!m_afterPublish || (lookup == null) || (lookup.getLastExportDate() == 0)){
                            // after publish we only add this link if it is was
                            // not exported befor.
                            allLinks.add(linksToAdd.elementAt(i));
                    }
                }
            }
            // now get the dependencies and write the link to the database
            Vector depsToAdd = cmsForStaticExport.getRequestContext().getDependencies();
            for(int i=0; i<depsToAdd.size(); i++){
                dbLink.addDependency((String)depsToAdd.elementAt(i));
            }
            try{
                dbLink.setProcessedState(true);
                m_cms.writeExportLink(dbLink);
            }catch(CmsException e){
                if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging()) {
                    A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_STATICEXPORT, "[CmsStaticExport] write ExportLink "+dbLink.getLink()+" failed: "+e.toString());
                }
            }

            // at last close the outputstream
            if(outStream != null){
                outStream.close();
            }
        }catch(CmsException exc){
            if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging()) {
                A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_STATICEXPORT, "[CmsStaticExport] "+deleteFileOnError+" export "+link+" failed: "+exc.toString());
            }
            if(deleteFileOnError != null){
                try{
                    File deleteMe = new File(deleteFileOnError);
                    if(deleteMe.exists() ){//&& deleteMe.length() == 0){
                        if(outStream != null){
                            outStream.close();
                        }
                        deleteMe.delete();
                        m_cms.deleteExportLink(remLink);
                    }

⌨️ 快捷键说明

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