1 16 package org.mortbay.http.handler; 17 18 import java.io.ByteArrayOutputStream ; 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.io.OutputStream ; 22 import java.io.OutputStreamWriter ; 23 import java.io.Writer ; 24 import java.util.Enumeration ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.Set ; 28 29 import javax.servlet.http.Cookie ; 30 31 import org.apache.commons.logging.Log; 32 import org.mortbay.log.LogFactory; 33 import org.mortbay.http.HttpException; 34 import org.mortbay.http.HttpFields; 35 import org.mortbay.http.HttpRequest; 36 import org.mortbay.http.HttpResponse; 37 import org.mortbay.util.LogSupport; 38 import org.mortbay.util.StringUtil; 39 40 41 48 public class DumpHandler extends AbstractHttpHandler 49 { 50 private static Log log = LogFactory.getLog(DumpHandler.class); 51 52 53 public String realPath(String pathSpec, String path) 54 { 55 return ""; 56 } 57 58 59 public void handle(String pathInContext, 60 String pathParams, 61 HttpRequest request, 62 HttpResponse response) 63 throws HttpException, IOException 64 { 65 if (!isStarted()) 66 return; 67 68 if (!HttpRequest.__GET.equals(request.getMethod()) && 70 !HttpRequest.__HEAD.equals(request.getMethod()) && 71 !HttpRequest.__POST.equals(request.getMethod())) 72 return; 73 74 log.debug("Dump"); 75 76 response.setField(HttpFields.__ContentType, 77 HttpFields.__TextHtml); 78 OutputStream out = response.getOutputStream(); 79 ByteArrayOutputStream buf = new ByteArrayOutputStream (2048); 80 Writer writer = new OutputStreamWriter (buf,StringUtil.__ISO_8859_1); 81 writer.write("<HTML><H1>Dump HttpHandler</H1>"); 82 writer.write("<PRE>\npath="+request.getPath()+ 83 "\ncontextPath="+getHttpContext().getContextPath()+ 84 "\npathInContext="+pathInContext+ 85 "\n</PRE>\n"); 86 writer.write("<H3>Header:</H3><PRE>"); 87 writer.write(request.toString()); 88 writer.write("</PRE>\n<H3>Parameters:</H3>\n<PRE>"); 89 Set names=request.getParameterNames(); 90 Iterator iter = names.iterator(); 91 while(iter.hasNext()) 92 { 93 String name=iter.next().toString(); 94 List values=request.getParameterValues(name); 95 if (values==null || values.size()==0) 96 { 97 writer.write(name); 98 writer.write("=\n"); 99 } 100 else if (values.size()==1) 101 { 102 writer.write(name); 103 writer.write("="); 104 writer.write((String )values.get(0)); 105 writer.write("\n"); 106 } 107 else 108 { 109 for (int i=0; i<values.size(); i++) 110 { 111 writer.write(name); 112 writer.write("["+i+"]="); 113 writer.write((String )values.get(i)); 114 writer.write("\n"); 115 } 116 } 117 } 118 119 String cookie_name=request.getParameter("CookieName"); 120 if (cookie_name!=null && cookie_name.trim().length()>0) 121 { 122 String cookie_action=request.getParameter("Button"); 123 try{ 124 Cookie cookie= 125 new Cookie (cookie_name.trim(), 126 request.getParameter("CookieVal")); 127 if ("Clear Cookie".equals(cookie_action)) 128 cookie.setMaxAge(0); 129 response.addSetCookie(cookie); 130 } 131 catch(IllegalArgumentException e) 132 { 133 writer.write("</PRE>\n<H3>BAD Set-Cookie:</H3>\n<PRE>"); 134 writer.write(e.toString()); 135 LogSupport.ignore(log,e); 136 } 137 } 138 139 writer.write("</PRE>\n<H3>Cookies:</H3>\n<PRE>"); 140 Cookie [] cookies=request.getCookies(); 141 if (cookies!=null && cookies.length>0) 142 { 143 for(int c=0;c<cookies.length;c++) 144 { 145 Cookie cookie=cookies[c]; 146 writer.write(cookie.getName()); 147 writer.write("="); 148 writer.write(cookie.getValue()); 149 writer.write("\n"); 150 } 151 } 152 153 writer.write("</PRE>\n<H3>Attributes:</H3>\n<PRE>"); 154 Enumeration attributes=request.getAttributeNames(); 155 if (attributes!=null && attributes.hasMoreElements()) 156 { 157 while(attributes.hasMoreElements()) 158 { 159 String attr=attributes.nextElement().toString(); 160 writer.write(attr); 161 writer.write("="); 162 writer.write(request.getAttribute(attr).toString()); 163 writer.write("\n"); 164 } 165 } 166 167 writer.write("</PRE>\n<H3>Content:</H3>\n<PRE>"); 168 byte[] content= new byte[4096]; 169 int len; 170 try{ 171 InputStream in=request.getInputStream(); 172 while((len=in.read(content))>=0) 173 writer.write(new String (content,0,len)); 174 } 175 catch(IOException e) 176 { 177 LogSupport.ignore(log,e); 178 writer.write(e.toString()); 179 } 180 181 request.getAcceptableTransferCodings(); 185 186 writer.flush(); 188 response.setIntField(HttpFields.__ContentLength,buf.size()+1000); 189 buf.writeTo(out); 190 out.flush(); 191 192 buf.reset(); 194 writer.write("</PRE>\n<H3>Response:</H3>\n<PRE>"); 195 writer.write(response.toString()); 196 writer.write("</PRE></HTML>"); 197 writer.flush(); 198 for (int pad=998-buf.size();pad-->0;) 199 writer.write(" "); 200 writer.write("\015\012"); 201 writer.flush(); 202 buf.writeTo(out); 203 204 request.setHandled(true); 205 } 206 } 207 208 209 210 211 212 | Popular Tags |