[Statelessfilter-commits] SF.net SVN: statelessfilter:[82] trunk/stateless-core
Status: Beta
Brought to you by:
nricheton
|
From: <nri...@us...> - 2011-07-24 21:51:21
|
Revision: 82
http://statelessfilter.svn.sourceforge.net/statelessfilter/?rev=82&view=rev
Author: nricheton
Date: 2011-07-24 21:51:13 +0000 (Sun, 24 Jul 2011)
Log Message:
-----------
https://sourceforge.net/apps/mantisbt/statelessfilter/view.php?id=25
0000025: Full response buffering is overkill for cookies backends
Modified Paths:
--------------
trunk/stateless-core/pom.xml
trunk/stateless-core/src/main/java/net/sourceforge/statelessfilter/backend/support/CookieBackendSupport.java
trunk/stateless-core/src/main/java/net/sourceforge/statelessfilter/filter/Configuration.java
trunk/stateless-core/src/main/java/net/sourceforge/statelessfilter/filter/IPlugin.java
trunk/stateless-core/src/main/java/net/sourceforge/statelessfilter/filter/StatelessFilter.java
trunk/stateless-core/src/main/java/net/sourceforge/statelessfilter/session/StatelessSession.java
trunk/stateless-core/src/main/java/net/sourceforge/statelessfilter/wrappers/StatelessRequestWrapper.java
trunk/stateless-core/src/test/java/net/sourceforge/statelessfilter/backend/MockSessionBackend.java
trunk/stateless-core/src/test/java/net/sourceforge/statelessfilter/filter/StatelessFilterTest.java
Added Paths:
-----------
trunk/stateless-core/src/main/java/net/sourceforge/statelessfilter/wrappers/headers/
trunk/stateless-core/src/main/java/net/sourceforge/statelessfilter/wrappers/headers/HeaderBufferedHttpResponseWrapper.java
Property Changed:
----------------
trunk/stateless-core/src/main/java/
Modified: trunk/stateless-core/pom.xml
===================================================================
--- trunk/stateless-core/pom.xml 2011-06-15 10:39:12 UTC (rev 81)
+++ trunk/stateless-core/pom.xml 2011-07-24 21:51:13 UTC (rev 82)
@@ -5,7 +5,6 @@
<version>0.8-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
- <groupId>net.sourceforge.statelessfilter</groupId>
<artifactId>stateless-core</artifactId>
<name>Core</name>
<version>0.8-SNAPSHOT</version>
Property changes on: trunk/stateless-core/src/main/java
___________________________________________________________________
Added: svn:ignore
+ META-INF
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 2011-06-15 10:39:12 UTC (rev 81)
+++ trunk/stateless-core/src/main/java/net/sourceforge/statelessfilter/backend/support/CookieBackendSupport.java 2011-07-24 21:51:13 UTC (rev 82)
@@ -25,6 +25,7 @@
import net.sourceforge.statelessfilter.backend.ISessionBackend;
import net.sourceforge.statelessfilter.backend.ISessionData;
+import net.sourceforge.statelessfilter.filter.Configuration;
import net.sourceforge.statelessfilter.utils.CookieUtils;
import org.apache.commons.codec.binary.Base64;
@@ -55,218 +56,215 @@
*
*/
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 {
+ // Name
+ String name = config.get(PARAM_COOKIENAME);
+ if (!StringUtils.isEmpty(name)) {
+ setCookieName(name);
+ }
- // Path
- String path = config.get(PARAM_COOKIEPATH);
- if (!StringUtils.isEmpty(path)) {
- this.path = path;
- }
+ // Path
+ String path = config.get(PARAM_COOKIEPATH);
+ if (!StringUtils.isEmpty(path)) {
+ this.path = path;
+ }
- // Domain
- String domain = config.get(PARAM_COOKIEDOMAIN);
- if (!StringUtils.isEmpty(domain)) {
- this.domain = domain;
- }
+ // Domain
+ String domain = config.get(PARAM_COOKIEDOMAIN);
+ if (!StringUtils.isEmpty(domain)) {
+ this.domain = domain;
+ }
- // MaxAge
- String maxAge = config.get(PARAM_COOKIEMAXAGE);
- if (!StringUtils.isEmpty(maxAge)) {
- this.maxAge = new Integer(Integer.parseInt(maxAge));
- }
+ // MaxAge
+ String maxAge = config.get(PARAM_COOKIEMAXAGE);
+ if (!StringUtils.isEmpty(maxAge)) {
+ this.maxAge = new Integer(Integer.parseInt(maxAge));
+ }
- }
+ }
- /**
- * Always true for cookies since we need to create cookie at the end of the
- * request, when data has already been sent.
- *
- * @see net.sourceforge.statelessfilter.backend.ISessionBackend#isBufferingRequired()
- */
- public boolean isBufferingRequired() {
- return true;
- }
+ /**
+ * 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;
+ }
- /**
- * @see net.sourceforge.statelessfilter.backend.ISessionBackend#restore(javax.servlet.http.HttpServletRequest)
- */
- abstract public ISessionData restore(HttpServletRequest request);
+ /**
+ * @see net.sourceforge.statelessfilter.backend.ISessionBackend#restore(javax.servlet.http.HttpServletRequest)
+ */
+ abstract public ISessionData restore(HttpServletRequest request);
- /**
- * @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;
+ /**
+ * @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;
- /**
- * @deprecated see :{@link CookieUtils}
- * @param request
- * @param content
- * @return
- */
- @Deprecated
- protected Cookie createCookie(String name, String content) {
- try {
- return CookieUtils
- .createCookie(name, content, domain, path, maxAge);
- } catch (SignatureException e) {
- logger.error("Error creating cookie", e); //$NON-NLS-1$
- }
- return null;
- }
+ /**
+ * @deprecated see :{@link CookieUtils}
+ * @param request
+ * @param content
+ * @return
+ */
+ @Deprecated
+ protected Cookie createCookie(String name, String content) {
+ try {
+ return CookieUtils.createCookie(name, content, domain, path, maxAge);
+ } catch (SignatureException e) {
+ logger.error("Error creating cookie", e); //$NON-NLS-1$
+ }
+ return null;
+ }
- /**
- * @deprecated see :{@link CookieUtils}
- * @param request
- * @param name
- * @return
- */
- @Deprecated
- protected Cookie getCookie(HttpServletRequest request, String name) {
- try {
- return CookieUtils.getCookie(request, name);
- } catch (SignatureException e) {
- logger.error("Error sending cookie", e); //$NON-NLS-1$
- }
- return null;
- }
+ /**
+ * @deprecated see :{@link CookieUtils}
+ * @param request
+ * @param name
+ * @return
+ */
+ @Deprecated
+ protected Cookie getCookie(HttpServletRequest request, String name) {
+ try {
+ return CookieUtils.getCookie(request, name);
+ } catch (SignatureException e) {
+ logger.error("Error sending cookie", e); //$NON-NLS-1$
+ }
+ return null;
+ }
- /**
- * Read raw data from cookie.
- *
- * @param request
- * @param response
- * @return
- */
- protected byte[] getCookieData(HttpServletRequest request,
- HttpServletResponse response) {
- int i = 0;
- Cookie c = null;
- StringBuilder data = new StringBuilder();
+ /**
+ * Read raw data from cookie.
+ *
+ * @param request
+ * @param response
+ * @return
+ */
+ protected byte[] getCookieData(HttpServletRequest request, HttpServletResponse response) {
+ int i = 0;
+ Cookie c = null;
+ StringBuilder data = new StringBuilder();
- while ((c = getCookie(request, cookieName + i)) != null) {
- data.append(c.getValue());
- i++;
- }
+ while ((c = getCookie(request, cookieName + i)) != null) {
+ data.append(c.getValue());
+ i++;
+ }
- request.setAttribute(ATTR_COUNT, new Integer(i));
+ request.setAttribute(ATTR_COUNT, new Integer(i));
- String dataString = data.toString();
- if (dataString.length() == 0) {
- return null;
- }
- return Base64.decodeBase64(dataString);
- }
+ String dataString = data.toString();
+ if (dataString.length() == 0) {
+ return null;
+ }
+ return Base64.decodeBase64(dataString);
+ }
- /**
- * 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
- */
- protected void setCookieData(HttpServletRequest request,
- HttpServletResponse response, byte[] data) {
- // 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");
+ /**
+ * 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
+ */
+ protected void setCookieData(HttpServletRequest request, HttpServletResponse response, byte[] data) {
+ // 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");
- String encoded = StringUtils.EMPTY;
- if (data != null) {
- encoded = new String(Base64.encodeBase64(data));
- }
+ 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);
- }
+ 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);
+ }
- int i = 0;
- Cookie c = null;
- for (String datapart : splittedData) {
- c = createCookie(cookieName + i, datapart);
- response.addCookie(c);
- i++;
- }
+ int i = 0;
+ Cookie c = null;
+ for (String datapart : splittedData) {
+ c = createCookie(cookieName + i, datapart);
+ response.addCookie(c);
+ i++;
+ }
- // Clear no longer used segments.
- int previousCount = ((Integer) request.getAttribute(ATTR_COUNT))
- .intValue();
- while (i < previousCount) {
- c = createCookie(cookieName + i, StringUtils.EMPTY);
- response.addCookie(c);
- i++;
- }
+ // Clear no longer used segments.
+ int previousCount = ((Integer) request.getAttribute(ATTR_COUNT)).intValue();
+ while (i < previousCount) {
+ c = createCookie(cookieName + i, StringUtils.EMPTY);
+ response.addCookie(c);
+ i++;
+ }
- }
+ }
- /**
- * Set the name of the cookie and update internal values accordingly.
- * @param cookieName
- */
- protected void setCookieName(String cookieName) {
- this.cookieName = cookieName;
-
- // Update constant to inclue cookie name.
- ATTR_COUNT = "stateless." + cookieName + ".count"; //$NON-NLS-1$ //$NON-NLS-2$
- }
+ /**
+ * Set the name of the cookie and update internal values accordingly.
+ *
+ * @param cookieName
+ */
+ protected void setCookieName(String cookieName) {
+ this.cookieName = cookieName;
+
+ // Update constant to inclue cookie name.
+ ATTR_COUNT = "stateless." + cookieName + ".count"; //$NON-NLS-1$ //$NON-NLS-2$
+ }
}
Modified: trunk/stateless-core/src/main/java/net/sourceforge/statelessfilter/filter/Configuration.java
===================================================================
--- trunk/stateless-core/src/main/java/net/sourceforge/statelessfilter/filter/Configuration.java 2011-06-15 10:39:12 UTC (rev 81)
+++ trunk/stateless-core/src/main/java/net/sourceforge/statelessfilter/filter/Configuration.java 2011-07-24 21:51:13 UTC (rev 82)
@@ -29,10 +29,15 @@
*
*/
public class Configuration {
+
+ public static final String BUFFERING_FALSE="false" ;
+ public static final String BUFFERING_HEADERS="headers" ;
+ public static final String BUFFERING_FULL="full" ;
+
public HashMap<String, ISessionBackend> backends = new HashMap<String, ISessionBackend>();
public HashMap<String, String> backendsAttributeMapping = new HashMap<String, String>();
public String defaultBackend = StringUtils.EMPTY;
- public boolean isBufferingRequired = false;
+ public String isBufferingRequired = BUFFERING_FALSE;
public List<IRequestProcessor> requestProcessors = new ArrayList<IRequestProcessor>();
public boolean useDirty = false;
}
Modified: trunk/stateless-core/src/main/java/net/sourceforge/statelessfilter/filter/IPlugin.java
===================================================================
--- trunk/stateless-core/src/main/java/net/sourceforge/statelessfilter/filter/IPlugin.java 2011-06-15 10:39:12 UTC (rev 81)
+++ trunk/stateless-core/src/main/java/net/sourceforge/statelessfilter/filter/IPlugin.java 2011-07-24 21:51:13 UTC (rev 82)
@@ -56,5 +56,5 @@
*
* @return
*/
- boolean isBufferingRequired();
+ String isBufferingRequired();
}
Modified: trunk/stateless-core/src/main/java/net/sourceforge/statelessfilter/filter/StatelessFilter.java
===================================================================
--- trunk/stateless-core/src/main/java/net/sourceforge/statelessfilter/filter/StatelessFilter.java 2011-06-15 10:39:12 UTC (rev 81)
+++ trunk/stateless-core/src/main/java/net/sourceforge/statelessfilter/filter/StatelessFilter.java 2011-07-24 21:51:13 UTC (rev 82)
@@ -41,6 +41,7 @@
import net.sourceforge.statelessfilter.spring.SpringObjectInstantiationListener;
import net.sourceforge.statelessfilter.wrappers.BufferedHttpResponseWrapper;
import net.sourceforge.statelessfilter.wrappers.StatelessRequestWrapper;
+import net.sourceforge.statelessfilter.wrappers.headers.HeaderBufferedHttpResponseWrapper;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
@@ -55,453 +56,465 @@
*
*/
public class StatelessFilter implements Filter {
- private static final String CONFIG_ATTRIBUTE_PREFIX = "attribute."; //$NON-NLS-1$
- private static final String CONFIG_DEFAULT_BACKEND = "default"; //$NON-NLS-1$
- private static final String CONFIG_DIRTY = "dirtycheck"; //$NON-NLS-1$
- private static final String CONFIG_INSTANTIATION_LISTENER = "instantiationListener"; //$NON-NLS-1$
- private static final String CONFIG_LOCATION = "configurationLocation"; //$NON-NLS-1$
- private static final String CONFIG_LOCATION_DEFAULT = "/stateless.properties"; //$NON-NLS-1$
- /**
- * Location of backend configuration file.
- */
- private static final String CONFIG_PLUGIN_BACKEND = "stateless-backend.properties"; //$NON-NLS-1$
- private static final String CONFIG_PLUGIN_BACKEND_IMPL = "backendImpl"; //$NON-NLS-1$
- /**
- * Location of request processor configuration file.
- */
- private static final String CONFIG_PLUGIN_PROCESSOR = "stateless-processor.properties"; //$NON-NLS-1$
- private static final String CONFIG_PLUGIN_PROCESSOR_IMPL = "processorImpl"; //$NON-NLS-1$
- private static final String DEBUG_INIT = "Stateless filter init..."; //$NON-NLS-1$
- private static final String DEBUG_PROCESSING = "Processing "; //$NON-NLS-1$
- private static final String DOT = "."; //$NON-NLS-1$
- private static final String EXCLUDE_PATTERN_SEPARATOR = ","; //$NON-NLS-1$
- private static final String INFO_BACKEND = "Backend "; //$NON-NLS-1$
- private static final String INFO_BUFFERING = " enables output buffering."; //$NON-NLS-1$
- private static final String INFO_DEFAULT_BACKEND = "Default Session backend is "; //$NON-NLS-1$
- private static final String INFO_READY = " ready."; //$NON-NLS-1$
- private static final String INFO_REQUEST_PROCESSOR = "Request processor "; //$NON-NLS-1$
+ private static final String CONFIG_ATTRIBUTE_PREFIX = "attribute."; //$NON-NLS-1$
+ private static final String CONFIG_DEFAULT_BACKEND = "default"; //$NON-NLS-1$
+ private static final String CONFIG_DIRTY = "dirtycheck"; //$NON-NLS-1$
+ private static final String CONFIG_INSTANTIATION_LISTENER = "instantiationListener"; //$NON-NLS-1$
+ private static final String CONFIG_LOCATION = "configurationLocation"; //$NON-NLS-1$
+ private static final String CONFIG_LOCATION_DEFAULT = "/stateless.properties"; //$NON-NLS-1$
+ /**
+ * Location of backend configuration file.
+ */
+ private static final String CONFIG_PLUGIN_BACKEND = "stateless-backend.properties"; //$NON-NLS-1$
+ private static final String CONFIG_PLUGIN_BACKEND_IMPL = "backendImpl"; //$NON-NLS-1$
+ /**
+ * Location of request processor configuration file.
+ */
+ private static final String CONFIG_PLUGIN_PROCESSOR = "stateless-processor.properties"; //$NON-NLS-1$
+ private static final String CONFIG_PLUGIN_PROCESSOR_IMPL = "processorImpl"; //$NON-NLS-1$
+ private static final String DEBUG_INIT = "Stateless filter init..."; //$NON-NLS-1$
+ private static final String DEBUG_PROCESSING = "Processing "; //$NON-NLS-1$
+ private static final String DOT = "."; //$NON-NLS-1$
+ private static final String EXCLUDE_PATTERN_SEPARATOR = ","; //$NON-NLS-1$
+ private static final String INFO_BACKEND = "Backend "; //$NON-NLS-1$
+ private static final String INFO_BUFFERING = " enables output buffering."; //$NON-NLS-1$
+ private static final String INFO_DEFAULT_BACKEND = "Default Session backend is "; //$NON-NLS-1$
+ private static final String INFO_READY = " ready."; //$NON-NLS-1$
+ private static final String INFO_REQUEST_PROCESSOR = "Request processor "; //$NON-NLS-1$
- private static Logger logger = LoggerFactory
- .getLogger(StatelessFilter.class);
- private static final String WARN_BACKEND_NOT_FOUND1 = "Specified backend '"; //$NON-NLS-1$
- private static final String WARN_BACKEND_NOT_FOUND2 = "' is not installed. Missing jar ? "; //$NON-NLS-1$
- private static final String WARN_LOAD_CONF = "Cannot load global configuration /stateless.properties. Using defaults"; //$NON-NLS-1$
- private IObjectInstantiationListener instantiationListener = null;
+ private static Logger logger = LoggerFactory.getLogger(StatelessFilter.class);
- private List<Pattern> excludePatterns = null;
- private ServletContext servletContext = null;
- protected Configuration configuration = new Configuration();
+ private static final String WARN_BACKEND_NOT_FOUND1 = "Specified backend '"; //$NON-NLS-1$
+ private static final String WARN_BACKEND_NOT_FOUND2 = "' is not installed. Missing jar ? "; //$NON-NLS-1$
+ private static final String WARN_LOAD_CONF = "Cannot load global configuration /stateless.properties. Using defaults"; //$NON-NLS-1$
+ private IObjectInstantiationListener instantiationListener = null;
- /**
- * Servlet config paramter name used to configure the list of the excluded
- * uri patterns.
- */
- public static final String PARAM_EXCLUDE_PATTERN_LIST = "excludePatternList"; //$NON-NLS-1$
+ private List<Pattern> excludePatterns = null;
+ private ServletContext servletContext = null;
+ protected Configuration configuration = new Configuration();
- /**
- * @see javax.servlet.Filter#destroy()
- */
- public void destroy() {
- // Destroy all backends
- for (ISessionBackend backend : configuration.backends.values()) {
- backend.destroy();
- }
- configuration.backends.clear();
- }
+ /**
+ * Servlet config paramter name used to configure the list of the excluded
+ * uri patterns.
+ */
+ public static final String PARAM_EXCLUDE_PATTERN_LIST = "excludePatternList"; //$NON-NLS-1$
- /**
- * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
- * javax.servlet.ServletResponse, javax.servlet.FilterChain)
- */
- public void doFilter(ServletRequest request, ServletResponse response,
- FilterChain chain) throws IOException, ServletException {
- HttpServletResponse httpResponse = (HttpServletResponse) response;
+ /**
+ * @see javax.servlet.Filter#destroy()
+ */
+ public void destroy() {
+ // Destroy all backends
+ for (ISessionBackend backend : configuration.backends.values()) {
+ backend.destroy();
+ }
+ configuration.backends.clear();
+ }
- // Test if the request is excluded
- HttpServletRequest httpRequest = (HttpServletRequest) request;
- if (isExcluded(httpRequest)) {
- chain.doFilter(request, response);
- return;
- }
+ /**
+ * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
+ * javax.servlet.ServletResponse, javax.servlet.FilterChain)
+ */
+ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
+ ServletException {
+ HttpServletResponse httpResponse = (HttpServletResponse) response;
- // Wrap request
- HttpServletRequest statelessRequest = new StatelessRequestWrapper(
- (HttpServletRequest) request, configuration);
+ // Test if the request is excluded
+ HttpServletRequest httpRequest = (HttpServletRequest) request;
+ if (isExcluded(httpRequest)) {
+ chain.doFilter(request, response);
+ return;
+ }
- // Wrap response if necessary
- HttpServletResponse targetResponse = httpResponse;
+ // Wrap request
+ StatelessRequestWrapper statelessRequest = new StatelessRequestWrapper((HttpServletRequest) request,
+ configuration);
- BufferedHttpResponseWrapper bufferedResponse = null;
- if (configuration.isBufferingRequired) {
- bufferedResponse = new BufferedHttpResponseWrapper(httpResponse);
- targetResponse = bufferedResponse;
- }
+ // Wrap response if necessary
+ HttpServletResponse targetResponse = httpResponse;
- // Request processors
- if (configuration.requestProcessors != null
- && !configuration.requestProcessors.isEmpty()) {
- IRequestProcessor rp = null;
- for (int i = 0; i < configuration.requestProcessors.size(); i++) {
- rp = configuration.requestProcessors.get(i);
- try {
- rp.preRequest(statelessRequest, targetResponse);
- } catch (Exception e) {
- throw new IOException(e.getMessage());
- }
- }
- }
+ BufferedHttpResponseWrapper bufferedResponse = null;
+ if (Configuration.BUFFERING_FULL.equals(configuration.isBufferingRequired)) {
+ bufferedResponse = new BufferedHttpResponseWrapper(httpResponse);
+ targetResponse = bufferedResponse;
+ }else
+ if (Configuration.BUFFERING_HEADERS.equals(configuration.isBufferingRequired)) {
+ targetResponse = new HeaderBufferedHttpResponseWrapper(statelessRequest,httpResponse);
+ }
- // Proceed with request
- chain.doFilter(statelessRequest, targetResponse);
+ // Request processors
+ if (configuration.requestProcessors != null && !configuration.requestProcessors.isEmpty()) {
+ IRequestProcessor rp = null;
+ for (int i = 0; i < configuration.requestProcessors.size(); i++) {
+ rp = configuration.requestProcessors.get(i);
+ try {
+ rp.preRequest(statelessRequest, targetResponse);
+ } catch (Exception e) {
+ throw new IOException(e.getMessage());
+ }
+ }
+ }
- // Write session
- ((StatelessRequestWrapper) statelessRequest).writeSession(
- statelessRequest, httpResponse);
+ // Proceed with request
+ chain.doFilter(statelessRequest, targetResponse);
- // Request processors : post process.
- if (configuration.requestProcessors != null
- && !configuration.requestProcessors.isEmpty()) {
- IRequestProcessor rp = null;
- for (int i = configuration.requestProcessors.size() - 1; i >= 0; i--) {
- rp = configuration.requestProcessors.get(i);
- try {
- rp.postProcess(statelessRequest, targetResponse);
- } catch (Exception e) {
- throw new IOException(e.getMessage());
- }
- }
- }
+ // Write session
+ if (!statelessRequest.isSessionWritten()) {
+ statelessRequest.writeSession(statelessRequest, httpResponse);
+ }
- // Flush buffer if necessary
- if (bufferedResponse != null) {
- if (!bufferedResponse.performSend()) {
- bufferedResponse.flushBuffer();
- response.getOutputStream().write(bufferedResponse.getBuffer());
- response.flushBuffer();
- }
- }
- }
+ // Request processors : post process.
+ if (configuration.requestProcessors != null && !configuration.requestProcessors.isEmpty()) {
+ IRequestProcessor rp = null;
+ for (int i = configuration.requestProcessors.size() - 1; i >= 0; i--) {
+ rp = configuration.requestProcessors.get(i);
+ try {
+ rp.postProcess(statelessRequest, targetResponse);
+ } catch (Exception e) {
+ throw new IOException(e.getMessage());
+ }
+ }
+ }
- /**
- * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
- */
- public void init(FilterConfig filterConfig) throws ServletException {
- if (logger.isDebugEnabled()) {
- logger.debug(DEBUG_INIT);
- }
+ // Flush buffer if necessary
+ if (bufferedResponse != null) {
+ if (!bufferedResponse.performSend()) {
+ bufferedResponse.flushBuffer();
+ response.getOutputStream().write(bufferedResponse.getBuffer());
+ response.flushBuffer();
+ }
+ }
+ }
- // Get Servlet Context
- this.servletContext = filterConfig.getServletContext();
+ /**
+ * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
+ */
+ public void init(FilterConfig filterConfig) throws ServletException {
+ if (logger.isDebugEnabled()) {
+ logger.debug(DEBUG_INIT);
+ }
- // Configuration
- Properties globalProp = new Properties();
+ // Get Servlet Context
+ this.servletContext = filterConfig.getServletContext();
- // Try to load global configuration.
- String configLocation = filterConfig.getInitParameter(CONFIG_LOCATION);
- if (configLocation == null) {
- configLocation = CONFIG_LOCATION_DEFAULT;
- }
- try {
- globalProp.load(StatelessFilter.class
- .getResourceAsStream(configLocation));
- } catch (Exception e) {
- logger.warn(WARN_LOAD_CONF);
- // This is not fatal, will just use defaults.
- }
+ // Configuration
+ Properties globalProp = new Properties();
- // Enables Spring instantiation listener if Spring is available and
- // configured.
- if (SpringContextChecker.checkForSpring(servletContext)) {
+ // Try to load global configuration.
+ String configLocation = filterConfig.getInitParameter(CONFIG_LOCATION);
+ if (configLocation == null) {
+ configLocation = CONFIG_LOCATION_DEFAULT;
+ }
+ try {
+ globalProp.load(StatelessFilter.class.getResourceAsStream(configLocation));
+ } catch (Exception e) {
+ logger.warn(WARN_LOAD_CONF);
+ // This is not fatal, will just use defaults.
+ }
- logger.info("Enabling Spring instantiation listener"); //$NON-NLS-1$
+ // Enables Spring instantiation listener if Spring is available and
+ // configured.
+ if (SpringContextChecker.checkForSpring(servletContext)) {
- instantiationListener = new SpringObjectInstantiationListener();
- instantiationListener.setServletContext(servletContext);
- }
+ logger.info("Enabling Spring instantiation listener"); //$NON-NLS-1$
- // Load instantiation listener
- try {
- initInstantiationListener(globalProp);
- } catch (Exception e) {
- throw new ServletException(
- "Failed to load instantiation listener from /stateless.properties", //$NON-NLS-1$
- e);
- }
+ instantiationListener = new SpringObjectInstantiationListener();
+ instantiationListener.setServletContext(servletContext);
+ }
- // Load and init all plugins
+ // Load instantiation listener
+ try {
+ initInstantiationListener(globalProp);
+ } catch (Exception e) {
+ throw new ServletException("Failed to load instantiation listener from /stateless.properties", //$NON-NLS-1$
+ e);
+ }
- try {
- detectAndInitPlugins(CONFIG_PLUGIN_BACKEND, globalProp,
- IPlugin.TYPE_BACKEND);
- detectAndInitPlugins(CONFIG_PLUGIN_PROCESSOR, globalProp,
- IPlugin.TYPE_REQUEST_PROCESSOR);
+ // Load and init all plugins
- } catch (Exception e) {
- throw new ServletException(e);
- }
+ try {
+ detectAndInitPlugins(CONFIG_PLUGIN_BACKEND, globalProp, IPlugin.TYPE_BACKEND);
+ detectAndInitPlugins(CONFIG_PLUGIN_PROCESSOR, globalProp, IPlugin.TYPE_REQUEST_PROCESSOR);
- // Process attribute mapping
- initAttributeMapping(globalProp);
+ } catch (Exception e) {
+ throw new ServletException(e);
+ }
- // Read default backend
- initDefaultBackend(globalProp);
+ // Process attribute mapping
+ initAttributeMapping(globalProp);
- // Use dirty state
- initDirtyState(globalProp);
+ // Read default backend
+ initDefaultBackend(globalProp);
- // init the excluded pattern from the servlet config
- initExcludedPattern(filterConfig);
+ // Use dirty state
+ initDirtyState(globalProp);
- checkConfiguration();
- }
+ // Init Buffering mode
+ initBuffering(globalProp);
- private void checkConfiguration() throws ServletException {
- if (this.configuration.backends.size() == 0) {
- throw new ServletException(
- "No backend installed. Please add one (stateless-session for instance) in the classpath"); //$NON-NLS-1$
- }
- }
+ // init the excluded pattern from the servlet config
+ initExcludedPattern(filterConfig);
- private void initExcludedPattern(FilterConfig filterConfig) {
- String excludedPatternList = filterConfig
- .getInitParameter(PARAM_EXCLUDE_PATTERN_LIST);
- if (excludedPatternList != null) {
- String[] splittedExcludedPatternList = excludedPatternList
- .split(EXCLUDE_PATTERN_SEPARATOR);
- List<Pattern> patterns = new ArrayList<Pattern>();
- Pattern pattern = null;
- for (String element : splittedExcludedPatternList) {
- pattern = Pattern.compile(element);
- patterns.add(pattern);
- }
- this.excludePatterns = patterns;
- }
- }
+ checkConfiguration();
+ }
- private void detectAndInitPlugins(String propertyFile,
- Properties filterConfiguration, String type) throws Exception {
- Enumeration<URL> configurationURLs;
+ private void initBuffering(Properties globalProp) {
+ String buffering = globalProp.getProperty("buffering", Configuration.BUFFERING_FALSE);
+ applyBuffering(buffering, "Configuration");
+ }
- configurationURLs = StatelessFilter.class.getClassLoader()
- .getResources(propertyFile);
+ /**
+ * Change buffering mode if current mode is lower than requested.
+ *
+ * @param mode
+ */
+ private void applyBuffering(String mode, String source) {
+ if (Configuration.BUFFERING_FULL.equals(mode)
+ && !Configuration.BUFFERING_FULL.equals(configuration.isBufferingRequired)) {
+ configuration.isBufferingRequired = Configuration.BUFFERING_FULL;
+ if (logger.isInfoEnabled())
+ logger.info("Switching to buffering mode " + configuration.isBufferingRequired + " (" + source + ")");
+ } else if (Configuration.BUFFERING_HEADERS.equals(mode)
+ && Configuration.BUFFERING_FALSE.equals(configuration.isBufferingRequired)) {
+ configuration.isBufferingRequired = Configuration.BUFFERING_HEADERS;
+ if (logger.isInfoEnabled())
+ logger.info("Switching to buffering mode " + configuration.isBufferingRequired + " (" + source + ")");
+ }
- URL url = null;
- Properties pluginConfiguration = null;
- InputStream is = null;
- while (configurationURLs.hasMoreElements()) {
- url = configurationURLs.nextElement();
- if (logger.isDebugEnabled()) {
- logger.debug(DEBUG_PROCESSING + url.toString());
- }
+ }
- // Load plugin configuration
- pluginConfiguration = new Properties();
- is = url.openStream();
- pluginConfiguration.load(is);
- is.close();
+ /**
+ * Does some self tests to ensure configuration is valid
+ *
+ * @throws ServletException
+ */
+ private void checkConfiguration() throws ServletException {
+ if (this.configuration.backends.size() == 0) {
+ throw new ServletException(
+ "No backend installed. Please add one (stateless-session for instance) in the classpath"); //$NON-NLS-1$
+ }
+ }
- // Init plugin
- initPlugin(pluginConfiguration, filterConfiguration, type);
- }
+ private void initExcludedPattern(FilterConfig filterConfig) {
+ String excludedPatternList = filterConfig.getInitParameter(PARAM_EXCLUDE_PATTERN_LIST);
+ if (excludedPatternList != null) {
+ String[] splittedExcludedPatternList = excludedPatternList.split(EXCLUDE_PATTERN_SEPARATOR);
+ List<Pattern> patterns = new ArrayList<Pattern>();
+ Pattern pattern = null;
+ for (String element : splittedExcludedPatternList) {
+ pattern = Pattern.compile(element);
+ patterns.add(pattern);
+ }
+ this.excludePatterns = patterns;
+ }
+ }
- }
+ private void detectAndInitPlugins(String propertyFile, Properties filterConfiguration, String type)
+ throws Exception {
+ Enumeration<URL> configurationURLs;
- private void initAttributeMapping(Properties globalProp)
- throws ServletException {
- for (Object key : globalProp.keySet()) {
- String paramName = (String) key;
- if (paramName.startsWith(CONFIG_ATTRIBUTE_PREFIX)) {
- String attrName = paramName.substring(CONFIG_ATTRIBUTE_PREFIX
- .length());
- String backend = globalProp.getProperty(paramName);
- configuration.backendsAttributeMapping.put(attrName, backend);
+ configurationURLs = StatelessFilter.class.getClassLoader().getResources(propertyFile);
- // Ensure backend is available
- if (!configuration.backends.containsKey(backend)) {
- throw new ServletException(
- "Attributes are mapped on backend " + backend //$NON-NLS-1$
- + " but it is not installed."); //$NON-NLS-1$
- }
- }
- }
- }
+ URL url = null;
+ Properties pluginConfiguration = null;
+ InputStream is = null;
+ while (configurationURLs.hasMoreElements()) {
+ url = configurationURLs.nextElement();
+ if (logger.isDebugEnabled()) {
+ logger.debug(DEBUG_PROCESSING + url.toString());
+ }
- private void initDefaultBackend(Properties globalProp) {
- String defaultBack = globalProp.getProperty(CONFIG_DEFAULT_BACKEND);
- if (defaultBack != null) {
- if (configuration.backends.containsKey(defaultBack)) {
- configuration.defaultBackend = defaultBack;
- } else {
- if (logger.isWarnEnabled()) {
- logger.warn(WARN_BACKEND_NOT_FOUND1 + defaultBack
- + WARN_BACKEND_NOT_FOUND2);
- }
- // This is not fatal will use first backend.
- }
- }
- }
+ // Load plugin configuration
+ pluginConfiguration = new Properties();
+ is = url.openStream();
+ pluginConfiguration.load(is);
+ is.close();
- private void initDirtyState(Properties globalProp) {
- String useDirty = globalProp.getProperty(CONFIG_DIRTY);
- if (Boolean.parseBoolean(useDirty)) {
- configuration.useDirty = true;
- }
+ // Init plugin
+ initPlugin(pluginConfiguration, filterConfiguration, type);
+ }
- if (logger.isInfoEnabled()) {
- logger.info("Use dirty state: " + configuration.useDirty); //$NON-NLS-1$
- logger.info(INFO_DEFAULT_BACKEND + configuration.defaultBackend);
- }
- }
+ }
- /**
- * Get and create instantiation listener from configuration file.
- *
- * @param globalProp
- * @throws ClassNotFoundException
- * @throws InstantiationException
- * @throws IllegalAccessException
- */
+ private void initAttributeMapping(Properties globalProp) throws ServletException {
+ for (Object key : globalProp.keySet()) {
+ String paramName = (String) key;
+ if (paramName.startsWith(CONFIG_ATTRIBUTE_PREFIX)) {
+ String attrName = paramName.substring(CONFIG_ATTRIBUTE_PREFIX.length());
+ String backend = globalProp.getProperty(paramName);
+ configuration.backendsAttributeMapping.put(attrName, backend);
- private void initInstantiationListener(Properties globalProp)
- throws ClassNotFoundException, InstantiationException,
- IllegalAccessException {
- String clazz = globalProp.getProperty(CONFIG_INSTANTIATION_LISTENER);
- if (!StringUtils.isEmpty(clazz)) {
- @SuppressWarnings("unchecked")
- Class<IObjectInstantiationListener> backClazz = (Class<IObjectInstantiationListener>) Class
- .forName(clazz);
+ // Ensure backend is available
+ if (!configuration.backends.containsKey(backend)) {
+ throw new ServletException("Attributes are mapped on backend " + backend //$NON-NLS-1$
+ + " but it is not installed."); //$NON-NLS-1$
+ }
+ }
+ }
+ }
- // Create new instance
- instantiationListener = backClazz.newInstance();
- instantiationListener.setServletContext(servletContext);
- logger.info("Using instantiation listener {}", clazz); //$NON-NLS-1$
- }
- }
+ private void initDefaultBackend(Properties globalProp) {
+ String defaultBack = globalProp.getProperty(CONFIG_DEFAULT_BACKEND);
+ if (defaultBack != null) {
+ if (configuration.backends.containsKey(defaultBack)) {
+ configuration.defaultBackend = defaultBack;
+ } else {
+ if (logger.isWarnEnabled()) {
+ logger.warn(WARN_BACKEND_NOT_FOUND1 + defaultBack + WARN_BACKEND_NOT_FOUND2);
+ }
+ // This is not fatal will use first backend.
+ }
+ }
+ }
- /**
- * Does backend initialization.
- *
- * @param backendProperties
- * @param globalProperties
- * @throws Exception
- */
- @SuppressWarnings("unchecked")
- private void initPlugin(Properties backendProperties,
- Properties globalProperties, String type) throws Exception {
+ private void initDirtyState(Properties globalProp) {
+ String useDirty = globalProp.getProperty(CONFIG_DIRTY);
+ if (Boolean.parseBoolean(useDirty)) {
+ configuration.useDirty = true;
+ }
- IPlugin plugin = null;
+ if (logger.isInfoEnabled()) {
+ logger.info("Use dirty state: " + configuration.useDirty); //$NON-NLS-1$
+ logger.info(INFO_DEFAULT_BACKEND + configuration.defaultBackend);
+ }
+ }
- String location = CONFIG_PLUGIN_BACKEND_IMPL;
- if (IPlugin.TYPE_REQUEST_PROCESSOR.equals(type)) {
- location = CONFIG_PLUGIN_PROCESSOR_IMPL;
- }
+ /**
+ * Get and create instantiation listener from configuration file.
+ *
+ * @param globalProp
+ * @throws ClassNotFoundException
+ * @throws InstantiationException
+ * @throws IllegalAccessException
+ */
- if (instantiationListener != null) {
- // Try to create class using listener
- plugin = (IPlugin) instantiationListener
- .getInstance((String) backendProperties.get(location));
- }
+ private void initInstantiationListener(Properties globalProp) throws ClassNotFoundException,
+ InstantiationException, IllegalAccessException {
+ String clazz = globalProp.getProperty(CONFIG_INSTANTIATION_LISTENER);
+ if (!StringUtils.isEmpty(clazz)) {
+ @SuppressWarnings("unchecked")
+ Class<IObjectInstantiationListener> backClazz = (Class<IObjectInstantiationListener>) Class.forName(clazz);
- if (plugin == null) {
- // backend is still null, try to create it.
+ // Create new instance
+ instantiationListener = backClazz.newInstance();
+ instantiationListener.setServletContext(servletContext);
+ logger.info("Using instantiation listener {}", clazz); //$NON-NLS-1$
+ }
+ }
- // Get class
- String clazz = (String) backendProperties.get(location);
- Class<IPlugin> backClazz = (Class<IPlugin>) Class.forName(clazz);
+ /**
+ * Does backend initialization.
+ *
+ * @param backendProperties
+ * @param globalProperties
+ * @throws Exception
+ */
+ @SuppressWarnings("unchecked")
+ private void initPlugin(Properties backendProperties, Properties globalProperties, String type) throws Exception {
- // Create new instance
- plugin = backClazz.newInstance();
- }
+ IPlugin plugin = null;
- // Load configuration from global.
- HashMap<String, String> conf = new HashMap<String, String>();
- String paramName = null;
- String prefix = null;
- String attrName = null;
- for (Object key : globalProperties.keySet()) {
- paramName = (String) key;
- prefix = plugin.getId() + DOT;
- if (paramName.startsWith(prefix)) {
- attrName = paramName.substring(prefix.length());
- conf.put(attrName, globalProperties.getProperty(paramName));
- }
- }
+ String location = CONFIG_PLUGIN_BACKEND_IMPL;
+ if (IPlugin.TYPE_REQUEST_PROCESSOR.equals(type)) {
+ location = CONFIG_PLUGIN_PROCESSOR_IMPL;
+ }
- // Init backend
- plugin.init(conf);
+ if (instantiationListener != null) {
+ // Try to create class using listener
+ plugin = (IPlugin) instantiationListener.getInstance((String) backendProperties.get(location));
+ }
- // Toggle buffering on if necessary
- if (plugin.isBufferingRequired()) {
- configuration.isBufferingRequired = true;
- if (logger.isInfoEnabled()) {
- logger.info("Plugin " + plugin.getId() + INFO_BUFFERING);
- }
- }
+ if (plugin == null) {
+ // backend is still null, try to create it.
- // Specific handling for request processors
- if (IPlugin.TYPE_REQUEST_PROCESSOR.equals(type)) {
- configuration.requestProcessors.add((IRequestProcessor) plugin);
+ // Get class
+ String clazz = (String) backendProperties.get(location);
+ Class<IPlugin> backClazz = (Class<IPlugin>) Class.forName(clazz);
- // Some info output.
- if (logger.isInfoEnabled()) {
- logger.info(INFO_REQUEST_PROCESSOR + plugin.getId()
- + INFO_READY);
- }
- }
+ // Create new instance
+ plugin = backClazz.newInstance();
+ }
- // Specific handling for backends
- if (IPlugin.TYPE_BACKEND.equals(type)) {
- configuration.backends
- .put(plugin.getId(), (ISessionBackend) plugin);
+ // Load configuration from global.
+ HashMap<String, String> conf = new HashMap<String, String>();
+ String paramName = null;
+ String prefix = null;
+ String attrName = null;
+ for (Object key : globalProperties.keySet()) {
+ paramName = (String) key;
+ prefix = plugin.getId() + DOT;
+ if (paramName.startsWith(prefix)) {
+ attrName = paramName.substring(prefix.length());
+ conf.put(attrName, globalProperties.getProperty(paramName));
+ }
+ }
- // Some info output.
- if (logger.isInfoEnabled()) {
- logger.info(INFO_BACKEND + plugin.getId() + INFO_READY);
- }
+ // Init backend
+ plugin.init(conf);
- // First backend is default.
- if (StringUtils.isEmpty(configuration.defaultBackend)) {
- configuration.defaultBackend = plugin.getId();
- }
- }
+ // Toggle buffering on if necessary
+ applyBuffering(plugin.isBufferingRequired(), plugin.getId());
- }
+ // Specific handling for request processors
+ if (IPlugin.TYPE_REQUEST_PROCESSOR.equals(type)) {
+ configuration.requestProcessors.add((IRequestProcessor) plugin);
- /**
- * Check if this request is excluded from the process.
- *
- * @param httpRequest
- * HTTP request
- * @return true if the URI requested match an excluded pattern
- */
- private boolean isExcluded(HttpServletRequest httpRequest) {
- if (this.excludePatterns == null) {
- return false;
- }
+ // Some info output.
+ if (logger.isInfoEnabled()) {
+ logger.info(INFO_REQUEST_PROCESSOR + plugin.getId() + INFO_READY);
+ }
+ }
- String uri = httpRequest.getRequestURI();
- if (logger.isDebugEnabled()) {
- logger.debug("Check URI : " + uri);
- }
+ // Specific handling for backends
+ if (IPlugin.TYPE_BACKEND.equals(type)) {
+ configuration.backends.put(plugin.getId(), (ISessionBackend) plugin);
- try {
- uri = new URI(uri).normalize().toString();
+ // Some info output.
+ if (logger.isInfoEnabled()) {
+ logger.info(INFO_BACKEND + plugin.getId() + INFO_READY);
+ }
- for (Pattern pattern : this.excludePatterns) {
- if (pattern.matcher(uri).matches()) {
- if (logger.isInfoEnabled()) {
- logger.info("URI excluded : " + uri);
- }
- return true;
- }
- }
+ // First backend is default.
+ if (StringUtils.isEmpty(configuration.defaultBackend)) {
+ configuration.defaultBackend = plugin.getId();
+ }
+ }
- } catch (URISyntaxException e) {
- logger.warn(
- "The following URI has a bad syntax. The request will be processed by the filter. URI : "
- + uri, e);
- }
+ }
- return false;
- }
+ /**
+ * Check if this request is excluded from the process.
+ *
+ * @param httpRequest
+ * HTTP request
+ * @return true if the URI requested match an excluded pattern
+ */
+ private boolean isExcluded(HttpServletRequest httpRequest) {
+ if (this.excludePatterns == null) {
+ return false;
+ }
+
+ String uri = httpRequest.getRequestURI();
+ if (logger.isDebugEnabled()) {
+ logger.debug("Check URI : " + uri);
+ }
+
+ try {
+ uri = new URI(uri).normalize().toString();
+
+ for (Pattern pattern : this.excludePatterns) {
+ if (pattern.matcher(uri).matches()) {
+ if (logger.isInfoEnabled()) {
+ logger.info("URI excluded : " + uri);
+ }
+ return true;
+ }
+ }
+
+ } catch (URISyntaxException e) {
+ logger.warn(
+ "The following URI has a bad syntax. The request will be processed by the filter. URI : " + uri, e);
+ }
+
+ return false;
+ }
}
Modified: trunk/stateless-core/src/main/java/net/sourceforge/statelessfilter/session/StatelessSession.java
===================================================================
--- trunk/stateless-core/src/main/java/net/sourceforge/statelessfilter/session/StatelessSession.java 2011-06-15 10:39:12 UTC (rev 81)
+++ trunk/stateless-core/src/main/java/net/sourceforge/statelessfilter/session/StatelessSession.java 2011-07-24 21:51:13 UTC (rev 82)
@@ -39,7 +39,6 @@
* @author Nicolas Richeton - Capgemini
*
*/
-@SuppressWarnings("deprecation")
public class StatelessSession implements HttpSession, Serializable {
final private static String SECURE_ALGORITHM = "SHA1PRNG"; //$NON-NLS-1$
Modified: trunk/stateless-core/src/main/java/net/sourceforge/statelessfilter/wrappers/StatelessRequestWrapper.java
===================================================================
--- trunk/stateless-core/src/main/java/net/sourceforge/statelessfilter/wrappers/StatelessRequestWrapper.java 2011-06-15 10:39:12 UTC (rev 81)
+++ trunk/stateless-core/src/main/java/net/sourceforge/statelessfilter/wrappers/StatelessRequestWrapper.java 2011-07-24 21:51:13 UTC (rev 82)
@@ -42,269 +42,258 @@
*
*/
public class StatelessRequestWrapper extends HttpServletRequestWrapper {
- private static final String INFO_USES = "{} uses {}"; //$NON-NLS-1$
- private static final String INFO_USES_DEFAULT = "{} uses default {}"; //$NON-NLS-1$
- // private static final String WARN_SESSION_SYNC = "Session are not synchronized between backends. Reseting..."; //$NON-NLS-1$
- Configuration backends = null;
- Logger logger = LoggerFactory.getLogger(StatelessRequestWrapper.class);
- HttpServletRequest originalRequest = null;
+ private static final String INFO_USES = "{} uses {}"; //$NON-NLS-1$
+ private static final String INFO_USES_DEFAULT = "{} uses default {}"; //$NON-NLS-1$
+ // private static final String WARN_SESSION_SYNC = "Session are not synchronized between backends. Reseting..."; //$NON-NLS-1$
+ Configuration backends = null;
+ Logger logger = LoggerFactory.getLogger(StatelessRequestWrapper.class);
+ HttpServletRequest originalRequest = null;
- StatelessSession session = null;
+ StatelessSession session = null;
+ private boolean sessionWritten;
- /**
- * Create a new request wrapper.
- *
- * @param request
- * @param backends
- */
- public StatelessRequestWrapper(HttpServletRequest request,
- Configuration backends) {
- super(request);
- originalRequest = request;
- this.backends = backends;
- }
+ public boolean isSessionWritten() {
+ return sessionWritten;
+ }
- /**
- * Returns real server session.
- *
- * @return
- */
- public HttpSession getServerSession() {
- return super.getSession();
- }
+ /**
+ * Create a new request wrapper.
+ *
+ * @param request
+ * @param backends
+ */
+ public StatelessRequestWrapper(HttpServletRequest request, Configuration backends) {
+ super(request);
+ originalRequest = request;
+ this.backends = backends;
+ }
- /**
- * (non-Javadoc)
- *
- * @see javax.servlet.http.HttpServletRequestWrapper#getSession()
- */
- @Override
- public HttpSession getSession() {
- if (session == null) {
- try {
- session = createSession();
- } catch (NoSuchAlgorithmException e) {
- throw new RuntimeException(e);
- }
- }
- return session;
- }
+ /**
+ * Returns real server session.
+ *
+ * @return
+ */
+ public HttpSession getServerSession() {
+ return super.getSession();
+ }
- /**
- * (non-Javadoc)
- *
- * @see javax.servlet.http.HttpServletRequestWrapper#getSession(boolean)
- */
- @Override
- public HttpSession getSession(boolean create) {
- if (create) {
- return getSession();
- }
+ /**
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.http.HttpServletRequestWrapper#getSession()
+ */
+ @Override
+ public HttpSession getSession() {
+ if (session == null) {
+ try {
+ session = createSession();
+ } catch (NoSuchAlgorithmException e) {
+ throw new RuntimeException(e);
+ }
+ }
+ return session;
+ }
- if (session == null) {
- try {
- session = restoreSession();
- } catch (NoSuchAlgorithmException e) {
- throw new RuntimeException(e);
- }
- }
+ /**
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.http.HttpServletRequestWrapper#getSession(boolean)
+ */
+ @Override
+ public HttpSession getSession(boolean create) {
+ if (create) {
+ return getSession();
+ }
- return session;
- }
+ if (session == null) {
+ try {
+ session = restoreSession();
+ } catch (NoSuchAlgorithmException e) {
+ throw new RuntimeException(e);
+ }
+ }
- /**
- * Stores session in backends
- *
- * @param myrequest
- * @param myresponse
- * @throws IOException
- */
- public void writeSession(HttpServletRequest myrequest,
- HttpServletResponse myresponse) throws IOException {
+ return session;
+ }
- // If there is a session (session requested by the application)
- if (session != null) {
+ /**
+ * Stores session in backends
+ *
+ * @param myrequest
+ * @param myresponse
+ * @throws IOException
+ */
+ public void writeSession(HttpServletRequest myrequest, HttpServletResponse myresponse) throws IOException {
- // Session has changed ?
- if (backends.useDirty && !session.isDirty()) {
- logger.info("Session has not changed."); //$NON-NLS-1$
- return;
- }
+ // If there is a session (session requested by the application)
+ if (session != null) {
- long requestId = System.currentTimeMillis();
- session.setNew(false);
+ // Session has changed ?
+ if (backends.useDirty && !session.isDirty()) {
+ logger.info("Session has not changed."); //$NON-NLS-1$
+ return;
+ }
- // Dispatch attributes between backends according to configuration
+ long requestId = System.currentTimeMillis();
+ session.setNew(false);
- // Session attributes
- Map<String, Object> sessionAttributes = session.getContent();
- // Session attributes which were modified during the request
- List<String> modifiedAttributes = session.getDirtyAttributes();
+ // Dispatch attributes between backends according to configuration
- // Backends flagged as dirty during dispatch
- List<String> modifiedBackends = new ArrayList<String>();
+ // Session attributes
+ Map<String, Object> sessionAttributes = session.getContent();
+ // Session attributes which were modified during the request
+ List<String> modifiedAttributes = session.getDirtyAttributes();
- // Remaining modified attributes to process
- List<String> remainingModifiedAttributes = new ArrayList<String>(
- modifiedAttributes);
- // Attributes for each backend
- Map<String, ISessionData> attributesDispatched = new HashMap<String, ISessionData>();
- Map<String, List<String>> modifiedAttributesDispatched = new HashMap<String, List<String>>();
+ // Backends flagged as dirty during dispatch
+ List<String> modifiedBackends = new ArrayList<String>();
- for (String name : sessionAttributes.keySet()) {
- if (isAttributeMapped(name)) {
- getBackendSessionData(attributesDispatched,
- backends.backendsAttributeMapping.get(name),
- requestId).getContent().put(name,
- sessionAttributes.get(name));
+ // Remaining modified attributes to process
+ List<String> remainingModifiedAttributes = new ArrayList<String>(modifiedAttributes);
+ // Attributes for each backend
+ Map<String, ISessionData> attributesDispatched = new HashMap<String, ISessionData>();
+ Map<String, List<String>> modifiedAttributesDispatched = new HashMap<String, List<String>>();
- setModified(modifiedBackends, modifiedAttributes, name);
+ for (String name : sessionAttributes.keySet()) {
+ if (isAttributeMapped(name)) {
+ getBackendSessionData(attributesDispatched, backends.backendsAttributeMapping.get(name), requestId)
+ .getContent().put(name, sessionAttributes.get(name));
- logger.info(INFO_USES, name,
- backends.backendsAttributeMapping.get(name));
+ setModified(modifiedBackends, modifiedAttributes, name);
-...
[truncated message content] |