[Statelessfilter-commits] SF.net SVN: statelessfilter:[113] trunk/stateless-core/src/main/java/net/
Status: Beta
Brought to you by:
nricheton
From: <nri...@us...> - 2013-10-24 08:23:02
|
Revision: 113 http://sourceforge.net/p/statelessfilter/code/113 Author: nricheton Date: 2013-10-24 08:22:59 +0000 (Thu, 24 Oct 2013) Log Message: ----------- cleanup Modified Paths: -------------- trunk/stateless-core/src/main/java/net/sourceforge/statelessfilter/backend/support/CookieBackendSupport.java Modified: trunk/stateless-core/src/main/java/net/sourceforge/statelessfilter/backend/support/CookieBackendSupport.java =================================================================== --- trunk/stateless-core/src/main/java/net/sourceforge/statelessfilter/backend/support/CookieBackendSupport.java 2013-10-23 16:08:03 UTC (rev 112) +++ trunk/stateless-core/src/main/java/net/sourceforge/statelessfilter/backend/support/CookieBackendSupport.java 2013-10-24 08:22:59 UTC (rev 113) @@ -13,6 +13,9 @@ */ package net.sourceforge.statelessfilter.backend.support; +import static org.apache.commons.lang.StringUtils.defaultIfEmpty; +import static org.apache.commons.lang.StringUtils.isEmpty; + import java.io.IOException; import java.security.SignatureException; import java.util.ArrayList; @@ -57,231 +60,212 @@ * */ public abstract class CookieBackendSupport implements ISessionBackend { - /** - * If cookie data exceed this value, multiple cookie are created. - */ - private static final int COOKIE_MAX_SIZE = 3000; - private static Logger logger = LoggerFactory.getLogger(CookieBackendSupport.class); + /** + * If cookie data exceed this value, multiple cookie are created. + */ + private static final int COOKIE_MAX_SIZE = 3000; + private static Logger logger = LoggerFactory.getLogger(CookieBackendSupport.class); - /* Constants used in properties */ - private static final String PARAM_COOKIEDOMAIN = "cookiedomain"; //$NON-NLS-1$ - private static final String PARAM_COOKIEMAXAGE = "cookiemaxage"; //$NON-NLS-1$ - private static final String PARAM_COOKIENAME = "cookiename"; //$NON-NLS-1$ - private static final String PARAM_COOKIEPATH = "cookiepath"; //$NON-NLS-1$ + /* Constants used in properties */ + private static final String PARAM_COOKIEDOMAIN = "cookiedomain"; //$NON-NLS-1$ + private static final String PARAM_COOKIEMAXAGE = "cookiemaxage"; //$NON-NLS-1$ + private static final String PARAM_COOKIENAME = "cookiename"; //$NON-NLS-1$ + private static final String PARAM_COOKIEPATH = "cookiepath"; //$NON-NLS-1$ - /** - * Constant used to store the number of cookie segment within a single - * request. This information is used for cleaning. - * - * <p> - * Constant value depends of the cookie name. - * - * @see CookieBackendSupport#setCookieName(String) - */ - private String ATTR_COUNT = "stateless.session.count"; //$NON-NLS-1$ + /** + * Constant used to store the number of cookie segment within a single + * request. This information is used for cleaning. + * + * <p> + * Constant value depends of the cookie name. + * + * @see CookieBackendSupport#setCookieName(String) + */ + private String ATTR_COUNT = "stateless.session.count"; //$NON-NLS-1$ - /* Default values */ - protected String cookieName = "session"; //$NON-NLS-1$ - protected String domain = null; - protected Integer maxAge = null; - protected String path = "/"; //$NON-NLS-1$ + /* Default values */ + protected String cookieName = "session"; //$NON-NLS-1$ + protected String domain = null; + protected Integer maxAge = null; + protected String path = "/"; //$NON-NLS-1$ - /** - * @see net.sourceforge.statelessfilter.backend.ISessionBackend#destroy() - */ - abstract public void destroy(); + /** + * @see net.sourceforge.statelessfilter.backend.ISessionBackend#destroy() + */ + abstract public void destroy(); - /** - * @see net.sourceforge.statelessfilter.backend.ISessionBackend#getId() - */ - abstract public String getId(); + /** + * @see net.sourceforge.statelessfilter.backend.ISessionBackend#getId() + */ + abstract public String getId(); - /** - * Read cookie configuration : name, path, domain and maxAge. - * - * @see net.sourceforge.statelessfilter.backend.ISessionBackend#init(java.util.Map) - */ - public void init(Map<String, String> config) throws Exception { - // Name - String name = config.get(PARAM_COOKIENAME); - if (!StringUtils.isEmpty(name)) { - setCookieName(name); - } + /** + * Read cookie configuration : name, path, domain and maxAge. + * + * @see net.sourceforge.statelessfilter.backend.ISessionBackend#init(java.util.Map) + */ + public void init(Map<String, String> config) throws Exception { + setCookieName(defaultIfEmpty(config.get(PARAM_COOKIENAME), "session")); + this.path = defaultIfEmpty(config.get(PARAM_COOKIEPATH), "/"); + this.domain = config.get(PARAM_COOKIEDOMAIN); + this.maxAge = isEmpty(config.get(PARAM_COOKIEMAXAGE)) ? null : new Integer(Integer.parseInt(config + .get(PARAM_COOKIEMAXAGE))); + } - // Path - String path = config.get(PARAM_COOKIEPATH); - if (!StringUtils.isEmpty(path)) { - this.path = path; - } + /** + * Buffering only headers should be enough for most cases. But if the + * application updates session after sending response body, switch to full + * buffering in configuration. + * + * @see net.sourceforge.statelessfilter.backend.ISessionBackend#isBufferingRequired() + */ + public String isBufferingRequired() { + return Configuration.BUFFERING_HEADERS; + } - // Domain - String domain = config.get(PARAM_COOKIEDOMAIN); - if (!StringUtils.isEmpty(domain)) { - this.domain = domain; - } + /** + * @see net.sourceforge.statelessfilter.backend.ISessionBackend#restore(javax.servlet.http.HttpServletRequest) + */ + abstract public ISessionData restore(HttpServletRequest request); - // MaxAge - String maxAge = config.get(PARAM_COOKIEMAXAGE); - if (!StringUtils.isEmpty(maxAge)) { - this.maxAge = new Integer(Integer.parseInt(maxAge)); - } + /** + * @see net.sourceforge.statelessfilter.backend.ISessionBackend#save(net.sourceforge.statelessfilter.backend.ISessionData, + * java.util.List, javax.servlet.http.HttpServletRequest, + * javax.servlet.http.HttpServletResponse) + */ + abstract public void save(ISessionData session, List<String> dirtyAttributes, HttpServletRequest request, + HttpServletResponse response) throws IOException; - } + /** + * Read raw data from cookie. + * + * @param request + * @param response + * @return + * @throws SignatureException + */ + protected byte[] getCookieData(HttpServletRequest request, HttpServletResponse response) throws SignatureException { + return getCookieData(request, response, false, null); + } - /** - * Buffering only headers should be enough for most cases. But if the - * application updates session after sending response body, switch to full - * buffering in configuration. - * - * @see net.sourceforge.statelessfilter.backend.ISessionBackend#isBufferingRequired() - */ - public String isBufferingRequired() { - return Configuration.BUFFERING_HEADERS; - } + protected byte[] getCookieData(HttpServletRequest request, HttpServletResponse response, boolean signed, String key) + throws SignatureException { + int i = 0; + Cookie c = null; + StringBuilder data = new StringBuilder(); - /** - * @see net.sourceforge.statelessfilter.backend.ISessionBackend#restore(javax.servlet.http.HttpServletRequest) - */ - abstract public ISessionData restore(HttpServletRequest request); + while ((c = CookieUtils.getCookie(request, cookieName + i, signed, key)) != null) { + data.append(c.getValue()); + i++; + } - /** - * @see net.sourceforge.statelessfilter.backend.ISessionBackend#save(net.sourceforge.statelessfilter.backend.ISessionData, - * java.util.List, javax.servlet.http.HttpServletRequest, - * javax.servlet.http.HttpServletResponse) - */ - abstract public void save(ISessionData session, List<String> dirtyAttributes, HttpServletRequest request, - HttpServletResponse response) throws IOException; + request.setAttribute(ATTR_COUNT, new Integer(i)); - /** - * Read raw data from cookie. - * - * @param request - * @param response - * @return - * @throws SignatureException - */ - protected byte[] getCookieData(HttpServletRequest request, HttpServletResponse response) throws SignatureException { - return getCookieData(request, response, false, null); - } + String dataString = data.toString(); + if (dataString.length() == 0) { + return null; + } + return Base64.decodeBase64(dataString); + } - protected byte[] getCookieData(HttpServletRequest request, HttpServletResponse response, boolean signed, String key) - throws SignatureException { - int i = 0; - Cookie c = null; - StringBuilder data = new StringBuilder(); + /** + * Set raw data in a cookie. Data is split in several cookies if it exceeds + * max cookie length. + * <p> + * Also ensure that the reponse cannot be cached (Cache-control header set + * to private/no-cache/no-store/must-revalidate) + * + * @param request + * @param response + * @param data + * @throws SignatureException + */ + protected void setCookieData(HttpServletRequest request, HttpServletResponse response, byte[] data) + throws SignatureException { + setCookieData(request, response, data, false, null); + } - while ((c = CookieUtils.getCookie(request, cookieName + i, signed, key)) != null) { - data.append(c.getValue()); - i++; - } + protected void setCookieData(HttpServletRequest request, HttpServletResponse response, byte[] data, boolean sign, + String key) throws SignatureException { + // As soon as we send a session cookie, the response must not be cached. + response.setHeader("Cache-Control", "private, no-cache, no-store, must-revalidate"); - request.setAttribute(ATTR_COUNT, new Integer(i)); + String encoded = StringUtils.EMPTY; + if (data != null) { + encoded = new String(Base64.encodeBase64(data)); + } - String dataString = data.toString(); - if (dataString.length() == 0) { - return null; - } - return Base64.decodeBase64(dataString); - } + ArrayList<String> splittedData = new ArrayList<String>(); + while (encoded.length() > COOKIE_MAX_SIZE) { + splittedData.add(encoded.substring(0, COOKIE_MAX_SIZE)); + encoded = encoded.substring(COOKIE_MAX_SIZE); + } + if (encoded.length() > 0) { + splittedData.add(encoded); + } - /** - * Set raw data in a cookie. Data is split in several cookies if it exceeds - * max cookie length. - * <p> - * Also ensure that the reponse cannot be cached (Cache-control header set - * to private/no-cache/no-store/must-revalidate) - * - * @param request - * @param response - * @param data - * @throws SignatureException - */ - protected void setCookieData(HttpServletRequest request, HttpServletResponse response, byte[] data) - throws SignatureException { - setCookieData(request, response, data, false, null); - } + int i = 0; + Cookie c = null; + for (String datapart : splittedData) { + c = CookieUtils.createCookie(cookieName + i, datapart, domain, path, maxAge, sign, key); + response.addCookie(c); + i++; + } - protected void setCookieData(HttpServletRequest request, HttpServletResponse response, byte[] data, boolean sign, - String key) throws SignatureException { - // As soon as we send a session cookie, the response must not be cached. - response.setHeader("Cache-Control", "private, no-cache, no-store, must-revalidate"); + // Clear no longer used segments. + int previousCount = ((Integer) request.getAttribute(ATTR_COUNT)).intValue(); + while (i < previousCount) { + c = CookieUtils.createCookie(cookieName + i, StringUtils.EMPTY, domain, path, maxAge, sign, key); + response.addCookie(c); + i++; + } - String encoded = StringUtils.EMPTY; - if (data != null) { - encoded = new String(Base64.encodeBase64(data)); - } + } - ArrayList<String> splittedData = new ArrayList<String>(); - while (encoded.length() > COOKIE_MAX_SIZE) { - splittedData.add(encoded.substring(0, COOKIE_MAX_SIZE)); - encoded = encoded.substring(COOKIE_MAX_SIZE); - } - if (encoded.length() > 0) { - splittedData.add(encoded); - } + /** + * Set the name of the cookie and update internal values accordingly. + * + * @param cookieName + */ + protected void setCookieName(String cookieName) { + this.cookieName = cookieName; - int i = 0; - Cookie c = null; - for (String datapart : splittedData) { - c = CookieUtils.createCookie(cookieName + i, datapart, domain, path, maxAge, sign, key); - response.addCookie(c); - i++; - } + // Update constant to include cookie name. + ATTR_COUNT = "stateless." + cookieName + ".count"; //$NON-NLS-1$ //$NON-NLS-2$ + } - // Clear no longer used segments. - int previousCount = ((Integer) request.getAttribute(ATTR_COUNT)).intValue(); - while (i < previousCount) { - c = CookieUtils.createCookie(cookieName + i, StringUtils.EMPTY, domain, path, maxAge, sign, key); - response.addCookie(c); - i++; - } + /** + * Get request remote address. If a proxy was in use, all X-Forwarded-For + * headers are also returned. + * + * @param request + * @return + */ + protected String getFullRemoteAddr(HttpServletRequest request) { + StringBuilder sb = new StringBuilder(); + Enumeration<?> headers = request.getHeaders("X-Forwarded-For"); - } + if (headers != null) { + logger.debug("X-Forwarded-For headers found."); + while (headers.hasMoreElements()) { + String h = (String) headers.nextElement(); + String[] splitted = StringUtils.split(h, ","); - /** - * Set the name of the cookie and update internal values accordingly. - * - * @param cookieName - */ - protected void setCookieName(String cookieName) { - this.cookieName = cookieName; + for (String s : splitted) { + if (sb.length() > 0) + sb.append(","); + sb.append(s.trim()); + } + } + } - // Update constant to include cookie name. - ATTR_COUNT = "stateless." + cookieName + ".count"; //$NON-NLS-1$ //$NON-NLS-2$ - } + if (sb.length() > 0) + sb.append(","); + sb.append(request.getRemoteAddr()); - /** - * Get request remote address. If a proxy was in use, all X-Forwarded-For - * headers are also returned. - * - * @param request - * @return - */ - protected String getFullRemoteAddr(HttpServletRequest request) { - StringBuilder sb = new StringBuilder(); - Enumeration<?> headers = request.getHeaders("X-Forwarded-For"); - - if (headers != null) { - logger.debug("X-Forwarded-For headers found."); - while (headers.hasMoreElements()) { - String h = (String) headers.nextElement(); - String[] splitted = StringUtils.split(h, ","); - - for (String s : splitted) { - if (sb.length() > 0) - sb.append(","); - sb.append(s.trim()); - } - } - } - - if (sb.length() > 0) - sb.append(","); - sb.append(request.getRemoteAddr()); - - if (logger.isDebugEnabled()) { - logger.debug("Remote ip address : " + sb.toString()); - } - return sb.toString(); - } + if (logger.isDebugEnabled()) { + logger.debug("Remote ip address : " + sb.toString()); + } + return sb.toString(); + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |