KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > plankton > http > ServletUtil


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: ServletUtil.java,v 1.3 2004/02/01 05:16:28 christianc Exp $
19  */

20 package org.enhydra.barracuda.plankton.http;
21
22 import java.io.*;
23 import java.util.*;
24 import javax.servlet.*;
25 import javax.servlet.http.*;
26
27 import org.apache.log4j.*;
28
29 /**
30  * This just contains some Servlet utility routines
31  */

32 public class ServletUtil {
33
34     //-------------------- Utility stuff -------------------------
35
/**
36      * print all request info
37      */

38     public static void printAllRequestInfo(HttpServletRequest req, Logger logger) {
39         showGeneral(req, logger);
40         showHeader(req, logger);
41         showAttrs(req, logger);
42         showParams(req, logger);
43         showCookies(req, logger);
44     }
45     
46     /**
47      * print the general request info
48      */

49     public static void showGeneral(HttpServletRequest req, Logger logger) {
50         logger.info("General Request Info:");
51         logger.info("...Method:"+req.getMethod());
52         logger.info("...Protocol:"+req.getProtocol());
53         logger.info("...Scheme:"+req.getScheme());
54         logger.info("...AuthType:"+req.getAuthType());
55         logger.info("...IsSecure:"+req.isSecure());
56         logger.info("...ContextPath:"+req.getContextPath());
57         logger.info("...PathInfo:"+req.getPathInfo());
58         logger.info("...PathTranslated:"+req.getPathTranslated());
59         logger.info("...QueryString:"+req.getQueryString());
60         logger.info("...RequestURI:"+req.getRequestURI());
61         logger.info("...ServletPath:"+req.getServletPath());
62     }
63     
64     /**
65      * print the header request info
66      */

67     public static void showHeader(HttpServletRequest req, Logger logger) {
68         logger.info("Headers:");
69         Enumeration enum = req.getHeaderNames();
70         while (enum.hasMoreElements()) {
71             String key =(String) enum.nextElement();
72             String val =(String) req.getHeader(key);
73             logger.info("...name:"+key+" val:"+val);
74         }
75     }
76     
77     /**
78      * print the attr request info
79      */

80     public static void showAttrs(HttpServletRequest req, Logger logger) {
81         logger.info("Attributes:");
82         Enumeration enum2 = req.getAttributeNames();
83         while (enum2.hasMoreElements()) {
84             String key =(String) enum2.nextElement();
85             String val =(String) req.getAttribute(key);
86             logger.info("...name:"+key+" attr:"+val);
87         }
88     }
89     
90     /**
91      * print the param request info
92      */

93     public static void showParams(HttpServletRequest req, Logger logger) {
94         logger.info("Parameters:");
95         Enumeration enum3 = req.getParameterNames();
96         while (enum3.hasMoreElements()) {
97             String key =(String) enum3.nextElement();
98             String vals[] = req.getParameterValues(key);
99             for (int i=0, max=vals.length; i<max; i++) {
100                 logger.info("...key:"+key+" value:"+vals[i]);
101             }
102         }
103     }
104     
105     /**
106      * print the cookie request info
107      */

108     public static void showCookies(HttpServletRequest req, Logger logger) {
109         logger.info("Cookies:");
110         Cookie[] cookies = req.getCookies();
111         for (int i=0,max=cookies.length; i<max; i++) {
112             Cookie c = cookies[i];
113             logger.info("...name:"+c.getName()+" value:"+c.getValue()+" ver:"+c.getVersion()+" dom:"+c.getDomain()+" comment:"+c.getComment()+" max age:"+c.getMaxAge());
114         }
115     }
116
117 }
118
Popular Tags