View Javadoc

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