1 17 18 19 package org.apache.catalina.valves; 20 21 22 import java.io.IOException ; 23 24 import javax.servlet.ServletException ; 25 import javax.servlet.http.HttpServletResponse ; 26 27 import org.apache.catalina.Context; 28 import org.apache.catalina.Manager; 29 import org.apache.catalina.Session; 30 import org.apache.catalina.Store; 31 import org.apache.catalina.connector.Request; 32 import org.apache.catalina.connector.Response; 33 import org.apache.catalina.core.StandardHost; 34 import org.apache.catalina.session.PersistentManager; 35 import org.apache.catalina.util.StringManager; 36 37 38 47 48 public class PersistentValve 49 extends ValveBase { 50 51 52 54 55 58 private static final String info = 59 "org.apache.catalina.valves.PersistentValve/1.0"; 60 61 62 65 private static final StringManager sm = 66 StringManager.getManager(Constants.Package); 67 68 69 71 72 75 public String getInfo() { 76 77 return (info); 78 79 } 80 81 82 84 85 96 public void invoke(Request request, Response response) 97 throws IOException , ServletException { 98 99 StandardHost host = (StandardHost) getContainer(); 101 Context context = request.getContext(); 102 if (context == null) { 103 response.sendError 104 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR, 105 sm.getString("standardHost.noContext")); 106 return; 107 } 108 109 Thread.currentThread().setContextClassLoader 111 (context.getLoader().getClassLoader()); 112 113 String sessionId = request.getRequestedSessionId(); 115 Manager manager = context.getManager(); 116 if (sessionId != null && manager != null) { 117 if (manager instanceof PersistentManager) { 118 Store store = ((PersistentManager) manager).getStore(); 119 if (store != null) { 120 Session session = null; 121 try { 122 session = store.load(sessionId); 123 } catch (Exception e) { 124 container.getLogger().error("deserializeError"); 125 } 126 if (session != null) { 127 if (!session.isValid() || 128 isSessionStale(session, System.currentTimeMillis())) { 129 if (container.getLogger().isDebugEnabled()) 130 container.getLogger().debug("session swapped in is invalid or expired"); 131 session.expire(); 132 store.remove(sessionId); 133 } else { 134 session.setManager(manager); 135 manager.add(session); 137 session.access(); 139 } 140 } 141 } 142 } 143 } 144 if (container.getLogger().isDebugEnabled()) 145 container.getLogger().debug("sessionId: " + sessionId); 146 147 getNext().invoke(request, response); 149 150 Session hsess; 153 try { 154 hsess = request.getSessionInternal(); 155 } catch (Exception ex) { 156 hsess = null; 157 } 158 String newsessionId = null; 159 if (hsess!=null) 160 newsessionId = hsess.getIdInternal(); 161 162 if (container.getLogger().isDebugEnabled()) 163 container.getLogger().debug("newsessionId: " + newsessionId); 164 if (newsessionId!=null) { 165 166 if (manager instanceof PersistentManager) { 167 Session session = manager.findSession(newsessionId); 168 Store store = ((PersistentManager) manager).getStore(); 169 if (store != null && session!=null && 170 session.isValid() && 171 !isSessionStale(session, System.currentTimeMillis())) { 172 store.save(session); 174 ((PersistentManager) manager).removeSuper(session); 175 session.recycle(); 176 } else { 177 if (container.getLogger().isDebugEnabled()) 178 container.getLogger().debug("newsessionId store: " + store + " session: " + 179 session + " valid: " + session.isValid() + 180 " Staled: " + 181 isSessionStale(session, System.currentTimeMillis())); 182 183 } 184 } else { 185 if (container.getLogger().isDebugEnabled()) 186 container.getLogger().debug("newsessionId Manager: " + manager); 187 } 188 } 189 } 190 191 197 protected boolean isSessionStale(Session session, long timeNow) { 198 199 int maxInactiveInterval = session.getMaxInactiveInterval(); 200 if (maxInactiveInterval >= 0) { 201 int timeIdle = (int) ((timeNow - session.getLastAccessedTime()) / 1000L); 203 if (timeIdle >= maxInactiveInterval) 204 return true; 205 } 206 207 return false; 208 209 } 210 211 } 212 | Popular Tags |