View Javadoc

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 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  
17  package org.apache.jetspeed.util;
18  
19  import org.apache.turbine.util.RunData;
20  
21  import org.apache.jetspeed.portal.portlets.VelocityPortlet;
22  import org.apache.jetspeed.portal.Portlet;
23  import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
24  import org.apache.jetspeed.services.logging.JetspeedLogger;
25  import org.apache.jetspeed.services.persistence.PersistenceManager;
26  import org.apache.jetspeed.portal.PortletInstance;
27  
28  /***
29   * Defines standard utility functions on session attributes
30   *
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   */
35  public class PortletSessionState
36  {
37      /***
38       * Static initialization of the logger for this class
39       */    
40      private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(PortletSessionState.class.getName());    
41  
42      /***
43       * Portlet attribute to save config state.
44       */
45      public static final String CONFIG_CHANGED = "config_changed";
46  
47      /***
48      Returns the parameter for this name from the Temp - session object
49      */
50      public static Object getAttribute(RunData rundata, String attrName)
51      {
52          return rundata.getUser().getTemp(attrName);
53      }
54  
55      public static Object getAttribute(RunData rundata, String attrName, Object defValue)
56      {
57          Object o = rundata.getUser().getTemp(attrName, defValue);
58          if (o instanceof java.lang.String && !(defValue instanceof java.lang.String))
59          {
60              return defValue;
61          }
62          return o;
63      }
64  
65      /***
66       * Sets the parameter for this name in Temp
67       * Uses the portlet parameter to generate a unique key.
68       */
69      public static void setAttribute(RunData rundata, String attrName, Object attrValue)
70      {
71          rundata.getUser().setTemp(attrName, attrValue);
72      }
73  
74      /***
75       * Clears the parameter for this name from Temp
76       * Uses the portlet parameter to generate a unique key.
77       */
78      public static void clearAttribute(RunData rundata, String attrName)
79      {
80          rundata.getUser().removeTemp(attrName);
81      }
82      
83      /***
84       * Returns the parameter for this name from the Temp - session object
85       * Uses the portlet parameter to generate a unique key.
86       */
87      public static Object getAttribute(Portlet portlet, RunData rundata, String attrName)
88      {
89          return rundata.getUser().getTemp(generateKey(portlet, attrName));
90      }
91  
92      /***
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 portlet
103      * @param rundata
104      * @param attrName
105      * @return attribute value
106      */
107     public static Object getAttributeWithFallback(Portlet portlet, RunData rundata, String attrName)
108     {
109         Object result = null;
110 
111         // Look in the request first
112         if(isMyRequest(rundata, portlet)) 
113         {
114             result =  rundata.getParameters().getString(attrName);
115             if (result != null) 
116             {
117                 if (result.toString().trim().equalsIgnoreCase("")) 
118                 {
119                     clearAttribute(portlet, rundata, attrName);
120                     result = null;
121                 } 
122                 else 
123                 {
124                     setAttribute(portlet, rundata, attrName, result);
125                 }
126             }
127         }
128 
129         // Look in the session attributes
130         if (result == null) 
131         {
132             result = getAttribute(portlet, rundata, attrName);
133         }
134 
135         // Look in the instance attributes
136         if (result == null)
137         {
138             result = portlet.getAttribute(attrName, null, rundata);
139         }
140         
141         // Finally, look in the config
142         if (result == null) 
143         {
144             result = portlet.getPortletConfig().getInitParameter(attrName);
145         }
146 
147         return result;
148 
149     }
150 
151     /***
152      * Returns true if the request pertains to current portlet instance. It assumes that the portlet interested in
153      * recognizing its own requests, has a hidden input "js_peid". For backwards compatibility, if "js_peid" was
154      * not set, this method will return TRUE.
155      * 
156      * @param rundata
157      * @return boolean
158      */
159     public static boolean isMyRequest(RunData rundata, Portlet portlet) {
160 
161         // 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");
164         if (requestPeid == null || requestPeid.equalsIgnoreCase(""))
165         {
166             return true;
167         }
168 
169         // If the portlet does not have its id set, assume that the portlet is not interested
170         // in isMyRequest functionality and return TRUE.
171         if (portlet == null || portlet.getID() == null)
172         {
173             return true;
174         }
175 
176         // Retrieve portlet instance
177         String peId = null;
178         PortletInstance instance = PersistenceManager.getInstance(portlet, rundata);
179         if (instance != null)
180         {
181             peId = instance.getPortlet().getID();
182         }
183 
184         // Compare the ids
185         if (peId != null && peId.equals(requestPeid))
186         {
187             return true;
188         }
189         else
190         {
191             return false;
192         }
193     }
194 
195     /***
196      * Sets the parameter for this name in Temp
197      * Uses the portlet parameter to generate a unique key.
198      */
199     public static void setAttribute(Portlet portlet,
200                              RunData rundata,
201                              String attrName,
202                              Object attrValue)
203     {
204         rundata.getUser().setTemp(generateKey(portlet, attrName), attrValue);
205     }
206 
207     /***
208      * Clears the parameter for this name from Temp
209      * Uses the portlet parameter to generate a unique key.
210      */
211     public static void clearAttribute(Portlet portlet, RunData rundata, String attrName)
212     {
213         rundata.getUser().removeTemp(generateKey(portlet, attrName));
214     }
215 
216     /***
217      * Uses the portlet parameter to generate a unique key, using the portlet.getId.
218      */
219     protected static String generateKey(Portlet portlet, String name)
220     {
221         if (portlet != null)
222         {
223             return (portlet.getID()+"."+name);
224         }
225         else
226         {
227             logger.error("PortletSessionState: Passed null Velocity Portlet for name: " + name);
228             return name;
229         }
230     }
231 
232     /***
233      * Returns the parameter for this name from the Temp - session object
234      * Uses the portlet parameter to generate a unique key.
235      */
236     public static Object getAttribute(VelocityPortlet portlet, RunData rundata, String attrName)
237     {
238         return getAttribute((Portlet) portlet, rundata, attrName);
239     }
240 
241     /***
242      * Sets the parameter for this name in Temp
243      * Uses the portlet parameter to generate a unique key.
244      */
245     public static void setAttribute(VelocityPortlet portlet,
246                              RunData rundata,
247                              String attrName,
248                              Object attrValue)
249     {
250         setAttribute((Portlet) portlet, rundata, attrName, attrValue);
251     }
252 
253     /***
254      * Clears the parameter for this name from Temp
255      * Uses the portlet parameter to generate a unique key.
256      */
257     public static void clearAttribute(VelocityPortlet portlet, RunData rundata, String attrName)
258     {
259         clearAttribute((Portlet) portlet, rundata, attrName);
260     }
261 
262     /***
263      * The portlet config has changed. Calling getPortletConfigChanged returns the current state
264      * and resets it.
265      * 
266      * @param portlet
267      * @param rundata
268      */
269     public static void setPortletConfigChanged(Portlet portlet, RunData rundata)
270     {
271         setAttribute(portlet, rundata, CONFIG_CHANGED, "true");
272     }
273 
274     /***
275      * Returns current state of portlet config and resets it if set.
276      * 
277      * @param portlet
278      * @param rundata
279      * @return TRUE if portlet config has changed
280      */
281     public static boolean getPortletConfigChanged(Portlet portlet, RunData rundata)
282     {
283         String state = (String) getAttribute(portlet, rundata, CONFIG_CHANGED);
284         if (state != null)
285         {
286             clearAttribute(portlet, rundata, CONFIG_CHANGED);
287         }
288 
289         return state != null;
290     }
291 
292 }