1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.jxpath.servlet;
  17. import java.util.Enumeration;
  18. import java.util.HashSet;
  19. import javax.servlet.http.HttpSession;
  20. import org.apache.commons.jxpath.JXPathException;
  21. /**
  22. * Implementation of the DynamicPropertyHandler interface that provides
  23. * access to attributes of a HttpSession.
  24. *
  25. * @author Dmitri Plotnikov
  26. * @version $Revision: 1.6 $ $Date: 2004/05/08 15:10:49 $
  27. */
  28. public class HttpSessionHandler extends ServletContextHandler {
  29. protected void collectPropertyNames(HashSet set, Object bean) {
  30. HttpSessionAndServletContext handle =
  31. (HttpSessionAndServletContext) bean;
  32. super.collectPropertyNames(set, handle.getServletContext());
  33. HttpSession session = handle.getSession();
  34. if (session != null) {
  35. Enumeration e = session.getAttributeNames();
  36. while (e.hasMoreElements()) {
  37. set.add(e.nextElement());
  38. }
  39. }
  40. }
  41. public Object getProperty(Object bean, String property) {
  42. HttpSessionAndServletContext handle =
  43. (HttpSessionAndServletContext) bean;
  44. HttpSession session = handle.getSession();
  45. if (session != null) {
  46. Object object = session.getAttribute(property);
  47. if (object != null) {
  48. return object;
  49. }
  50. }
  51. return super.getProperty(handle.getServletContext(), property);
  52. }
  53. public void setProperty(Object bean, String property, Object value) {
  54. HttpSessionAndServletContext handle =
  55. (HttpSessionAndServletContext) bean;
  56. HttpSession session = handle.getSession();
  57. if (session != null) {
  58. session.setAttribute(property, value);
  59. }
  60. else {
  61. throw new JXPathException("Cannot set session attribute: "
  62. + "there is no session");
  63. }
  64. }
  65. }