spring-session SessionEventHttpSessionListenerAdapter 源码

  • 2022-08-16
  • 浏览 (404)

spring-session SessionEventHttpSessionListenerAdapter 代码

文件路径:/spring-session-core/src/main/java/org/springframework/session/web/http/SessionEventHttpSessionListenerAdapter.java

/*
 * Copyright 2014-2021 the original author or authors.
 *
 * 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
 *
 *      https://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.springframework.session.web.http;

import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.springframework.context.ApplicationListener;
import org.springframework.session.Session;
import org.springframework.session.events.AbstractSessionEvent;
import org.springframework.session.events.SessionCreatedEvent;
import org.springframework.session.events.SessionDestroyedEvent;
import org.springframework.web.context.ServletContextAware;

/**
 * Receives {@link SessionDestroyedEvent} and {@link SessionCreatedEvent} and translates
 * them into {@link HttpSessionEvent} and submits the {@link HttpSessionEvent} to every
 * registered {@link HttpSessionListener}.
 *
 * @author Rob Winch
 * @since 1.1
 */
public class SessionEventHttpSessionListenerAdapter
		implements ApplicationListener<AbstractSessionEvent>, ServletContextAware {

	private final List<HttpSessionListener> listeners;

	private ServletContext context;

	public SessionEventHttpSessionListenerAdapter(List<HttpSessionListener> listeners) {
		super();
		this.listeners = listeners;
	}

	/*
	 * @see org.springframework.context.ApplicationListener#onApplicationEvent(org.
	 * springframework.context.ApplicationEvent)
	 */
	@Override
	public void onApplicationEvent(AbstractSessionEvent event) {
		if (this.listeners.isEmpty()) {
			return;
		}

		HttpSessionEvent httpSessionEvent = createHttpSessionEvent(event);

		for (HttpSessionListener listener : this.listeners) {
			if (event instanceof SessionDestroyedEvent) {
				listener.sessionDestroyed(httpSessionEvent);
			}
			else if (event instanceof SessionCreatedEvent) {
				listener.sessionCreated(httpSessionEvent);
			}
		}
	}

	private HttpSessionEvent createHttpSessionEvent(AbstractSessionEvent event) {
		Session session = event.getSession();
		HttpSession httpSession = new HttpSessionAdapter<>(session, this.context);
		return new HttpSessionEvent(httpSession);
	}

	/*
	 * @see
	 * org.springframework.web.context.ServletContextAware#setServletContext(javax.servlet
	 * .ServletContext)
	 */
	@Override
	public void setServletContext(ServletContext servletContext) {
		this.context = servletContext;
	}

}

相关信息

spring-session 源码目录

相关文章

spring-session CookieHttpSessionIdResolver 源码

spring-session CookieSerializer 源码

spring-session DefaultCookieSerializer 源码

spring-session HeaderHttpSessionIdResolver 源码

spring-session HttpSessionAdapter 源码

spring-session HttpSessionIdResolver 源码

spring-session OnCommittedResponseWrapper 源码

spring-session OncePerRequestFilter 源码

spring-session SessionRepositoryFilter 源码

0  赞