Coverage Report - net.sourceforge.pebble.logging.CombinedLogFormatLogger
 
Classes in this File Line Coverage Branch Coverage Complexity
CombinedLogFormatLogger
33%
33/99
13%
3/22
2.5
 
 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.logging;
 33  
 
 34  
 import net.sourceforge.pebble.domain.Blog;
 35  
 import net.sourceforge.pebble.Constants;
 36  
 
 37  
 import javax.servlet.http.HttpServletRequest;
 38  
 import java.io.*;
 39  
 import java.text.SimpleDateFormat;
 40  
 import java.util.ArrayList;
 41  
 import java.util.Calendar;
 42  
 import java.util.Iterator;
 43  
 import java.util.List;
 44  
 
 45  
 /**
 46  
  * Supports the <a href="http://httpd.apache.org/docs/logs.html#combined">Combined Log Format</a>.
 47  
  *
 48  
  * @author    Simon Brown
 49  
  */
 50  
 public class CombinedLogFormatLogger extends AbstractLogger {
 51  
 
 52  
   private static final String REFERER_HEADER = "Referer";
 53  
   private static final String USER_AGENT_HEADER = "User-Agent";
 54  
   private static final int FLUSH_SIZE = 0;
 55  
 
 56  
   /** the format of the log filenames */
 57  2724
   private SimpleDateFormat filenameFormat = new SimpleDateFormat("'blog-'yyyyMMdd'.log'");
 58  
 
 59  2724
   private List entries = new ArrayList();
 60  
 
 61  
   public CombinedLogFormatLogger(Blog blog) {
 62  2724
     super(blog);
 63  2724
     filenameFormat.setTimeZone(blog.getTimeZone());
 64  2724
   }
 65  
 
 66  
   /**
 67  
    * Logs a HTTP request.
 68  
    *
 69  
    * @param request   a HttpServletRequest
 70  
    */
 71  
   public synchronized void log(HttpServletRequest request, int status) {
 72  0
     String externalUri = (String)request.getAttribute(Constants.EXTERNAL_URI);
 73  0
     LogEntry entry = new LogEntry();
 74  0
     entry.setHost(request.getRemoteAddr());
 75  0
     entry.setDate(blog.getCalendar().getTime());
 76  0
     entry.setStatusCode(status);
 77  0
     StringBuffer buf = new StringBuffer();
 78  0
     buf.append(request.getMethod());
 79  0
     buf.append(" ");
 80  0
     buf.append(externalUri);
 81  0
     entry.setRequest(buf.toString());
 82  0
     entry.setReferer(request.getHeader(REFERER_HEADER));
 83  0
     entry.setAgent(request.getHeader(USER_AGENT_HEADER));
 84  0
     entries.add(entry);
 85  
 
 86  0
     if (entries.size() >= FLUSH_SIZE) {
 87  0
       flush();
 88  
     }
 89  0
   }
 90  
 
 91  
   private void flush() {
 92  
     try {
 93  2696
       write(entries);
 94  2696
       entries.clear();
 95  0
     } catch (IOException ioe) {
 96  0
       ioe.printStackTrace();
 97  2696
     }
 98  2696
   }
 99  
 
 100  
   /**
 101  
    * Called to start this logger.
 102  
    */
 103  
   public void start() {
 104  2692
   }
 105  
 
 106  
   /**
 107  
    * Called to stop this logger.
 108  
    */
 109  
   public synchronized void stop() {
 110  2696
     flush();
 111  2696
   }
 112  
 
 113  
   /**
 114  
    * Gets a copy of the log file for a given year, month and day.
 115  
    *
 116  
    * @param year    the year to get entries for
 117  
    * @param month   the month to get entries for
 118  
    * @param day     the day to get entries for
 119  
    * @return    a String containing the contents of the requested log file
 120  
    */
 121  
   public String getLogFile(int year, int month, int day) {
 122  0
     StringBuffer buf = new StringBuffer();
 123  
     try {
 124  
       // read the file a line at a time, creating a String as we go
 125  0
       File file = new File(blog.getLogsDirectory(), getFilename(year, month, day));
 126  0
       if (file.exists()) {
 127  0
         BufferedReader reader = new BufferedReader(new FileReader(file));
 128  0
         String line = reader.readLine();
 129  0
         while (line != null) {
 130  0
           buf.append(line);
 131  0
           buf.append(System.getProperty("line.separator"));
 132  0
           line = reader.readLine();
 133  
         }
 134  0
         reader.close();
 135  
       }
 136  0
     } catch (Exception e) {
 137  0
       e.printStackTrace();
 138  
     } finally {
 139  0
       return buf.toString();
 140  
     }
 141  
   }
 142  
 
 143  
   /**
 144  
    * Gets the log for a given year, month and day.
 145  
    *
 146  
    * @param year    the year to get entries for
 147  
    * @param month   the month to get entries for
 148  
    * @param day     the day to get entries for
 149  
    * @return    a Log object
 150  
    */
 151  
   public Log getLog(int year, int month, int day) {
 152  0
     List logEntries = new ArrayList();
 153  0
     CombinedFormatLogEntryFormat format = new CombinedFormatLogEntryFormat(blog);
 154  
 
 155  
     try {
 156  
       // read the file a line at a time, parsing into LogEntry objects
 157  0
       File file = new File(blog.getLogsDirectory(), getFilename(year, month, day));
 158  0
       if (file.exists()) {
 159  0
         BufferedReader reader = new BufferedReader(new FileReader(file));
 160  0
         String line = reader.readLine();
 161  0
         while (line != null) {
 162  0
           logEntries.add(format.parse(line));
 163  0
           line = reader.readLine();
 164  
         }
 165  0
         reader.close();
 166  
       }
 167  0
     } catch (Exception e) {
 168  0
       e.printStackTrace();
 169  
     } finally {
 170  0
       return new Log(blog, logEntries);
 171  
     }
 172  
   }
 173  
 
 174  
   /**
 175  
    * Gets the log summary information for the given year, month and day.
 176  
    *
 177  
    * @param year  the year to get entries for
 178  
    * @param month the month to get entries for
 179  
    * @param day   the day to get entries for
 180  
    * @return a LogSummary object
 181  
    */
 182  
   public LogSummary getLogSummary(int year, int month, int day) {
 183  1460
     Calendar cal = blog.getCalendar();
 184  1460
     cal.set(Calendar.YEAR, year);
 185  1460
     cal.set(Calendar.MONTH, month-1);
 186  1460
     cal.set(Calendar.DAY_OF_MONTH, day);
 187  1460
     int totalRequests = 0;
 188  
 
 189  
     try {
 190  
       // read the file a line at a time
 191  1460
       File file = new File(blog.getLogsDirectory(), getFilename(year, month, day));
 192  1460
       if (file.exists()) {
 193  0
         BufferedReader reader = new BufferedReader(new FileReader(file));
 194  0
         String line = reader.readLine();
 195  0
         while (line != null) {
 196  0
           totalRequests++;
 197  0
           line = reader.readLine();
 198  
         }
 199  0
         reader.close();
 200  
       }
 201  0
     } catch (Exception e) {
 202  0
       e.printStackTrace();
 203  1460
     }
 204  
 
 205  1460
     return new LogSummaryItem(blog, cal.getTime(), totalRequests);
 206  
   }
 207  
 
 208  
   /**
 209  
    * Determines the name of the log file.
 210  
    *
 211  
    * @param year    the year to get entries for
 212  
    * @param month   the month to get entries for
 213  
    * @param day     the day to get entries for
 214  
    * @return    the name of the log file for the given year, month and day
 215  
    */
 216  
   private String getFilename(int year, int month, int day) {
 217  1460
     Calendar cal = blog.getCalendar();
 218  1460
     cal.set(Calendar.YEAR, year);
 219  1460
     cal.set(Calendar.MONTH, month-1);
 220  1460
     cal.set(Calendar.DAY_OF_MONTH, day);
 221  
 
 222  1460
     return filenameFormat.format(cal.getTime());
 223  
   }
 224  
 
 225  
   /**
 226  
    * Writes the given list of entries to the log file, creating it if
 227  
    * necessary.
 228  
    *
 229  
    * @param entries   the list of entries to write
 230  
    */
 231  
   private void write(List entries) throws IOException {
 232  2696
     CombinedFormatLogEntryFormat format = new CombinedFormatLogEntryFormat(blog);
 233  
     File file;
 234  2696
     BufferedWriter writer = null;
 235  2696
     String currentFilename = "";
 236  
     String filename;
 237  2696
     Iterator it = entries.iterator();
 238  2696
     while (it.hasNext()) {
 239  0
       LogEntry entry = (LogEntry)it.next();
 240  0
         filename = filenameFormat.format(entry.getDate());
 241  0
         if (!filename.equals(currentFilename)) {
 242  
           // close the old file (if there is one)
 243  0
           if (writer != null) {
 244  0
             writer.flush();
 245  0
             writer.close();
 246  
           }
 247  
 
 248  
           // and open a new file
 249  0
           currentFilename = filename;
 250  0
           file = new File(blog.getLogsDirectory(), currentFilename);
 251  0
           writer = new BufferedWriter(new FileWriter(file, true));
 252  
         }
 253  
 
 254  0
       writer.write(format.format(entry));
 255  0
       writer.newLine();
 256  0
     }
 257  
 
 258  2696
     if (writer != null) {
 259  0
       writer.flush();
 260  0
       writer.close();
 261  
     }
 262  2696
   }
 263  
 
 264  
 }