001/*
002 *  Copyright 2010 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.web.cache;
017
018import java.util.Collection;
019import java.util.Map;
020
021import javax.jcr.Session;
022
023import org.ametys.core.observation.Event;
024import org.ametys.core.observation.Observer;
025import org.ametys.core.right.RightManager;
026import org.ametys.web.ObservationConstants;
027import org.ametys.web.repository.page.SitemapElement;
028import org.ametys.web.repository.site.Site;
029import org.ametys.web.repository.sitemap.Sitemap;
030
031/**
032 * {@link Observer} for observing page move in order to 
033 * invalidate cache on front-office.
034 */
035public class InvalidateCacheOnRobotsChangeObserver extends AbstractSiteCacheObserver
036{
037    @Override
038    public boolean supports(Event event)
039    {
040        if (event.getId().equals(ObservationConstants.EVENT_ROBOTS_CHANGED))
041        {
042            return true;
043        }
044        else if (event.getId().equals(org.ametys.core.ObservationConstants.EVENT_ACL_UPDATED))
045        {
046            Map<String, Object> args = event.getArguments();
047            Object aclContext = args.get(org.ametys.core.ObservationConstants.ARGS_ACL_CONTEXT);
048            @SuppressWarnings("unchecked")
049            Collection<String> profileIds = (Collection<String>) args.get(org.ametys.core.ObservationConstants.ARGS_ACL_PROFILES);
050            
051            return aclContext instanceof SitemapElement && profileIds.contains(RightManager.READER_PROFILE_ID);
052        }
053        
054        return false;
055    }
056    
057    @Override
058    protected Site _getSite(Event event)
059    {
060        Map<String, Object> args = event.getArguments();
061        
062        SitemapElement page = (SitemapElement) args.get(ObservationConstants.ARGS_SITEMAP_ELEMENT);
063        if (page != null)
064        {
065            return page.getSite();
066        }
067
068        Sitemap sitemap = (Sitemap) args.get(ObservationConstants.ARGS_SITEMAP);
069        if (sitemap != null)
070        {
071            return sitemap.getSite();
072        }
073        
074        SitemapElement aclContext = (SitemapElement) args.get(org.ametys.core.ObservationConstants.ARGS_ACL_CONTEXT);
075        if (aclContext != null)
076        {
077            return aclContext.getSite();
078        }
079        
080        return null;
081    }
082    
083    @Override
084    protected void _internalObserve(Event event, Site site, Session liveSession) throws Exception
085    {
086        if (getLogger().isInfoEnabled())
087        {
088            getLogger().info("Robots changed: " + event + ", invalidating cache");
089        }
090
091        CacheHelper.testWS("/_invalidate-page/" + site.getName() + "/robots.txt", getLogger());
092        CacheHelper.testWS("/_invalidate-page/" + site.getName() + "/sitemap.xml", getLogger());
093    }
094}