ipaddresslistener.java

### File Source: ipaddresslistener.java

         1  /*
         2   * Copyright (c) 2003-2006, 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   */
             /*
        P/P   *  Method: void net.sourceforge.pebble.event.response.IpAddressListener()
              */
        49  public class IpAddressListener extends BlogEntryResponseListenerSupport {
        50
        51    /** the log used by this class */
               /*
        P/P     *  Method: net.sourceforge.pebble.event.response.IpAddressListener__static_init
                *
                *  Postconditions:
                *    init'ed(log)
                */
        52    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) {
                 /*
        P/P       *  Method: void blogEntryResponseAdded(Response)
                  *
                  *  Preconditions:
                  *    response != null
                  *
                  *  Presumptions:
                  *    org.apache.commons.logging.LogFactory:getLog(...)@52 != null
                  *
                  *  Preconditions:
                  *    response.blogEntry.blog.pluginProperties != null
                  *    response.blogEntry != null
                  *    response.blogEntry.blog != null
                  *    init'ed(response.ipAddress)
                  *    (soft) response.spamScore <= 232-2
                  *    (soft) init'ed(response.title)
                  *
                  *  Postconditions:
                  *    response.spamScore == One-of{old response.spamScore + 1, old response.spamScore}
                  *    (soft) init'ed(response.spamScore)
                  *    response.state == One-of{&net.sourceforge.pebble.domain.State__static_init.new State(State__static_init#3), old response.state}
                  */
        66      PluginProperties props = response.getBlogEntry().getBlog().getPluginProperties();
        67
        68      if (isListed(response, props.getProperty(BLACKLIST_KEY))) {
        69        log.info(response.getTitle() + " marked as pending : IP address " + response.getIpAddress() + " is on blacklist");
        70        response.setPending();
        71        response.incrementSpamScore();
        72      } else if (isListed(response, props.getProperty(WHITELIST_KEY))) {
        73        // do nothing
        74      } else {
        75        log.info(response.getTitle() + " marked as pending : IP address " + response.getIpAddress() + " not on blacklist or whitelist");
        76        response.setPending();
        77      }
        78    }
        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) {
                 /*
        P/P       *  Method: void blogEntryResponseApproved(Response)
                  *
                  *  Preconditions:
                  *    response != null
                  *
                  *  Test Vectors:
                  *    java.lang.String:length(...)@88: {1..232-1}, {0}
                  *
                  *  Preconditions:
                  *    (soft) response.blogEntry.blog.pluginProperties != null
                  *    response.blogEntry != null
                  *    response.blogEntry.blog != null
                  *    init'ed(response.ipAddress)
                  *
                  *  Test Vectors:
                  *    response.ipAddress: Addr_Set{null}, Inverse{null}
                  */
        86      PluginProperties props = response.getBlogEntry().getBlog().getPluginProperties();
        87
        88      if (response.getIpAddress() == null || response.getIpAddress().trim().length() == 0) {
        89        return;
        90      }
        91
        92      synchronized (props) {
        93        String whitelist = props.getProperty(WHITELIST_KEY);
        94        String blacklist = props.getProperty(BLACKLIST_KEY);
        95        whitelist = addIpAddress(response, whitelist);
        96        blacklist = removeIpAddress(response, blacklist);
        97        props.setProperty(WHITELIST_KEY, whitelist);
        98        props.setProperty(BLACKLIST_KEY, blacklist);
        99        props.store();
       100      }
       101    }
       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) {
                 /*
        P/P       *  Method: void blogEntryResponseRejected(Response)
                  *
                  *  Preconditions:
                  *    response != null
                  *
                  *  Test Vectors:
                  *    java.lang.String:length(...)@111: {1..232-1}, {0}
                  *
                  *  Preconditions:
                  *    (soft) response.blogEntry.blog.pluginProperties != null
                  *    response.blogEntry != null
                  *    response.blogEntry.blog != null
                  *    init'ed(response.ipAddress)
                  *
                  *  Test Vectors:
                  *    response.ipAddress: Addr_Set{null}, Inverse{null}
                  */
       109      PluginProperties props = response.getBlogEntry().getBlog().getPluginProperties();
       110
       111      if (response.getIpAddress() == null || response.getIpAddress().trim().length() == 0) {
       112        return;
       113      }
       114
       115      synchronized (props) {
       116        String blacklist = props.getProperty(BLACKLIST_KEY);
       117        String whitelist = props.getProperty(WHITELIST_KEY);
       118        blacklist = addIpAddress(response, blacklist);
       119        whitelist = removeIpAddress(response, whitelist);
       120        props.setProperty(BLACKLIST_KEY, blacklist);
       121        props.setProperty(WHITELIST_KEY, whitelist);
       122        props.store();
       123      }
       124    }
       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) {
                 /*
        P/P       *  Method: bool isListed(Response, String)
                  *
                  *  Preconditions:
                  *    response != null
                  *
                  *  Presumptions:
                  *    ipAddresses.length@142 <= 232-1
                  *
                  *  Postconditions:
                  *    init'ed(return_value)
                  *
                  *  Test Vectors:
                  *    list: Addr_Set{null}, Inverse{null}
                  *    java.lang.String:equals(...)@148: {0}, {1}
                  *
                  *  Preconditions:
                  *    init'ed(response.ipAddress)
                  *
                  *  Test Vectors:
                  *    response.ipAddress: Inverse{null}, Addr_Set{null}
                  */
       136      if (response.getIpAddress() == null) {
       137        return false;
       138      }
       139
       140      String ipAddresses[] = null;
       141      if (list != null) {
       142        ipAddresses = list.split(",");
       143      } else {
       144        ipAddresses = new String[0];
       145      }
       146
       147      for (int i = 0; i < ipAddresses.length; i++) {
       148        if (response.getIpAddress().equals(ipAddresses[i])) {
       149          return true;
       150        }
       151      }
       152
       153      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) {
                 /*
        P/P       *  Method: String addIpAddress(Response, String)
                  *
                  *  Postconditions:
                  *    init'ed(return_value)
                  *
                  *  Test Vectors:
                  *    list: Addr_Set{null}, Inverse{null}
                  *    java.lang.String:length(...)@164: {1..232-1}, {0}
                  *
                  *  Preconditions:
                  *    (soft) response != null
                  *    (soft) init'ed(response.ipAddress)
                  */
       164      if (list == null || list.trim().length() == 0) {
       165        return response.getIpAddress();
       166      } else if (!isListed(response, list)) {
       167        return list + "," + response.getIpAddress();
       168      } else {
       169        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) {
                 /*
        P/P       *  Method: String removeIpAddress(Response, String)
                  *
                  *  Preconditions:
                  *    response != null
                  *
                  *  Presumptions:
                  *    ipAddresses.length@187 <= 232-1
                  *
                  *  Postconditions:
                  *    init'ed(return_value)
                  *
                  *  Test Vectors:
                  *    list: Addr_Set{null}, Inverse{null}
                  *    java.lang.String:equals(...)@194: {1}, {0}
                  *    java.lang.StringBuffer:length(...)@195: {-231..0}, {1..232-1}
                  *
                  *  Preconditions:
                  *    init'ed(response.ipAddress)
                  *
                  *  Test Vectors:
                  *    response.ipAddress: Inverse{null}, Addr_Set{null}
                  */
       181      if (response.getIpAddress() == null) {
       182        return list;
       183      }
       184
       185      String ipAddresses[] = null;
       186      if (list != null) {
       187        ipAddresses = list.split(",");
       188      } else {
       189        ipAddresses = new String[0];
       190      }
       191
       192      StringBuffer buf = new StringBuffer();
       193      for (int i = 0; i < ipAddresses.length; i++) {
       194        if (!response.getIpAddress().equals(ipAddresses[i])) {
       195          if (buf.length() > 0) {
       196            buf.append(",");
       197          }
       198          buf.append(ipAddresses[i]);
       199        }
       200      }
       201      return buf.toString();
       202    }
       203
       204  }

SofCheck Inspector Build Version : 2.22510

ipaddresslistener.java | 2010-Jun-25 19:40:32
ipaddresslistener.class | 2010-Jul-19 20:23:38