1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package net.sourceforge.servletspy.handler.servlet;
24
25 import javax.servlet.ServletContext;
26 import javax.servlet.ServletRequest;
27 import javax.servlet.http.HttpSession;
28 import javax.servlet.jsp.PageContext;
29
30 import net.sourceforge.servletspy.IContext;
31 import net.sourceforge.servletspy.IContextHandler;
32 import net.sourceforge.servletspy.config.Param;
33
34 /***
35 * @author arno schumacher
36 */
37 public final class PageContextHandler implements IContextHandler {
38
39 /***
40 * Returns the name of the class without leading package name.
41 *
42 * @param clazz The class.
43 * @return The name of the class without leading package name.
44 */
45 private static String getSimpleClassName(final Class clazz) {
46 if (clazz == null) {
47 return "";
48 }
49 final String packageName = clazz.getPackage().getName();
50 if (packageName == null || packageName.length() == 0) {
51 return clazz.getName();
52 }
53 return clazz.getName().substring(packageName.length() + 1);
54 }
55
56
57
58
59 public void destroy() {
60 }
61
62
63
64
65 public void handle(final IContext context) throws Exception {
66 if (context.getSubject() instanceof PageContext) {
67 final PageContext pageContext = (PageContext) context
68 .getSubject();
69 context.add(getSimpleClassName(ServletContext.class), pageContext
70 .getServletContext());
71 if (pageContext.getSession() != null) {
72 context.add(getSimpleClassName(HttpSession.class),
73 pageContext.getSession());
74 }
75 context.add(getSimpleClassName(ServletRequest.class), pageContext
76 .getRequest());
77 } else {
78 context.proceed();
79 }
80 }
81
82
83
84
85 public void init(final Param[] params) {
86 }
87 }