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