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