Clover Coverage Report - Pebble 2.5-SNAPSHOT
Coverage timestamp: Sat Jun 12 2010 09:39:29 EST
../../../../img/srcFileCovDistChart4.png 41% of files have more coverage
55   199   19   7,86
18   95   0,35   7
7     2,71  
1    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  RefererFilterManager       Line # 48 55 0% 19 55 31,2% 0.3125
 
  (22)
 
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.domain;
33   
34    import net.sourceforge.pebble.dao.DAOFactory;
35    import net.sourceforge.pebble.dao.PersistenceException;
36    import net.sourceforge.pebble.dao.RefererFilterDAO;
37    import net.sourceforge.pebble.logging.CountedUrl;
38   
39    import java.util.*;
40    import java.util.regex.Matcher;
41   
42    /**
43    * A class to manage regular expressions used to filter out obscene and spam
44    * referers that appear in the logs.
45    *
46    * @author Simon Brown
47    */
 
48    public class RefererFilterManager {
49   
50    /** the next filter id - used internally */
51    private int nextId = 1;
52   
53    /** the owning root blog */
54    private Blog rootBlog;
55   
56    /** the collection of all filters */
57    private Collection filters;
58   
59    /**
60    * Creates a new instance.
61    */
 
62  1342 toggle RefererFilterManager(Blog rootBlog) {
63  1342 this.rootBlog = rootBlog;
64  1342 init();
65    }
66   
67    /**
68    * Initializes the filters.
69    */
 
70  1342 toggle private void init() {
71  1342 try {
72  1342 DAOFactory factory = DAOFactory.getConfiguredFactory();
73  1342 RefererFilterDAO dao = factory.getRefererFilterDAO();
74  1342 filters = dao.getRefererFilters(rootBlog);
75   
76  1342 Iterator it = filters.iterator();
77  1342 RefererFilter filter;
78  1342 while (it.hasNext()) {
79  0 filter = (RefererFilter)it.next();
80  0 filter.setId(nextId);
81  0 nextId++;
82    }
83    } catch (PersistenceException pe) {
84  0 pe.printStackTrace();
85    }
86    }
87   
88    /**
89    * Adds a new filter to the existing list.
90    *
91    * @param newFilter a RefererFilter instance
92    */
 
93  6 toggle public synchronized void addFilter(RefererFilter newFilter) {
94  6 try {
95  6 DAOFactory factory = DAOFactory.getConfiguredFactory();
96  6 RefererFilterDAO dao = factory.getRefererFilterDAO();
97   
98  6 if (!filters.contains(newFilter)) {
99  4 dao.addRefererFilter(newFilter, rootBlog);
100  4 filters.add(newFilter);
101  4 newFilter.setId(nextId);
102  4 nextId++;
103    }
104    } catch (PersistenceException pe) {
105  0 pe.printStackTrace();
106    }
107    }
108   
109    /**
110    * Removes a filter from the list.
111    *
112    * @param expression the expression to be removed
113    */
 
114  0 toggle public synchronized boolean removeFilter(String expression) {
115  0 try {
116  0 DAOFactory factory = DAOFactory.getConfiguredFactory();
117  0 RefererFilterDAO dao = factory.getRefererFilterDAO();
118   
119  0 Iterator it = filters.iterator();
120  0 RefererFilter filter;
121  0 while (it.hasNext()) {
122  0 filter = (RefererFilter)it.next();
123   
124  0 if (filter.getExpression().equals(expression)) {
125    // remove it from the persistent store
126  0 dao.deleteRefererFilter(filter, rootBlog);
127   
128    // and now remove the in-memory representation
129  0 filters.remove(filter);
130   
131  0 return true;
132    }
133    }
134    } catch (PersistenceException pe) {
135  0 pe.printStackTrace();
136    }
137   
138  0 return false;
139    }
140   
141    /**
142    * Gets a collection containing filters.
143    *
144    * @return a Collection of RefererFilter instances
145    */
 
146  14 toggle public Collection getFilters() {
147  14 return Collections.unmodifiableCollection(filters);
148    }
149   
150    /**
151    * Filters a collection of referers using the filters
152    * managed by this instance. Any urls matching a filter are removed.
153    *
154    * @param referers the List of referers (CountedUrls) to be filtered
155    * @return a filtered List containing CountedUrls
156    */
 
157  0 toggle public List filter(List referers) {
158  0 List results = new ArrayList();
159  0 Iterator it = referers.iterator();
160  0 CountedUrl referer;
161  0 while (it.hasNext()) {
162  0 referer = (CountedUrl)it.next();
163  0 if (!filter(referer)) {
164  0 results.add(referer);
165    }
166    }
167   
168  0 return results;
169    }
170   
171    /**
172    * Helper method to determine whether a single referer should
173    * be filtered out.
174    *
175    * @param referer a CountedUrl instance
176    * @return true if the referer should be filtered (i.e. matches one of the
177    * regular expressions), false otherwise
178    */
 
179  0 toggle private boolean filter(CountedUrl referer) {
180   
181  0 if (referer.getUrl() == null) {
182  0 return false;
183    }
184   
185  0 Iterator it = filters.iterator();
186  0 RefererFilter filter;
187  0 Matcher matcher;
188  0 while (it.hasNext()) {
189  0 filter = (RefererFilter)it.next();
190  0 matcher = filter.getCompiledExpression().matcher(referer.getUrl());
191  0 if (matcher.matches()) {
192  0 return true;
193    }
194    }
195   
196  0 return false;
197    }
198   
199    }