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.Map;
019
020import org.apache.cocoon.environment.Request;
021
022import org.ametys.core.observation.Event;
023import org.ametys.plugins.repository.AmetysObject;
024import org.ametys.web.repository.SiteAwareAmetysObject;
025import org.ametys.web.repository.page.SitemapElement;
026import org.ametys.web.repository.page.ZoneItem;
027import org.ametys.web.repository.site.Site;
028
029/**
030 * Helper for web
031 */
032public final class WebHelper
033{
034    private WebHelper()
035    {
036        // Empty
037    }
038    
039    /**
040     * Get the site name from the request.
041     * The site name is searched in the following order: the 'siteName' request parameter, the 'site' request attribute, the 'siteName' request attribute.
042     * @param request the request. Cannot be null.
043     * @return the site name or <code>null</code> if not found
044     */
045    public static String getSiteName(Request request)
046    {
047        return getSiteName(request, null);
048    }
049    
050    /**
051     * Get the site name from a object or current request.
052     * The site name is searched in the following order: the Ametys object, the 'siteName' request parameter, the 'site' request attribute, the 'siteName' request attribute.
053     * @param request the request. Cannot be null.
054     * @param object the object. Can be null.
055     * @return the site name or <code>null</code> if not found
056     */
057    public static String getSiteName(Request request, AmetysObject object)
058    {
059        String siteName = null;
060        if (object != null && object instanceof SiteAwareAmetysObject)
061        {
062            siteName = ((SiteAwareAmetysObject) object).getSiteName();
063        }
064        else
065        {
066            siteName = request.getParameter("siteName");
067            
068            if (siteName == null)
069            {
070                siteName = (String) request.getAttribute("siteName"); // From FO request
071            }
072            
073            if (siteName == null)
074            {
075                siteName = (String) request.getAttribute(WebConstants.REQUEST_ATTR_SITE_NAME);
076            }
077            
078            if (siteName == null)
079            {
080                Site site = (Site) request.getAttribute(WebConstants.REQUEST_ATTR_SITE);
081                if (site != null)
082                {
083                    siteName = site.getName();
084                }
085            }
086        }
087        
088        return siteName;
089    }
090    
091    /**
092     * Retrieves the site from an observation {@link Event}.<br>
093     * This method search in the events' arguments to find :<br>
094     * <ul>
095     * <li><code>ObservationConstants.ARGS_SITE</code>
096     * <li><code>ObservationConstants.ARGS_PAGE</code>
097     * <li><code>ObservationConstants.ARGS_ZONE_ITEM</code>
098     * <li><code>ObservationConstants.ARGS_ACL_CONTEXT</code>
099     * <li><code>object</code> if it's instance of {@link SiteAwareAmetysObject}
100     * </ul>
101     * @param event the {@link Event}.
102     * @return the current site or null if not found.
103     */
104    public static Site findSite(Event event)
105    {
106        Map<String, Object> args = event.getArguments();
107        
108        if (args.containsKey(ObservationConstants.ARGS_SITE))
109        {
110            return (Site) args.get(ObservationConstants.ARGS_SITE);
111        }
112        else if (args.containsKey(ObservationConstants.ARGS_SITEMAP_ELEMENT))
113        {
114            return ((SitemapElement) args.get(ObservationConstants.ARGS_SITEMAP_ELEMENT)).getSite();
115        }
116        else if (args.containsKey(ObservationConstants.ARGS_ZONE_ITEM))
117        {
118            return ((ZoneItem) args.get(ObservationConstants.ARGS_ZONE_ITEM)).getZone().getSitemapElement().getSite();
119        }
120        else if (args.containsKey(org.ametys.core.ObservationConstants.ARGS_ACL_CONTEXT))
121        {
122            Object object = args.get(org.ametys.core.ObservationConstants.ARGS_ACL_CONTEXT);
123            if (object != null && object instanceof SiteAwareAmetysObject)
124            {
125                return ((SiteAwareAmetysObject) object).getSite();
126            }
127        }
128        else
129        {
130            AmetysObject ao = (AmetysObject) args.get("object");
131            if (ao != null && ao instanceof SiteAwareAmetysObject)
132            {
133                return ((SiteAwareAmetysObject) ao).getSite();
134            }
135        }
136
137        return null;
138    }
139}