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