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  package org.apache.portals.bridges.jsf;
18  
19  import java.util.Enumeration;
20  
21  import javax.portlet.PortletRequest;
22  import javax.portlet.PortletSession;
23  
24  import org.apache.portals.bridges.jsf.AbstractAttributeMap;
25  import org.apache.portals.bridges.jsf.NullEnumeration;
26  
27  /***
28   * <p>
29   * Session attibutes as Map.
30   * </p>
31   * <p>
32   * See MyFaces project for servlet implementation.
33   * </p>
34   * 
35   * @author <a href="dlestrat@apache.org">David Le Strat </a>
36   */
37  public class SessionMap extends AbstractAttributeMap
38  {
39      /*** Illegal argument exception message. */
40      final private static String ILLEGAL_ARGUMENT = "Only PortletContext supported";
41  
42      /*** The {@link PortletRequest}. */
43      private final PortletRequest portletRequest;
44  
45      /***
46       * @param request The request.
47       */
48      public SessionMap(Object request)
49      {
50          if (request instanceof PortletRequest)
51          {
52              this.portletRequest = (PortletRequest) request;
53          }
54          else
55          {
56              throw new IllegalArgumentException(ILLEGAL_ARGUMENT);
57          }
58      }
59  
60      /***
61       * @see org.apache.portals.bridges.jsf.AbstractAttributeMap#getAttribute(java.lang.String)
62       */
63      protected Object getAttribute(String key)
64      {
65          if (null != this.portletRequest)
66          {
67              PortletSession portletSession = this.portletRequest.getPortletSession(false);
68              return (portletSession == null) ? null : portletSession.getAttribute(key.toString());
69          }
70          else
71          {
72              throw new IllegalArgumentException(ILLEGAL_ARGUMENT);
73          }
74      }
75  
76      /***
77       * @see org.apache.portals.bridges.jsf.AbstractAttributeMap#setAttribute(java.lang.String,
78       *      java.lang.Object)
79       */
80      protected void setAttribute(String key, Object value)
81      {
82          if (null != this.portletRequest)
83          {
84              this.portletRequest.getPortletSession(true).setAttribute(key, value);
85          }
86      }
87  
88      /***
89       * @see org.apache.portals.bridges.jsf.AbstractAttributeMap#removeAttribute(java.lang.String)
90       */
91      protected void removeAttribute(String key)
92      {
93          if (null != this.portletRequest)
94          {
95              PortletSession portletSession = this.portletRequest.getPortletSession(false);
96              ;
97              if (null != portletSession)
98              {
99                  portletSession.removeAttribute(key);
100             }
101         }
102     }
103 
104     /***
105      * @see org.apache.portals.bridges.jsf.AbstractAttributeMap#getAttributeNames()
106      */
107     protected Enumeration getAttributeNames()
108     {
109         if (null != this.portletRequest)
110         {
111             PortletSession portletSession = this.portletRequest.getPortletSession(false);
112             ;
113             return (portletSession == null) ? NullEnumeration.instance() : portletSession.getAttributeNames();
114         }
115         else
116         {
117             throw new IllegalArgumentException(ILLEGAL_ARGUMENT);
118         }
119     }
120 
121 }