001/*
002 *  Copyright 2016 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 org.apache.avalon.framework.context.Context;
019import org.apache.avalon.framework.context.ContextException;
020import org.apache.avalon.framework.context.Contextualizable;
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.cocoon.components.ContextHelper;
024import org.apache.cocoon.environment.Request;
025
026import org.ametys.plugins.repository.RepositoryConstants;
027import org.ametys.plugins.repository.collection.AmetysObjectCollection;
028import org.ametys.web.repository.site.Site;
029import org.ametys.web.repository.site.SiteManager;
030
031/**
032 *  Helper for retrieving root of contents for web
033 */
034public class RootContentHelper extends org.ametys.cms.content.RootContentHelper implements Contextualizable
035{
036    /** The site manager */
037    protected SiteManager _siteManager;
038    /** The context */
039    protected Context _context;
040    
041    @Override
042    public void contextualize(Context context) throws ContextException
043    {
044        _context = context;
045    }
046
047    @Override
048    public void service(ServiceManager manager) throws ServiceException
049    {
050        super.service(manager);
051        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
052    }
053    
054    @Override
055    public AmetysObjectCollection getRootContent()
056    {
057        String siteName = _getSiteName();
058        Site site = _siteManager.getSite(siteName);
059        if (site != null)
060        {
061            return site.getChild(RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ":contents");
062        }
063        else
064        {
065            return null;
066        }
067    }
068    
069    private String _getSiteName()
070    {
071        Request request = ContextHelper.getRequest(_context);
072        String siteName = request.getParameter("siteName");
073        
074        if (siteName == null)
075        {
076            siteName = (String) request.getAttribute("siteName");
077        }
078        return siteName;
079    }
080}