1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.jxpath.servlet;
19
20 import java.util.Enumeration;
21 import java.util.HashSet;
22
23 import javax.servlet.http.HttpSession;
24
25 import org.apache.commons.jxpath.JXPathException;
26
27
28
29
30 public class HttpSessionHandler extends ServletContextHandler {
31
32
33
34
35 public HttpSessionHandler() {
36
37 }
38
39 @Override
40 protected void collectPropertyNames(final HashSet<String> set, final Object bean) {
41 final HttpSessionAndServletContext handle = (HttpSessionAndServletContext) bean;
42 super.collectPropertyNames(set, handle.getServletContext());
43 final HttpSession session = handle.getSession();
44 if (session != null) {
45 final Enumeration<String> e = session.getAttributeNames();
46 while (e.hasMoreElements()) {
47 set.add(e.nextElement());
48 }
49 }
50 }
51
52 @Override
53 public Object getProperty(final Object bean, final String property) {
54 final HttpSessionAndServletContext handle = (HttpSessionAndServletContext) bean;
55 final HttpSession session = handle.getSession();
56 if (session != null) {
57 final Object object = session.getAttribute(property);
58 if (object != null) {
59 return object;
60 }
61 }
62 return super.getProperty(handle.getServletContext(), property);
63 }
64
65 @Override
66 public void setProperty(final Object bean, final String property, final Object value) {
67 final HttpSessionAndServletContext handle = (HttpSessionAndServletContext) bean;
68 final HttpSession session = handle.getSession();
69 if (session == null) {
70 throw new JXPathException("Cannot set session attribute: " + "there is no session");
71 }
72 session.setAttribute(property, value);
73 }
74 }