All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.qwazr.webapps.WebappHttpSessionImpl Maven / Gradle / Ivy

There is a newer version: 1.5.0
Show newest version
/**
 * Copyright 2014-2016 Emmanuel Keller / QWAZR
 * 

* 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 com.qwazr.webapps; import javax.servlet.ServletContext; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSessionContext; import java.util.*; public class WebappHttpSessionImpl implements WebappHttpSession { private final HttpSession session; private final AttributesMap attributesMap; WebappHttpSessionImpl(HttpSession session) { this.session = session; this.attributesMap = new AttributesMap(); } @Override public long getCreationTime() { return session.getCreationTime(); } @Override public String getId() { return session.getId(); } @Override public long getLastAccessedTime() { return session.getLastAccessedTime(); } @Override public ServletContext getServletContext() { return null; } @Override public void setMaxInactiveInterval(int interval) { session.setMaxInactiveInterval(interval); } @Override public int getMaxInactiveInterval() { return session.getMaxInactiveInterval(); } @Override @Deprecated public HttpSessionContext getSessionContext() { return null; } @Override public Object getAttribute(String name) { return session.getAttribute(name); } @Override @Deprecated public Object getValue(String name) { return session.getValue(name); } @Override public Enumeration getAttributeNames() { return session.getAttributeNames(); } @Override @Deprecated public String[] getValueNames() { return session.getValueNames(); } @Override public void setAttribute(String name, Object value) { session.setAttribute(name, value); } @Override @Deprecated public void putValue(String name, Object value) { session.putValue(name, value); } @Override public boolean isAttribute(String name) { return session.getAttribute(name) != null; } @Override public void removeAttribute(String name) { session.removeAttribute(name); } @Override @Deprecated public void removeValue(String name) { session.removeValue(name); } @Override public void invalidate() { session.invalidate(); } @Override public boolean isNew() { return session.isNew(); } @Override public Map getAttributes() { return attributesMap; } private class AttributesMap implements Map { @Override public int size() { int i = 0; Enumeration e = session.getAttributeNames(); if (e == null) return 0; while (e.hasMoreElements()) { e.nextElement(); i++; } return i; } @Override public boolean isEmpty() { Enumeration e = session.getAttributeNames(); if (e == null) return true; return !e.hasMoreElements(); } @Override public boolean containsKey(Object key) { if (key == null) return false; return isAttribute(key.toString()); } @Override public boolean containsValue(Object value) { if (value == null) return false; Enumeration e = session.getAttributeNames(); if (e == null) return false; while (e.hasMoreElements()) if (value.equals(e.nextElement())) return true; return false; } @Override public Object get(Object key) { if (key == null) return null; return session.getAttribute(key.toString()); } @Override public Object put(String key, Object value) { Object old = session.getAttribute(key); session.setAttribute(key, value); return old; } @Override public Object remove(Object key) { if (key == null) return null; String attr = key.toString(); Object old = session.getAttribute(attr); session.removeAttribute(attr); return old; } @Override public void putAll(Map m) { if (m == null) return; m.forEach((key, value) -> put(key, value)); } @Override public void clear() { Enumeration e = session.getAttributeNames(); if (e == null) return; while (e.hasMoreElements()) removeAttribute(e.nextElement()); } @Override public Set keySet() { LinkedHashSet set = new LinkedHashSet(); Enumeration e = session.getAttributeNames(); if (e != null) while (e.hasMoreElements()) set.add(e.nextElement()); return set; } @Override public Collection values() { ArrayList values = new ArrayList<>(); Enumeration e = session.getAttributeNames(); if (e != null) while (e.hasMoreElements()) values.add(session.getAttribute(e.nextElement())); return values; } @Override public Set> entrySet() { LinkedHashMap map = new LinkedHashMap(); Enumeration e = session.getAttributeNames(); if (e != null) { while (e.hasMoreElements()) { String key = e.nextElement(); map.put(key, session.getAttribute(key)); } } return map.entrySet(); } } }