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

📄 cmslinkcheck.java

📁 内容管理
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
* File   : $Source: /usr/local/cvs/opencms/src/com/opencms/file/Attic/CmsLinkCheck.java,v $
* Date   : $Date: 2003/01/20 23:59:17 $
* Version: $Revision: 1.4 $
*
* 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.core.*;
import com.opencms.template.*;
import com.opencms.util.*;
import com.opencms.defaults.*;
import java.util.*;
import java.net.*;
import java.io.*;

/**
 * This class contains the functionaility for checking the url of external links.<p>
 * 
 * @author Edna Falkenhan
 */
public class CmsLinkCheck extends CmsXmlTemplate implements I_CmsCronJob,I_CmsConstants{


    public CmsLinkCheck() {
    }

    /**
     * this method is called by the scheduler
     * @param CmsObject
     * @param parameter
     * @return String - String that is written to the opencms -log
     */
    public String launch(CmsObject cms, String parameter) throws CmsException {
        linksUrlCheck(cms, parameter);
        return "CmsLinkCheck.launch(): Links checked.";
    }

    /**
     * Check all external links
     *
     * @param CmsObject
     * @param parameter
     */
	public void linksUrlCheck(CmsObject cms, String parameter) throws CmsException {
        // hashtable that contains the urls that are not available and the resourcenames of the links
        // where this url is referenced
        Hashtable notAvailable = new Hashtable();
        
        // vector that contains all external links from the database
        Vector linkList = new Vector();
        
        // vector and hashtable that contains the links of an owner that are not available,
        // so there can be created a mail to the owner with all the broken links
        Hashtable ownerLinkList = new Hashtable();

        // get the hashtable with the last check result from the system table
        Hashtable linkckecktable = cms.readLinkCheckTable();

        Hashtable newLinkchecktable = new Hashtable();       
        // get the values for email from the registry
        Hashtable emailValues = cms.getRegistry().getSystemValues("checklink");
        // get templateFile this way because there is no actual file if
        // method is called from scheduler ...
        CmsXmlTemplateFile template=getOwnTemplateFile(cms, (String)emailValues.get("mailtemplate"), "", null, "");
        
        // set the current date and time
        GregorianCalendar actDate = new GregorianCalendar();
        String actDateString = getDateString(actDate);
        template.setData("actdate", actDateString);
        newLinkchecktable.put(C_LINKCHECKTABLE_DATE, actDateString);

        StringBuffer mailContent = new StringBuffer(template.getProcessedDataValue("single_message"));

        // get all links from the database
        linkList = cms.readFilesByType(I_CmsConstants.C_UNKNOWN_INT, cms.getResourceType(CmsResourceTypeLink.C_TYPE_RESOURCE_NAME).getResourceType());
        for(int i=0; i<linkList.size(); i++){
            CmsFile linkElement = (CmsFile)linkList.elementAt(i);
            String linkName = linkElement.getAbsolutePath();
			String linkUrl = new String(linkElement.getContents());
            // do not check internal links
            if(!linkUrl.startsWith("/")){
                // get the number of failed checks for the link
                int failedCheck = 0;
                String numFromTable = (String)linkckecktable.get(linkName+", "+linkUrl);
                if((numFromTable != null) && (!"".equals(numFromTable.trim()))){
                    failedCheck = Integer.parseInt(numFromTable);
                }

			    // check the url,
                // if the url is not readable add it to the list of not available urls
			    if (!checkUrl(linkUrl)){
				    // get the vector of resourcenames from the hashtable of urls
                    Vector inList = null;
				    inList = (Vector)notAvailable.get(linkUrl);
				    if(inList == null){
					    inList = new Vector();
                    }
				    inList.addElement(linkName);
                    notAvailable.put(linkUrl, inList);
                    
                    // create the hashtable for the owner mails if requested
                    if((parameter != null) && ("owneremail".equals(parameter.trim()))){
                        // add the failed link to the links of the owner
                        // first try to get the email
                        String ownerEmail = null;
                        try{
                            CmsUser owner = cms.readUser(linkElement.getOwnerId());
                            ownerEmail = owner.getEmail();
                        } catch (Exception e){
                        }

                        if((ownerEmail == null) || ("".equals(ownerEmail.trim()))){
                            ownerEmail = (String)emailValues.get("mailto");
                        }
                        Hashtable ownerLinks = null;
                        ownerLinks = (Hashtable)ownerLinkList.get(ownerEmail);
                        if(ownerLinks == null){
                            ownerLinks = new Hashtable();
                        }
                        ownerLinks.put(linkName,linkUrl);
                        ownerLinkList.put(ownerEmail, ownerLinks);                  
                    }
                    
                    // add the failed link to the new linkchecktable
                    newLinkchecktable.put(linkName+", "+linkUrl, ""+(failedCheck+1));
                }
            }
        }
        // write the linkchecktable to database
        cms.writeLinkCheckTable(newLinkchecktable);

        // get the information for the output
        if((parameter != null) && (!"".equals(parameter.trim()))){
            // send an email to the owner of the link
            if("owneremail".equals(parameter.trim())){
                // get the owners from the owner list
                if(ownerLinkList.size() > 0){
                    Enumeration ownerKeys = ownerLinkList.keys();
                    while (ownerKeys.hasMoreElements()){
                        StringBuffer ownerContent = new StringBuffer();
                        ownerContent.append(mailContent.toString());
                        String mailTo = (String)ownerKeys.nextElement();
                        Hashtable linknames = (Hashtable)ownerLinkList.get(mailTo);
                        // get all failed links of the owner
                        Enumeration linkKeys = linknames.keys();
                        String singleLink = "";
                        while(linkKeys.hasMoreElements()){
                            // set the data for the link
                            singleLink = (String)linkKeys.nextElement();
                            template.setData("ownerlinkname", singleLink);
                            template.setData("ownerlinkurl", (String)linknames.get(singleLink));
                            ownerContent.append(template.getProcessedDataValue("ownermail_link"));
                        }
                        // get the email data
                        String mailSubject = template.getProcessedDataValue("emailsubject");
                        String mailFrom = (String)emailValues.get("mailfrom");
                        String[] mailCc = getReceiverArray(template.getDataValue("emailcc"));
                        String[] mailBcc = getReceiverArray(template.getDataValue("emailbcc"));
                        String mailType = template.getDataValue("emailtype");
                        generateEmail(mailFrom, getReceiverArray(mailTo), mailCc, mailBcc, mailSubject, ownerContent.toString(), mailType);
                    }
                }
            } else {
                // if there are not readable urls create the content of the eMail
                // and send it to the specified user(s)
                if(notAvailable.size() > 0){
                    Enumeration linkKeys = notAvailable.keys();
                    StringBuffer mailUrls = new StringBuffer();
                    while (linkKeys.hasMoreElements()){
                        String url = (String)linkKeys.nextElement();
                        template.setData("url", url);
                        Vector linknames = (Vector)notAvailable.get(url);
                        StringBuffer mailLinks = new StringBuffer();
                        for(int j=0; j < linknames.size(); j++){
                            String nextLink = (String)linknames.elementAt(j);
                            template.setData("linkname", nextLink);
                            mailLinks.append(template.getProcessedDataValue("single_link"));
                        }

⌨️ 快捷键说明

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