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