KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbforms > util > ServletUtil


1 /*
2  * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/util/ServletUtil.java,v 1.5 2004/08/18 12:26:09 hkollmann Exp $
3  * $Revision: 1.5 $
4  * $Date: 2004/08/18 12:26:09 $
5  *
6  * DbForms - a Rapid Application Development Framework
7  * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */

23
24 package org.dbforms.util;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 import java.io.*;
30
31 import java.util.*;
32
33 import javax.servlet.*;
34 import javax.servlet.http.*;
35
36
37
38 /**
39  * Servlet Utility class
40  *
41  * @author Luca Fossato
42  * @created 10 June 2002
43  */

44 public class ServletUtil {
45    /** Log4j category. */
46    private static Log cat = LogFactory.getLog(ServletUtil.class);
47
48    /**
49     * Dumps all the incoming HttpServletRequest informations.
50     *
51     * @param req the input HttpServletRequest object to dump
52     * @return a string with dumped information
53     */

54    public static String JavaDoc dumpRequest(HttpServletRequest req) {
55       return dumpRequest(req, "\n");
56    }
57
58
59    /**
60     * Dumps all the incoming HttpServletRequest informations.
61     *
62     * @param req the input HttpServletRequest object to dump
63     * @return a string with dumped information
64     */

65    public static String JavaDoc dumpRequest(HttpServletRequest req,
66                                     String JavaDoc returnToken) {
67       String JavaDoc s = null;
68
69       try {
70          s = dump(req, returnToken);
71       } catch (Exception JavaDoc e) {
72          cat.error("::dumpRequest - exception: ", e);
73       }
74
75       return s;
76    }
77
78
79    /**
80     * Get the length of the element having the longer name
81     *
82     * @param paramNames the enumeration element
83     * @return the length of the element having the longer name
84     */

85    private static int getElementNameMaxLength(Iterator paramNames) {
86       int len = 0;
87
88       while (paramNames.hasNext()) {
89          String JavaDoc paramName = (String JavaDoc) paramNames.next();
90          int tmpLen = paramName.length();
91
92          if (tmpLen > len) {
93             len = tmpLen;
94          }
95       }
96
97       return len;
98    }
99
100
101    /**
102     * Return a String containin blank chars.
103     * Its length is equal to ($spacesToAdd - $fromString.length)
104     *
105     * @param spacesToAdd number of space characters to add
106     * @param fromString the string where to start to add chars
107     * @return a String containin blank chars whose length is equal
108     * to ($spacesToAdd - $fromString.length)
109     */

110    private static String JavaDoc addSpaces(int spacesToAdd,
111                                    String JavaDoc fromString) {
112       int len = spacesToAdd;
113       String JavaDoc res = "";
114
115       if (!Util.isNull(fromString)) {
116          len = (spacesToAdd - fromString.length());
117
118          for (int i = 0; i < len; i++) {
119             res += " ";
120          }
121       }
122
123       return res;
124    }
125
126
127    /**
128     * Dump the incoming HttpServletRequest object
129     *
130     * @param req the HttpServletRequest object to dump
131     * @return Description of the Return Value
132     * @exception ServletException if any error occurs
133     * @exception IOException if any error occurs
134     */

135    private static String JavaDoc dump(HttpServletRequest req,
136                               String JavaDoc returnToken)
137                        throws ServletException, IOException {
138       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
139       String JavaDoc s = null;
140
141       sb.append("HTTP Snooper Servlet")
142         .append(returnToken)
143         .append(returnToken);
144       sb.append("Request URL:")
145         .append(returnToken);
146       sb.append(" http://" + req.getServerName() + ":" + req.getServerPort()
147                 + req.getRequestURI() + returnToken)
148         .append(returnToken);
149
150       sb.append("Request Info:")
151         .append(returnToken);
152       sb.append(" Request Method: " + req.getMethod())
153         .append(returnToken);
154       sb.append(" Request URI: " + req.getRequestURI())
155         .append(returnToken);
156       sb.append(" Request Protocol: " + req.getProtocol())
157         .append(returnToken);
158       sb.append(" Servlet Path: " + req.getServletPath())
159         .append(returnToken);
160       sb.append(" Path Info: " + req.getPathInfo())
161         .append(returnToken);
162       sb.append(" Path Translated: " + req.getPathTranslated())
163         .append(returnToken);
164       sb.append(" Content Length: " + req.getContentLength())
165         .append(returnToken);
166       sb.append(" Content Type: " + req.getContentType())
167         .append(returnToken);
168
169       String JavaDoc queryString = req.getQueryString();
170       sb.append(" QueryString: " + queryString)
171         .append(returnToken);
172
173       if (queryString != null) {
174          Map ht = req.getParameterMap();
175          sb.append(geKeyValuesData(ht, returnToken))
176            .append(returnToken);
177       }
178
179       sb.append(" Server Name: " + req.getServerName())
180         .append(returnToken);
181       sb.append(" Server Port: " + req.getServerPort())
182         .append(returnToken);
183       sb.append(" Remote User: " + req.getRemoteUser())
184         .append(returnToken);
185       sb.append(" Remote Host: " + req.getRemoteHost())
186         .append(returnToken);
187       sb.append(" Remote Address: " + req.getRemoteAddr())
188         .append(returnToken);
189       sb.append(" Authentication Scheme: " + req.getAuthType())
190         .append("")
191         .append(returnToken);
192
193       // return parameters (name/value pairs)
194
int maxParamNameLength = getElementNameMaxLength(req.getParameterMap().keySet().iterator());
195       Enumeration params = req.getParameterNames();
196
197       if (params.hasMoreElements()) {
198          sb.append(returnToken)
199            .append("Parameters:")
200            .append(returnToken);
201
202          while (params.hasMoreElements()) {
203             String JavaDoc name = (String JavaDoc) params.nextElement();
204             sb.append(" ")
205               .append(name)
206               .append(addSpaces(maxParamNameLength, name))
207               .append(" = ");
208
209             String JavaDoc[] values = req.getParameterValues(name);
210
211             for (int x = 0; x < values.length; x++) {
212                if (x > 0) {
213                   sb.append(", ");
214                }
215
216                sb.append(values[x]);
217             }
218
219             sb.append(returnToken);
220          }
221
222          sb.append(returnToken);
223       }
224
225       // return HTTP Headers
226
Enumeration e = req.getHeaderNames();
227
228       if (e.hasMoreElements()) {
229          sb.append("Request Headers:")
230            .append(returnToken);
231
232          while (e.hasMoreElements()) {
233             String JavaDoc name = (String JavaDoc) e.nextElement();
234             sb.append(" " + name + ": " + req.getHeader(name))
235               .append(returnToken);
236          }
237
238          sb.append(returnToken);
239       }
240
241       sb.append(returnToken);
242       s = sb.toString();
243
244       return s;
245    }
246
247
248    /**
249     * Get the data from the input hashMap where keys are string
250     * and key values are string arrays.
251     *
252     * @param ht the hash table
253     * @param returnToken the return token, i.e.: "\n" or "<br>\n"
254     */

255    private static StringBuffer JavaDoc geKeyValuesData(Map ht,
256                                                String JavaDoc returnToken) {
257       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
258       Iterator keys = ht.keySet()
259                             .iterator();
260       int maxKeyNameLength = getElementNameMaxLength(keys);
261
262       while (keys.hasNext()) {
263          String JavaDoc key = (String JavaDoc) keys.next();
264          String JavaDoc[] values = (String JavaDoc[]) ht.get(key);
265
266          for (int i = 0; i < values.length; i++) {
267             sb.append(" {")
268               .append(key)
269               .append(" [")
270               .append(i)
271               .append("]}")
272               .append(addSpaces(maxKeyNameLength, key))
273               .append(" = ")
274               .append(values[i])
275               .append(returnToken);
276          }
277       }
278
279       return sb;
280    }
281 }
282
Popular Tags