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