View Javadoc

1   // ========================================================================
2   // Copyright (c) 1996-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package com.acme;
15  import java.io.BufferedWriter;
16  import java.io.IOException;
17  import java.io.OutputStream;
18  import java.io.OutputStreamWriter;
19  import java.io.PrintWriter;
20  import java.io.Reader;
21  import java.lang.reflect.Array;
22  import java.lang.reflect.Field;
23  import java.util.Enumeration;
24  import java.util.Locale;
25  
26  import javax.servlet.ServletConfig;
27  import javax.servlet.ServletContext;
28  import javax.servlet.ServletException;
29  import javax.servlet.ServletRequest;
30  import javax.servlet.ServletRequestWrapper;
31  import javax.servlet.ServletResponse;
32  import javax.servlet.ServletResponseWrapper;
33  import javax.servlet.UnavailableException;
34  import javax.servlet.http.Cookie;
35  import javax.servlet.http.HttpServlet;
36  import javax.servlet.http.HttpServletRequest;
37  import javax.servlet.http.HttpServletRequestWrapper;
38  import javax.servlet.http.HttpServletResponse;
39  import javax.servlet.http.HttpServletResponseWrapper;
40  
41  import org.eclipse.jetty.continuation.Continuation;
42  import org.eclipse.jetty.continuation.ContinuationListener;
43  import org.eclipse.jetty.continuation.ContinuationSupport;
44  import org.eclipse.jetty.http.HttpHeaders;
45  import org.eclipse.jetty.util.StringUtil;
46  import org.eclipse.jetty.util.log.Log;
47  
48  
49  
50  /* ------------------------------------------------------------ */
51  /** Dump Servlet Request.
52   * 
53   */
54  public class Dump extends HttpServlet
55  {
56      static boolean fixed;
57      /* ------------------------------------------------------------ */
58      @Override
59      public void init(ServletConfig config) throws ServletException
60      {
61      	super.init(config);
62      	
63      	if (config.getInitParameter("unavailable")!=null && !fixed)
64      	{
65      	    
66      	    fixed=true;
67      	    throw new UnavailableException("Unavailable test",Integer.parseInt(config.getInitParameter("unavailable")));
68      	}
69      }
70  
71      /* ------------------------------------------------------------ */
72      @Override
73      public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
74      {
75          doGet(request, response);
76      }
77  
78      /* ------------------------------------------------------------ */
79      @Override
80      public void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException
81      {
82          // Handle a dump of data
83          final String data= request.getParameter("data");
84          final String chars= request.getParameter("chars");
85          final String block= request.getParameter("block");
86          final String dribble= request.getParameter("dribble");
87          final boolean flush= request.getParameter("flush")!=null?Boolean.parseBoolean(request.getParameter("flush")):false;
88  
89          
90          
91          if(request.getPathInfo()!=null && request.getPathInfo().toLowerCase().indexOf("script")!=-1)
92          {
93              response.sendRedirect(response.encodeRedirectURL(getServletContext().getContextPath() + "/dump/info"));
94              return;
95          }
96              
97          request.setCharacterEncoding("UTF-8");
98          
99          if (request.getParameter("empty")!=null)
100         {
101             response.setStatus(200);
102             response.flushBuffer();
103             return;
104         }
105         
106         if (request.getParameter("sleep")!=null)
107         {
108             try
109             {
110                 long s = Long.parseLong(request.getParameter("sleep"));
111                 if (request.getHeader(HttpHeaders.EXPECT)!=null &&request.getHeader(HttpHeaders.EXPECT).indexOf("102")>=0)
112                 {
113                     Thread.sleep(s/2);
114                     response.sendError(102);
115                     Thread.sleep(s/2);
116                 }
117                 else
118                     Thread.sleep(s);
119             }
120             catch (InterruptedException e)
121             {
122                 return;
123             }
124             catch (Exception e)
125             {
126                 throw new ServletException(e);
127             }
128         }
129 
130         if (request.getAttribute("RESUME")==null && request.getParameter("resume")!=null)
131         {
132             request.setAttribute("RESUME",Boolean.TRUE);
133 
134             final long resume=Long.parseLong(request.getParameter("resume"));
135             new Thread(new Runnable()
136             {
137                 public void run()
138                 {
139                     try
140                     {
141                         Thread.sleep(resume);
142                     }
143                     catch (InterruptedException e)
144                     {
145                         e.printStackTrace();
146                     }
147                     Continuation continuation = ContinuationSupport.getContinuation(request);
148                     continuation.resume();
149                 }
150                 
151             }).start();
152         }
153 
154         if (request.getParameter("complete")!=null)
155         {
156             final long complete=Long.parseLong(request.getParameter("complete"));
157             new Thread(new Runnable()
158             {
159                 public void run()
160                 {
161                     try
162                     {
163                         Thread.sleep(complete);
164                     }
165                     catch (InterruptedException e)
166                     {
167                         e.printStackTrace();
168                     }
169                     try
170                     {
171                         response.setContentType("text/html");
172                         response.getOutputStream().println("<h1>COMPLETED</h1>"); 
173                         Continuation continuation = ContinuationSupport.getContinuation(request);
174                         continuation.complete();
175                     }
176                     catch (IOException e)
177                     {
178                         e.printStackTrace();
179                     }
180                 }
181 
182             }).start();
183         }
184         
185         if (request.getParameter("suspend")!=null && request.getAttribute("SUSPEND")!=Boolean.TRUE)
186         {
187             request.setAttribute("SUSPEND",Boolean.TRUE);
188             try
189             {
190                 Continuation continuation = ContinuationSupport.getContinuation(request);
191                 continuation.setTimeout(Long.parseLong(request.getParameter("suspend")));
192                 continuation.suspend();
193                 
194                 continuation.addContinuationListener(new ContinuationListener()
195                 {   
196                     public void onTimeout(Continuation continuation)
197                     {
198                         response.addHeader("Dump","onTimeout");
199                         try
200                         {
201                             dump(response,data,chars,block,dribble,flush);
202                             continuation.complete();
203                         }
204                         catch (IOException e)
205                         {
206                             Log.ignore(e);
207                         }
208                     }
209                     
210                     public void onComplete(Continuation continuation)
211                     {
212                         response.addHeader("Dump","onComplete");
213                     }
214                 });
215                 
216                 continuation.undispatch();
217             }
218             catch(Exception e)
219             {
220                 throw new ServletException(e);
221             }
222         }        
223             
224         request.setAttribute("Dump", this);
225         getServletContext().setAttribute("Dump",this);
226         // getServletContext().log("dump "+request.getRequestURI());
227 
228         // Force a content length response
229         String length= request.getParameter("length");
230         if (length != null && length.length() > 0)
231         {
232             response.setContentLength(Integer.parseInt(length));
233         }
234 
235         // Handle a dump of data
236         if (dump(response,data,chars,block,dribble,flush))
237             return;
238         
239         // handle an exception
240         String info= request.getPathInfo();
241         if (info != null && info.endsWith("Exception"))
242         {
243             try
244             {
245                 throw (Throwable) Thread.currentThread().getContextClassLoader().loadClass(info.substring(1)).newInstance();
246             }
247             catch (Throwable th)
248             {
249                 throw new ServletException(th);
250             }
251         }
252 
253         // test a reset
254         String reset= request.getParameter("reset");
255         if (reset != null && reset.length() > 0)
256         {
257             response.getOutputStream().println("THIS SHOULD NOT BE SEEN!");
258             response.setHeader("SHOULD_NOT","BE SEEN");
259             response.reset();
260         }
261         
262         
263         // handle an redirect
264         String redirect= request.getParameter("redirect");
265         if (redirect != null && redirect.length() > 0)
266         {
267             response.getOutputStream().println("THIS SHOULD NOT BE SEEN!");
268             response.sendRedirect(response.encodeRedirectURL(redirect));
269             try
270             {
271                 response.getOutputStream().println("THIS SHOULD NOT BE SEEN!");
272             }
273             catch(IOException e)
274             {
275                 // ignored as stream is closed.
276             }
277             return;
278         }
279 
280         // handle an error
281         String error= request.getParameter("error");
282         if (error != null && error.length() > 0 && request.getAttribute("javax.servlet.error.status_code")==null)
283         {
284             response.getOutputStream().println("THIS SHOULD NOT BE SEEN!");
285             response.sendError(Integer.parseInt(error));
286             try
287             {
288                 response.getOutputStream().println("THIS SHOULD NOT BE SEEN!");
289             }
290             catch(IllegalStateException e)
291             {
292                 try
293                 {
294                     response.getWriter().println("NOR THIS!!"); 
295                 }
296                 catch(IOException e2){}
297             }
298             catch(IOException e){}
299             return;
300         }
301 
302         // Handle a extra headers 
303         String headers= request.getParameter("headers");
304         if (headers != null && headers.length() > 0)
305         {
306             long h=Long.parseLong(headers);
307             for (int i=0;i<h;i++)
308                 response.addHeader("Header"+i,"Value"+i);
309         }
310 
311         String buffer= request.getParameter("buffer");
312         if (buffer != null && buffer.length() > 0)
313             response.setBufferSize(Integer.parseInt(buffer));
314 
315         String charset= request.getParameter("charset");
316         if (charset==null)
317             charset="UTF-8";
318         response.setCharacterEncoding(charset);
319         response.setContentType("text/html");
320 
321         if (info != null && info.indexOf("Locale/") >= 0)
322         {
323             try
324             {
325                 String locale_name= info.substring(info.indexOf("Locale/") + 7);
326                 Field f= java.util.Locale.class.getField(locale_name);
327                 response.setLocale((Locale)f.get(null));
328             }
329             catch (Exception e)
330             {
331                 e.printStackTrace();
332                 response.setLocale(Locale.getDefault());
333             }
334         }
335 
336         String cn= request.getParameter("cookie");
337         String cv=request.getParameter("cookiev");
338         if (cn!=null && cv!=null)
339         {
340             Cookie cookie= new Cookie(cn, cv);
341             if (request.getParameter("version")!=null)
342                 cookie.setVersion(Integer.parseInt(request.getParameter("version")));
343             cookie.setComment("Cookie from dump servlet");
344             response.addCookie(cookie);
345         }
346 
347         String pi= request.getPathInfo();
348         if (pi != null && pi.startsWith("/ex"))
349         {
350             OutputStream out= response.getOutputStream();
351             out.write("</H1>This text should be reset</H1>".getBytes());
352             if ("/ex0".equals(pi))
353                 throw new ServletException("test ex0", new Throwable());
354             else if ("/ex1".equals(pi))
355                 throw new IOException("test ex1");
356             else if ("/ex2".equals(pi))
357                 throw new UnavailableException("test ex2");
358             else if (pi.startsWith("/ex3/"))
359                 throw new UnavailableException("test ex3",Integer.parseInt(pi.substring(5)));
360             throw new RuntimeException("test");
361         }
362 
363         if ("true".equals(request.getParameter("close")))
364             response.setHeader("Connection","close");
365 
366         String buffered= request.getParameter("buffered");
367         
368         PrintWriter pout=null;
369         
370         try
371         {
372             pout =response.getWriter();
373         }
374         catch(IllegalStateException e)
375         {
376             pout=new PrintWriter(new OutputStreamWriter(response.getOutputStream(),charset));
377         }
378         if (buffered!=null)
379             pout = new PrintWriter(new BufferedWriter(pout,Integer.parseInt(buffered)));
380         
381         try
382         {
383             pout.write("<html>\n<body>\n");
384             pout.write("<h1>Dump Servlet</h1>\n");
385             pout.write("<table width=\"95%\">");
386             pout.write("<tr>\n");
387             pout.write("<th align=\"right\">getMethod:&nbsp;</th>");
388             pout.write("<td>" + notag(request.getMethod())+"</td>");
389             pout.write("</tr><tr>\n");
390             pout.write("<th align=\"right\">getContentLength:&nbsp;</th>");
391             pout.write("<td>"+Integer.toString(request.getContentLength())+"</td>");
392             pout.write("</tr><tr>\n");
393             pout.write("<th align=\"right\">getContentType:&nbsp;</th>");
394             pout.write("<td>"+notag(request.getContentType())+"</td>");
395             pout.write("</tr><tr>\n");
396             pout.write("<th align=\"right\">getRequestURI:&nbsp;</th>");
397             pout.write("<td>"+notag(request.getRequestURI())+"</td>");
398             pout.write("</tr><tr>\n");
399             pout.write("<th align=\"right\">getRequestURL:&nbsp;</th>");
400             pout.write("<td>"+notag(request.getRequestURL().toString())+"</td>");
401             pout.write("</tr><tr>\n");
402             pout.write("<th align=\"right\">getContextPath:&nbsp;</th>");
403             pout.write("<td>"+request.getContextPath()+"</td>");
404             pout.write("</tr><tr>\n");
405             pout.write("<th align=\"right\">getServletPath:&nbsp;</th>");
406             pout.write("<td>"+notag(request.getServletPath())+"</td>");
407             pout.write("</tr><tr>\n");
408             pout.write("<th align=\"right\">getPathInfo:&nbsp;</th>");
409             pout.write("<td>"+notag(request.getPathInfo())+"</td>");
410             pout.write("</tr><tr>\n");
411             pout.write("<th align=\"right\">getPathTranslated:&nbsp;</th>");
412             pout.write("<td>"+notag(request.getPathTranslated())+"</td>");
413             pout.write("</tr><tr>\n");
414             pout.write("<th align=\"right\">getQueryString:&nbsp;</th>");
415             pout.write("<td>"+notag(request.getQueryString())+"</td>");
416             pout.write("</tr><tr>\n");
417             
418             pout.write("<th align=\"right\">getProtocol:&nbsp;</th>");
419             pout.write("<td>"+request.getProtocol()+"</td>");
420             pout.write("</tr><tr>\n");
421             pout.write("<th align=\"right\">getScheme:&nbsp;</th>");
422             pout.write("<td>"+request.getScheme()+"</td>");
423             pout.write("</tr><tr>\n");
424             pout.write("<th align=\"right\">getServerName:&nbsp;</th>");
425             pout.write("<td>"+notag(request.getServerName())+"</td>");
426             pout.write("</tr><tr>\n");
427             pout.write("<th align=\"right\">getServerPort:&nbsp;</th>");
428             pout.write("<td>"+Integer.toString(request.getServerPort())+"</td>");
429             pout.write("</tr><tr>\n");
430             pout.write("<th align=\"right\">getLocalName:&nbsp;</th>");
431             pout.write("<td>"+request.getLocalName()+"</td>");
432             pout.write("</tr><tr>\n");
433             pout.write("<th align=\"right\">getLocalAddr:&nbsp;</th>");
434             pout.write("<td>"+request.getLocalAddr()+"</td>");
435             pout.write("</tr><tr>\n");
436             pout.write("<th align=\"right\">getLocalPort:&nbsp;</th>");
437             pout.write("<td>"+Integer.toString(request.getLocalPort())+"</td>");
438             pout.write("</tr><tr>\n");
439             pout.write("<th align=\"right\">getRemoteUser:&nbsp;</th>");
440             pout.write("<td>"+request.getRemoteUser()+"</td>");
441             pout.write("</tr><tr>\n");
442             pout.write("<th align=\"right\">getRemoteAddr:&nbsp;</th>");
443             pout.write("<td>"+request.getRemoteAddr()+"</td>");
444             pout.write("</tr><tr>\n");
445             pout.write("<th align=\"right\">getRemoteHost:&nbsp;</th>");
446             pout.write("<td>"+request.getRemoteHost()+"</td>");
447             pout.write("</tr><tr>\n");
448             pout.write("<th align=\"right\">getRemotePort:&nbsp;</th>");
449             pout.write("<td>"+request.getRemotePort()+"</td>");
450             pout.write("</tr><tr>\n");
451             pout.write("<th align=\"right\">getRequestedSessionId:&nbsp;</th>");
452             pout.write("<td>"+request.getRequestedSessionId()+"</td>");
453             pout.write("</tr><tr>\n");
454             pout.write("<th align=\"right\">isSecure():&nbsp;</th>");
455             pout.write("<td>"+request.isSecure()+"</td>");
456 
457             pout.write("</tr><tr>\n");
458             pout.write("<th align=\"right\">isUserInRole(admin):&nbsp;</th>");
459             pout.write("<td>"+request.isUserInRole("admin")+"</td>");
460 
461             pout.write("</tr><tr>\n");
462             pout.write("<th align=\"right\">getLocale:&nbsp;</th>");
463             pout.write("<td>"+request.getLocale()+"</td>");
464             
465             Enumeration locales= request.getLocales();
466             while (locales.hasMoreElements())
467             {
468                 pout.write("</tr><tr>\n");
469                 pout.write("<th align=\"right\">getLocales:&nbsp;</th>");
470                 pout.write("<td>"+locales.nextElement()+"</td>");
471             }
472             pout.write("</tr><tr>\n");
473             
474             pout.write("<th align=\"left\" colspan=\"2\"><big><br/>Other HTTP Headers:</big></th>");
475             Enumeration h= request.getHeaderNames();
476             String name;
477             while (h.hasMoreElements())
478             {
479                 name= (String)h.nextElement();
480 
481                 Enumeration h2= request.getHeaders(name);
482                 while (h2.hasMoreElements())
483                 {
484                     String hv= (String)h2.nextElement();
485                     pout.write("</tr><tr>\n");
486                     pout.write("<th align=\"right\">"+notag(name)+":&nbsp;</th>");
487                     pout.write("<td>"+notag(hv)+"</td>");
488                 }
489             }
490 
491             pout.write("</tr><tr>\n");
492             pout.write("<th align=\"left\" colspan=\"2\"><big><br/>Request Parameters:</big></th>");
493             h= request.getParameterNames();
494             while (h.hasMoreElements())
495             {
496                 name= (String)h.nextElement();
497                 pout.write("</tr><tr>\n");
498                 pout.write("<th align=\"right\">"+notag(name)+":&nbsp;</th>");
499                 pout.write("<td>"+notag(request.getParameter(name))+"</td>");
500                 String[] values= request.getParameterValues(name);
501                 if (values == null)
502                 {
503                     pout.write("</tr><tr>\n");
504                     pout.write("<th align=\"right\">"+notag(name)+" Values:&nbsp;</th>");
505                     pout.write("<td>"+"NULL!"+"</td>");
506                 }
507                 else if (values.length > 1)
508                 {
509                     for (int i= 0; i < values.length; i++)
510                     {
511                         pout.write("</tr><tr>\n");
512                         pout.write("<th align=\"right\">"+notag(name)+"["+i+"]:&nbsp;</th>");
513                         pout.write("<td>"+notag(values[i])+"</td>");
514                     }
515                 }
516             }
517 
518             pout.write("</tr><tr>\n");
519             pout.write("<th align=\"left\" colspan=\"2\"><big><br/>Cookies:</big></th>");
520             Cookie[] cookies = request.getCookies();
521             for (int i=0; cookies!=null && i<cookies.length;i++)
522             {
523                 Cookie cookie = cookies[i];
524 
525                 pout.write("</tr><tr>\n");
526                 pout.write("<th align=\"right\">"+notag(cookie.getName())+":&nbsp;</th>");
527                 pout.write("<td>"+notag(cookie.getValue())+"</td>");
528             }
529             
530             String content_type=request.getContentType();
531             if (content_type!=null &&
532                 !content_type.startsWith("application/x-www-form-urlencoded") &&
533                 !content_type.startsWith("multipart/form-data"))
534             {
535                 pout.write("</tr><tr>\n");
536                 pout.write("<th align=\"left\" valign=\"top\" colspan=\"2\"><big><br/>Content:</big></th>");
537                 pout.write("</tr><tr>\n");
538                 pout.write("<td><pre>");
539                 char[] content= new char[4096];
540                 int len;
541                 try{
542                     Reader in=request.getReader();
543                     
544                     while((len=in.read(content))>=0)
545                         pout.write(notag(new String(content,0,len)));
546                 }
547                 catch(IOException e)
548                 {
549                     pout.write(e.toString());
550                 }
551                 
552                 pout.write("</pre></td>");
553             }
554             
555             
556             pout.write("</tr><tr>\n");
557             pout.write("<th align=\"left\" colspan=\"2\"><big><br/>Request Attributes:</big></th>");
558             Enumeration a= request.getAttributeNames();
559             while (a.hasMoreElements())
560             {
561                 name= (String)a.nextElement();
562                 pout.write("</tr><tr>\n");
563                 pout.write("<th align=\"right\" valign=\"top\">"+name.replace("."," .")+":&nbsp;</th>");
564                 pout.write("<td>"+"<pre>" + toString(request.getAttribute(name)) + "</pre>"+"</td>");
565             }            
566 
567             
568             pout.write("</tr><tr>\n");
569             pout.write("<th align=\"left\" colspan=\"2\"><big><br/>Servlet InitParameters:</big></th>");
570             a= getInitParameterNames();
571             while (a.hasMoreElements())
572             {
573                 name= (String)a.nextElement();
574                 pout.write("</tr><tr>\n");
575                 pout.write("<th align=\"right\">"+name+":&nbsp;</th>");
576                 pout.write("<td>"+ toString(getInitParameter(name)) +"</td>");
577             }
578 
579             pout.write("</tr><tr>\n");
580             pout.write("<th align=\"left\" colspan=\"2\"><big><br/>Context InitParameters:</big></th>");
581             a= getServletContext().getInitParameterNames();
582             while (a.hasMoreElements())
583             {
584                 name= (String)a.nextElement();
585                 pout.write("</tr><tr>\n");
586                 pout.write("<th align=\"right\" valign=\"top\">"+name.replace("."," .")+":&nbsp;</th>");
587                 pout.write("<td>"+ toString(getServletContext().getInitParameter(name)) + "</td>");
588             }
589 
590             pout.write("</tr><tr>\n");
591             pout.write("<th align=\"left\" colspan=\"2\"><big><br/>Context Attributes:</big></th>");
592             a= getServletContext().getAttributeNames();
593             while (a.hasMoreElements())
594             {
595                 name= (String)a.nextElement();
596                 pout.write("</tr><tr>\n");
597                 pout.write("<th align=\"right\" valign=\"top\">"+name.replace("."," .")+":&nbsp;</th>");
598                 pout.write("<td>"+"<pre>" + toString(getServletContext().getAttribute(name)) + "</pre>"+"</td>");
599             }
600 
601             String res= request.getParameter("resource");
602             if (res != null && res.length() > 0)
603             {
604                 pout.write("</tr><tr>\n");
605                 pout.write("<th align=\"left\" colspan=\"2\"><big><br/>Get Resource: \""+res+"\"</big></th>");
606 
607                 pout.write("</tr><tr>\n");
608                 pout.write("<th align=\"right\">getServletContext().getContext(...):&nbsp;</th>");
609                 
610                 ServletContext context = getServletContext().getContext(res);
611                 pout.write("<td>"+context+"</td>");
612                 
613                 if (context!=null)
614                 {
615                     String cp=context.getContextPath();
616                     if (cp==null || "/".equals(cp))
617                         cp="";
618                     pout.write("</tr><tr>\n");
619                     pout.write("<th align=\"right\">getServletContext().getContext(...),getRequestDispatcher(...):&nbsp;</th>");
620                     pout.write("<td>"+getServletContext().getContext(res).getRequestDispatcher(res.substring(cp.length()))+"</td>");
621                 }
622 
623                 pout.write("</tr><tr>\n");
624                 pout.write("<th align=\"right\">this.getClass().getResource(...):&nbsp;</th>");
625                 pout.write("<td>"+this.getClass().getResource(res)+"</td>");
626 
627                 pout.write("</tr><tr>\n");
628                 pout.write("<th align=\"right\">this.getClass().getClassLoader().getResource(...):&nbsp;</th>");
629                 pout.write("<td>"+this.getClass().getClassLoader().getResource(res)+"</td>");
630 
631                 pout.write("</tr><tr>\n");
632                 pout.write("<th align=\"right\">Thread.currentThread().getContextClassLoader().getResource(...):&nbsp;</th>");
633                 pout.write("<td>"+Thread.currentThread().getContextClassLoader().getResource(res)+"</td>");
634 
635                 pout.write("</tr><tr>\n");
636                 pout.write("<th align=\"right\">getServletContext().getResource(...):&nbsp;</th>");
637                 try{pout.write("<td>"+getServletContext().getResource(res)+"</td>");}
638                 catch(Exception e) {pout.write("<td>"+"" +e+"</td>");}
639             }
640             
641             pout.write("</tr></table>\n");
642 
643             /* ------------------------------------------------------------ */
644             pout.write("<h2>Request Wrappers</h2>\n");
645             ServletRequest rw=request;
646             int w=0;
647             while (rw !=null)
648             {
649                 pout.write((w++)+": "+rw.getClass().getName()+"<br/>");
650                 if (rw instanceof HttpServletRequestWrapper)
651                     rw=((HttpServletRequestWrapper)rw).getRequest();
652                 else if (rw instanceof ServletRequestWrapper)
653                     rw=((ServletRequestWrapper)rw).getRequest();
654                 else
655                     rw=null;
656             }
657 
658             /* ------------------------------------------------------------ */
659             pout.write("<h2>Response Wrappers</h2>\n");
660             ServletResponse rsw=response;
661             w=0;
662             while (rsw !=null)
663             {
664                 pout.write((w++)+": "+rsw.getClass().getName()+"<br/>");
665                 if (rsw instanceof HttpServletResponseWrapper)
666                     rsw=((HttpServletResponseWrapper)rsw).getResponse();
667                 else if (rsw instanceof ServletResponseWrapper)
668                     rsw=((ServletResponseWrapper)rsw).getResponse();
669                 else
670                     rsw=null;
671             }
672             
673             pout.write("<br/>");
674             pout.write("<h2>International Characters (UTF-8)</h2>");
675             pout.write("LATIN LETTER SMALL CAPITAL AE<br/>\n");
676             pout.write("Directly uni encoded(\\u1d01): \u1d01<br/>");
677             pout.write("HTML reference (&amp;AElig;): &AElig;<br/>");
678             pout.write("Decimal (&amp;#7425;): &#7425;<br/>");
679             pout.write("Javascript unicode (\\u1d01) : <script language='javascript'>document.write(\"\u1d01\");</script><br/>");
680             pout.write("<br/>");
681             pout.write("<h2>Form to generate GET content</h2>");
682             pout.write("<form method=\"GET\" action=\""+response.encodeURL(getURI(request))+"\">");
683             pout.write("TextField: <input type=\"text\" name=\"TextField\" value=\"value\"/><br/>\n");
684             pout.write("<input type=\"submit\" name=\"Action\" value=\"Submit\">");
685             pout.write("</form>");
686 
687             pout.write("<br/>");
688             
689             pout.write("<h2>Form to generate POST content</h2>");
690             pout.write("<form method=\"POST\" accept-charset=\"utf-8\" action=\""+response.encodeURL(getURI(request))+"\">");
691             pout.write("TextField: <input type=\"text\" name=\"TextField\" value=\"value\"/><br/>\n");
692             pout.write("Select: <select multiple name=\"Select\">\n");
693             pout.write("<option>ValueA</option>");
694             pout.write("<option>ValueB1,ValueB2</option>");
695             pout.write("<option>ValueC</option>");
696             pout.write("</select><br/>");
697             pout.write("<input type=\"submit\" name=\"Action\" value=\"Submit\"><br/>");
698             pout.write("</form>");
699             pout.write("<br/>");
700             
701             pout.write("<h2>Form to generate UPLOAD content</h2>");
702             pout.write("<form method=\"POST\" enctype=\"multipart/form-data\" accept-charset=\"utf-8\" action=\""+
703                     response.encodeURL(getURI(request))+(request.getQueryString()==null?"":("?"+request.getQueryString()))+
704                     "\">");
705             pout.write("TextField: <input type=\"text\" name=\"TextField\" value=\"comment\"/><br/>\n");
706             pout.write("File 1: <input type=\"file\" name=\"file1\" /><br/>\n");
707             pout.write("File 2: <input type=\"file\" name=\"file2\" /><br/>\n");
708             pout.write("<input type=\"submit\" name=\"Action\" value=\"Submit\"><br/>");
709             pout.write("</form>");
710 
711             pout.write("<h2>Form to set Cookie</h2>");
712             pout.write("<form method=\"POST\" action=\""+response.encodeURL(getURI(request))+"\">");
713             pout.write("cookie: <input type=\"text\" name=\"cookie\" /><br/>\n");
714             pout.write("value: <input type=\"text\" name=\"cookiev\" /><br/>\n");
715             pout.write("<input type=\"submit\" name=\"Action\" value=\"setCookie\">");
716             pout.write("</form>\n");
717             
718             pout.write("<h2>Form to get Resource</h2>");
719             pout.write("<form method=\"POST\" action=\""+response.encodeURL(getURI(request))+"\">");
720             pout.write("resource: <input type=\"text\" name=\"resource\" /><br/>\n");
721             pout.write("<input type=\"submit\" name=\"Action\" value=\"getResource\">");
722             pout.write("</form>\n");
723         }
724         catch (Exception e)
725         {
726             getServletContext().log("dump", e);
727         }
728         
729         String lines= request.getParameter("lines");
730         if (lines!=null)
731         {
732             char[] line = "<span>A line of characters. Blah blah blah blah.  blooble blooble</span></br>\n".toCharArray();
733             for (int l=Integer.parseInt(lines);l-->0;)
734             {
735                 pout.write("<span>"+l+" </span>");
736                 pout.write(line);
737             }
738         }
739         
740         pout.write("</body>\n</html>\n");
741         
742         pout.close();
743 
744         if (pi != null)
745         {
746             if ("/ex4".equals(pi))
747                 throw new ServletException("test ex4", new Throwable());
748             if ("/ex5".equals(pi))
749                 throw new IOException("test ex5");
750             if ("/ex6".equals(pi))
751                 throw new UnavailableException("test ex6");
752         }
753 
754 
755     }
756 
757 
758     /* ------------------------------------------------------------ */
759     @Override
760     public String getServletInfo()
761     {
762         return "Dump Servlet";
763     }
764 
765     /* ------------------------------------------------------------ */
766     @Override
767     public synchronized void destroy()
768     {
769     }
770 
771     /* ------------------------------------------------------------ */
772     private String getURI(HttpServletRequest request)
773     {
774         String uri= (String)request.getAttribute("javax.servlet.forward.request_uri");
775         if (uri == null)
776             uri= request.getRequestURI();
777         return uri;
778     }
779 
780     /* ------------------------------------------------------------ */
781     private static String toString(Object o)
782     {
783         if (o == null)
784             return null;
785 
786         try
787         {
788             if (o.getClass().isArray())
789             {
790                 StringBuffer sb = new StringBuffer();
791                 if (!o.getClass().getComponentType().isPrimitive())
792                 {
793                     Object[] array= (Object[])o;
794                     for (int i= 0; i < array.length; i++)
795                     {
796                         if (i > 0)
797                             sb.append("\n");
798                         sb.append(array.getClass().getComponentType().getName());
799                         sb.append("[");
800                         sb.append(i);
801                         sb.append("]=");
802                         sb.append(toString(array[i]));
803                     }
804                     return sb.toString();
805                 }
806                 else
807                 { 
808                     int length = Array.getLength(o);
809                     for (int i=0;i<length;i++)
810                     {
811                         if (i > 0)
812                             sb.append("\n");
813                         sb.append(o.getClass().getComponentType().getName()); 
814                         sb.append("[");
815                         sb.append(i);
816                         sb.append("]=");
817                         sb.append(toString(Array.get(o, i)));
818                     }
819                     return sb.toString();
820                 }
821             }
822             else
823                 return o.toString();
824         }
825         catch (Exception e)
826         {
827             return e.toString();
828         }
829     }
830 
831     private boolean dump(HttpServletResponse response, String data, String chars, String block, String dribble, boolean flush) throws IOException
832     {
833         if (data != null && data.length() > 0)
834         {
835             long d=Long.parseLong(data);
836             int b=(block!=null&&block.length()>0)?Integer.parseInt(block):50;
837             byte[] buf=new byte[b];
838             for (int i=0;i<b;i++)
839             {
840                 
841                 buf[i]=(byte)('0'+(i%10));
842                 if (i%10==9)
843                     buf[i]=(byte)'\n';
844             }
845             buf[0]='o';
846             OutputStream out=response.getOutputStream();
847             response.setContentType("text/plain");
848             while (d > 0)
849             {
850                 if (b==1)
851                 {
852                     out.write(d%80==0?'\n':'.');
853                     d--;
854                 }
855                 else if (d>=b)
856                 {
857                     out.write(buf);
858                     d=d-b;
859                 }
860                 else
861                 {
862                     out.write(buf,0,(int)d);
863                     d=0;
864                 }
865                 
866                 if (dribble!=null)
867                 {
868                     out.flush();
869                     try
870                     {
871                         Thread.sleep(Long.parseLong(dribble));
872                     }
873                     catch (Exception e)
874                     {
875                         e.printStackTrace();
876                         break;
877                     }
878                 }
879                 
880             }
881             
882             if (flush)
883                 out.flush();
884             
885             return true;
886         }
887 
888         // Handle a dump of data
889         if (chars != null && chars.length() > 0)
890         {
891             long d=Long.parseLong(chars);
892             int b=(block!=null&&block.length()>0)?Integer.parseInt(block):50;
893             char[] buf=new char[b];
894             for (int i=0;i<b;i++)
895             {
896                 buf[i]=(char)('0'+(i%10));
897                 if (i%10==9)
898                     buf[i]='\n';
899             }
900             buf[0]='o';
901             response.setContentType("text/plain");
902             PrintWriter out=response.getWriter();
903             while (d > 0 && !out.checkError())
904             {
905                 if (b==1)
906                 {
907                     out.write(d%80==0?'\n':'.');
908                     d--;
909                 }
910                 else if (d>=b)
911                 {
912                     out.write(buf);
913                     d=d-b;
914                 }
915                 else
916                 {
917                     out.write(buf,0,(int)d);
918                     d=0;
919                 }
920             }
921             return true;
922         }    
923         return false;
924     }
925     
926     private String notag(String s)
927     {
928         if (s==null)
929             return "null";
930         s=StringUtil.replace(s,"&","&amp;");
931         s=StringUtil.replace(s,"<","&lt;");
932         s=StringUtil.replace(s,">","&gt;");
933         return s;
934     }
935 }