KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joseki > server > webserver > servlets > DumpServlet


1 /*
2  * (c) Copyright 2003, 2004 Hewlett-Packard Development Company, LP
3  * [See end of file]
4  *
5  * dumpEnvironment and dumpServletContext from patch by Fred Hartman // webMethods.
6  */

7
8 /** A servlet that dumps its request
9  */

10
11 // Could be neater - much, much neater!
12
package org.joseki.server.webserver.servlets;
13
14 import java.util.* ;
15 import java.io.* ;
16 import javax.servlet.http.*;
17 import javax.servlet.* ;
18
19 public class DumpServlet extends HttpServlet
20 {
21
22     public DumpServlet()
23     {
24
25     }
26
27     public void init()
28     {
29         return ;
30     }
31
32     public void doGet(HttpServletRequest req, HttpServletResponse resp)
33     {
34         try {
35             PrintWriter out = resp.getWriter() ;
36             resp.setContentType("text/html");
37
38             String now = new Date().toString() ;
39
40             // HEAD
41
out.println("<html>") ;
42             out.println("<head>") ;
43             out.println("<Title>Dump @ "+now+"</Title>") ;
44             // Reduce the desire to cache it.
45
out.println("<meta CONTENT=now HTTP-EQUIV=expires>") ;
46             out.println("</head>") ;
47
48             // BODY
49
out.println("<body>") ;
50             out.println("<pre>") ;
51
52             out.println("Dump : "+now);
53             out.println() ;
54             out.println("==== Request");
55             out.println() ;
56             out.print(dumpRequest(req)) ;
57             out.println() ;
58                         
59             out.println("==== ServletContext");
60             out.println() ;
61             out.print(dumpServletContext());
62             out.println() ;
63
64             out.println("==== Environment");
65             out.println() ;
66             out.print(dumpEnvironment());
67             out.println() ;
68
69             out.println("</pre>") ;
70
71             out.println("</body>") ;
72             out.println("</html>") ;
73             out.flush() ;
74         } catch (IOException e)
75         { }
76     }
77
78     // This resets the input stream
79

80     static public String dumpRequest(HttpServletRequest req)
81     {
82         try {
83             StringWriter sw = new StringWriter() ;
84             PrintWriter pw = new PrintWriter(sw) ;
85
86             // Standard environment
87
pw.println("Method: "+req.getMethod());
88             pw.println("getContentLength: "+Integer.toString(req.getContentLength()));
89             pw.println("getContentType: "+req.getContentType());
90             pw.println("getRequestURI: "+req.getRequestURI());
91             pw.println("getRequestURL: "+req.getRequestURL());
92             pw.println("getContextPath: "+req.getContextPath());
93             pw.println("getServletPath: "+req.getServletPath());
94             pw.println("getPathInfo: "+req.getPathInfo());
95             pw.println("getPathTranslated: "+req.getPathTranslated());
96             pw.println("getQueryString: "+req.getQueryString());
97             pw.println("getProtocol: "+req.getProtocol());
98             pw.println("getScheme: "+req.getScheme());
99             pw.println("getServerName: "+req.getServerName());
100             pw.println("getServerPort: "+req.getServerPort());
101             pw.println("getRemoteUser: "+req.getRemoteUser());
102             pw.println("getRemoteAddr: "+req.getRemoteAddr());
103             pw.println("getRemoteHost: "+req.getRemoteHost());
104             pw.println("getRequestedSessionId: "+req.getRequestedSessionId());
105             {
106                 String tmp = "" ;
107                 Cookie c[] = req.getCookies() ;
108                 if ( c == null )
109                     pw.println("getCookies: <none>");
110                 else
111                 {
112                     for ( int i = 0 ; i < c.length ; i++ )
113                     {
114                         pw.println("Cookie: "+c[i].getName());
115                         pw.println(" value: "+c[i].getValue());
116                         pw.println(" version: "+c[i].getVersion());
117                         pw.println(" comment: "+c[i].getComment());
118                         pw.println(" domain: "+c[i].getDomain());
119                         pw.println(" maxAge: "+c[i].getMaxAge());
120                         pw.println(" path: "+c[i].getPath());
121                         pw.println(" secure: "+c[i].getSecure());
122                         pw.println();
123                     }
124                 }
125             }
126             // To do: create a string for the output so can send to console and return it.
127
Enumeration enum = req.getHeaderNames() ;
128
129             for ( ; enum.hasMoreElements() ; )
130             {
131                 String name = (String)enum.nextElement() ;
132                 String value = req.getHeader(name) ;
133                 pw.println("Head: "+name + " = " + value) ;
134             }
135
136             enum = req.getAttributeNames() ;
137             if ( enum.hasMoreElements() )
138                 pw.println();
139             for ( ; enum.hasMoreElements() ; )
140             {
141                 String name = (String)enum.nextElement() ;
142                 String value = req.getAttribute(name).toString() ;
143                 pw.println("Attr: "+name + " = " + value) ;
144             }
145
146             // Note that doing this on a form causes the forms content (body) to be read
147
// and parsed as form variables.
148
// enum = req.getParameterNames() ;
149
// if ( enum.hasMoreElements() )
150
// pw.println();
151
// for ( ; enum.hasMoreElements() ; )
152
// {
153
// String name = (String)enum.nextElement() ;
154
// String value = req.getParameter(name) ;
155
// pw.println("Param: "+name + " = " + value) ;
156
// }
157

158             // Don't use ServletRequest.getParameter or getParamterNames
159
// as that reads form data. This code parses just the query string.
160
if ( req.getQueryString() != null )
161             {
162                 pw.println();
163                 String[] params = req.getQueryString().split("&") ;
164                 for ( int i = 0 ; i < params.length ; i++ )
165                 {
166                     String p = params[i] ;
167                     String[] x = p.split("=",2) ;
168                     String name = null ;
169                     String value = null ;
170                     
171                     if ( x.length == 0 )
172                     {
173                         name = p ;
174                         value = "" ;
175                     }
176                     else if ( x.length == 1 )
177                     {
178                         name = x[0] ;
179                         value = "" ;
180                     }
181                     else
182                     {
183                         name = x[0] ;
184                         value = x[1] ;
185                     }
186                     pw.println("Param: "+name + " = " + value) ;
187                 }
188             }
189             
190             enum = req.getLocales();
191             if ( enum.hasMoreElements() )
192                 pw.println();
193             for ( ; enum.hasMoreElements() ; )
194             {
195                 String name = ((Locale)enum.nextElement()).toString() ;
196                 pw.println("Locale: "+name) ;
197             }
198
199             pw.println() ;
200
201             BufferedReader in = req.getReader() ;
202             if ( req.getContentLength() > 0 )
203                 // Need +2 because last line may not have a CR/LF on it.
204
in.mark(req.getContentLength()+2) ;
205             else
206                 // This is a dump - try to do something that works, even if inefficient.
207
in.mark(100*1024) ;
208
209
210             while(in.ready())
211             {
212                 pw.println(in.readLine());
213             }
214
215             try { in.reset() ;} catch (IOException e) { System.out.println("DumpServlet: Reset of content failed: "+e) ; }
216
217             pw.close() ;
218             sw.close() ;
219             return sw.toString() ;
220         } catch (IOException e)
221         { }
222         return null ;
223     }
224
225     /**
226      * <code>dumpEnvironment</code>
227      * @return String that is the HTML of the System properties as
228 name/value pairs.
229      * The values are with single quotes independent of whether or not
230 the value has
231      * single quotes in it.
232      */

233     static public String dumpEnvironment()
234     {
235         Properties properties = System.getProperties();
236         StringWriter sw = new StringWriter() ;
237         PrintWriter pw = new PrintWriter(sw) ;
238         Enumeration enum = properties.keys();
239         while(enum.hasMoreElements())
240         {
241             String key = (String)enum.nextElement();
242             pw.println(key+": '"+properties.getProperty(key)+"'");
243         }
244         pw.println() ;
245         pw.close() ;
246         try {
247             sw.close() ;
248         } catch (IOException e) {
249             e.printStackTrace();
250         }
251         return sw.toString() ;
252     }
253
254     public String dumpServletContext()
255     {
256         StringWriter sw = new StringWriter() ;
257         PrintWriter pw = new PrintWriter(sw) ;
258
259         ServletContext sc = getServletContext();
260         pw.println("majorVersion: '"+sc.getMajorVersion()+"'");
261         pw.println("minorVersion: '"+sc.getMinorVersion()+"'");
262         pw.println("contextName: '"+sc.getServletContextName()+"'");
263         pw.println("servletInfo: '"+getServletInfo()+"'");
264         pw.println("serverInfo: '"+sc.getServerInfo()+"'");
265
266         Enumeration enum = null ;
267         // Deprecated and will be removed - from Servlet API 2.0
268
// Enumeration enum = sc.getServlets();
269
// if (enum != null) {
270
// pw.println("servlets: ");
271
// while(enum.hasMoreElements())
272
// {
273
// String key = (String)enum.nextElement();
274
// try {
275
// pw.println(key+": '"+sc.getServlet(key)+"'");
276
// } catch (ServletException e1) {
277
// pw.println(key+": '"+e1.toString()+"'");
278
// }
279
// }
280
// }
281
enum = sc.getInitParameterNames();
282         if (enum != null) {
283             pw.println("initParameters: ");
284             while(enum.hasMoreElements())
285             {
286                 String key = (String)enum.nextElement();
287                 pw.println(key+": '"+sc.getInitParameter(key)+"'");
288             }
289         }
290         enum = sc.getAttributeNames();
291         if (enum != null) {
292             pw.println("attributes: ");
293             while(enum.hasMoreElements())
294             {
295                 String key = (String)enum.nextElement();
296                 pw.println(key+": '"+sc.getAttribute(key)+"'");
297             }
298         }
299         pw.println() ;
300         pw.close() ;
301         try {
302             sw.close() ;
303         } catch (IOException e) {
304             e.printStackTrace();
305         }
306         return sw.toString() ;
307     }
308
309     
310     public void doPost(HttpServletRequest req, HttpServletResponse resp)
311     {
312         doGet(req, resp) ;
313     }
314
315
316     public String getServletInfo()
317     {
318         return "Dump";
319     }
320 }
321
322 /*
323  * (c) Copyright 2003, 2004 Hewlett-Packard Development Company, LP
324  * All rights reserved.
325  *
326  * Redistribution and use in source and binary forms, with or without
327  * modification, are permitted provided that the following conditions
328  * are met:
329  * 1. Redistributions of source code must retain the above copyright
330  * notice, this list of conditions and the following disclaimer.
331  * 2. Redistributions in binary form must reproduce the above copyright
332  * notice, this list of conditions and the following disclaimer in the
333  * documentation and/or other materials provided with the distribution.
334  * 3. The name of the author may not be used to endorse or promote products
335  * derived from this software without specific prior written permission.
336  *
337  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
338  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
339  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
340  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
341  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
342  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
343  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
344  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
345  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
346  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
347  */

348
Popular Tags