[Statelessfilter-commits] SF.net SVN: statelessfilter:[97] trunk/stateless-core/src/main/java/net/
Status: Beta
Brought to you by:
nricheton
|
From: <nri...@us...> - 2011-12-23 16:02:14
|
Revision: 97
http://statelessfilter.svn.sourceforge.net/statelessfilter/?rev=97&view=rev
Author: nricheton
Date: 2011-12-23 16:02:07 +0000 (Fri, 23 Dec 2011)
Log Message:
-----------
cleanup
Removed Paths:
-------------
trunk/stateless-core/src/main/java/net/sourceforge/statelessfilter/wrappers/StatelessRequestWrapperDev.java
Deleted: trunk/stateless-core/src/main/java/net/sourceforge/statelessfilter/wrappers/StatelessRequestWrapperDev.java
===================================================================
--- trunk/stateless-core/src/main/java/net/sourceforge/statelessfilter/wrappers/StatelessRequestWrapperDev.java 2011-12-23 15:55:50 UTC (rev 96)
+++ trunk/stateless-core/src/main/java/net/sourceforge/statelessfilter/wrappers/StatelessRequestWrapperDev.java 2011-12-23 16:02:07 UTC (rev 97)
@@ -1,356 +0,0 @@
-/*
- * Copyright 2009-2010 Capgemini Licensed under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with the
- * License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations under
- * the License.
- */
-package net.sourceforge.statelessfilter.wrappers;
-
-import java.io.IOException;
-import java.security.NoSuchAlgorithmException;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletRequestWrapper;
-import javax.servlet.http.HttpServletResponse;
-import javax.servlet.http.HttpSession;
-
-import net.sourceforge.statelessfilter.backend.ISessionBackend;
-import net.sourceforge.statelessfilter.backend.ISessionData;
-import net.sourceforge.statelessfilter.filter.Configuration;
-import net.sourceforge.statelessfilter.session.SessionData;
-import net.sourceforge.statelessfilter.session.StatelessSession;
-
-import org.apache.commons.lang.StringUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * Stateless request wrapper
- *
- * @author Nicolas Richeton - Capgemini
- *
- */
-public class StatelessRequestWrapperDev 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(StatelessRequestWrapperDev.class);
- HttpServletRequest originalRequest = null;
-
- StatelessSession session = null;
-
- /**
- * Create a new request wrapper.
- *
- * @param request
- * @param backends
- */
- public StatelessRequestWrapperDev(HttpServletRequest request,
- Configuration backends) {
- super(request);
- originalRequest = request;
- this.backends = backends;
- }
-
- /**
- * Returns real server session.
- *
- * @return
- */
- public HttpSession getServerSession() {
- return super.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;
- }
-
- /**
- * (non-Javadoc)
- *
- * @see javax.servlet.http.HttpServletRequestWrapper#getSession(boolean)
- */
- @Override
- public HttpSession getSession(boolean create) {
- if (create) {
- return getSession();
- }
-
- if (session == null) {
- try {
- session = restoreSession();
- } catch (NoSuchAlgorithmException e) {
- throw new RuntimeException(e);
- }
- }
-
- return session;
- }
-
- /**
- * Stores session in backends
- *
- * @param myrequest
- * @param myresponse
- * @throws IOException
- */
- public void writeSession(HttpServletRequest myrequest,
- HttpServletResponse myresponse) throws IOException {
-
- // If there is a session (session requested by the application)
- if (session != null) {
-
- // Session has changed ?
- if (backends.useDirty && !session.isDirty()) {
- logger.info("Session has not changed."); //$NON-NLS-1$
- return;
- }
-
- long requestId = System.currentTimeMillis();
- session.setNew(false);
-
- // Dispatch attributes between backends according to configuration
-
- // Session attributes
- Map<String, Object> sessionAttributes = session.getContent();
- // Session attributes which were modified during the request
- List<String> modifiedAttributes = session.getDirtyAttributes();
-
- // Backends flagged as dirty during dispatch
- List<String> modifiedBackends = new ArrayList<String>();
-
- // 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>>();
-
- for (String name : sessionAttributes.keySet()) {
- if (isAttributeMapped(name)) {
- // Add attribute to backend session
- getBackendSessionData(attributesDispatched,
- backends.backendsAttributeMapping.get(name),
- requestId).getContent().put(name,
- sessionAttributes.get(name));
-
- // Add attribute name as modified for this backend
- if( remainingModifiedAttributes.contains(name))
- getBackendModifiedAttributes(modifiedAttributesDispatched,
- backends.backendsAttributeMapping.get(name)).add(
- name);
-
- // Set backend modified.
- setModified(modifiedBackends, modifiedAttributes, name);
-
- logger.info(INFO_USES, name,
- backends.backendsAttributeMapping.get(name));
-
- } else {
- getBackendSessionData(attributesDispatched,
- backends.defaultBackend, requestId).getContent()
- .put(name, sessionAttributes.get(name));
-
- if( remainingModifiedAttributes.contains(name))
- getBackendModifiedAttributes(modifiedAttributesDispatched,
- backends.defaultBackend).add(name);
-
- setModified(modifiedBackends, modifiedAttributes, name);
- logger.info(INFO_USES_DEFAULT, name,
- backends.defaultBackend);
-
- }
-
- // Remove attribute from remaining attributes to process
- remainingModifiedAttributes.remove(name);
- }
-
- // Process remaining attributes
- for (String name : remainingModifiedAttributes) {
- if (isAttributeMapped(name)) {
- getBackendModifiedAttributes(modifiedAttributesDispatched,
- backends.backendsAttributeMapping.get(name)).add(
- name);
-
- setModified(modifiedBackends, modifiedAttributes, name);
- logger.info(INFO_USES, name,
- backends.backendsAttributeMapping.get(name));
- } else {
- getBackendModifiedAttributes(modifiedAttributesDispatched,
- backends.defaultBackend).add(name);
-
- setModified(modifiedBackends, modifiedAttributes, name);
-
- logger.info(INFO_USES_DEFAULT, name,
- backends.defaultBackend);
- }
- }
-
- if (session.isPropertyDirty()) {
- // Force update on all backends.
- logger.info("Session properties have changed. Forcing update on all backends."); //$NON-NLS-1$
-
- for (String back : backends.backends.keySet()) {
- ISessionBackend backend = backends.backends.get(back);
- backend.save(
- getBackendSessionData(attributesDispatched, back,
- requestId), modifiedAttributesDispatched
- .get(back), originalRequest, myresponse);
- }
- } else {
- // Update only modified backends.
- for (String back : modifiedBackends) {
- ISessionBackend backend = backends.backends.get(back);
- backend.save(
- getBackendSessionData(attributesDispatched, back,
- requestId), modifiedAttributesDispatched
- .get(back), originalRequest, myresponse);
- }
- }
- }
-
- }
-
- /**
- * Restore a session by querying all backends. If session cannot be
- * restored, a new one is created.
- *
- *
- * @return the restored or created session.
- * @throws NoSuchAlgorithmException
- */
- private StatelessSession createSession() throws NoSuchAlgorithmException {
-
- StatelessSession s = restoreSession();
-
- if (s == null) {
- s = new StatelessSession(this);
- s.init(true);
- }
-
- return s;
- }
-
- /**
- * Get current data for session backend. Creates a new ISessionData if
- * necessary.
- *
- * @param dispatched
- * @param backendName
- * @param requestId
- * @return
- */
- private ISessionData getBackendSessionData(
- Map<String, ISessionData> dispatched, String backendName,
- long requestId) {
-
- // If session data exists for this backend, return immediately.
- if (dispatched.containsKey(backendName)) {
- return dispatched.get(backendName);
- }
-
- // Else create empty session data for this backend.
- SessionData data = new SessionData();
- data.setId(session.getId());
- data.setCreationTime(session.getCreationTime());
- data.setValid(session.isValid());
- data.setRequestId(requestId);
- dispatched.put(backendName, data);
- return data;
- }
-
- private List<String> getBackendModifiedAttributes(
- Map<String, List<String>> modifiedAttributesDispatched,
- String backendName) {
- if (modifiedAttributesDispatched.containsKey(backendName)) {
- return modifiedAttributesDispatched.get(backendName);
- }
-
- List<String> result = new ArrayList<String>();
- modifiedAttributesDispatched.put(backendName, result);
-
- return result;
- }
-
- /**
- * Checks if attrName is specifically mapped to a session backend in
- * configuration.
- *
- * @param attrName
- * session attribute name.
- * @return true if mapped to a session backend, false if using default.
- */
- private boolean isAttributeMapped(String attrName) {
- if (backends != null && backends.backendsAttributeMapping != null) {
- return backends.backendsAttributeMapping.containsKey(attrName);
- }
-
- return false;
- }
-
- private StatelessSession restoreSession() throws NoSuchAlgorithmException {
- StatelessSession s = new StatelessSession(this);
- ISessionData data = null;
- boolean restored = false;
- // long requestId = -1;
-
- s.init(false);
- for (ISessionBackend back : backends.backends.values()) {
- data = back.restore(originalRequest);
-
- // Mark session restored if at least one backend returned a session
- if (data != null) {
- restored = true;
- s.merge(data);
- }
- }
-
- // Reset session
- if (!restored) {
- return null;
- }
- return s;
- }
-
- private void setModified(List<String> modifiedBackends,
- List<String> modifiedAttributes, String attributeName) {
- String backend = backends.backendsAttributeMapping.get(attributeName);
-
- if (StringUtils.isEmpty(backend)) {
- backend = backends.defaultBackend;
- }
-
- if (modifiedAttributes.contains(attributeName)
- && !modifiedBackends.contains(backend)) {
-
- modifiedBackends.add(backend);
-
- logger.info("Flagging backend {} as modified", backend); //$NON-NLS-1$
- }
- }
-}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|