001/* 002 * Copyright 2015 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.cache; 017 018import java.util.Collection; 019import java.util.Iterator; 020 021import org.apache.avalon.framework.component.Component; 022import org.apache.avalon.framework.logger.AbstractLogEnabled; 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025import org.apache.avalon.framework.service.Serviceable; 026 027import org.ametys.core.right.RightManager; 028import org.ametys.plugins.repository.AmetysObjectIterable; 029import org.ametys.web.inputdata.InputData; 030import org.ametys.web.inputdata.InputDataExtensionPoint; 031import org.ametys.web.repository.page.Page; 032import org.ametys.web.repository.page.Page.PageType; 033import org.ametys.web.repository.page.Zone; 034import org.ametys.web.repository.page.ZoneItem; 035import org.ametys.web.repository.page.ZoneItem.ZoneType; 036import org.ametys.web.service.Service; 037import org.ametys.web.service.ServiceExtensionPoint; 038import org.ametys.web.site.SiteConfigurationExtensionPoint; 039 040/** 041 * Helper component providing status of pages. 042 */ 043public class PageHelper extends AbstractLogEnabled implements Component, Serviceable 044{ 045 /** The component role. */ 046 public static final String ROLE = PageHelper.class.getName(); 047 048 private ServiceExtensionPoint _serviceExtPt; 049 private RightManager _rightManager; 050 private InputDataExtensionPoint _inputDataExtensionPoint; 051 private SiteConfigurationExtensionPoint _siteConfiguration; 052 053 @Override 054 public void service(ServiceManager serviceManager) throws ServiceException 055 { 056 _serviceExtPt = (ServiceExtensionPoint) serviceManager.lookup(ServiceExtensionPoint.ROLE); 057 _rightManager = (RightManager) serviceManager.lookup(RightManager.ROLE); 058 _inputDataExtensionPoint = (InputDataExtensionPoint) serviceManager.lookup(InputDataExtensionPoint.ROLE); 059 _siteConfiguration = (SiteConfigurationExtensionPoint) serviceManager.lookup(SiteConfigurationExtensionPoint.ROLE); 060 } 061 062 /** 063 * Tests if a page is cacheable, i.e:<ul> 064 * <li>its access is not restricted</li> 065 * <li>all its page elements are cachable</li> 066 * </ul> 067 * @param page The page to test. 068 * @return true if the page is cachable, false otherwise. 069 */ 070 public boolean isCacheable(Page page) 071 { 072 if (!_rightManager.hasAnonymousReadAccess(page)) 073 { 074 // never cache a private page 075 return false; 076 } 077 078 if (page.getType() != PageType.CONTAINER) 079 { 080 // makes no sense to cache a Node or link page 081 return false; 082 } 083 084 boolean isCacheable = true; 085 AmetysObjectIterable<? extends Zone> zones = page.getZones(); 086 Iterator<? extends Zone> itZ = zones.iterator(); 087 088 // test each service of the page 089 while (isCacheable && itZ.hasNext()) 090 { 091 Zone zone = itZ.next(); 092 AmetysObjectIterable<? extends ZoneItem> zoneItems = zone.getZoneItems(); 093 Iterator<? extends ZoneItem> itZI = zoneItems.iterator(); 094 095 while (isCacheable && itZI.hasNext()) 096 { 097 ZoneItem zoneItem = itZI.next(); 098 if (zoneItem.getType() == ZoneType.SERVICE) 099 { 100 Service service = _serviceExtPt.getExtension(zoneItem.getServiceId()); 101 try 102 { 103 isCacheable = service != null && service.isCacheable(page, zoneItem); 104 } 105 catch (Exception e) 106 { 107 // Ignore 108 } 109 } 110 } 111 } 112 113 // Test each inputdata 114 if (isCacheable) 115 { 116 Collection<String> ids = _inputDataExtensionPoint.getExtensionsIds(); 117 Iterator<String> it = ids.iterator(); 118 while (isCacheable && it.hasNext()) 119 { 120 String id = it.next(); 121 InputData inputData = _inputDataExtensionPoint.getExtension(id); 122 isCacheable = inputData.isCacheable(page.getSite(), page); 123 } 124 } 125 126 return isCacheable; 127 } 128 129 /** 130 * Determines if a captcha is required on forms of the page 131 * @param page The page to test 132 * @return true if a captcha is required 133 */ 134 public boolean isCaptchaRequired (Page page) 135 { 136 String siteName = page.getSiteName(); 137 138 String captchaPolicy = _siteConfiguration.getValueAsString(siteName, "display-captcha-policy"); 139 140 if (captchaPolicy == null || "restricted".equals(captchaPolicy)) 141 { 142 // Display captcha on public page only 143 return _rightManager.hasAnonymousReadAccess(page); 144 } 145 146 return "always".equals(captchaPolicy); 147 } 148 149}