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