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