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