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