1 7 8 10 11 package org.joseki.server.webserver.servlets; 13 14 import java.util.* ; 15 import java.io.* ; 16 import javax.servlet.http.*; 17 import javax.servlet.* ; 18 19 public class DumpServlet extends HttpServlet 20 { 21 22 public DumpServlet() 23 { 24 25 } 26 27 public void init() 28 { 29 return ; 30 } 31 32 public void doGet(HttpServletRequest req, HttpServletResponse resp) 33 { 34 try { 35 PrintWriter out = resp.getWriter() ; 36 resp.setContentType("text/html"); 37 38 String now = new Date().toString() ; 39 40 out.println("<html>") ; 42 out.println("<head>") ; 43 out.println("<Title>Dump @ "+now+"</Title>") ; 44 out.println("<meta CONTENT=now HTTP-EQUIV=expires>") ; 46 out.println("</head>") ; 47 48 out.println("<body>") ; 50 out.println("<pre>") ; 51 52 out.println("Dump : "+now); 53 out.println() ; 54 out.println("==== Request"); 55 out.println() ; 56 out.print(dumpRequest(req)) ; 57 out.println() ; 58 59 out.println("==== ServletContext"); 60 out.println() ; 61 out.print(dumpServletContext()); 62 out.println() ; 63 64 out.println("==== Environment"); 65 out.println() ; 66 out.print(dumpEnvironment()); 67 out.println() ; 68 69 out.println("</pre>") ; 70 71 out.println("</body>") ; 72 out.println("</html>") ; 73 out.flush() ; 74 } catch (IOException e) 75 { } 76 } 77 78 80 static public String dumpRequest(HttpServletRequest req) 81 { 82 try { 83 StringWriter sw = new StringWriter() ; 84 PrintWriter pw = new PrintWriter(sw) ; 85 86 pw.println("Method: "+req.getMethod()); 88 pw.println("getContentLength: "+Integer.toString(req.getContentLength())); 89 pw.println("getContentType: "+req.getContentType()); 90 pw.println("getRequestURI: "+req.getRequestURI()); 91 pw.println("getRequestURL: "+req.getRequestURL()); 92 pw.println("getContextPath: "+req.getContextPath()); 93 pw.println("getServletPath: "+req.getServletPath()); 94 pw.println("getPathInfo: "+req.getPathInfo()); 95 pw.println("getPathTranslated: "+req.getPathTranslated()); 96 pw.println("getQueryString: "+req.getQueryString()); 97 pw.println("getProtocol: "+req.getProtocol()); 98 pw.println("getScheme: "+req.getScheme()); 99 pw.println("getServerName: "+req.getServerName()); 100 pw.println("getServerPort: "+req.getServerPort()); 101 pw.println("getRemoteUser: "+req.getRemoteUser()); 102 pw.println("getRemoteAddr: "+req.getRemoteAddr()); 103 pw.println("getRemoteHost: "+req.getRemoteHost()); 104 pw.println("getRequestedSessionId: "+req.getRequestedSessionId()); 105 { 106 String tmp = "" ; 107 Cookie c[] = req.getCookies() ; 108 if ( c == null ) 109 pw.println("getCookies: <none>"); 110 else 111 { 112 for ( int i = 0 ; i < c.length ; i++ ) 113 { 114 pw.println("Cookie: "+c[i].getName()); 115 pw.println(" value: "+c[i].getValue()); 116 pw.println(" version: "+c[i].getVersion()); 117 pw.println(" comment: "+c[i].getComment()); 118 pw.println(" domain: "+c[i].getDomain()); 119 pw.println(" maxAge: "+c[i].getMaxAge()); 120 pw.println(" path: "+c[i].getPath()); 121 pw.println(" secure: "+c[i].getSecure()); 122 pw.println(); 123 } 124 } 125 } 126 Enumeration enum = req.getHeaderNames() ; 128 129 for ( ; enum.hasMoreElements() ; ) 130 { 131 String name = (String)enum.nextElement() ; 132 String value = req.getHeader(name) ; 133 pw.println("Head: "+name + " = " + value) ; 134 } 135 136 enum = req.getAttributeNames() ; 137 if ( enum.hasMoreElements() ) 138 pw.println(); 139 for ( ; enum.hasMoreElements() ; ) 140 { 141 String name = (String)enum.nextElement() ; 142 String value = req.getAttribute(name).toString() ; 143 pw.println("Attr: "+name + " = " + value) ; 144 } 145 146 158 if ( req.getQueryString() != null ) 161 { 162 pw.println(); 163 String[] params = req.getQueryString().split("&") ; 164 for ( int i = 0 ; i < params.length ; i++ ) 165 { 166 String p = params[i] ; 167 String[] x = p.split("=",2) ; 168 String name = null ; 169 String value = null ; 170 171 if ( x.length == 0 ) 172 { 173 name = p ; 174 value = "" ; 175 } 176 else if ( x.length == 1 ) 177 { 178 name = x[0] ; 179 value = "" ; 180 } 181 else 182 { 183 name = x[0] ; 184 value = x[1] ; 185 } 186 pw.println("Param: "+name + " = " + value) ; 187 } 188 } 189 190 enum = req.getLocales(); 191 if ( enum.hasMoreElements() ) 192 pw.println(); 193 for ( ; enum.hasMoreElements() ; ) 194 { 195 String name = ((Locale)enum.nextElement()).toString() ; 196 pw.println("Locale: "+name) ; 197 } 198 199 pw.println() ; 200 201 BufferedReader in = req.getReader() ; 202 if ( req.getContentLength() > 0 ) 203 in.mark(req.getContentLength()+2) ; 205 else 206 in.mark(100*1024) ; 208 209 210 while(in.ready()) 211 { 212 pw.println(in.readLine()); 213 } 214 215 try { in.reset() ;} catch (IOException e) { System.out.println("DumpServlet: Reset of content failed: "+e) ; } 216 217 pw.close() ; 218 sw.close() ; 219 return sw.toString() ; 220 } catch (IOException e) 221 { } 222 return null ; 223 } 224 225 233 static public String dumpEnvironment() 234 { 235 Properties properties = System.getProperties(); 236 StringWriter sw = new StringWriter() ; 237 PrintWriter pw = new PrintWriter(sw) ; 238 Enumeration enum = properties.keys(); 239 while(enum.hasMoreElements()) 240 { 241 String key = (String)enum.nextElement(); 242 pw.println(key+": '"+properties.getProperty(key)+"'"); 243 } 244 pw.println() ; 245 pw.close() ; 246 try { 247 sw.close() ; 248 } catch (IOException e) { 249 e.printStackTrace(); 250 } 251 return sw.toString() ; 252 } 253 254 public String dumpServletContext() 255 { 256 StringWriter sw = new StringWriter() ; 257 PrintWriter pw = new PrintWriter(sw) ; 258 259 ServletContext sc = getServletContext(); 260 pw.println("majorVersion: '"+sc.getMajorVersion()+"'"); 261 pw.println("minorVersion: '"+sc.getMinorVersion()+"'"); 262 pw.println("contextName: '"+sc.getServletContextName()+"'"); 263 pw.println("servletInfo: '"+getServletInfo()+"'"); 264 pw.println("serverInfo: '"+sc.getServerInfo()+"'"); 265 266 Enumeration enum = null ; 267 enum = sc.getInitParameterNames(); 282 if (enum != null) { 283 pw.println("initParameters: "); 284 while(enum.hasMoreElements()) 285 { 286 String key = (String)enum.nextElement(); 287 pw.println(key+": '"+sc.getInitParameter(key)+"'"); 288 } 289 } 290 enum = sc.getAttributeNames(); 291 if (enum != null) { 292 pw.println("attributes: "); 293 while(enum.hasMoreElements()) 294 { 295 String key = (String)enum.nextElement(); 296 pw.println(key+": '"+sc.getAttribute(key)+"'"); 297 } 298 } 299 pw.println() ; 300 pw.close() ; 301 try { 302 sw.close() ; 303 } catch (IOException e) { 304 e.printStackTrace(); 305 } 306 return sw.toString() ; 307 } 308 309 310 public void doPost(HttpServletRequest req, HttpServletResponse resp) 311 { 312 doGet(req, resp) ; 313 } 314 315 316 public String getServletInfo() 317 { 318 return "Dump"; 319 } 320 } 321 322 348 | Popular Tags |