This project has retired. For details please refer to its Attic page.
SessionScopeExtractor xref
View Javadoc

1   /*
2    * $Id: SessionScopeExtractor.java 1066849 2011-02-03 16:12:39Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.tiles.request.portlet.extractor;
22  
23  import java.util.Enumeration;
24  
25  import javax.portlet.PortletRequest;
26  import javax.portlet.PortletSession;
27  
28  import org.apache.tiles.request.attribute.AttributeExtractor;
29  
30  /**
31   * Extracts attributes from the session scope of a portlet request.
32   *
33   * @version $Rev: 1066849 $ $Date: 2011-02-04 03:12:39 +1100 (Fri, 04 Feb 2011) $
34   */
35  public class SessionScopeExtractor implements AttributeExtractor {
36  
37      /**
38       * The portlet request.
39       */
40      private PortletRequest request;
41  
42      /**
43       * The subscope (application or portlet).
44       */
45      private int scope;
46  
47      /**
48       * Constructor.
49       *
50       * @param request The request.
51       * @param scope The subscope (application or portlet).
52       */
53      public SessionScopeExtractor(PortletRequest request, int scope) {
54          this.request = request;
55          if (scope != PortletSession.APPLICATION_SCOPE
56                  && scope != PortletSession.PORTLET_SCOPE) {
57              throw new IllegalArgumentException(
58                      "The scope must be either APPLICATION_SCOPE or PORTLET_SCOPE");
59          }
60          this.scope = scope;
61      }
62  
63      @Override
64      public void setValue(String name, Object value) {
65          request.getPortletSession().setAttribute(name, value, scope);
66      }
67  
68      @Override
69      public void removeValue(String name) {
70          PortletSession session = request.getPortletSession(false);
71          if (session != null) {
72              session.removeAttribute(name, scope);
73          }
74      }
75  
76      @Override
77      public Enumeration<String> getKeys() {
78          PortletSession session = request.getPortletSession(false);
79          if (session != null) {
80              return session.getAttributeNames(scope);
81          }
82          return null;
83      }
84  
85      @Override
86      public Object getValue(String key) {
87          PortletSession session = request.getPortletSession(false);
88          if (session != null) {
89              return session.getAttribute(key, scope);
90          }
91          return null;
92      }
93  }