1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
57
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
81
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
109
110
111 public void init(final Param[] params) {
112 }
113 }