View Javadoc

1   
2   /*
3    * Copyright (C) 2005 by Arno Schumacher
4    *
5    * This file is part of net.sourceforge.servletspy
6    *
7    * net.sourceforge.servletspy is free software; you can redistribute
8    * it and/or modify it under the terms of the GNU General Public 
9    * License as published by the Free Software Foundation; either 
10   * version 2, or (at your option) any later version.
11   *
12   * net.sourceforge.servletspy is distributed in the hope that it will
13   * be useful, but WITHOUT ANY WARRANTY; without even the implied 
14   * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
15   * See the GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program; if not, write to the Free Software
19   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA 
20   */
21  
22  package net.sourceforge.servletspy.handler.servlet;
23  
24  import java.lang.reflect.InvocationHandler;
25  import java.lang.reflect.Method;
26  import java.lang.reflect.Proxy;
27  import java.util.Arrays;
28  import java.util.Enumeration;
29  import java.util.HashMap;
30  import java.util.List;
31  import java.util.Map;
32  
33  import javax.servlet.http.HttpSession;
34  
35  import net.sourceforge.servletspy.IContext;
36  import net.sourceforge.servletspy.IContextHandler;
37  import net.sourceforge.servletspy.config.Param;
38  
39  /***
40   * @author arno schumacher
41   */
42  public final class SessionContextHandler implements IContextHandler {
43  
44      /*** The name of the session attribute map used for the sub context. */
45      private static final String ATTRIBUTES_KEY_NAME = "~attributes";
46  
47      /***
48       * A list methods which should be excluded while generating a node
49       * representation for the session object. The servlet context and the
50       * session context are already top level nodes, so we will exclude them
51       * here.
52       */
53      private static List excludefilterList = Arrays.asList(new String[] {
54              "getServletContext", "getSessionContext" });
55  
56      /* (non-Javadoc)
57       * @see net.sourceforge.servletspy.ILifecycle#destroy()
58       */
59      public void destroy() {
60      }
61  
62      /***
63       * Returns a map holding all attributes of the provided session object.
64       *
65       * @param session
66       *            The session.
67       * @return The attribute map generated using the provided session.
68       */
69      private Map generateAttributesMap(final HttpSession session) {
70          final Map map = new HashMap();
71          final Enumeration enumeration = session.getAttributeNames();
72          while (enumeration.hasMoreElements()) {
73              final String key = String.valueOf(enumeration.nextElement());
74              final Object value = session.getAttribute(key);
75              map.put(key, value);
76          }
77          return map;
78      }
79  
80      /* (non-Javadoc)
81       * @see net.sourceforge.servletspy.IContextHandler#handle(net.sourceforge.servletspy.IContext)
82       */
83      public void handle(final IContext context) throws Exception {
84          if (context.getSubject() instanceof HttpSession) {
85              final HttpSession session = (HttpSession) context.getSubject();
86              final HttpSession sessionWrapper = (HttpSession) Proxy
87                      .newProxyInstance(context.getClass().getClassLoader(),
88                              new Class[] {HttpSession.class},
89                              new InvocationHandler() {
90                                  public Object invoke(
91                                       final Object proxy,
92                                       final Method method,
93                                       final Object[] args)
94                                             throws Throwable {
95                                      return excludefilterList.contains(method
96                                              .getName()) ? null : method.invoke(
97                                              session, args);
98                                  }
99                              });
100             context.setSubject(sessionWrapper);
101             context.proceed();
102             context.add(ATTRIBUTES_KEY_NAME, generateAttributesMap(session));
103         } else {
104             context.proceed();
105         }
106     }
107 
108     /* (non-Javadoc)
109      * @see net.sourceforge.servletspy.ILifecycle#init(net.sourceforge.servletspy.config.Param[])
110      */
111     public void init(final Param[] params) {
112     }
113 }