1/*2 * $Id: SessionScopeExtractor.java 1199216 2011-11-08 12:25:24Z mck $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.servlet.extractor;
2223import java.util.Enumeration;
24import java.util.Collections;
2526import javax.servlet.http.HttpServletRequest;
27import javax.servlet.http.HttpSession;
2829import org.apache.tiles.request.attribute.AttributeExtractor;
3031/**32 * Extract attributes from session scope.33 *34 * @version $Rev: 1199216 $ $Date: 2011-11-08 23:25:24 +1100 (Tue, 08 Nov 2011) $35 */36publicclassSessionScopeExtractorimplementsAttributeExtractor {
3738/**39 * The servlet request.40 */41private HttpServletRequest request;
4243/**44 * Constructor.45 *46 * @param request The servlet request.47 */48publicSessionScopeExtractor(HttpServletRequest request) {
49this.request = request;
50 }
5152 @Override
53publicvoid setValue(String name, Object value) {
54 request.getSession().setAttribute(name, value);
55 }
5657 @Override
58publicvoid removeValue(String name) {
59 HttpSession session = request.getSession(false);
60if (session != null) {
61 session.removeAttribute(name);
62 }
63 }
6465 @SuppressWarnings("unchecked")
66 @Override
67public Enumeration<String> getKeys() {
68 HttpSession session = request.getSession(false);
69if (session != null) {
70return session.getAttributeNames();
71 }
72return Collections.enumeration(Collections.<String>emptySet());
73 }
7475 @Override
76public Object getValue(String key) {
77 HttpSession session = request.getSession(false);
78if (session != null) {
79return session.getAttribute(key);
80 }
81returnnull;
82 }
83 }