1
2
3
4
5
6
7
8
9
10
11
12
13 package org.abstracthorizon.danube.http.logging;
14
15 import org.abstracthorizon.danube.connection.Connection;
16 import org.abstracthorizon.danube.http.HTTPConnection;
17 import org.abstracthorizon.danube.support.logging.patternsupport.PatternProcessor;
18 import org.abstracthorizon.danube.support.logging.util.StringUtil;
19
20 import java.util.ArrayList;
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 public class HTTPPatternProcessor implements PatternProcessor {
44
45
46 protected int bytesSentIndex = -1;
47
48
49 protected int bytesSent0Index = -1;
50
51
52 protected int requestProtocolIndex = -1;
53
54
55 protected int lIndex = -1;
56
57 protected int requestMethodIndex = -1;
58
59 protected int queryStringiIndex = -1;
60
61 protected int firstLineOfRequestIndex = -1;
62
63 protected int responseStatusIndex = -1;
64
65 protected int requestedURLIndex = -1;
66
67 protected int remoteUserIndex = -1;
68
69 protected Pair[] requestHeaders;
70 protected Pair[] cookies;
71
72
73
74
75 public HTTPPatternProcessor() {
76 }
77
78
79
80
81
82
83 public int init(int index, StringBuffer message) {
84 bytesSentIndex = -1;
85 bytesSent0Index = -1;
86 requestProtocolIndex = -1;
87 lIndex = -1;
88 requestMethodIndex = -1;
89 queryStringiIndex = -1;
90 firstLineOfRequestIndex = -1;
91 responseStatusIndex = -1;
92 requestedURLIndex = -1;
93 remoteUserIndex = -1;
94 requestHeaders = null;
95 cookies = null;
96
97 if (message.indexOf("%b") >= 0) {
98 StringUtil.replaceAll(message, "%b", "{" + index + "}");
99 bytesSentIndex = index;
100 index = index + 1;
101 }
102
103 if (message.indexOf("%B") >= 0) {
104 StringUtil.replaceAll(message, "%B", "{" + index + "}");
105 bytesSent0Index = index;
106 index = index + 1;
107 }
108
109 if (message.indexOf("%H") >= 0) {
110 StringUtil.replaceAll(message, "%H", "{" + index + "}");
111 requestProtocolIndex = index;
112 index = index + 1;
113 }
114
115 if (message.indexOf("%l") >= 0) {
116 StringUtil.replaceAll(message, "%l", "{" + index + "}");
117 lIndex = index;
118 index = index + 1;
119 }
120
121 if (message.indexOf("%m") >= 0) {
122 StringUtil.replaceAll(message, "%m", "{" + index + "}");
123 requestMethodIndex = index;
124 index = index + 1;
125 }
126
127 if (message.indexOf("%q") >= 0) {
128 StringUtil.replaceAll(message, "%q", "{" + index + "}");
129 queryStringiIndex = index;
130 index = index + 1;
131 }
132
133 if (message.indexOf("%r") >= 0) {
134 StringUtil.replaceAll(message, "%r", "{" + index + "}");
135 firstLineOfRequestIndex = index;
136 index = index + 1;
137 }
138
139 if ((message.indexOf("%s") >= 0) || (message.indexOf("%>s") >= 0)) {
140 StringUtil.replaceAll(message, "%s", "{" + index + "}");
141 StringUtil.replaceAll(message, "%>s", "{" + index + "}");
142 responseStatusIndex = index;
143 index = index + 1;
144 }
145
146 if (message.indexOf("%U") >= 0) {
147 StringUtil.replaceAll(message, "%U", "{" + index + "}");
148 requestedURLIndex = index;
149 index = index + 1;
150 }
151
152 if (message.indexOf("%u") >= 0) {
153 StringUtil.replaceAll(message, "%u", "{" + index + "}");
154 remoteUserIndex = index;
155 index = index + 1;
156 }
157
158 int i = message.indexOf("%{");
159 if (i >= 0) {
160 ArrayList<Pair> requestHeaders = new ArrayList<Pair>();
161 ArrayList<Pair> cookies = new ArrayList<Pair>();
162 while (i >= 0) {
163 int j = message.indexOf("}", i);
164 if ((j > i) && (message.length() > j + 1)) {
165 char c = message.charAt(j + 1);
166 String name = message.substring(i + 2, j);
167 Pair pair = new Pair();
168 pair.name = name;
169 pair.fieldIndex = index;
170 boolean ok = true;
171 if (c == 'i') {
172 requestHeaders.add(pair);
173 } else if (c == 'c') {
174 cookies.add(pair);
175 } else {
176 ok = false;
177 }
178 if (ok) {
179 message.replace(i, j+2, "{" + index + "}");
180 index = index + 1;
181 }
182 i = message.indexOf("%{", i + 1);
183 } else {
184 i = -1;
185 }
186 }
187 if (requestHeaders.size() > 0) {
188 this.requestHeaders = new Pair[requestHeaders.size()];
189 this.requestHeaders = requestHeaders.toArray(this.requestHeaders);
190 }
191 if (cookies.size() > 0) {
192 this.cookies = new Pair[cookies.size()];
193 this.cookies = cookies.toArray(this.cookies);
194 }
195 }
196
197 return index;
198 }
199
200
201
202
203
204
205 public void process(Connection connection, Object[] array) {
206 HTTPConnection httpConnection = (HTTPConnection)connection.adapt(HTTPConnection.class);
207
208 if (bytesSentIndex >= 0) {
209 array[bytesSentIndex] = "-";
210 }
211 if (bytesSent0Index >= 0) {
212 array[bytesSent0Index] = 0;
213 }
214 if (requestProtocolIndex >= 0) {
215 array[requestProtocolIndex] = httpConnection.getRequestProtocol();
216 }
217 if (lIndex >= 0) {
218 array[lIndex] = "-";
219 }
220 if (requestMethodIndex >= 0) {
221 array[requestMethodIndex] = httpConnection.getRequestMethod();
222 }
223 if (queryStringiIndex >= 0) {
224 String q = httpConnection.getRequestURI();
225 int i = q.indexOf('?');
226 if (i >= 0) {
227 q = q.substring(i);
228 } else {
229 q = "-";
230 }
231 array[queryStringiIndex] = q;
232 }
233 if (firstLineOfRequestIndex >= 0) {
234 array[firstLineOfRequestIndex] = httpConnection.getRequestMethod() + " " + httpConnection.getRequestURI() + " " + httpConnection.getRequestProtocol();
235 }
236 if (responseStatusIndex >= 0) {
237 array[responseStatusIndex] = httpConnection.getResponseStatus().getCode();
238 }
239 if (requestedURLIndex >= 0) {
240 String q = httpConnection.getRequestURI();
241 int i = q.indexOf('?');
242 if (i >= 0) {
243 q = q.substring(0, i);
244 }
245 array[requestedURLIndex] = q;
246 }
247 if (remoteUserIndex >= 0) {
248 array[remoteUserIndex] = "-";
249 }
250 if (requestHeaders.length > 0) {
251 for (Pair pair : requestHeaders) {
252 String value = httpConnection.getRequestHeaders().getFirst(pair.name);
253 if ((value == null) || (value.length() == 0)) {
254 value = "";
255 }
256 array[pair.fieldIndex] = value;
257 }
258 }
259 }
260
261 protected static class Pair {
262 public String name;
263 public int fieldIndex;
264 }
265
266 }