Clover Coverage Report - Pebble 2.5-SNAPSHOT
Coverage timestamp: Sat Jun 12 2010 09:39:29 EST
../../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
58   204   24   9,67
30   99   0,41   6
6     4  
1    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  IpAddressListener       Line # 49 58 0% 24 6 93,6% 0.9361702
 
  (66)
 
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    */
 
49    public class IpAddressListener extends BlogEntryResponseListenerSupport {
50   
51    /** the log used by this class */
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  106 toggle protected void blogEntryResponseAdded(Response response) {
66  106 PluginProperties props = response.getBlogEntry().getBlog().getPluginProperties();
67   
68  106 if (isListed(response, props.getProperty(BLACKLIST_KEY))) {
69  4 log.info(response.getTitle() + " marked as pending : IP address " + response.getIpAddress() + " is on blacklist");
70  4 response.setPending();
71  4 response.incrementSpamScore();
72  102 } else if (isListed(response, props.getProperty(WHITELIST_KEY))) {
73    // do nothing
74    } else {
75  92 log.info(response.getTitle() + " marked as pending : IP address " + response.getIpAddress() + " not on blacklist or whitelist");
76  92 response.setPending();
77    }
78    }
79   
80    /**
81    * Called when a comment or TrackBack has been approved.
82    *
83    * @param response a Response
84    */
 
85  52 toggle protected void blogEntryResponseApproved(Response response) {
86  52 PluginProperties props = response.getBlogEntry().getBlog().getPluginProperties();
87   
88  52 if (response.getIpAddress() == null || response.getIpAddress().trim().length() == 0) {
89  6 return;
90    }
91   
92  46 synchronized (props) {
93  46 String whitelist = props.getProperty(WHITELIST_KEY);
94  46 String blacklist = props.getProperty(BLACKLIST_KEY);
95  46 whitelist = addIpAddress(response, whitelist);
96  46 blacklist = removeIpAddress(response, blacklist);
97  46 props.setProperty(WHITELIST_KEY, whitelist);
98  46 props.setProperty(BLACKLIST_KEY, blacklist);
99  46 props.store();
100    }
101    }
102   
103    /**
104    * Called when a comment or TrackBack has been rejected.
105    *
106    * @param response a Response
107    */
 
108  24 toggle protected void blogEntryResponseRejected(Response response) {
109  24 PluginProperties props = response.getBlogEntry().getBlog().getPluginProperties();
110   
111  24 if (response.getIpAddress() == null || response.getIpAddress().trim().length() == 0) {
112  0 return;
113    }
114   
115  24 synchronized (props) {
116  24 String blacklist = props.getProperty(BLACKLIST_KEY);
117  24 String whitelist = props.getProperty(WHITELIST_KEY);
118  24 blacklist = addIpAddress(response, blacklist);
119  24 whitelist = removeIpAddress(response, whitelist);
120  24 props.setProperty(BLACKLIST_KEY, blacklist);
121  24 props.setProperty(WHITELIST_KEY, whitelist);
122  24 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  256 toggle private boolean isListed(Response response, String list) {
136  256 if (response.getIpAddress() == null) {
137  16 return false;
138    }
139   
140  240 String ipAddresses[] = null;
141  240 if (list != null) {
142  74 ipAddresses = list.split(",");
143    } else {
144  166 ipAddresses = new String[0];
145    }
146   
147  252 for (int i = 0; i < ipAddresses.length; i++) {
148  74 if (response.getIpAddress().equals(ipAddresses[i])) {
149  62 return true;
150    }
151    }
152   
153  178 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  70 toggle private String addIpAddress(Response response, String list) {
164  70 if (list == null || list.trim().length() == 0) {
165  22 return response.getIpAddress();
166  48 } else if (!isListed(response, list)) {
167  0 return list + "," + response.getIpAddress();
168    } else {
169  48 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  70 toggle private String removeIpAddress(Response response, String list) {
181  70 if (response.getIpAddress() == null) {
182  0 return list;
183    }
184   
185  70 String ipAddresses[] = null;
186  70 if (list != null) {
187  52 ipAddresses = list.split(",");
188    } else {
189  18 ipAddresses = new String[0];
190    }
191   
192  70 StringBuffer buf = new StringBuffer();
193  138 for (int i = 0; i < ipAddresses.length; i++) {
194  68 if (!response.getIpAddress().equals(ipAddresses[i])) {
195  52 if (buf.length() > 0) {
196  4 buf.append(",");
197    }
198  52 buf.append(ipAddresses[i]);
199    }
200    }
201  70 return buf.toString();
202    }
203   
204    }