1 package com.lutris.appserver.debugger.business; 2 3 import java.util.Vector ; 4 import java.util.Enumeration ; 5 import java.util.Hashtable ; 6 import java.util.StringTokenizer ; 7 import javax.servlet.http.*; 8 import java.io.IOException ; 9 import javax.servlet.*; 10 import javax.servlet.http.HttpServletResponse ; 11 import javax.servlet.http.HttpServletRequest ; 12 import javax.servlet.http.Cookie ; 13 import com.lutris.util.*; 14 import com.lutris.appserver.server.session.Session; 15 import com.lutris.appserver.server.sessionEnhydra.StandardSession; 16 import com.lutris.appserver.server.Application; 17 import com.lutris.appserver.server.session.SessionData; 18 import com.lutris.appserver.server.httpPresentation.servlet.*; 19 import com.lutris.appserver.server.sessionContainerAdapter.ContainerAdapterSessionManager; 20 import com.lutris.appserver.server.sessionContainerAdapter.JmxContainerAdapterSessionManager; 21 import com.lutris.appserver.server.sessionEnhydra.SimpleServletSessionManager; 22 import com.lutris.appserver.server.sessionEnhydra.StandardSessionManager; 23 import org.mortbay.jetty.*; 24 import org.mortbay.http.*; 25 import org.mortbay.servlet.*; 26 import org.mortbay.jetty.servlet.*; 27 import org.mortbay.jetty.servlet.DirectorServletHttpResponse; 28 29 public class DebuggerFilter implements Filter { 30 31 32 private static Object sinc = new Object (); 33 private String servletName; 34 private Servlet servlet; 35 private boolean work=true; 36 public DebuggerFilter(){ 37 38 } 39 40 41 public String getInfo(){ 42 return "com.lutris.appserver.debugger.business.DebuggerFilter"; 43 } 44 45 46 public void doFilter(ServletRequest request, 47 ServletResponse response, 48 FilterChain chain) 49 throws java.io.IOException , 50 ServletException 51 { 52 53 54 if(work) 55 { 56 Hashtable record=new Hashtable (); 57 58 manageRequest(record,(ServletRequest )request,(HttpServletRequest )request); 59 60 chain.doFilter(request, response); 61 62 manageResponse(record,response,(HttpServletRequest )request); 63 64 Globals.addTransaction(record); 65 66 ServletMonitorImpl.newRequest(); 67 } 68 else 69 { 70 chain.doFilter(request, response); 71 } 72 } 73 74 public boolean isFiltering(){ 75 return work; 76 } 77 78 public void setFiltering(boolean b){ 79 work=b; 80 } 81 82 83 public StringBuffer getAttribuestAsStringBuffer(HttpSession httpsession) 84 { 85 StringBuffer stringbuffer = new StringBuffer (); 86 Enumeration enumeration = httpsession.getAttributeNames(); 87 if(httpsession != null) 88 { 89 stringbuffer.append("<UL>\n"); 90 for(; enumeration.hasMoreElements(); stringbuffer.append("\n")) 91 { 92 String s = (String )enumeration.nextElement(); 93 Object obj = httpsession.getAttribute(s); 94 stringbuffer.append("<LI> <TT>"); 95 stringbuffer.append(s); 96 stringbuffer.append(": </TT>"); 97 stringbuffer.append(obj.toString()); 98 } 99 100 stringbuffer.append("</UL>\n"); 101 } 102 return stringbuffer; 103 } 104 105 106 private static void wait(int wait) 107 { 108 109 long now = System.currentTimeMillis(); 110 long giveUpTime = now + (wait *10); 111 112 long leftToGo = giveUpTime - now; 113 114 synchronized (sinc) { 115 try { 116 sinc.wait(leftToGo); 117 } catch (InterruptedException e) { 118 119 } 120 } 121 } 122 123 124 125 private String getSessionData(HttpServletRequest request){ 126 127 Session session; 128 HttpSession httpsession; 129 session = null; 130 httpsession = request.getSession(false); 131 132 133 134 if(servlet!=null) 135 { 136 Application app = ((HttpPresentationServlet)servlet).getApplication(); 138 139 com.lutris.appserver.server.session.SessionManager sm = app.getSessionManager(); 140 141 if((sm instanceof StandardSessionManager) || (sm instanceof SimpleServletSessionManager)) 142 { 143 session = ((HttpPresentationServlet)servlet).getSession(request); 144 if(!(sm instanceof SimpleServletSessionManager) && session != null) 145 httpsession = session.getHttpSession(); 146 147 148 } 149 if((sm instanceof ContainerAdapterSessionManager) || (sm instanceof JmxContainerAdapterSessionManager)) 150 { 151 if(httpsession != null) 152 session = (Session)httpsession.getAttribute("session"); 153 else 154 return "<I>No Session</I>"; 155 156 } 157 }else 158 { 159 String s; 161 if(httpsession != null) 162 { 163 s = ""; 164 StringBuffer stringbuffer = getAttribuestAsStringBuffer(httpsession); 165 s = s + stringbuffer.toString(); 166 if(s.length() == 0) 167 s = "<I>Empty</I>"; 168 return s; 169 }else{ 170 return "<I>No Session</I>"; 171 } 172 } 173 174 175 176 177 if(httpsession==null) 179 return "<I>No Session</I>"; 180 else 181 { 182 if(session == null) 183 { 184 String s=""; 185 StringBuffer stringbuffer = getAttribuestAsStringBuffer(httpsession); 186 s = s + stringbuffer.toString(); 187 if(s.length() == 0) 188 s = "<I>Empty</I>"; 189 return s; 190 191 }else{ 192 String s=""; 193 SessionData sessiondata = session.getSessionData(); 194 s = sessiondata == null ? "" : sessiondata.toHtml(); 195 if(s == null) 196 s = ""; 197 StringBuffer stringbuffer = getAttribuestAsStringBuffer(httpsession); 198 s = s + stringbuffer.toString(); 199 if(s.length() == 0) 200 s = "<I>Empty</I>"; 201 return s; 202 } 203 } 204 205 206 } 207 208 209 private String check(String data){ 210 if(data==null || data=="") 211 return "N/A"; 212 else 213 return new String (data); 214 } 215 216 217 private void manageRequest(Hashtable record,ServletRequest req,HttpServletRequest hreq){ 218 String sessionDataRequest=getSessionData(hreq); 219 record.put("S",sessionDataRequest); 220 221 record.put("ServletName",servletName); 222 record.put("RemoteHost",check(req.getRemoteHost())); 223 record.put("RemoteAdr",check(req.getRemoteAddr())); 224 record.put("Method",check(hreq.getMethod())); 225 record.put("ServerName",check(req.getServerName())); 226 record.put("ServerPort",new Integer (req.getServerPort())); 227 record.put("RequestURI",check(hreq.getRequestURI())); 228 record.put("Scheme",check(req.getScheme())); 229 record.put("QueryString",check(hreq.getQueryString())); 230 record.put("RemoteUser",check(hreq.getRemoteUser())); 231 record.put("AuthType",check(hreq.getAuthType())); 232 record.put("ContentLength",new Integer (req.getContentLength())); 233 record.put("ContentType",check(req.getContentType())); 234 record.put("PathInfo",check(hreq.getPathInfo())); 235 record.put("PathTranslated",check(hreq.getPathTranslated())); 236 record.put("Protocol",check(req.getProtocol())); 237 238 239 Enumeration e = hreq.getHeaderNames(); 240 Vector names=new Vector (); 241 242 while (e.hasMoreElements()) 243 { 244 String name= (String )e.nextElement(); 245 String header = hreq.getHeader(name); 246 names.add(name); 247 record.put(name,header); 248 } 249 record.put("HeaderNames",names); 250 251 252 Cookie [] temp=hreq.getCookies(); 253 if(temp!=null) 254 record.put("Cookies",temp); 255 else 256 record.put("Cookies",new Cookie [0]); 257 258 259 Enumeration en = req.getParameterNames(); 260 Vector namess=new Vector (); 261 262 while (en.hasMoreElements()) 263 { 264 String name= (String )en.nextElement(); 265 String [] values = hreq.getParameterValues(name); 266 namess.add(name); 267 record.put(name,values); 268 } 269 record.put("ParameterNames",namess); 270 271 272 273 StringBuffer uri = new StringBuffer (); 274 uri.append(hreq.getRequestURI()); 275 276 277 if(hreq.getQueryString() != null) 278 { 279 uri.append('?'); 280 uri.append(hreq.getQueryString()); 281 } 282 283 record.put("RequestURL",uri.toString()); 284 285 286 } 287 288 289 private void manageResponse(Hashtable record,ServletResponse response,HttpServletRequest hreq) 290 { 291 292 String sessionDataResponse=getSessionData(hreq); 293 record.put("S1",sessionDataResponse); 294 295 String data=response.toString(); 297 StringTokenizer st=new StringTokenizer (data); 298 Hashtable result=new Hashtable (); 299 st.nextToken(); 300 String statusCode=st.nextToken(); 301 String message=st.nextToken(); 302 String name=""; 303 StringBuffer value=new StringBuffer (); 304 while(st.hasMoreTokens()) 305 { 306 String temp=st.nextToken(); 307 if(temp.endsWith(":")) 308 { 309 if(!name.equals("")) 310 result.put(name,value.toString()); 311 name=temp.replaceAll(":",""); 312 value=new StringBuffer (); 313 } 314 else 315 { 316 value.append(" "); 317 value.append(temp); 318 } 319 } 320 321 result.put(name,value.toString()); 322 323 Cookie [] coc=null; 324 325 328 if(coc==null) 329 record.put("CookiesResponse",new Cookie [0]); 330 else 331 record.put("CookiesResponse",coc); 332 333 record.put("StatusCode",new Integer (Integer.parseInt(statusCode))); 334 record.put("Message",message); 335 record.put("TotalBytesResponse",new Integer (-1)); 336 String cl=(String )result.get("Content-Length"); 337 if(cl!=null) 338 record.put("ContentLengthResponse",new Integer (Integer.parseInt(cl.trim()))); 339 else 340 record.put("ContentLengthResponse",new Integer (-1)); 341 String ct=(String )result.get("Content-Type"); 342 if(ct!=null) 343 record.put("ContentTypeResponse",check(ct)); 344 else 345 record.put("ContentTypeResponse",""); 346 record.put("StatusResponse",new Integer (Integer.parseInt(statusCode.trim()))); 347 348 String [] hnames=new String [result.size()]; 350 int yy=0; 351 for (Enumeration e = result.keys() ; e.hasMoreElements() ;) { 352 String namee=(String )e.nextElement(); 353 hnames[yy]=namee; 354 String valuee=(String )result.get(namee); 355 356 record.put("Response"+namee,valuee); 357 yy++; 358 } 359 record.put("HeaderNamesResponse",hnames); 360 361 } 362 363 364 public void init(FilterConfig filterConfig) 365 throws ServletException{ 366 try{ 367 368 369 servletName=filterConfig.getFilterName(); 370 HttpContext sc=Globals.getServlet(servletName); 371 ServletHandler ttt=(ServletHandler)sc.getHandler(Class.forName("org.mortbay.jetty.servlet.ServletHandler")); 372 ServletHolder sh=ttt.getServletHolder("enhydra"); 373 if(sh!=null) 374 { 375 376 servlet= sh.getServlet(); 377 } 378 }catch(Exception e){ 379 } 380 381 } 382 public void destroy(){ 383 servlet=null; 384 } 385 386 } 387 388 389 390 391 392 | Popular Tags |