File Source: combinedformatlogentryformat.java
/*
P/P * Method: net.sourceforge.pebble.logging.CombinedFormatLogEntryFormat__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.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 SimpleDateFormat dateFormatter = new SimpleDateFormat("[dd/MMM/yyyy:HH:mm:ss Z]");
48
49 /**
50 * Default, no args constructor.
51 */
/*
P/P * Method: void net.sourceforge.pebble.logging.CombinedFormatLogEntryFormat(Blog)
*
* Preconditions:
* blog != null
*
* Postconditions:
* this.dateFormatter == &new SimpleDateFormat(CombinedFormatLogEntryFormat#1)
* new SimpleDateFormat(CombinedFormatLogEntryFormat#1) num objects == 1
*/
52 public CombinedFormatLogEntryFormat(Blog blog) {
53 dateFormatter.setTimeZone(blog.getTimeZone());
54 }
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) {
/*
P/P * Method: String format(LogEntry)
*
* Preconditions:
* entry != null
* init'ed(entry.agent)
* init'ed(entry.date)
* init'ed(entry.host)
* init'ed(entry.referer)
* init'ed(entry.request)
* init'ed(entry.statusCode)
* this.dateFormatter != null
*
* Postconditions:
* return_value != null
*
* Test Vectors:
* entry.agent: Addr_Set{null}, Inverse{null}
* entry.host: Addr_Set{null}, Inverse{null}
* entry.referer: Addr_Set{null}, Inverse{null}
*/
63 StringBuffer buf = new StringBuffer();
64 if (entry.getHost() != null) {
65 buf.append(entry.getHost());
66 } else {
67 buf.append("-");
68 }
69 buf.append(" ");
70 buf.append("-");
71 buf.append(" ");
72 buf.append("-");
73 buf.append(" ");
74 buf.append(dateFormatter.format(entry.getDate()));
75 buf.append(" ");
76 buf.append("\"" + entry.getRequest() + "\"");
77 buf.append(" ");
78 buf.append(entry.getStatusCode());
79 buf.append(" ");
80 buf.append("-");
81 buf.append(" ");
82 if (entry.getReferer() != null) {
83 buf.append("\"" + entry.getReferer() + "\"");
84 } else {
85 buf.append("-");
86 }
87 buf.append(" ");
88 if (entry.getAgent() != null) {
89 buf.append("\"" + entry.getAgent() + "\"");
90 } else {
91 buf.append("-");
92 }
93
94 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) {
/*
P/P * Method: LogEntry parse(String)
*
* Preconditions:
* s != null
* (soft) this.dateFormatter != null
*
* Presumptions:
* java.lang.String:indexOf(...)@108 <= 232-2
* java.lang.String:indexOf(...)@111 <= 232-2
* java.lang.String:indexOf(...)@114 <= 232-2
* java.lang.String:indexOf(...)@117 <= 232-4
* java.lang.String:indexOf(...)@120 <= 232-3
* ...
*
* Postconditions:
* return_value == &new LogEntry(parse#1)
* new Date(LogEntry#1) num objects == 1
* new LogEntry(parse#1) num objects == 1
* init'ed(return_value.agent)
* init'ed(return_value.bytes)
* init'ed(return_value.date)
* init'ed(return_value.host)
* init'ed(return_value.referer)
* return_value.request != null
* init'ed(return_value.statusCode)
*
* Test Vectors:
* java.lang.String:charAt(...)@130: {0..44, 46..216-1}, {45}
* java.lang.String:charAt(...)@140: {0..44, 46..216-1}, {45}
* java.lang.String:equals(...)@149: {1}, {0}
* java.lang.String:equals(...)@169: {1}, {0}
* java.lang.String:equals(...)@177: {1}, {0}
* java.lang.String:equals(...)@185: {1}, {0}
* java.lang.String:equals(...)@189: {1}, {0}
*/
104 LogEntry logEntry = new LogEntry();
105
106 // there are 9 tokens in the combined log file format
107 int start = 0;
108 int end = s.indexOf(" ", start);
109 String host = s.substring(start, end);
110 start = end + 1;
111 end = s.indexOf(" ", start);
112 String rfc931 = s.substring(start, end);
113 start = end + 1;
114 end = s.indexOf(" ", start);
115 String authuser = s.substring(start, end);
116 start = end + 1;
117 end = s.indexOf("]", start);
118 String date = s.substring(start, end+1);
119 start = end + 2;
120 end = s.indexOf("\"", start+1);
121 String request = s.substring(start+1, end);
122 start = end + 2;
123 end = s.indexOf(" ", start);
124 String statusCode = s.substring(start, end);
125 start = end + 1;
126 end = s.indexOf(" ", start);
127 String bytes = s.substring(start, end);
128 start = end + 1;
129 String referer;
130 if (s.charAt(start) == '-') {
131 referer = "-";
132 start = start + 2;
133 } else {
134 end = s.indexOf("\"", start+1);
135 referer = s.substring(start+1, end);
136 start = end + 2;
137 }
138 end = s.length();
139 String agent;
140 if (s.charAt(start) == '-') {
141 agent = "-";
142 start = start + 2;
143 } else {
144 end = s.indexOf("\"", start+1);
145 agent = s.substring(start+1, end);
146 start = end + 2;
147 }
148
149 if (!host.equals("-")) {
150 logEntry.setHost(host);
151 }
152
153 if (!rfc931.equals("-")) {
154 //logEntry.setHost(host);
155 }
156
157 if (!authuser.equals("-")) {
158 //logEntry.setUser(authuser);
159 }
160
161 try {
162 logEntry.setDate(dateFormatter.parse(date));
163 } catch (ParseException e) {
164 // ignore
165 }
166
167 logEntry.setRequest(request);
168
169 if (!statusCode.equals("-")) {
170 try {
171 logEntry.setStatusCode(Integer.parseInt(statusCode));
172 } catch (NumberFormatException e) {
173 // ignore
174 }
175 }
176
177 if (!bytes.equals("-")) {
178 try {
179 logEntry.setBytes(Long.parseLong(bytes));
180 } catch (NumberFormatException e) {
181 // ignore
182 }
183 }
184
185 if (!referer.equals("-")) {
186 logEntry.setReferer(referer);
187 }
188
189 if (!agent.equals("-")) {
190 logEntry.setAgent(agent);
191 }
192
193 return logEntry;
194 }
195
196 }
197
198
SofCheck Inspector Build Version : 2.22510
| combinedformatlogentryformat.java |
2010-Jun-25 19:40:32 |
| combinedformatlogentryformat.class |
2010-Jul-19 20:23:38 |