1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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 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 dumpRequest(HttpServletRequest req,
66 String returnToken) {
67 String s = null;
68
69 try {
70 s = dump(req, returnToken);
71 } catch (Exception 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 paramName = (String) 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 addSpaces(int spacesToAdd,
111 String fromString) {
112 int len = spacesToAdd;
113 String 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 dump(HttpServletRequest req,
136 String returnToken)
137 throws ServletException, IOException {
138 StringBuffer sb = new StringBuffer();
139 String 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 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
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 name = (String) params.nextElement();
204 sb.append(" ")
205 .append(name)
206 .append(addSpaces(maxParamNameLength, name))
207 .append(" = ");
208
209 String[] 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
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 name = (String) 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 geKeyValuesData(Map ht,
256 String returnToken) {
257 StringBuffer sb = new StringBuffer();
258 Iterator keys = ht.keySet()
259 .iterator();
260 int maxKeyNameLength = getElementNameMaxLength(keys);
261
262 while (keys.hasNext()) {
263 String key = (String) keys.next();
264 String[] values = (String[]) 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 }