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().getRealPath(...): </th>");
673 try{pout.write("<td>"+getServletContext().getRealPath(res)+"</td>");}
674 catch(Exception e) {pout.write("<td>"+"" +e+"</td>");}
675
676 pout.write("</tr><tr>\n");
677 pout.write("<th align=\"right\">getServletContext().getContext(...): </th>");
678
679 ServletContext context = getServletContext().getContext(res);
680 pout.write("<td>"+context+"</td>");
681
682 if (context!=null)
683 {
684 pout.write("</tr><tr>\n");
685 pout.write("<th align=\"right\">getServletContext().getContext(...).getResource(...): </th>");
686 try{pout.write("<td>"+context.getResource(res)+"</td>");}
687 catch(Exception e) {pout.write("<td>"+"" +e+"</td>");}
688
689 pout.write("</tr><tr>\n");
690 pout.write("<th align=\"right\">getServletContext().getContext(...).getResourcePaths(...): </th>");
691 try{pout.write("<td>"+context.getResourcePaths(res)+"</td>");}
692 catch(Exception e) {pout.write("<td>"+"" +e+"</td>");}
693
694 String cp=context.getContextPath();
695 if (cp==null || "/".equals(cp))
696 cp="";
697 pout.write("</tr><tr>\n");
698 pout.write("<th align=\"right\">getServletContext().getContext(...).getRequestDispatcher(...): </th>");
699 pout.write("<td>"+context.getRequestDispatcher(res.substring(cp.length()))+"</td>");
700
701 pout.write("</tr><tr>\n");
702 pout.write("<th align=\"right\">getServletContext().getContext(...).getRealPath(...): </th>");
703 pout.write("<td>"+context.getRealPath(res.substring(cp.length()))+"</td>");
704 }
705
706 pout.write("</tr><tr>\n");
707 pout.write("<th align=\"right\">this.getClass().getResource(...): </th>");
708 pout.write("<td>"+this.getClass().getResource(res)+"</td>");
709
710 pout.write("</tr><tr>\n");
711 pout.write("<th align=\"right\">this.getClass().getClassLoader().getResource(...): </th>");
712 pout.write("<td>"+this.getClass().getClassLoader().getResource(res)+"</td>");
713
714 pout.write("</tr><tr>\n");
715 pout.write("<th align=\"right\">Thread.currentThread().getContextClassLoader().getResource(...): </th>");
716 pout.write("<td>"+Thread.currentThread().getContextClassLoader().getResource(res)+"</td>");
717 pout.write("</tr><tr>\n");
718 pout.write("<th align=\"right\">Thread.currentThread().getContextClassLoader().getResources(...): </th>");
719 Enumeration<URL> urls = Thread.currentThread().getContextClassLoader().getResources(res);
720 if (urls==null)
721 pout.write("<td>null</td>");
722 else
723 pout.write("<td>"+Collections.list(urls)+"</td>");
724
725 }
726
727 pout.write("</tr></table>\n");
728
729
730 pout.write("<h2>Request Wrappers</h2>\n");
731 ServletRequest rw=request;
732 int w=0;
733 while (rw !=null)
734 {
735 pout.write((w++)+": "+rw.getClass().getName()+"<br/>");
736 if (rw instanceof HttpServletRequestWrapper)
737 rw=((HttpServletRequestWrapper)rw).getRequest();
738 else if (rw instanceof ServletRequestWrapper)
739 rw=((ServletRequestWrapper)rw).getRequest();
740 else
741 rw=null;
742 }
743
744
745 pout.write("<h2>Response Wrappers</h2>\n");
746 ServletResponse rsw=response;
747 w=0;
748 while (rsw !=null)
749 {
750 pout.write((w++)+": "+rsw.getClass().getName()+"<br/>");
751 if (rsw instanceof HttpServletResponseWrapper)
752 rsw=((HttpServletResponseWrapper)rsw).getResponse();
753 else if (rsw instanceof ServletResponseWrapper)
754 rsw=((ServletResponseWrapper)rsw).getResponse();
755 else
756 rsw=null;
757 }
758
759 pout.write("<br/>");
760 pout.write("<h2>International Characters (UTF-8)</h2>");
761 pout.write("LATIN LETTER SMALL CAPITAL AE<br/>\n");
762 pout.write("Directly uni encoded(\\u1d01): \u1d01<br/>");
763 pout.write("HTML reference (&AElig;): Æ<br/>");
764 pout.write("Decimal (&#7425;): ᴁ<br/>");
765 pout.write("Javascript unicode (\\u1d01) : <script language='javascript'>document.write(\"\u1d01\");</script><br/>");
766 pout.write("<br/>");
767 pout.write("<h2>Form to generate GET content</h2>");
768 pout.write("<form method=\"GET\" action=\""+response.encodeURL(getURI(request))+"\">");
769 pout.write("TextField: <input type=\"text\" name=\"TextField\" value=\"value\"/><br/>\n");
770 pout.write("<input type=\"submit\" name=\"Action\" value=\"Submit\">");
771 pout.write("</form>");
772
773 pout.write("<br/>");
774
775 pout.write("<h2>Form to generate POST content</h2>");
776 pout.write("<form method=\"POST\" accept-charset=\"utf-8\" action=\""+response.encodeURL(getURI(request))+"\">");
777 pout.write("TextField: <input type=\"text\" name=\"TextField\" value=\"value\"/><br/>\n");
778 pout.write("Select: <select multiple name=\"Select\">\n");
779 pout.write("<option>ValueA</option>");
780 pout.write("<option>ValueB1,ValueB2</option>");
781 pout.write("<option>ValueC</option>");
782 pout.write("</select><br/>");
783 pout.write("<input type=\"submit\" name=\"Action\" value=\"Submit\"><br/>");
784 pout.write("</form>");
785 pout.write("<br/>");
786
787 pout.write("<h2>Form to generate UPLOAD content</h2>");
788 pout.write("<form method=\"POST\" enctype=\"multipart/form-data\" accept-charset=\"utf-8\" action=\""+
789 response.encodeURL(getURI(request))+(request.getQueryString()==null?"":("?"+request.getQueryString()))+
790 "\">");
791 pout.write("TextField: <input type=\"text\" name=\"TextField\" value=\"comment\"/><br/>\n");
792 pout.write("File 1: <input type=\"file\" name=\"file1\" /><br/>\n");
793 pout.write("File 2: <input type=\"file\" name=\"file2\" /><br/>\n");
794 pout.write("<input type=\"submit\" name=\"Action\" value=\"Submit\"><br/>");
795 pout.write("</form>");
796
797 pout.write("<h2>Form to set Cookie</h2>");
798 pout.write("<form method=\"POST\" action=\""+response.encodeURL(getURI(request))+"\">");
799 pout.write("cookie: <input type=\"text\" name=\"cookie\" /><br/>\n");
800 pout.write("value: <input type=\"text\" name=\"cookiev\" /><br/>\n");
801 pout.write("<input type=\"submit\" name=\"Action\" value=\"setCookie\">");
802 pout.write("</form>\n");
803
804 pout.write("<h2>Form to get Resource</h2>");
805 pout.write("<form method=\"POST\" action=\""+response.encodeURL(getURI(request))+"\">");
806 pout.write("resource: <input type=\"text\" name=\"resource\" /><br/>\n");
807 pout.write("<input type=\"submit\" name=\"Action\" value=\"getResource\">");
808 pout.write("</form>\n");
809 }
810 catch (Exception e)
811 {
812 getServletContext().log("dump "+e);
813 }
814
815 String lines= request.getParameter("lines");
816 if (lines!=null)
817 {
818 char[] line = "<span>A line of characters. Blah blah blah blah. blooble blooble</span></br>\n".toCharArray();
819 for (int l=Integer.parseInt(lines);l-->0;)
820 {
821 pout.write("<span>"+l+" </span>");
822 pout.write(line);
823 }
824 }
825
826 pout.write("</body>\n</html>\n");
827
828 pout.close();
829
830 if (pi != null)
831 {
832 if ("/ex4".equals(pi))
833 throw new ServletException("test ex4", new Throwable());
834 if ("/ex5".equals(pi))
835 throw new IOException("test ex5");
836 if ("/ex6".equals(pi))
837 throw new UnavailableException("test ex6");
838 }
839
840
841 }
842
843
844
845 @Override
846 public String getServletInfo()
847 {
848 return "Dump Servlet";
849 }
850
851
852 @Override
853 public synchronized void destroy()
854 {
855 _timer.cancel();
856 }
857
858
859 private String getURI(HttpServletRequest request)
860 {
861 String uri= (String)request.getAttribute("javax.servlet.forward.request_uri");
862 if (uri == null)
863 uri= request.getRequestURI();
864 return uri;
865 }
866
867
868 private static String toString(Object o)
869 {
870 if (o == null)
871 return null;
872
873 try
874 {
875 if (o.getClass().isArray())
876 {
877 StringBuffer sb = new StringBuffer();
878 if (!o.getClass().getComponentType().isPrimitive())
879 {
880 Object[] array= (Object[])o;
881 for (int i= 0; i < array.length; i++)
882 {
883 if (i > 0)
884 sb.append("\n");
885 sb.append(array.getClass().getComponentType().getName());
886 sb.append("[");
887 sb.append(i);
888 sb.append("]=");
889 sb.append(toString(array[i]));
890 }
891 return sb.toString();
892 }
893 else
894 {
895 int length = Array.getLength(o);
896 for (int i=0;i<length;i++)
897 {
898 if (i > 0)
899 sb.append("\n");
900 sb.append(o.getClass().getComponentType().getName());
901 sb.append("[");
902 sb.append(i);
903 sb.append("]=");
904 sb.append(toString(Array.get(o, i)));
905 }
906 return sb.toString();
907 }
908 }
909 else
910 return o.toString();
911 }
912 catch (Exception e)
913 {
914 return e.toString();
915 }
916 }
917
918 private boolean dump(HttpServletResponse response, String data, String chars, String block, String dribble, boolean flush) throws IOException
919 {
920 if (data != null && data.length() > 0)
921 {
922 long d=Long.parseLong(data);
923 int b=(block!=null&&block.length()>0)?Integer.parseInt(block):50;
924 byte[] buf=new byte[b];
925 for (int i=0;i<b;i++)
926 {
927
928 buf[i]=(byte)('0'+(i%10));
929 if (i%10==9)
930 buf[i]=(byte)'\n';
931 }
932 buf[0]='o';
933 OutputStream out=response.getOutputStream();
934 response.setContentType("text/plain");
935 while (d > 0)
936 {
937 if (b==1)
938 {
939 out.write(d%80==0?'\n':'.');
940 d--;
941 }
942 else if (d>=b)
943 {
944 out.write(buf);
945 d=d-b;
946 }
947 else
948 {
949 out.write(buf,0,(int)d);
950 d=0;
951 }
952
953 if (dribble!=null)
954 {
955 out.flush();
956 try
957 {
958 Thread.sleep(Long.parseLong(dribble));
959 }
960 catch (Exception e)
961 {
962 e.printStackTrace();
963 break;
964 }
965 }
966
967 }
968
969 if (flush)
970 out.flush();
971
972 return true;
973 }
974
975
976 if (chars != null && chars.length() > 0)
977 {
978 long d=Long.parseLong(chars);
979 int b=(block!=null&&block.length()>0)?Integer.parseInt(block):50;
980 char[] buf=new char[b];
981 for (int i=0;i<b;i++)
982 {
983 buf[i]=(char)('0'+(i%10));
984 if (i%10==9)
985 buf[i]='\n';
986 }
987 buf[0]='o';
988 response.setContentType("text/plain");
989 PrintWriter out=response.getWriter();
990 while (d > 0 && !out.checkError())
991 {
992 if (b==1)
993 {
994 out.write(d%80==0?'\n':'.');
995 d--;
996 }
997 else if (d>=b)
998 {
999 out.write(buf);
1000 d=d-b;
1001 }
1002 else
1003 {
1004 out.write(buf,0,(int)d);
1005 d=0;
1006 }
1007 }
1008 return true;
1009 }
1010 return false;
1011 }
1012
1013 private String notag(String s)
1014 {
1015 if (s==null)
1016 return "null";
1017 s=s.replaceAll("&","&");
1018 s=s.replaceAll("<","<");
1019 s=s.replaceAll(">",">");
1020 return s;
1021 }
1022 }