2
* $Header: /home/cvs/jakarta-struts/src/tiles-documentation/org/apache/struts/webapp/tiles/portal/UserMenuAction.java,v 1.9 2004/03/14 06:23:52 sraeburn Exp $
4
* $Date: 2004/03/14 06:23:52 $
6
* Copyright 1999-2004 The Apache Software Foundation.
8
* Licensed under the Apache License, Version 2.0 (the "License");
9
* you may not use this file except in compliance with the License.
10
* You may obtain a copy of the License at
12
* http://www.apache.org/licenses/LICENSE-2.0
14
* Unless required by applicable law or agreed to in writing, software
15
* distributed under the License is distributed on an "AS IS" BASIS,
16
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
* See the License for the specific language governing permissions and
18
* limitations under the License.
21
package org.apache.struts.webapp.tiles.portal;
23
import java.io.IOException;
24
import java.util.ArrayList;
25
import java.util.Iterator;
26
import java.util.List;
28
import javax.servlet.ServletContext;
29
import javax.servlet.ServletException;
30
import javax.servlet.http.HttpServletRequest;
31
import javax.servlet.http.HttpServletResponse;
32
import javax.servlet.http.HttpSession;
34
import org.apache.commons.logging.Log;
35
import org.apache.commons.logging.LogFactory;
36
import org.apache.struts.action.ActionForm;
37
import org.apache.struts.action.ActionForward;
38
import org.apache.struts.action.ActionMapping;
39
import org.apache.struts.tiles.ComponentContext;
40
import org.apache.struts.tiles.ComponentDefinition;
41
import org.apache.struts.tiles.Controller;
42
import org.apache.struts.tiles.DefinitionsUtil;
43
import org.apache.struts.tiles.actions.TilesAction;
44
import org.apache.struts.tiles.beans.MenuItem;
47
* This controller load user menu settings and put them in tile context.
48
* If menu settings are not defined for user, defined them based on tiles
49
* attributes used as default.
51
* This implementation extends Struts Action, and also define Tiles Controller interface.
52
* This allows to use it as well as a controller type or a controller url. If used
53
* as controller type, Struts Action functionality are not availables.
55
* Tiles input attributes :
57
* <li>title : menu title</li>
58
* <li>items : Menu entries used as default when user settings is created</li>
59
* <li>defaultChoice : Menus or menu entries porposed as choice to user</li>
60
* <li>storeUnderName : Store user settings under provided name in session context [optional]</li>
63
* Tiles output attributes :
65
* <li>title : menu title</li>
66
* <li>items : Menu items to display</li>
70
* @version $Revision: 1.9 $ $Date: 2004/03/14 06:23:52 $
72
public final class UserMenuAction extends TilesAction implements Controller {
75
* Commons Logging instance.
77
private static Log log = LogFactory.getLog(UserMenuAction.class);
80
* Tile attribute containing name used to store user settings in session
83
public static String USER_SETTINGS_NAME_ATTRIBUTE = "userSettingsName";
86
* Default name used to store settings in session context.
88
public static String DEFAULT_USER_SETTINGS_NAME =
89
"tiles.examples.portal.USER_MENU_SETTINGS";
92
* Default name used to store menu catalog in application scope.
94
public static String DEFAULT_MENU_CATALOG_NAME =
95
"tiles.examples.portal.MenuCatalog";
98
* Tile attribute containing name used to store menu catalog in application
101
public static String MENU_CATALOG_NAME_ATTRIBUTE = "catalogName";
104
* Tile attribute containing name of the settings definition used to
105
* initialize catalog.
107
public static final String CATALOG_SETTING_ATTRIBUTE = "catalogSettings";
110
* Tile attribute containing items to render.
112
public static String USER_ITEMS_ATTRIBUTE = "items";
115
* Struts' action perform().
116
* Process the specified HTTP request, and create the corresponding HTTP
117
* response (or forward to another web component that will create it).
118
* Return an <code>ActionForward</code> instance describing where and how
119
* control should be forwarded, or <code>null</code> if the response has
120
* already been completed.
122
* @param context The current Tile context, containing Tile attributes.
123
* @param mapping The ActionMapping used to select this instance.
124
* @param form The optional ActionForm bean for this request (if any).
125
* @param request The HTTP request we are processing.
126
* @param response The HTTP response we are creating.
128
* @exception Exception if the application business logic throws
132
public ActionForward execute(
133
ComponentContext context,
134
ActionMapping mapping,
136
HttpServletRequest request,
137
HttpServletResponse response)
144
getServlet().getServletContext());
150
* Method associated to a tile and called immediately before tile is included.
151
* @param tileContext Current tile context.
152
* @param request Current request
153
* @param response Current response
154
* @param servletContext Current servlet context
157
ComponentContext context,
158
HttpServletRequest request,
159
HttpServletResponse response,
160
ServletContext servletContext)
161
throws ServletException, IOException {
163
log.debug("Enter action UserMenuAction");
165
// Load user settings from user context
166
MenuSettings settings = getUserSettings(request, context);
168
// Set parameters for rendering page
169
context.putAttribute(USER_ITEMS_ATTRIBUTE, settings.getItems());
171
log.debug("settings=" + settings);
172
log.debug("Exit action UserMenuAction");
178
* This implementation load setting from user context.
179
* If settings are not found, initialized them from default items defined
181
* If settings are not found, initialized them.
183
public static MenuSettings getUserSettings(
184
HttpServletRequest request,
185
ComponentContext context)
186
throws ServletException {
188
// Get current session.
189
HttpSession session = request.getSession();
191
// Retrieve attribute name used to store settings.
192
String userSettingsName =
193
(String) context.getAttribute(USER_SETTINGS_NAME_ATTRIBUTE);
195
if (userSettingsName == null) {
196
userSettingsName = DEFAULT_USER_SETTINGS_NAME;
199
// Get user list from user context
200
MenuSettings settings =
201
(MenuSettings) session.getAttribute(userSettingsName);
203
// If settings don't exist, create and initialize them
204
// Initialization is done from context attribute denoted by ITEMS
205
if (settings == null) {
206
// List doesn't exist, create it and initialize it from Tiles parameters
207
settings = new MenuSettings();
210
(List) context.getAttribute(USER_ITEMS_ATTRIBUTE));
211
} catch (ClassCastException ex) {
212
throw new ServletException("Can't initialize user menu : default items must be a list of items");
215
// Save user settings in session
216
session.setAttribute(userSettingsName, settings);
223
* Get catalog of available menu entries.
224
* This implementation creates catalog list from the provided menu bar
227
public static List getCatalog(
228
ComponentContext context,
229
HttpServletRequest request,
230
ServletContext servletContext)
231
throws ServletException {
233
// Retrieve name used to store catalog in application context.
234
// If not found, use default name
236
(String) context.getAttribute(MENU_CATALOG_NAME_ATTRIBUTE);
238
if (catalogName == null) {
239
catalogName = DEFAULT_MENU_CATALOG_NAME;
242
// Get catalog from context
243
List catalog = (List) servletContext.getAttribute(catalogName);
245
// If not found, initialize it from provided default menu
246
if (catalog == null) {
247
Object menuBar = context.getAttribute(CATALOG_SETTING_ATTRIBUTE);
248
if (menuBar == null) {
249
throw new ServletException(
251
+ CATALOG_SETTING_ATTRIBUTE
252
+ "' must be set. It define entries used in catalog");
255
catalog = new ArrayList();
256
extractItems(catalog, menuBar, request, servletContext);
257
if (catalog.size() == 0) {
258
throw new ServletException("Can't initialize menu items catalog");
261
// save it for future use
262
servletContext.setAttribute(catalogName, catalog);
269
* Extract menu items from passed object. Items are stored in
270
* <code>result</code> parameter.
271
* This method allows to create a list of menu entries from existing menus.
272
* Check object type class :
274
* <ul>MenuItems : add it</ul>
275
* <ul>ComponentDefinition : get attribute items, or list if not found.
276
* Call ExtractItems with resulting attribute.
278
* <ul>List : iterate on list, and call ExtractItems for each element.
280
* @param result result list (should be initialized)
281
* @param object object to add (MenuItems, Definition, ...)
282
* @param request current request
283
* @param servletContext current servlet context.
285
private static void extractItems(
288
HttpServletRequest request,
289
ServletContext servletContext) {
291
log.debug("Extract menu item from '" + object + "'");
293
if (object instanceof String) { // definition name
295
ComponentDefinition def =
296
DefinitionsUtil.getDefinition(
301
extractItems(result, def, request, servletContext);
303
} catch (Exception ex) { // silently fail
306
} else if (object instanceof List) {
307
List list = (List) object;
308
Iterator iter = list.iterator();
309
while (iter.hasNext()) {
310
extractItems(result, iter.next(), request, servletContext);
313
} else if (object instanceof ComponentDefinition) {
314
ComponentDefinition definition = (ComponentDefinition) object;
315
Object attribute = definition.getAttribute("items");
316
if (attribute == null) {
317
attribute = definition.getAttribute("list");
320
if (attribute == null) {
324
extractItems(result, attribute, request, servletContext);
326
} else if (object instanceof MenuItem) {
332
* @see org.apache.struts.tiles.Controller#execute(org.apache.struts.tiles.ComponentContext, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, javax.servlet.ServletContext)
335
ComponentContext tileContext,
336
HttpServletRequest request,
337
HttpServletResponse response,
338
ServletContext servletContext)
341
log.debug("Enter action UserMenuAction");
343
// Load user settings from user context
344
MenuSettings settings = getUserSettings(request, tileContext);
346
// Set parameters for rendering page
347
tileContext.putAttribute(USER_ITEMS_ATTRIBUTE, settings.getItems());
349
log.debug("settings=" + settings);
350
log.debug("Exit action UserMenuAction");