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