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