1/*2 * $Id: SessionScopeExtractor.java 1066849 2011-02-03 16:12:39Z apetrelli $3 *4 * Licensed to the Apache Software Foundation (ASF) under one5 * or more contributor license agreements. See the NOTICE file6 * distributed with this work for additional information7 * regarding copyright ownership. The ASF licenses this file8 * to you under the Apache License, Version 2.0 (the9 * "License"); you may not use this file except in compliance10 * with the License. You may obtain a copy of the License at11 *12 * http://www.apache.org/licenses/LICENSE-2.013 *14 * Unless required by applicable law or agreed to in writing,15 * software distributed under the License is distributed on an16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY17 * KIND, either express or implied. See the License for the18 * specific language governing permissions and limitations19 * under the License.20 */21package org.apache.tiles.request.portlet.extractor;
2223import java.util.Enumeration;
2425import javax.portlet.PortletRequest;
26import javax.portlet.PortletSession;
2728import org.apache.tiles.request.attribute.AttributeExtractor;
2930/**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 */35publicclassSessionScopeExtractorimplementsAttributeExtractor {
3637/**38 * The portlet request.39 */40private PortletRequest request;
4142/**43 * The subscope (application or portlet).44 */45privateint scope;
4647/**48 * Constructor.49 *50 * @param request The request.51 * @param scope The subscope (application or portlet).52 */53publicSessionScopeExtractor(PortletRequest request, int scope) {
54this.request = request;
55if (scope != PortletSession.APPLICATION_SCOPE
56 && scope != PortletSession.PORTLET_SCOPE) {
57thrownew IllegalArgumentException(
58"The scope must be either APPLICATION_SCOPE or PORTLET_SCOPE");
59 }
60this.scope = scope;
61 }
6263 @Override
64publicvoid setValue(String name, Object value) {
65 request.getPortletSession().setAttribute(name, value, scope);
66 }
6768 @Override
69publicvoid removeValue(String name) {
70 PortletSession session = request.getPortletSession(false);
71if (session != null) {
72 session.removeAttribute(name, scope);
73 }
74 }
7576 @Override
77public Enumeration<String> getKeys() {
78 PortletSession session = request.getPortletSession(false);
79if (session != null) {
80return session.getAttributeNames(scope);
81 }
82returnnull;
83 }
8485 @Override
86public Object getValue(String key) {
87 PortletSession session = request.getPortletSession(false);
88if (session != null) {
89return session.getAttribute(key, scope);
90 }
91returnnull;
92 }
93 }