View Javadoc

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