Coverage Report - net.sourceforge.pebble.logging.CombinedFormatLogEntryFormat
 
Classes in this File Line Coverage Branch Coverage Complexity
CombinedFormatLogEntryFormat
94%
81/86
83%
20/24
6
 
 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  
 
 36  
 import java.text.ParseException;
 37  
 import java.text.SimpleDateFormat;
 38  
 
 39  
 /**
 40  
  * Represents a log entry in the combined log file format.
 41  
  *
 42  
  * @author Simon Brown
 43  
  */
 44  
 public class CombinedFormatLogEntryFormat {
 45  
 
 46  
   /** the format used for dates */
 47  2756
   SimpleDateFormat dateFormatter = new SimpleDateFormat("[dd/MMM/yyyy:HH:mm:ss Z]");
 48  
 
 49  
   /**
 50  
    * Default, no args constructor.
 51  
    */
 52  2756
   public CombinedFormatLogEntryFormat(Blog blog) {
 53  2756
     dateFormatter.setTimeZone(blog.getTimeZone());
 54  2756
   }
 55  
 
 56  
   /**
 57  
    * Formats a given entry into the combined log file format.
 58  
    *
 59  
    * @param entry   a log entry
 60  
    * @return  a formatted String
 61  
    */
 62  
   public String format(LogEntry entry) {
 63  60
     StringBuffer buf = new StringBuffer();
 64  60
     if (entry.getHost() != null) {
 65  8
       buf.append(entry.getHost());
 66  
     } else {
 67  52
       buf.append("-");
 68  
     }
 69  60
     buf.append(" ");
 70  60
     buf.append("-");
 71  60
     buf.append(" ");
 72  60
     buf.append("-");
 73  60
     buf.append(" ");
 74  60
     buf.append(dateFormatter.format(entry.getDate()));
 75  60
     buf.append(" ");
 76  60
     buf.append("\"" + entry.getRequest() + "\"");
 77  60
     buf.append(" ");
 78  60
     buf.append(entry.getStatusCode());
 79  60
     buf.append(" ");
 80  60
     buf.append("-");
 81  60
     buf.append(" ");
 82  60
     if (entry.getReferer() != null) {
 83  8
       buf.append("\"" + entry.getReferer() + "\"");
 84  
     } else {
 85  52
       buf.append("-");
 86  
     }
 87  60
     buf.append(" ");
 88  60
     if (entry.getAgent() != null) {
 89  8
       buf.append("\"" + entry.getAgent() + "\"");
 90  
     } else {
 91  52
       buf.append("-");
 92  
     }
 93  
 
 94  60
     return buf.toString();
 95  
   }
 96  
 
 97  
   /**
 98  
    * Parses a string in the combined log file format into a log entry.
 99  
    *
 100  
    * @param s   the String to parse
 101  
    * @return  a LogEntry instance
 102  
    */
 103  
   public LogEntry parse(String s) {
 104  32
     LogEntry logEntry = new LogEntry();
 105  
 
 106  
     // there are 9 tokens in the combined log file format
 107  32
     int start = 0;
 108  32
     int end = s.indexOf(" ", start);
 109  32
     String host = s.substring(start, end);
 110  32
     start = end + 1;
 111  32
     end = s.indexOf(" ", start);
 112  32
     String rfc931 = s.substring(start, end);
 113  32
     start = end + 1;
 114  32
     end = s.indexOf(" ", start);
 115  32
     String authuser = s.substring(start, end);
 116  32
     start = end + 1;
 117  32
     end = s.indexOf("]", start);
 118  32
     String date = s.substring(start, end+1);
 119  32
     start = end + 2;
 120  32
     end = s.indexOf("\"", start+1);
 121  32
     String request = s.substring(start+1, end);
 122  32
     start = end + 2;
 123  32
     end = s.indexOf(" ", start);
 124  32
     String statusCode = s.substring(start, end);
 125  32
     start = end + 1;
 126  32
     end = s.indexOf(" ", start);
 127  32
     String bytes = s.substring(start, end);
 128  32
     start = end + 1;
 129  
     String referer;
 130  32
     if (s.charAt(start) == '-') {
 131  28
       referer = "-";
 132  28
       start = start + 2;
 133  
     } else {
 134  4
       end = s.indexOf("\"", start+1);
 135  4
       referer = s.substring(start+1, end);
 136  4
       start = end + 2;
 137  
     }
 138  32
     end = s.length();
 139  
     String agent;
 140  32
     if (s.charAt(start) == '-') {
 141  28
       agent = "-";
 142  28
       start = start + 2;
 143  
     } else {
 144  4
       end = s.indexOf("\"", start+1);
 145  4
       agent = s.substring(start+1, end);
 146  4
       start = end + 2;
 147  
     }
 148  
 
 149  32
     if (!host.equals("-")) {
 150  4
       logEntry.setHost(host);
 151  
     }
 152  
 
 153  32
     if (!rfc931.equals("-")) {
 154  
       //logEntry.setHost(host);
 155  
     }
 156  
 
 157  32
     if (!authuser.equals("-")) {
 158  
       //logEntry.setUser(authuser);
 159  
     }
 160  
 
 161  
     try {
 162  32
       logEntry.setDate(dateFormatter.parse(date));
 163  0
     } catch (ParseException e) {
 164  
       // ignore
 165  32
     }
 166  
 
 167  32
     logEntry.setRequest(request);
 168  
 
 169  32
     if (!statusCode.equals("-")) {
 170  
       try {
 171  32
         logEntry.setStatusCode(Integer.parseInt(statusCode));
 172  0
       } catch (NumberFormatException e) {
 173  
         // ignore
 174  32
       }
 175  
     }
 176  
 
 177  32
     if (!bytes.equals("-")) {
 178  
       try {
 179  0
         logEntry.setBytes(Long.parseLong(bytes));
 180  0
       } catch (NumberFormatException e) {
 181  
         // ignore
 182  0
       }
 183  
     }
 184  
 
 185  32
     if (!referer.equals("-")) {
 186  4
       logEntry.setReferer(referer);
 187  
     }
 188  
 
 189  32
     if (!agent.equals("-")) {
 190  4
       logEntry.setAgent(agent);
 191  
     }
 192  
 
 193  32
     return logEntry;
 194  
   }
 195  
 
 196  
 }
 197  
 
 198