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