001/*
002 *  Copyright 2010 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.web;
017
018import java.util.ArrayList;
019import java.util.List;
020import java.util.Map;
021
022import org.apache.cocoon.environment.Request;
023import org.apache.commons.lang.StringUtils;
024
025import org.ametys.core.observation.Event;
026import org.ametys.core.user.population.PopulationContextHelper;
027import org.ametys.plugins.repository.AmetysObject;
028import org.ametys.web.repository.SiteAwareAmetysObject;
029import org.ametys.web.repository.page.SitemapElement;
030import org.ametys.web.repository.page.ZoneItem;
031import org.ametys.web.repository.site.Site;
032
033/**
034 * Helper for web
035 */
036public final class WebHelper
037{
038    private WebHelper()
039    {
040        // Empty
041    }
042    
043    /**
044     * Get the site name from the request.
045     * The site name is searched in the following order: the 'siteName' request parameter, the 'site' request attribute, the 'siteName' request attribute.
046     * @param request the request. Cannot be null.
047     * @return the site name or <code>null</code> if not found
048     */
049    public static String getSiteName(Request request)
050    {
051        return getSiteName(request, null);
052    }
053    
054    /**
055     * Get the site name from a object or current request.
056     * The site name is searched in the following order: the Ametys object, the 'siteName' request parameter, the 'site' request attribute, the 'siteName' request attribute.
057     * @param request the request. Cannot be null.
058     * @param object the object. Can be null.
059     * @return the site name or <code>null</code> if not found
060     */
061    public static String getSiteName(Request request, AmetysObject object)
062    {
063        String siteName = null;
064        if (object != null && object instanceof SiteAwareAmetysObject)
065        {
066            siteName = ((SiteAwareAmetysObject) object).getSiteName();
067        }
068        else
069        {
070            siteName = request.getParameter("siteName");
071            
072            if (siteName == null)
073            {
074                siteName = (String) request.getAttribute("siteName"); // From FO request
075            }
076            
077            if (siteName == null)
078            {
079                siteName = (String) request.getAttribute(WebConstants.REQUEST_ATTR_SITE_NAME);
080            }
081            
082            if (siteName == null)
083            {
084                Site site = (Site) request.getAttribute(WebConstants.REQUEST_ATTR_SITE);
085                if (site != null)
086                {
087                    siteName = site.getName();
088                }
089            }
090        }
091        
092        return siteName;
093    }
094    
095    /**
096     * Retrieves the site from an observation {@link Event}.<br>
097     * This method search in the events' arguments to find :<br>
098     * <ul>
099     * <li><code>ObservationConstants.ARGS_SITE</code>
100     * <li><code>ObservationConstants.ARGS_PAGE</code>
101     * <li><code>ObservationConstants.ARGS_ZONE_ITEM</code>
102     * <li><code>ObservationConstants.ARGS_ACL_CONTEXT</code>
103     * <li><code>object</code> if it's instance of {@link SiteAwareAmetysObject}
104     * </ul>
105     * @param event the {@link Event}.
106     * @return the current site or null if not found.
107     */
108    public static Site findSite(Event event)
109    {
110        Map<String, Object> args = event.getArguments();
111        
112        if (args.containsKey(ObservationConstants.ARGS_SITE))
113        {
114            return (Site) args.get(ObservationConstants.ARGS_SITE);
115        }
116        else if (args.containsKey(ObservationConstants.ARGS_SITEMAP_ELEMENT))
117        {
118            return ((SitemapElement) args.get(ObservationConstants.ARGS_SITEMAP_ELEMENT)).getSite();
119        }
120        else if (args.containsKey(ObservationConstants.ARGS_ZONE_ITEM))
121        {
122            return ((ZoneItem) args.get(ObservationConstants.ARGS_ZONE_ITEM)).getZone().getSitemapElement().getSite();
123        }
124        else if (args.containsKey(org.ametys.core.ObservationConstants.ARGS_ACL_CONTEXT))
125        {
126            Object object = args.get(org.ametys.core.ObservationConstants.ARGS_ACL_CONTEXT);
127            if (object != null && object instanceof SiteAwareAmetysObject)
128            {
129                return ((SiteAwareAmetysObject) object).getSite();
130            }
131        }
132        else
133        {
134            AmetysObject ao = (AmetysObject) args.get("object");
135            if (ao != null && ao instanceof SiteAwareAmetysObject)
136            {
137                return ((SiteAwareAmetysObject) ao).getSite();
138            }
139        }
140
141        return null;
142    }
143    
144    /**
145     * Set the population context attribute from the given site in the request
146     * @param request the request
147     * @param site the site. Can be null. In this case, take the site in the request
148     */
149    public static void setPopulationContextAttribute(Request request, Site site)
150    {
151        String siteName = site != null ? site.getName() : getSiteName(request);
152        if (StringUtils.isNotBlank(siteName))
153        {
154            // Set the site name into the request for the other components to be able to retrieve it.
155            request.setAttribute("siteName", siteName);
156            
157            List<String> populationContexts = new ArrayList<>();
158            
159            // Set the population contexts to be able to get allowed users
160            populationContexts.add("/sites/" + siteName);
161            populationContexts.add("/sites-fo/" + siteName);
162            
163            request.setAttribute(PopulationContextHelper.POPULATION_CONTEXTS_REQUEST_ATTR, populationContexts);
164        }
165    }
166}