You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@roller.apache.org by ag...@apache.org on 2005/09/15 21:20:34 UTC
svn commit: r289293 - in /incubator/roller/branches/roller_2.0:
src/org/roller/presentation/CommentServlet.java
src/org/roller/presentation/velocity/CommentServlet.java
web/WEB-INF/classes/comments.vm
Author: agilliland
Date: Thu Sep 15 12:18:45 2005
New Revision: 289293
URL: http://svn.apache.org/viewcvs?rev=289293&view=rev
Log:
new comments architecture.
we've defined a new CommentServlet which only handles posted comments. the old CommentServlet has been rewritten to forward/redirect requests as appropriate. the comments.vm macros have been updated with new links.
Added:
incubator/roller/branches/roller_2.0/src/org/roller/presentation/CommentServlet.java
Modified:
incubator/roller/branches/roller_2.0/src/org/roller/presentation/velocity/CommentServlet.java
incubator/roller/branches/roller_2.0/web/WEB-INF/classes/comments.vm
Added: incubator/roller/branches/roller_2.0/src/org/roller/presentation/CommentServlet.java
URL: http://svn.apache.org/viewcvs/incubator/roller/branches/roller_2.0/src/org/roller/presentation/CommentServlet.java?rev=289293&view=auto
==============================================================================
--- incubator/roller/branches/roller_2.0/src/org/roller/presentation/CommentServlet.java (added)
+++ incubator/roller/branches/roller_2.0/src/org/roller/presentation/CommentServlet.java Thu Sep 15 12:18:45 2005
@@ -0,0 +1,452 @@
+package org.roller.presentation;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.util.ArrayList;
+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.RequestDispatcher;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpSession;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.struts.util.RequestUtils;
+import org.roller.RollerException;
+import org.roller.config.RollerConfig;
+import org.roller.config.RollerRuntimeConfig;
+import org.roller.model.IndexManager;
+import org.roller.model.RollerFactory;
+import org.roller.model.WeblogManager;
+import org.roller.pojos.CommentData;
+import org.roller.pojos.UserData;
+import org.roller.pojos.WeblogEntryData;
+import org.roller.pojos.WebsiteData;
+import org.roller.presentation.pagecache.PageCacheFilter;
+import org.roller.presentation.velocity.CommentAuthenticator;
+import org.roller.presentation.weblog.formbeans.CommentFormEx;
+import org.roller.util.CommentSpamChecker;
+import org.roller.util.MailUtil;
+import org.roller.util.StringUtils;
+
+
+/**
+ * The CommentServlet handles all incoming weblog entry comment posts.
+ *
+ * We validate each incoming comment based on various comment settings and
+ * if all checks are passed then the comment is saved.
+ *
+ * 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.
+ *
+ * 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.
+ *
+ * @author Allen Gilliland
+ *
+ * @web.servlet name="CommentServlet"
+ * @web.servlet-mapping url-pattern="/comment/*"
+ */
+public class CommentServlet extends HttpServlet {
+
+ private static final String EMAIL_ADDR_REGEXP = "^.*@.*[.].{2,}$";
+
+ 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);
+
+
+ /**
+ * Handle incoming http GET requests.
+ *
+ * The CommentServlet is not meant to handle GET requests, so we just
+ * redirect these request to the root of the webapp.
+ */
+ public void doGet(HttpServletRequest request, HttpServletResponse response)
+ throws IOException, ServletException {
+
+ // we should never get any GET requests, but just in case
+ response.sendRedirect(request.getContextPath());
+ }
+
+
+ /**
+ * Service incoming POST requests.
+ *
+ * Here we handle incoming comment postings. We will collect the data,
+ * validate it, and save it.
+ */
+ public void doPost(HttpServletRequest request, HttpServletResponse response)
+ throws IOException, ServletException {
+
+ boolean preview = false;
+ String error = null;
+ String message = null;
+ String entry_permalink = request.getContextPath();
+
+ String method = request.getParameter("method");
+ if(method == null)
+ method = "post";
+ else if (method.equals("preview"))
+ preview = true;
+
+ RollerRequest rreq = RollerRequest.getRollerRequest(request);
+ HttpSession session = request.getSession();
+ try {
+ // Get weblog entry object
+ WeblogEntryData entry = rreq.getWeblogEntry();
+ if (entry == null || entry.getId() == null) {
+ throw new RollerException("Unable to find WeblogEntry for "+
+ request.getParameter(RollerRequest.WEBLOGENTRYID_KEY));
+ }
+
+ // we know what our weblog entry is, so setup our permalink url
+ entry_permalink = entry.getPermaLink();
+
+ mLogger.debug("Doing comment posting for entry = "+entry_permalink);
+
+ if (!entry.getWebsite().getAllowComments().booleanValue() ||
+ !entry.getCommentsStillAllowed())
+ throw new Exception("Comments not allowed");
+
+ WebsiteData website = entry.getWebsite();
+
+ // Construct our Comment object from the submitted data
+ WeblogManager mgr = RollerFactory.getRoller().getWeblogManager();
+ CommentFormEx cf = new CommentFormEx();
+ CommentData comment = new CommentData();
+ RequestUtils.populate(cf, request);
+ cf.copyTo(comment, request.getLocale());
+
+ comment.setWeblogEntry(entry);
+ comment.setRemoteHost(request.getRemoteHost());
+ comment.setPostTime(new java.sql.Timestamp(System.currentTimeMillis()));
+
+ cf.setWeblogEntry(entry);
+ cf.setPostTime(new java.sql.Timestamp(System.currentTimeMillis()));
+
+ request.setAttribute("commentForm", cf);
+ request.setAttribute("blogEntry", entry);
+
+ if(preview) {
+ message = "This is a comment preview only";
+ request.setAttribute("previewComments", "dummy");
+
+ mLogger.debug("Comment is a preview");
+
+ } else if(!commentSpam(comment)) {
+
+ // this is a real comment posting.
+ // lets authenticate it then save
+ CommentAuthenticator commentAuth =
+ RollerContext.getCommentAuthenticator();
+ if (commentAuth.authenticate(comment, request)) {
+
+ mLogger.debug("Comment is valid ... saving it");
+
+ comment.save();
+ RollerFactory.getRoller().commit();
+ reindexEntry(entry);
+
+ // Refresh user's entries in page cache
+ PageCacheFilter.removeFromCache(request, website);
+
+ // Send email notifications
+ sendEmailNotification(request, rreq, entry, comment);
+
+ } else {
+ error = bundle.getString("error.commentAuthFailed");
+ mLogger.debug("Comment failed authentication");
+ }
+
+ } else {
+ error = COMMENT_SPAM_MSG;
+ mLogger.debug("Comment marked as spam");
+ }
+
+ } catch (RollerException re) {
+ mLogger.error("ERROR posting comment", re);
+ error = re.getMessage();
+ } catch (Exception e) {
+ error = e.getMessage();
+ }
+
+ // the work has been done, now send the user back to the entry page
+ if(error != null)
+ session.setAttribute(RollerSession.ERROR_MESSAGE, error);
+ else if(message != null)
+ session.setAttribute(RollerSession.STATUS_MESSAGE, message);
+
+ if(error == null && message == null && !preview) {
+ entry_permalink = request.getContextPath()+entry_permalink;
+
+ mLogger.debug("comment complete, redirecting to "+entry_permalink);
+ response.sendRedirect(entry_permalink);
+ } else {
+ mLogger.debug("more work needed, forwarding to "+entry_permalink);
+ RequestDispatcher dispatcher =
+ request.getRequestDispatcher(entry_permalink);
+ dispatcher.forward(request, response);
+ }
+ }
+
+
+ /**
+ * Re-index the WeblogEntry so that the new comment gets indexed.
+ */
+ private void reindexEntry(WeblogEntryData entry)
+ throws RollerException {
+
+ IndexManager manager = RollerFactory.getRoller().getIndexManager();
+
+ // remove entry before (re)adding it, or in case it isn't Published
+ manager.removeEntryIndexOperation(entry);
+
+ // if published, index the entry
+ if (entry.isPublished()) {
+ manager.addEntryIndexOperation(entry);
+ }
+ }
+
+
+ /**
+ * Test CommentData to see if it is spam.
+ */
+ private boolean commentSpam(CommentData cd) {
+
+ CommentSpamChecker checker = new CommentSpamChecker();
+ checker.testComment(cd);
+ if (cd.getSpam().booleanValue())
+ return true;
+
+ return false;
+ }
+
+
+ /**
+ * Send email notification of comment.
+ *
+ * TODO: Make the addressing options configurable on a per-website basis.
+ */
+ private void sendEmailNotification(HttpServletRequest request,
+ RollerRequest rreq,
+ WeblogEntryData entry,
+ CommentData cd)
+ throws MalformedURLException {
+
+ RollerContext rc = RollerContext.getRollerContext(request);
+ ResourceBundle resources = ResourceBundle.getBundle(
+ "ApplicationResources",LanguageUtil.getViewLocale(request));
+
+ WebsiteData site = entry.getWebsite();
+ UserData user = entry.getCreator();
+
+ // Send e-mail to owner and subscribed users (if enabled)
+ boolean notify = RollerRuntimeConfig.getBooleanProperty("users.comments.emailnotify");
+ if (notify && site.getEmailComments().booleanValue()) {
+ mLogger.debug("Comment notification enabled ... preparing email");
+
+ // 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
+
+ List comments = null;
+ try {
+ WeblogManager wMgr = RollerFactory.getRoller().getWeblogManager();
+ comments = wMgr.getComments(entry.getId());
+ } catch(RollerException re) {
+ // should never happen
+ comments = new ArrayList();
+ }
+
+ // 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(user.getUserName());
+
+ org.roller.pojos.Template page = rreq.getPage();
+ if (page == null) {
+ commentURL.append("?entry=");
+ } else {
+ commentURL.append("/").append(page.getLink()).append("/");
+ }
+
+ commentURL.append(entry.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="+entry.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 += entry.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);
+ }
+ }
+
+ mLogger.debug("Done sending email message");
+
+ } // 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);
+ }
+
+}
+
Modified: incubator/roller/branches/roller_2.0/src/org/roller/presentation/velocity/CommentServlet.java
URL: http://svn.apache.org/viewcvs/incubator/roller/branches/roller_2.0/src/org/roller/presentation/velocity/CommentServlet.java?rev=289293&r1=289292&r2=289293&view=diff
==============================================================================
--- incubator/roller/branches/roller_2.0/src/org/roller/presentation/velocity/CommentServlet.java (original)
+++ incubator/roller/branches/roller_2.0/src/org/roller/presentation/velocity/CommentServlet.java Thu Sep 15 12:18:45 2005
@@ -1,590 +1,95 @@
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.RequestDispatcher;
import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
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.RollerFactory;
-import org.roller.model.UserManager;
-import org.roller.model.WeblogManager;
-import org.roller.pojos.CommentData;
-import org.roller.pojos.WeblogTemplate;
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.
+ * This is the old Roller CommentServlet which used to extend the PageServlet
+ * and do some page rendering. This was replaced by the new version ...
+ * org.roller.persentation.CommentServlet which does not do any rendering.
*
- * @web.servlet name="CommentServlet"
+ * This servlet is left in place to maintain old urls and redirect them to
+ * their proper new location.
+ *
+ * @web.servlet name="CommentsServlet"
* @web.servlet-mapping url-pattern="/comments/*"
- * @web.servlet-init-param name="org.apache.velocity.properties"
- * value="/WEB-INF/velocity.properties"
*
- * @author Dave Johnson
+ * @author Allen Gilliland
*/
-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);
+public class CommentServlet extends HttpServlet {
+
+ 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 = RollerFactory.getRoller().getUserManager();
- WebsiteData website = rreq.getWebsite();
-
- // Request specifies popup
- org.roller.pojos.Template page = null;
- Exception pageException = null;
- try
- {
- // Does user have a popupcomments page?
- page = website.getPageByName("_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 WeblogTemplate("/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;
- }
-
+ * Handle GET requests.
+ *
+ * This method just responds to all GET requests with a 301 redirect
+ * because these old comment servlet urls are deprecated now.
+ */
+ public void doGet(HttpServletRequest request, HttpServletResponse response)
+ throws IOException, ServletException {
+
+ // a GET request means that this client is trying to use this old
+ // comment servlet as a form of permalink for rendering an entry.
+ // we want to discourage this, so we send a 301 response which means
+ // this url has been permanently moved.
+ String forward = request.getContextPath();
+
+ // calculate the location of the requested permalink
RollerRequest rreq = RollerRequest.getRollerRequest(request);
- HttpSession session = request.getSession();
- try
- {
- // Get weblog entry object, put in page context
- WeblogEntryData entry = rreq.getWeblogEntry();
- WebsiteData website = entry.getWebsite();
- if (entry == null || entry.getId() == null)
- {
- throw new RollerException(
- "Unable to find WeblogEntry for "
- + request.getParameter(RollerRequest.WEBLOGENTRYID_KEY));
- }
- if ( !entry.getWebsite().getAllowComments().booleanValue()
- || !entry.getCommentsStillAllowed())
- {
- throw new RollerException("ERROR comments not allowed");
- }
-
- request.setAttribute("blogEntry", entry);
-
- // TODO: A hack to be replaced by Object.canEdit()
- request.setAttribute(RollerRequest.OWNING_WEBSITE, website);
-
- // Save comment
- WeblogManager mgr = RollerFactory.getRoller().getWeblogManager();
- CommentFormEx cf = new CommentFormEx();
- CommentData cd = new CommentData();
- RequestUtils.populate(cf, request);
- cf.copyTo(cd, request.getLocale());
- cd.setWeblogEntry(entry);
- cd.setRemoteHost(request.getRemoteHost());
- cd.setPostTime(new java.sql.Timestamp(System.currentTimeMillis()));
-
- if (!testCommentSpam(cd, request))
- {
- if (RollerContext.getCommentAuthenticator().authenticate(cd, request))
- {
- cd.save();
- RollerFactory.getRoller().commit();
- reindexEntry(RollerFactory.getRoller(), entry);
-
- // Refresh user's entries in page cache
- PageCacheFilter.removeFromCache(request, website);
-
- // Put array of comments in context
- List comments = mgr.getComments(entry.getId());
- request.setAttribute("blogComments", comments);
-
- // MR: Added functionality to e-mail comments
- sendEmailNotification(request, rreq, entry, cd, website, comments);
-
- super.doPost(request, response);
- return;
- }
+ WeblogEntryData entry = rreq.getWeblogEntry();
+ if (entry != null) {
+ forward += entry.getPermaLink();
+
+ // make sure to propogate popup requests
+ if(request.getParameter("popup") != null) {
+ if(forward.indexOf("?") == -1)
+ forward += "?popup=true";
else
- {
- request.getSession().setAttribute(
- RollerSession.ERROR_MESSAGE,
- bundle.getString("error.commentAuthFailed"));
- }
+ forward += "&popup=true";
}
- 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_WEBSITE, wd);
-
- 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.isPublished());
- {
- manager.addEntryIndexOperation(entry);
+
}
+
+ mLogger.debug("forwarding to "+forward);
+
+ // send an HTTP 301 response
+ response.setStatus(response.SC_MOVED_PERMANENTLY);
+ response.setHeader("Location", forward);
}
- //-----------------------------------------------------------------------
- /**
- * 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,
- WebsiteData website,
- 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 = wd.getWebsite();
- }
- 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())
- {
- mLogger.debug("Comment notification enabled ... preparing email");
-
- // 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()))
- ? website.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(site.getHandle());
-
- org.roller.pojos.Template 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(), website.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[]{website.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[]{website.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);
- }
- }
-
- mLogger.debug("Done sending email message");
-
- } // if email enabled
+ * Handle POST requests.
+ *
+ * POST requests to this old CommentServlet are simply dispatched to the
+ * new CommentServlet inside our servlet container. We log these requests
+ * in the log file with a WARN so that site admins can track which users
+ * have hard coded the use of the old comment servlet and hopefully those
+ * users can be contacted about udating their templates.
+ */
+ public void doPost(HttpServletRequest request, HttpServletResponse response)
+ throws IOException, ServletException {
+
+ mLogger.warn(request.getHeader("referer")+
+ " is posting to the OLD CommentServlet");
+
+ // just dispatch to new CommentServlet
+ RequestDispatcher dispatch = request.getRequestDispatcher("/comment");
+ dispatch.forward(request, response);
}
- // 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();
- }
- */
}
Modified: incubator/roller/branches/roller_2.0/web/WEB-INF/classes/comments.vm
URL: http://svn.apache.org/viewcvs/incubator/roller/branches/roller_2.0/web/WEB-INF/classes/comments.vm?rev=289293&r1=289292&r2=289293&view=diff
==============================================================================
--- incubator/roller/branches/roller_2.0/web/WEB-INF/classes/comments.vm (original)
+++ incubator/roller/branches/roller_2.0/web/WEB-INF/classes/comments.vm Thu Sep 15 12:18:45 2005
@@ -11,7 +11,7 @@
#macro( showCommentsLink $entry )
#set( $commentCount = $pageModel.getCommentCount($entry.Id) )
#if (($entry.commentsStillAllowed && $website.allowComments) || $commentCount > 0)
- #set( $link = "$ctxPath/comments/$userName/$page.link/$utilities.encode($entry.anchor)?popup=true#comments" )
+ #set( $link = "$ctxPath$entry.permaLink&popup=true#comments" )
<a href="$link " onclick="window.open('$link', 'comments',
'width=480,height=480,scrollbars=yes,status=yes,resizable'); return false;"
class="entrycommentslink">$text.get( "macro.weblog.comments" ) [$commentCount]</a>
@@ -25,7 +25,7 @@
#macro( showCommentsPageLink $entry )
#set( $commentCount = $pageModel.getCommentCount($entry.Id) )
#if (($entry.commentsStillAllowed && $website.allowComments) || $commentCount > 0)
- #set( $link = "$ctxPath/comments/$userName/$page.link/$utilities.encode($entry.anchor)#comments" )
+ #set( $link = "$ctxPath$entry.permaLink#comments" )
<a href="$link" class="entrycommentslink">$text.get( "macro.weblog.comments" ) [$commentCount]</a>
#end
#end
@@ -41,7 +41,7 @@
<div class="comments" id="comments">
<div id="commentTwisty$entry.Id" class="commentTwisty"
onclick="toggleComments('$entry.Id', '$ctxPath/page/$userName'); return false;">
- <a href="$ctxPath/comments/$userName/$page.link/$utilities.encode($entry.anchor)" class="plain">
+ <a href="$ctxPath$entry.permaLink" class="plain">
#if($commentCount == 0)
$text.get( "macro.weblog.addcomment" )
#elseif($commentCount == 1)
@@ -79,7 +79,7 @@
$text.get( "macro.weblog.postedbywebsite", [$comment.url, $comment.url] )
#end
#if( $showPermalink )
- <a href="${ctxPath}/comments/${userName}/${page.link}/${entry.anchor}#comment${velocityCount}"
+ <a href="${ctxPath}${entry.permaLink}#comment${velocityCount}"
class="entrypermalink"
title="$text.get( "macro.weblog.commentpermalink.title" )">#</a>
#end
@@ -161,7 +161,7 @@
#set($content = $utilities.encodeEmail($comment.content))
#set($content = $utilities.addNofollow($content))
<li class="commentsListItem"><a
- href="${ctxPath}/comments/${userName}/${page.link}/${comment.weblogEntry.anchor}#comments"
+ href="${ctxPath}${comment.weblogEntry.permaLink}#comments"
class="entrypermalink"
title="$text.get( "macro.weblog.commentpermalink.title" ) to
'$utilities.removeHTML($comment.weblogEntry.title)'"
@@ -186,7 +186,7 @@
#macro( showCommentForm $entry )
<div class="comments-form">
<div class="comments-head">$text.get("macro.weblog.postcommentHeader")</div><br/>
- <form method="post" action="$ctxPath/comments" focus="name"
+ <form method="post" action="$ctxPath/comment" focus="name"
name="form" onsubmit="fixURL(this); return validateComments(this)">
#if($requestParameters.popup)
@@ -320,7 +320,7 @@
<div style="display: none" class="comments">
<div id="commentBoxTemplate" class="commentBox">
<form class="commentFormBox" id="commentForm"
- method="post" action="$ctxPath/comments"
+ method="post" action="$ctxPath/comment"
onsubmit="onSubmitComments(this.entryid.value);fixURL(this);return validateComments(this)">
<table>