001/* 002 * Copyright 2012 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; 033 034/** 035 * Generates SAX events for contents matching a {@link ContentFilter} set in request attributes 036 */ 037public class FilteredPagesGenerator extends ServiceableGenerator 038{ 039 private PageFilterHelper _filterHelper; 040 041 @Override 042 public void service(ServiceManager smanager) throws ServiceException 043 { 044 super.service(smanager); 045 _filterHelper = (PageFilterHelper) smanager.lookup(PageFilterHelper.ROLE); 046 } 047 048 @SuppressWarnings("deprecation") 049 @Override 050 public void generate() throws IOException, SAXException, ProcessingException 051 { 052 Request request = ObjectModelHelper.getRequest(objectModel); 053 String currentSiteName = (String) request.getAttribute(WebConstants.REQUEST_ATTR_SITE_NAME); 054 String currentSkinName = (String) request.getAttribute(WebConstants.REQUEST_ATTR_SKIN_ID); 055 String currentTemplateName = (String) request.getAttribute(WebConstants.REQUEST_ATTR_TEMPLATE_ID); 056 057 // Issue CMS-3391 058 request.setAttribute(GetSiteAction.OVERRIDE_SITE_REQUEST_ATTR, currentSiteName); 059 request.setAttribute(GetSiteAction.OVERRIDE_SKIN_REQUEST_ATTR, currentSkinName); 060 061 // Get filter in request attributes 062 PageFilter filter = (PageFilter) request.getAttribute("filter"); 063 064 // Get site, language and page in request attributes. Can be null if the filter does not need them. 065 String siteName = (String) request.getAttribute(WebConstants.REQUEST_ATTR_SITE_NAME); 066 Page page = (Page) request.getAttribute(WebConstants.REQUEST_ATTR_PAGE); 067 String lang = page != null ? page.getSitemap().getName() : null; 068 069 try 070 { 071 contentHandler.startDocument(); 072 XMLUtils.startElement(contentHandler, "pages"); 073 074 if (filter != null) 075 { 076 _filterHelper.saxMatchingPages(contentHandler, filter, siteName, lang, page); 077 } 078 079 XMLUtils.endElement(contentHandler, "pages"); 080 contentHandler.endDocument(); 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_SITE_NAME, currentSiteName); 087 request.setAttribute(WebConstants.REQUEST_ATTR_SKIN_ID, currentSkinName); 088 request.setAttribute(WebConstants.REQUEST_ATTR_TEMPLATE_ID, currentTemplateName); 089 } 090 091 } 092}