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  
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      /* (non-Javadoc)
57       * @see net.sourceforge.servletspy.ILifecycle#destroy()
58       */
59      public void destroy() {
60      }
61  
62      /* (non-Javadoc)
63       * @see net.sourceforge.servletspy.IContextHandler#handle(net.sourceforge.servletspy.IContext)
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      /* (non-Javadoc)
83       * @see net.sourceforge.servletspy.ILifecycle#init(net.sourceforge.servletspy.config.Param[])
84       */
85      public void init(final Param[] params) {
86      }
87  }