org.thymeleaf.context.WebSessionVariablesMap Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of thymeleaf Show documentation
Show all versions of thymeleaf Show documentation
XML/XHTML/HTML5 template engine for Java
/*
* =============================================================================
*
* Copyright (c) 2011-2014, The THYMELEAF team (http://www.thymeleaf.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* =============================================================================
*/
package org.thymeleaf.context;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
/**
*
* Specialization of {@link VariablesMap} that forwards read and write
* calls to the attributes of an HttpSession.
*
*
* @author Daniel Fernández
*
* @since 2.0.9
*
*/
class WebSessionVariablesMap extends VariablesMap {
private static final long serialVersionUID = 3866833203758601975L;
private final HttpServletRequest request;
WebSessionVariablesMap(final HttpServletRequest request) {
super(1, 1.0f);
this.request = request;
}
@Override
@SuppressWarnings("unchecked")
public int size() {
final HttpSession session = this.request.getSession(false);
if (session == null) {
return 0;
}
int size = 0;
final Enumeration attributeNames = session.getAttributeNames();
while (attributeNames.hasMoreElements()) {
attributeNames.nextElement();
size++;
}
return size;
}
@Override
public boolean isEmpty() {
final HttpSession session = this.request.getSession(false);
return session == null || !session.getAttributeNames().hasMoreElements();
}
@Override
public Object get(final Object key) {
final HttpSession session = this.request.getSession(false);
if (session == null) {
return null;
}
return session.getAttribute((String)key);
}
@Override
@SuppressWarnings("unchecked")
public boolean containsKey(final Object key) {
final HttpSession session = this.request.getSession(false);
if (session == null) {
return false;
}
final Enumeration attributeNames = session.getAttributeNames();
while (attributeNames.hasMoreElements()) {
final String attributeName = attributeNames.nextElement();
if (key == null) {
if (attributeName == null) {
return true;
}
} else {
if (key.equals(attributeName)) {
return true;
}
}
}
return false;
}
@Override
public Object put(final String key, final Object value) {
final HttpSession session = this.request.getSession(false);
if (session == null) {
throw new IllegalStateException(
"Cannot put attribute into session: no HTTP session exists!");
}
session.setAttribute(key, value);
return value;
}
@Override
public void putAll(final Map m) {
final HttpSession session = this.request.getSession(false);
if (session == null) {
throw new IllegalStateException(
"Cannot put attributes into session: no HTTP session exists!");
}
for (final Map.Entry mEntry : m.entrySet()) {
session.setAttribute(mEntry.getKey(), mEntry.getValue());
}
}
@Override
public Object remove(final Object key) {
final HttpSession session = this.request.getSession(false);
if (session == null) {
throw new IllegalStateException(
"Cannot remove attribute from session: no HTTP session exists!");
}
final Object value = session.getAttribute((String)key);
session.removeAttribute((String)key);
return value;
}
@Override
@SuppressWarnings("unchecked")
public void clear() {
final HttpSession session = this.request.getSession(false);
if (session == null) {
throw new IllegalStateException(
"Cannot remove attribute from session: no HTTP session exists!");
}
final List attributeNamesList = new ArrayList(5);
final Enumeration attributeNames = session.getAttributeNames();
while (attributeNames.hasMoreElements()) {
attributeNamesList.add(attributeNames.nextElement());
}
for (final String attributeName : attributeNamesList) {
session.removeAttribute(attributeName);
}
}
@Override
@SuppressWarnings("unchecked")
public boolean containsValue(final Object value) {
final HttpSession session = this.request.getSession(false);
if (session == null) {
return false;
}
final Enumeration attributeNames = session.getAttributeNames();
while (attributeNames.hasMoreElements()) {
final String attributeName = attributeNames.nextElement();
final Object attributeValue = session.getAttribute(attributeName);
if (value == null) {
if (attributeValue == null) {
return true;
}
} else {
if (value.equals(attributeValue)) {
return true;
}
}
}
return false;
}
@Override
public WebSessionVariablesMap clone() {
return (WebSessionVariablesMap) super.clone();
}
@Override
@SuppressWarnings("unchecked")
public Set keySet() {
final HttpSession session = this.request.getSession(false);
if (session == null) {
return Collections.emptySet();
}
final Set keySet = new LinkedHashSet(5);
final Enumeration attributeNames = session.getAttributeNames();
while (attributeNames.hasMoreElements()) {
keySet.add(attributeNames.nextElement());
}
return keySet;
}
@Override
@SuppressWarnings("unchecked")
public Collection