View Javadoc

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