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.filter;
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.ContentFilter;
030import org.ametys.web.WebConstants;
031import org.ametys.web.content.GetSiteAction;
032import org.ametys.web.repository.page.Page;
033import org.ametys.web.repository.page.ZoneItem;
034
035/**
036 * Generates SAX events for contents matching a {@link ContentFilter} set in request attributes
037 */
038public class FilteredContentsGenerator extends ServiceableGenerator
039{
040    private ContentFilterHelper _filterHelper;
041    
042    @Override
043    public void service(ServiceManager smanager) throws ServiceException
044    {
045        super.service(smanager);
046        _filterHelper = (ContentFilterHelper) smanager.lookup(ContentFilterHelper.ROLE);
047    }
048    
049    @SuppressWarnings("deprecation")
050    @Override
051    public void generate() throws IOException, SAXException, ProcessingException
052    {
053        long t0 = System.currentTimeMillis();
054        
055        Request request = ObjectModelHelper.getRequest(objectModel);
056        String currentSiteName = (String) request.getAttribute(WebConstants.REQUEST_ATTR_SITE_NAME);
057        String currentSkinName = (String) request.getAttribute(WebConstants.REQUEST_ATTR_SKIN_ID);
058        String currentTemplateName = (String) request.getAttribute(WebConstants.REQUEST_ATTR_TEMPLATE_ID);
059        String currentLanguage = (String) request.getAttribute("renderingLanguage");
060        
061        // Issue CMS-3391
062        request.setAttribute(GetSiteAction.OVERRIDE_SITE_REQUEST_ATTR, currentSiteName);
063        request.setAttribute(GetSiteAction.OVERRIDE_SKIN_REQUEST_ATTR, currentSkinName);
064        
065        // Get filter in request attributes
066        ContentFilter filter = (ContentFilter) request.getAttribute("filter");
067        
068        // Get site, language and page in request attributes. Can be null if the filter does not need them.
069        String siteName = (String) request.getAttribute(WebConstants.REQUEST_ATTR_SITE_NAME);
070        Page page = (Page) request.getAttribute(WebConstants.REQUEST_ATTR_PAGE);
071        String lang = page != null ? page.getSitemap().getName() : null;
072        ZoneItem zoneItem = (ZoneItem) request.getAttribute(WebConstants.REQUEST_ATTR_ZONEITEM);
073        
074        contentHandler.startDocument();
075        XMLUtils.startElement(contentHandler, "contents");
076        
077        try
078        {
079            _saxRSSFeedURL(zoneItem);
080            
081            if (filter != null && filter instanceof WebContentFilter)
082            {
083                _filterHelper.saxMatchingContents(contentHandler, (WebContentFilter) filter, siteName, lang, page);
084            }
085        }
086        finally
087        {
088            request.removeAttribute(GetSiteAction.OVERRIDE_SITE_REQUEST_ATTR);
089            request.removeAttribute(GetSiteAction.OVERRIDE_SKIN_REQUEST_ATTR);
090            request.setAttribute(WebConstants.REQUEST_ATTR_SITE_NAME, currentSiteName);
091            request.setAttribute("siteName", currentSiteName);
092            request.setAttribute(WebConstants.REQUEST_ATTR_SKIN_ID, currentSkinName);
093            request.setAttribute(WebConstants.REQUEST_ATTR_TEMPLATE_ID, currentTemplateName);
094            request.setAttribute("renderingLanguage", currentLanguage);
095        }
096        
097        XMLUtils.endElement(contentHandler, "contents");
098        contentHandler.endDocument();
099        
100        if (getLogger().isDebugEnabled())
101        {
102            long t1 = System.currentTimeMillis();
103            getLogger().debug("Filtered processing time " + (t1 - t0) + "ms");
104        }
105    }
106    
107    /**
108     * Saxes the RSS feed URL
109     * @param zoneItem the zone item containing the service
110     * @throws SAXException if an error occurs while saxing the RSS feed URL
111     */
112    protected void _saxRSSFeedURL (ZoneItem zoneItem) throws SAXException
113    {
114        if (zoneItem != null && zoneItem.getServiceParameters().getValue("rss", false, false))
115        {
116            // Split protocol and id
117            String[] zoneItemId = zoneItem.getId().split("://");
118            String url = "_plugins/web/" + zoneItemId[0] + "/" + zoneItemId[1] + "/rss.xml";
119            
120            XMLUtils.createElement(contentHandler, "RSSFeedURL", url);
121        }
122    }
123}