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