View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.layout.impl;
18  
19  import java.util.Locale;
20  import java.util.Map;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.jetspeed.JetspeedActions;
25  import org.apache.jetspeed.ajax.AjaxAction;
26  import org.apache.jetspeed.ajax.AjaxBuilder;
27  import org.apache.jetspeed.layout.PortletActionSecurityBehavior;
28  import org.apache.jetspeed.page.document.NodeNotFoundException;
29  import org.apache.jetspeed.portalsite.Menu;
30  import org.apache.jetspeed.portalsite.PortalSiteRequestContext;
31  import org.apache.jetspeed.profiler.impl.ProfilerValveImpl;
32  import org.apache.jetspeed.request.RequestContext;
33  
34  /***
35   * Get menu action retrieves a menu defined for the addressed page.
36   *
37   * AJAX Parameters: 
38   *    menu = the name of the menu definition to retrieve 
39   *    
40   * @author <a href="mailto:rwatler@apache.org">Randy Watler</a>
41   * @version $Id: $
42   */
43  public class GetMenuAction extends BasePortletAction 
44      implements AjaxAction, AjaxBuilder, Constants
45  {
46      protected static final Log log = LogFactory.getLog(GetMenusAction.class);
47      
48      public GetMenuAction(String template,
49                           String errorTemplate,
50                           PortletActionSecurityBehavior securityBehavior)
51      {
52          super(template, errorTemplate, securityBehavior);
53      }
54  
55      public boolean run(RequestContext requestContext, Map resultMap)
56      {
57          boolean success = true;
58          String status = "success";
59          try
60          {
61              // generate action result
62              resultMap.put(ACTION, "getmenu");
63  
64              // check permission to use ajax api
65              if (!checkAccess(requestContext, JetspeedActions.VIEW))
66              {
67                  success = false;
68                  resultMap.put(REASON, "Insufficient access to get menu");
69                  return success;
70              }
71  
72              // get action parameter
73              String menuName = getActionParameter(requestContext, MENU_NAME);
74              if (menuName == null)
75              {
76                  success = false;
77                  resultMap.put(REASON, "Missing required '" + MENU_NAME + "' parameter");
78                  return success;
79              }
80  
81              // get request context
82              PortalSiteRequestContext siteRequestContext = (PortalSiteRequestContext)requestContext.getAttribute(ProfilerValveImpl.PORTAL_SITE_REQUEST_CONTEXT_ATTR_KEY);
83              if (siteRequestContext == null)
84              {
85                  success = false;
86                  resultMap.put(REASON, "Missing portal site request context from ProfilerValve");
87                  return success;
88              }
89  
90              // get request locale
91              Locale locale = requestContext.getLocale();
92  
93              // get menu definition
94              Menu menuDefinition = null;
95              try
96              {
97                  menuDefinition = siteRequestContext.getMenu(menuName);
98              }
99              catch (NodeNotFoundException nnfe)
100             {
101             }
102             if (menuDefinition == null)
103             {
104                 success = false;
105                 resultMap.put(REASON, "Unable to lookup specified menu for page");
106                 return success;
107             }
108 
109             // return menu definition action results
110             resultMap.put(MENU, menuDefinition);
111             resultMap.put(MENU_CONTEXT, siteRequestContext);
112             resultMap.put(MENU_LOCALE, locale);
113             resultMap.put(STATUS, status);
114         }
115         catch (Exception e)
116         {
117             log.error("Exception while getting page menus info", e);
118             success = false;
119         }
120 
121         return success;
122 	}
123 }