001/*
002 *  Copyright 2020 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.ugc.cachepolicy;
017
018import java.util.Arrays;
019import java.util.Collections;
020import java.util.HashSet;
021import java.util.List;
022import java.util.Set;
023
024import org.apache.avalon.framework.logger.AbstractLogEnabled;
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027import org.apache.avalon.framework.service.Serviceable;
028
029import org.ametys.cms.repository.ModifiableDefaultContent;
030import org.ametys.core.observation.Event;
031import org.ametys.plugins.ugc.page.UGCPageHandler;
032import org.ametys.web.ObservationConstants;
033import org.ametys.web.cache.pageelement.PageElementCachePolicy;
034import org.ametys.web.inputdata.SitemapInputData;
035import org.ametys.web.repository.site.Site;
036import org.ametys.web.repository.sitemap.Sitemap;
037
038/**
039 * Cache policy for the sitemap, handling UGC virtual pages linked with contents.
040 * Used for the Sitemap InputData as well as for the Sitemap service, even if the page element cache is not the same.
041 */
042public class UGCVirtualPagesCachePolicy extends AbstractLogEnabled implements Serviceable, PageElementCachePolicy
043{
044    
045    private static final Set<String> _UGC_PE_TYPES = new HashSet<>();
046    static
047    {
048        // Used for the Sitemap InputData as well as for the Sitemap service.
049        _UGC_PE_TYPES.add(SitemapInputData.class.getName());
050        _UGC_PE_TYPES.add("SERVICE:org.ametys.web.service.SitemapService");
051    }
052    
053    /** The UGC page handler */
054    protected UGCPageHandler _UGCPageHandler;
055    
056    @Override
057    public void service(ServiceManager manager) throws ServiceException
058    {
059        _UGCPageHandler = (UGCPageHandler) manager.lookup(UGCPageHandler.ROLE);
060    }
061    
062    @Override
063    public Set<String> getPageElementTypes()
064    {
065        return _UGC_PE_TYPES;
066    }
067    
068    @Override
069    public final PolicyResult shouldClearCache(String workspace, Site site, String pageElementType, Event event)
070    {
071        String id = event.getId();
072        
073        if (_getRemovingCacheEventIds(workspace).contains(id))
074        {
075            Object object = event.getArguments().get(org.ametys.cms.ObservationConstants.ARGS_CONTENT);
076            
077            // The target must be a UGC content and the site must possess a UGC root
078            // in one of its sitemaps to be invalidated.
079            if (object instanceof ModifiableDefaultContent)
080            {
081                ModifiableDefaultContent content = (ModifiableDefaultContent) object;
082                try
083                {
084                    for (String contentTypeId : content.getTypes())
085                    {
086                        for (Sitemap sitemap : site.getSitemaps())
087                        {
088                            if (_UGCPageHandler.getUGCRootPage(site.getName(), sitemap.getSitemapName(), contentTypeId) != null)
089                            {
090                                return PolicyResult.REMOVE;
091                            }
092                            
093                        }
094                    }
095                }
096                catch (Exception e)
097                {
098                    getLogger().error("An error occurred with an event on content " + content.getId(), e);
099                    return PolicyResult.KEEP;
100                }
101            }
102        }
103        
104        return PolicyResult.KEEP;
105    }
106    
107    @Override
108    public final PolicyResult shouldClearCache(String workspace, Site site, String pageElementType, String elementId, Event event)
109    {
110        // Never called because the first-level method never returns NEED_INFORMATION.
111        throw new UnsupportedOperationException("Should never be called.");
112    }
113    
114    /**
115     * Returns all event ids for which the cache should be removed.
116     * @param workspace the current JCR workspace.
117     * @return all event ids for which the cache should be removed.
118     */
119    protected List<String> _getRemovingCacheEventIds(String workspace)
120    {
121        if ("default".equals(workspace))
122        {
123            return Arrays.asList(org.ametys.cms.ObservationConstants.EVENT_CONTENT_ADDED,
124                                 org.ametys.cms.ObservationConstants.EVENT_CONTENT_MODIFIED,
125                                 org.ametys.cms.ObservationConstants.EVENT_CONTENT_DELETED);
126        }
127        else if ("live".equals(workspace))
128        {
129            return Arrays.asList(org.ametys.cms.ObservationConstants.EVENT_CONTENT_ADDED,
130                                 org.ametys.cms.ObservationConstants.EVENT_CONTENT_VALIDATED,
131                                 org.ametys.cms.ObservationConstants.EVENT_CONTENT_DELETED,
132                                 ObservationConstants.EVENT_CONTENT_UNPUBLISHED);
133        }
134        
135        return Collections.emptyList();
136    }
137}