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