📄 commentservlet.java
字号:
package org.roller.presentation.velocity;import java.io.IOException;import java.net.MalformedURLException;import java.util.Date;import java.util.Iterator;import java.util.List;import java.util.ResourceBundle;import java.util.Set;import java.util.TreeSet;import javax.mail.MessagingException;import javax.mail.Session;import javax.naming.InitialContext;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import javax.servlet.jsp.JspFactory;import javax.servlet.jsp.PageContext;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.apache.struts.util.RequestUtils;import org.apache.velocity.Template;import org.apache.velocity.context.Context;import org.roller.RollerException;import org.roller.config.RollerConfig;import org.roller.config.RollerRuntimeConfig;import org.roller.model.IndexManager;import org.roller.model.Roller;import org.roller.model.UserManager;import org.roller.model.WeblogManager;import org.roller.pojos.CommentData;import org.roller.pojos.PageData;import org.roller.pojos.UserData;import org.roller.pojos.WeblogEntryData;import org.roller.pojos.WebsiteData;import org.roller.presentation.LanguageUtil;import org.roller.presentation.RollerContext;import org.roller.presentation.RollerRequest;import org.roller.presentation.RollerSession;import org.roller.presentation.pagecache.PageCacheFilter;import org.roller.presentation.weblog.formbeans.CommentFormEx;import org.roller.util.CommentSpamChecker;import org.roller.util.MailUtil;import org.roller.util.StringUtils;/** * Extend PageServlet to do special handling needed to support viewing and * posting of comments. Handles in-page comments and popup-style comments. * <p /> * This servlet overrides the VelocityServlet's handleRequest method so that * the correct comments page template can be loaded for popup comments. * If this servlet is called with the request paramteter 'popup' * defined, then it will load the user's _popupcomments page and if no such * page is found it will use /popupcomments.vm which looks just like the old * pre-0.9.9 popup comments page. * <p /> * This servlet also overrides doPost() to handle review and posting of new * comments. If the request paramter 'method' is set to 'preview' then the * posted comment will be previewed, otherwise if it will be posted. * <p /> * Incoming comments are tested against the MT Blacklist. If they are found * to be spam, then they are marked as spam and hidden from view. * <p /> * If email notification is turned on, each new comment will result in an * email sent to the blog owner and all who have commented on the same post. * * @web.servlet name="CommentServlet" * @web.servlet-mapping url-pattern="/comments/*" * @web.servlet-init-param name="org.apache.velocity.properties" * value="/WEB-INF/velocity.properties" * * @author Dave Johnson */public class CommentServlet extends PageServlet { private static final String COMMENT_SPAM_MSG = "Your comment has been recognized as " + "<a href='http://www.jayallen.org/projects/mt-blacklist/'>" + "Comment Spam</a> and rejected."; private transient ResourceBundle bundle = ResourceBundle.getBundle("ApplicationResources"); private static Log mLogger = LogFactory.getFactory().getInstance(CommentServlet.class); //----------------------------------------------------------------------- /** * Override VelocityServlet so we can pick the right page and stick * the right stuff into the VelocityContext before page execution. */ public Template handleRequest( HttpServletRequest request, HttpServletResponse response, Context ctx ) throws Exception { Template template = null; if (request.getParameter("popup") == null) { // Request does not specify popup, so normal return template = super.handleRequest(request, response, ctx); } else { PageContext pageContext = JspFactory.getDefaultFactory().getPageContext( this, request, response,"", true, 8192, true); RollerRequest rreq= RollerRequest.getRollerRequest(pageContext); UserManager userMgr = rreq.getRoller().getUserManager(); WebsiteData website = rreq.getWebsite(); // Request specifies popup PageData page = null; Exception pageException = null; try { // Does user have a popupcomments page? page = userMgr.getPageByName(website, "_popupcomments"); } catch(Exception e ) { pageException = e; response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } if (pageException != null) { mLogger.error("EXCEPTION: in RollerServlet", pageException); request.setAttribute("DisplayException", pageException); } // User doesn't have one so return the default if (page == null) { page = new PageData("/popupcomments.vm", website, "Comments", "Comments", "dummy_link", "dummy_template", new Date()); } rreq.setPage(page); template = prepareForPageExecution(ctx, rreq, response, page); } return template; } //----------------------------------------------------------------------- /** * Handle POST from comment form, then hand off to super for page rendering. */ public void doPost( HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { if (request.getParameter("method") != null && request.getParameter("method").equals("preview")) { doPreviewPost(request, response); return; } RollerRequest rreq = RollerRequest.getRollerRequest(request); HttpSession session = request.getSession(); try { // Get weblog entry object, put in page context WeblogEntryData wd = rreq.getWeblogEntry(); if (wd == null || wd.getId() == null) { throw new RollerException( "Unable to find WeblogEntry for " + request.getParameter(RollerRequest.WEBLOGENTRYID_KEY)); } if ( !wd.getWebsite().getAllowComments().booleanValue() || !wd.getCommentsStillAllowed()) { throw new RollerException("ERROR comments not allowed"); } request.setAttribute("blogEntry", wd); // get the User to which the blog belongs UserData user = wd.getWebsite().getUser(); // TODO: A hack to be replaced by Object.canEdit() request.setAttribute(RollerRequest.OWNING_USER, user); // Save comment WeblogManager mgr = rreq.getRoller().getWeblogManager(); CommentFormEx cf = new CommentFormEx(); CommentData cd = new CommentData(); RequestUtils.populate(cf, request); cf.copyTo(cd, request.getLocale()); cd.setWeblogEntry(wd); cd.setRemoteHost(request.getRemoteHost()); cd.setPostTime(new java.sql.Timestamp(System.currentTimeMillis())); if (!testCommentSpam(cd, request)) { if (RollerContext.getCommentAuthenticator().authenticate(cd, request)) { cd.save(); rreq.getRoller().commit(); reindexEntry(rreq.getRoller(), wd); // Refresh user's entries in page cache PageCacheFilter.removeFromCache(request, user); // Put array of comments in context List comments = mgr.getComments(wd.getId()); request.setAttribute("blogComments", comments); // MR: Added functionality to e-mail comments sendEmailNotification(request, rreq, wd, cd, user,comments); super.doPost(request, response); return; } else { request.getSession().setAttribute( RollerSession.ERROR_MESSAGE, bundle.getString("error.commentAuthFailed")); } } doPreviewPost(request, response); } catch (Exception e) { mLogger.error("ERROR posting comment", e); // Noted: this never gets back to the user. Not sure why it is being set. session.setAttribute(RollerSession.ERROR_MESSAGE, e.getMessage()); } } //----------------------------------------------------------------------- /** * Load comment and blog entry objects and forward to either the popup * or the in-page comment page for comment preview. */ public void doPreviewPost( HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { RollerRequest rreq = RollerRequest.getRollerRequest(request); try { WeblogEntryData wd = rreq.getWeblogEntry(); if (wd == null || wd.getId() == null) { throw new RollerException( "Unable to find WeblogEntry for " + request.getParameter(RollerRequest.WEBLOGENTRYID_KEY)); } request.setAttribute("blogEntry", wd); // TODO: A hack to be replaced by Object.canEdit() request.setAttribute(RollerRequest.OWNING_USER, wd.getWebsite().getUser()); CommentFormEx cf = new CommentFormEx(); RequestUtils.populate(cf, request); cf.setWeblogEntry(wd); cf.setPostTime(new java.sql.Timestamp(System.currentTimeMillis())); request.setAttribute("commentForm", cf); request.setAttribute("previewComments","dummy"); } catch (Exception e) { // TODO: error message for browser and log mLogger.error(e); } super.doPost(request, response); } /** * Re-index the WeblogEntry so that the new comment gets indexed. * @param entry */ private void reindexEntry(Roller roller, WeblogEntryData entry) throws RollerException { IndexManager manager = roller.getIndexManager(); // remove entry before (re)adding it, or in case it isn't Published manager.removeEntryIndexOperation(entry); // if published, index the entry if (entry.getPublishEntry() == Boolean.TRUE) { manager.addEntryIndexOperation(entry); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -