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

📄 commentservlet.java

📁 这个weblogging 设计得比较精巧
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    }    //-----------------------------------------------------------------------    /**     * Test CommentData to see if it is spam, if it is set it's spam property     * to true and put a RollerSession.ERROR_MESSAGE message in the session.     * @param cd CommentData to be tested.     */    private boolean testCommentSpam(CommentData cd, HttpServletRequest req)    {        boolean ret = false;        CommentSpamChecker checker = new CommentSpamChecker();        checker.testComment(cd);        if (cd.getSpam().booleanValue())        {           HttpSession session = req.getSession();           session.setAttribute(              RollerSession.ERROR_MESSAGE, COMMENT_SPAM_MSG);          ret = true;        }                return ret;    }    //-----------------------------------------------------------------------    // Email notification    // agangolli: Incorporated suggested changes from Ken Blackler, with server-wide configurable options    // TODO: Make the addressing options configurable on a per-website basis.    private static final String EMAIL_ADDR_REGEXP = "^.*@.*[.].{2,}$";    // Servlet init params that control how messages are addressed server-wide.  These default to false for old behavior    // Controls whether the owner and commenters get separate messages (owner's message contains a link to the entry edit page).    private static final String SEPARATE_OWNER_MSG_PARAM = CommentServlet.class.getName() + ".separateOwnerMessage";    // Controls whether the commenters addresses are placed in a Bcc header or a visible address field    private static final String HIDE_COMMENTER_ADDRESSES_PARAM = CommentServlet.class.getName() + ".hideCommenterAddresses";    /**     * Send email notification of comment.     */    private void sendEmailNotification(        HttpServletRequest request,        RollerRequest rreq,         WeblogEntryData wd,        CommentData cd,        UserData user,         List comments) throws MalformedURLException    {        RollerContext rc = RollerContext.getRollerContext(request);        ResourceBundle resources = ResourceBundle.getBundle(            "ApplicationResources",LanguageUtil.getViewLocale(request));        UserManager userMgr = null;        WebsiteData site = null;        try        {            userMgr = RollerContext.getRoller(request).getUserManager();            site = userMgr.getWebsite(user.getUserName());        }        catch (RollerException re)        {            re.printStackTrace();            mLogger.error(              "Couldn't get UserManager from RollerContext", re.getRootCause());        }        // Send e-mail to owner and subscribed users (if enabled)        boolean notify = RollerRuntimeConfig.getBooleanProperty("users.comments.emailnotify");        if (notify && site.getEmailComments().booleanValue())        {            // Determine message and addressing options from init parameters            boolean separateMessages =                     RollerConfig.getBooleanProperty("comment.notification.separateOwnerMessage");            boolean hideCommenterAddrs =                     RollerConfig.getBooleanProperty("comment.notification.hideCommenterAddresses");            //------------------------------------------            // --- Determine the "from" address            // --- Use either the site configured from address or the user's address                        String from =                (StringUtils.isEmpty(site.getEmailFromAddress()))                   ? user.getEmailAddress()                   : site.getEmailFromAddress();                        //------------------------------------------            // --- Build list of email addresses to send notification to                        // Get all the subscribers to this comment thread            Set subscribers = new TreeSet();            for (Iterator it = comments.iterator(); it.hasNext();)            {                CommentData comment = (CommentData) it.next();                if (!StringUtils.isEmpty(comment.getEmail()))                {	                // If user has commented twice, 	                // count the most recent notify setting	                if (comment.getNotify().booleanValue())	                {	                    // only add those with valid email	                    if (comment.getEmail().matches(EMAIL_ADDR_REGEXP))	                    {	                        subscribers.add(comment.getEmail());	                    }	                }	                else	                {	                    // remove user who doesn't want to be notified	                    subscribers.remove(comment.getEmail());	                }                }            }            // Form array of commenter addrs            String[] commenterAddrs = (String[])subscribers.toArray(new String[0]);            //------------------------------------------            // --- Form the messages to be sent -            // For simplicity we always build separate owner and commenter messages even if sending a single one                        // Determine with mime type to use for e-mail            StringBuffer msg = new StringBuffer();            StringBuffer ownermsg = new StringBuffer();            boolean escapeHtml = RollerRuntimeConfig.getBooleanProperty("users.comments.escapehtml");                                    if (!escapeHtml)            {                msg.append("<html><body style=\"background: white; ");                msg.append(" color: black; font-size: 12px\">");            }            if (!StringUtils.isEmpty(cd.getName()))            {                msg.append(cd.getName() + " "                            + resources.getString("email.comment.wrote")+": ");            }            else            {                msg.append(resources.getString("email.comment.anonymous")+": ");            }            msg.append((escapeHtml) ? "\n\n" : "<br /><br />");            msg.append(cd.getContent());            msg.append((escapeHtml) ? "\n\n----\n"                    : "<br /><br /><hr /><span style=\"font-size: 11px\">");            msg.append(resources.getString("email.comment.respond") + ": ");            msg.append((escapeHtml) ? "\n" : "<br />");            String rootURL = rc.getAbsoluteContextUrl(request);            if (rootURL == null || rootURL.trim().length()==0)            {            	rootURL = RequestUtils.serverURL(request) + request.getContextPath();            }            // Build link back to comment                        StringBuffer commentURL = new StringBuffer(rootURL);            commentURL.append("/comments/");            commentURL.append(rreq.getUser().getUserName());                        PageData page = rreq.getPage();            if (page == null)            {                commentURL.append("?entry=");            }            else            {                commentURL.append("/").append(page.getLink()).append("/");            }            commentURL.append(wd.getAnchor());	            if (escapeHtml)             {                msg.append(commentURL.toString());            }             else             {             msg.append("<a href=\""+commentURL+"\">"+commentURL+"</a></span>");            }                        ownermsg.append(msg);             // add link to weblog edit page so user can login to manage comments            ownermsg.append((escapeHtml) ? "\n\n----\n" :                    "<br /><br /><hr /><span style=\"font-size: 11px\">");            ownermsg.append("Link to comment management page:");                        ownermsg.append((escapeHtml) ? "\n" : "<br />");                        StringBuffer deleteURL = new StringBuffer(rootURL);			deleteURL.append("/editor/weblog.do?method=edit&entryid="+wd.getId());                                    if (escapeHtml)             {                 ownermsg.append(deleteURL.toString());            }             else             {                 ownermsg.append(                  "<a href=\"" + deleteURL + "\">" + deleteURL + "</a></span>");                 msg.append("</Body></html>");                 ownermsg.append("</Body></html>");            }                        String subject = null;            if ((subscribers.size() > 1) ||                 (StringUtils.equals(cd.getEmail(), user.getEmailAddress())))            {                subject= "RE: "+resources.getString("email.comment.title")+": ";            }            else            {                subject = resources.getString("email.comment.title") + ": ";            }            subject += wd.getTitle();            //------------------------------------------            // --- Send message to email recipients            try            {                javax.naming.Context ctx = (javax.naming.Context)                    new InitialContext().lookup("java:comp/env");                Session session = (Session)ctx.lookup("mail/Session");                boolean isHtml = !escapeHtml;                if (separateMessages)                {                    // Send separate messages to owner and commenters                    sendMessage(session, from,                        new String[]{user.getEmailAddress()}, null, null, subject, ownermsg.toString(), isHtml);                    if (commenterAddrs.length > 0)                    {                        // If hiding commenter addrs, they go in Bcc: otherwise in the To: of the second message                        String[] to = hideCommenterAddrs ? null : commenterAddrs;                        String[] bcc = hideCommenterAddrs ? commenterAddrs : null;                        sendMessage(session, from, to, null, bcc, subject, msg.toString(), isHtml);                    }                }                else                {                    // Single message.  User in To: header, commenters in either cc or bcc depending on hiding option                    String[] cc = hideCommenterAddrs ? null : commenterAddrs;                    String[] bcc = hideCommenterAddrs ? commenterAddrs : null;                    sendMessage(session, from, new String[]{user.getEmailAddress()}, cc, bcc, subject,                        ownermsg.toString(), isHtml);                }            }            catch (javax.naming.NamingException ne)            {                mLogger.error("Unable to lookup mail session.  Check configuration.  NamingException: " + ne.getMessage());            }            catch (Exception e)            {                mLogger.warn("Exception sending comment mail: " + e.getMessage());                // This will log the stack trace if debug is enabled                if (mLogger.isDebugEnabled())                {                    mLogger.debug(e);                }            }        } // if email enabled    }    // This is somewhat ridiculous, but avoids duplicating a bunch of logic in the already messy sendEmailNotification    private void sendMessage(Session session, String from, String[] to, String[] cc, String[] bcc, String subject,                             String msg, boolean isHtml) throws MessagingException    {        if (isHtml)            MailUtil.sendHTMLMessage(session, from, to, cc, bcc, subject, msg);        else            MailUtil.sendTextMessage(session, from, to, cc, bcc, subject, msg);    }    /* old method not used anymore -- Allen G    private boolean getBooleanContextParam(String paramName, boolean defaultValue) {        String paramValue = getServletContext().getInitParameter(paramName);        if (paramValue == null) return defaultValue;        return Boolean.valueOf(paramValue).booleanValue();    }    */}

⌨️ 快捷键说明

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