| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 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 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
public class CombinedFormatLogEntryFormat { |
| 45 | |
|
| 46 | |
|
| 47 | 2756 | SimpleDateFormat dateFormatter = new SimpleDateFormat("[dd/MMM/yyyy:HH:mm:ss Z]"); |
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | 2756 | public CombinedFormatLogEntryFormat(Blog blog) { |
| 53 | 2756 | dateFormatter.setTimeZone(blog.getTimeZone()); |
| 54 | 2756 | } |
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 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 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
public LogEntry parse(String s) { |
| 104 | 32 | LogEntry logEntry = new LogEntry(); |
| 105 | |
|
| 106 | |
|
| 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 | |
|
| 155 | |
} |
| 156 | |
|
| 157 | 32 | if (!authuser.equals("-")) { |
| 158 | |
|
| 159 | |
} |
| 160 | |
|
| 161 | |
try { |
| 162 | 32 | logEntry.setDate(dateFormatter.parse(date)); |
| 163 | 0 | } catch (ParseException e) { |
| 164 | |
|
| 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 | |
|
| 174 | 32 | } |
| 175 | |
} |
| 176 | |
|
| 177 | 32 | if (!bytes.equals("-")) { |
| 178 | |
try { |
| 179 | 0 | logEntry.setBytes(Long.parseLong(bytes)); |
| 180 | 0 | } catch (NumberFormatException e) { |
| 181 | |
|
| 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 | |
|