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