View Javadoc

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