package org.glassfish.grizzly.servlet;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;
import java.util.Collections;
import java.util.Enumeration;
import java.util.EventListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.glassfish.grizzly.Grizzly;
import org.glassfish.grizzly.http.server.Session;
import org.glassfish.grizzly.localization.LogMessages;
import jakarta.servlet.ServletContext;
import jakarta.servlet.http.HttpSession;
import jakarta.servlet.http.HttpSessionAttributeListener;
import jakarta.servlet.http.HttpSessionBindingEvent;
import jakarta.servlet.http.HttpSessionBindingListener;
import jakarta.servlet.http.HttpSessionEvent;
import jakarta.servlet.http.HttpSessionIdListener;
import jakarta.servlet.http.HttpSessionListener;
@SuppressWarnings("deprecation")
public class HttpSessionImpl implements HttpSession {
private static final Logger LOGGER = Grizzly.logger(HttpSessionImpl.class);
private final Session session;
private final WebappContext contextImpl;
public HttpSessionImpl(final WebappContext contextImpl, final Session session) {
this.contextImpl = contextImpl;
this.session = session;
}
@Override
public long getCreationTime() {
if (!session.isValid()) {
throw new IllegalStateException("The session was invalidated");
}
return session.getCreationTime();
}
@Override
public String getId() {
return session.getIdInternal();
}
protected boolean isValid() {
return session.isValid();
}
@Override
public long getLastAccessedTime() {
if (!session.isValid()) {
throw new IllegalStateException("The session was invalidated");
}
return session.getTimestamp();
}
protected void access() {
session.access();
}
@Override
public ServletContext getServletContext() {
return contextImpl;
}
@Override
public void setMaxInactiveInterval(int sessionTimeout) {
if (sessionTimeout < 0) {
sessionTimeout = -1;
} else {
sessionTimeout = (int) MILLISECONDS.convert(sessionTimeout, SECONDS);
}
session.setSessionTimeout(sessionTimeout);
}
@Override
public int getMaxInactiveInterval() {
long sessionTimeout = session.getSessionTimeout();
if (sessionTimeout < 0) {
return -1;
}
sessionTimeout = SECONDS.convert(sessionTimeout, MILLISECONDS);
if (sessionTimeout > Integer.MAX_VALUE) {
throw new IllegalArgumentException(sessionTimeout + " cannot be cast to int.");
}
return (int) sessionTimeout;
}
@Override
public jakarta.servlet.http.HttpSessionContext getSessionContext() {
return null;
}
@Override
public Object getAttribute(String key) {
return session.getAttribute(key);
}
@Override
public Object getValue(String value) {
return session.getAttribute(value);
}
@Override
public Enumeration<String> getAttributeNames() {
return Collections.enumeration(session.attributes().keySet());
}
@Override
public String[] getValueNames() {
return session.attributes().entrySet().toArray(new String[session.attributes().size()]);
}
@Override
public void setAttribute(String key, Object value) {
if (value == null) {
removeAttribute(key);
return;
}
Object unbound = session.getAttribute(key);
session.setAttribute(key, value);
if (unbound != null && unbound != value && unbound instanceof HttpSessionBindingListener) {
try {
((HttpSessionBindingListener) unbound).valueUnbound(new HttpSessionBindingEvent(this, key));
} catch (Throwable t) {
if (LOGGER.isLoggable(Level.WARNING)) {
LOGGER.log(Level.WARNING, LogMessages.WARNING_GRIZZLY_HTTP_SERVLET_SESSION_LISTENER_UNBOUND_ERROR(unbound.getClass().getName()));
}
}
}
HttpSessionBindingEvent event = null;
if (value instanceof HttpSessionBindingListener) {
if (value != unbound) {
event = new HttpSessionBindingEvent(this, key, value);
try {
((HttpSessionBindingListener) value).valueBound(event);
} catch (Throwable t) {
if (LOGGER.isLoggable(Level.WARNING)) {
LOGGER.log(Level.WARNING, LogMessages.WARNING_GRIZZLY_HTTP_SERVLET_SESSION_LISTENER_BOUND_ERROR(value.getClass().getName()));
}
}
}
}
EventListener[] listeners = contextImpl.getEventListeners();
if (listeners.length == 0) {
return;
}
for (int i = 0, len = listeners.length; i < len; i++) {
if (!(listeners[i] instanceof HttpSessionAttributeListener)) {
continue;
}
HttpSessionAttributeListener listener = (HttpSessionAttributeListener) listeners[i];
try {
if (unbound != null) {
if (event == null) {
event = new HttpSessionBindingEvent(this, key, unbound);
}
listener.attributeReplaced(event);
} else {
if (event == null) {
event = new HttpSessionBindingEvent(this, key, value);
}
listener.attributeAdded(event);
}
} catch (Throwable t) {
if (LOGGER.isLoggable(Level.WARNING)) {
LOGGER.log(Level.WARNING, LogMessages.WARNING_GRIZZLY_HTTP_SERVLET_ATTRIBUTE_LISTENER_ADD_ERROR("HttpSessionAttributeListener",
listener.getClass().getName()), t);
}
}
}
}
@Override
public void putValue(String key, Object value) {
setAttribute(key, value);
}
@Override
public void removeAttribute(String key) {
Object value = session.removeAttribute(key);
if (value == null) {
return;
}
HttpSessionBindingEvent event = null;
if (value instanceof HttpSessionBindingListener) {
event = new HttpSessionBindingEvent(this, key, value);
((HttpSessionBindingListener) value).valueUnbound(event);
}
EventListener[] listeners = contextImpl.getEventListeners();
if (listeners.length == 0) {
return;
}
for (int i = 0, len = listeners.length; i < len; i++) {
if (!(listeners[i] instanceof HttpSessionAttributeListener)) {
continue;
}
HttpSessionAttributeListener listener = (HttpSessionAttributeListener) listeners[i];
try {
if (event == null) {
event = new HttpSessionBindingEvent(this, key, value);
}
listener.attributeRemoved(event);
} catch (Throwable t) {
if (LOGGER.isLoggable(Level.WARNING)) {
LOGGER.log(Level.WARNING, LogMessages.WARNING_GRIZZLY_HTTP_SERVLET_ATTRIBUTE_LISTENER_REMOVE_ERROR("HttpSessionAttributeListener",
listener.getClass().getName()), t);
}
}
}
}
@Override
public void removeValue(String key) {
removeAttribute(key);
}
@Override
public synchronized void invalidate() {
session.setValid(false);
session.attributes().clear();
EventListener[] listeners = contextImpl.getEventListeners();
if (listeners.length > 0) {
HttpSessionEvent event = new HttpSessionEvent(this);
for (int i = 0, len = listeners.length; i < len; i++) {
Object listenerObj = listeners[i];
if (!(listenerObj instanceof HttpSessionListener)) {
continue;
}
HttpSessionListener listener = (HttpSessionListener) listenerObj;
try {
listener.sessionDestroyed(event);
} catch (Throwable t) {
if (LOGGER.isLoggable(Level.WARNING)) {
LOGGER.log(Level.WARNING, LogMessages.WARNING_GRIZZLY_HTTP_SERVLET_CONTAINER_OBJECT_DESTROYED_ERROR("sessionDestroyed",
"HttpSessionListener", listener.getClass().getName()), t);
}
}
}
}
}
@Override
public boolean isNew() {
if (!session.isValid()) {
throw new IllegalStateException("The session was invalidated");
}
return session.isNew();
}
protected void notifyNew() {
EventListener[] listeners = contextImpl.getEventListeners();
if (listeners.length > 0) {
HttpSessionEvent event = new HttpSessionEvent(this);
for (int i = 0, len = listeners.length; i < len; i++) {
Object listenerObj = listeners[i];
if (!(listenerObj instanceof HttpSessionListener)) {
continue;
}
HttpSessionListener listener = (HttpSessionListener) listenerObj;
try {
listener.sessionCreated(event);
} catch (Throwable t) {
if (LOGGER.isLoggable(Level.WARNING)) {
LOGGER.log(Level.WARNING, LogMessages.WARNING_GRIZZLY_HTTP_SERVLET_CONTAINER_OBJECT_INITIALIZED_ERROR("sessionCreated",
"HttpSessionListener", listener.getClass().getName()), t);
}
}
}
}
}
protected void notifyIdChanged(final String oldId) {
EventListener[] listeners = contextImpl.getEventListeners();
if (listeners.length > 0) {
HttpSessionEvent event = new HttpSessionEvent(this);
for (int i = 0, len = listeners.length; i < len; i++) {
Object listenerObj = listeners[i];
if (!(listenerObj instanceof HttpSessionIdListener)) {
continue;
}
HttpSessionIdListener listener = (HttpSessionIdListener) listenerObj;
try {
listener.sessionIdChanged(event, oldId);
} catch (Throwable t) {
if (LOGGER.isLoggable(Level.WARNING)) {
LOGGER.log(Level.WARNING, LogMessages.WARNING_GRIZZLY_HTTP_SERVLET_CONTAINER_OBJECT_INITIALIZED_ERROR("sessionCreated",
"HttpSessionListener", listener.getClass().getName()), t);
}
}
}
}
}
}