1 package org.apache.turbine.services.logging;
2
3 /* ====================================================================
4 * The Apache Software License, Version 1.1
5 *
6 * Copyright (c) 2001 The Apache Software Foundation. All rights
7 * reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution,
22 * if any, must include the following acknowledgment:
23 * "This product includes software developed by the
24 * Apache Software Foundation (http://www.apache.org/)."
25 * Alternately, this acknowledgment may appear in the software itself,
26 * if and wherever such third-party acknowledgments normally appear.
27 *
28 * 4. The names "Apache" and "Apache Software Foundation" and
29 * "Apache Turbine" must not be used to endorse or promote products
30 * derived from this software without prior written permission. For
31 * written permission, please contact apache@apache.org.
32 *
33 * 5. Products derived from this software may not be called "Apache",
34 * "Apache Turbine", nor may "Apache" appear in their name, without
35 * prior written permission of the Apache Software Foundation.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals on behalf of the Apache Software Foundation. For more
53 * information on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57 import java.lang.reflect.Method;
58 import java.util.ArrayList;
59 import java.util.Date;
60 import java.util.Enumeration;
61 import java.util.HashMap;
62 import java.util.List;
63 import java.util.Map;
64 import java.util.StringTokenizer;
65 import javax.servlet.http.Cookie;
66 import javax.servlet.http.HttpUtils;
67 import org.apache.turbine.util.RunData;
68
69 /***
70 * This class extracts data from RunData object. It is configured
71 * by a string describing format of output.
72 *
73 * <P>
74 * Format description<BR>
75 * Each token should be seperate by space. In output known token
76 * will be replaced by coresponding data from RunData, unkonwn will be
77 * returned unchanged on output.<BR>
78 *
79 * Conversion token:
80 * <UL>
81 * <LI> %t - current Time
82 * </LI><LI> %U - URL Requested
83 * </LI><LI> %h - Remote Host
84 * </LI><LI> %a - Remote Address
85 * </LI><LI> %l - Remote User
86 * </LI><LI> %p - Server Port
87 * </LI><LI> %v - Server Name
88 * </LI><LI> %m - Method
89 * </LI><LI> %q - Query String
90 * </LI><LI> %cp - Context Path
91 * </LI><LI> %sid - Session Id
92 * </LI><LI> %au - Authentication Type
93 * </LI><LI> %ct - Content Type
94 * </LI><LI> %enc - Character Encoding
95 * </LI><LI> %pro - Protocol
96 * </LI><LI> %sce - Scheme
97 * </LI><LI> %cln - Content Length
98 * </LI><LI> %ua - User Agent
99 * </LI><LI> %ban - Banner Info
100 * </LI><LI> %usr - User
101 * </LI><LI> %cook - Cookies
102 * </LI>
103 * </UL>
104 *
105 * @author <a href="mailto:Tomasz.Zielinski@e-point.pl">Tomasz Zielinski</a>
106 * @author <a href="mailto:Michal.Majdan@e-point.pl">Michal Majdan</a>
107 * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
108 * @version $Id: BaseRunDataFilter.java,v 1.2 2002/07/11 16:53:26 mpoeschl Exp $
109 */
110 public class BaseRunDataFilter implements RunDataFilter
111 {
112 /*** table matching conversion tokens to methods names */
113 protected static Map methodNamesMap = null;
114
115 /*** applied format */
116 protected String format = null;
117
118 /*** parsed format */
119 protected List pattern = null;
120
121 protected final String DELIM = " ";
122
123 /*** initialization of the conversion table */
124 static
125 {
126 if (methodNamesMap == null)
127 {
128 methodNamesMap= new HashMap();
129 methodNamesMap.put("%t", "getTime");
130 methodNamesMap.put("%U", "getURLRequested");
131 methodNamesMap.put("%h", "getRemoteHost");
132 methodNamesMap.put("%a", "getRemoteAddr");
133 methodNamesMap.put("%l", "getRemoteUser");
134 methodNamesMap.put("%p", "getServerPort");
135 methodNamesMap.put("%v", "getServerName");
136 methodNamesMap.put("%m", "getMethod");
137 methodNamesMap.put("%q", "getQueryString");
138 methodNamesMap.put("%cp", "getContextPath");
139 methodNamesMap.put("%sid", "getSessionId");
140 methodNamesMap.put("%au", "getAuthType");
141 methodNamesMap.put("%ct", "getContentType");
142 methodNamesMap.put("%enc", "getCharacterEncoding");
143 methodNamesMap.put("%pro", "getProtocol");
144 methodNamesMap.put("%sce", "getScheme");
145 methodNamesMap.put("%cln", "getContentLength");
146 methodNamesMap.put("%ua", "getUserAgent");
147 methodNamesMap.put("%ban", "getBannerInfo");
148 methodNamesMap.put("%usr", "getUser");
149 methodNamesMap.put("%cook", "getCookies");
150 }
151 }
152
153 /*** parses format string */
154 public void setFormat(String format)
155 {
156 if (format != null && !format.trim().equals(""))
157 {
158 pattern = new ArrayList();
159 StringTokenizer st = new StringTokenizer(format);
160 while (st.hasMoreTokens())
161 {
162 pattern.add(st.nextToken());
163 }
164 this.format = format;
165 }
166 }
167
168 /***
169 * For each field in tha pattern looking for method extracting data from
170 * RunData, invokes the method, and adds return value to return value.
171 * If there is no method for the token, adds token to return value
172 *
173 * @param data - RunDate from which data will be extracted
174 */
175 public String getString(RunData data)
176 {
177 Method method = null;
178 StringBuffer answer = new StringBuffer();
179 if (format == null)
180 {
181 return "";
182 }
183
184 for(int i = 0; i < pattern.size(); i++)
185 {
186 try
187 {
188 //when we want ordinary information we do
189 String methodName = (String)methodNamesMap.get(pattern.get(i));
190 if(methodName != null)
191 {
192 method = getClass().getDeclaredMethod(methodName,
193 new Class[]{RunData.class});
194 answer.append((String)method.invoke(null,
195 new Object[] {data}));
196 answer.append(DELIM);
197 }
198 else
199 {
200 //when we need the content of a header line
201 if(((String)pattern.get(i)).endsWith("i"))
202 {
203 method=getClass().getDeclaredMethod("getHeader",
204 new Class[]{RunData.class, String.class});
205 answer.append((String)method.invoke(null,
206 new Object[] {data, (String)pattern.get(i)}));
207 answer.append(DELIM);
208 }
209 else
210 {
211 answer.append((String)pattern.get(i));
212 answer.append(DELIM);
213 }
214 }
215 }
216 catch (Exception e)
217 {
218 }
219 }
220 return answer.toString();
221 }
222
223 /***
224 * Retrives current system time.
225 *
226 * @param data RunData object associated with this request
227 * @return date and time
228 */
229 private static String getTime(RunData data) {
230 long time = System.currentTimeMillis();
231 Date d = new Date(time);
232 return d.toString();
233 }
234
235 /***
236 * Retrives the url requested by the client
237 *
238 * @param data RunData object associated with this request
239 * @return url requested by the client
240 */
241 private static String getURLRequested(RunData data)
242 {
243 return HttpUtils.getRequestURL(data.getRequest()).toString();
244 }
245
246 /***
247 * Retrives remote host name.
248 *
249 * @param data RunData object associated with this request
250 * @return name of the host that sent the request
251 */
252 private static String getRemoteHost(RunData data)
253 {
254 return data.getRemoteHost();
255 }
256
257 /***
258 * Retrives remote host address.
259 *
260 * @param data RunData object associated with this request
261 * @return address of the host that sent the request
262 */
263 private static String getRemoteAddr(RunData data)
264 {
265 return data.getRemoteAddr();
266 }
267
268 /***
269 * Retrives the login of the user making this request, if the user
270 * has been authenticated, or null if the user has not been authenticated.
271 *
272 * @param data RunData object associated with this request
273 * @return remote user login name if he/she has been authenticated
274 */
275 private static String getRemoteUser(RunData data)
276 {
277 return data.getRequest().getRemoteUser();
278 }
279
280 /***
281 * Retrives the cached serverPort that is stored in the ServerData object
282 *
283 * @param data RunData object associated with this request
284 * @return port that this request was recived on
285 */
286 private static String getServerPort(RunData data)
287 {
288 return String.valueOf(data.getServerPort());
289 }
290
291 /***
292 * Retrives the cached serverName that is stored in the ServerData object
293 *
294 * @param data RunData object associated with this request
295 * @return name of the server that served the request
296 */
297 private static String getServerName(RunData data)
298 {
299 return data.getServerName();
300 }
301
302 /***
303 * Retrives the cached method that is stored in the ServerData object
304 *
305 * @param data RunData object associated with this request
306 * @return method used by the request
307 */
308 private static String getMethod(RunData data)
309 {
310 return data.getRequest().getMethod();
311 }
312
313 /***
314 * Retrives the value of the specified request header.
315 *
316 * @param data RunData object associated with this request
317 * @param symbol pattern element containig header line name between {}
318 * brackets
319 * @return header line contents
320 */
321 private static String getHeader(RunData data, String symbol)
322 {
323 Enumeration names = data.getRequest().getHeaderNames();
324 StringTokenizer st = new StringTokenizer(symbol, "%{}");
325 return data.getRequest().getHeader(st.nextToken());
326 }
327
328 /***
329 * Retrives the query string that is contained in the request URL after the
330 * path.
331 *
332 * @param data RunData object associated with this request
333 * @return query string
334 */
335 private static String getQueryString(RunData data)
336 {
337 return data.getRequest().getQueryString();
338 }
339
340 /***
341 * Retrives the portion of the request URI that indicates the context of the
342 * request.
343 *
344 * @param data RunData object associated with this request
345 * @return the portion of the request URI that indicates the context of the
346 * request.
347 */
348 private static String getContextPath(RunData data)
349 {
350 return data.getRequest().getContextPath();
351 }
352
353 /***
354 * Retrives a string containing the unique identifier assigned to this
355 * session
356 *
357 * @param data RunData object associated with this request
358 * @return Session Id
359 */
360 private static String getSessionId(RunData data)
361 {
362 return data.getRequest().getSession().getId();
363 }
364
365 /***
366 * Retrives the name of the authentication scheme used to protect the
367 * servlet, for example, "BASIC" or "SSL," or null if the servlet was not
368 * protected.
369 *
370 * @param data RunData object associated with this request
371 * @return authentication scheme used
372 */
373 private static String getAuthType(RunData data)
374 {
375 return data.getRequest().getAuthType();
376 }
377
378 /***
379 * Retrives the MIME type of the body of the request, or null if the type is
380 * not known.
381 *
382 * @param data RunData object associated with this request
383 * @return content type of the request
384 */
385 private static String getContentType(RunData data)
386 {
387 return data.getRequest().getContentType();
388 }
389
390 /***
391 * Retrives the name of the character encoding used in the body of this
392 * request.
393 *
394 * @param data RunData object associated with this request
395 * @return character encoding
396 */
397 private static String getCharacterEncoding(RunData data)
398 {
399 return data.getRequest().getCharacterEncoding();
400 }
401
402 /***
403 * Retrives the name and version of the protocol the request uses in the
404 * form protocol/majorVersion.minorVersion
405 *
406 * @param data RunData object associated with this request
407 * @return protocol
408 */
409 private static String getProtocol(RunData data)
410 {
411 return data.getRequest().getProtocol();
412 }
413
414 /***
415 * Retrives the name of the scheme used to make this request, for example,
416 * http, https, or ftp.
417 *
418 * @param data RunData object associated with this request
419 * @return scheme
420 */
421 private static String getScheme(RunData data)
422 {
423 return data.getRequest().getScheme();
424 }
425
426 /***
427 * Retrives the length, in bytes, of the request body and made available
428 * by the input stream, or -1 if the length is not known.
429 *
430 * @param data RunData object associated with this request
431 * @return content length or -1 if not yet known
432 */
433 private static String getContentLength(RunData data)
434 {
435 return String.valueOf(data.getRequest().getContentLength());
436 }
437
438 /***
439 * Retrives the user agent name.
440 *
441 * @param data RunData object associated with this request
442 * @return user agent string
443 */
444 private static String getUserAgent(RunData data)
445 {
446 return data.getUserAgent();
447 }
448
449 /***
450 * Retrives the banner info asociated with the user making the request.
451 *
452 * @param data RunData object associated with this request
453 * @return banner info or empty stirng
454 */
455 private static String getBannerInfo(RunData data)
456 {
457 return (String) data.getUser().getTemp("bannerInfo", "");
458 }
459
460 /***
461 * Retrives first and last name of the user making the request.
462 *
463 * @param data RunData object associated with this request
464 * @return user
465 */
466 private static String getUser(RunData data)
467 {
468 return data.getUser().getFirstName() + " " +
469 data.getUser().getLastName() ;
470 }
471
472 /***
473 * Retrives cookies.
474 *
475 * @param data RunData object associated with this request
476 * @return cookies
477 */
478 private static String getCookies(RunData data) {
479 Cookie cookies[] = data.getRequest().getCookies();
480 StringBuffer answer = new StringBuffer();
481 answer.append("Cookies: [ ");
482 for (int i = 0; i < cookies.length; i++ )
483 {
484 answer.append(cookies[i].getName());
485 answer.append(" = ");
486 answer.append(cookies[i].getValue());
487 answer.append(";\t");
488 }
489 answer.append("]");
490 return answer.toString();
491 }
492 }
This page was automatically generated by Maven