Coverage Report - net.sourceforge.pebble.event.response.IpAddressListener
 
Classes in this File Line Coverage Branch Coverage Complexity
IpAddressListener
95%
62/65
86%
31/36
5.167
 
 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.response;
 33  
 
 34  
 import net.sourceforge.pebble.PluginProperties;
 35  
 import net.sourceforge.pebble.domain.Response;
 36  
 import org.apache.commons.logging.Log;
 37  
 import org.apache.commons.logging.LogFactory;
 38  
 
 39  
 /**
 40  
  * Checks comment and TrackBack IP address against a whitelist and a blacklist.
 41  
  * If in the whitelist, the response is left as-is. If in the blacklist,
 42  
  * the response is set to pending and the spam score incremented by 1 point.
 43  
  * If in neither, the response is set to pending but the spam score isn't
 44  
  * increased. This allows responses from new IP addresses to be manually
 45  
  * verified before publication.
 46  
  *
 47  
  * @author Simon Brown
 48  
  */
 49  5492
 public class IpAddressListener extends BlogEntryResponseListenerSupport {
 50  
 
 51  
   /** the log used by this class */
 52  4
   private static final Log log = LogFactory.getLog(IpAddressListener.class);
 53  
 
 54  
   /** the name of the whitelist property */
 55  
   public static final String WHITELIST_KEY = "IpAddressListener.whitelist";
 56  
 
 57  
   /** the name of the blacklist property */
 58  
   public static final String BLACKLIST_KEY = "IpAddressListener.blacklist";
 59  
 
 60  
   /**
 61  
    * Called when a comment or TrackBack has been added.
 62  
    *
 63  
    * @param response a Response
 64  
    */
 65  
   protected void blogEntryResponseAdded(Response response) {
 66  212
     PluginProperties props = response.getBlogEntry().getBlog().getPluginProperties();
 67  
 
 68  212
     if (isListed(response, props.getProperty(BLACKLIST_KEY))) {
 69  8
       log.info(response.getTitle() + " marked as pending : IP address " + response.getIpAddress() + " is on blacklist");
 70  8
       response.setPending();
 71  8
       response.incrementSpamScore();
 72  204
     } else if (isListed(response, props.getProperty(WHITELIST_KEY))) {
 73  
       // do nothing
 74  
     } else {
 75  184
       log.info(response.getTitle() + " marked as pending : IP address " + response.getIpAddress() + " not on blacklist or whitelist");
 76  184
       response.setPending();
 77  
     }
 78  212
   }
 79  
 
 80  
   /**
 81  
    * Called when a comment or TrackBack has been approved.
 82  
    *
 83  
    * @param response a Response
 84  
    */
 85  
   protected void blogEntryResponseApproved(Response response) {
 86  104
     PluginProperties props = response.getBlogEntry().getBlog().getPluginProperties();
 87  
 
 88  104
     if (response.getIpAddress() == null || response.getIpAddress().trim().length() == 0) {
 89  12
       return;
 90  
     }
 91  
 
 92  92
     synchronized (props) {
 93  92
       String whitelist = props.getProperty(WHITELIST_KEY);
 94  92
       String blacklist = props.getProperty(BLACKLIST_KEY);
 95  92
       whitelist = addIpAddress(response, whitelist);
 96  92
       blacklist = removeIpAddress(response, blacklist);
 97  92
       props.setProperty(WHITELIST_KEY, whitelist);
 98  92
       props.setProperty(BLACKLIST_KEY, blacklist);
 99  92
       props.store();
 100  92
     }
 101  92
   }
 102  
 
 103  
   /**
 104  
    * Called when a comment or TrackBack has been rejected.
 105  
    *
 106  
    * @param response a Response
 107  
    */
 108  
   protected void blogEntryResponseRejected(Response response) {
 109  48
     PluginProperties props = response.getBlogEntry().getBlog().getPluginProperties();
 110  
 
 111  48
     if (response.getIpAddress() == null || response.getIpAddress().trim().length() == 0) {
 112  0
       return;
 113  
     }
 114  
 
 115  48
     synchronized (props) {
 116  48
       String blacklist = props.getProperty(BLACKLIST_KEY);
 117  48
       String whitelist = props.getProperty(WHITELIST_KEY);
 118  48
       blacklist = addIpAddress(response, blacklist);
 119  48
       whitelist = removeIpAddress(response, whitelist);
 120  48
       props.setProperty(BLACKLIST_KEY, blacklist);
 121  48
       props.setProperty(WHITELIST_KEY, whitelist);
 122  48
       props.store();
 123  48
     }
 124  48
   }
 125  
 
 126  
   /**
 127  
    * Determines whether the IP address of the specified response is contained
 128  
    * within a given list of IP addresses.
 129  
    *
 130  
    * @param response    a Response instance
 131  
    * @param list        a list of IP addresses, comma separated
 132  
    * @return    true if the IP address is contained within the list,
 133  
    *            false otherwise
 134  
    */
 135  
   private boolean isListed(Response response, String list) {
 136  512
     if (response.getIpAddress() == null) {
 137  32
       return false;
 138  
     }
 139  
 
 140  480
     String ipAddresses[] = null;
 141  480
     if (list != null) {
 142  148
       ipAddresses = list.split(",");
 143  
     } else {
 144  332
       ipAddresses = new String[0];
 145  
     }
 146  
 
 147  504
     for (int i = 0; i < ipAddresses.length; i++) {
 148  148
       if (response.getIpAddress().equals(ipAddresses[i])) {
 149  124
         return true;
 150  
       }
 151  
     }
 152  
 
 153  356
     return false;
 154  
   }
 155  
 
 156  
   /**
 157  
    * Adds the IP address of the specified response to the given list.
 158  
    *
 159  
    * @param response    a Response instance
 160  
    * @param list        a list of IP addresses, comma separated
 161  
    * @return  an updated list of IP addresses
 162  
    */
 163  
   private String addIpAddress(Response response, String list) {
 164  140
     if (list == null || list.trim().length() == 0) {
 165  44
       return response.getIpAddress();
 166  96
     } else if (!isListed(response, list)) {
 167  0
       return list + "," + response.getIpAddress();
 168  
     } else {
 169  96
       return list;
 170  
     }
 171  
   }
 172  
 
 173  
   /**
 174  
    * Removes the IP address of the specified response to the given list.
 175  
    *
 176  
    * @param response    a Response instance
 177  
    * @param list        a list of IP addresses, comma separated
 178  
    * @return  an updated list of IP addresses
 179  
    */
 180  
   private String removeIpAddress(Response response, String list) {
 181  140
     if (response.getIpAddress() == null) {
 182  0
       return list;
 183  
     }
 184  
 
 185  140
     String ipAddresses[] = null;
 186  140
     if (list != null) {
 187  104
       ipAddresses = list.split(",");
 188  
     } else {
 189  36
       ipAddresses = new String[0];
 190  
     }
 191  
 
 192  140
     StringBuffer buf = new StringBuffer();
 193  276
     for (int i = 0; i < ipAddresses.length; i++) {
 194  136
       if (!response.getIpAddress().equals(ipAddresses[i])) {
 195  104
         if (buf.length() > 0) {
 196  8
           buf.append(",");
 197  
         }
 198  104
         buf.append(ipAddresses[i]);
 199  
       }
 200  
     }
 201  140
     return buf.toString();
 202  
   }
 203  
 
 204  
 }