KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lutris > appserver > debugger > business > DebuggerFilter


1 package com.lutris.appserver.debugger.business;
2
3 import java.util.Vector JavaDoc;
4 import java.util.Enumeration JavaDoc;
5 import java.util.Hashtable JavaDoc;
6 import java.util.StringTokenizer JavaDoc;
7 import javax.servlet.http.*;
8 import java.io.IOException JavaDoc;
9 import javax.servlet.*;
10 import javax.servlet.http.HttpServletResponse JavaDoc;
11 import javax.servlet.http.HttpServletRequest JavaDoc;
12 import javax.servlet.http.Cookie JavaDoc;
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 JavaDoc sinc = new Object JavaDoc();
33    private String JavaDoc servletName;
34    private Servlet servlet;
35    private boolean work=true;
36 public DebuggerFilter(){
37
38 }
39
40
41 public String JavaDoc getInfo(){
42   return "com.lutris.appserver.debugger.business.DebuggerFilter";
43 }
44
45
46 public void doFilter(ServletRequest JavaDoc request,
47                      ServletResponse JavaDoc response,
48                      FilterChain chain)
49               throws java.io.IOException JavaDoc,
50                      ServletException
51  {
52  
53
54  if(work)
55   {
56     Hashtable JavaDoc record=new Hashtable JavaDoc();
57
58     manageRequest(record,(ServletRequest JavaDoc)request,(HttpServletRequest JavaDoc)request);
59    
60     chain.doFilter(request, response);
61   
62     manageResponse(record,response,(HttpServletRequest JavaDoc)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 JavaDoc getAttribuestAsStringBuffer(HttpSession httpsession)
84     {
85         StringBuffer JavaDoc stringbuffer = new StringBuffer JavaDoc();
86         Enumeration JavaDoc enumeration = httpsession.getAttributeNames();
87         if(httpsession != null)
88         {
89             stringbuffer.append("<UL>\n");
90             for(; enumeration.hasMoreElements(); stringbuffer.append("\n"))
91             {
92                 String JavaDoc s = (String JavaDoc)enumeration.nextElement();
93                 Object JavaDoc 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 JavaDoc e) {
118                  
119              }
120             }
121   }
122     
123
124
125 private String JavaDoc getSessionData(HttpServletRequest JavaDoc 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         //enhydra servlets
137
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        //non-enhydra servlets
160
String JavaDoc s;
161      if(httpsession != null)
162        {
163         s = "";
164         StringBuffer JavaDoc 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 //enhydra
178
if(httpsession==null)
179    return "<I>No Session</I>";
180 else
181   {
182   if(session == null)
183     {
184         String JavaDoc s="";
185         StringBuffer JavaDoc 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 JavaDoc s="";
193         SessionData sessiondata = session.getSessionData();
194         s = sessiondata == null ? "" : sessiondata.toHtml();
195         if(s == null)
196             s = "";
197         StringBuffer JavaDoc 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 JavaDoc check(String JavaDoc data){
210   if(data==null || data=="")
211     return "N/A";
212   else
213     return new String JavaDoc(data);
214 }
215
216
217 private void manageRequest(Hashtable JavaDoc record,ServletRequest JavaDoc req,HttpServletRequest JavaDoc hreq){
218 String JavaDoc 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 JavaDoc(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 JavaDoc(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 JavaDoc e = hreq.getHeaderNames();
240 Vector JavaDoc names=new Vector JavaDoc();
241
242 while (e.hasMoreElements())
243   {
244    String JavaDoc name= (String JavaDoc)e.nextElement();
245    String JavaDoc header = hreq.getHeader(name);
246    names.add(name);
247    record.put(name,header);
248   }
249 record.put("HeaderNames",names);
250     
251
252 Cookie JavaDoc[] temp=hreq.getCookies();
253 if(temp!=null)
254  record.put("Cookies",temp);
255 else
256  record.put("Cookies",new Cookie JavaDoc[0]);
257     
258  
259 Enumeration JavaDoc en = req.getParameterNames();
260 Vector JavaDoc namess=new Vector JavaDoc();
261     
262 while (en.hasMoreElements())
263  {
264   String JavaDoc name= (String JavaDoc)en.nextElement();
265   String JavaDoc[] values = hreq.getParameterValues(name);
266   namess.add(name);
267   record.put(name,values);
268   }
269 record.put("ParameterNames",namess);
270     
271  
272  
273 StringBuffer JavaDoc uri = new StringBuffer JavaDoc();
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 JavaDoc record,ServletResponse JavaDoc response,HttpServletRequest JavaDoc hreq)
290  {
291     
292    String JavaDoc sessionDataResponse=getSessionData(hreq);
293    record.put("S1",sessionDataResponse);
294
295   /////parse
296
String JavaDoc data=response.toString();
297   StringTokenizer JavaDoc st=new StringTokenizer JavaDoc(data);
298   Hashtable JavaDoc result=new Hashtable JavaDoc();
299   st.nextToken();
300   String JavaDoc statusCode=st.nextToken();
301   String JavaDoc message=st.nextToken();
302   String JavaDoc name="";
303   StringBuffer JavaDoc value=new StringBuffer JavaDoc();
304   while(st.hasMoreTokens())
305    {
306      String JavaDoc 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 JavaDoc();
313     }
314    else
315     {
316         value.append(" ");
317     value.append(temp);
318     }
319    }
320    
321    result.put(name,value.toString());
322
323 Cookie JavaDoc[] coc=null;
324
325 //if (response instanceof DirectorServletHttpResponse)
326
// coc=((DirectorServletHttpResponse)response).getCookies();
327

328    if(coc==null)
329      record.put("CookiesResponse",new Cookie JavaDoc[0]);
330    else
331      record.put("CookiesResponse",coc);
332
333     record.put("StatusCode",new Integer JavaDoc(Integer.parseInt(statusCode)));
334     record.put("Message",message);
335     record.put("TotalBytesResponse",new Integer JavaDoc(-1));
336   String JavaDoc cl=(String JavaDoc)result.get("Content-Length");
337   if(cl!=null)
338    record.put("ContentLengthResponse",new Integer JavaDoc(Integer.parseInt(cl.trim())));
339   else
340   record.put("ContentLengthResponse",new Integer JavaDoc(-1));
341   String JavaDoc ct=(String JavaDoc)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 JavaDoc(Integer.parseInt(statusCode.trim())));
347  
348           // result.keys();
349
String JavaDoc[] hnames=new String JavaDoc[result.size()];
350 int yy=0;
351  for (Enumeration JavaDoc e = result.keys() ; e.hasMoreElements() ;) {
352      String JavaDoc namee=(String JavaDoc)e.nextElement();
353      hnames[yy]=namee;
354      String JavaDoc valuee=(String JavaDoc)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 JavaDoc e){
379        }
380
381 }
382        public void destroy(){
383         servlet=null;
384         }
385
386 }
387
388
389
390
391
392
Popular Tags