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  import java.util.HashMap;
22  import java.util.Set;
23  import java.util.Iterator;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.jetspeed.JetspeedActions;
28  import org.apache.jetspeed.ajax.AjaxAction;
29  import org.apache.jetspeed.ajax.AjaxBuilder;
30  import org.apache.jetspeed.layout.PortletActionSecurityBehavior;
31  import org.apache.jetspeed.page.document.NodeNotFoundException;
32  import org.apache.jetspeed.portalsite.Menu;
33  import org.apache.jetspeed.portalsite.PortalSiteRequestContext;
34  import org.apache.jetspeed.profiler.impl.ProfilerValveImpl;
35  import org.apache.jetspeed.request.RequestContext;
36  
37  /***
38   * Get menus action retrieves all menu names defined for the addressed page.
39   *
40   * AJAX Parameters: 
41   *    none
42   *    
43   * @author <a href="mailto:rwatler@apache.org">Randy Watler</a>
44   * @version $Id: $
45   */
46  public class GetMenusAction extends BasePortletAction 
47      implements AjaxAction, AjaxBuilder, Constants
48  {
49      protected static final Log log = LogFactory.getLog(GetMenusAction.class);
50      
51      public GetMenusAction(String template,
52                            String errorTemplate,
53                            PortletActionSecurityBehavior securityBehavior)
54      {
55          super(template, errorTemplate, securityBehavior);
56      }
57  
58      public boolean run(RequestContext requestContext, Map resultMap)
59      {
60          boolean success = true;
61          String status = "success";
62          try
63          {
64              // generate action result
65              resultMap.put(ACTION, "getmenus");
66  
67              // check permission to use ajax api
68              if (!checkAccess(requestContext, JetspeedActions.VIEW))
69              {
70                  success = false;
71                  resultMap.put(REASON, "Insufficient access to get menus");
72                  return success;
73              }
74  
75              // get request context
76              PortalSiteRequestContext siteRequestContext = (PortalSiteRequestContext)requestContext.getAttribute(ProfilerValveImpl.PORTAL_SITE_REQUEST_CONTEXT_ATTR_KEY);
77              if (siteRequestContext == null)
78              {
79                  success = false;
80                  resultMap.put(REASON, "Missing portal site request context from ProfilerValve");
81                  return success;
82              }
83  
84              // get menu names
85              Set standardMenuNames = siteRequestContext.getStandardMenuNames();
86              Set customMenuNames = null;
87              try
88              {
89                  customMenuNames = siteRequestContext.getCustomMenuNames();
90              }
91              catch (NodeNotFoundException nnfe)
92              {
93              }
94              
95              // return menu names action results
96              resultMap.put(STANDARD_MENUS, standardMenuNames);
97              resultMap.put(CUSTOM_MENUS, customMenuNames);
98              
99              // get action parameter
100             String includeMenuDefinitions = getActionParameter(requestContext, INCLUDE_MENU_DEFS);
101             if ( includeMenuDefinitions != null && includeMenuDefinitions.toLowerCase().equals( "true" ) )
102             {
103                 // get request locale
104                 Locale locale = requestContext.getLocale();
105                 
106                 HashMap menuDefinitionsMap = new HashMap();
107                 
108                 StringBuffer failReason = new StringBuffer();
109                 Iterator menuNamesIter = standardMenuNames.iterator();
110                 while ( menuNamesIter.hasNext() )
111                 {
112                     String menuName = (String)menuNamesIter.next();
113                     Menu menuDefinition = getMenuDefinition( menuName, siteRequestContext, failReason );
114                     if ( menuDefinition != null )
115                         menuDefinitionsMap.put( menuName, menuDefinition );
116                 }
117                 menuNamesIter = customMenuNames.iterator();
118                 while ( menuNamesIter.hasNext() )
119                 {
120                     String menuName = (String)menuNamesIter.next();
121                     Menu menuDefinition = getMenuDefinition( menuName, siteRequestContext, failReason );
122                     if ( menuDefinition != null )
123                         menuDefinitionsMap.put( menuName, menuDefinition );
124                 }
125                 
126                 if ( failReason.length() > 0 )
127                 {
128                     success = false;
129                     resultMap.put(REASON, failReason.toString() );
130                     return success;
131                 }
132                 resultMap.put( INCLUDE_MENU_DEFS, new Boolean( true ) );
133                 resultMap.put( MENU_DEFINITIONS, menuDefinitionsMap );
134                 resultMap.put( MENU_CONTEXT, siteRequestContext );
135                 resultMap.put( MENU_LOCALE, locale );
136             }
137             else
138             {
139                 resultMap.put( INCLUDE_MENU_DEFS, new Boolean( false ) );
140             }
141             resultMap.put(STATUS, status);
142         }
143         catch (Exception e)
144         {
145             log.error("Exception while getting page menus info", e);
146             success = false;
147         }
148 
149         return success;
150 	}
151     
152     private Menu getMenuDefinition( String menuName, PortalSiteRequestContext siteRequestContext, StringBuffer failReason )
153     {
154         // get menu definition
155         Menu menuDefinition = null;
156         try
157         {
158             menuDefinition = siteRequestContext.getMenu( menuName );
159         }
160         catch ( NodeNotFoundException nnfe )
161         {
162         }
163         if ( menuDefinition == null && failReason != null )
164         {
165             if ( failReason.length() == 0 )
166                 failReason.append( "Unable to lookup specified menus: " ).append( menuName );
167             else
168                 failReason.append( ", " ).append( menuName );
169         }
170         return menuDefinition;
171     }
172 }