View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Implementation of the {@link org.apache.commons.jxpath.DynamicPropertyHandler} interface that provides access to attributes of a @{link HttpSession}.
29   */
30  public class HttpSessionHandler extends ServletContextHandler {
31  
32      /**
33       * Constructs a new instance.
34       */
35      public HttpSessionHandler() {
36          // empty
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  }