From f2307be2bc32ef1a976016002e2f1b5935c8dd96 Mon Sep 17 00:00:00 2001 From: "christopher.exell" Date: Fri, 3 Apr 2015 19:20:39 -0700 Subject: [PATCH 1/2] Track session dirty state so that cookies can be written again if the session has been changed, and a method on the wrapped response that triggers a cookie write is called. --- .../plugins/cookiesession/SerializableSession.java | 13 +++++++++++++ .../SessionRepositoryRequestWrapper.java | 6 ++++++ .../SessionRepositoryResponseWrapper.java | 12 +++++------- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/java/com/granicus/grails/plugins/cookiesession/SerializableSession.java b/src/java/com/granicus/grails/plugins/cookiesession/SerializableSession.java index 3785ebd..6279797 100644 --- a/src/java/com/granicus/grails/plugins/cookiesession/SerializableSession.java +++ b/src/java/com/granicus/grails/plugins/cookiesession/SerializableSession.java @@ -63,6 +63,7 @@ public boolean hasMoreElements() { private HashMap attributes; private Boolean isValid = true; + private Boolean isDirty = false; transient private ServletContext servletContext; transient private boolean newSession; transient private int maxInactiveInterval; @@ -166,12 +167,24 @@ public void removeValue(String name){ public void invalidate(){ isValid = false; + isDirty = true; } protected void setIsNewSession( boolean isNewSession ){ this.newSession = isNewSession; } + public boolean isNew(){ return ( this.newSession ); } + + protected void setIsDirty(boolean isDirty) + { + this.isDirty = isDirty; + } + + protected boolean getIsDirty() + { + return this.isDirty; + } } diff --git a/src/java/com/granicus/grails/plugins/cookiesession/SessionRepositoryRequestWrapper.java b/src/java/com/granicus/grails/plugins/cookiesession/SessionRepositoryRequestWrapper.java index ea72dbd..0481494 100644 --- a/src/java/com/granicus/grails/plugins/cookiesession/SessionRepositoryRequestWrapper.java +++ b/src/java/com/granicus/grails/plugins/cookiesession/SessionRepositoryRequestWrapper.java @@ -56,6 +56,12 @@ public void restoreSession() { // - assign the servlet context session = sessionRepository.restoreSession( this ); + + // we just loaded so we can't be dirty + if (session != null) + { + session.setIsDirty(false); + } if( sessionPersistenceListeners != null ){ // call sessionPersistenceListeners diff --git a/src/java/com/granicus/grails/plugins/cookiesession/SessionRepositoryResponseWrapper.java b/src/java/com/granicus/grails/plugins/cookiesession/SessionRepositoryResponseWrapper.java index 042327d..398ba2f 100644 --- a/src/java/com/granicus/grails/plugins/cookiesession/SessionRepositoryResponseWrapper.java +++ b/src/java/com/granicus/grails/plugins/cookiesession/SessionRepositoryResponseWrapper.java @@ -63,11 +63,6 @@ public void saveSession(){ return; } - if( sessionSaved == true ){ - if( log.isTraceEnabled() ){ log.trace("session is already saved, not attempting to save again."); } - return; - } - SerializableSession session = (SerializableSession) request.getSession(this.enforceSession); if( session == null ){ @@ -75,8 +70,10 @@ public void saveSession(){ return; } - // flag the session as saved. - sessionSaved = true; + if(session.getIsDirty() == false ){ + if( log.isTraceEnabled() ){ log.trace("session is not dirty, not saving."); } + return; + } if( log.isTraceEnabled() ){ log.trace("calling session repository to save session."); } @@ -93,6 +90,7 @@ public void saveSession(){ } sessionRepository.saveSession(session,this); + session.setIsDirty(false); } @Override From c13f3c2ac470e230eed7ab2801be88c1b24ec7ed Mon Sep 17 00:00:00 2001 From: "christopher.exell" Date: Fri, 3 Apr 2015 19:25:36 -0700 Subject: [PATCH 2/2] Remove unused variable --- .../plugins/cookiesession/SessionRepositoryResponseWrapper.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/java/com/granicus/grails/plugins/cookiesession/SessionRepositoryResponseWrapper.java b/src/java/com/granicus/grails/plugins/cookiesession/SessionRepositoryResponseWrapper.java index 398ba2f..1ea041f 100644 --- a/src/java/com/granicus/grails/plugins/cookiesession/SessionRepositoryResponseWrapper.java +++ b/src/java/com/granicus/grails/plugins/cookiesession/SessionRepositoryResponseWrapper.java @@ -35,7 +35,6 @@ public class SessionRepositoryResponseWrapper extends HttpServletResponseWrapper private String sessionId = "simplesession"; private SessionRepository sessionRepository; private SessionRepositoryRequestWrapper request; - private boolean sessionSaved = false; private boolean enforceSession = false; private ArrayList sessionPersistenceListeners;