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