Coverage Report - net.sourceforge.pebble.event.comment.AbstractEmailNotificationListener
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractEmailNotificationListener
0%
0/75
0%
0/18
2.833
 
 1  
 /*
 2  
  * Copyright (c) 2003-2011, Simon Brown
 3  
  * All rights reserved.
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions are met:
 7  
  *
 8  
  *   - Redistributions of source code must retain the above copyright
 9  
  *     notice, this list of conditions and the following disclaimer.
 10  
  *
 11  
  *   - Redistributions in binary form must reproduce the above copyright
 12  
  *     notice, this list of conditions and the following disclaimer in
 13  
  *     the documentation and/or other materials provided with the
 14  
  *     distribution.
 15  
  *
 16  
  *   - Neither the name of Pebble nor the names of its contributors may
 17  
  *     be used to endorse or promote products derived from this software
 18  
  *     without specific prior written permission.
 19  
  *
 20  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 21  
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 22  
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 23  
  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 24  
  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 25  
  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 26  
  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 27  
  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 28  
  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 29  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 30  
  * POSSIBILITY OF SUCH DAMAGE.
 31  
  */
 32  
 package net.sourceforge.pebble.event.comment;
 33  
 
 34  
 import net.sourceforge.pebble.domain.Blog;
 35  
 import net.sourceforge.pebble.domain.Comment;
 36  
 import net.sourceforge.pebble.api.decorator.ContentDecoratorContext;
 37  
 import net.sourceforge.pebble.api.event.comment.CommentEvent;
 38  
 import net.sourceforge.pebble.util.MailUtils;
 39  
 import net.sourceforge.pebble.util.StringUtils;
 40  
 import net.sourceforge.pebble.web.security.SecurityTokenValidator;
 41  
 import net.sourceforge.pebble.web.security.SecurityTokenValidatorImpl;
 42  
 
 43  
 import javax.mail.Session;
 44  
 import java.text.SimpleDateFormat;
 45  
 import java.util.Collection;
 46  
 import java.util.HashMap;
 47  
 import java.util.Iterator;
 48  
 import java.util.Map;
 49  
 
 50  
 /**
 51  
  * Base class for listeners that send an e-mail notification when new
 52  
  * comments are added.
 53  
  *
 54  
  * @author Simon Brown
 55  
  */
 56  0
 public abstract class AbstractEmailNotificationListener extends CommentListenerSupport {
 57  
 
 58  
   /**
 59  
    * a token to be replaced when sending e-mails
 60  
    */
 61  
   private static final String EMAIL_ADDRESS_TOKEN = "EMAIL_ADDRESS";
 62  
 
 63  
   /**
 64  
    * Called when a comment has been added.
 65  
    *
 66  
    * @param event a CommentEvent instance
 67  
    */
 68  
   public void commentAdded(CommentEvent event) {
 69  0
     Comment comment = event.getComment();
 70  0
     if (comment.isApproved()) {
 71  0
       sendNotification(comment);
 72  0
     } else if (comment.isPending()) {
 73  0
       sendApprovalRequest(comment);
 74  
     }
 75  0
   }
 76  
 
 77  
   /**
 78  
    * Called when a comment has been approved.
 79  
    *
 80  
    * @param event a CommentEvent instance
 81  
    */
 82  
   public void commentApproved(CommentEvent event) {
 83  0
     Comment comment = event.getComment();
 84  0
     sendNotification(comment);
 85  0
   }
 86  
 
 87  
   private void sendNotification(Comment comment) {
 88  0
     Blog blog = comment.getBlogEntry().getBlog();
 89  
 
 90  0
     ContentDecoratorContext context = new ContentDecoratorContext();
 91  0
     context.setView(ContentDecoratorContext.DETAIL_VIEW);
 92  0
     context.setMedia(ContentDecoratorContext.EMAIL);
 93  
 
 94  0
     blog.getContentDecoratorChain().decorate(context, comment);
 95  
 
 96  0
     SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy HH:mm:ss z");
 97  0
     sdf.setTimeZone(blog.getTimeZone());
 98  
 
 99  0
     String subject = MailUtils.getCommentPrefix(blog) + " " + comment.getTitle();
 100  0
     String author = StringUtils.transformHTML(comment.getAuthor());
 101  0
     if (comment.getWebsite() != null) {
 102  0
       author = "<a href=\"" + comment.getWebsite() + "\">" + author + "</a>";
 103  
     }
 104  
 
 105  0
     String message = "Comment from " + author + " on " + sdf.format(comment.getDate());
 106  0
     message += " in response to " + comment.getBlogEntry().getTitle();
 107  0
     message += "\n\n<br><br>";
 108  0
     message += comment.getBody();
 109  0
     message += "\n\n<br><br>";
 110  0
     message += "<a href=\"" + comment.getPermalink() + "\">Permalink</a>";
 111  0
     message += " | ";
 112  0
     message += "<a href=\"" + blog.getUrl() + "removeEmailAddress.action?entry=" + comment.getBlogEntry().getId() + "&email=" + EMAIL_ADDRESS_TOKEN + "\">Opt-out</a>";
 113  
 
 114  0
     Collection to = getEmailAddresses(comment);
 115  0
     Iterator it = comment.getBlogEntry().getComments().iterator();
 116  
     Comment blogComment;
 117  0
     while (it.hasNext()) {
 118  0
       blogComment = (Comment) it.next();
 119  0
       if (blogComment.getEmail() != null && blogComment.getEmail().length() > 0) {
 120  0
         to.add(blogComment.getEmail());
 121  
       }
 122  
     }
 123  
 
 124  
     // now send personalized e-mails to the blog owner and everybody
 125  
     // that left a comment specifying their e-mail address
 126  
     try {
 127  0
       Session session = MailUtils.createSession();
 128  0
       Iterator emailAddresses = to.iterator();
 129  0
       while (emailAddresses.hasNext()) {
 130  0
         String emailAddress = (String) emailAddresses.next();
 131  
 
 132  
         // customize the opt-out link and send the message
 133  0
         MailUtils.sendMail(session, blog, emailAddress, subject,
 134  
                 message.replaceAll(EMAIL_ADDRESS_TOKEN, emailAddress));
 135  0
       }
 136  0
     } catch (Exception e) {
 137  0
       e.printStackTrace();
 138  0
     }
 139  0
   }
 140  
 
 141  
   private void sendApprovalRequest(Comment comment) {
 142  0
     Blog blog = comment.getBlogEntry().getBlog();
 143  
 
 144  0
     SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy HH:mm:ss z");
 145  0
     sdf.setTimeZone(blog.getTimeZone());
 146  
 
 147  0
     String subject = MailUtils.getCommentPrefix(blog, comment.getState()) + " " + comment.getTitle();
 148  
 
 149  0
     String author = StringUtils.transformHTML(comment.getAuthor());
 150  0
     if (comment.getWebsite() != null) {
 151  0
       author = "<a href=\"" + comment.getWebsite() + "\">" + author + "</a>";
 152  
     }
 153  
 
 154  0
     SecurityTokenValidator validator = new SecurityTokenValidatorImpl();
 155  
 
 156  0
     String message = "Comment from " + author + " on " + sdf.format(comment.getDate());
 157  0
     message += " in response to " + comment.getBlogEntry().getTitle();
 158  0
     message += "\n\n<br><br>";
 159  0
     message += comment.getBody();
 160  0
     message += "\n\n<br><br>";
 161  0
     message += "<a href=\"" + comment.getPermalink() + "\">Permalink</a>";
 162  0
     message += " | ";
 163  0
     message += "<a href=\"" + blog.getUrl() + validator.generateSignedQueryString("manageResponses.secureaction",
 164  
             createMap("response", comment.getGuid(), "submit", "Approve"), blog.getXsrfSigningSalt()) + "\">Approve</a>";
 165  0
     message += " | ";
 166  0
     message += "<a href=\"" + blog.getUrl() + validator.generateSignedQueryString("manageResponses.secureaction",
 167  
             createMap("response", comment.getGuid(), "submit", "Reject"), blog.getXsrfSigningSalt()) + "\">Reject</a>";
 168  0
     message += " | ";
 169  0
     message += "<a href=\"" + blog.getUrl() + validator.generateSignedQueryString("manageResponses.secureaction",
 170  
             createMap("response", comment.getGuid(), "submit", "Remove"), blog.getXsrfSigningSalt()) + "\">Remove</a>";
 171  
 
 172  0
     Collection to = getEmailAddresses(comment);
 173  
 
 174  
     try {
 175  0
       MailUtils.sendMail(MailUtils.createSession(), blog, to, subject, message);
 176  0
     } catch (Exception e) {
 177  0
       e.printStackTrace();
 178  0
     }
 179  0
   }
 180  
 
 181  
   private Map<String, String[]> createMap(String... values) {
 182  0
     Map<String, String[]> map = new HashMap<String, String[]>();
 183  0
     for (int i = 0; i < values.length - 1; i += 2) {
 184  0
       map.put(values[i], new String[]{values[i + 1]});
 185  
     }
 186  0
     return map;
 187  
   }
 188  
 
 189  
   /**
 190  
    * Returns the collection of recipients.
 191  
    *
 192  
    * @param comment the Comment from the event
 193  
    * @return a Collection of e-mail addresses (Strings)
 194  
    */
 195  
   protected abstract Collection getEmailAddresses(Comment comment);
 196  
 
 197  
 }