1 16 package org.apache.cocoon.acting; 17 18 import org.apache.avalon.framework.configuration.Configuration; 19 import org.apache.avalon.framework.configuration.ConfigurationException; 20 import org.apache.avalon.framework.parameters.Parameters; 21 import org.apache.avalon.framework.thread.ThreadSafe; 22 import org.apache.cocoon.environment.ObjectModelHelper; 23 import org.apache.cocoon.environment.Redirector; 24 import org.apache.cocoon.environment.Request; 25 import org.apache.cocoon.environment.Session; 26 import org.apache.cocoon.environment.SourceResolver; 27 28 import java.util.HashMap ; 29 import java.util.Map ; 30 31 82 public class SessionStateAction 83 extends AbstractConfigurableAction 84 implements ThreadSafe { 85 86 protected String statekey = "org.apache.cocoon.SessionState"; 87 protected String newstate = null; 88 protected int sublevels = 0; 89 protected int mylevel = 0; 90 91 94 public void configure(Configuration conf) throws ConfigurationException { 95 super.configure(conf); 96 97 if (settings.containsKey("state-key-prefix")) { 98 statekey = (String ) settings.get("state-key-prefix"); 99 } 100 if (settings.containsKey("new-state")) { 101 newstate = (String ) settings.get("new-state"); 102 } 103 if (settings.containsKey("sub-levels")) { 104 sublevels = Integer.parseInt((String ) settings.get("sub-levels")); 105 } 106 if (settings.containsKey("state-level")) { 107 mylevel = Integer.parseInt((String ) settings.get("state-level")); 108 } 109 } 110 111 public Map act(Redirector redirector, 112 SourceResolver resolver, 113 Map objectModel, 114 String src, 115 Parameters par) throws Exception { 116 117 Request request = ObjectModelHelper.getRequest(objectModel); 118 119 String newstate = par.getParameter("new-state", this.newstate); 121 String statekey = par.getParameter("state-key", this.statekey); 122 int sublevels = par.getParameterAsInteger("sublevels", this.sublevels); 123 int mylevel = par.getParameterAsInteger("state-level", this.mylevel); 124 125 if (newstate == null) { 126 if (this.getLogger().isDebugEnabled()) { 127 getLogger().error("new-state is null"); 128 } 129 return null; 130 } 131 132 if (request != null) { 133 Session session = request.getSession(false); 134 135 if (session != null && request.isRequestedSessionIdValid()) { 136 String oldstate = null; 137 if (sublevels == 0) { 138 oldstate = (String ) session.getAttribute(statekey); 139 session.setAttribute(statekey, newstate); 140 if (this.getLogger().isDebugEnabled()) { 141 getLogger().debug(statekey + "=" + newstate); 142 } 143 144 } else { oldstate = (String )session.getAttribute(statekey + mylevel); 146 for (int i = mylevel + 1; i <= sublevels; i++) { 147 session.removeAttribute(statekey + i); 148 if (this.getLogger().isDebugEnabled()) { 149 getLogger().debug("Remove " + statekey + i); 150 } 151 } 152 session.setAttribute(statekey + mylevel, newstate); 153 if (this.getLogger().isDebugEnabled()) { 154 getLogger().debug(statekey + mylevel + "=" + newstate); 155 } 156 } 157 if (this.getLogger().isDebugEnabled()) { 158 getLogger().debug("Transition " + oldstate + " -> " + newstate); 159 } 160 161 HashMap map = new HashMap (1); 162 map.put("newstate", newstate); 163 return map; 164 } else { 165 getLogger().warn( 166 "A session object was not present or no longer valid"); 167 return null; 168 } 169 } else { 170 getLogger().warn("No request object"); 171 return null; 172 } 173 } 174 } 175 | Popular Tags |