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