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.WebConstants;
032import org.ametys.web.WebHelper;
033import org.ametys.web.repository.site.Site;
034import org.ametys.web.repository.site.SiteManager;
035
036/**
037 * This action exports the current site and skin name.
038 */
039public class GetSiteAction extends ServiceableAction
040{
041    /** request attribute to force skin choice (instead of choosing the skin associated of the content) */
042    @Deprecated
043    public static final String OVERRIDE_SKIN_REQUEST_ATTR = "finalskin"; 
044    /** request attribute to force site choice (instead of choosing the site associated of the content) */
045    @Deprecated
046    public static final String OVERRIDE_SITE_REQUEST_ATTR = "finalsite"; 
047    
048    private SiteManager _siteManager;
049    
050    @Override
051    public void service(ServiceManager smanager) throws ServiceException
052    {
053        super.service(smanager);
054        _siteManager = (SiteManager) smanager.lookup(SiteManager.ROLE);
055    }
056    
057    @Override
058    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
059    {
060        String siteName = source;
061        String skin = null;
062        
063        Request request = ObjectModelHelper.getRequest(objectModel);
064        
065        if (siteName == null)
066        {
067            siteName = WebHelper.getSiteName(request, (Content) request.getAttribute(Content.class.getName()));
068        }
069        
070        if (siteName == null)
071        {
072            throw new RuntimeException("Unable to get site name from environment");
073        }
074
075        Site site = _siteManager.getSite(siteName);
076        
077        skin = (String) request.getAttribute(OVERRIDE_SKIN_REQUEST_ATTR);
078        if (skin == null)
079        {
080            skin = site.getSkinId();
081        }
082        
083        // FIXME hack bug CMS-2315
084        request.setAttribute(WebConstants.REQUEST_ATTR_SITE_NAME, siteName);
085        request.setAttribute(WebConstants.REQUEST_ATTR_SITE, site);
086        request.setAttribute("siteName", siteName);
087        request.setAttribute(WebConstants.REQUEST_ATTR_SKIN_ID, skin);
088        
089        Map<String, String> result = new HashMap<>();
090        result.put("site", siteName);
091        result.put("skin", skin);
092        result.put("jcrSitePath", site.getPath());
093        
094        return result;
095    }
096
097}