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