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