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