001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.security.permission;
016    
017    import com.liferay.portal.NoSuchResourceActionException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.language.LanguageUtil;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.util.ContentTypes;
023    import com.liferay.portal.kernel.util.GetterUtil;
024    import com.liferay.portal.kernel.util.ListUtil;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.util.StringUtil;
027    import com.liferay.portal.kernel.util.Validator;
028    import com.liferay.portal.kernel.xml.Document;
029    import com.liferay.portal.kernel.xml.Element;
030    import com.liferay.portal.kernel.xml.SAXReaderUtil;
031    import com.liferay.portal.model.Group;
032    import com.liferay.portal.model.Organization;
033    import com.liferay.portal.model.PasswordPolicy;
034    import com.liferay.portal.model.Permission;
035    import com.liferay.portal.model.Portlet;
036    import com.liferay.portal.model.PortletConstants;
037    import com.liferay.portal.model.ResourceAction;
038    import com.liferay.portal.model.Role;
039    import com.liferay.portal.model.RoleConstants;
040    import com.liferay.portal.model.User;
041    import com.liferay.portal.model.UserGroup;
042    import com.liferay.portal.service.PortletLocalServiceUtil;
043    import com.liferay.portal.service.ResourceActionLocalServiceUtil;
044    import com.liferay.portal.service.RoleLocalServiceUtil;
045    import com.liferay.portal.util.PortalUtil;
046    import com.liferay.portal.util.PortletKeys;
047    import com.liferay.portal.util.PropsValues;
048    import com.liferay.portlet.PortletResourceBundles;
049    import com.liferay.portlet.expando.model.ExpandoColumn;
050    import com.liferay.portlet.social.model.SocialEquityActionMapping;
051    import com.liferay.util.UniqueList;
052    
053    import java.io.InputStream;
054    
055    import java.util.ArrayList;
056    import java.util.Collections;
057    import java.util.HashMap;
058    import java.util.HashSet;
059    import java.util.Iterator;
060    import java.util.List;
061    import java.util.Locale;
062    import java.util.Map;
063    import java.util.Set;
064    
065    import javax.servlet.jsp.PageContext;
066    
067    /**
068     * @author Brian Wing Shun Chan
069     * @author Daeyoung Song
070     */
071    public class ResourceActionsUtil {
072    
073            public static final String ACTION_NAME_PREFIX = "action.";
074    
075            public static final String MODEL_RESOURCE_NAME_PREFIX = "model.resource.";
076    
077            public static final String[] ORGANIZATION_MODEL_RESOURCES = {
078                    Organization.class.getName(), PasswordPolicy.class.getName(),
079                    User.class.getName()
080            };
081    
082            public static final String[] PORTAL_MODEL_RESOURCES = {
083                    ExpandoColumn.class.getName(), Organization.class.getName(),
084                    PasswordPolicy.class.getName(), Role.class.getName(),
085                    User.class.getName(), UserGroup.class.getName()
086            };
087    
088            public static void checkAction(String name, String actionId)
089                    throws NoSuchResourceActionException {
090    
091                    List<String> resourceActions = getResourceActions(name);
092    
093                    if (!resourceActions.contains(actionId)) {
094                            throw new NoSuchResourceActionException(
095                                    name.concat(StringPool.POUND).concat(actionId));
096                    }
097            }
098    
099            public static String getAction(Locale locale, String action) {
100                    String key = ACTION_NAME_PREFIX + action;
101    
102                    String value = LanguageUtil.get(locale, key, null);
103    
104                    if ((value == null) || (value.equals(key))) {
105                            value = PortletResourceBundles.getString(locale, key);
106                    }
107    
108                    if (value == null) {
109                            value = key;
110                    }
111    
112                    return value;
113            }
114    
115            public static String getAction(PageContext pageContext, String action) {
116                    String key = ACTION_NAME_PREFIX + action;
117    
118                    String value = LanguageUtil.get(pageContext, key, null);
119    
120                    if ((value == null) || (value.equals(key))) {
121                            value = PortletResourceBundles.getString(pageContext, key);
122                    }
123    
124                    if (value == null) {
125                            value = key;
126                    }
127    
128                    return value;
129            }
130    
131            public static List<String> getActions(List<Permission> permissions) {
132                    List<String> actions = new UniqueList<String>();
133    
134                    for (Permission permission : permissions) {
135                            actions.add(permission.getActionId());
136                    }
137    
138                    return actions;
139            }
140    
141            public static List<String> getActionsNames(
142                    PageContext pageContext, List<String> actions) {
143    
144                    List<String> uniqueList = new UniqueList<String>();
145    
146                    for (String action : actions) {
147                            uniqueList.add(getAction(pageContext, action));
148                    }
149    
150                    List<String> list = new ArrayList<String>();
151    
152                    list.addAll(uniqueList);
153    
154                    return list;
155            }
156    
157            public static List<String> getActionsNames(
158                    PageContext pageContext, String name, long actionIds) {
159    
160                    try {
161                            List<ResourceAction> resourceActions =
162                                    ResourceActionLocalServiceUtil.getResourceActions(name);
163    
164                            List<String> actions = new ArrayList<String>();
165    
166                            for (ResourceAction resourceAction : resourceActions) {
167                                    long bitwiseValue = resourceAction.getBitwiseValue();
168    
169                                    if ((actionIds & bitwiseValue) == bitwiseValue) {
170                                            actions.add(resourceAction.getActionId());
171                                    }
172                            }
173    
174                            return getActionsNames(pageContext, actions);
175                    }
176                    catch (Exception e) {
177                            _log.error(e, e);
178    
179                            return Collections.EMPTY_LIST;
180                    }
181            }
182    
183            public static List<String> getModelNames() {
184                    return _instance._getModelNames();
185            }
186    
187            public static List<String> getModelPortletResources(String name) {
188                    return _instance._getModelPortletResources(name);
189            }
190    
191            public static String getModelResource(Locale locale, String name) {
192                    String key = MODEL_RESOURCE_NAME_PREFIX + name;
193    
194                    String value = LanguageUtil.get(locale, key, null);
195    
196                    if ((value == null) || (value.equals(key))) {
197                            value = PortletResourceBundles.getString(locale, key);
198                    }
199    
200                    if (value == null) {
201                            value = key;
202                    }
203    
204                    return value;
205            }
206    
207            public static String getModelResource(
208                    PageContext pageContext, String name) {
209    
210                    String key = MODEL_RESOURCE_NAME_PREFIX + name;
211    
212                    String value = LanguageUtil.get(pageContext, key, null);
213    
214                    if ((value == null) || (value.equals(key))) {
215                            value = PortletResourceBundles.getString(pageContext, key);
216                    }
217    
218                    if (value == null) {
219                            value = key;
220                    }
221    
222                    return value;
223            }
224    
225            public static List<String> getModelResourceActions(String name) {
226                    return _instance._getModelResourceActions(name);
227            }
228    
229            public static List<String> getModelResourceCommunityDefaultActions(
230                    String name) {
231    
232                    return _instance._getModelResourceCommunityDefaultActions(name);
233            }
234    
235            public static List<String> getModelResourceGuestDefaultActions(
236                    String name) {
237    
238                    return _instance._getModelResourceGuestDefaultActions(name);
239            }
240    
241            public static List<String> getModelResourceGuestUnsupportedActions(
242                    String name) {
243    
244                    return _instance._getModelResourceGuestUnsupportedActions(name);
245            }
246    
247            public static List<String> getModelResourceOwnerDefaultActions(
248                    String name) {
249    
250                    return _instance._getModelResourceOwnerDefaultActions(name);
251            }
252    
253            public static String getPortletBaseResource(String portletName) {
254                    return _instance._getPortletBaseResource(portletName);
255            }
256    
257            public static List<String> getPortletModelResources(String portletName) {
258                    return _instance._getPortletModelResources(portletName);
259            }
260    
261            public static List<String> getPortletNames() {
262                    return _instance._getPortletNames();
263            }
264    
265            public static List<String> getPortletResourceActions(String name) {
266                    return _instance._getPortletResourceActions(name);
267            }
268    
269            public static List<String> getPortletResourceCommunityDefaultActions(
270                    String name) {
271    
272                    return _instance._getPortletResourceCommunityDefaultActions(name);
273            }
274    
275            public static List<String> getPortletResourceGuestDefaultActions(
276                    String name) {
277    
278                    return _instance._getPortletResourceGuestDefaultActions(name);
279            }
280    
281            public static List<String> getPortletResourceGuestUnsupportedActions(
282                    String name) {
283    
284                    return _instance._getPortletResourceGuestUnsupportedActions(name);
285            }
286    
287            public static List<String> getPortletResourceLayoutManagerActions(
288                    String name) {
289    
290                    return _instance._getPortletResourceLayoutManagerActions(name);
291            }
292    
293            public static List<String> getResourceActions(String name) {
294                    if (name.contains(StringPool.PERIOD)) {
295                            return getModelResourceActions(name);
296                    }
297                    else {
298                            return getPortletResourceActions(name);
299                    }
300            }
301    
302            public static List<String> getResourceActions(
303                    String portletResource, String modelResource) {
304    
305                    List<String> actions = null;
306    
307                    if (Validator.isNull(modelResource)) {
308                            actions = getPortletResourceActions(portletResource);
309                    }
310                    else {
311                            actions = getModelResourceActions(modelResource);
312                    }
313    
314                    return actions;
315            }
316    
317            public static List<String> getResourceCommunityDefaultActions(String name) {
318                    if (name.contains(StringPool.PERIOD)) {
319                            return getModelResourceCommunityDefaultActions(name);
320                    }
321                    else {
322                            return getPortletResourceCommunityDefaultActions(name);
323                    }
324            }
325    
326            public static List<String> getResourceGuestUnsupportedActions(
327                    String portletResource, String modelResource) {
328    
329                    List<String> actions = null;
330    
331                    if (Validator.isNull(modelResource)) {
332                            actions =
333                                    getPortletResourceGuestUnsupportedActions(portletResource);
334                    }
335                    else {
336                            actions = getModelResourceGuestUnsupportedActions(modelResource);
337                    }
338    
339                    return actions;
340            }
341    
342            public static List<Role> getRoles(
343                            long companyId, Group group, String modelResource)
344                    throws SystemException {
345    
346                    List<Role> allRoles = RoleLocalServiceUtil.getRoles(companyId);
347    
348                    int[] types = new int[] {
349                            RoleConstants.TYPE_REGULAR, RoleConstants.TYPE_COMMUNITY
350                    };
351    
352                    if (isPortalModelResource(modelResource)) {
353                            if (modelResource.equals(Organization.class.getName()) ||
354                                    modelResource.equals(User.class.getName())) {
355    
356                                    types = new int[] {
357                                            RoleConstants.TYPE_REGULAR,
358                                            RoleConstants.TYPE_ORGANIZATION
359                                    };
360                            }
361                            else {
362                                    types = new int[] {RoleConstants.TYPE_REGULAR};
363                            }
364                    }
365                    else {
366                            if (group != null) {
367                                    if (group.isOrganization()) {
368                                            types = new int[] {
369                                                    RoleConstants.TYPE_REGULAR,
370                                                    RoleConstants.TYPE_ORGANIZATION
371                                            };
372                                    }
373                                    else if (group.isUser()) {
374                                            types = new int[] {RoleConstants.TYPE_REGULAR};
375                                    }
376                            }
377                    }
378    
379                    List<Role> roles = new ArrayList<Role>();
380    
381                    for (int type : types) {
382                            for (Role role : allRoles) {
383                                    if (role.getType() == type) {
384                                            roles.add(role);
385                                    }
386                            }
387                    }
388    
389                    return roles;
390            }
391    
392            public static SocialEquityActionMapping getSocialEquityActionMapping(
393                    String name, String actionId) {
394    
395                    return _instance._getSocialEquityActionMapping(name, actionId);
396            }
397    
398            public static List<SocialEquityActionMapping> getSocialEquityActionMappings(
399                    String name) {
400    
401                    return _instance._getSocialEquityActionMappings(name);
402            }
403    
404            public static String[] getSocialEquityClassNames() {
405                    return _instance._getSocialEquityClassNames();
406            }
407    
408            public static boolean hasModelResourceActions(String name) {
409                    return _instance._hasModelResourceActions(name);
410            }
411    
412            public static void init() {
413                    _instance._init();
414            }
415    
416            public static boolean isOrganizationModelResource(String modelResource) {
417                    return _instance._isOrganizationModelResource(modelResource);
418            }
419    
420            public static boolean isPortalModelResource(String modelResource) {
421                    return _instance._isPortalModelResource(modelResource);
422            }
423    
424            public static void read(
425                            String servletContextName, ClassLoader classLoader, String source)
426                    throws Exception {
427    
428                    _instance._read(servletContextName, classLoader, source);
429            }
430    
431            public static void read(String servletContextName, InputStream inputStream)
432                    throws Exception {
433    
434                    _instance._read(servletContextName, inputStream);
435            }
436    
437            private ResourceActionsUtil() {
438                    _organizationModelResources = new HashSet<String>();
439    
440                    for (String resource : ORGANIZATION_MODEL_RESOURCES) {
441                            _organizationModelResources.add(resource);
442                    }
443    
444                    _portalModelResources = new HashSet<String>();
445    
446                    for (String resource : PORTAL_MODEL_RESOURCES) {
447                            _portalModelResources.add(resource);
448                    }
449    
450                    _portletModelResources = new HashMap<String, Set<String>>();
451                    _portletResourceActions = new HashMap<String, List<String>>();
452                    _portletResourceCommunityDefaultActions =
453                            new HashMap<String, List<String>>();
454                    _portletResourceGuestDefaultActions =
455                            new HashMap<String, List<String>>();
456                    _portletResourceGuestUnsupportedActions =
457                            new HashMap<String, List<String>>();
458                    _portletResourceLayoutManagerActions =
459                            new HashMap<String, List<String>>();
460                    _modelPortletResources = new HashMap<String, Set<String>>();
461                    _modelResourceActions = new HashMap<String, List<String>>();
462                    _modelResourceCommunityDefaultActions =
463                            new HashMap<String, List<String>>();
464                    _modelResourceGuestDefaultActions =
465                            new HashMap<String, List<String>>();
466                    _modelResourceGuestUnsupportedActions =
467                            new HashMap<String, List<String>>();
468                    _modelResourceOwnerDefaultActions =
469                            new HashMap<String, List<String>>();
470                    _socialEquityActionMappings =
471                            new HashMap<String, Map<String, SocialEquityActionMapping>>();
472    
473                    try {
474                            ClassLoader classLoader = getClass().getClassLoader();
475    
476                            for (String config : PropsValues.RESOURCE_ACTIONS_CONFIGS) {
477                                    _read(null, classLoader, config);
478                            }
479                    }
480                    catch (Exception e) {
481                            _log.error(e, e);
482                    }
483            }
484    
485            private void _checkGuestUnsupportedActions(
486                    List<String> guestUnsupportedActions,
487                    List<String> guestDefaultActions) {
488    
489                    // Guest default actions cannot reference guest unsupported actions
490    
491                    Iterator<String> itr = guestDefaultActions.iterator();
492    
493                    while (itr.hasNext()) {
494                            String actionId = itr.next();
495    
496                            if (guestUnsupportedActions.contains(actionId)) {
497                                    itr.remove();
498                            }
499                    }
500            }
501    
502            private void _checkModelActions(List<String> actions) {
503                    if (!actions.contains(ActionKeys.PERMISSIONS)) {
504                            actions.add(ActionKeys.PERMISSIONS);
505                    }
506            }
507    
508            private void _checkPortletActions(List<String> actions) {
509                    if (!actions.contains(ActionKeys.ACCESS_IN_CONTROL_PANEL) &&
510                            !actions.contains(ActionKeys.ADD_TO_PAGE)) {
511    
512                            actions.add(ActionKeys.ADD_TO_PAGE);
513                    }
514    
515                    if (!actions.contains(ActionKeys.CONFIGURATION)) {
516                            actions.add(ActionKeys.CONFIGURATION);
517                    }
518    
519                    if (!actions.contains(ActionKeys.VIEW)) {
520                            actions.add(ActionKeys.VIEW);
521                    }
522            }
523    
524            private void _checkPortletCommunityDefaultActions(List<String> actions) {
525                    if (actions.size() == 0) {
526                            actions.add(ActionKeys.VIEW);
527                    }
528            }
529    
530            private void _checkPortletGuestDefaultActions(List<String> actions) {
531                    if (actions.size() == 0) {
532                            actions.add(ActionKeys.VIEW);
533                    }
534            }
535    
536            private void _checkPortletLayoutManagerActions(List<String> actions) {
537                    if (!actions.contains(ActionKeys.CONFIGURATION)) {
538                            actions.add(ActionKeys.CONFIGURATION);
539                    }
540    
541                    if (!actions.contains(ActionKeys.PREFERENCES)) {
542                            actions.add(ActionKeys.PREFERENCES);
543                    }
544    
545                    if (!actions.contains(ActionKeys.VIEW)) {
546                            actions.add(ActionKeys.VIEW);
547                    }
548            }
549    
550            private List<String> _getActions(
551                    Map<String, List<String>> actionsMap, String name) {
552    
553                    List<String> actions = actionsMap.get(name);
554    
555                    if (actions == null) {
556                            actions = new UniqueList<String>();
557    
558                            actionsMap.put(name, actions);
559                    }
560    
561                    return actions;
562            }
563    
564            private List<String> _getModelNames() {
565                    return ListUtil.fromCollection(_modelPortletResources.keySet());
566            }
567    
568            private List<String> _getModelPortletResources(String name) {
569                    Set<String> resources = _modelPortletResources.get(name);
570    
571                    if (resources == null) {
572                            return new UniqueList<String>();
573                    }
574                    else {
575                            return Collections.list(Collections.enumeration(resources));
576                    }
577            }
578    
579            private List<String> _getModelResourceActions(String name) {
580                    return _getActions(_modelResourceActions, name);
581            }
582    
583            private List<String> _getModelResourceCommunityDefaultActions(
584                    String name) {
585    
586                    return _getActions(_modelResourceCommunityDefaultActions, name);
587            }
588    
589            private List<String> _getModelResourceGuestDefaultActions(String name) {
590                    return _getActions(_modelResourceGuestDefaultActions, name);
591            }
592    
593            private List<String> _getModelResourceGuestUnsupportedActions(String name) {
594                    return _getActions(_modelResourceGuestUnsupportedActions, name);
595            }
596    
597            private List<String> _getModelResourceOwnerDefaultActions(String name) {
598                    return _getActions(_modelResourceOwnerDefaultActions, name);
599            }
600    
601            private Element _getPermissionsChildElement(
602                    Element parentElement, String childElementName) {
603    
604                    Element permissionsElement = parentElement.element("permissions");
605    
606                    if (permissionsElement != null) {
607                            return permissionsElement.element(childElementName);
608                    }
609                    else {
610                            return parentElement.element(childElementName);
611                    }
612            }
613    
614            private String _getPortletBaseResource(String portletName) {
615                    List<String> modelNames = _getPortletModelResources(portletName);
616    
617                    for (String modelName : modelNames) {
618                            if (!modelName.contains(".model.")) {
619                                    return modelName;
620                            }
621                    }
622    
623                    return null;
624            }
625    
626            private List<String> _getPortletMimeTypeActions(String name) {
627                    List<String> actions = new UniqueList<String>();
628    
629                    Portlet portlet = PortletLocalServiceUtil.getPortletById(name);
630    
631                    if (portlet != null) {
632                            Map<String, Set<String>> portletModes = portlet.getPortletModes();
633    
634                            Set<String> mimeTypePortletModes = portletModes.get(
635                                    ContentTypes.TEXT_HTML);
636    
637                            if (mimeTypePortletModes != null) {
638                                    for (String actionId : mimeTypePortletModes) {
639                                            if (actionId.equalsIgnoreCase("edit")) {
640                                                    actions.add(ActionKeys.PREFERENCES);
641                                            }
642                                            else if (actionId.equalsIgnoreCase("edit_guest")) {
643                                                    actions.add(ActionKeys.GUEST_PREFERENCES);
644                                            }
645                                            else {
646                                                    actions.add(actionId.toUpperCase());
647                                            }
648                                    }
649                            }
650                    }
651                    else {
652                            if (_log.isDebugEnabled()) {
653                                    _log.debug(
654                                            "Unable to obtain resource actions for unknown portlet " +
655                                                    name);
656                            }
657                    }
658    
659                    return actions;
660            }
661    
662            private List<String> _getPortletModelResources(String portletName) {
663                    portletName = PortletConstants.getRootPortletId(portletName);
664    
665                    Set<String> resources = _portletModelResources.get(portletName);
666    
667                    if (resources == null) {
668                            return new UniqueList<String>();
669                    }
670                    else {
671                            return Collections.list(Collections.enumeration(resources));
672                    }
673            }
674    
675            private List<String> _getPortletNames() {
676                    return ListUtil.fromCollection(_portletModelResources.keySet());
677            }
678    
679            private List<String> _getPortletResourceActions(String name) {
680                    name = PortletConstants.getRootPortletId(name);
681    
682                    List<String> actions = _getActions(_portletResourceActions, name);
683    
684                    if (actions.size() == 0) {
685                            synchronized (this) {
686                                    actions = _getPortletMimeTypeActions(name);
687    
688                                    if (!name.equals(PortletKeys.PORTAL)) {
689                                            _checkPortletActions(actions);
690                                    }
691    
692                                    List<String> communityDefaultActions =
693                                            _portletResourceCommunityDefaultActions.get(name);
694    
695                                    if (communityDefaultActions == null) {
696                                            communityDefaultActions = new UniqueList<String>();
697    
698                                            _portletResourceCommunityDefaultActions.put(
699                                                    name, communityDefaultActions);
700    
701                                            _checkPortletCommunityDefaultActions(
702                                                    communityDefaultActions);
703                                    }
704    
705                                    List<String> guestDefaultActions =
706                                            _portletResourceGuestDefaultActions.get(name);
707    
708                                    if (guestDefaultActions == null) {
709                                            guestDefaultActions = new UniqueList<String>();
710    
711                                            _portletResourceGuestDefaultActions.put(
712                                                    name, guestDefaultActions);
713    
714                                            _checkPortletGuestDefaultActions(guestDefaultActions);
715                                    }
716    
717                                    List<String> layoutManagerActions =
718                                            _portletResourceLayoutManagerActions.get(name);
719    
720                                    if (layoutManagerActions == null) {
721                                            layoutManagerActions = new UniqueList<String>();
722    
723                                            _portletResourceLayoutManagerActions.put(
724                                                    name, layoutManagerActions);
725    
726                                            _checkPortletLayoutManagerActions(layoutManagerActions);
727                                    }
728                            }
729                    }
730    
731                    return actions;
732            }
733    
734            private List<String> _getPortletResourceCommunityDefaultActions(
735                    String name) {
736    
737                    // This method should always be called only after
738                    // _getPortletResourceActions has been called at least once to
739                    // populate the default community actions. Check to make sure this is
740                    // the case. However, if it is not, that means the methods
741                    // _getPortletResourceGuestDefaultActions and
742                    // _getPortletResourceGuestDefaultActions may not work either.
743    
744                    name = PortletConstants.getRootPortletId(name);
745    
746                    return _getActions(_portletResourceCommunityDefaultActions, name);
747            }
748    
749            private List<String> _getPortletResourceGuestDefaultActions(String name) {
750                    name = PortletConstants.getRootPortletId(name);
751    
752                    return _getActions(_portletResourceGuestDefaultActions, name);
753            }
754    
755            private List<String> _getPortletResourceGuestUnsupportedActions(
756                    String name) {
757    
758                    name = PortletConstants.getRootPortletId(name);
759    
760                    return _getActions(_portletResourceGuestUnsupportedActions, name);
761            }
762    
763            private List<String> _getPortletResourceLayoutManagerActions(String name) {
764                    name = PortletConstants.getRootPortletId(name);
765    
766                    List<String> actions = _getActions(
767                            _portletResourceLayoutManagerActions, name);
768    
769                    // This check can never return an empty list. If the list is empty, it
770                    // means that the portlet does not have an explicit resource-actions
771                    // configuration file and should therefore be handled as if it has
772                    // defaults of CONFIGURATION, PREFERENCES, and VIEW.
773    
774                    if (actions.size() < 1) {
775                            actions.add(ActionKeys.CONFIGURATION);
776                            actions.add(ActionKeys.PREFERENCES);
777                            actions.add(ActionKeys.VIEW);
778                    }
779    
780                    return actions;
781            }
782    
783            private SocialEquityActionMapping _getSocialEquityActionMapping(
784                    String name, String actionId) {
785    
786                    Map<String, SocialEquityActionMapping> socialEquityActionMappings =
787                            _socialEquityActionMappings.get(name);
788    
789                    if (socialEquityActionMappings == null) {
790                            return null;
791                    }
792    
793                    return socialEquityActionMappings.get(actionId);
794            }
795    
796            private List<SocialEquityActionMapping> _getSocialEquityActionMappings(
797                    String name) {
798    
799                    Map<String, SocialEquityActionMapping> socialEquityActionMappings =
800                            _socialEquityActionMappings.get(name);
801    
802                    if (socialEquityActionMappings == null) {
803                            return Collections.EMPTY_LIST;
804                    }
805    
806                    List<SocialEquityActionMapping> socialEquityActionMappingList =
807                            new ArrayList<SocialEquityActionMapping>();
808    
809                    for (Map.Entry<String, SocialEquityActionMapping> entry :
810                                    socialEquityActionMappings.entrySet()) {
811    
812                            socialEquityActionMappingList.add(entry.getValue());
813                    }
814    
815                    return socialEquityActionMappingList;
816            }
817    
818            private String[] _getSocialEquityClassNames() {
819                    Set<String> classNames = _socialEquityActionMappings.keySet();
820    
821                    return classNames.toArray(new String[classNames.size()]);
822            }
823    
824            private boolean _hasModelResourceActions(String name) {
825                    List<String> actions = _modelResourceActions.get(name);
826    
827                    if ((actions != null) && !actions.isEmpty()) {
828                            return true;
829                    }
830                    else {
831                            return false;
832                    }
833            }
834    
835            private void _init() {
836            }
837    
838            private boolean _isOrganizationModelResource(String modelResource) {
839                    if (_organizationModelResources.contains(modelResource)) {
840                            return true;
841                    }
842                    else {
843                            return false;
844                    }
845            }
846    
847            private boolean _isPortalModelResource(String modelResource) {
848                    if (_portalModelResources.contains(modelResource)) {
849                            return true;
850                    }
851                    else {
852                            return false;
853                    }
854            }
855    
856            private void _read(
857                            String servletContextName, ClassLoader classLoader, String source)
858                    throws Exception {
859    
860                    InputStream inputStream = classLoader.getResourceAsStream(source);
861    
862                    if (inputStream == null) {
863                            if (_log.isWarnEnabled() && !source.endsWith("-ext.xml")) {
864                                    _log.warn("Cannot load " + source);
865                            }
866    
867                            return;
868                    }
869    
870                    if (_log.isDebugEnabled()) {
871                            _log.debug("Loading " + source);
872                    }
873    
874                    Document document = SAXReaderUtil.read(inputStream);
875    
876                    Element rootElement = document.getRootElement();
877    
878                    for (Element resourceElement : rootElement.elements("resource")) {
879                            String file = resourceElement.attributeValue("file").trim();
880    
881                            _read(servletContextName, classLoader, file);
882    
883                            String extFile = StringUtil.replace(file, ".xml", "-ext.xml");
884    
885                            _read(servletContextName, classLoader, extFile);
886                    }
887    
888                    _read(servletContextName, document);
889            }
890    
891            private void _read(String servletContextName, Document document)
892                    throws Exception {
893    
894                    Element rootElement = document.getRootElement();
895    
896                    if (PropsValues.RESOURCE_ACTIONS_READ_PORTLET_RESOURCES) {
897                            for (Element portletResourceElement :
898                                            rootElement.elements("portlet-resource")) {
899    
900                                    _readPortletResource(
901                                            servletContextName, portletResourceElement);
902                            }
903                    }
904    
905                    for (Element modelResourceElement :
906                                    rootElement.elements("model-resource")) {
907    
908                            _readModelResource(servletContextName, modelResourceElement);
909                    }
910            }
911    
912            private void _read(String servletContextName, InputStream inputStream)
913                    throws Exception {
914    
915                    Document document = SAXReaderUtil.read(inputStream);
916    
917                    _read(servletContextName, document);
918            }
919    
920            private void _readActionKeys(Element parentElement, List<String> actions) {
921                    for (Element actionKeyElement : parentElement.elements("action-key")) {
922                            String actionKey = actionKeyElement.getTextTrim();
923    
924                            if (Validator.isNull(actionKey)) {
925                                    continue;
926                            }
927    
928                            actions.add(actionKey);
929                    }
930            }
931    
932            private void _readCommunityDefaultActions(
933                    Element parentElement, Map<String, List<String>> actionsMap,
934                    String name) {
935    
936                    List<String> communityDefaultActions = _getActions(actionsMap, name);
937    
938                    Element communityDefaultsElement = _getPermissionsChildElement(
939                            parentElement, "community-defaults");
940    
941                    _readActionKeys(communityDefaultsElement, communityDefaultActions);
942            }
943    
944            private List<String> _readGuestDefaultActions(
945                    Element parentElement, Map<String, List<String>> actionsMap,
946                    String name) {
947    
948                    List<String> guestDefaultActions = _getActions(actionsMap, name);
949    
950                    Element guestDefaultsElement = _getPermissionsChildElement(
951                            parentElement, "guest-defaults");
952    
953                    _readActionKeys(guestDefaultsElement, guestDefaultActions);
954    
955                    return guestDefaultActions;
956            }
957    
958            private void _readGuestUnsupportedActions(
959                    Element parentElement, Map<String, List<String>> actionsMap,
960                    String name, List<String> guestDefaultActions) {
961    
962                    List<String> guestUnsupportedActions = _getActions(actionsMap, name);
963    
964                    Element guestUnsupportedElement = _getPermissionsChildElement(
965                            parentElement, "guest-unsupported");
966    
967                    _readActionKeys(guestUnsupportedElement, guestUnsupportedActions);
968    
969                    _checkGuestUnsupportedActions(
970                            guestUnsupportedActions, guestDefaultActions);
971            }
972    
973            private void _readLayoutManagerActions(
974                    Element parentElement, Map<String, List<String>> actionsMap,
975                    String name, List<String> supportsActions) {
976    
977                    List<String> layoutManagerActions = _getActions(actionsMap, name);
978    
979                    Element layoutManagerElement = _getPermissionsChildElement(
980                            parentElement, "layout-manager");
981    
982                    if (layoutManagerElement != null) {
983                            _readActionKeys(layoutManagerElement, layoutManagerActions);
984                    }
985                    else {
986                            layoutManagerActions.addAll(supportsActions);
987                    }
988            }
989    
990            private void _readModelResource(
991                    String servletContextName, Element modelResourceElement) {
992    
993                    String name = modelResourceElement.elementTextTrim("model-name");
994    
995                    Element portletRefElement = modelResourceElement.element("portlet-ref");
996    
997                    for (Element portletNameElement :
998                                    portletRefElement.elements("portlet-name")) {
999    
1000                            String portletName = portletNameElement.getTextTrim();
1001    
1002                            if (servletContextName != null) {
1003                                    portletName =
1004                                            portletName.concat(PortletConstants.WAR_SEPARATOR).concat(
1005                                                    servletContextName);
1006                            }
1007    
1008                            portletName = PortalUtil.getJsSafePortletId(portletName);
1009    
1010                            // Reference for a portlet to child models
1011    
1012                            Set<String> modelResources = _portletModelResources.get(
1013                                    portletName);
1014    
1015                            if (modelResources == null) {
1016                                    modelResources = new HashSet<String>();
1017    
1018                                    _portletModelResources.put(portletName, modelResources);
1019                            }
1020    
1021                            modelResources.add(name);
1022    
1023                            // Reference for a model to parent portlets
1024    
1025                            Set<String> portletResources = _modelPortletResources.get(name);
1026    
1027                            if (portletResources == null) {
1028                                    portletResources = new HashSet<String>();
1029    
1030                                    _modelPortletResources.put(name, portletResources);
1031                            }
1032    
1033                            portletResources.add(portletName);
1034                    }
1035    
1036                    List<String> supportsActions = _readSupportsActions(
1037                            modelResourceElement, _modelResourceActions, name);
1038    
1039                    _checkModelActions(supportsActions);
1040    
1041                    _readCommunityDefaultActions(
1042                            modelResourceElement, _modelResourceCommunityDefaultActions,  name);
1043    
1044                    List<String> guestDefaultActions = _readGuestDefaultActions(
1045                            modelResourceElement, _modelResourceGuestDefaultActions, name);
1046    
1047                    _readGuestUnsupportedActions(
1048                            modelResourceElement, _modelResourceGuestUnsupportedActions, name,
1049                            guestDefaultActions);
1050    
1051                    _readOwnerDefaultActions(
1052                            modelResourceElement, _modelResourceOwnerDefaultActions, name);
1053    
1054                    _readSocialEquity(modelResourceElement, name);
1055            }
1056    
1057            private void _readOwnerDefaultActions(
1058                    Element parentElement, Map<String, List<String>> actionsMap,
1059                    String name) {
1060    
1061                    List<String> ownerDefaultActions = _getActions(actionsMap, name);
1062    
1063                    Element ownerDefaultsElement = _getPermissionsChildElement(
1064                            parentElement, "owner-defaults");
1065    
1066                    if (ownerDefaultsElement == null) {
1067                            return;
1068                    }
1069    
1070                    _readActionKeys(ownerDefaultsElement, ownerDefaultActions);
1071            }
1072    
1073            private void _readPortletResource(
1074                    String servletContextName, Element portletResourceElement) {
1075    
1076                    String name = portletResourceElement.elementTextTrim("portlet-name");
1077    
1078                    if (servletContextName != null) {
1079                            name = name.concat(PortletConstants.WAR_SEPARATOR).concat(
1080                                    servletContextName);
1081                    }
1082    
1083                    name = PortalUtil.getJsSafePortletId(name);
1084    
1085                    List<String> supportsActions = _readSupportsActions(
1086                            portletResourceElement, _portletResourceActions, name);
1087    
1088                    supportsActions.addAll(_getPortletMimeTypeActions(name));
1089    
1090                    if (!name.equals(PortletKeys.PORTAL)) {
1091                            _checkPortletActions(supportsActions);
1092                    }
1093    
1094                    _readCommunityDefaultActions(
1095                            portletResourceElement, _portletResourceCommunityDefaultActions,
1096                            name);
1097    
1098                    List<String> guestDefaultActions = _readGuestDefaultActions(
1099                            portletResourceElement, _portletResourceGuestDefaultActions, name);
1100    
1101                    _readGuestUnsupportedActions(
1102                            portletResourceElement, _portletResourceGuestUnsupportedActions,
1103                            name, guestDefaultActions);
1104    
1105                    _readLayoutManagerActions(
1106                            portletResourceElement, _portletResourceLayoutManagerActions, name,
1107                            supportsActions);
1108            }
1109    
1110            private void _readSocialEquity(Element parentElement, String name) {
1111                    Element socialEquityElement = parentElement.element("social-equity");
1112    
1113                    if (socialEquityElement == null) {
1114                            return;
1115                    }
1116    
1117                    for (Element socialEquityMappingElement :
1118                                    socialEquityElement.elements("social-equity-mapping")) {
1119    
1120                            _readSocialEquityMapping(socialEquityMappingElement, name);
1121                    }
1122            }
1123    
1124            private void _readSocialEquityMapping(
1125                    Element socialEquityMappingElement, String name) {
1126    
1127                    Element actionKeyElement =
1128                            socialEquityMappingElement.element("action-key");
1129    
1130                    if (actionKeyElement == null) {
1131                            return;
1132                    }
1133    
1134                    String actionKey = actionKeyElement.getTextTrim();
1135    
1136                    if (Validator.isNull(actionKey)) {
1137                            return;
1138                    }
1139    
1140                    int informationDailyLimit = GetterUtil.getInteger(
1141                            socialEquityMappingElement.elementText("information-daily-limit"));
1142                    int informationLifespan = GetterUtil.getInteger(
1143                            socialEquityMappingElement.elementText("information-lifespan"));
1144                    int informationValue = GetterUtil.getInteger(
1145                            socialEquityMappingElement.elementText("information-value"));
1146                    int participationDailyLimit = GetterUtil.getInteger(
1147                            socialEquityMappingElement.elementText(
1148                                    "participation-daily-limit"));
1149                    int participationLifespan = GetterUtil.getInteger(
1150                            socialEquityMappingElement.elementText("participation-lifespan"));
1151                    int participationValue = GetterUtil.getInteger(
1152                            socialEquityMappingElement.elementText("participation-value"));
1153    
1154                    SocialEquityActionMapping socialEquityActionMapping =
1155                            new SocialEquityActionMapping();
1156    
1157                    socialEquityActionMapping.setActionId(actionKey);
1158                    socialEquityActionMapping.setClassName(name);
1159                    socialEquityActionMapping.setInformationDailyLimit(
1160                            informationDailyLimit);
1161                    socialEquityActionMapping.setInformationLifespan(informationLifespan);
1162                    socialEquityActionMapping.setInformationValue(informationValue);
1163                    socialEquityActionMapping.setParticipationDailyLimit(
1164                            participationDailyLimit);
1165                    socialEquityActionMapping.setParticipationLifespan(
1166                            participationLifespan);
1167                    socialEquityActionMapping.setParticipationValue(participationValue);
1168    
1169                    Map<String, SocialEquityActionMapping> socialEquityActionMappings =
1170                            _socialEquityActionMappings.get(name);
1171    
1172                    if (socialEquityActionMappings == null) {
1173                            socialEquityActionMappings =
1174                                    new HashMap<String, SocialEquityActionMapping>();
1175    
1176                            _socialEquityActionMappings.put(name, socialEquityActionMappings);
1177                    }
1178    
1179                    socialEquityActionMappings.put(actionKey, socialEquityActionMapping);
1180            }
1181    
1182            private List<String> _readSupportsActions(
1183                    Element parentElement, Map<String, List<String>> actionsMap,
1184                    String name) {
1185    
1186                    List<String> supportsActions = _getActions(actionsMap, name);
1187    
1188                    Element supportsElement = _getPermissionsChildElement(
1189                            parentElement, "supports");
1190    
1191                    _readActionKeys(supportsElement, supportsActions);
1192    
1193                    return supportsActions;
1194            }
1195    
1196            private static Log _log = LogFactoryUtil.getLog(ResourceActionsUtil.class);
1197    
1198            private static ResourceActionsUtil _instance = new ResourceActionsUtil();
1199    
1200            private Map<String, Set<String>> _modelPortletResources;
1201            private Map<String, List<String>> _modelResourceActions;
1202            private Map<String, List<String>> _modelResourceCommunityDefaultActions;
1203            private Map<String, List<String>> _modelResourceGuestDefaultActions;
1204            private Map<String, List<String>> _modelResourceGuestUnsupportedActions;
1205            private Map<String, List<String>> _modelResourceOwnerDefaultActions;
1206            private Set<String> _organizationModelResources;
1207            private Set<String> _portalModelResources;
1208            private Map<String, Set<String>> _portletModelResources;
1209            private Map<String, List<String>> _portletResourceActions;
1210            private Map<String, List<String>> _portletResourceCommunityDefaultActions;
1211            private Map<String, List<String>> _portletResourceGuestDefaultActions;
1212            private Map<String, List<String>> _portletResourceGuestUnsupportedActions;
1213            private Map<String, List<String>> _portletResourceLayoutManagerActions;
1214            private Map<String, Map<String, SocialEquityActionMapping>>
1215                    _socialEquityActionMappings;
1216    
1217    }