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

📄 cmslinkcheck.java

📁 内容管理
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                        template.setData("links", mailLinks.toString());
                        mailUrls.append(template.getProcessedDataValue("single_url"));
                    }
                    mailContent.append(mailUrls.toString());
                    if("email".equals(parameter.trim())){
                        // get the eMail information
                        String mailSubject = template.getProcessedDataValue("emailsubject");
                        String mailFrom = (String)emailValues.get("mailfrom");
                        String[] mailTo = getReceiverArray((String)emailValues.get("mailto"));
                        String[] mailCc = getReceiverArray(template.getDataValue("emailcc"));
                        String[] mailBcc = getReceiverArray(template.getDataValue("emailbcc"));
                        String mailType = template.getDataValue("emailtype");
                        generateEmail(mailFrom, mailTo, mailCc, mailBcc, mailSubject, mailContent.toString(), mailType);
                    } else {
                        generateFile(mailContent.toString(), parameter, actDate);
                    }
                }
            }
        }
	}

    /**
     * check if the url is valid
     *
     * @param url The url to check
     * @return boolean If false the url could not be accessed
     */
    public static boolean checkUrl(String url){
        try {
            URL checkedUrl = new URL(url);
            if(url.toLowerCase().startsWith("http")){
                HttpURLConnection httpcon = (HttpURLConnection)checkedUrl.openConnection();

                if (httpcon.getResponseCode() == 200) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return true;
            }
        } catch (MalformedURLException mue) {
            return false;
        } catch (IOException ioe){
            return false;
        }
    }

    /**
     * checks if the url is valid and returns the response message
     *
     * @param url The url to check
     * @return String return the response message
     */
    public String checkUrlGetMessage(String url){
        try {
            URL checkedUrl = new URL(url);
            HttpURLConnection con = (HttpURLConnection)checkedUrl.openConnection();

            if (con.getResponseCode() == 200) {
                return "";
            } else {
                return con.getResponseMessage();
            }
        } catch (MalformedURLException mue) {
            return "MalformedURLException: "+mue.getMessage();
        } catch (IOException ioe){
            return "IOException: "+ioe.getMessage();
        }
    }

	/**
	 * Generates the eMail
     *
     * @param mailFrom The email adress of the sender
     * @param mailTo The email adress(es) of the receiver(s)
     * @param mailCc The email adress(es) of the CC-receiver(s)
     * @param mailBcc The email adress(es) of the BCC-receiver(s)
     * @param mailSubject The subject of the mail
     * @param mailContent The content of the mail
     * @param mailType The type of the mail
	 */
	private void generateEmail(String mailFrom, String[] mailTo, String[] mailCc, String[] mailBcc,
                                String mailSubject, String mailContent, String mailType) throws CmsException{
        // create a new CmsMail object and start sending the mails
        CmsMail mail = null;
        if (MailUtils.checkEmail(mailFrom)){
            if (MailUtils.checkEmail((String)mailTo[0])){
                if (mailCc.length > 0 && mailBcc.length > 0) {
                    mail=new CmsMail(mailFrom, mailTo, mailCc, mailBcc, mailSubject, mailContent, mailType);
                    mail.start();
                } else if (mailBcc.length > 0) {
                    mail=new CmsMail(mailFrom, mailTo, mailBcc, mailSubject, mailContent, mailType);
                    mail.start();
                } else {
                    mail=new CmsMail(mailFrom, mailTo, mailSubject, mailContent, mailType);
                    mail.start();
                }
            } else {
            }
        }
	}

	/**
	 * Generates the file
     *
     * @param content The content to store into the file
     * @param pathname The path where the file should be stored
     * @param actDate The date and time when the urls were checked
	 */
	private void generateFile(String content, String pathname, GregorianCalendar actDate) throws CmsException{
        StringBuffer filename = new StringBuffer("check_");
        String year = actDate.get(GregorianCalendar.YEAR)+"";
        String month = (actDate.get(GregorianCalendar.MONTH)+1)+"";
        if(month.length() == 1){
            month = "0"+month;
        }
        String day = (actDate.get(GregorianCalendar.DATE)+"");
        if(day.length() == 1){
            day = "0"+day;
        }
        String hour = actDate.get(GregorianCalendar.HOUR_OF_DAY)+"";
        if(hour.length() == 1){
            hour = "0"+hour;
        }
        String minute = actDate.get(GregorianCalendar.MINUTE)+"";
        if(minute.length() == 1){
            minute = "0"+minute;
        }
        filename.append(year.substring(2));
        filename.append(month);
        filename.append(day);
        filename.append("_"+hour+"-");
        filename.append(minute);
        filename.append(".html");
        // create a new file in the folder with the pathname and writes the filecontent
        try{
            File outputFile = new File(pathname, filename.toString());
            FileWriter writer = new FileWriter(outputFile);
            writer.write(content);
            writer.close();
        } catch (IOException e){
            throw new CmsException("Cannot write output file.", e);
        }
	}

    /**
     * Returns the receivers in a string as string array.
     * The receivers are separated by semicolon
     *
     * @param receivers The string that contains the receivers
     * @return String[] The receivers as elements in an string array
     */
    private String[] getReceiverArray(String receivers){
        String[] retArray = null;
        if(receivers != null){
            if (!"".equals(receivers.trim())){
                StringTokenizer tokens = new StringTokenizer(receivers, ";");
                Vector vec = new Vector();
                while (tokens.hasMoreElements()) {
                    vec.addElement( tokens.nextElement());
                }
                retArray= new String[vec.size()];
                vec.copyInto(retArray);
            } else {
                retArray = new String[] {};
            }
        } else {
            retArray = new String[] {};
        }
        return retArray;
    }
    
    /**
     * Returns the current date as formatted String
     * 
     * @param curDate The current date
     * @return String The current date as String dd.mm.yyyy hh:min
     */
    private String getDateString(GregorianCalendar actDate){
        String month = (actDate.get(GregorianCalendar.MONTH)+1)+"";
        if(month.length() == 1){
            month = "0"+month;
        }
        String day = (actDate.get(GregorianCalendar.DATE)+"");
        if(day.length() == 1){
            day = "0"+day;
        }
        String hour = actDate.get(GregorianCalendar.HOUR_OF_DAY)+"";
        if(hour.length() == 1){
            hour = "0"+hour;
        }
        String minute = actDate.get(GregorianCalendar.MINUTE)+"";
        if(minute.length() == 1){
            minute = "0"+minute;
        }
        return (day+"."+month+"."+actDate.get(GregorianCalendar.YEAR)+" "+hour+":"+minute);
    }
}

⌨️ 快捷键说明

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