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                             if (!dump(response,data,chars,block,dribble,flush))
204                             {
205                                 response.setContentType("text/plain");
206                                 response.getOutputStream().println("EXPIRED");
207                             }
208                             continuation.complete();
209                         }
210                         catch (IOException e)
211                         {
212                             Log.ignore(e);
213                         }
214                     }
215                     
216                     public void onComplete(Continuation continuation)
217                     {
218                         response.addHeader("Dump","onComplete");
219                     }
220                 });
221                 
222                 continuation.undispatch();
223             }
224             catch(Exception e)
225             {
226                 throw new ServletException(e);
227             }
228         }        
229             
230         request.setAttribute("Dump", this);
231         getServletContext().setAttribute("Dump",this);
232         // getServletContext().log("dump "+request.getRequestURI());
233 
234         // Force a content length response
235         String length= request.getParameter("length");
236         if (length != null && length.length() > 0)
237         {
238             response.setContentLength(Integer.parseInt(length));
239         }
240 
241         // Handle a dump of data
242         if (dump(response,data,chars,block,dribble,flush))
243             return;
244         
245         // handle an exception
246         String info= request.getPathInfo();
247         if (info != null && info.endsWith("Exception"))
248         {
249             try
250             {
251                 throw (Throwable) Thread.currentThread().getContextClassLoader().loadClass(info.substring(1)).newInstance();
252             }
253             catch (Throwable th)
254             {
255                 throw new ServletException(th);
256             }
257         }
258 
259         // test a reset
260         String reset= request.getParameter("reset");
261         if (reset != null && reset.length() > 0)
262         {
263             response.getOutputStream().println("THIS SHOULD NOT BE SEEN!");
264             response.setHeader("SHOULD_NOT","BE SEEN");
265             response.reset();
266         }
267         
268         
269         // handle an redirect
270         String redirect= request.getParameter("redirect");
271         if (redirect != null && redirect.length() > 0)
272         {
273             response.getOutputStream().println("THIS SHOULD NOT BE SEEN!");
274             response.sendRedirect(response.encodeRedirectURL(redirect));
275             try
276             {
277                 response.getOutputStream().println("THIS SHOULD NOT BE SEEN!");
278             }
279             catch(IOException e)
280             {
281                 // ignored as stream is closed.
282             }
283             return;
284         }
285 
286         // handle an error
287         String error= request.getParameter("error");
288         if (error != null && error.length() > 0 && request.getAttribute("javax.servlet.error.status_code")==null)
289         {
290             response.getOutputStream().println("THIS SHOULD NOT BE SEEN!");
291             response.sendError(Integer.parseInt(error));
292             try
293             {
294                 response.getOutputStream().println("THIS SHOULD NOT BE SEEN!");
295             }
296             catch(IllegalStateException e)
297             {
298                 try
299                 {
300                     response.getWriter().println("NOR THIS!!"); 
301                 }
302                 catch(IOException e2){}
303             }
304             catch(IOException e){}
305             return;
306         }
307 
308         // Handle a extra headers 
309         String headers= request.getParameter("headers");
310         if (headers != null && headers.length() > 0)
311         {
312             long h=Long.parseLong(headers);
313             for (int i=0;i<h;i++)
314                 response.addHeader("Header"+i,"Value"+i);
315         }
316 
317         String buffer= request.getParameter("buffer");
318         if (buffer != null && buffer.length() > 0)
319             response.setBufferSize(Integer.parseInt(buffer));
320 
321         String charset= request.getParameter("charset");
322         if (charset==null)
323             charset="UTF-8";
324         response.setCharacterEncoding(charset);
325         response.setContentType("text/html");
326 
327         if (info != null && info.indexOf("Locale/") >= 0)
328         {
329             try
330             {
331                 String locale_name= info.substring(info.indexOf("Locale/") + 7);
332                 Field f= java.util.Locale.class.getField(locale_name);
333                 response.setLocale((Locale)f.get(null));
334             }
335             catch (Exception e)
336             {
337                 e.printStackTrace();
338                 response.setLocale(Locale.getDefault());
339             }
340         }
341 
342         String cn= request.getParameter("cookie");
343         String cv=request.getParameter("cookiev");
344         if (cn!=null && cv!=null)
345         {
346             Cookie cookie= new Cookie(cn, cv);
347             if (request.getParameter("version")!=null)
348                 cookie.setVersion(Integer.parseInt(request.getParameter("version")));
349             cookie.setComment("Cookie from dump servlet");
350             response.addCookie(cookie);
351         }
352 
353         String pi= request.getPathInfo();
354         if (pi != null && pi.startsWith("/ex"))
355         {
356             OutputStream out= response.getOutputStream();
357             out.write("</H1>This text should be reset</H1>".getBytes());
358             if ("/ex0".equals(pi))
359                 throw new ServletException("test ex0", new Throwable());
360             else if ("/ex1".equals(pi))
361                 throw new IOException("test ex1");
362             else if ("/ex2".equals(pi))
363                 throw new UnavailableException("test ex2");
364             else if (pi.startsWith("/ex3/"))
365                 throw new UnavailableException("test ex3",Integer.parseInt(pi.substring(5)));
366             throw new RuntimeException("test");
367         }
368 
369         if ("true".equals(request.getParameter("close")))
370             response.setHeader("Connection","close");
371 
372         String buffered= request.getParameter("buffered");
373         
374         PrintWriter pout=null;
375         
376         try
377         {
378             pout =response.getWriter();
379         }
380         catch(IllegalStateException e)
381         {
382             pout=new PrintWriter(new OutputStreamWriter(response.getOutputStream(),charset));
383         }
384         if (buffered!=null)
385             pout = new PrintWriter(new BufferedWriter(pout,Integer.parseInt(buffered)));
386         
387         try
388         {
389             pout.write("<html>\n<body>\n");
390             pout.write("<h1>Dump Servlet</h1>\n");
391             pout.write("<table width=\"95%\">");
392             pout.write("<tr>\n");
393             pout.write("<th align=\"right\">getMethod:&nbsp;</th>");
394             pout.write("<td>" + notag(request.getMethod())+"</td>");
395             pout.write("</tr><tr>\n");
396             pout.write("<th align=\"right\">getContentLength:&nbsp;</th>");
397             pout.write("<td>"+Integer.toString(request.getContentLength())+"</td>");
398             pout.write("</tr><tr>\n");
399             pout.write("<th align=\"right\">getContentType:&nbsp;</th>");
400             pout.write("<td>"+notag(request.getContentType())+"</td>");
401             pout.write("</tr><tr>\n");
402             pout.write("<th align=\"right\">getRequestURI:&nbsp;</th>");
403             pout.write("<td>"+notag(request.getRequestURI())+"</td>");
404             pout.write("</tr><tr>\n");
405             pout.write("<th align=\"right\">getRequestURL:&nbsp;</th>");
406             pout.write("<td>"+notag(request.getRequestURL().toString())+"</td>");
407             pout.write("</tr><tr>\n");
408             pout.write("<th align=\"right\">getContextPath:&nbsp;</th>");
409             pout.write("<td>"+request.getContextPath()+"</td>");
410             pout.write("</tr><tr>\n");
411             pout.write("<th align=\"right\">getServletPath:&nbsp;</th>");
412             pout.write("<td>"+notag(request.getServletPath())+"</td>");
413             pout.write("</tr><tr>\n");
414             pout.write("<th align=\"right\">getPathInfo:&nbsp;</th>");
415             pout.write("<td>"+notag(request.getPathInfo())+"</td>");
416             pout.write("</tr><tr>\n");
417             pout.write("<th align=\"right\">getPathTranslated:&nbsp;</th>");
418             pout.write("<td>"+notag(request.getPathTranslated())+"</td>");
419             pout.write("</tr><tr>\n");
420             pout.write("<th align=\"right\">getQueryString:&nbsp;</th>");
421             pout.write("<td>"+notag(request.getQueryString())+"</td>");
422             pout.write("</tr><tr>\n");
423             
424             pout.write("<th align=\"right\">getProtocol:&nbsp;</th>");
425             pout.write("<td>"+request.getProtocol()+"</td>");
426             pout.write("</tr><tr>\n");
427             pout.write("<th align=\"right\">getScheme:&nbsp;</th>");
428             pout.write("<td>"+request.getScheme()+"</td>");
429             pout.write("</tr><tr>\n");
430             pout.write("<th align=\"right\">getServerName:&nbsp;</th>");
431             pout.write("<td>"+notag(request.getServerName())+"</td>");
432             pout.write("</tr><tr>\n");
433             pout.write("<th align=\"right\">getServerPort:&nbsp;</th>");
434             pout.write("<td>"+Integer.toString(request.getServerPort())+"</td>");
435             pout.write("</tr><tr>\n");
436             pout.write("<th align=\"right\">getLocalName:&nbsp;</th>");
437             pout.write("<td>"+request.getLocalName()+"</td>");
438             pout.write("</tr><tr>\n");
439             pout.write("<th align=\"right\">getLocalAddr:&nbsp;</th>");
440             pout.write("<td>"+request.getLocalAddr()+"</td>");
441             pout.write("</tr><tr>\n");
442             pout.write("<th align=\"right\">getLocalPort:&nbsp;</th>");
443             pout.write("<td>"+Integer.toString(request.getLocalPort())+"</td>");
444             pout.write("</tr><tr>\n");
445             pout.write("<th align=\"right\">getRemoteUser:&nbsp;</th>");
446             pout.write("<td>"+request.getRemoteUser()+"</td>");
447             pout.write("</tr><tr>\n");
448             pout.write("<th align=\"right\">getUserPrincipal:&nbsp;</th>");
449             pout.write("<td>"+request.getUserPrincipal()+"</td>");
450             pout.write("</tr><tr>\n");
451             pout.write("<th align=\"right\">getRemoteAddr:&nbsp;</th>");
452             pout.write("<td>"+request.getRemoteAddr()+"</td>");
453             pout.write("</tr><tr>\n");
454             pout.write("<th align=\"right\">getRemoteHost:&nbsp;</th>");
455             pout.write("<td>"+request.getRemoteHost()+"</td>");
456             pout.write("</tr><tr>\n");
457             pout.write("<th align=\"right\">getRemotePort:&nbsp;</th>");
458             pout.write("<td>"+request.getRemotePort()+"</td>");
459             pout.write("</tr><tr>\n");
460             pout.write("<th align=\"right\">getRequestedSessionId:&nbsp;</th>");
461             pout.write("<td>"+request.getRequestedSessionId()+"</td>");
462             pout.write("</tr><tr>\n");
463             pout.write("<th align=\"right\">isSecure():&nbsp;</th>");
464             pout.write("<td>"+request.isSecure()+"</td>");
465 
466             pout.write("</tr><tr>\n");
467             pout.write("<th align=\"right\">isUserInRole(admin):&nbsp;</th>");
468             pout.write("<td>"+request.isUserInRole("admin")+"</td>");
469 
470             pout.write("</tr><tr>\n");
471             pout.write("<th align=\"right\">getLocale:&nbsp;</th>");
472             pout.write("<td>"+request.getLocale()+"</td>");
473             
474             Enumeration locales= request.getLocales();
475             while (locales.hasMoreElements())
476             {
477                 pout.write("</tr><tr>\n");
478                 pout.write("<th align=\"right\">getLocales:&nbsp;</th>");
479                 pout.write("<td>"+locales.nextElement()+"</td>");
480             }
481             pout.write("</tr><tr>\n");
482             
483             pout.write("<th align=\"left\" colspan=\"2\"><big><br/>Other HTTP Headers:</big></th>");
484             Enumeration h= request.getHeaderNames();
485             String name;
486             while (h.hasMoreElements())
487             {
488                 name= (String)h.nextElement();
489 
490                 Enumeration h2= request.getHeaders(name);
491                 while (h2.hasMoreElements())
492                 {
493                     String hv= (String)h2.nextElement();
494                     pout.write("</tr><tr>\n");
495                     pout.write("<th align=\"right\">"+notag(name)+":&nbsp;</th>");
496                     pout.write("<td>"+notag(hv)+"</td>");
497                 }
498             }
499 
500             pout.write("</tr><tr>\n");
501             pout.write("<th align=\"left\" colspan=\"2\"><big><br/>Request Parameters:</big></th>");
502             h= request.getParameterNames();
503             while (h.hasMoreElements())
504             {
505                 name= (String)h.nextElement();
506                 pout.write("</tr><tr>\n");
507                 pout.write("<th align=\"right\">"+notag(name)+":&nbsp;</th>");
508                 pout.write("<td>"+notag(request.getParameter(name))+"</td>");
509                 String[] values= request.getParameterValues(name);
510                 if (values == null)
511                 {
512                     pout.write("</tr><tr>\n");
513                     pout.write("<th align=\"right\">"+notag(name)+" Values:&nbsp;</th>");
514                     pout.write("<td>"+"NULL!"+"</td>");
515                 }
516                 else if (values.length > 1)
517                 {
518                     for (int i= 0; i < values.length; i++)
519                     {
520                         pout.write("</tr><tr>\n");
521                         pout.write("<th align=\"right\">"+notag(name)+"["+i+"]:&nbsp;</th>");
522                         pout.write("<td>"+notag(values[i])+"</td>");
523                     }
524                 }
525             }
526 
527             pout.write("</tr><tr>\n");
528             pout.write("<th align=\"left\" colspan=\"2\"><big><br/>Cookies:</big></th>");
529             Cookie[] cookies = request.getCookies();
530             for (int i=0; cookies!=null && i<cookies.length;i++)
531             {
532                 Cookie cookie = cookies[i];
533 
534                 pout.write("</tr><tr>\n");
535                 pout.write("<th align=\"right\">"+notag(cookie.getName())+":&nbsp;</th>");
536                 pout.write("<td>"+notag(cookie.getValue())+"</td>");
537             }
538             
539             String content_type=request.getContentType();
540             if (content_type!=null &&
541                 !content_type.startsWith("application/x-www-form-urlencoded") &&
542                 !content_type.startsWith("multipart/form-data"))
543             {
544                 pout.write("</tr><tr>\n");
545                 pout.write("<th align=\"left\" valign=\"top\" colspan=\"2\"><big><br/>Content:</big></th>");
546                 pout.write("</tr><tr>\n");
547                 pout.write("<td><pre>");
548                 char[] content= new char[4096];
549                 int len;
550                 try{
551                     Reader in=request.getReader();
552                     
553                     while((len=in.read(content))>=0)
554                         pout.write(notag(new String(content,0,len)));
555                 }
556                 catch(IOException e)
557                 {
558                     pout.write(e.toString());
559                 }
560                 
561                 pout.write("</pre></td>");
562             }
563             
564             pout.write("</tr><tr>\n");
565             pout.write("<th align=\"left\" colspan=\"2\"><big><br/>Request Attributes:</big></th>");
566             Enumeration a= request.getAttributeNames();
567             while (a.hasMoreElements())
568             {
569                 name= (String)a.nextElement();
570                 pout.write("</tr><tr>\n");
571                 pout.write("<th align=\"right\" valign=\"top\">"+name.replace("."," .")+":&nbsp;</th>");
572                 Object value=request.getAttribute(name);
573                 if (value instanceof File)
574                 {
575                     File file = (File)value;
576                     pout.write("<td>"+"<pre>" + file.getName()+" ("+file.length()+" "+new Date(file.lastModified())+ ")</pre>"+"</td>");
577                 }
578                 else
579                     pout.write("<td>"+"<pre>" + toString(request.getAttribute(name)) + "</pre>"+"</td>");
580             }
581             request.setAttribute("org.eclipse.jetty.servlet.MultiPartFilter.files",null);
582 
583             
584             pout.write("</tr><tr>\n");
585             pout.write("<th align=\"left\" colspan=\"2\"><big><br/>Servlet InitParameters:</big></th>");
586             a= getInitParameterNames();
587             while (a.hasMoreElements())
588             {
589                 name= (String)a.nextElement();
590                 pout.write("</tr><tr>\n");
591                 pout.write("<th align=\"right\">"+name+":&nbsp;</th>");
592                 pout.write("<td>"+ toString(getInitParameter(name)) +"</td>");
593             }
594 
595             pout.write("</tr><tr>\n");
596             pout.write("<th align=\"left\" colspan=\"2\"><big><br/>Context InitParameters:</big></th>");
597             a= getServletContext().getInitParameterNames();
598             while (a.hasMoreElements())
599             {
600                 name= (String)a.nextElement();
601                 pout.write("</tr><tr>\n");
602                 pout.write("<th align=\"right\" valign=\"top\">"+name.replace("."," .")+":&nbsp;</th>");
603                 pout.write("<td>"+ toString(getServletContext().getInitParameter(name)) + "</td>");
604             }
605 
606             pout.write("</tr><tr>\n");
607             pout.write("<th align=\"left\" colspan=\"2\"><big><br/>Context Attributes:</big></th>");
608             a= getServletContext().getAttributeNames();
609             while (a.hasMoreElements())
610             {
611                 name= (String)a.nextElement();
612                 pout.write("</tr><tr>\n");
613                 pout.write("<th align=\"right\" valign=\"top\">"+name.replace("."," .")+":&nbsp;</th>");
614                 pout.write("<td>"+"<pre>" + toString(getServletContext().getAttribute(name)) + "</pre>"+"</td>");
615             }
616 
617             String res= request.getParameter("resource");
618             if (res != null && res.length() > 0)
619             {
620                 pout.write("</tr><tr>\n");
621                 pout.write("<th align=\"left\" colspan=\"2\"><big><br/>Get Resource: \""+res+"\"</big></th>");
622 
623                 pout.write("</tr><tr>\n");
624                 pout.write("<th align=\"right\">getServletContext().getContext(...):&nbsp;</th>");
625                 
626                 ServletContext context = getServletContext().getContext(res);
627                 pout.write("<td>"+context+"</td>");
628                 
629                 if (context!=null)
630                 {
631                     String cp=context.getContextPath();
632                     if (cp==null || "/".equals(cp))
633                         cp="";
634                     pout.write("</tr><tr>\n");
635                     pout.write("<th align=\"right\">getServletContext().getContext(...),getRequestDispatcher(...):&nbsp;</th>");
636                     pout.write("<td>"+getServletContext().getContext(res).getRequestDispatcher(res.substring(cp.length()))+"</td>");
637                 }
638 
639                 pout.write("</tr><tr>\n");
640                 pout.write("<th align=\"right\">this.getClass().getResource(...):&nbsp;</th>");
641                 pout.write("<td>"+this.getClass().getResource(res)+"</td>");
642 
643                 pout.write("</tr><tr>\n");
644                 pout.write("<th align=\"right\">this.getClass().getClassLoader().getResource(...):&nbsp;</th>");
645                 pout.write("<td>"+this.getClass().getClassLoader().getResource(res)+"</td>");
646 
647                 pout.write("</tr><tr>\n");
648                 pout.write("<th align=\"right\">Thread.currentThread().getContextClassLoader().getResource(...):&nbsp;</th>");
649                 pout.write("<td>"+Thread.currentThread().getContextClassLoader().getResource(res)+"</td>");
650 
651                 pout.write("</tr><tr>\n");
652                 pout.write("<th align=\"right\">getServletContext().getResource(...):&nbsp;</th>");
653                 try{pout.write("<td>"+getServletContext().getResource(res)+"</td>");}
654                 catch(Exception e) {pout.write("<td>"+"" +e+"</td>");}
655             }
656             
657             pout.write("</tr></table>\n");
658 
659             /* ------------------------------------------------------------ */
660             pout.write("<h2>Request Wrappers</h2>\n");
661             ServletRequest rw=request;
662             int w=0;
663             while (rw !=null)
664             {
665                 pout.write((w++)+": "+rw.getClass().getName()+"<br/>");
666                 if (rw instanceof HttpServletRequestWrapper)
667                     rw=((HttpServletRequestWrapper)rw).getRequest();
668                 else if (rw instanceof ServletRequestWrapper)
669                     rw=((ServletRequestWrapper)rw).getRequest();
670                 else
671                     rw=null;
672             }
673 
674             /* ------------------------------------------------------------ */
675             pout.write("<h2>Response Wrappers</h2>\n");
676             ServletResponse rsw=response;
677             w=0;
678             while (rsw !=null)
679             {
680                 pout.write((w++)+": "+rsw.getClass().getName()+"<br/>");
681                 if (rsw instanceof HttpServletResponseWrapper)
682                     rsw=((HttpServletResponseWrapper)rsw).getResponse();
683                 else if (rsw instanceof ServletResponseWrapper)
684                     rsw=((ServletResponseWrapper)rsw).getResponse();
685                 else
686                     rsw=null;
687             }
688             
689             pout.write("<br/>");
690             pout.write("<h2>International Characters (UTF-8)</h2>");
691             pout.write("LATIN LETTER SMALL CAPITAL AE<br/>\n");
692             pout.write("Directly uni encoded(\\u1d01): \u1d01<br/>");
693             pout.write("HTML reference (&amp;AElig;): &AElig;<br/>");
694             pout.write("Decimal (&amp;#7425;): &#7425;<br/>");
695             pout.write("Javascript unicode (\\u1d01) : <script language='javascript'>document.write(\"\u1d01\");</script><br/>");
696             pout.write("<br/>");
697             pout.write("<h2>Form to generate GET content</h2>");
698             pout.write("<form method=\"GET\" action=\""+response.encodeURL(getURI(request))+"\">");
699             pout.write("TextField: <input type=\"text\" name=\"TextField\" value=\"value\"/><br/>\n");
700             pout.write("<input type=\"submit\" name=\"Action\" value=\"Submit\">");
701             pout.write("</form>");
702 
703             pout.write("<br/>");
704             
705             pout.write("<h2>Form to generate POST content</h2>");
706             pout.write("<form method=\"POST\" accept-charset=\"utf-8\" action=\""+response.encodeURL(getURI(request))+"\">");
707             pout.write("TextField: <input type=\"text\" name=\"TextField\" value=\"value\"/><br/>\n");
708             pout.write("Select: <select multiple name=\"Select\">\n");
709             pout.write("<option>ValueA</option>");
710             pout.write("<option>ValueB1,ValueB2</option>");
711             pout.write("<option>ValueC</option>");
712             pout.write("</select><br/>");
713             pout.write("<input type=\"submit\" name=\"Action\" value=\"Submit\"><br/>");
714             pout.write("</form>");
715             pout.write("<br/>");
716             
717             pout.write("<h2>Form to generate UPLOAD content</h2>");
718             pout.write("<form method=\"POST\" enctype=\"multipart/form-data\" accept-charset=\"utf-8\" action=\""+
719                     response.encodeURL(getURI(request))+(request.getQueryString()==null?"":("?"+request.getQueryString()))+
720                     "\">");
721             pout.write("TextField: <input type=\"text\" name=\"TextField\" value=\"comment\"/><br/>\n");
722             pout.write("File 1: <input type=\"file\" name=\"file1\" /><br/>\n");
723             pout.write("File 2: <input type=\"file\" name=\"file2\" /><br/>\n");
724             pout.write("<input type=\"submit\" name=\"Action\" value=\"Submit\"><br/>");
725             pout.write("</form>");
726 
727             pout.write("<h2>Form to set Cookie</h2>");
728             pout.write("<form method=\"POST\" action=\""+response.encodeURL(getURI(request))+"\">");
729             pout.write("cookie: <input type=\"text\" name=\"cookie\" /><br/>\n");
730             pout.write("value: <input type=\"text\" name=\"cookiev\" /><br/>\n");
731             pout.write("<input type=\"submit\" name=\"Action\" value=\"setCookie\">");
732             pout.write("</form>\n");
733             
734             pout.write("<h2>Form to get Resource</h2>");
735             pout.write("<form method=\"POST\" action=\""+response.encodeURL(getURI(request))+"\">");
736             pout.write("resource: <input type=\"text\" name=\"resource\" /><br/>\n");
737             pout.write("<input type=\"submit\" name=\"Action\" value=\"getResource\">");
738             pout.write("</form>\n");
739         }
740         catch (Exception e)
741         {
742             getServletContext().log("dump", e);
743         }
744         
745         String lines= request.getParameter("lines");
746         if (lines!=null)
747         {
748             char[] line = "<span>A line of characters. Blah blah blah blah.  blooble blooble</span></br>\n".toCharArray();
749             for (int l=Integer.parseInt(lines);l-->0;)
750             {
751                 pout.write("<span>"+l+" </span>");
752                 pout.write(line);
753             }
754         }
755         
756         pout.write("</body>\n</html>\n");
757         
758         pout.close();
759 
760         if (pi != null)
761         {
762             if ("/ex4".equals(pi))
763                 throw new ServletException("test ex4", new Throwable());
764             if ("/ex5".equals(pi))
765                 throw new IOException("test ex5");
766             if ("/ex6".equals(pi))
767                 throw new UnavailableException("test ex6");
768         }
769 
770 
771     }
772 
773 
774     /* ------------------------------------------------------------ */
775     @Override
776     public String getServletInfo()
777     {
778         return "Dump Servlet";
779     }
780 
781     /* ------------------------------------------------------------ */
782     @Override
783     public synchronized void destroy()
784     {
785     }
786 
787     /* ------------------------------------------------------------ */
788     private String getURI(HttpServletRequest request)
789     {
790         String uri= (String)request.getAttribute("javax.servlet.forward.request_uri");
791         if (uri == null)
792             uri= request.getRequestURI();
793         return uri;
794     }
795 
796     /* ------------------------------------------------------------ */
797     private static String toString(Object o)
798     {
799         if (o == null)
800             return null;
801 
802         try
803         {
804             if (o.getClass().isArray())
805             {
806                 StringBuffer sb = new StringBuffer();
807                 if (!o.getClass().getComponentType().isPrimitive())
808                 {
809                     Object[] array= (Object[])o;
810                     for (int i= 0; i < array.length; i++)
811                     {
812                         if (i > 0)
813                             sb.append("\n");
814                         sb.append(array.getClass().getComponentType().getName());
815                         sb.append("[");
816                         sb.append(i);
817                         sb.append("]=");
818                         sb.append(toString(array[i]));
819                     }
820                     return sb.toString();
821                 }
822                 else
823                 { 
824                     int length = Array.getLength(o);
825                     for (int i=0;i<length;i++)
826                     {
827                         if (i > 0)
828                             sb.append("\n");
829                         sb.append(o.getClass().getComponentType().getName()); 
830                         sb.append("[");
831                         sb.append(i);
832                         sb.append("]=");
833                         sb.append(toString(Array.get(o, i)));
834                     }
835                     return sb.toString();
836                 }
837             }
838             else
839                 return o.toString();
840         }
841         catch (Exception e)
842         {
843             return e.toString();
844         }
845     }
846 
847     private boolean dump(HttpServletResponse response, String data, String chars, String block, String dribble, boolean flush) throws IOException
848     {
849         if (data != null && data.length() > 0)
850         {
851             long d=Long.parseLong(data);
852             int b=(block!=null&&block.length()>0)?Integer.parseInt(block):50;
853             byte[] buf=new byte[b];
854             for (int i=0;i<b;i++)
855             {
856                 
857                 buf[i]=(byte)('0'+(i%10));
858                 if (i%10==9)
859                     buf[i]=(byte)'\n';
860             }
861             buf[0]='o';
862             OutputStream out=response.getOutputStream();
863             response.setContentType("text/plain");
864             while (d > 0)
865             {
866                 if (b==1)
867                 {
868                     out.write(d%80==0?'\n':'.');
869                     d--;
870                 }
871                 else if (d>=b)
872                 {
873                     out.write(buf);
874                     d=d-b;
875                 }
876                 else
877                 {
878                     out.write(buf,0,(int)d);
879                     d=0;
880                 }
881                 
882                 if (dribble!=null)
883                 {
884                     out.flush();
885                     try
886                     {
887                         Thread.sleep(Long.parseLong(dribble));
888                     }
889                     catch (Exception e)
890                     {
891                         e.printStackTrace();
892                         break;
893                     }
894                 }
895                 
896             }
897             
898             if (flush)
899                 out.flush();
900             
901             return true;
902         }
903 
904         // Handle a dump of data
905         if (chars != null && chars.length() > 0)
906         {
907             long d=Long.parseLong(chars);
908             int b=(block!=null&&block.length()>0)?Integer.parseInt(block):50;
909             char[] buf=new char[b];
910             for (int i=0;i<b;i++)
911             {
912                 buf[i]=(char)('0'+(i%10));
913                 if (i%10==9)
914                     buf[i]='\n';
915             }
916             buf[0]='o';
917             response.setContentType("text/plain");
918             PrintWriter out=response.getWriter();
919             while (d > 0 && !out.checkError())
920             {
921                 if (b==1)
922                 {
923                     out.write(d%80==0?'\n':'.');
924                     d--;
925                 }
926                 else if (d>=b)
927                 {
928                     out.write(buf);
929                     d=d-b;
930                 }
931                 else
932                 {
933                     out.write(buf,0,(int)d);
934                     d=0;
935                 }
936             }
937             return true;
938         }    
939         return false;
940     }
941     
942     private String notag(String s)
943     {
944         if (s==null)
945             return "null";
946         s=StringUtil.replace(s,"&","&amp;");
947         s=StringUtil.replace(s,"<","&lt;");
948         s=StringUtil.replace(s,">","&gt;");
949         return s;
950     }
951 }