View Javadoc

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