KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > http > handler > DumpHandler


1 // ========================================================================
2
// $Id: DumpHandler.java,v 1.14 2005/08/13 00:01:26 gregwilkins Exp $
3
// Copyright 1999-2004 Mort Bay Consulting Pty. Ltd.
4
// ------------------------------------------------------------------------
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15

16 package org.mortbay.http.handler;
17
18 import java.io.ByteArrayOutputStream JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.io.OutputStream JavaDoc;
22 import java.io.OutputStreamWriter JavaDoc;
23 import java.io.Writer JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Set JavaDoc;
28
29 import javax.servlet.http.Cookie JavaDoc;
30
31 import org.apache.commons.logging.Log;
32 import org.mortbay.log.LogFactory;
33 import org.mortbay.http.HttpException;
34 import org.mortbay.http.HttpFields;
35 import org.mortbay.http.HttpRequest;
36 import org.mortbay.http.HttpResponse;
37 import org.mortbay.util.LogSupport;
38 import org.mortbay.util.StringUtil;
39
40 /* ------------------------------------------------------------ */
41 /** Dump request handler.
42  * Dumps GET and POST requests.
43  * Useful for testing and debugging.
44  *
45  * @version $Id: DumpHandler.java,v 1.14 2005/08/13 00:01:26 gregwilkins Exp $
46  * @author Greg Wilkins (gregw)
47  */

48 public class DumpHandler extends AbstractHttpHandler
49 {
50     private static Log log = LogFactory.getLog(DumpHandler.class);
51
52     /* ----------------------------------------------------------------- */
53     public String JavaDoc realPath(String JavaDoc pathSpec, String JavaDoc path)
54     {
55         return "";
56     }
57     
58     /* ------------------------------------------------------------ */
59     public void handle(String JavaDoc pathInContext,
60                        String JavaDoc pathParams,
61                        HttpRequest request,
62                        HttpResponse response)
63         throws HttpException, IOException JavaDoc
64     {
65         if (!isStarted())
66             return;
67         
68         // Only handle GET, HEAD and POST
69
if (!HttpRequest.__GET.equals(request.getMethod()) &&
70             !HttpRequest.__HEAD.equals(request.getMethod()) &&
71             !HttpRequest.__POST.equals(request.getMethod()))
72             return;
73
74         log.debug("Dump");
75         
76         response.setField(HttpFields.__ContentType,
77                           HttpFields.__TextHtml);
78         OutputStream JavaDoc out = response.getOutputStream();
79         ByteArrayOutputStream JavaDoc buf = new ByteArrayOutputStream JavaDoc(2048);
80         Writer JavaDoc writer = new OutputStreamWriter JavaDoc(buf,StringUtil.__ISO_8859_1);
81         writer.write("<HTML><H1>Dump HttpHandler</H1>");
82         writer.write("<PRE>\npath="+request.getPath()+
83                     "\ncontextPath="+getHttpContext().getContextPath()+
84                     "\npathInContext="+pathInContext+
85                     "\n</PRE>\n");
86         writer.write("<H3>Header:</H3><PRE>");
87         writer.write(request.toString());
88         writer.write("</PRE>\n<H3>Parameters:</H3>\n<PRE>");
89         Set JavaDoc names=request.getParameterNames();
90         Iterator JavaDoc iter = names.iterator();
91         while(iter.hasNext())
92         {
93             String JavaDoc name=iter.next().toString();
94             List JavaDoc values=request.getParameterValues(name);
95             if (values==null || values.size()==0)
96             {
97                 writer.write(name);
98                 writer.write("=\n");
99             }
100             else if (values.size()==1)
101             {
102                 writer.write(name);
103                 writer.write("=");
104                 writer.write((String JavaDoc)values.get(0));
105                 writer.write("\n");
106             }
107             else
108             {
109                 for (int i=0; i<values.size(); i++)
110                 {
111                     writer.write(name);
112                     writer.write("["+i+"]=");
113                     writer.write((String JavaDoc)values.get(i));
114                     writer.write("\n");
115                 }
116             }
117         }
118         
119         String JavaDoc cookie_name=request.getParameter("CookieName");
120         if (cookie_name!=null && cookie_name.trim().length()>0)
121         {
122             String JavaDoc cookie_action=request.getParameter("Button");
123             try{
124                 Cookie JavaDoc cookie=
125                     new Cookie JavaDoc(cookie_name.trim(),
126                                request.getParameter("CookieVal"));
127                 if ("Clear Cookie".equals(cookie_action))
128                     cookie.setMaxAge(0);
129                 response.addSetCookie(cookie);
130             }
131             catch(IllegalArgumentException JavaDoc e)
132             {
133                 writer.write("</PRE>\n<H3>BAD Set-Cookie:</H3>\n<PRE>");
134                 writer.write(e.toString());
135                 LogSupport.ignore(log,e);
136             }
137         }
138         
139         writer.write("</PRE>\n<H3>Cookies:</H3>\n<PRE>");
140         Cookie JavaDoc[] cookies=request.getCookies();
141         if (cookies!=null && cookies.length>0)
142         {
143             for(int c=0;c<cookies.length;c++)
144             {
145                 Cookie JavaDoc cookie=cookies[c];
146                 writer.write(cookie.getName());
147                 writer.write("=");
148                 writer.write(cookie.getValue());
149                 writer.write("\n");
150             }
151         }
152         
153         writer.write("</PRE>\n<H3>Attributes:</H3>\n<PRE>");
154         Enumeration JavaDoc attributes=request.getAttributeNames();
155         if (attributes!=null && attributes.hasMoreElements())
156         {
157             while(attributes.hasMoreElements())
158             {
159                 String JavaDoc attr=attributes.nextElement().toString();
160                 writer.write(attr);
161                 writer.write("=");
162                 writer.write(request.getAttribute(attr).toString());
163                 writer.write("\n");
164             }
165         }
166         
167         writer.write("</PRE>\n<H3>Content:</H3>\n<PRE>");
168         byte[] content= new byte[4096];
169         int len;
170         try{
171             InputStream JavaDoc in=request.getInputStream();
172             while((len=in.read(content))>=0)
173                 writer.write(new String JavaDoc(content,0,len));
174         }
175         catch(IOException JavaDoc e)
176         {
177             LogSupport.ignore(log,e);
178             writer.write(e.toString());
179         }
180         
181         // You wouldn't normally set a trailer like this, but
182
// we don't want to commit the output to force trailers as
183
// it makes test harness messy
184
request.getAcceptableTransferCodings();
185         
186         // commit now
187
writer.flush();
188         response.setIntField(HttpFields.__ContentLength,buf.size()+1000);
189         buf.writeTo(out);
190         out.flush();
191         
192         // Now add the response
193
buf.reset();
194         writer.write("</PRE>\n<H3>Response:</H3>\n<PRE>");
195         writer.write(response.toString());
196         writer.write("</PRE></HTML>");
197         writer.flush();
198         for (int pad=998-buf.size();pad-->0;)
199             writer.write(" ");
200         writer.write("\015\012");
201         writer.flush();
202         buf.writeTo(out);
203             
204         request.setHandled(true);
205     }
206 }
207
208
209
210
211
212
Popular Tags