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; 019import java.util.Iterator; 020 021import org.apache.avalon.framework.component.Component; 022import org.apache.avalon.framework.service.ServiceException; 023import org.apache.avalon.framework.service.ServiceManager; 024import org.apache.avalon.framework.service.Serviceable; 025import org.apache.cocoon.xml.AttributesImpl; 026import org.apache.cocoon.xml.XMLUtils; 027import org.xml.sax.ContentHandler; 028import org.xml.sax.SAXException; 029 030import org.ametys.cms.filter.ContentFilter; 031import org.ametys.core.right.RightManager; 032import org.ametys.core.user.CurrentUserProvider; 033import org.ametys.plugins.repository.AmetysObjectIterable; 034import org.ametys.web.pageaccess.RestrictedPagePolicy; 035import org.ametys.web.renderingcontext.RenderingContext; 036import org.ametys.web.renderingcontext.RenderingContextHandler; 037import org.ametys.web.repository.page.Page; 038 039/** 040 * Component helper for manipulating {@link ContentFilter} 041 */ 042public class PageFilterHelper implements Component, Serviceable 043{ 044 /** The Avalon Role */ 045 public static final String ROLE = PageFilterHelper.class.getName(); 046 /** The right manager */ 047 protected RightManager _rightManager; 048 /** The rendering context handler. */ 049 protected RenderingContextHandler _renderingContextHandler; 050 /** The current user provider */ 051 protected CurrentUserProvider _currentUserProvider; 052 053 public void service(ServiceManager manager) throws ServiceException 054 { 055 _rightManager = (RightManager) manager.lookup(RightManager.ROLE); 056 _renderingContextHandler = (RenderingContextHandler) manager.lookup(RenderingContextHandler.ROLE); 057 _currentUserProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE); 058 } 059 060 /** 061 * SAX all pages matching the given filter 062 * @param handler The content handler to SAX into 063 * @param filter The filter 064 * @param siteName The current site name. Can be null. 065 * @param lang The current language. Can be null. 066 * @param currentPage The current page. Can be null. 067 * @throws SAXException If an error occurs while SAXing 068 * @throws IOException If an error occurs while retrieving content. 069 */ 070 public void saxMatchingPages (ContentHandler handler, PageFilter filter, String siteName, String lang, Page currentPage) throws SAXException, IOException 071 { 072 RestrictedPagePolicy policy = currentPage.getSite().getRestrictedPagePolicy(); 073 RenderingContext currentContext = _renderingContextHandler.getRenderingContext(); 074 AmetysObjectIterable<Page> pages = filter.getMatchingPages(siteName, lang, currentPage); 075 Iterator<Page> it = pages.iterator(); 076 077 int index = 0; 078 while (it.hasNext() && index < filter.getLength()) 079 { 080 Page page = it.next(); 081 082 // In back-office or in preview or live mode, access is always granted. 083 if (currentContext == RenderingContext.BACK || currentContext == RenderingContext.PREVIEW 084 || policy == RestrictedPagePolicy.DISPLAYED || isPageAccessible(page)) 085 { 086 saxPage(handler, page); 087 index++; 088 } 089 } 090 } 091 092 /** 093 * Test the page accessible by the current user. 094 * @param page the page 095 * @return true if the page is accessible, false otherwise. 096 */ 097 protected boolean isPageAccessible (Page page) 098 { 099 return _rightManager.hasReadAccess(_currentUserProvider.getUser(), page); 100 } 101 102 /** 103 * SAX a page in its specific view 104 * @param handler The content handler to SAX into 105 * @param page The page to SAX 106 * @throws SAXException If an error occurs while SAXing 107 * @throws IOException If an error occurs while retrieving content. 108 */ 109 public void saxPage (ContentHandler handler, Page page) throws SAXException, IOException 110 { 111 AttributesImpl attrs = new AttributesImpl(); 112 attrs.addCDATAAttribute("id", page.getId()); 113 attrs.addCDATAAttribute("name", page.getName()); 114 attrs.addCDATAAttribute("title", page.getTitle()); 115 116 XMLUtils.createElement(handler, "page", attrs); 117 } 118 119}