Coverage Report - net.sourceforge.pebble.dao.file.FileRefererFilterDAO
 
Classes in this File Line Coverage Branch Coverage Complexity
FileRefererFilterDAO
17%
7/39
16%
1/6
2.75
 
 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  
 
 33  
 package net.sourceforge.pebble.dao.file;
 34  
 
 35  
 import net.sourceforge.pebble.dao.PersistenceException;
 36  
 import net.sourceforge.pebble.dao.RefererFilterDAO;
 37  
 import net.sourceforge.pebble.domain.Blog;
 38  
 import net.sourceforge.pebble.domain.RefererFilter;
 39  
 import net.sourceforge.pebble.domain.RefererFilterManager;
 40  
 import org.apache.commons.logging.Log;
 41  
 import org.apache.commons.logging.LogFactory;
 42  
 
 43  
 import java.io.*;
 44  
 import java.util.ArrayList;
 45  
 import java.util.Collection;
 46  
 import java.util.Iterator;
 47  
 
 48  
 /**
 49  
  * A mock implementation of the CategoryDAO interface that does nothing. This
 50  
  * is used when performing unit tests.
 51  
  *
 52  
  * @author    Simon Brown
 53  
  */
 54  5020
 public class FileRefererFilterDAO implements RefererFilterDAO {
 55  
 
 56  
   /** the name of the file containing the filter */
 57  
   private static final String FILTERS_FILE = "refererFilters.txt";
 58  
 
 59  
   /** the log used by this class */
 60  4
   private static Log log = LogFactory.getLog(RefererFilterManager.class);
 61  
 
 62  
   /**
 63  
    * Loads the referer filters.
 64  
    *
 65  
    * @param rootBlog    the owning Blog instance
 66  
    * @return  a Collection of RefererFilter instances
 67  
    * @throws  PersistenceException    if filters cannot be loaded
 68  
    */
 69  
   public Collection getRefererFilters(Blog rootBlog) throws PersistenceException {
 70  32
     ArrayList filters = new ArrayList();
 71  32
     String root = rootBlog.getRoot();
 72  
     try {
 73  
 
 74  32
       File filtersFile = new File(root, FILTERS_FILE);
 75  32
       if (!filtersFile.exists()) {
 76  32
         return filters;
 77  
       }
 78  
 
 79  0
       BufferedReader reader = new BufferedReader(new FileReader(filtersFile));
 80  0
       String expression = reader.readLine();
 81  0
       while (expression != null) {
 82  0
         filters.add(new RefererFilter(expression));
 83  0
         expression = reader.readLine();
 84  
       }
 85  
 
 86  0
       reader.close();
 87  0
     } catch (IOException ioe) {
 88  0
       log.error("A " + FILTERS_FILE + " file at " + root + " cannot be loaded", ioe);
 89  0
     }
 90  
 
 91  0
     return filters;
 92  
   }
 93  
 
 94  
   /**
 95  
    * Adds the specified referer filter.
 96  
    *
 97  
    * @param filter    the RefererFilter instance to be added
 98  
    * @param rootBlog    the owning Blog instance
 99  
    * @throws PersistenceException   if something goes wrong storing the filters
 100  
    */
 101  
   public void addRefererFilter(RefererFilter filter, Blog rootBlog) throws PersistenceException {
 102  0
     Collection filters = getRefererFilters(rootBlog);
 103  0
     filters.add(filter);
 104  0
     store(filters, rootBlog);
 105  0
   }
 106  
 
 107  
   /**
 108  
    * Removes the specified referer filter.
 109  
    *
 110  
    * @param filter    the RefererFilter instance to be removed
 111  
    * @param rootBlog    the owning Blog instance
 112  
    * @throws PersistenceException   if something goes wrong removing the filter
 113  
    */
 114  
   public void deleteRefererFilter(RefererFilter filter, Blog rootBlog) throws PersistenceException {
 115  0
     Collection filters = getRefererFilters(rootBlog);
 116  0
     filters.remove(filter);
 117  0
     store(filters, rootBlog);
 118  0
   }
 119  
 
 120  
   /**
 121  
    * Helper method to store all filters for a given blog.
 122  
    *
 123  
    * @param filters   the Collection of RefererFilter instances to store
 124  
    * @param rootBlog      the blog to which the filters belong
 125  
    */
 126  
   private void store(Collection filters, Blog rootBlog) throws PersistenceException {
 127  
     try {
 128  0
       String root = rootBlog.getRoot();
 129  0
       BufferedWriter writer = new BufferedWriter(new FileWriter(new File(root, FILTERS_FILE)));
 130  
 
 131  0
       Iterator it = filters.iterator();
 132  
       RefererFilter filter;
 133  0
       while (it.hasNext()) {
 134  0
         filter = (RefererFilter)it.next();
 135  0
         writer.write(filter.getExpression());
 136  0
         writer.newLine();
 137  
       }
 138  
 
 139  0
       writer.flush();
 140  0
       writer.close();
 141  0
     } catch (IOException ioe) {
 142  0
       log.error(ioe);
 143  0
       throw new PersistenceException("Filters could not be saved : " + ioe.getMessage());
 144  0
     }
 145  0
   }
 146  
 
 147  
 }