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