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