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 */
016
017package org.ametys.web.repository;
018
019import java.util.Map;
020
021import org.apache.avalon.framework.parameters.Parameters;
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.avalon.framework.thread.ThreadSafe;
025import org.apache.cocoon.acting.ServiceableAction;
026import org.apache.cocoon.environment.ObjectModelHelper;
027import org.apache.cocoon.environment.Redirector;
028import org.apache.cocoon.environment.Request;
029import org.apache.cocoon.environment.Response;
030import org.apache.cocoon.environment.SourceResolver;
031
032import org.ametys.web.cache.PageHelper;
033import org.ametys.web.cache.monitoring.Constants;
034import org.ametys.web.cache.monitoring.process.access.impl.PageResourceAccess;
035import org.ametys.web.repository.page.Page;
036
037/**
038 * Set the "X-Ametys-Cacheable" header if the page is cacheable.
039 */
040public class IsPageCacheableAction extends ServiceableAction implements ThreadSafe
041{
042    
043    private PageHelper _pageCacheHelper;
044    
045    @Override
046    public void service(ServiceManager serviceManager) throws ServiceException
047    {
048        super.service(serviceManager);
049        _pageCacheHelper = (PageHelper) serviceManager.lookup(PageHelper.ROLE);
050    }
051    
052    @Override
053    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
054    {
055        Request request = ObjectModelHelper.getRequest(objectModel);
056        Page page = (Page) request.getAttribute(Page.class.getName());
057        
058        boolean isCacheable = _pageCacheHelper.isCacheable(page);
059        if (isCacheable)
060        {
061            Response response = ObjectModelHelper.getResponse(objectModel);
062            response.addHeader("X-Ametys-Cacheable", "true");
063        }
064        
065        // Page access monitoring.
066        String uuid = request.getHeader("X-Ametys-FO-UUID");
067        PageResourceAccess pageAccess = new PageResourceAccess(uuid, page.getId(), "/" + page.getSiteName() + "/" + page.getSitemapName() + "/" + page.getPathInSitemap() + ".html");
068        pageAccess.setCacheable(isCacheable);
069        request.setAttribute(Constants.REQUEST_ATTRIBUTE_PAGEACCESS, pageAccess);
070        
071        return EMPTY_MAP;
072    }
073    
074}