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      boolean fixed;
59      
60      /* ------------------------------------------------------------ */
61      @Override
62      public void init(ServletConfig config) throws ServletException
63      {
64      	super.init(config);
65      	
66      	if (config.getInitParameter("unavailable")!=null && !fixed)
67      	{
68      	    
69      	    fixed=true;
70      	    throw new UnavailableException("Unavailable test",Integer.parseInt(config.getInitParameter("unavailable")));
71      	}
72      }
73  
74      /* ------------------------------------------------------------ */
75      @Override
76      public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
77      {
78          doGet(request, response);
79      }
80  
81      /* ------------------------------------------------------------ */
82      @Override
83      public void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException
84      {
85          // Handle a dump of data
86          final String data= request.getParameter("data");
87          final String chars= request.getParameter("chars");
88          final String block= request.getParameter("block");
89          final String dribble= request.getParameter("dribble");
90          final boolean flush= request.getParameter("flush")!=null?Boolean.parseBoolean(request.getParameter("flush")):false;
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\">getUserPrincipal:&nbsp;</th>");
445             pout.write("<td>"+request.getUserPrincipal()+"</td>");
446             pout.write("</tr><tr>\n");
447             pout.write("<th align=\"right\">getRemoteAddr:&nbsp;</th>");
448             pout.write("<td>"+request.getRemoteAddr()+"</td>");
449             pout.write("</tr><tr>\n");
450             pout.write("<th align=\"right\">getRemoteHost:&nbsp;</th>");
451             pout.write("<td>"+request.getRemoteHost()+"</td>");
452             pout.write("</tr><tr>\n");
453             pout.write("<th align=\"right\">getRemotePort:&nbsp;</th>");
454             pout.write("<td>"+request.getRemotePort()+"</td>");
455             pout.write("</tr><tr>\n");
456             pout.write("<th align=\"right\">getRequestedSessionId:&nbsp;</th>");
457             pout.write("<td>"+request.getRequestedSessionId()+"</td>");
458             pout.write("</tr><tr>\n");
459             pout.write("<th align=\"right\">isSecure():&nbsp;</th>");
460             pout.write("<td>"+request.isSecure()+"</td>");
461 
462             pout.write("</tr><tr>\n");
463             pout.write("<th align=\"right\">isUserInRole(admin):&nbsp;</th>");
464             pout.write("<td>"+request.isUserInRole("admin")+"</td>");
465 
466             pout.write("</tr><tr>\n");
467             pout.write("<th align=\"right\">getLocale:&nbsp;</th>");
468             pout.write("<td>"+request.getLocale()+"</td>");
469             
470             Enumeration locales= request.getLocales();
471             while (locales.hasMoreElements())
472             {
473                 pout.write("</tr><tr>\n");
474                 pout.write("<th align=\"right\">getLocales:&nbsp;</th>");
475                 pout.write("<td>"+locales.nextElement()+"</td>");
476             }
477             pout.write("</tr><tr>\n");
478             
479             pout.write("<th align=\"left\" colspan=\"2\"><big><br/>Other HTTP Headers:</big></th>");
480             Enumeration h= request.getHeaderNames();
481             String name;
482             while (h.hasMoreElements())
483             {
484                 name= (String)h.nextElement();
485 
486                 Enumeration h2= request.getHeaders(name);
487                 while (h2.hasMoreElements())
488                 {
489                     String hv= (String)h2.nextElement();
490                     pout.write("</tr><tr>\n");
491                     pout.write("<th align=\"right\">"+notag(name)+":&nbsp;</th>");
492                     pout.write("<td>"+notag(hv)+"</td>");
493                 }
494             }
495 
496             pout.write("</tr><tr>\n");
497             pout.write("<th align=\"left\" colspan=\"2\"><big><br/>Request Parameters:</big></th>");
498             h= request.getParameterNames();
499             while (h.hasMoreElements())
500             {
501                 name= (String)h.nextElement();
502                 pout.write("</tr><tr>\n");
503                 pout.write("<th align=\"right\">"+notag(name)+":&nbsp;</th>");
504                 pout.write("<td>"+notag(request.getParameter(name))+"</td>");
505                 String[] values= request.getParameterValues(name);
506                 if (values == null)
507                 {
508                     pout.write("</tr><tr>\n");
509                     pout.write("<th align=\"right\">"+notag(name)+" Values:&nbsp;</th>");
510                     pout.write("<td>"+"NULL!"+"</td>");
511                 }
512                 else if (values.length > 1)
513                 {
514                     for (int i= 0; i < values.length; i++)
515                     {
516                         pout.write("</tr><tr>\n");
517                         pout.write("<th align=\"right\">"+notag(name)+"["+i+"]:&nbsp;</th>");
518                         pout.write("<td>"+notag(values[i])+"</td>");
519                     }
520                 }
521             }
522 
523             pout.write("</tr><tr>\n");
524             pout.write("<th align=\"left\" colspan=\"2\"><big><br/>Cookies:</big></th>");
525             Cookie[] cookies = request.getCookies();
526             for (int i=0; cookies!=null && i<cookies.length;i++)
527             {
528                 Cookie cookie = cookies[i];
529 
530                 pout.write("</tr><tr>\n");
531                 pout.write("<th align=\"right\">"+notag(cookie.getName())+":&nbsp;</th>");
532                 pout.write("<td>"+notag(cookie.getValue())+"</td>");
533             }
534             
535             String content_type=request.getContentType();
536             if (content_type!=null &&
537                 !content_type.startsWith("application/x-www-form-urlencoded") &&
538                 !content_type.startsWith("multipart/form-data"))
539             {
540                 pout.write("</tr><tr>\n");
541                 pout.write("<th align=\"left\" valign=\"top\" colspan=\"2\"><big><br/>Content:</big></th>");
542                 pout.write("</tr><tr>\n");
543                 pout.write("<td><pre>");
544                 char[] content= new char[4096];
545                 int len;
546                 try{
547                     Reader in=request.getReader();
548                     
549                     while((len=in.read(content))>=0)
550                         pout.write(notag(new String(content,0,len)));
551                 }
552                 catch(IOException e)
553                 {
554                     pout.write(e.toString());
555                 }
556                 
557                 pout.write("</pre></td>");
558             }
559             
560             pout.write("</tr><tr>\n");
561             pout.write("<th align=\"left\" colspan=\"2\"><big><br/>Request Attributes:</big></th>");
562             Enumeration a= request.getAttributeNames();
563             while (a.hasMoreElements())
564             {
565                 name= (String)a.nextElement();
566                 pout.write("</tr><tr>\n");
567                 pout.write("<th align=\"right\" valign=\"top\">"+name.replace("."," .")+":&nbsp;</th>");
568                 Object value=request.getAttribute(name);
569                 if (value instanceof File)
570                 {
571                     File file = (File)value;
572                     pout.write("<td>"+"<pre>" + file.getName()+" ("+file.length()+" "+new Date(file.lastModified())+ ")</pre>"+"</td>");
573                 }
574                 else
575                     pout.write("<td>"+"<pre>" + toString(request.getAttribute(name)) + "</pre>"+"</td>");
576             }
577             request.setAttribute("org.eclipse.jetty.servlet.MultiPartFilter.files",null);
578 
579             
580             pout.write("</tr><tr>\n");
581             pout.write("<th align=\"left\" colspan=\"2\"><big><br/>Servlet InitParameters:</big></th>");
582             a= getInitParameterNames();
583             while (a.hasMoreElements())
584             {
585                 name= (String)a.nextElement();
586                 pout.write("</tr><tr>\n");
587                 pout.write("<th align=\"right\">"+name+":&nbsp;</th>");
588                 pout.write("<td>"+ toString(getInitParameter(name)) +"</td>");
589             }
590 
591             pout.write("</tr><tr>\n");
592             pout.write("<th align=\"left\" colspan=\"2\"><big><br/>Context InitParameters:</big></th>");
593             a= getServletContext().getInitParameterNames();
594             while (a.hasMoreElements())
595             {
596                 name= (String)a.nextElement();
597                 pout.write("</tr><tr>\n");
598                 pout.write("<th align=\"right\" valign=\"top\">"+name.replace("."," .")+":&nbsp;</th>");
599                 pout.write("<td>"+ toString(getServletContext().getInitParameter(name)) + "</td>");
600             }
601 
602             pout.write("</tr><tr>\n");
603             pout.write("<th align=\"left\" colspan=\"2\"><big><br/>Context Attributes:</big></th>");
604             a= getServletContext().getAttributeNames();
605             while (a.hasMoreElements())
606             {
607                 name= (String)a.nextElement();
608                 pout.write("</tr><tr>\n");
609                 pout.write("<th align=\"right\" valign=\"top\">"+name.replace("."," .")+":&nbsp;</th>");
610                 pout.write("<td>"+"<pre>" + toString(getServletContext().getAttribute(name)) + "</pre>"+"</td>");
611             }
612 
613             String res= request.getParameter("resource");
614             if (res != null && res.length() > 0)
615             {
616                 pout.write("</tr><tr>\n");
617                 pout.write("<th align=\"left\" colspan=\"2\"><big><br/>Get Resource: \""+res+"\"</big></th>");
618 
619                 pout.write("</tr><tr>\n");
620                 pout.write("<th align=\"right\">getServletContext().getContext(...):&nbsp;</th>");
621                 
622                 ServletContext context = getServletContext().getContext(res);
623                 pout.write("<td>"+context+"</td>");
624                 
625                 if (context!=null)
626                 {
627                     String cp=context.getContextPath();
628                     if (cp==null || "/".equals(cp))
629                         cp="";
630                     pout.write("</tr><tr>\n");
631                     pout.write("<th align=\"right\">getServletContext().getContext(...),getRequestDispatcher(...):&nbsp;</th>");
632                     pout.write("<td>"+getServletContext().getContext(res).getRequestDispatcher(res.substring(cp.length()))+"</td>");
633                 }
634 
635                 pout.write("</tr><tr>\n");
636                 pout.write("<th align=\"right\">this.getClass().getResource(...):&nbsp;</th>");
637                 pout.write("<td>"+this.getClass().getResource(res)+"</td>");
638 
639                 pout.write("</tr><tr>\n");
640                 pout.write("<th align=\"right\">this.getClass().getClassLoader().getResource(...):&nbsp;</th>");
641                 pout.write("<td>"+this.getClass().getClassLoader().getResource(res)+"</td>");
642 
643                 pout.write("</tr><tr>\n");
644                 pout.write("<th align=\"right\">Thread.currentThread().getContextClassLoader().getResource(...):&nbsp;</th>");
645                 pout.write("<td>"+Thread.currentThread().getContextClassLoader().getResource(res)+"</td>");
646 
647                 pout.write("</tr><tr>\n");
648                 pout.write("<th align=\"right\">getServletContext().getResource(...):&nbsp;</th>");
649                 try{pout.write("<td>"+getServletContext().getResource(res)+"</td>");}
650                 catch(Exception e) {pout.write("<td>"+"" +e+"</td>");}
651             }
652             
653             pout.write("</tr></table>\n");
654 
655             /* ------------------------------------------------------------ */
656             pout.write("<h2>Request Wrappers</h2>\n");
657             ServletRequest rw=request;
658             int w=0;
659             while (rw !=null)
660             {
661                 pout.write((w++)+": "+rw.getClass().getName()+"<br/>");
662                 if (rw instanceof HttpServletRequestWrapper)
663                     rw=((HttpServletRequestWrapper)rw).getRequest();
664                 else if (rw instanceof ServletRequestWrapper)
665                     rw=((ServletRequestWrapper)rw).getRequest();
666                 else
667                     rw=null;
668             }
669 
670             /* ------------------------------------------------------------ */
671             pout.write("<h2>Response Wrappers</h2>\n");
672             ServletResponse rsw=response;
673             w=0;
674             while (rsw !=null)
675             {
676                 pout.write((w++)+": "+rsw.getClass().getName()+"<br/>");
677                 if (rsw instanceof HttpServletResponseWrapper)
678                     rsw=((HttpServletResponseWrapper)rsw).getResponse();
679                 else if (rsw instanceof ServletResponseWrapper)
680                     rsw=((ServletResponseWrapper)rsw).getResponse();
681                 else
682                     rsw=null;
683             }
684             
685             pout.write("<br/>");
686             pout.write("<h2>International Characters (UTF-8)</h2>");
687             pout.write("LATIN LETTER SMALL CAPITAL AE<br/>\n");
688             pout.write("Directly uni encoded(\\u1d01): \u1d01<br/>");
689             pout.write("HTML reference (&amp;AElig;): &AElig;<br/>");
690             pout.write("Decimal (&amp;#7425;): &#7425;<br/>");
691             pout.write("Javascript unicode (\\u1d01) : <script language='javascript'>document.write(\"\u1d01\");</script><br/>");
692             pout.write("<br/>");
693             pout.write("<h2>Form to generate GET content</h2>");
694             pout.write("<form method=\"GET\" action=\""+response.encodeURL(getURI(request))+"\">");
695             pout.write("TextField: <input type=\"text\" name=\"TextField\" value=\"value\"/><br/>\n");
696             pout.write("<input type=\"submit\" name=\"Action\" value=\"Submit\">");
697             pout.write("</form>");
698 
699             pout.write("<br/>");
700             
701             pout.write("<h2>Form to generate POST content</h2>");
702             pout.write("<form method=\"POST\" accept-charset=\"utf-8\" action=\""+response.encodeURL(getURI(request))+"\">");
703             pout.write("TextField: <input type=\"text\" name=\"TextField\" value=\"value\"/><br/>\n");
704             pout.write("Select: <select multiple name=\"Select\">\n");
705             pout.write("<option>ValueA</option>");
706             pout.write("<option>ValueB1,ValueB2</option>");
707             pout.write("<option>ValueC</option>");
708             pout.write("</select><br/>");
709             pout.write("<input type=\"submit\" name=\"Action\" value=\"Submit\"><br/>");
710             pout.write("</form>");
711             pout.write("<br/>");
712             
713             pout.write("<h2>Form to generate UPLOAD content</h2>");
714             pout.write("<form method=\"POST\" enctype=\"multipart/form-data\" accept-charset=\"utf-8\" action=\""+
715                     response.encodeURL(getURI(request))+(request.getQueryString()==null?"":("?"+request.getQueryString()))+
716                     "\">");
717             pout.write("TextField: <input type=\"text\" name=\"TextField\" value=\"comment\"/><br/>\n");
718             pout.write("File 1: <input type=\"file\" name=\"file1\" /><br/>\n");
719             pout.write("File 2: <input type=\"file\" name=\"file2\" /><br/>\n");
720             pout.write("<input type=\"submit\" name=\"Action\" value=\"Submit\"><br/>");
721             pout.write("</form>");
722 
723             pout.write("<h2>Form to set Cookie</h2>");
724             pout.write("<form method=\"POST\" action=\""+response.encodeURL(getURI(request))+"\">");
725             pout.write("cookie: <input type=\"text\" name=\"cookie\" /><br/>\n");
726             pout.write("value: <input type=\"text\" name=\"cookiev\" /><br/>\n");
727             pout.write("<input type=\"submit\" name=\"Action\" value=\"setCookie\">");
728             pout.write("</form>\n");
729             
730             pout.write("<h2>Form to get Resource</h2>");
731             pout.write("<form method=\"POST\" action=\""+response.encodeURL(getURI(request))+"\">");
732             pout.write("resource: <input type=\"text\" name=\"resource\" /><br/>\n");
733             pout.write("<input type=\"submit\" name=\"Action\" value=\"getResource\">");
734             pout.write("</form>\n");
735         }
736         catch (Exception e)
737         {
738             getServletContext().log("dump", e);
739         }
740         
741         String lines= request.getParameter("lines");
742         if (lines!=null)
743         {
744             char[] line = "<span>A line of characters. Blah blah blah blah.  blooble blooble</span></br>\n".toCharArray();
745             for (int l=Integer.parseInt(lines);l-->0;)
746             {
747                 pout.write("<span>"+l+" </span>");
748                 pout.write(line);
749             }
750         }
751         
752         pout.write("</body>\n</html>\n");
753         
754         pout.close();
755 
756         if (pi != null)
757         {
758             if ("/ex4".equals(pi))
759                 throw new ServletException("test ex4", new Throwable());
760             if ("/ex5".equals(pi))
761                 throw new IOException("test ex5");
762             if ("/ex6".equals(pi))
763                 throw new UnavailableException("test ex6");
764         }
765 
766 
767     }
768 
769 
770     /* ------------------------------------------------------------ */
771     @Override
772     public String getServletInfo()
773     {
774         return "Dump Servlet";
775     }
776 
777     /* ------------------------------------------------------------ */
778     @Override
779     public synchronized void destroy()
780     {
781     }
782 
783     /* ------------------------------------------------------------ */
784     private String getURI(HttpServletRequest request)
785     {
786         String uri= (String)request.getAttribute("javax.servlet.forward.request_uri");
787         if (uri == null)
788             uri= request.getRequestURI();
789         return uri;
790     }
791 
792     /* ------------------------------------------------------------ */
793     private static String toString(Object o)
794     {
795         if (o == null)
796             return null;
797 
798         try
799         {
800             if (o.getClass().isArray())
801             {
802                 StringBuffer sb = new StringBuffer();
803                 if (!o.getClass().getComponentType().isPrimitive())
804                 {
805                     Object[] array= (Object[])o;
806                     for (int i= 0; i < array.length; i++)
807                     {
808                         if (i > 0)
809                             sb.append("\n");
810                         sb.append(array.getClass().getComponentType().getName());
811                         sb.append("[");
812                         sb.append(i);
813                         sb.append("]=");
814                         sb.append(toString(array[i]));
815                     }
816                     return sb.toString();
817                 }
818                 else
819                 { 
820                     int length = Array.getLength(o);
821                     for (int i=0;i<length;i++)
822                     {
823                         if (i > 0)
824                             sb.append("\n");
825                         sb.append(o.getClass().getComponentType().getName()); 
826                         sb.append("[");
827                         sb.append(i);
828                         sb.append("]=");
829                         sb.append(toString(Array.get(o, i)));
830                     }
831                     return sb.toString();
832                 }
833             }
834             else
835                 return o.toString();
836         }
837         catch (Exception e)
838         {
839             return e.toString();
840         }
841     }
842 
843     private boolean dump(HttpServletResponse response, String data, String chars, String block, String dribble, boolean flush) throws IOException
844     {
845         if (data != null && data.length() > 0)
846         {
847             long d=Long.parseLong(data);
848             int b=(block!=null&&block.length()>0)?Integer.parseInt(block):50;
849             byte[] buf=new byte[b];
850             for (int i=0;i<b;i++)
851             {
852                 
853                 buf[i]=(byte)('0'+(i%10));
854                 if (i%10==9)
855                     buf[i]=(byte)'\n';
856             }
857             buf[0]='o';
858             OutputStream out=response.getOutputStream();
859             response.setContentType("text/plain");
860             while (d > 0)
861             {
862                 if (b==1)
863                 {
864                     out.write(d%80==0?'\n':'.');
865                     d--;
866                 }
867                 else if (d>=b)
868                 {
869                     out.write(buf);
870                     d=d-b;
871                 }
872                 else
873                 {
874                     out.write(buf,0,(int)d);
875                     d=0;
876                 }
877                 
878                 if (dribble!=null)
879                 {
880                     out.flush();
881                     try
882                     {
883                         Thread.sleep(Long.parseLong(dribble));
884                     }
885                     catch (Exception e)
886                     {
887                         e.printStackTrace();
888                         break;
889                     }
890                 }
891                 
892             }
893             
894             if (flush)
895                 out.flush();
896             
897             return true;
898         }
899 
900         // Handle a dump of data
901         if (chars != null && chars.length() > 0)
902         {
903             long d=Long.parseLong(chars);
904             int b=(block!=null&&block.length()>0)?Integer.parseInt(block):50;
905             char[] buf=new char[b];
906             for (int i=0;i<b;i++)
907             {
908                 buf[i]=(char)('0'+(i%10));
909                 if (i%10==9)
910                     buf[i]='\n';
911             }
912             buf[0]='o';
913             response.setContentType("text/plain");
914             PrintWriter out=response.getWriter();
915             while (d > 0 && !out.checkError())
916             {
917                 if (b==1)
918                 {
919                     out.write(d%80==0?'\n':'.');
920                     d--;
921                 }
922                 else if (d>=b)
923                 {
924                     out.write(buf);
925                     d=d-b;
926                 }
927                 else
928                 {
929                     out.write(buf,0,(int)d);
930                     d=0;
931                 }
932             }
933             return true;
934         }    
935         return false;
936     }
937     
938     private String notag(String s)
939     {
940         if (s==null)
941             return "null";
942         s=StringUtil.replace(s,"&","&amp;");
943         s=StringUtil.replace(s,"<","&lt;");
944         s=StringUtil.replace(s,">","&gt;");
945         return s;
946     }
947 }