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