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