File Source: refererfiltermanager.java

         /* 
    P/P   *  Method: net.sourceforge.pebble.domain.RefererFilterManager__static_init
          */
     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     */
           /* 
    P/P     *  Method: void net.sourceforge.pebble.domain.RefererFilterManager(Blog)
            * 
            *  Preconditions:
            *    (soft) net/sourceforge/pebble/dao/DAOFactory.configuredFactory != null
            *    (soft) net/sourceforge/pebble/dao/DAOFactory.configuredFactory.refererFilterDAO != null
            *    (soft) net/sourceforge/pebble/dao/file/FileRefererFilterDAO.log != null
            *    (soft) rootBlog != null
            * 
            *  Postconditions:
            *    this.filters == One-of{&new ArrayList(getRefererFilters#1), undefined}
            *    this.filters in Addr_Set{null,&new ArrayList(getRefererFilters#1)}
            *    init'ed(this.nextId)
            *    this.rootBlog == rootBlog
            *    (soft) this.rootBlog != null
            *    new ArrayList(getRefererFilters#1) num objects <= 1
            */
    62    RefererFilterManager(Blog rootBlog) {
    63      this.rootBlog = rootBlog;
    64      init();
    65    }
    66  
    67    /**
    68     * Initializes the filters.
    69     */
    70    private void init() {
    71      try {
               /* 
    P/P         *  Method: void init()
                * 
                *  Preconditions:
                *    (soft) net/sourceforge/pebble/dao/DAOFactory.configuredFactory != null
                *    (soft) net/sourceforge/pebble/dao/DAOFactory.configuredFactory.refererFilterDAO != null
                *    (soft) net/sourceforge/pebble/dao/file/FileRefererFilterDAO.log != null
                *    (soft) this.nextId <= 232-2
                *    (soft) this.rootBlog != null
                * 
                *  Presumptions:
                *    java.util.Iterator:next(...)@79 != null
                * 
                *  Postconditions:
                *    this.filters == One-of{&new ArrayList(getRefererFilters#1), old this.filters}
                *    init'ed(this.nextId)
                *    new ArrayList(getRefererFilters#1) num objects <= 1
                */
    72        DAOFactory factory = DAOFactory.getConfiguredFactory();
    73        RefererFilterDAO dao = factory.getRefererFilterDAO();
    74        filters = dao.getRefererFilters(rootBlog);
    75  
    76        Iterator it = filters.iterator();
    77        RefererFilter filter;
    78        while (it.hasNext()) {
    79          filter = (RefererFilter)it.next();
    80          filter.setId(nextId);
    81          nextId++;
    82        }
    83      } catch (PersistenceException pe) {
    84        pe.printStackTrace();
    85      }
    86    }
    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 {
               /* 
    P/P         *  Method: void addFilter(RefererFilter)
                * 
                *  Preconditions:
                *    (soft) net/sourceforge/pebble/dao/DAOFactory.configuredFactory != null
                *    (soft) net/sourceforge/pebble/dao/DAOFactory.configuredFactory.refererFilterDAO != null
                *    (soft) net/sourceforge/pebble/dao/file/FileRefererFilterDAO.log != null
                *    (soft) newFilter != null
                *    (soft) this.nextId <= 232-2
                *    (soft) this.filters != null
                *    (soft) this.rootBlog != null
                * 
                *  Postconditions:
                *    newFilter.id == One-of{old newFilter.id, old this.nextId}
                *    this.nextId == One-of{old this.nextId, old this.nextId + 1}
                *    (soft) init'ed(this.nextId)
                * 
                *  Test Vectors:
                *    java.util.Collection:contains(...)@98: {1}, {0}
                */
    95        DAOFactory factory = DAOFactory.getConfiguredFactory();
    96        RefererFilterDAO dao = factory.getRefererFilterDAO();
    97  
    98        if (!filters.contains(newFilter)) {
    99          dao.addRefererFilter(newFilter, rootBlog);
   100          filters.add(newFilter);
   101          newFilter.setId(nextId);
   102          nextId++;
   103        }
   104      } catch (PersistenceException pe) {
   105        pe.printStackTrace();
   106      }
   107    }
   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 {
               /* 
    P/P         *  Method: bool removeFilter(String)
                * 
                *  Preconditions:
                *    (soft) net/sourceforge/pebble/dao/DAOFactory.configuredFactory != null
                *    (soft) net/sourceforge/pebble/dao/DAOFactory.configuredFactory.refererFilterDAO != null
                *    (soft) net/sourceforge/pebble/dao/file/FileRefererFilterDAO.log != null
                *    (soft) this.filters != null
                *    (soft) this.rootBlog != null
                * 
                *  Presumptions:
                *    filter.expression@122 != null
                *    java.util.Iterator:next(...)@122 != null
                * 
                *  Postconditions:
                *    init'ed(return_value)
                * 
                *  Test Vectors:
                *    java.lang.String:equals(...)@124: {0}, {1}
                */
   116        DAOFactory factory = DAOFactory.getConfiguredFactory();
   117        RefererFilterDAO dao = factory.getRefererFilterDAO();
   118  
   119        Iterator it = filters.iterator();
   120        RefererFilter filter;
   121        while (it.hasNext()) {
   122          filter = (RefererFilter)it.next();
   123  
   124          if (filter.getExpression().equals(expression)) {
   125            // remove it from the persistent store
   126            dao.deleteRefererFilter(filter, rootBlog);
   127  
   128            // and now remove the in-memory representation
   129            filters.remove(filter);
   130  
   131            return true;
   132          }
   133        }
   134      } catch (PersistenceException pe) {
   135        pe.printStackTrace();
   136      }
   137  
   138      return false;
   139    }
   140  
   141    /**
   142     * Gets a collection containing filters.
   143     *
   144     * @return  a Collection of RefererFilter instances
   145     */
   146    public Collection getFilters() {
             /* 
    P/P       *  Method: Collection getFilters()
              * 
              *  Preconditions:
              *    init'ed(this.filters)
              * 
              *  Postconditions:
              *    init'ed(return_value)
              */
   147      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) {
             /* 
    P/P       *  Method: List filter(List)
              * 
              *  Preconditions:
              *    referers != null
              *    (soft) this.filters != null
              * 
              *  Presumptions:
              *    java.util.Iterator:next(...)@162 != null
              * 
              *  Postconditions:
              *    return_value == &new ArrayList(filter#1)
              *    new ArrayList(filter#1) num objects == 1
              * 
              *  Test Vectors:
              *    java.util.Iterator:hasNext(...)@161: {1}, {0}
              */
   158      List results = new ArrayList();
   159      Iterator it = referers.iterator();
   160      CountedUrl referer;
   161      while (it.hasNext()) {
   162        referer = (CountedUrl)it.next();
   163        if (!filter(referer)) {
   164          results.add(referer);
   165        }
   166      }
   167  
   168      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  
             /* 
    P/P       *  Method: bool filter(CountedUrl)
              * 
              *  Test Vectors:
              *    net.sourceforge.pebble.logging.CountedUrl:getUrl(...)@181: Inverse{null}, Addr_Set{null}
              * 
              *  Preconditions:
              *    referer != null
              *    (soft) this.filters != null
              * 
              *  Presumptions:
              *    filter.compiledExpression@189 != null
              *    java.util.Iterator:next(...)@189 != null
              *    java.util.regex.Pattern:matcher(...)@190 != null
              * 
              *  Postconditions:
              *    init'ed(return_value)
              * 
              *  Test Vectors:
              *    java.util.Iterator:hasNext(...)@188: {1}, {0}
              *    java.util.regex.Matcher:matches(...)@191: {0}, {1}
              */
   181      if (referer.getUrl() == null) {
   182        return false;
   183      }
   184  
   185      Iterator it = filters.iterator();
   186      RefererFilter filter;
   187      Matcher matcher;
   188      while (it.hasNext()) {
   189        filter = (RefererFilter)it.next();
   190        matcher = filter.getCompiledExpression().matcher(referer.getUrl());
   191        if (matcher.matches()) {
   192          return true;
   193        }
   194      }
   195  
   196      return false;
   197    }
   198  
   199  }








SofCheck Inspector Build Version : 2.22510
refererfiltermanager.java 2010-Jun-25 19:40:32
refererfiltermanager.class 2010-Jul-19 20:23:40