~ubuntu-branches/ubuntu/trusty/libstruts1.2-java/trusty-proposed

« back to all changes in this revision

Viewing changes to src/tiles-documentation/org/apache/struts/webapp/tiles/portal/UserMenuAction.java

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Vandyck
  • Date: 2004-11-19 15:35:25 UTC
  • Revision ID: james.westby@ubuntu.com-20041119153525-mdu08a76z4zo67xt
Tags: upstream-1.2.4
ImportĀ upstreamĀ versionĀ 1.2.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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 $
 
3
 * $Revision: 1.9 $
 
4
 * $Date: 2004/03/14 06:23:52 $
 
5
 *
 
6
 * Copyright 1999-2004 The Apache Software Foundation.
 
7
 * 
 
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
 
11
 * 
 
12
 *      http://www.apache.org/licenses/LICENSE-2.0
 
13
 * 
 
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.
 
19
 */
 
20
 
 
21
package org.apache.struts.webapp.tiles.portal;
 
22
 
 
23
import java.io.IOException;
 
24
import java.util.ArrayList;
 
25
import java.util.Iterator;
 
26
import java.util.List;
 
27
 
 
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;
 
33
 
 
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;
 
45
 
 
46
/**
 
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.
 
50
 *
 
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.
 
54
 *
 
55
 * Tiles input attributes :
 
56
 * <ul>
 
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>
 
61
 *   <li></li>
 
62
 * </ul>
 
63
 * Tiles output attributes :
 
64
 * <ul>
 
65
 *   <li>title : menu title</li>
 
66
 *   <li>items : Menu items to display</li>
 
67
 *   <li></li>
 
68
 * </ul>
 
69
 *
 
70
 * @version $Revision: 1.9 $ $Date: 2004/03/14 06:23:52 $
 
71
 */
 
72
public final class UserMenuAction extends TilesAction implements Controller {
 
73
 
 
74
        /** 
 
75
         * Commons Logging instance.
 
76
         */
 
77
        private static Log log = LogFactory.getLog(UserMenuAction.class);
 
78
 
 
79
        /** 
 
80
         * Tile attribute containing name used to store user settings in session 
 
81
         * context.
 
82
         */
 
83
        public static String USER_SETTINGS_NAME_ATTRIBUTE = "userSettingsName";
 
84
 
 
85
        /** 
 
86
         * Default name used to store settings in session context. 
 
87
         */
 
88
        public static String DEFAULT_USER_SETTINGS_NAME =
 
89
                "tiles.examples.portal.USER_MENU_SETTINGS";
 
90
 
 
91
        /** 
 
92
         * Default name used to store menu catalog in application scope.
 
93
         */
 
94
        public static String DEFAULT_MENU_CATALOG_NAME =
 
95
                "tiles.examples.portal.MenuCatalog";
 
96
 
 
97
        /** 
 
98
         * Tile attribute containing name used to store menu catalog in application 
 
99
         * scope.
 
100
         */
 
101
        public static String MENU_CATALOG_NAME_ATTRIBUTE = "catalogName";
 
102
 
 
103
        /** 
 
104
         * Tile attribute containing name of the settings definition used to 
 
105
         * initialize catalog. 
 
106
         */
 
107
        public static final String CATALOG_SETTING_ATTRIBUTE = "catalogSettings";
 
108
 
 
109
        /** 
 
110
         * Tile attribute containing items to render.
 
111
         */
 
112
        public static String USER_ITEMS_ATTRIBUTE = "items";
 
113
 
 
114
        /**
 
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.
 
121
         *
 
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.
 
127
         *
 
128
         * @exception Exception if the application business logic throws
 
129
         *  an exception
 
130
         * @since Struts 1.1
 
131
         */
 
132
        public ActionForward execute(
 
133
                ComponentContext context,
 
134
                ActionMapping mapping,
 
135
                ActionForm form,
 
136
                HttpServletRequest request,
 
137
                HttpServletResponse response)
 
138
                throws Exception {
 
139
 
 
140
                this.execute(
 
141
                        context,
 
142
                        request,
 
143
                        response,
 
144
                        getServlet().getServletContext());
 
145
 
 
146
                return null;
 
147
        }
 
148
 
 
149
        /**
 
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
 
155
         */
 
156
        public void perform(
 
157
                ComponentContext context,
 
158
                HttpServletRequest request,
 
159
                HttpServletResponse response,
 
160
                ServletContext servletContext)
 
161
                throws ServletException, IOException {
 
162
 
 
163
                log.debug("Enter action UserMenuAction");
 
164
 
 
165
                // Load user settings from user context
 
166
                MenuSettings settings = getUserSettings(request, context);
 
167
 
 
168
                // Set parameters for rendering page
 
169
                context.putAttribute(USER_ITEMS_ATTRIBUTE, settings.getItems());
 
170
 
 
171
                log.debug("settings=" + settings);
 
172
                log.debug("Exit action UserMenuAction");
 
173
 
 
174
        }
 
175
 
 
176
        /**
 
177
         * Load user setting.
 
178
         * This implementation load setting from user context.
 
179
         * If settings are not found, initialized them from default items defined
 
180
         * in Tile's context.
 
181
         * If settings are not found, initialized them.
 
182
         */
 
183
        public static MenuSettings getUserSettings(
 
184
                HttpServletRequest request,
 
185
                ComponentContext context)
 
186
                throws ServletException {
 
187
 
 
188
                // Get current session.
 
189
                HttpSession session = request.getSession();
 
190
 
 
191
                // Retrieve attribute name used to store settings.
 
192
                String userSettingsName =
 
193
                        (String) context.getAttribute(USER_SETTINGS_NAME_ATTRIBUTE);
 
194
 
 
195
                if (userSettingsName == null) {
 
196
                        userSettingsName = DEFAULT_USER_SETTINGS_NAME;
 
197
                }
 
198
 
 
199
                // Get user list from user context
 
200
                MenuSettings settings =
 
201
                        (MenuSettings) session.getAttribute(userSettingsName);
 
202
 
 
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();
 
208
                        try {
 
209
                                settings.addItems(
 
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");
 
213
                        }
 
214
 
 
215
                        // Save user settings in session
 
216
                        session.setAttribute(userSettingsName, settings);
 
217
                }
 
218
 
 
219
                return settings;
 
220
        }
 
221
 
 
222
        /**
 
223
         * Get catalog of available menu entries.
 
224
         * This implementation creates catalog list from the provided menu bar 
 
225
         * entries.
 
226
         */
 
227
        public static List getCatalog(
 
228
                ComponentContext context,
 
229
                HttpServletRequest request,
 
230
                ServletContext servletContext)
 
231
                throws ServletException {
 
232
 
 
233
                // Retrieve name used to store catalog in application context.
 
234
                // If not found, use default name
 
235
                String catalogName =
 
236
                        (String) context.getAttribute(MENU_CATALOG_NAME_ATTRIBUTE);
 
237
 
 
238
                if (catalogName == null) {
 
239
                        catalogName = DEFAULT_MENU_CATALOG_NAME;
 
240
                }
 
241
 
 
242
                // Get catalog from context
 
243
                List catalog = (List) servletContext.getAttribute(catalogName);
 
244
 
 
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(
 
250
                                        "Attribute '"
 
251
                                                + CATALOG_SETTING_ATTRIBUTE
 
252
                                                + "' must be set. It define entries used in catalog");
 
253
                        }
 
254
 
 
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");
 
259
                        }
 
