1 18 19 package org.apache.struts.webapp.tiles.portal; 20 21 import java.io.IOException ; 22 import java.util.ArrayList ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 26 import javax.servlet.ServletContext ; 27 import javax.servlet.ServletException ; 28 import javax.servlet.http.HttpServletRequest ; 29 import javax.servlet.http.HttpServletResponse ; 30 import javax.servlet.http.HttpSession ; 31 32 import org.apache.commons.logging.Log; 33 import org.apache.commons.logging.LogFactory; 34 import org.apache.struts.action.ActionForm; 35 import org.apache.struts.action.ActionForward; 36 import org.apache.struts.action.ActionMapping; 37 import org.apache.struts.tiles.ComponentContext; 38 import org.apache.struts.tiles.ComponentDefinition; 39 import org.apache.struts.tiles.Controller; 40 import org.apache.struts.tiles.DefinitionsUtil; 41 import org.apache.struts.tiles.actions.TilesAction; 42 import org.apache.struts.tiles.beans.MenuItem; 43 44 70 public final class UserMenuAction extends TilesAction implements Controller { 71 72 75 private static Log log = LogFactory.getLog(UserMenuAction.class); 76 77 81 public static String USER_SETTINGS_NAME_ATTRIBUTE = "userSettingsName"; 82 83 86 public static String DEFAULT_USER_SETTINGS_NAME = 87 "tiles.examples.portal.USER_MENU_SETTINGS"; 88 89 92 public static String DEFAULT_MENU_CATALOG_NAME = 93 "tiles.examples.portal.MenuCatalog"; 94 95 99 public static String MENU_CATALOG_NAME_ATTRIBUTE = "catalogName"; 100 101 105 public static final String CATALOG_SETTING_ATTRIBUTE = "catalogSettings"; 106 107 110 public static String USER_ITEMS_ATTRIBUTE = "items"; 111 112 130 public ActionForward execute( 131 ComponentContext context, 132 ActionMapping mapping, 133 ActionForm form, 134 HttpServletRequest request, 135 HttpServletResponse response) 136 throws Exception { 137 138 this.execute( 139 context, 140 request, 141 response, 142 getServlet().getServletContext()); 143 144 return null; 145 } 146 147 154 public void perform( 155 ComponentContext context, 156 HttpServletRequest request, 157 HttpServletResponse response, 158 ServletContext servletContext) 159 throws ServletException , IOException { 160 161 log.debug("Enter action UserMenuAction"); 162 163 MenuSettings settings = getUserSettings(request, context); 165 166 context.putAttribute(USER_ITEMS_ATTRIBUTE, settings.getItems()); 168 169 log.debug("settings=" + settings); 170 log.debug("Exit action UserMenuAction"); 171 172 } 173 174 181 public static MenuSettings getUserSettings( 182 HttpServletRequest request, 183 ComponentContext context) 184 throws ServletException { 185 186 HttpSession session = request.getSession(); 188 189 String userSettingsName = 191 (String ) context.getAttribute(USER_SETTINGS_NAME_ATTRIBUTE); 192 193 if (userSettingsName == null) { 194 userSettingsName = DEFAULT_USER_SETTINGS_NAME; 195 } 196 197 MenuSettings settings = 199 (MenuSettings) session.getAttribute(userSettingsName); 200 201 if (settings == null) { 204 settings = new MenuSettings(); 206 try { 207 settings.addItems( 208 (List ) context.getAttribute(USER_ITEMS_ATTRIBUTE)); 209 } catch (ClassCastException ex) { 210 throw new ServletException ("Can't initialize user menu : default items must be a list of items"); 211 } 212 213 session.setAttribute(userSettingsName, settings); 215 } 216 217 return settings; 218 } 219 220 225 public static List getCatalog( 226 ComponentContext context, 227 HttpServletRequest request, 228 ServletContext servletContext) 229 throws ServletException { 230 231 String catalogName = 234 (String ) context.getAttribute(MENU_CATALOG_NAME_ATTRIBUTE); 235 236 if (catalogName == null) { 237 catalogName = DEFAULT_MENU_CATALOG_NAME; 238 } 239 240 List catalog = (List ) servletContext.getAttribute(catalogName); 242 243 if (catalog == null) { 245 Object menuBar = context.getAttribute(CATALOG_SETTING_ATTRIBUTE); 246 if (menuBar == null) { 247 throw new ServletException ( 248 "Attribute '" 249 + CATALOG_SETTING_ATTRIBUTE 250 + "' must be set. It define entries used in catalog"); 251 } 252 253 catalog = new ArrayList (); 254 extractItems(catalog, menuBar, request, servletContext); 255 if (catalog.size() == 0) { 256 throw new ServletException ("Can't initialize menu items catalog"); 257 } 258 259 servletContext.setAttribute(catalogName, catalog); 261 } 262 263 return catalog; 264 } 265 266 283 private static void extractItems( 284 List result, 285 Object object, 286 HttpServletRequest request, 287 ServletContext servletContext) { 288 289 log.debug("Extract menu item from '" + object + "'"); 290 291 if (object instanceof String ) { try { 293 ComponentDefinition def = 294 DefinitionsUtil.getDefinition( 295 (String ) object, 296 request, 297 servletContext); 298 299 extractItems(result, def, request, servletContext); 300 301 } catch (Exception ex) { } 303 304 } else if (object instanceof List ) { 305 List list = (List ) object; 306 Iterator iter = list.iterator(); 307 while (iter.hasNext()) { 308 extractItems(result, iter.next(), request, servletContext); 309 } 310 311 } else if (object instanceof ComponentDefinition) { 312 ComponentDefinition definition = (ComponentDefinition) object; 313 Object attribute = definition.getAttribute("items"); 314 if (attribute == null) { 315 attribute = definition.getAttribute("list"); 316 } 317 318 if (attribute == null) { 319 return; 320 } 321 322 extractItems(result, attribute, request, servletContext); 323 324 } else if (object instanceof MenuItem) { 325 result.add(object); 326 } 327 } 328 329 332 public void execute( 333 ComponentContext tileContext, 334 HttpServletRequest request, 335 HttpServletResponse response, 336 ServletContext servletContext) 337 throws Exception { 338 339 log.debug("Enter action UserMenuAction"); 340 341 MenuSettings settings = getUserSettings(request, tileContext); 343 344 tileContext.putAttribute(USER_ITEMS_ATTRIBUTE, settings.getItems()); 346 347 log.debug("settings=" + settings); 348 log.debug("Exit action UserMenuAction"); 349 350 } 351 352 } 353 | Popular Tags |