View Javadoc

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