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