1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.security.permission;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.language.LanguageUtil;
27  import com.liferay.portal.kernel.log.Log;
28  import com.liferay.portal.kernel.log.LogFactoryUtil;
29  import com.liferay.portal.kernel.util.ContentTypes;
30  import com.liferay.portal.kernel.util.ListUtil;
31  import com.liferay.portal.kernel.util.PropsKeys;
32  import com.liferay.portal.kernel.util.StringPool;
33  import com.liferay.portal.kernel.util.StringUtil;
34  import com.liferay.portal.kernel.util.Validator;
35  import com.liferay.portal.kernel.xml.Document;
36  import com.liferay.portal.kernel.xml.Element;
37  import com.liferay.portal.kernel.xml.SAXReaderUtil;
38  import com.liferay.portal.model.Group;
39  import com.liferay.portal.model.Organization;
40  import com.liferay.portal.model.PasswordPolicy;
41  import com.liferay.portal.model.Permission;
42  import com.liferay.portal.model.Portlet;
43  import com.liferay.portal.model.PortletConstants;
44  import com.liferay.portal.model.ResourceAction;
45  import com.liferay.portal.model.Role;
46  import com.liferay.portal.model.RoleConstants;
47  import com.liferay.portal.model.User;
48  import com.liferay.portal.model.UserGroup;
49  import com.liferay.portal.service.PortletLocalServiceUtil;
50  import com.liferay.portal.service.ResourceActionLocalServiceUtil;
51  import com.liferay.portal.service.RoleLocalServiceUtil;
52  import com.liferay.portal.util.PortalUtil;
53  import com.liferay.portal.util.PortletKeys;
54  import com.liferay.portal.util.PropsUtil;
55  import com.liferay.portlet.PortletResourceBundles;
56  import com.liferay.portlet.expando.model.ExpandoColumn;
57  import com.liferay.util.UniqueList;
58  
59  import java.io.IOException;
60  
61  import java.util.ArrayList;
62  import java.util.Collections;
63  import java.util.HashMap;
64  import java.util.HashSet;
65  import java.util.Iterator;
66  import java.util.List;
67  import java.util.Locale;
68  import java.util.Map;
69  import java.util.Set;
70  
71  import javax.servlet.jsp.PageContext;
72  
73  /**
74   * <a href="ResourceActionsUtil.java.html"><b><i>View Source</i></b></a>
75   *
76   * @author Brian Wing Shun Chan
77   */
78  public class ResourceActionsUtil {
79  
80      public static final String ACTION_NAME_PREFIX = "action.";
81  
82      public static final String MODEL_RESOURCE_NAME_PREFIX = "model.resource.";
83  
84      public static final String[] ORGANIZATION_MODEL_RESOURCES = {
85          Organization.class.getName(), PasswordPolicy.class.getName(),
86          User.class.getName()
87      };
88  
89      public static final String[] PORTAL_MODEL_RESOURCES = {
90          ExpandoColumn.class.getName(), Organization.class.getName(),
91          PasswordPolicy.class.getName(), Role.class.getName(),
92          User.class.getName(), UserGroup.class.getName()
93      };
94  
95      public static String getAction(Locale locale, String action) {
96          String key = ACTION_NAME_PREFIX + action;
97  
98          String value = LanguageUtil.get(locale, key, null);
99  
100         if ((value == null) || (value.equals(key))) {
101             value = PortletResourceBundles.getString(locale, key);
102         }
103 
104         if (value == null) {
105             value = key;
106         }
107 
108         return value;
109     }
110 
111     public static String getAction(PageContext pageContext, String action) {
112         String key = ACTION_NAME_PREFIX + action;
113 
114         String value = LanguageUtil.get(pageContext, key, null);
115 
116         if ((value == null) || (value.equals(key))) {
117             value = PortletResourceBundles.getString(pageContext, key);
118         }
119 
120         if (value == null) {
121             value = key;
122         }
123 
124         return value;
125     }
126 
127     public static List<String> getActions(List<Permission> permissions) {
128         List<String> actions = new UniqueList<String>();
129 
130         for (Permission permission : permissions) {
131             actions.add(permission.getActionId());
132         }
133 
134         return actions;
135     }
136 
137     public static List<String> getActionsNames(
138         PageContext pageContext, List<String> actions) {
139 
140         List<String> uniqueList = new UniqueList<String>();
141 
142         for (String action : actions) {
143             uniqueList.add(getAction(pageContext, action));
144         }
145 
146         List<String> list = new ArrayList<String>();
147 
148         list.addAll(uniqueList);
149 
150         return list;
151     }
152 
153     public static List<String> getActionsNames(
154         PageContext pageContext, String name, long actionIds) {
155 
156         try {
157             List<ResourceAction> resourceActions =
158                 ResourceActionLocalServiceUtil.getResourceActions(name);
159 
160             List<String> actions = new ArrayList<String>();
161 
162             for (ResourceAction resourceAction : resourceActions) {
163                 long bitwiseValue = resourceAction.getBitwiseValue();
164 
165                 if ((actionIds & bitwiseValue) == bitwiseValue) {
166                     actions.add(resourceAction.getActionId());
167                 }
168             }
169 
170             return getActionsNames(pageContext, actions);
171         }
172         catch (Exception e) {
173             _log.error(e, e);
174 
175             return Collections.EMPTY_LIST;
176         }
177     }
178 
179     public static List<String> getModelNames() {
180         return _instance._getModelNames();
181     }
182 
183     public static List<String> getModelPortletResources(String name) {
184         return _instance._getModelPortletResources(name);
185     }
186 
187     public static String getModelResource(Locale locale, String name) {
188         String key = MODEL_RESOURCE_NAME_PREFIX + name;
189 
190         String value = LanguageUtil.get(locale, key, null);
191 
192         if ((value == null) || (value.equals(key))) {
193             value = PortletResourceBundles.getString(locale, key);
194         }
195 
196         if (value == null) {
197             value = key;
198         }
199 
200         return value;
201     }
202 
203     public static String getModelResource(
204         PageContext pageContext, String name) {
205 
206         String key = MODEL_RESOURCE_NAME_PREFIX + name;
207 
208         String value = LanguageUtil.get(pageContext, key, null);
209 
210         if ((value == null) || (value.equals(key))) {
211             value = PortletResourceBundles.getString(pageContext, key);
212         }
213 
214         if (value == null) {
215             value = key;
216         }
217 
218         return value;
219     }
220 
221     public static List<String> getModelResourceActions(String name) {
222         return _instance._getModelResourceActions(name);
223     }
224 
225     public static List<String> getModelResourceCommunityDefaultActions(
226         String name) {
227 
228         return _instance._getModelResourceCommunityDefaultActions(name);
229     }
230 
231     public static List<String> getModelResourceGuestDefaultActions(
232         String name) {
233 
234         return _instance._getModelResourceGuestDefaultActions(name);
235     }
236 
237     public static List<String> getModelResourceGuestUnsupportedActions(
238         String name) {
239 
240         return _instance._getModelResourceGuestUnsupportedActions(name);
241     }
242 
243     public static List<String> getModelResourceOwnerDefaultActions(
244         String name) {
245 
246         return _instance._getModelResourceOwnerDefaultActions(name);
247     }
248 
249     public static List<String> getPortletModelResources(String portletName) {
250         return _instance._getPortletModelResources(portletName);
251     }
252 
253     public static List<String> getPortletNames() {
254         return _instance._getPortletNames();
255     }
256 
257     public static List<String> getPortletResourceActions(String name) {
258         return _instance._getPortletResourceActions(name);
259     }
260 
261     public static List<String> getPortletResourceCommunityDefaultActions(
262         String name) {
263 
264         return _instance._getPortletResourceCommunityDefaultActions(name);
265     }
266 
267     public static List<String> getPortletResourceGuestDefaultActions(
268         String name) {
269 
270         return _instance._getPortletResourceGuestDefaultActions(name);
271     }
272 
273     public static List<String> getPortletResourceGuestUnsupportedActions(
274         String name) {
275 
276         return _instance._getPortletResourceGuestUnsupportedActions(name);
277     }
278 
279     public static List<String> getPortletResourceLayoutManagerActions(
280         String name) {
281 
282         return _instance._getPortletResourceLayoutManagerActions(name);
283     }
284 
285     public static List<String> getResourceActions(String name) {
286         if (name.contains(StringPool.PERIOD)) {
287             return getModelResourceActions(name);
288         }
289         else {
290             return getPortletResourceActions(name);
291         }
292     }
293 
294     public static List<String> getResourceActions(
295         String portletResource, String modelResource) {
296 
297         List<String> actions = null;
298 
299         if (Validator.isNull(modelResource)) {
300             actions = getPortletResourceActions(portletResource);
301         }
302         else {
303             actions = getModelResourceActions(modelResource);
304         }
305 
306         return actions;
307     }
308 
309     public static List<String> getResourceCommunityDefaultActions(String name) {
310         if (name.contains(StringPool.PERIOD)) {
311             return getModelResourceCommunityDefaultActions(name);
312         }
313         else {
314             return getPortletResourceCommunityDefaultActions(name);
315         }
316     }
317 
318     public static List<String> getResourceGuestUnsupportedActions(
319         String portletResource, String modelResource) {
320 
321         List<String> actions = null;
322 
323         if (Validator.isNull(modelResource)) {
324             actions =
325                 getPortletResourceGuestUnsupportedActions(portletResource);
326         }
327         else {
328             actions = getModelResourceGuestUnsupportedActions(modelResource);
329         }
330 
331         return actions;
332     }
333 
334     public static List<Role> getRoles(Group group, String modelResource)
335         throws SystemException {
336 
337         List<Role> allRoles = RoleLocalServiceUtil.getRoles(
338             group.getCompanyId());
339 
340         int[] types = new int[] {
341             RoleConstants.TYPE_REGULAR, RoleConstants.TYPE_COMMUNITY
342         };
343 
344         if (isPortalModelResource(modelResource)) {
345             if (modelResource.equals(Organization.class.getName()) ||
346                 modelResource.equals(User.class.getName())) {
347 
348                 types = new int[] {
349                     RoleConstants.TYPE_REGULAR,
350                     RoleConstants.TYPE_ORGANIZATION
351                 };
352             }
353             else {
354                 types = new int[] {RoleConstants.TYPE_REGULAR};
355             }
356         }
357         else {
358             if (group.isOrganization()) {
359                 types = new int[] {
360                     RoleConstants.TYPE_REGULAR,
361                     RoleConstants.TYPE_ORGANIZATION
362                 };
363             }
364             else if (group.isUser()) {
365                 types = new int[] {RoleConstants.TYPE_REGULAR};
366             }
367         }
368 
369         List<Role> roles = new ArrayList<Role>();
370 
371         for (int type : types) {
372             for (Role role : allRoles) {
373                 if (role.getType() == type) {
374                     roles.add(role);
375                 }
376             }
377         }
378 
379         return roles;
380     }
381 
382     public static void init() {
383         _instance._init();
384     }
385 
386     public static boolean isOrganizationModelResource(String modelResource) {
387         return _instance._isOrganizationModelResource(modelResource);
388     }
389 
390     public static boolean isPortalModelResource(String modelResource) {
391         return _instance._isPortalModelResource(modelResource);
392     }
393 
394     public static void read(
395             String servletContextName, ClassLoader classLoader, String source)
396         throws Exception {
397 
398         _instance._read(servletContextName, classLoader, source);
399     }
400 
401     private ResourceActionsUtil() {
402         _organizationModelResources = new HashSet<String>();
403 
404         for (int i = 0; i < ORGANIZATION_MODEL_RESOURCES.length; i++) {
405             _organizationModelResources.add(ORGANIZATION_MODEL_RESOURCES[i]);
406         }
407 
408         _portalModelResources = new HashSet<String>();
409 
410         for (int i = 0; i < PORTAL_MODEL_RESOURCES.length; i++) {
411             _portalModelResources.add(PORTAL_MODEL_RESOURCES[i]);
412         }
413 
414         _portletModelResources = new HashMap<String, Set<String>>();
415         _portletResourceActions = new HashMap<String, List<String>>();
416         _portletResourceCommunityDefaultActions =
417             new HashMap<String, List<String>>();
418         _portletResourceGuestDefaultActions =
419             new HashMap<String, List<String>>();
420         _portletResourceGuestUnsupportedActions =
421             new HashMap<String, List<String>>();
422         _portletResourceLayoutManagerActions =
423             new HashMap<String, List<String>>();
424         _modelPortletResources = new HashMap<String, Set<String>>();
425         _modelResourceActions = new HashMap<String, List<String>>();
426         _modelResourceCommunityDefaultActions =
427             new HashMap<String, List<String>>();
428         _modelResourceGuestDefaultActions =
429             new HashMap<String, List<String>>();
430         _modelResourceGuestUnsupportedActions =
431             new HashMap<String, List<String>>();
432         _modelResourceOwnerDefaultActions =
433             new HashMap<String, List<String>>();
434 
435         try {
436             ClassLoader classLoader = getClass().getClassLoader();
437 
438             String[] configs = StringUtil.split(
439                 PropsUtil.get(PropsKeys.RESOURCE_ACTIONS_CONFIGS));
440 
441             for (int i = 0; i < configs.length; i++) {
442                 _read(null, classLoader, configs[i]);
443             }
444         }
445         catch (Exception e) {
446             _log.error(e, e);
447         }
448     }
449 
450     private void _checkGuestUnsupportedActions(
451         List<String> guestUnsupportedActions,
452         List<String> guestDefaultActions) {
453 
454         // Guest default actions cannot reference guest unsupported actions
455 
456         Iterator<String> itr = guestDefaultActions.iterator();
457 
458         while (itr.hasNext()) {
459             String actionId = itr.next();
460 
461             if (guestUnsupportedActions.contains(actionId)) {
462                 itr.remove();
463             }
464         }
465     }
466 
467     private void _checkPortletActions(List<String> actions) {
468         if (!actions.contains(ActionKeys.CONFIGURATION)) {
469             actions.add(ActionKeys.CONFIGURATION);
470         }
471 
472         if (!actions.contains(ActionKeys.VIEW)) {
473             actions.add(ActionKeys.VIEW);
474         }
475     }
476 
477     private void _checkPortletCommunityDefaultActions(List<String> actions) {
478         if (actions.size() == 0) {
479             actions.add(ActionKeys.VIEW);
480         }
481     }
482 
483     private void _checkPortletGuestDefaultActions(List<String> actions) {
484         if (actions.size() == 0) {
485             actions.add(ActionKeys.VIEW);
486         }
487     }
488 
489     private void _checkPortletLayoutManagerActions(List<String> actions) {
490         if (!actions.contains(ActionKeys.CONFIGURATION)) {
491             actions.add(ActionKeys.CONFIGURATION);
492         }
493 
494         if (!actions.contains(ActionKeys.PREFERENCES)) {
495             actions.add(ActionKeys.PREFERENCES);
496         }
497 
498         if (!actions.contains(ActionKeys.VIEW)) {
499             actions.add(ActionKeys.VIEW);
500         }
501     }
502 
503     private List<String> _getActions(
504         Map<String, List<String>> map, String name) {
505 
506         List<String> actions = map.get(name);
507 
508         if (actions == null) {
509             actions = new UniqueList<String>();
510 
511             map.put(name, actions);
512         }
513 
514         return actions;
515     }
516 
517     private List<String> _getModelNames() {
518         return ListUtil.fromCollection(_modelPortletResources.keySet());
519     }
520 
521     private List<String> _getModelPortletResources(String name) {
522         Set<String> resources = _modelPortletResources.get(name);
523 
524         if (resources == null) {
525             return new UniqueList<String>();
526         }
527         else {
528             return Collections.list(Collections.enumeration(resources));
529         }
530     }
531 
532     private List<String> _getModelResourceActions(String name) {
533         return _getActions(_modelResourceActions, name);
534     }
535 
536     private List<String> _getModelResourceCommunityDefaultActions(
537         String name) {
538 
539         return _getActions(_modelResourceCommunityDefaultActions, name);
540     }
541 
542     private List<String> _getModelResourceGuestDefaultActions(String name) {
543         return _getActions(_modelResourceGuestDefaultActions, name);
544     }
545 
546     private List<String> _getModelResourceGuestUnsupportedActions(String name) {
547         return _getActions(_modelResourceGuestUnsupportedActions, name);
548     }
549 
550     private List<String> _getModelResourceOwnerDefaultActions(String name) {
551         return _getActions(_modelResourceOwnerDefaultActions, name);
552     }
553 
554     private List<String> _getPortletModelResources(String portletName) {
555         portletName = PortletConstants.getRootPortletId(portletName);
556 
557         Set<String> resources = _portletModelResources.get(portletName);
558 
559         if (resources == null) {
560             return new UniqueList<String>();
561         }
562         else {
563             return Collections.list(Collections.enumeration(resources));
564         }
565     }
566 
567     private List<String> _getPortletNames() {
568         return ListUtil.fromCollection(_portletModelResources.keySet());
569     }
570 
571     private List<String> _getPortletResourceActions(String name) {
572         name = PortletConstants.getRootPortletId(name);
573 
574         List<String> actions = _getActions(_portletResourceActions, name);
575 
576         if (actions.size() == 0) {
577             synchronized (this) {
578                 Portlet portlet = PortletLocalServiceUtil.getPortletById(name);
579 
580                 if (portlet != null) {
581                     Map<String, Set<String>> portletModes =
582                         portlet.getPortletModes();
583 
584                     Set<String> mimeTypePortletModes = portletModes.get(
585                         ContentTypes.TEXT_HTML);
586 
587                     if (mimeTypePortletModes != null) {
588                         for (String actionId : mimeTypePortletModes) {
589                             if (actionId.equalsIgnoreCase("edit")) {
590                                 actions.add(ActionKeys.PREFERENCES);
591                             }
592                             else if (actionId.equalsIgnoreCase("edit_guest")) {
593                                 actions.add(ActionKeys.GUEST_PREFERENCES);
594                             }
595                             else {
596                                 actions.add(actionId.toUpperCase());
597                             }
598                         }
599                     }
600                 }
601                 else {
602                     if (_log.isWarnEnabled()) {
603                         _log.warn(
604                             "Unable to obtain resource actions for unknown " +
605                                 "portlet " + name);
606                     }
607                 }
608 
609                 _checkPortletActions(actions);
610 
611                 List<String> communityDefaultActions =
612                     _portletResourceCommunityDefaultActions.get(name);
613 
614                 if (communityDefaultActions == null) {
615                     communityDefaultActions = new UniqueList<String>();
616 
617                     _portletResourceCommunityDefaultActions.put(
618                         name, communityDefaultActions);
619 
620                     _checkPortletCommunityDefaultActions(
621                         communityDefaultActions);
622                 }
623 
624                 List<String> guestDefaultActions =
625                     _portletResourceGuestDefaultActions.get(name);
626 
627                 if (guestDefaultActions == null) {
628                     guestDefaultActions = new UniqueList<String>();
629 
630                     _portletResourceGuestDefaultActions.put(
631                         name, guestDefaultActions);
632 
633                     _checkPortletGuestDefaultActions(guestDefaultActions);
634                 }
635 
636                 List<String> layoutManagerActions =
637                     _portletResourceLayoutManagerActions.get(name);
638 
639                 if (layoutManagerActions == null) {
640                     layoutManagerActions = new UniqueList<String>();
641 
642                     _portletResourceLayoutManagerActions.put(
643                         name, layoutManagerActions);
644 
645                     _checkPortletLayoutManagerActions(layoutManagerActions);
646                 }
647             }
648         }
649 
650         return actions;
651     }
652 
653     private List<String> _getPortletResourceCommunityDefaultActions(
654         String name) {
655 
656         // This method should always be called only after
657         // _getPortletResourceActions has been called at least once to
658         // populate the default community actions. Check to make sure this is
659         // the case. However, if it is not, that means the methods
660         // _getPortletResourceGuestDefaultActions and
661         // _getPortletResourceGuestDefaultActions may not work either.
662 
663         name = PortletConstants.getRootPortletId(name);
664 
665         return _getActions(_portletResourceCommunityDefaultActions, name);
666     }
667 
668     private List<String> _getPortletResourceGuestDefaultActions(String name) {
669         name = PortletConstants.getRootPortletId(name);
670 
671         return _getActions(_portletResourceGuestDefaultActions, name);
672     }
673 
674     private List<String> _getPortletResourceGuestUnsupportedActions(
675         String name) {
676 
677         name = PortletConstants.getRootPortletId(name);
678 
679         return _getActions(_portletResourceGuestUnsupportedActions, name);
680     }
681 
682     private List<String> _getPortletResourceLayoutManagerActions(String name) {
683         name = PortletConstants.getRootPortletId(name);
684 
685         List<String> actions = _getActions(
686             _portletResourceLayoutManagerActions, name);
687 
688         // This check can never return an empty list. If the list is empty, it
689         // means that the portlet does not have an explicit resource-actions
690         // configuration file and should therefore be handled as if it has
691         // defaults of CONFIGURATION, PREFERENCES, and VIEW.
692 
693         if (actions.size() < 1) {
694             actions.add(ActionKeys.CONFIGURATION);
695             actions.add(ActionKeys.PREFERENCES);
696             actions.add(ActionKeys.VIEW);
697         }
698 
699         return actions;
700     }
701 
702     private void _init() {
703     }
704 
705     private boolean _isOrganizationModelResource(String modelResource) {
706         if (_organizationModelResources.contains(modelResource)) {
707             return true;
708         }
709         else {
710             return false;
711         }
712     }
713 
714     private boolean _isPortalModelResource(String modelResource) {
715         if (_portalModelResources.contains(modelResource)) {
716             return true;
717         }
718         else {
719             return false;
720         }
721     }
722 
723     private void _read(
724             String servletContextName, ClassLoader classLoader, String source)
725         throws Exception {
726 
727         String xml = null;
728 
729         try {
730             xml = StringUtil.read(classLoader, source);
731         }
732         catch (IOException ioe) {
733             if (_log.isWarnEnabled() && !source.endsWith("-ext.xml")) {
734                 _log.warn("Cannot load " + source);
735             }
736         }
737         catch (Exception e) {
738             if (_log.isWarnEnabled()) {
739                 _log.warn("Error reading " + source, e);
740             }
741         }
742 
743         if (xml == null) {
744             return;
745         }
746 
747         if (_log.isDebugEnabled()) {
748             _log.debug("Loading " + source);
749         }
750 
751         Document doc = SAXReaderUtil.read(xml);
752 
753         Element root = doc.getRootElement();
754 
755         Iterator<Element> itr1 = root.elements("resource").iterator();
756 
757         while (itr1.hasNext()) {
758             Element resource = itr1.next();
759 
760             String file = resource.attributeValue("file");
761 
762             _read(servletContextName, classLoader, file);
763 
764             String extFile = StringUtil.replace(file, ".xml", "-ext.xml");
765 
766             _read(servletContextName, classLoader, extFile);
767         }
768 
769         itr1 = root.elements("portlet-resource").iterator();
770 
771         while (itr1.hasNext()) {
772             Element resource = itr1.next();
773 
774             String name = resource.elementText("portlet-name");
775 
776             if (servletContextName != null) {
777                 name =
778                     name + PortletConstants.WAR_SEPARATOR + servletContextName;
779             }
780 
781             name = PortalUtil.getJsSafePortletId(name);
782 
783             // Actions
784 
785             List<String> actions = _getActions(_portletResourceActions, name);
786 
787             Element supports = resource.element("supports");
788 
789             Iterator<Element> itr2 = supports.elements("action-key").iterator();
790 
791             while (itr2.hasNext()) {
792                 Element actionKey = itr2.next();
793 
794                 String actionKeyText = actionKey.getText();
795 
796                 if (Validator.isNotNull(actionKeyText)) {
797                     actions.add(actionKeyText);
798                 }
799             }
800 
801             if (!name.equals(PortletKeys.PORTAL)) {
802                 _checkPortletActions(actions);
803             }
804 
805             // Community default actions
806 
807             List<String> communityDefaultActions = _getActions(
808                 _portletResourceCommunityDefaultActions, name);
809 
810             Element communityDefaults = resource.element("community-defaults");
811 
812             itr2 = communityDefaults.elements("action-key").iterator();
813 
814             while (itr2.hasNext()) {
815                 Element actionKey = itr2.next();
816 
817                 String actionKeyText = actionKey.getText();
818 
819                 if (Validator.isNotNull(actionKeyText)) {
820                     communityDefaultActions.add(actionKeyText);
821                 }
822             }
823 
824             // Guest default actions
825 
826             List<String> guestDefaultActions = _getActions(
827                 _portletResourceGuestDefaultActions, name);
828 
829             Element guestDefaults = resource.element("guest-defaults");
830 
831             itr2 = guestDefaults.elements("action-key").iterator();
832 
833             while (itr2.hasNext()) {
834                 Element actionKey = itr2.next();
835 
836                 String actionKeyText = actionKey.getText();
837 
838                 if (Validator.isNotNull(actionKeyText)) {
839                     guestDefaultActions.add(actionKeyText);
840                 }
841             }
842 
843             // Guest unsupported actions
844 
845             List<String> guestUnsupportedActions = _getActions(
846                 _portletResourceGuestUnsupportedActions, name);
847 
848             Element guestUnsupported = resource.element("guest-unsupported");
849 
850             itr2 = guestUnsupported.elements("action-key").iterator();
851 
852             while (itr2.hasNext()) {
853                 Element actionKey = itr2.next();
854 
855                 String actionKeyText = actionKey.getText();
856 
857                 if (Validator.isNotNull(actionKeyText)) {
858                     guestUnsupportedActions.add(actionKeyText);
859                 }
860             }
861 
862             _checkGuestUnsupportedActions(
863                 guestUnsupportedActions, guestDefaultActions);
864 
865             // Layout manager actions
866 
867             List<String> layoutManagerActions = _getActions(
868                 _portletResourceLayoutManagerActions, name);
869 
870             Element layoutManager = resource.element("layout-manager");
871 
872             if (layoutManager != null) {
873                 itr2 = layoutManager.elements("action-key").iterator();
874 
875                 while (itr2.hasNext()) {
876                     Element actionKey = itr2.next();
877 
878                     String actionKeyText = actionKey.getText();
879 
880                     if (Validator.isNotNull(actionKeyText)) {
881                         layoutManagerActions.add(actionKeyText);
882                     }
883                 }
884             }
885             else {
886 
887                 // Set the layout manager actions to contain all the portlet
888                 // resource actions if the element is not specified
889 
890                 layoutManagerActions.addAll(actions);
891             }
892         }
893 
894         itr1 = root.elements("model-resource").iterator();
895 
896         while (itr1.hasNext()) {
897             Element resource = itr1.next();
898 
899             String name = resource.elementText("model-name");
900 
901             Element portletRef = resource.element("portlet-ref");
902 
903             Iterator<Element> itr2 = portletRef.elements(
904                 "portlet-name").iterator();
905 
906             while (itr2.hasNext()) {
907                 Element portletName = itr2.next();
908 
909                 String portletNameString = portletName.getText();
910 
911                 if (servletContextName != null) {
912                     portletNameString =
913                         portletNameString + PortletConstants.WAR_SEPARATOR +
914                             servletContextName;
915                 }
916 
917                 portletNameString = PortalUtil.getJsSafePortletId(
918                     portletNameString);
919 
920                 // Reference for a portlet to child models
921 
922                 Set<String> modelResources = _portletModelResources.get(
923                     portletNameString);
924 
925                 if (modelResources == null) {
926                     modelResources = new HashSet<String>();
927 
928                     _portletModelResources.put(
929                         portletNameString, modelResources);
930                 }
931 
932                 modelResources.add(name);
933 
934                 // Reference for a model to parent portlets
935 
936                 Set<String> portletResources = _modelPortletResources.get(name);
937 
938                 if (portletResources == null) {
939                     portletResources = new HashSet<String>();
940 
941                     _modelPortletResources.put(name, portletResources);
942                 }
943 
944                 portletResources.add(portletNameString);
945             }
946 
947             // Actions
948 
949             List<String> actions = _getActions(_modelResourceActions, name);
950 
951             Element supports = resource.element("supports");
952 
953             itr2 = supports.elements("action-key").iterator();
954 
955             while (itr2.hasNext()) {
956                 Element actionKey = itr2.next();
957 
958                 String actionKeyText = actionKey.getText();
959 
960                 if (Validator.isNotNull(actionKeyText)) {
961                     actions.add(actionKeyText);
962                 }
963             }
964 
965             // Community default actions
966 
967             List<String> communityDefaultActions = _getActions(
968                 _modelResourceCommunityDefaultActions, name);
969 
970             Element communityDefaults = resource.element("community-defaults");
971 
972             itr2 = communityDefaults.elements("action-key").iterator();
973 
974             while (itr2.hasNext()) {
975                 Element actionKey = itr2.next();
976 
977                 String actionKeyText = actionKey.getText();
978 
979                 if (Validator.isNotNull(actionKeyText)) {
980                     communityDefaultActions.add(actionKeyText);
981                 }
982             }
983 
984             // Guest default actions
985 
986             List<String> guestDefaultActions = _getActions(
987                 _modelResourceGuestDefaultActions, name);
988 
989             Element guestDefaults = resource.element("guest-defaults");
990 
991             itr2 = guestDefaults.elements("action-key").iterator();
992 
993             while (itr2.hasNext()) {
994                 Element actionKey = itr2.next();
995 
996                 String actionKeyText = actionKey.getText();
997 
998                 if (Validator.isNotNull(actionKeyText)) {
999                     guestDefaultActions.add(actionKeyText);
1000                }
1001            }
1002
1003            // Guest unsupported actions
1004
1005            List<String> guestUnsupportedActions = _getActions(
1006                _modelResourceGuestUnsupportedActions, name);
1007
1008            Element guestUnsupported = resource.element("guest-unsupported");
1009
1010            itr2 = guestUnsupported.elements("action-key").iterator();
1011
1012            while (itr2.hasNext()) {
1013                Element actionKey = itr2.next();
1014
1015                String actionKeyText = actionKey.getText();
1016
1017                if (Validator.isNotNull(actionKeyText)) {
1018                    guestUnsupportedActions.add(actionKeyText);
1019                }
1020            }
1021
1022            _checkGuestUnsupportedActions(
1023                guestUnsupportedActions, guestDefaultActions);
1024
1025            // Owner default actions
1026
1027            List<String> ownerDefaultActions = _getActions(
1028                _modelResourceOwnerDefaultActions, name);
1029
1030            Element ownerDefaults = resource.element("owner-defaults");
1031
1032            if (ownerDefaults != null) {
1033                itr2 = ownerDefaults.elements("action-key").iterator();
1034
1035                while (itr2.hasNext()) {
1036                    Element actionKey = itr2.next();
1037
1038                    String actionKeyText = actionKey.getText();
1039
1040                    if (Validator.isNotNull(actionKeyText)) {
1041                        ownerDefaultActions.add(actionKeyText);
1042                    }
1043                }
1044            }
1045        }
1046    }
1047
1048    private static Log _log = LogFactoryUtil.getLog(ResourceActionsUtil.class);
1049
1050    private static ResourceActionsUtil _instance = new ResourceActionsUtil();
1051
1052    private Set<String> _organizationModelResources;
1053    private Set<String> _portalModelResources;
1054    private Map<String, Set<String>> _portletModelResources;
1055    private Map<String, List<String>> _portletResourceActions;
1056    private Map<String, List<String>> _portletResourceCommunityDefaultActions;
1057    private Map<String, List<String>> _portletResourceGuestDefaultActions;
1058    private Map<String, List<String>> _portletResourceGuestUnsupportedActions;
1059    private Map<String, List<String>> _portletResourceLayoutManagerActions;
1060    private Map<String, Set<String>> _modelPortletResources;
1061    private Map<String, List<String>> _modelResourceActions;
1062    private Map<String, List<String>> _modelResourceCommunityDefaultActions;
1063    private Map<String, List<String>> _modelResourceGuestDefaultActions;
1064    private Map<String, List<String>> _modelResourceGuestUnsupportedActions;
1065    private Map<String, List<String>> _modelResourceOwnerDefaultActions;
1066
1067}