View Javadoc

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