KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > acting > SessionStateAction


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
29 import java.util.Map JavaDoc;
30
31 /**
32  * Store the session's current state in a session attribute.
33  *
34  * <p> To keep track of the state of a user's session, a string is
35  * stored in a session attribute in order to allow to chose between
36  * different pipelines in the sitemap accordingly.</p>
37  *
38  * <p> For added flexibility it is possible to use sub states as
39  * well. For this declare your own name for the session state
40  * attribute and give the number of sublevels plus the level to
41  * modify. (This is <b>one</b> based!) Sub states below the current
42  * one are removed from the session so that the default sub state will
43  * be reentered when the user returns. If you don't like this
44  * behaviour and prefer independent sub states, use this action
45  * several times with different attribute names rather than sub
46  * levels. </p>
47  *
48  * <p><b>Global and local parameters:</b></p>
49  *
50  * <table border="1">
51  * <tr>
52  * <td><code>state-key-prefix</code></td>
53  * <td>String that identifies the attribute that stores the session state in the
54  * session object. When sublevels are used, this is a prefix ie. the
55  * number of the level is appended to the prefix. Example prefix is
56  * "<code>__sessionState</code>", sub-levels is 2, attributes
57  * "<code>__sessionState1</code>", "<code>__sessionState2</code>", and
58  * "<code>__sessionState3</code>" will be used to store the
59  * information.
60  * </td>
61  * </tr>
62  * <tr>
63  * <td><code>new-state</code></td>
64  * <td>String that identifies the current state</td>
65  * </tr>
66  * <tr>
67  * <td><code>sub-levels</code></td>
68  * <td>Number of sub levels to use</td>
69  * </tr>
70  * <tr>
71  * <td><code>state-level</code></td>
72  * <td>Sub level to modify, this is <b>one</b> based</td>
73  * </tr>
74  * </table>
75  *
76  * @see org.apache.cocoon.matching.WildcardSessionAttributeMatcher
77  * @see org.apache.cocoon.selection.SessionAttributeSelector
78  *
79  * @author <a HREF="mailto:haul@apache.org">Christian Haul</a>
80  * @version CVS $Id: SessionStateAction.java 30932 2004-07-29 17:35:38Z vgritsenko $
81  */

82 public class SessionStateAction
83     extends AbstractConfigurableAction
84     implements ThreadSafe {
85
86     protected String JavaDoc statekey = "org.apache.cocoon.SessionState";
87     protected String JavaDoc newstate = null;
88     protected int sublevels = 0;
89     protected int mylevel = 0;
90
91     /**
92      * Configures the Action.
93      */

94     public void configure(Configuration conf) throws ConfigurationException {
95         super.configure(conf);
96
97         if (settings.containsKey("state-key-prefix")) {
98             statekey = (String JavaDoc) settings.get("state-key-prefix");
99         }
100         if (settings.containsKey("new-state")) {
101             newstate = (String JavaDoc) settings.get("new-state");
102         }
103         if (settings.containsKey("sub-levels")) {
104             sublevels = Integer.parseInt((String JavaDoc) settings.get("sub-levels"));
105         }
106         if (settings.containsKey("state-level")) {
107             mylevel = Integer.parseInt((String JavaDoc) settings.get("state-level"));
108         }
109     }
110
111     public Map JavaDoc act(Redirector redirector,
112                    SourceResolver resolver,
113                    Map JavaDoc objectModel,
114                    String JavaDoc src,
115                    Parameters par) throws Exception JavaDoc {
116
117         Request request = ObjectModelHelper.getRequest(objectModel);
118
119         // read local settings
120
String JavaDoc newstate = par.getParameter("new-state", this.newstate);
121         String JavaDoc 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 JavaDoc oldstate = null;
137                 if (sublevels == 0) {
138                     oldstate = (String JavaDoc) session.getAttribute(statekey);
139                     session.setAttribute(statekey, newstate);
140                     if (this.getLogger().isDebugEnabled()) {
141                          getLogger().debug(statekey + "=" + newstate);
142                     }
143
144                 } else { // sublevels != 0
145
oldstate = (String JavaDoc)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 JavaDoc map = new HashMap JavaDoc(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