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.io.IOException;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022import org.apache.cocoon.ProcessingException;
023import org.apache.cocoon.environment.ObjectModelHelper;
024import org.apache.cocoon.environment.Request;
025import org.apache.cocoon.generation.ServiceableGenerator;
026import org.apache.cocoon.xml.XMLUtils;
027import org.xml.sax.SAXException;
028
029import org.ametys.cms.filter.ContentFilterHelper;
030import org.ametys.cms.repository.Content;
031import org.ametys.plugins.repository.AmetysObjectResolver;
032import org.ametys.plugins.repository.AmetysRepositoryException;
033import org.ametys.plugins.repository.UnknownAmetysObjectException;
034import org.ametys.web.WebConstants;
035import org.ametys.web.filter.WebContentFilterHelper;
036
037/**
038 * Generate the main view of a {@link Content}
039 */
040public class HtmlContentGenerator extends ServiceableGenerator
041{
042    private WebContentFilterHelper _filterHelper;
043    private AmetysObjectResolver _resolver;
044    
045    @Override
046    public void service(ServiceManager smanager) throws ServiceException
047    {
048        super.service(smanager);
049        _filterHelper = (WebContentFilterHelper) smanager.lookup(ContentFilterHelper.ROLE);
050        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
051    }
052    
053    @SuppressWarnings("deprecation")
054    @Override
055    public void generate() throws IOException, SAXException, ProcessingException
056    {
057        Request request = ObjectModelHelper.getRequest(objectModel);
058        String currentSiteName = (String) request.getAttribute(WebConstants.REQUEST_ATTR_SITE_NAME);
059        String currentSkin = (String) request.getAttribute(WebConstants.REQUEST_ATTR_SKIN_ID);
060        
061        request.setAttribute(GetSiteAction.OVERRIDE_SKIN_REQUEST_ATTR, currentSkin);
062        request.setAttribute(GetSiteAction.OVERRIDE_SITE_REQUEST_ATTR, currentSiteName);
063        
064        contentHandler.startDocument();
065        
066        try
067        {
068            Content content = _resolver.resolveById(source);
069            String viewName = parameters.getParameter("viewName", "main");
070            _filterHelper.saxContent(contentHandler, content, viewName, false);
071        }
072        catch (UnknownAmetysObjectException e)
073        {
074            getLogger().warn("The content " + source + " does not exist anymore", e);
075            XMLUtils.createElement(contentHandler, "content");
076        }
077        catch (AmetysRepositoryException e)
078        {
079            getLogger().error("Unable to retrieve content of id '" + source + "'", e);
080            XMLUtils.createElement(contentHandler, "content");
081        }
082        finally
083        {
084            request.removeAttribute(GetSiteAction.OVERRIDE_SITE_REQUEST_ATTR);
085            request.removeAttribute(GetSiteAction.OVERRIDE_SKIN_REQUEST_ATTR);
086            request.setAttribute(WebConstants.REQUEST_ATTR_SKIN_ID, currentSkin);
087            request.setAttribute(WebConstants.REQUEST_ATTR_SITE_NAME, currentSiteName);
088        }
089        
090        contentHandler.endDocument();
091
092    }
093
094}