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   * GenericMVCAction.java
18   *
19   * Created on January 29, 2003, 2:56 PM
20   */
21  package org.apache.jetspeed.modules.actions.portlets;
22  
23  import org.apache.jetspeed.portal.Portlet;
24  import org.apache.jetspeed.portal.PortletInstance;
25  import org.apache.jetspeed.portal.portlets.GenericMVCContext;
26  import org.apache.jetspeed.portal.portlets.GenericMVCPortlet;
27  import org.apache.jetspeed.services.JetspeedSecurity;
28  import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
29  import org.apache.jetspeed.services.logging.JetspeedLogger;
30  import org.apache.jetspeed.services.persistence.PersistenceManager;
31  import org.apache.jetspeed.services.persistence.PortalPersistenceException;
32  import org.apache.jetspeed.services.rundata.JetspeedRunData;
33  import org.apache.jetspeed.util.PortletSessionState;
34  import org.apache.turbine.util.RunData;
35  import org.apache.velocity.context.Context;
36  
37  
38  /***
39   * Provides standard portal and MVC related action functionality.  Developers
40   * extend this class for thier own actions and provide implementations of the
41   * build*Context methods apropos to thier portlet needs.
42   *
43   * @author  tkuebler
44   * @version $Id: GenericMVCAction.java,v 1.8 2003/02/11 23:09:17 tkuebler Exp $
45   * @stereotype moment-interval
46   */
47  public class GenericMVCAction
48      extends PortletAction
49    {
50  
51      /***
52       * Static initialization of the logger for this class
53       */    
54      protected static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(GenericMVCAction.class.getName());      
55        
56      /*** Creates a new instance of GenericMVCAction */
57      public GenericMVCAction()
58        {
59  
60          // remove if empty at end of design phase
61        }
62  
63      // override what you need to here
64      protected void perform(RunData rundata)
65                      throws Exception
66        {
67  
68          Context context = getContext(rundata);
69  
70          if ((context != null) && (rundata.getParameters().getString("action") != null))
71            {
72  
73              // if context is already defined and Actions defined, events
74              // have already been processed, call doPerform
75              logger.debug("Action detected with action + context");
76              doPerform(rundata, context);
77            }
78          else
79            {
80  
81              // if context is null, create a new one
82              if (context == null)
83                {
84                  logger.debug("Action: building action context");
85                  context = new GenericMVCContext();
86                  rundata.getTemplateInfo().setTemplateContext("VelocityActionContext", context);
87                }
88  
89              try
90                {
91  
92                  // process implicit ActionEvent invocation
93                  logger.debug("Action: try executing events");
94  
95                  GenericMVCPortlet portlet = (GenericMVCPortlet) context.get("portlet");
96  
97                  if (portlet != null)
98                    {
99  
100                     // verify this portlet is the one requested by checking the
101                     // js_peid request var.  If there is no js_peid
102                     // do not worry a about verifing.  helps with backward compat.
103                     if (rundata.getParameters().getString("js_peid") == null || PortletSessionState.isMyRequest(rundata, portlet))
104                       {
105                         executeEvents(rundata, context);
106                       }
107                     else
108                       {
109                         logger.debug("Action: calling doPerform");
110                         doPerform(rundata, context);
111                       }
112                   }
113                 else
114                   {
115                     executeEvents(rundata, context);
116                   }
117               }
118             catch (NoSuchMethodException e)
119               {
120 
121                 // no event selected, process normal context generation
122                 logger.debug("Action: calling doPerform");
123                 
124               }
125               
126 			doPerform(rundata, context);
127           }
128       }
129 
130     public void doPerform(RunData rundata, Context context)
131                    throws Exception
132       {
133 
134         GenericMVCPortlet portlet = null;
135         JetspeedRunData jdata = (JetspeedRunData) rundata;
136         logger.debug("GenericMVCAction: retrieved context: " + context);
137 
138         if (context != null)
139           {
140             portlet = (GenericMVCPortlet) context.get("portlet");
141           }
142 
143         logger.debug("GenericMVCAction: retrieved portlet: " + portlet);
144 
145         if (portlet != null)
146           {
147 
148             //System.out.println("class = " + this.getClass().getName());
149             //rundata.getUser().setTemp(this.getClass().getName(), portlet.getID());
150             // we're bein configured
151             if ((jdata.getMode() == JetspeedRunData.CUSTOMIZE) && (portlet.getName().equals(jdata.getCustomized().getName())))
152               {
153                 logger.debug("GenericMVCAction: building customize");
154                 buildConfigureContext(portlet, context, rundata);
155 
156                 return;
157               }
158 
159             // we're maximized
160             if (jdata.getMode() == JetspeedRunData.MAXIMIZE)
161               {
162                 logger.debug("GenericMVCAction: building maximize");
163                 buildMaximizedContext(portlet, context, rundata);
164 
165                 return;
166               }
167 
168             logger.debug("GenericMVCAction: building normal");
169             buildNormalContext(portlet, context, rundata);
170           }
171       }
172 
173     /***
174      * Subclasses should override this method if they wish to
175      * build specific content when maximized. Default behavior is
176      * to do the same as normal content.
177      */
178     protected void buildMaximizedContext(Portlet portlet, Context context, RunData rundata)
179                                   throws Exception
180       {
181         buildNormalContext(portlet, context, rundata);
182       }
183 
184     /***
185      * Subclasses should override this method if they wish to
186      * provide their own customization behavior.
187      * Default is to use Portal base customizer action
188      */
189     protected void buildConfigureContext(Portlet portlet, Context context, RunData rundata)
190                                   throws Exception
191       {
192 
193         // code goes here. :)
194       }
195 
196     /***
197      * Subclasses must override this method to provide default behavior
198      * for the portlet action
199      */
200     protected void buildNormalContext(Portlet portlet, Context context, RunData rundata)
201                                throws Exception
202       {
203       }
204 
205     /***
206      * Convenience method for retreiving this action's PortletInstance
207      * @param Context context Current context object.
208      * @return Portlet the Portlet for this action
209      * @author <a href="mailto:sweaver@rippe.com">Scott Weaver</a>
210      */
211     public PortletInstance getPortletInstance(Context context)
212     {
213         return getPortlet(context).getInstance((RunData) context.get("data"));
214     }
215     
216     /***
217     * Convenience method for retreiving this action's PortletInstance
218     * attribute.
219     * @param String Attribute Name
220     * @param Context context Current context object.
221     * @return String portlet attribute
222     * @author <a href="mailto:sweaver@rippe.com">Scott Weaver</a>
223     */   
224     public String getAttribute(String attrName, Context context)
225     {
226         return getPortletInstance(context).getAttribute(attrName);
227     }
228     
229     /***
230     * Convenience method for retreiving this action's PortletInstance
231     * attribute.
232     * @param String Attribute Name
233     * @param String Default to return if no attribute is found
234     * @param Context context Current context object.
235     * @return String portlet attribute
236     * @author <a href="mailto:sweaver@rippe.com">Scott Weaver</a>
237     */
238     public String getAttribute(String attrName, String defaultValue, Context context)
239     {
240         return getPortletInstance(context).getAttribute(attrName, defaultValue);
241     }
242     /***
243     * Convenience method for setting this action's PortletInstance
244     * attribute.
245     * @param String Attribute Name
246     * @param String Attribute Value
247     * @param Context context Current context object.
248     * @author <a href="mailto:sweaver@rippe.com">Scott Weaver</a>
249     */
250     public void setAttribute(String attrName, String value, Context context)
251         throws PortalPersistenceException
252     {
253         PortletInstance instance = getPortletInstance(context);
254         instance.setAttribute(attrName, value);
255         PersistenceManager.store(instance);
256     }
257     
258     /***
259      * Throws an exception if user attempts to perform unathorized action.
260      * 
261      * @param data
262      * @throws SecurityException
263      */
264     public void checkAdministrativeAction(RunData data) throws SecurityException
265     {
266 		if (!JetspeedSecurity.hasAdminRole(data.getUser()))
267 		{
268 			if (logger.isWarnEnabled())
269 			{
270 				logger.warn(
271 					"User ["
272 						+ data.getUser().getUserName()
273 						+ "] attempted to perform administrative action");
274 			}
275 			throw new SecurityException(
276 				"User ["
277 					+ data.getUser().getUserName()
278 					+ "] must be an administrator to perform this action");
279 		}    	
280     }
281   }