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