From 7693d0e6248e209d26da6ee8ed784fa3fda4d33b Mon Sep 17 00:00:00 2001 From: Rob Winch Date: Tue, 11 Aug 2015 10:52:57 -0500 Subject: [PATCH] Extract out ExpiringSessionHttpSession This allows the HttpSession Spring Session adapter to be reused for HttpSessionEvent. Issue gh-4 --- .../web/http/ExpiringSessionHttpSession.java | 162 ++++++++++++++++++ .../web/http/SessionRepositoryFilter.java | 134 +-------------- 2 files changed, 171 insertions(+), 125 deletions(-) create mode 100644 spring-session/src/main/java/org/springframework/session/web/http/ExpiringSessionHttpSession.java diff --git a/spring-session/src/main/java/org/springframework/session/web/http/ExpiringSessionHttpSession.java b/spring-session/src/main/java/org/springframework/session/web/http/ExpiringSessionHttpSession.java new file mode 100644 index 0000000..5171708 --- /dev/null +++ b/spring-session/src/main/java/org/springframework/session/web/http/ExpiringSessionHttpSession.java @@ -0,0 +1,162 @@ +/* + * Copyright 2002-2015 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 + * + * 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.springframework.session.web.http; + +import java.util.Collections; +import java.util.Enumeration; +import java.util.NoSuchElementException; +import java.util.Set; + +import javax.servlet.ServletContext; +import javax.servlet.http.HttpSession; +import javax.servlet.http.HttpSessionContext; + +import org.springframework.session.ExpiringSession; + +/** + * Adapts Spring Session's {@link ExpiringSession} to an {@link HttpSession}. + * + * @author Rob Winch + * @since 1.1 + */ +@SuppressWarnings("deprecation") +class ExpiringSessionHttpSession implements HttpSession { + private S session; + private final ServletContext servletContext; + private boolean invalidated; + private boolean old; + + public ExpiringSessionHttpSession(S session, ServletContext servletContext) { + this.session = session; + this.servletContext = servletContext; + } + + public void setSession(S session) { + this.session = session; + } + + public S getSession() { + return session; + } + + public long getCreationTime() { + checkState(); + return session.getCreationTime(); + } + + public String getId() { + return session.getId(); + } + + public long getLastAccessedTime() { + checkState(); + return session.getLastAccessedTime(); + } + + public ServletContext getServletContext() { + return servletContext; + } + + public void setMaxInactiveInterval(int interval) { + session.setMaxInactiveIntervalInSeconds(interval); + } + + public int getMaxInactiveInterval() { + return session.getMaxInactiveIntervalInSeconds(); + } + + public HttpSessionContext getSessionContext() { + return NOOP_SESSION_CONTEXT; + } + + public Object getAttribute(String name) { + checkState(); + return session.getAttribute(name); + } + + public Object getValue(String name) { + return getAttribute(name); + } + + public Enumeration getAttributeNames() { + checkState(); + return Collections.enumeration(session.getAttributeNames()); + } + + public String[] getValueNames() { + checkState(); + Set attrs = session.getAttributeNames(); + return attrs.toArray(new String[0]); + } + + public void setAttribute(String name, Object value) { + checkState(); + session.setAttribute(name, value); + } + + public void putValue(String name, Object value) { + setAttribute(name, value); + } + + public void removeAttribute(String name) { + checkState(); + session.removeAttribute(name); + } + + public void removeValue(String name) { + removeAttribute(name); + } + + public void invalidate() { + checkState(); + this.invalidated = true; + } + + public void setNew(boolean isNew) { + this.old = !isNew; + } + + public boolean isNew() { + checkState(); + return !old; + } + + private void checkState() { + if(invalidated) { + throw new IllegalStateException("The HttpSession has already be invalidated."); + } + } + + private static final HttpSessionContext NOOP_SESSION_CONTEXT = new HttpSessionContext() { + public HttpSession getSession(String sessionId) { + return null; + } + + public Enumeration getIds() { + return EMPTY_ENUMERATION; + } + }; + + private static final Enumeration EMPTY_ENUMERATION = new Enumeration() { + public boolean hasMoreElements() { + return false; + } + + public String nextElement() { + throw new NoSuchElementException("a"); + } + }; +} diff --git a/spring-session/src/main/java/org/springframework/session/web/http/SessionRepositoryFilter.java b/spring-session/src/main/java/org/springframework/session/web/http/SessionRepositoryFilter.java index c64b802..39d6a32 100644 --- a/spring-session/src/main/java/org/springframework/session/web/http/SessionRepositoryFilter.java +++ b/spring-session/src/main/java/org/springframework/session/web/http/SessionRepositoryFilter.java @@ -16,12 +16,9 @@ package org.springframework.session.web.http; import java.io.IOException; -import java.util.Collections; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; -import java.util.NoSuchElementException; -import java.util.Set; import javax.servlet.FilterChain; import javax.servlet.ServletContext; @@ -30,7 +27,6 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; -import javax.servlet.http.HttpSessionContext; import org.springframework.core.annotation.Order; import org.springframework.session.ExpiringSession; @@ -63,7 +59,6 @@ import org.springframework.session.SessionRepository; * @since 1.0 * @author Rob Winch */ -@SuppressWarnings("deprecation") @Order(SessionRepositoryFilter.DEFAULT_ORDER) public class SessionRepositoryFilter extends OncePerRequestFilter { public static final String SESSION_REPOSITORY_ATTR = SessionRepository.class.getName(); @@ -189,7 +184,7 @@ public class SessionRepositoryFilter extends OncePerR httpSessionStrategy.onInvalidateSession(this, response); } } else { - S session = wrappedSession.session; + S session = wrappedSession.getSession(); sessionRepository.save(session); if(!isRequestedSessionIdValid() || !session.getId().equals(getRequestedSessionId())) { httpSessionStrategy.onNewSession(session, this, response); @@ -210,7 +205,7 @@ public class SessionRepositoryFilter extends OncePerR } } - @SuppressWarnings({ "unused", "unchecked" }) + @SuppressWarnings("unused") public String changeSessionId() { HttpSession session = getSession(false); @@ -232,8 +227,8 @@ public class SessionRepositoryFilter extends OncePerR HttpSessionWrapper original = getCurrentSession(); setCurrentSession(null); - HttpSession newSession = getSession(); - original.session = ((HttpSessionWrapper)newSession).session; + HttpSessionWrapper newSession = getSession(); + original.setSession(newSession.getSession()); newSession.setMaxInactiveInterval(session.getMaxInactiveInterval()); for(Map.Entry attr : attrs.entrySet()) { @@ -266,7 +261,7 @@ public class SessionRepositoryFilter extends OncePerR } @Override - public HttpSession getSession(boolean create) { + public HttpSessionWrapper getSession(boolean create) { HttpSessionWrapper currentSession = getCurrentSession(); if(currentSession != null) { return currentSession; @@ -300,7 +295,7 @@ public class SessionRepositoryFilter extends OncePerR } @Override - public HttpSession getSession() { + public HttpSessionWrapper getSession() { return getSession(true); } @@ -315,132 +310,21 @@ public class SessionRepositoryFilter extends OncePerR * @author Rob Winch * @since 1.0 */ - private final class HttpSessionWrapper implements HttpSession { - private S session; - private final ServletContext servletContext; - private boolean invalidated; - private boolean old; + private final class HttpSessionWrapper extends ExpiringSessionHttpSession { public HttpSessionWrapper(S session, ServletContext servletContext) { - this.session = session; - this.servletContext = servletContext; - } - - public long getCreationTime() { - checkState(); - return session.getCreationTime(); - } - - public String getId() { - return session.getId(); - } - - public long getLastAccessedTime() { - checkState(); - return session.getLastAccessedTime(); - } - - public ServletContext getServletContext() { - return servletContext; - } - - public void setMaxInactiveInterval(int interval) { - session.setMaxInactiveIntervalInSeconds(interval); - } - - public int getMaxInactiveInterval() { - return session.getMaxInactiveIntervalInSeconds(); - } - - @SuppressWarnings("deprecation") - public HttpSessionContext getSessionContext() { - return NOOP_SESSION_CONTEXT; - } - - public Object getAttribute(String name) { - checkState(); - return session.getAttribute(name); - } - - public Object getValue(String name) { - return getAttribute(name); - } - - public Enumeration getAttributeNames() { - checkState(); - return Collections.enumeration(session.getAttributeNames()); - } - - public String[] getValueNames() { - checkState(); - Set attrs = session.getAttributeNames(); - return attrs.toArray(new String[0]); - } - - public void setAttribute(String name, Object value) { - checkState(); - session.setAttribute(name, value); - } - - public void putValue(String name, Object value) { - setAttribute(name, value); - } - - public void removeAttribute(String name) { - checkState(); - session.removeAttribute(name); - } - - public void removeValue(String name) { - removeAttribute(name); + super(session, servletContext); } public void invalidate() { - checkState(); - this.invalidated = true; + super.invalidate(); requestedSessionInvalidated = true; setCurrentSession(null); sessionRepository.delete(getId()); } - - public void setNew(boolean isNew) { - this.old = !isNew; - } - - public boolean isNew() { - checkState(); - return !old; - } - - private void checkState() { - if(invalidated) { - throw new IllegalStateException("The HttpSession has already be invalidated."); - } - } } } - @SuppressWarnings("deprecation") - private static final HttpSessionContext NOOP_SESSION_CONTEXT = new HttpSessionContext() { - public HttpSession getSession(String sessionId) { - return null; - } - - public Enumeration getIds() { - return EMPTY_ENUMERATION; - } - }; - - private static final Enumeration EMPTY_ENUMERATION = new Enumeration() { - public boolean hasMoreElements() { - return false; - } - - public String nextElement() { - throw new NoSuchElementException("a"); - } - }; - static class MultiHttpSessionStrategyAdapter implements MultiHttpSessionStrategy { private HttpSessionStrategy delegate;