Author: agilliland
Date: Sun Jun 3 15:13:06 2007
New Revision: 544003
URL: http://svn.apache.org/viewvc?view=rev&rev=544003
Log:
move mail sending code to MailUtil class.
Modified:
roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/ui/rendering/servlets/CommentServlet.java
roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/ui/rendering/servlets/TrackbackServlet.java
roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/util/MailUtil.java
roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/util/Utilities.java
Modified:
roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/ui/rendering/servlets/CommentServlet.java
URL:
http://svn.apache.org/viewvc/roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/ui/rendering/servlets/CommentServlet.java?view=diff&rev=544003&r1=544002&r2=544003
==============================================================================
---
roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/ui/rendering/servlets/CommentServlet.java
(original)
+++
roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/ui/rendering/servlets/CommentServlet.java
Sun Jun 3 15:13:06 2007
@@ -20,22 +20,12 @@
import java.io.IOException;
import java.sql.Timestamp;
-import java.text.MessageFormat;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Set;
-import java.util.TreeSet;
-import javax.mail.MessagingException;
-import javax.mail.Session;
-import javax.naming.Context;
-import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.roller.weblogger.WebloggerException;
@@ -46,10 +36,8 @@
import org.apache.roller.weblogger.business.UserManager;
import org.apache.roller.weblogger.business.WeblogManager;
import org.apache.roller.weblogger.pojos.WeblogEntryComment;
-import org.apache.roller.weblogger.pojos.User;
import org.apache.roller.weblogger.pojos.WeblogEntry;
import org.apache.roller.weblogger.pojos.Weblog;
-import org.apache.roller.weblogger.ui.rendering.model.UtilitiesModel;
import org.apache.roller.weblogger.ui.rendering.util.CommentAuthenticator;
import org.apache.roller.weblogger.ui.rendering.util.CommentValidationManager;
import
org.apache.roller.weblogger.ui.rendering.util.DefaultCommentAuthenticator;
@@ -60,9 +48,7 @@
import org.apache.roller.weblogger.util.MailUtil;
import org.apache.roller.weblogger.util.I18nMessages;
import org.apache.roller.weblogger.util.RollerMessages;
-import org.apache.roller.weblogger.util.RollerMessages.RollerMessage;
import org.apache.roller.weblogger.util.URLUtilities;
-import org.apache.roller.weblogger.util.Utilities;
import org.apache.roller.weblogger.util.cache.CacheManager;
@@ -77,16 +63,11 @@
*
* 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" load-on-startup="7"
- * @web.servlet-mapping url-pattern="/roller-ui/rendering/comment/*"
*/
public class CommentServlet extends HttpServlet {
private static Log log = LogFactory.getLog(CommentServlet.class);
- private static final String EMAIL_ADDR_REGEXP = "[EMAIL PROTECTED],}$";
-
private CommentAuthenticator authenticator = null;
private CommentValidationManager commentValidationManager = null;
private GenericThrottle commentThrottle = null;
@@ -157,8 +138,7 @@
* The CommentServlet does not support GET requests, it's a 404.
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws IOException, ServletException {
-
+ throws IOException, ServletException {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
}
@@ -169,7 +149,7 @@
* Here we handle incoming comment postings.
*/
public void doPost(HttpServletRequest request, HttpServletResponse
response)
- throws IOException, ServletException {
+ throws IOException, ServletException {
String error = null;
String dispatch_url = null;
@@ -237,7 +217,7 @@
}
- log.debug("Doing comment posting for entry = "+entry.getPermaLink());
+ log.debug("Doing comment posting for entry = "+entry.getPermalink());
// collect input from request params and construct new comment object
// fields: name, email, url, content, notify
@@ -307,14 +287,21 @@
mgr.saveComment(comment);
RollerFactory.getRoller().flush();
- // Send email notifications, but only to subscribers if
comment is 100% valid
+ // Send email notifications only to subscribers if comment
is 100% valid
boolean notifySubscribers = (validationScore == 100);
- String rootURL =
RollerRuntimeConfig.getAbsoluteContextURL();
- sendEmailNotification(comment, notifySubscribers,
messages, rootURL, messageUtils);
+ MailUtil.sendEmailNotification(comment, messages,
messageUtils, notifySubscribers);
// only re-index/invalidate the cache if comment isn't
moderated
if(!weblog.getCommentModerationRequired()) {
- reindexEntry(entry);
+ 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);
+ }
// Clear all caches associated with comment
CacheManager.invalidate(comment);
@@ -344,313 +331,6 @@
RequestDispatcher dispatcher =
request.getRequestDispatcher(dispatch_url);
dispatcher.forward(request, response);
- }
-
-
- /**
- * Re-index the WeblogEntry so that the new comment gets indexed.
- */
- private void reindexEntry(WeblogEntry entry)
- throws WebloggerException {
-
- 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);
- }
- }
-
-
- /**
- * Send email notification of new or newly approved comment.
- * TODO: Make the addressing options configurable on a per-website basis.
- *
- * @param commentObject The new comment
- * @param notifySubscribers True if subscribers are to be notified
- * @param messages Messages to be included in e-mail (or null).
- * Errors will be assumed to be "validation
errors"
- * and messages will be assumed to be "from the
system"
- * @param rootURL Root URL of the Roller site
- */
- public static void sendEmailNotification(
- WeblogEntryComment commentObject, boolean notifySubscribers,
- RollerMessages messages, String rootURL, I18nMessages resources) {
-
- WeblogEntry entry = commentObject.getWeblogEntry();
- Weblog site = entry.getWebsite();
- User 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()) {
- log.debug("Comment notification enabled ... preparing email");
-
- // Determine message and addressing options from init parameters
- 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 = entry.getComments(true, true);
-
- Set subscribers = new TreeSet();
-
- // If we are to notify subscribers, then...
- if (notifySubscribers) {
- log.debug("Sending notification email to all subscribers");
-
- // Get all the subscribers to this comment thread
- for (Iterator it = comments.iterator(); it.hasNext();) {
- WeblogEntryComment comment = (WeblogEntryComment)
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());
- }
- }
- }
- } else {
- log.debug("Sending notification email only to weblog owner");
- }
-
- // Form array of commenter addrs
- String[] commenterAddrs = (String[])subscribers.toArray(new
String[0]);
-
- //------------------------------------------
- // --- Form the messages to be sent -
- // Build separate owner and commenter (aka subscriber) messages
-
- // Determine with mime type to use for e-mail
- StringBuffer msg = new StringBuffer();
- StringBuffer ownermsg = new StringBuffer();
- boolean escapeHtml =
RollerRuntimeConfig.getBooleanProperty("users.comments.escapehtml");
-
- // first the commenter message
-
- if (!escapeHtml) {
- msg.append("<html><body style=\"background: white; ");
- msg.append(" color: black; font-size: 12px\">");
- }
-
- if (!StringUtils.isEmpty(commentObject.getName())) {
- msg.append(commentObject.getName() + " "
- + resources.getString("email.comment.wrote")+": ");
- } else {
- msg.append(resources.getString("email.comment.anonymous")+":
");
- }
-
- msg.append((escapeHtml) ? "\n\n" : "<br /><br />");
-
- msg.append((escapeHtml) ?
Utilities.escapeHTML(commentObject.getContent())
- :
UtilitiesModel.transformToHTMLSubset(Utilities.escapeHTML(commentObject.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 />");
-
- // Build link back to comment
- StringBuffer commentURL = new StringBuffer(rootURL);
- commentURL.append(entry.getPermaLink());
- commentURL.append("#comments");
-
- if (escapeHtml) {
- msg.append(commentURL.toString());
- } else {
- msg.append("<a
href=\""+commentURL+"\">"+commentURL+"</a></span>");
- }
-
- // next the owner message
-
- // First, list any messages from the system that were passed in:
- if (messages.getMessageCount() > 0) {
- ownermsg.append((escapeHtml) ? "" : "<p>");
-
ownermsg.append(resources.getString("commentServlet.email.thereAreSystemMessages"));
- ownermsg.append((escapeHtml) ? "\n\n" : "</p>");
- ownermsg.append((escapeHtml) ? "" : "<ul>");
- }
- for (Iterator it = messages.getMessages(); it.hasNext();) {
- RollerMessage rollerMessage = (RollerMessage)it.next();
- ownermsg.append((escapeHtml) ? "" : "<li>");
-
ownermsg.append(MessageFormat.format(resources.getString(rollerMessage.getKey()),
(Object[])rollerMessage.getArgs()) );
- ownermsg.append((escapeHtml) ? "\n\n" : "</li>");
- }
- if (messages.getMessageCount() > 0) {
- ownermsg.append((escapeHtml) ? "\n\n" : "</ul>");
- }
-
- // Next, list any validation error messages that were passed in:
- if (messages.getErrorCount() > 0) {
- ownermsg.append((escapeHtml) ? "" : "<p>");
-
ownermsg.append(resources.getString("commentServlet.email.thereAreErrorMessages"));
- ownermsg.append((escapeHtml) ? "\n\n" : "</p>");
- ownermsg.append((escapeHtml) ? "" : "<ul>");
- }
- for (Iterator it = messages.getErrors(); it.hasNext();) {
- RollerMessage rollerMessage = (RollerMessage)it.next();
- ownermsg.append((escapeHtml) ? "" : "<li>");
-
ownermsg.append(MessageFormat.format(resources.getString(rollerMessage.getKey()),
(Object[])rollerMessage.getArgs()) );
- ownermsg.append((escapeHtml) ? "\n\n" : "</li>");
- }
- if (messages.getErrorCount() > 0) {
- ownermsg.append((escapeHtml) ? "\n\n" : "</ul>");
- }
-
- 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("/roller-ui/authoring/comments.rol?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(commentObject.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 {
- boolean isHtml = !escapeHtml;
- // Send separate messages to owner and commenters
- sendMessage(
- from, new String[]{user.getEmailAddress()}, null,
- null, subject, ownermsg.toString(), isHtml);
- if (notifySubscribers && 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(from, to, null, bcc, subject, msg.toString(),
isHtml);
-
- }
- } catch (Exception e) {
- log.warn("Exception sending comment mail: " + e.getMessage());
- // This will log the stack trace if debug is enabled
- if (log.isDebugEnabled()) {
- log.debug(e);
- }
- }
-
- log.debug("Done sending email message");
-
- } // if email enabled
- }
-
-
- /**
- * Send message to author of approved comment
- *
- * TODO: Make the addressing options configurable on a per-website basis.
- */
- public static void sendEmailApprovalNotification(WeblogEntryComment cd,
String rootURL, I18nMessages resources) {
-
- WeblogEntry entry = cd.getWeblogEntry();
- Weblog site = entry.getWebsite();
- User user = entry.getCreator();
-
- // Only send email if email notificaiton is enabled
- boolean notify =
RollerRuntimeConfig.getBooleanProperty("users.comments.emailnotify");
- if (notify && site.getEmailComments().booleanValue()) {
- log.debug("Comment notification enabled ... preparing email");
-
-
-
- //------------------------------------------
- // --- 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();
-
- //------------------------------------------
- // --- Form the message to be sent -
-
- String subject =
resources.getString("email.comment.commentApproved");
-
- StringBuffer msg = new StringBuffer();
- msg.append(resources.getString("email.comment.commentApproved"));
- msg.append("\n\n");
-
- // Build link back to comment
- StringBuffer commentURL = new StringBuffer();
- commentURL.append(entry.getPermalink());
- commentURL.append("#comments");
- msg.append(commentURL.toString());
-
- //------------------------------------------
- // --- Send message to author of approved comment
- try {
- String[] cc = null;
- String[] bcc = null;
- sendMessage(from,
- new String[] {cd.getEmail()},
- null, // cc
- null, // bcc
- subject, msg.toString(), false);
- } catch (Exception e) {
- log.warn("Exception sending comment mail: " + e.getMessage());
- // This will log the stack trace if debug is enabled
- if (log.isDebugEnabled()) {
- log.debug(e);
- }
- }
-
- log.debug("Done sending email message");
-
- } // if email enabled
- }
-
-
- /*
- * This is somewhat ridiculous, but avoids duplicating a bunch of logic
- * in the already messy sendEmailNotification.
- */
- static void sendMessage(String from, String[] to, String[] cc, String[]
bcc, String subject,
- String msg, boolean isHtml) throws MessagingException {
- if (isHtml)
- MailUtil.sendHTMLMessage(from, to, cc, bcc, subject, msg);
- else
- MailUtil.sendTextMessage(from, to, cc, bcc, subject, msg);
}
}
Modified:
roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/ui/rendering/servlets/TrackbackServlet.java
URL:
http://svn.apache.org/viewvc/roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/ui/rendering/servlets/TrackbackServlet.java?view=diff&rev=544003&r1=544002&r2=544003
==============================================================================
---
roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/ui/rendering/servlets/TrackbackServlet.java
(original)
+++
roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/ui/rendering/servlets/TrackbackServlet.java
Sun Jun 3 15:13:06 2007
@@ -41,6 +41,7 @@
import
org.apache.roller.weblogger.ui.rendering.util.TrackbackLinkbackCommentValidator;
import org.apache.roller.weblogger.ui.rendering.util.WeblogTrackbackRequest;
import org.apache.roller.weblogger.util.I18nMessages;
+import org.apache.roller.weblogger.util.MailUtil;
import org.apache.roller.weblogger.util.RollerMessages;
import org.apache.roller.weblogger.util.cache.CacheManager;
@@ -191,9 +192,9 @@
}
// Send email notifications
- String rootURL =
RollerRuntimeConfig.getAbsoluteContextURL();
- CommentServlet.sendEmailNotification(comment,
validationScore == 100, messages, rootURL,
-
I18nMessages.getMessages(trackbackRequest.getLocaleInstance()));
+ MailUtil.sendEmailNotification(comment, messages,
+
I18nMessages.getMessages(trackbackRequest.getLocaleInstance()),
+ validationScore == 100);
if(WeblogEntryComment.PENDING.equals(comment.getStatus()))
{
pw.println(this.getSuccessResponse("Trackback
submitted to moderator"));
Modified:
roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/util/MailUtil.java
URL:
http://svn.apache.org/viewvc/roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/util/MailUtil.java?view=diff&rev=544003&r1=544002&r2=544003
==============================================================================
---
roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/util/MailUtil.java
(original)
+++
roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/util/MailUtil.java
Sun Jun 3 15:13:06 2007
@@ -20,12 +20,13 @@
import java.text.MessageFormat;
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
-import java.util.Locale;
+import java.util.Map;
import java.util.ResourceBundle;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
+import java.util.Set;
+import java.util.TreeSet;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.SendFailedException;
@@ -35,15 +36,20 @@
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.apache.commons.lang.StringUtils;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
import org.apache.roller.weblogger.WebloggerException;
import org.apache.roller.weblogger.business.MailProvider;
import org.apache.roller.weblogger.business.RollerFactory;
import org.apache.roller.weblogger.business.UserManager;
+import org.apache.roller.weblogger.config.RollerConfig;
import org.apache.roller.weblogger.config.RollerRuntimeConfig;
import org.apache.roller.weblogger.pojos.WeblogPermission;
import org.apache.roller.weblogger.pojos.User;
import org.apache.roller.weblogger.pojos.WeblogEntry;
import org.apache.roller.weblogger.pojos.Weblog;
+import org.apache.roller.weblogger.pojos.WeblogEntryComment;
+import org.apache.roller.weblogger.util.RollerMessages.RollerMessage;
/**
@@ -53,6 +59,9 @@
private static Log log = LogFactory.getLog(MailUtil.class);
+ private static final String EMAIL_ADDR_REGEXP = "[EMAIL PROTECTED],}$";
+
+
/**
* Ideally mail senders should call this first to avoid errors that occur
* when mail is not properly configured. We'll complain about that at
@@ -236,6 +245,304 @@
}
+ /**
+ * Send email notification of new or newly approved comment.
+ * TODO: Make the addressing options configurable on a per-website basis.
+ *
+ * @param commentObject The new comment
+ * @param messages Messages to be included in e-mail (or null).
+ * Errors will be assumed to be "validation
errors"
+ * and messages will be assumed to be "from the
system"
+ */
+ public static void sendEmailNotification(WeblogEntryComment commentObject,
+ RollerMessages messages,
+ I18nMessages resources,
+ boolean notifySubscribers)
+ throws MailingException {
+
+ WeblogEntry entry = commentObject.getWeblogEntry();
+ Weblog weblog = entry.getWebsite();
+ User user = entry.getCreator();
+
+ // Only send email if email notificaiton is enabled
+ boolean notify =
RollerRuntimeConfig.getBooleanProperty("users.comments.emailnotify");
+ if (!notify || !weblog.getEmailComments().booleanValue()) {
+ // notifications disabled, just bail
+ return;
+ }
+
+ log.debug("Comment notification enabled ... preparing email");
+
+ // Determine message and addressing options from init parameters
+ boolean hideCommenterAddrs = RollerConfig.getBooleanProperty(
+ "comment.notification.hideCommenterAddresses");
+
+ // use either the weblog configured from address or the site
configured from address
+ String from = weblog.getEmailFromAddress();
+ if(StringUtils.isEmpty(from)) {
+ // TODO: this should not be the users email address
+ from = user.getEmailAddress();
+ }
+
+ // build list of email addresses to send notification to
+ Set subscribers = new TreeSet();
+
+ // If we are to notify subscribers, then...
+ if (notifySubscribers) {
+ log.debug("Sending notification email to all subscribers");
+
+ // Get all the subscribers to this comment thread
+ List comments = entry.getComments(true, true);
+ for (Iterator it = comments.iterator(); it.hasNext();) {
+ WeblogEntryComment comment = (WeblogEntryComment) 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());
+ }
+ }
+ }
+ } else {
+ log.debug("Sending notification email only to weblog owner");
+ }
+
+ // Form array of commenter addrs
+ String[] commenterAddrs = (String[])subscribers.toArray(new String[0]);
+
+ //------------------------------------------
+ // --- Form the messages to be sent -
+ // Build separate owner and commenter (aka subscriber) messages
+
+ // Determine with mime type to use for e-mail
+ StringBuffer msg = new StringBuffer();
+ StringBuffer ownermsg = new StringBuffer();
+ boolean escapeHtml =
RollerRuntimeConfig.getBooleanProperty("users.comments.escapehtml");
+
+ // first the commenter message
+
+ if (!escapeHtml) {
+ msg.append("<html><body style=\"background: white; ");
+ msg.append(" color: black; font-size: 12px\">");
+ }
+
+ if (!StringUtils.isEmpty(commentObject.getName())) {
+ msg.append(commentObject.getName() + " "
+ + resources.getString("email.comment.wrote")+": ");
+ } else {
+ msg.append(resources.getString("email.comment.anonymous")+": ");
+ }
+
+ msg.append((escapeHtml) ? "\n\n" : "<br /><br />");
+
+ msg.append((escapeHtml) ?
Utilities.escapeHTML(commentObject.getContent())
+ :
Utilities.transformToHTMLSubset(Utilities.escapeHTML(commentObject.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 />");
+
+ // Build link back to comment
+ String commentURL = URLUtilities.getWeblogCommentsURL(weblog, null,
entry.getAnchor(), true);
+
+ if (escapeHtml) {
+ msg.append(commentURL);
+ } else {
+ msg.append("<a href=\""+commentURL+"\">"+commentURL+"</a></span>");
+ }
+
+ // next the owner message
+
+ // First, list any messages from the system that were passed in:
+ if (messages.getMessageCount() > 0) {
+ ownermsg.append((escapeHtml) ? "" : "<p>");
+
ownermsg.append(resources.getString("commentServlet.email.thereAreSystemMessages"));
+ ownermsg.append((escapeHtml) ? "\n\n" : "</p>");
+ ownermsg.append((escapeHtml) ? "" : "<ul>");
+ }
+ for (Iterator it = messages.getMessages(); it.hasNext();) {
+ RollerMessage rollerMessage = (RollerMessage)it.next();
+ ownermsg.append((escapeHtml) ? "" : "<li>");
+
ownermsg.append(MessageFormat.format(resources.getString(rollerMessage.getKey()),
(Object[])rollerMessage.getArgs()) );
+ ownermsg.append((escapeHtml) ? "\n\n" : "</li>");
+ }
+ if (messages.getMessageCount() > 0) {
+ ownermsg.append((escapeHtml) ? "\n\n" : "</ul>");
+ }
+
+ // Next, list any validation error messages that were passed in:
+ if (messages.getErrorCount() > 0) {
+ ownermsg.append((escapeHtml) ? "" : "<p>");
+
ownermsg.append(resources.getString("commentServlet.email.thereAreErrorMessages"));
+ ownermsg.append((escapeHtml) ? "\n\n" : "</p>");
+ ownermsg.append((escapeHtml) ? "" : "<ul>");
+ }
+ for (Iterator it = messages.getErrors(); it.hasNext();) {
+ RollerMessage rollerMessage = (RollerMessage)it.next();
+ ownermsg.append((escapeHtml) ? "" : "<li>");
+
ownermsg.append(MessageFormat.format(resources.getString(rollerMessage.getKey()),
(Object[])rollerMessage.getArgs()) );
+ ownermsg.append((escapeHtml) ? "\n\n" : "</li>");
+ }
+ if (messages.getErrorCount() > 0) {
+ ownermsg.append((escapeHtml) ? "\n\n" : "</ul>");
+ }
+
+ 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 />");
+
+ Map<String, String> parameters = new HashMap();
+ parameters.put("entryId", entry.getId());
+ String deleteURL = URLUtilities.getActionURL(
+ "comments", "/roller-ui/authoring", weblog.getHandle(),
parameters, true);
+
+ if (escapeHtml) {
+ ownermsg.append(deleteURL);
+ } 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(commentObject.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 {
+ boolean isHtml = !escapeHtml;
+
+ // Send separate messages to owner and commenters
+ if(isHtml) {
+ sendHTMLMessage(
+ from,
+ new String[]{user.getEmailAddress()},
+ null,
+ null,
+ subject,
+ ownermsg.toString());
+ } else {
+ sendTextMessage(
+ from,
+ new String[]{user.getEmailAddress()},
+ null,
+ null,
+ subject,
+ ownermsg.toString());
+ }
+
+ // now send to subscribers
+ if (notifySubscribers && 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;
+
+ if(isHtml) {
+ sendHTMLMessage(
+ from,
+ to,
+ null,
+ bcc,
+ subject,
+ msg.toString());
+ } else {
+ sendTextMessage(
+ from,
+ to,
+ null,
+ bcc,
+ subject,
+ msg.toString());
+ }
+ }
+ } catch (Exception e) {
+ log.warn("Exception sending comment mail: " + e.getMessage());
+ // This will log the stack trace if debug is enabled
+ if (log.isDebugEnabled()) {
+ log.debug(e);
+ }
+ }
+
+ log.debug("Done sending email message");
+ }
+
+
+ /**
+ * Send message to author of approved comment
+ *
+ * TODO: Make the addressing options configurable on a per-website basis.
+ */
+ public static void sendEmailApprovalNotification(WeblogEntryComment cd,
+ I18nMessages resources)
+ throws MailingException {
+
+ WeblogEntry entry = cd.getWeblogEntry();
+ Weblog weblog = entry.getWebsite();
+ User user = entry.getCreator();
+
+ // Only send email if email notificaiton is enabled
+ boolean notify =
RollerRuntimeConfig.getBooleanProperty("users.comments.emailnotify");
+ if (!notify || !weblog.getEmailComments().booleanValue()) {
+ // notifications disabled, just bail
+ return;
+ }
+
+ log.debug("Comment notification enabled ... preparing email");
+
+ // use either the weblog configured from address or the site
configured from address
+ String from = weblog.getEmailFromAddress();
+ if(StringUtils.isEmpty(from)) {
+ // TODO: this should not be the users email address
+ from = user.getEmailAddress();
+ }
+
+ // form the message to be sent
+ String subject = resources.getString("email.comment.commentApproved");
+
+ StringBuffer msg = new StringBuffer();
+ msg.append(resources.getString("email.comment.commentApproved"));
+ msg.append("\n\n");
+ msg.append(URLUtilities.getWeblogCommentsURL(weblog, null,
entry.getAnchor(), true));
+
+ // send message to author of approved comment
+ try {
+ sendTextMessage(
+ from, // from
+ new String[] {cd.getEmail()}, // to
+ null, // cc
+ null, // bcc
+ subject, // subject
+ msg.toString()); // message
+ } catch (Exception e) {
+ log.warn("Exception sending comment mail: " + e.getMessage());
+ // This will log the stack trace if debug is enabled
+ if (log.isDebugEnabled()) {
+ log.debug(e);
+ }
+ }
+
+ log.debug("Done sending email message");
+ }
+
+
+
// agangolli: Incorporated suggested changes from Ken Blackler.
/**
@@ -514,4 +821,13 @@
sendMessage(from, recipient, cc, bcc, subject, content, "text/html;
charset=utf-8");
}
+
+ /**
+ * An exception thrown if there is a problem sending an email.
+ */
+ public class MailingException extends WebloggerException {
+ public MailingException(Throwable t) {
+ super(t);
+ }
+ }
}
Modified:
roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/util/Utilities.java
URL:
http://svn.apache.org/viewvc/roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/util/Utilities.java?view=diff&rev=544003&r1=544002&r2=544003
==============================================================================
---
roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/util/Utilities.java
(original)
+++
roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/util/Utilities.java
Sun Jun 3 15:13:06 2007
@@ -18,6 +18,8 @@
import java.util.Locale;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
@@ -35,6 +37,50 @@
public final static String TAG_SPLIT_CHARS = " ,\n\r\f\t";
+ private static Pattern mLinkPattern =
+ Pattern.compile("<a href=.*?>", Pattern.CASE_INSENSITIVE);
+ private static final Pattern OPENING_B_TAG_PATTERN =
+ Pattern.compile("<b>", Pattern.CASE_INSENSITIVE);
+ private static final Pattern CLOSING_B_TAG_PATTERN =
+ Pattern.compile("</b>", Pattern.CASE_INSENSITIVE);
+ private static final Pattern OPENING_I_TAG_PATTERN =
+ Pattern.compile("<i>", Pattern.CASE_INSENSITIVE);
+ private static final Pattern CLOSING_I_TAG_PATTERN =
+ Pattern.compile("</i>", Pattern.CASE_INSENSITIVE);
+ private static final Pattern OPENING_BLOCKQUOTE_TAG_PATTERN =
+ Pattern.compile("<blockquote>", Pattern.CASE_INSENSITIVE);
+ private static final Pattern CLOSING_BLOCKQUOTE_TAG_PATTERN =
+ Pattern.compile("</blockquote>", Pattern.CASE_INSENSITIVE);
+ private static final Pattern BR_TAG_PATTERN =
+ Pattern.compile("<br */*>", Pattern.CASE_INSENSITIVE);
+ private static final Pattern OPENING_P_TAG_PATTERN =
+ Pattern.compile("<p>", Pattern.CASE_INSENSITIVE);
+ private static final Pattern CLOSING_P_TAG_PATTERN =
+ Pattern.compile("</p>", Pattern.CASE_INSENSITIVE);
+ private static final Pattern OPENING_PRE_TAG_PATTERN =
+ Pattern.compile("<pre>", Pattern.CASE_INSENSITIVE);
+ private static final Pattern CLOSING_PRE_TAG_PATTERN =
+ Pattern.compile("</pre>", Pattern.CASE_INSENSITIVE);
+ private static final Pattern OPENING_UL_TAG_PATTERN =
+ Pattern.compile("<ul>", Pattern.CASE_INSENSITIVE);
+ private static final Pattern CLOSING_UL_TAG_PATTERN =
+ Pattern.compile("</ul>", Pattern.CASE_INSENSITIVE);
+ private static final Pattern OPENING_OL_TAG_PATTERN =
+ Pattern.compile("<ol>", Pattern.CASE_INSENSITIVE);
+ private static final Pattern CLOSING_OL_TAG_PATTERN =
+ Pattern.compile("</ol>", Pattern.CASE_INSENSITIVE);
+ private static final Pattern OPENING_LI_TAG_PATTERN =
+ Pattern.compile("<li>", Pattern.CASE_INSENSITIVE);
+ private static final Pattern CLOSING_LI_TAG_PATTERN =
+ Pattern.compile("</li>", Pattern.CASE_INSENSITIVE);
+ private static final Pattern CLOSING_A_TAG_PATTERN =
+ Pattern.compile("</a>", Pattern.CASE_INSENSITIVE);
+ private static final Pattern OPENING_A_TAG_PATTERN =
+ Pattern.compile("<a href=.*?>", Pattern.CASE_INSENSITIVE);
+ private static final Pattern QUOTE_PATTERN =
+ Pattern.compile(""", Pattern.CASE_INSENSITIVE);
+
+
//------------------------------------------------------------------------
/** Strip jsessionid off of a URL */
public static String stripJsessionId( String url ) {
@@ -848,4 +894,65 @@
return Collections.EMPTY_LIST;
return Arrays.asList(tagsarr);
}
+
+
+ /**
+ * Transforms the given String into a subset of HTML displayable on a web
+ * page. The subset includes <b>, <i>, <p>, <br>,
+ * <pre> and <a href> (and their corresponding end tags).
+ *
+ * @param s the String to transform
+ * @return the transformed String
+ */
+ public static String transformToHTMLSubset(String s) {
+
+ if (s == null) {
+ return null;
+ }
+
+ s = replace(s, OPENING_B_TAG_PATTERN, "<b>");
+ s = replace(s, CLOSING_B_TAG_PATTERN, "</b>");
+ s = replace(s, OPENING_I_TAG_PATTERN, "<i>");
+ s = replace(s, CLOSING_I_TAG_PATTERN, "</i>");
+ s = replace(s, OPENING_BLOCKQUOTE_TAG_PATTERN, "<blockquote>");
+ s = replace(s, CLOSING_BLOCKQUOTE_TAG_PATTERN, "</blockquote>");
+ s = replace(s, BR_TAG_PATTERN, "<br />");
+ s = replace(s, OPENING_P_TAG_PATTERN, "<p>");
+ s = replace(s, CLOSING_P_TAG_PATTERN, "</p>");
+ s = replace(s, OPENING_PRE_TAG_PATTERN, "<pre>");
+ s = replace(s, CLOSING_PRE_TAG_PATTERN, "</pre>");
+ s = replace(s, OPENING_UL_TAG_PATTERN, "<ul>");
+ s = replace(s, CLOSING_UL_TAG_PATTERN, "</ul>");
+ s = replace(s, OPENING_OL_TAG_PATTERN, "<ol>");
+ s = replace(s, CLOSING_OL_TAG_PATTERN, "</ol>");
+ s = replace(s, OPENING_LI_TAG_PATTERN, "<li>");
+ s = replace(s, CLOSING_LI_TAG_PATTERN, "</li>");
+ s = replace(s, QUOTE_PATTERN, "\"");
+
+ // HTTP links
+ s = replace(s, CLOSING_A_TAG_PATTERN, "</a>");
+ Matcher m = OPENING_A_TAG_PATTERN.matcher(s);
+ while (m.find()) {
+ int start = m.start();
+ int end = m.end();
+ String link = s.substring(start, end);
+ link = "<" + link.substring(4, link.length() - 4) + ">";
+ s = s.substring(0, start) + link + s.substring(end, s.length());
+ m = OPENING_A_TAG_PATTERN.matcher(s);
+ }
+
+ // escaped angle brackets
+ s = s.replaceAll("&lt;", "<");
+ s = s.replaceAll("&gt;", ">");
+ s = s.replaceAll("&#", "&#");
+
+ return s;
+ }
+
+
+ private static String replace(String string, Pattern pattern, String
replacement) {
+ Matcher m = pattern.matcher(string);
+ return m.replaceAll(replacement);
+ }
+
}