1/*2 * Copyright 2000-2001,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 at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * 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 and14 * limitations under the License.15 */1617packageorg.apache.jetspeed.util;
1819import org.apache.turbine.util.RunData;
2021import org.apache.jetspeed.portal.portlets.VelocityPortlet;
22import org.apache.jetspeed.portal.Portlet;
23import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
24import org.apache.jetspeed.services.logging.JetspeedLogger;
25import org.apache.jetspeed.services.persistence.PersistenceManager;
26import org.apache.jetspeed.portal.PortletInstance;
2728/***29 * Defines standard utility functions on session attributes30 *31 * @author <a href="mailto:david@apache.org">David Sean Taylor</a>32 * @author <a href="mailto:morciuch@apache.org">Mark Orciuch</a> 33 * @version $Id: PortletSessionState.java,v 1.9 2004/02/23 03:23:42 jford Exp $34 */35publicclassPortletSessionState36 {
37/***38 * Static initialization of the logger for this class39 */40privatestaticfinalJetspeedLogger logger = JetspeedLogFactoryService.getLogger(PortletSessionState.class.getName());
4142/***43 * Portlet attribute to save config state.44 */45publicstaticfinal String CONFIG_CHANGED = "config_changed";
4647/***48 Returns the parameter for this name from the Temp - session object49 */50publicstatic Object getAttribute(RunData rundata, String attrName)
51 {
52return rundata.getUser().getTemp(attrName);
53 }
5455publicstatic Object getAttribute(RunData rundata, String attrName, Object defValue)
56 {
57 Object o = rundata.getUser().getTemp(attrName, defValue);
58if (o instanceof java.lang.String && !(defValue instanceof java.lang.String))
59 {
60return defValue;
61 }
62return o;
63 }
6465/***66 * Sets the parameter for this name in Temp67 * Uses the portlet parameter to generate a unique key.68 */69publicstaticvoid setAttribute(RunData rundata, String attrName, Object attrValue)
70 {
71 rundata.getUser().setTemp(attrName, attrValue);
72 }
7374/***75 * Clears the parameter for this name from Temp76 * Uses the portlet parameter to generate a unique key.77 */78publicstaticvoid clearAttribute(RunData rundata, String attrName)
79 {
80 rundata.getUser().removeTemp(attrName);
81 }
8283/***84 * Returns the parameter for this name from the Temp - session object85 * Uses the portlet parameter to generate a unique key.86 */87publicstatic Object getAttribute(Portlet portlet, RunData rundata, String attrName)
88 {
89return rundata.getUser().getTemp(generateKey(portlet, attrName));
90 }
9192/***93 * Returns the attribute for this name using the following search path:94 * <ul>95 * <li>request parameter</li>96 * <li>session attribute</li>97 * <li>instance attribute</li> 98 * <li>config parameter</li>99 * <ul>100 * Uses the portlet parameter to generate a unique key.101 * 102 * @param portlet103 * @param rundata104 * @param attrName105 * @return attribute value106 */107publicstatic Object getAttributeWithFallback(Portlet portlet, RunData rundata, String attrName)
108 {
109 Object result = null;
110111// Look in the request first112if(isMyRequest(rundata, portlet))
113 {
114 result = rundata.getParameters().getString(attrName);
115if (result != null)
116 {
117if (result.toString().trim().equalsIgnoreCase(""))
118 {
119 clearAttribute(portlet, rundata, attrName);
120 result = null;
121 }
122else123 {
124 setAttribute(portlet, rundata, attrName, result);
125 }
126 }
127 }
128129// Look in the session attributes130if (result == null)
131 {
132 result = getAttribute(portlet, rundata, attrName);
133 }
134135// Look in the instance attributes136if (result == null)
137 {
138 result = portlet.getAttribute(attrName, null, rundata);
139 }
140141// Finally, look in the config142if (result == null)
143 {
144 result = portlet.getPortletConfig().getInitParameter(attrName);
145 }
146147return result;
148149 }
150151/***152 * Returns true if the request pertains to current portlet instance. It assumes that the portlet interested in153 * recognizing its own requests, has a hidden input "js_peid". For backwards compatibility, if "js_peid" was154 * not set, this method will return TRUE.155 * 156 * @param rundata157 * @return boolean158 */159publicstaticboolean isMyRequest(RunData rundata, Portlet portlet) {
160161// If the request does not contain "js_peid", assume that the portlet is not interested 162// in isMyRequest functionality and return TRUE.163 String requestPeid = rundata.getParameters().getString("js_peid");
164if (requestPeid == null || requestPeid.equalsIgnoreCase(""))
165 {
166returntrue;
167 }
168169// If the portlet does not have its id set, assume that the portlet is not interested170// in isMyRequest functionality and return TRUE.171if (portlet == null || portlet.getID() == null)
172 {
173returntrue;
174 }
175176// Retrieve portlet instance177 String peId = null;
178PortletInstance instance = PersistenceManager.getInstance(portlet, rundata);
179if (instance != null)
180 {
181 peId = instance.getPortlet().getID();
182 }
183184// Compare the ids185if (peId != null && peId.equals(requestPeid))
186 {
187returntrue;
188 }
189else190 {
191return false;
192 }
193 }
194195/***196 * Sets the parameter for this name in Temp197 * Uses the portlet parameter to generate a unique key.198 */199publicstaticvoid setAttribute(Portlet portlet,
200 RunData rundata,
201 String attrName,
202 Object attrValue)
203 {
204 rundata.getUser().setTemp(generateKey(portlet, attrName), attrValue);
205 }
206207/***208 * Clears the parameter for this name from Temp209 * Uses the portlet parameter to generate a unique key.210 */211publicstaticvoid clearAttribute(Portlet portlet, RunData rundata, String attrName)
212 {
213 rundata.getUser().removeTemp(generateKey(portlet, attrName));
214 }
215216/***217 * Uses the portlet parameter to generate a unique key, using the portlet.getId.218 */219protectedstatic String generateKey(Portlet portlet, String name)
220 {
221if (portlet != null)
222 {
223return (portlet.getID()+"."+name);
224 }
225else226 {
227 logger.error("PortletSessionState: Passed null Velocity Portlet for name: " + name);
228return name;
229 }
230 }
231232/***233 * Returns the parameter for this name from the Temp - session object234 * Uses the portlet parameter to generate a unique key.235 */236publicstatic Object getAttribute(VelocityPortlet portlet, RunData rundata, String attrName)
237 {
238return getAttribute((Portlet) portlet, rundata, attrName);
239 }
240241/***242 * Sets the parameter for this name in Temp243 * Uses the portlet parameter to generate a unique key.244 */245publicstaticvoid setAttribute(VelocityPortlet portlet,
246 RunData rundata,
247 String attrName,
248 Object attrValue)
249 {
250 setAttribute((Portlet) portlet, rundata, attrName, attrValue);
251 }
252253/***254 * Clears the parameter for this name from Temp255 * Uses the portlet parameter to generate a unique key.256 */257publicstaticvoid clearAttribute(VelocityPortlet portlet, RunData rundata, String attrName)
258 {
259 clearAttribute((Portlet) portlet, rundata, attrName);
260 }
261262/***263 * The portlet config has changed. Calling getPortletConfigChanged returns the current state264 * and resets it.265 * 266 * @param portlet267 * @param rundata268 */269publicstaticvoid setPortletConfigChanged(Portlet portlet, RunData rundata)
270 {
271 setAttribute(portlet, rundata, CONFIG_CHANGED, "true");
272 }
273274/***275 * Returns current state of portlet config and resets it if set.276 * 277 * @param portlet278 * @param rundata279 * @return TRUE if portlet config has changed280 */281publicstaticboolean getPortletConfigChanged(Portlet portlet, RunData rundata)
282 {
283 String state = (String) getAttribute(portlet, rundata, CONFIG_CHANGED);
284if (state != null)
285 {
286 clearAttribute(portlet, rundata, CONFIG_CHANGED);
287 }
288289return state != null;
290 }
291292 }