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.blog.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; 029import org.apache.commons.lang3.ArrayUtils; 030 031import org.ametys.cms.ObservationConstants; 032import org.ametys.cms.repository.Content; 033import org.ametys.core.observation.Event; 034import org.ametys.plugins.blog.BlogConstants; 035import org.ametys.plugins.blog.BlogObservationConstants; 036import org.ametys.plugins.blog.BlogPageHandler; 037import org.ametys.web.cache.pageelement.PageElementCachePolicy; 038import org.ametys.web.inputdata.SitemapInputData; 039import org.ametys.web.repository.site.Site; 040 041/** 042 * Cache policy for the sitemap, handling blog virtual pages linked with contents. 043 * Used for the Sitemap InputData as well as the Sitemap service, even if the page element cache is not the same. 044 */ 045public class BlogVirtualPagesCachePolicy extends AbstractLogEnabled implements Serviceable, PageElementCachePolicy 046{ 047 048 private static final Set<String> _TYPES = new HashSet<>(); 049 static 050 { 051 // Used for the Sitemap InputData, the Sitemap service, and other post-based services. 052 _TYPES.add(SitemapInputData.class.getName()); 053 _TYPES.add("SERVICE:org.ametys.web.service.SitemapService"); 054 _TYPES.add("SERVICE:org.ametys.blog.service.PostsService"); 055 _TYPES.add("SERVICE:org.ametys.blog.service.ArchivesService"); 056 _TYPES.add("SERVICE:org.ametys.blog.service.PostsByTagsService"); 057 _TYPES.add("SERVICE:org.ametys.plugins.blog.service.FilteredPostsService"); 058 } 059 060 /** The blog page handler. */ 061 protected BlogPageHandler _blogPageHandler; 062 063 @Override 064 public void service(ServiceManager manager) throws ServiceException 065 { 066 _blogPageHandler = (BlogPageHandler) manager.lookup(BlogPageHandler.ROLE); 067 } 068 069 @Override 070 public Set<String> getPageElementTypes() 071 { 072 return _TYPES; 073 } 074 075 @Override 076 public final PolicyResult shouldClearCache(String workspace, Site site, String pageElementType, Event event) 077 { 078 String id = event.getId(); 079 080 if (_getRemovingCacheEventIds(workspace).contains(id)) 081 { 082 Map<String, Object> args = event.getArguments(); 083 084 Content content = (Content) args.get(ObservationConstants.ARGS_CONTENT); 085 086 if (content != null) 087 { 088 String[] cTypes = content.getTypes(); 089 090 if (ArrayUtils.contains(cTypes, BlogConstants.POST_CONTENT_TYPE)) 091 { 092 if (_blogPageHandler.hasBlogRootPage(site)) 093 { 094 return PolicyResult.REMOVE; 095 } 096 } 097 } 098 } 099 100 return PolicyResult.KEEP; 101 } 102 103 @Override 104 public final PolicyResult shouldClearCache(String workspace, Site site, String pageElementType, String elementId, Event event) 105 { 106 // Never called because the first-level method never returns NEED_INFORMATION. 107 throw new UnsupportedOperationException("Should never be called."); 108 } 109 110 /** 111 * Returns all event ids for which the cache should be removed. 112 * @param workspace the current JCR workspace. 113 * @return all event ids for which the cache should be removed. 114 */ 115 protected List<String> _getRemovingCacheEventIds(String workspace) 116 { 117 if ("default".equals(workspace)) 118 { 119 return Arrays.asList(ObservationConstants.EVENT_CONTENT_ADDED, 120 ObservationConstants.EVENT_CONTENT_MODIFIED, 121 ObservationConstants.EVENT_CONTENT_DELETING, 122 BlogObservationConstants.EVENT_FUTURE_CONTENT_VISIBLE); 123 } 124 else if ("live".equals(workspace)) 125 { 126 return Arrays.asList(ObservationConstants.EVENT_CONTENT_ADDED, 127 ObservationConstants.EVENT_CONTENT_VALIDATED, 128 ObservationConstants.EVENT_CONTENT_DELETING, 129 ObservationConstants.EVENT_CONTENT_UNTAG_LIVE, 130 BlogObservationConstants.EVENT_FUTURE_CONTENT_VISIBLE); 131 } 132 133 return Collections.emptyList(); 134 } 135}