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.content;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import org.apache.avalon.framework.parameters.Parameters;
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.cocoon.acting.ServiceableAction;
025import org.apache.cocoon.environment.ObjectModelHelper;
026import org.apache.cocoon.environment.Redirector;
027import org.apache.cocoon.environment.Request;
028import org.apache.cocoon.environment.SourceResolver;
029
030import org.ametys.cms.repository.Content;
031import org.ametys.web.repository.content.WebContent;
032import org.ametys.web.repository.site.Site;
033import org.ametys.web.repository.site.SiteManager;
034
035/**
036 * This action exports the current site and skin name.
037 */
038public class GetSiteAction extends ServiceableAction
039{
040    /** request attribute to force skin choice (instead of choosing the skin associated of the content) */
041    @Deprecated
042    public static final String OVERRIDE_SKIN_REQUEST_ATTR = "finalskin"; 
043    /** request attribute to force site choice (instead of choosing the site associated of the content) */
044    @Deprecated
045    public static final String OVERRIDE_SITE_REQUEST_ATTR = "finalsite"; 
046    
047    private SiteManager _siteManager;
048    
049    @Override
050    public void service(ServiceManager smanager) throws ServiceException
051    {
052        super.service(smanager);
053        _siteManager = (SiteManager) smanager.lookup(SiteManager.ROLE);
054    }
055    
056    @Override
057    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
058    {
059        String siteName = source;
060        String skin = null;
061        
062        Request request = ObjectModelHelper.getRequest(objectModel);
063        
064        if (siteName == null)
065        {
066            Content content = (Content) request.getAttribute(Content.class.getName());
067            if (content instanceof WebContent)
068            {
069                siteName = ((WebContent) content).getSiteName();
070            }
071        }
072        
073        if (siteName == null)
074        {
075            siteName = request.getParameter("siteName");
076        }
077        
078        if (siteName == null)
079        {
080            siteName = (String) request.getAttribute("siteName");
081        }
082        
083        if (siteName == null)
084        {
085            siteName = (String) request.getAttribute("site");
086        }
087        
088        if (siteName == null)
089        {
090            throw new RuntimeException("Unable to get site name from environment");
091        }
092
093        Site site = _siteManager.getSite(siteName);
094        
095        skin = (String) request.getAttribute(OVERRIDE_SKIN_REQUEST_ATTR);
096        if (skin == null)
097        {
098            skin = site.getSkinId();
099        }
100        
101        // FIXME hack bug CMS-2315
102        request.setAttribute("site", siteName);
103        request.setAttribute("siteName", siteName);
104        request.setAttribute("skin", skin);
105        
106        Map<String, String> result = new HashMap<>();
107        result.put("site", siteName);
108        result.put("skin", skin);
109        result.put("jcrSitePath", site.getPath());
110        
111        return result;
112    }
113
114}