260
 
 
261
                        // save it for future use
 
262
                        servletContext.setAttribute(catalogName, catalog);
 
263
                }
 
264
 
 
265
                return catalog;
 
266
        }
 
267
 
 
268
        /**
 
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 :
 
273
         * <li>
 
274
         *   <ul>MenuItems : add it</ul>
 
275
         *   <ul>ComponentDefinition : get attribute items, or list if not found.
 
276
         *       Call ExtractItems with resulting attribute.
 
277
         *   </ul>
 
278
         *   <ul>List : iterate on list, and call ExtractItems for each element.
 
279
         * </li>
 
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.
 
284
         */
 
285
        private static void extractItems(
 
286
                List result,
 
287
                Object object,
 
288
                HttpServletRequest request,
 
289
                ServletContext servletContext) {
 
290
 
 
291
                log.debug("Extract menu item from '" + object + "'");
 
292
 
 
293
                if (object instanceof String) { // definition name
 
294
                        try {
 
295
                                ComponentDefinition def =
 
296
                                        DefinitionsUtil.getDefinition(
 
297
                                                (String) object,
 
298
                                                request,
 
299
                                                servletContext);
 
300
 
 
301
                                extractItems(result, def, request, servletContext);
 
302
 
 
303
                        } catch (Exception ex) { // silently fail
 
304
                        }
 
305
 
 
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);
 
311
                        }
 
312
 
 
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");
 
318
                        }
 
319
 
 
320
                        if (attribute == null) {
 
321
                                return;
 
322
                        }
 
323
 
 
324
                        extractItems(result, attribute, request, servletContext);
 
325
 
 
326
                } else if (object instanceof MenuItem) {
 
327
                        result.add(object);
 
328
                }
 
329
        }
 
330
 
 
331
        /**
 
332
         * @see org.apache.struts.tiles.Controller#execute(org.apache.struts.tiles.ComponentContext, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, javax.servlet.ServletContext)
 
333
         */
 
334
        public void execute(
 
335
                ComponentContext tileContext,
 
336
                HttpServletRequest request,
 
337
                HttpServletResponse response,
 
338
                ServletContext servletContext)
 
339
                throws Exception {
 
340
            
 
341
                log.debug("Enter action UserMenuAction");
 
342
 
 
343
                // Load user settings from user context
 
344
                MenuSettings settings = getUserSettings(request, tileContext);
 
345
 
 
346
                // Set parameters for rendering page
 
347
                tileContext.putAttribute(USER_ITEMS_ATTRIBUTE, settings.getItems());
 
348
 
 
349
                log.debug("settings=" + settings);
 
350
                log.debug("Exit action UserMenuAction");
 
351
 
 
352
        }
 
353
 
 
354
}