Coverage Report - net.sourceforge.pebble.domain.RefererFilterManager
 
Classes in this File Line Coverage Branch Coverage Complexity
RefererFilterManager
37%
22/58
16%
3/18
3.143
 
 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.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  2736
   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  2736
   RefererFilterManager(Blog rootBlog) {
 63  2736
     this.rootBlog = rootBlog;
 64  2736
     init();
 65  2736
   }
 66  
 
 67  
   /**
 68  
    * Initializes the filters.
 69  
    */
 70  
   private void init() {
 71  
     try {
 72  2736
       DAOFactory factory = DAOFactory.getConfiguredFactory();
 73  2736
       RefererFilterDAO dao = factory.getRefererFilterDAO();
 74  2736
       filters = dao.getRefererFilters(rootBlog);
 75  
 
 76  2736
       Iterator it = filters.iterator();
 77  
       RefererFilter filter;
 78  2736
       while (it.hasNext()) {
 79  0
         filter = (RefererFilter)it.next();
 80  0
         filter.setId(nextId);
 81  0
         nextId++;
 82  
       }
 83  0
     } catch (PersistenceException pe) {
 84  0
       pe.printStackTrace();
 85  2736
     }
 86  2736
   }
 87  
 
 88  
   /**
 89  
    * Adds a new filter to the existing list.
 90  
    *
 91  
    * @param newFilter   a RefererFilter instance
 92  
    */
 93  
   public synchronized void addFilter(RefererFilter newFilter) {
 94  
     try {
 95  12
       DAOFactory factory = DAOFactory.getConfiguredFactory();
 96  12
       RefererFilterDAO dao = factory.getRefererFilterDAO();
 97  
 
 98  12
       if (!filters.contains(newFilter)) {
 99  8
         dao.addRefererFilter(newFilter, rootBlog);
 100  8
         filters.add(newFilter);
 101  8
         newFilter.setId(nextId);
 102  8
         nextId++;
 103  
       }
 104  0
     } catch (PersistenceException pe) {
 105  0
       pe.printStackTrace();
 106  12
     }
 107  12
   }
 108  
 
 109  
   /**
 110  
    * Removes a filter from the list.
 111  
    *
 112  
    * @param expression    the expression to be removed
 113  
    */
 114  
   public synchronized boolean removeFilter(String expression) {
 115  
     try {
 116  0
       DAOFactory factory = DAOFactory.getConfiguredFactory();
 117  0
       RefererFilterDAO dao = factory.getRefererFilterDAO();
 118  
 
 119  0
       Iterator it = filters.iterator();
 120  
       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  0
     } catch (PersistenceException pe) {
 135  0
       pe.printStackTrace();
 136  0
     }
 137  
 
 138  0
     return false;
 139  
   }
 140  
 
 141  
   /**
 142  
    * Gets a collection containing filters.
 143  
    *
 144  
    * @return  a Collection of RefererFilter instances
 145  
    */
 146  
   public Collection getFilters() {
 147  28
     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  
   public List filter(List referers) {
 158  0
     List results = new ArrayList();
 159  0
     Iterator it = referers.iterator();
 160  
     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  
   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  
     RefererFilter filter;
 187  
     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  
 }