1 24 package org.riotfamily.cachius.support; 25 26 import javax.servlet.http.HttpServletRequest ; 27 import javax.servlet.http.HttpServletResponse ; 28 import javax.servlet.http.HttpSession ; 29 30 33 public final class SessionUtils { 34 35 private static final String INITIAL_SESSION_STATE = 36 SessionUtils.class.getName() + ".initialState"; 37 38 private SessionUtils() { 39 } 40 41 50 public static boolean urlsNeedEncoding(HttpServletRequest request) { 51 boolean encodeUrls = urlsCurrentlyNeedEncoding(request); 52 Boolean initialState = (Boolean ) request.getAttribute(INITIAL_SESSION_STATE); 53 if (initialState == null) { 54 initialState = Boolean.valueOf(encodeUrls); 55 request.setAttribute(INITIAL_SESSION_STATE, initialState); 56 } 57 return encodeUrls; 58 } 59 60 public static boolean sessionStateChanged(HttpServletRequest request) { 61 Boolean initialState = (Boolean ) request.getAttribute(INITIAL_SESSION_STATE); 62 if (initialState == null) { 63 throw new IllegalStateException ("urlsNeedEncoding() must be called first."); 64 } 65 return urlsCurrentlyNeedEncoding(request) != initialState.booleanValue(); 66 } 67 68 public static void addStateToCacheKey(HttpServletRequest request, 69 StringBuffer key) { 70 71 if (urlsNeedEncoding(request)) { 72 key.append(";jsessionid"); 73 } 74 } 75 76 private static boolean urlsCurrentlyNeedEncoding(HttpServletRequest request) { 77 HttpSession session = request.getSession(false); 78 if (session == null) { 79 return false; 80 } 81 return session.isNew() || request.isRequestedSessionIdFromURL(); 82 } 83 84 } 85 | Popular Tags |