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

📄 cmsstaticexport.java

📁 内容管理
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                if(resWithProp != null && resWithProp.size() != 0){
                    m_dynamicExportNameRules = new Vector();
                    m_dynamicExportNameRulesExtern = new Vector();
                    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(CmsObject.getStaticExportProperties().isExportDefault()){
                            m_dynamicExportNameRules.addElement("s#^"+oldName+"#"+CmsObject.getStaticExportProperties().getUrlPrefixArray()[0]+newName+"#");
                        }else{
                            m_dynamicExportNameRules.addElement("s#^("+CmsObject.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+")#"+ CmsObject.getStaticExportProperties().getUrlPrefixArray()[1] +"$1#");//2
                        }
                        if(propertyValue.equalsIgnoreCase("https")){
                            m_dynamicExportRulesExtern.addElement("s#^"+resName+".*##");//1
                            m_dynamicExportRulesOnline.addElement("s#^("+resName+")#"+ CmsObject.getStaticExportProperties().getUrlPrefixArray()[2] +"$1#");
                        }
                        if(propertyValue.equalsIgnoreCase("true")){
                            m_dynamicExportRulesExtern.addElement("s#^("+resName+")#$1#");
                            m_dynamicExportRulesOnline.addElement("s#^("+resName+")#"+ CmsObject.getStaticExportProperties().getUrlPrefixArray()[0] +"$1#");
                        }
                        if(propertyValue.equalsIgnoreCase("false")){
                            m_dynamicExportRulesExtern.addElement("s#^"+resName+".*#export value was set to false#");
                            m_dynamicExportRulesOnline.addElement("s#^("+resName+")#"+ CmsObject.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+")#"+ CmsObject.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() );
            }
        }
    }
    
    /**
     * 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;
    }

    /**
     * Parse a name in the query string.
     */
    static private String parseName(String s, StringBuffer sb) {
    sb.setLength(0);
    for (int i = 0; i < s.length(); i++) {
        char c = s.charAt(i); 
        switch (c) {
        case '+':
        sb.append(' ');
        break;
        case '%':
        try {
            sb.append((char) Integer.parseInt(s.substring(i+1, i+3), 
                              16));
            i += 2;
        } catch (NumberFormatException e) {
            // XXX
            // need to be more specific about illegal arg
            throw new IllegalArgumentException();
        } catch (StringIndexOutOfBoundsException e) {
            String rest  = s.substring(i);
            sb.append(rest);
            if (rest.length()==2)
            i++;
        }
        
        break;
        default:
        sb.append(c);
        break;
        }
    }
    return sb.toString();
    }

    /**
     *
     * Parses a query string passed from the client to the
     * server and builds a <code>HashTable</code> object
     * with key-value pairs. 
     * The query string should be in the form of a string
     * packaged by the GET or POST method, that is, it
     * should have key-value pairs in the form <i>key=value</i>,
     * with each pair separated from the next by a & character.
     *
     * <p>A key can appear more than once in the query string
     * with different values. However, the key appears only once in 
     * the hashtable, with its value being
     * an array of strings containing the multiple values sent
     * by the query string.
     * 
     * <p>The keys and values in the hashtable are stored in their
     * decoded form, so
     * any + characters are converted to spaces, and characters
     * sent in hexadecimal notation (like <i>%xx</i>) are
     * converted to ASCII characters.
     *
     * @param s     a string containing the query to be parsed
     *
     * @return      a <code>HashTable</code> object built
     *          from the parsed key-value pairs
     *
     * @throws IllegalArgumentException  if the query string 
     *                      is invalid
     *
     */
    static public Hashtable parseQueryString(String s) {
    String valArray[] = null;
    if (s == null) {
        throw new IllegalArgumentException();
    }
    Hashtable ht = new Hashtable();
    StringBuffer sb = new StringBuffer();
    StringTokenizer st = new StringTokenizer(s, "&");
    while (st.hasMoreTokens()) {
        String pair = (String)st.nextToken();
        int pos = pair.indexOf('=');
        if (pos == -1) {
        // XXX
        // should give more detail about the illegal argument
        throw new IllegalArgumentException();
        }
        String key = parseName(pair.substring(0, pos), sb);
        String val = parseName(pair.substring(pos+1, pair.length()), sb);
        if (ht.containsKey(key)) {
        String oldVals[] = (String []) ht.get(key);
        valArray = new String[oldVals.length + 1];
        for (int i = 0; i < oldVals.length; i++) 
            valArray[i] = oldVals[i];
        valArray[oldVals.length] = val;
        } else {
        valArray = new String[1];
        valArray[0] = val;
        }
        ht.put(key, valArray);
    }
    return ht;
    }

    /**
     * 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.
     * @param writeAccess says if the DB should be updated. Needed for the clustering mode
     *      where only the files must be written but the database was updated by the master system.
     */
    private void exportLink(String link, Vector allLinks, boolean writeAccess) 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 resource must not be exported and has no links on it that must be exported
                m_report.print(m_report.key("report.skipping"), I_CmsReport.C_FORMAT_NOTE);
                m_report.println(link);
                return;
            }
            if(paraStart >= 0){
                Hashtable parameter = parseQueryString(link.substring(paraStart +1));
                link = link.substring(0, paraStart);
                dReq.setParameters(parameter);
            }
            CmsExportResponse dRes = new CmsExportResponse();

            if(writeFile){
                // update the report
                m_report.print(m_report.key("report.exporting"), I_CmsReport.C_FORMAT_NOTE);
                m_report.println(link);
                // 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{
                        new URL(externLink);
                    }catch(MalformedURLException e){
                        linkIsExtern = false;
                    }
                    if(linkIsExtern){
                        throw new CmsException(" This is an external 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);
                if (! discFile.isDirectory()) {
                    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() + "] " + "couldn't open file "
                                    + m_exportPath + correctur + externLink + ": " + e.getMessage());
                    }
                }
            }else{
                // update the report
                m_report.print(m_report.key("report.following_links_on"), I_CmsReport.C_FORMAT_NOTE);
                m_report.println(link);
                // 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());
            // Encoding project:
            // make new detection of current encoding because we have changed the requested resource
            cmsForStaticExport.getRequestContext().initEncoding();
            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
            if(writeAccess){
                Vector depsToAdd = cmsForStaticExport.getRequestContext().getDependencies();

⌨️ 快捷键说明

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