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.plugins.userdirectory.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.userdirectory.UserDirectoryPageHandler;
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;
036
037/**
038 * Cache policy for the sitemap, handling user directory virtual pages linked with contents.
039 * Used for the Sitemap InputData as well as for the Sitemap service, even if the page element cache is not the same.
040 */
041public class UserDirectoryVirtualPagesCachePolicy extends AbstractLogEnabled implements Serviceable, PageElementCachePolicy
042{
043    
044    private static final Set<String> _USER_DIRECTORY_PE_TYPES = new HashSet<>();
045    static
046    {
047        // Used for the Sitemap InputData as well as for the Sitemap service.
048        _USER_DIRECTORY_PE_TYPES.add(SitemapInputData.class.getName());
049        _USER_DIRECTORY_PE_TYPES.add("SERVICE:org.ametys.web.service.SitemapService");
050    }
051    
052    /** The user directory page handler */
053    protected UserDirectoryPageHandler _uDPageHandler;
054    
055    @Override
056    public void service(ServiceManager manager) throws ServiceException
057    {
058        _uDPageHandler = (UserDirectoryPageHandler) manager.lookup(UserDirectoryPageHandler.ROLE);
059    }
060    
061    @Override
062    public Set<String> getPageElementTypes()
063    {
064        return _USER_DIRECTORY_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(org.ametys.cms.ObservationConstants.ARGS_CONTENT);
075            
076            // The target must be a user content and the site must possess a user directory 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                        if (!_uDPageHandler.getUserDirectoryRootPages(contentTypeId).isEmpty())
086                        {
087                            return PolicyResult.REMOVE;
088                        }
089                    }
090                }
091                catch (Exception e)
092                {
093                    getLogger().error("An error occurred with an event on content " + content.getId(), e);
094                    return PolicyResult.KEEP;
095                }
096            }
097        }
098        
099        return PolicyResult.KEEP;
100    }
101    
102    @Override
103    public final PolicyResult shouldClearCache(String workspace, Site site, String pageElementType, String elementId, Event event)
104    {
105        // Never called because the first-level method never returns NEED_INFORMATION.
106        throw new UnsupportedOperationException("Should never be called.");
107    }
108    
109    /**
110     * Returns all event ids for which the cache should be removed.
111     * @param workspace the current JCR workspace.
112     * @return all event ids for which the cache should be removed.
113     */
114    protected List<String> _getRemovingCacheEventIds(String workspace)
115    {
116        if ("default".equals(workspace))
117        {
118            return Arrays.asList(org.ametys.cms.ObservationConstants.EVENT_CONTENT_ADDED,
119                                 org.ametys.cms.ObservationConstants.EVENT_CONTENT_MODIFIED,
120                                 org.ametys.cms.ObservationConstants.EVENT_CONTENT_DELETED);
121        }
122        else if ("live".equals(workspace))
123        {
124            return Arrays.asList(org.ametys.cms.ObservationConstants.EVENT_CONTENT_ADDED,
125                                 org.ametys.cms.ObservationConstants.EVENT_CONTENT_VALIDATED,
126                                 org.ametys.cms.ObservationConstants.EVENT_CONTENT_DELETED,
127                                 ObservationConstants.EVENT_CONTENT_UNPUBLISHED);
128        }
129        
130        return Collections.emptyList();
131    }
132}