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