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.plugins.odfweb.cachepolicy; 017 018import java.util.Arrays; 019import java.util.Collections; 020import java.util.HashSet; 021import java.util.List; 022import java.util.Map; 023import java.util.Set; 024 025import org.apache.avalon.framework.logger.AbstractLogEnabled; 026import org.apache.avalon.framework.service.ServiceException; 027import org.apache.avalon.framework.service.ServiceManager; 028import org.apache.avalon.framework.service.Serviceable; 029 030import org.ametys.cms.ObservationConstants; 031import org.ametys.core.observation.Event; 032import org.ametys.odf.ProgramItem; 033import org.ametys.odf.orgunit.OrgUnit; 034import org.ametys.odf.person.Person; 035import org.ametys.plugins.odfweb.repository.OdfPageHandler; 036import org.ametys.web.cache.pageelement.PageElementCachePolicy; 037import org.ametys.web.inputdata.SitemapInputData; 038import org.ametys.web.repository.site.Site; 039 040/** 041 * Cache policy for the sitemap, handling ODF virtual pages linked with contents. 042 * Used for the Sitemap InputData as well as for the Sitemap service, even if the page element cache is not the same. 043 */ 044public class OdfVirtualPagesCachePolicy extends AbstractLogEnabled implements Serviceable, PageElementCachePolicy 045{ 046 private static final Set<String> _ODF_PE_TYPES = new HashSet<>(); 047 static 048 { 049 // Used for the Sitemap InputData as well as for the Sitemap service. 050 _ODF_PE_TYPES.add(SitemapInputData.class.getName()); 051 _ODF_PE_TYPES.add("SERVICE:org.ametys.web.service.SitemapService"); 052 _ODF_PE_TYPES.add("CONTENT"); 053 } 054 055 /** The ODF root page handler. */ 056 protected OdfPageHandler _odfRootPageHandler; 057 058 @Override 059 public void service(ServiceManager manager) throws ServiceException 060 { 061 _odfRootPageHandler = (OdfPageHandler) manager.lookup(OdfPageHandler.ROLE); 062 } 063 064 @Override 065 public Set<String> getPageElementTypes() 066 { 067 return _ODF_PE_TYPES; 068 } 069 070 @Override 071 public final PolicyResult shouldClearCache(String workspace, Site site, String pageElementType, Event event) 072 { 073 String id = event.getId(); 074 075 if (_getRemovingCacheEventIds(workspace).contains(id)) 076 { 077 Map<String, Object> args = event.getArguments(); 078 079 if (args.containsKey(org.ametys.cms.ObservationConstants.ARGS_CONTENT)) 080 { 081 Object object = args.get(org.ametys.cms.ObservationConstants.ARGS_CONTENT); 082 if (object instanceof ProgramItem || object instanceof OrgUnit || object instanceof Person) 083 { 084 if (_odfRootPageHandler.hasOdfRootPage(site)) 085 { 086 return PolicyResult.REMOVE; 087 } 088 } 089 } 090 } 091 092 return PolicyResult.KEEP; 093 } 094 095 @Override 096 public final PolicyResult shouldClearCache(String workspace, Site site, String pageElementType, String elementId, Event event) 097 { 098 // Never called because the first-level method never returns NEED_INFORMATION. 099 throw new UnsupportedOperationException("Should never be called."); 100 } 101 102 /** 103 * Returns all event ids for which the cache should be removed. 104 * @param workspace the current JCR workspace. 105 * @return all event ids for which the cache should be removed. 106 */ 107 protected List<String> _getRemovingCacheEventIds(String workspace) 108 { 109 if ("default".equals(workspace)) 110 { 111 return Arrays.asList(ObservationConstants.EVENT_CONTENT_ADDED, 112 ObservationConstants.EVENT_CONTENT_MODIFIED, 113 ObservationConstants.EVENT_CONTENT_DELETED); 114 } 115 else if ("live".equals(workspace)) 116 { 117 return Arrays.asList(ObservationConstants.EVENT_CONTENT_ADDED, 118 ObservationConstants.EVENT_CONTENT_VALIDATED, 119 ObservationConstants.EVENT_CONTENT_DELETED, 120 org.ametys.web.ObservationConstants.EVENT_CONTENT_UNPUBLISHED); 121 } 122 123 return Collections.emptyList(); 124 } 125}