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