View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.context.servlet;
20  
21  import java.util.Enumeration;
22  import java.util.Map;
23  
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpSession;
26  
27  import org.apache.myfaces.shared_impl.util.NullEnumeration;
28  import org.apache.myfaces.util.AbstractThreadSafeAttributeMap;
29  
30  
31  /**
32   * HttpSession attibutes as Map.
33   *
34   * @author Anton Koinov (latest modification by $Author: lu4242 $)
35   * @version $Revision: 1000660 $ $Date: 2010-09-23 18:02:43 -0500 (Thu, 23 Sep 2010) $
36   */
37  public class SessionMap extends AbstractThreadSafeAttributeMap
38  {
39      private final HttpServletRequest _httpRequest;
40  
41      SessionMap(HttpServletRequest httpRequest)
42      {
43          _httpRequest = httpRequest;
44      }
45  
46      protected Object getAttribute(String key)
47      {
48          HttpSession httpSession = getSession();
49          return (httpSession == null)
50              ? null : httpSession.getAttribute(key.toString());
51      }
52  
53      protected void setAttribute(String key, Object value)
54      {
55          _httpRequest.getSession(true).setAttribute(key, value);
56      }
57  
58      protected void removeAttribute(String key)
59      {
60          HttpSession httpSession = getSession();
61          if (httpSession != null)
62          {
63              httpSession.removeAttribute(key);
64          }
65      }
66  
67      protected Enumeration getAttributeNames()
68      {
69          HttpSession httpSession = getSession();
70          return (httpSession == null)
71              ? NullEnumeration.instance()
72              : httpSession.getAttributeNames();
73      }
74  
75      private HttpSession getSession()
76      {
77          return _httpRequest.getSession(false);
78      }
79  
80  
81      public void putAll(Map t)
82      {
83          throw new UnsupportedOperationException();
84      }
85  
86  
87      public void clear()
88      {
89          throw new UnsupportedOperationException();
90      }
91  }