Coverage Report - net.sourceforge.pebble.event.trackback.AbstractEmailNotificationListener
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractEmailNotificationListener
0%
0/31
0%
0/4
1.75
 
 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.trackback;
 33  
 
 34  
 import net.sourceforge.pebble.domain.Blog;
 35  
 import net.sourceforge.pebble.domain.TrackBack;
 36  
 import net.sourceforge.pebble.util.MailUtils;
 37  
 import net.sourceforge.pebble.api.event.trackback.TrackBackEvent;
 38  
 import net.sourceforge.pebble.web.security.SecurityTokenValidator;
 39  
 import net.sourceforge.pebble.web.security.SecurityTokenValidatorImpl;
 40  
 
 41  
 import java.text.SimpleDateFormat;
 42  
 import java.util.Collection;
 43  
 import java.util.HashMap;
 44  
 import java.util.Map;
 45  
 
 46  
 /**
 47  
  * Base class for listeners that send an e-mail notification when new
 48  
  * TrackBacks are added.
 49  
  *
 50  
  * @author Simon Brown
 51  
  */
 52  0
 public abstract class AbstractEmailNotificationListener extends TrackBackListenerSupport {
 53  
 
 54  
   /**
 55  
    * Called when a TrackBack has been added.
 56  
    *
 57  
    * @param event a TrackBackEvent instance
 58  
    */
 59  
   public void trackBackAdded(TrackBackEvent event) {
 60  0
     sendNotification(event.getTrackBack());
 61  0
   }
 62  
 
 63  
   private void sendNotification(TrackBack trackBack) {
 64  0
     Blog blog = trackBack.getBlogEntry().getBlog();
 65  
 
 66  0
     SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy HH:mm:ss z");
 67  0
     sdf.setTimeZone(blog.getTimeZone());
 68  
 
 69  0
     String subject = MailUtils.getTrackbackPrefix(blog, trackBack.getState()) + " " + trackBack.getTitle();
 70  
 
 71  0
     String message = "TrackBack from <a href=\"" + trackBack.getUrl() + "\">" + trackBack.getBlogName() + "</a> on " + sdf.format(trackBack.getDate());
 72  0
     message += " in response to " + trackBack.getBlogEntry().getTitle();
 73  0
     message += "\n\n<br><br>";
 74  0
     message += trackBack.getExcerpt();
 75  0
     message += "\n\n<br><br>";
 76  0
     message += "<a href=\"" + trackBack.getPermalink() + "\">Permalink</a>";
 77  
 
 78  0
     SecurityTokenValidator validator = new SecurityTokenValidatorImpl();
 79  
 
 80  0
     if (trackBack.isPending()) {
 81  0
       message += " | ";
 82  0
       message += "<a href=\"" + blog.getUrl() + validator.generateSignedQueryString("manageResponses.secureaction",
 83  
               createMap("response", trackBack.getGuid(), "submit", "Approve"), blog.getXsrfSigningSalt()) + "\">Approve</a>";
 84  0
       message += " | ";
 85  0
       message += "<a href=\"" + blog.getUrl() + validator.generateSignedQueryString("manageResponses.secureaction",
 86  
               createMap("response", trackBack.getGuid(), "submit", "Reject"), blog.getXsrfSigningSalt()) + "\">Reject</a>";
 87  
     }
 88  
 
 89  0
     message += " | ";
 90  0
     message += "<a href=\"" + blog.getUrl() + validator.generateSignedQueryString("manageResponses.secureaction",
 91  
             createMap("response", trackBack.getGuid(), "submit", "Remove"), blog.getXsrfSigningSalt()) + "\">Remove</a>";
 92  
 
 93  0
     Collection to = getEmailAddresses(trackBack);
 94  
 
 95  
     try {
 96  0
       MailUtils.sendMail(MailUtils.createSession(), blog, to, subject, message);
 97  0
     } catch (Exception e) {
 98  0
       e.printStackTrace();
 99  0
     }
 100  0
   }
 101  
 
 102  
   private Map<String, String[]> createMap(String... values) {
 103  0
     Map<String, String[]> map = new HashMap<String, String[]>();
 104  0
     for (int i = 0; i < values.length - 1; i += 2) {
 105  0
       map.put(values[i], new String[]{values[i + 1]});
 106  
     }
 107  0
     return map;
 108  
   }
 109  
 
 110  
   /**
 111  
    * Returns the collection of recipients.
 112  
    *
 113  
    * @param trackBack the TrackBack from the event
 114  
    * @return a Collection of e-mail addresses (Strings)
 115  
    */
 116  
   protected abstract Collection getEmailAddresses(TrackBack trackBack);
 117  
 
 118  
 }