001/* 002 * Copyright 2016 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.Map; 020 021import org.apache.avalon.framework.service.ServiceException; 022import org.apache.avalon.framework.service.ServiceManager; 023import org.apache.avalon.framework.service.Serviceable; 024 025import org.ametys.core.observation.Event; 026import org.ametys.core.observation.Observer; 027import org.ametys.core.right.RightManager; 028import org.ametys.runtime.plugin.component.AbstractLogEnabled; 029import org.ametys.web.repository.page.Page; 030import org.ametys.web.repository.page.SitemapElement; 031import org.ametys.web.repository.sitemap.Sitemap; 032 033/** 034 * {@link Observer} for observing page or sitemap ACL update in order to 035 * invalidate cache on front-office. 036 */ 037public class InvalidateCacheOnSitemapACLUpdateObserver extends AbstractLogEnabled implements Observer, Serviceable 038{ 039 private FOCommHelper _foCommHelper; 040 041 public void service(ServiceManager manager) throws ServiceException 042 { 043 _foCommHelper = (FOCommHelper) manager.lookup(FOCommHelper.ROLE); 044 } 045 046 public int getPriority() 047 { 048 // Will be processed after Solr update 049 return Observer.MAX_PRIORITY + 4000; 050 } 051 052 public boolean supports(Event event) 053 { 054 return event.getId().equals(org.ametys.core.ObservationConstants.EVENT_ACL_UPDATED); 055 } 056 057 public void observe(Event event, Map<String, Object> transientVars) throws Exception 058 { 059 SitemapElement pageCt = _getSitemapElement (event); 060 061 if (pageCt != null) 062 { 063 if (pageCt instanceof Sitemap) 064 { 065 // Invalidate the whole sitemap 066 _foCommHelper.invalidateFOCache((Sitemap) pageCt); 067 } 068 else if (pageCt instanceof Page) 069 { 070 // Invalidate the whole sitemap (menu can changed) 071 _foCommHelper.invalidateFOCache(pageCt.getSitemap()); 072 } 073 } 074 } 075 076 private SitemapElement _getSitemapElement (Event event) 077 { 078 Map<String, Object> args = event.getArguments(); 079 Object aclContext = args.get(org.ametys.core.ObservationConstants.ARGS_ACL_CONTEXT); 080 @SuppressWarnings("unchecked") 081 Collection<String> profileIds = (Collection<String>) args.get(org.ametys.core.ObservationConstants.ARGS_ACL_PROFILES); 082 083 // Observer event only if the ACL context is a page and the reading profile has been updated 084 return aclContext instanceof SitemapElement && profileIds.contains(RightManager.READER_PROFILE_ID) ? (SitemapElement) aclContext : null; 085 } 086}