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