001/*
002 *  Copyright 2015 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.linkdirectory;
017
018import java.util.Arrays;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022
023import org.ametys.plugins.repository.data.holder.ModelAwareDataHolder;
024import org.ametys.web.repository.page.Page;
025import org.ametys.web.repository.page.ZoneItem;
026
027/**
028 *  Service for the link directory : not cacheable when dealing with links that have a limited access or internal url
029 */
030public class LinkDirectoryService extends org.ametys.web.service.StaticService
031{
032    private DirectoryHelper _directoryHelper;
033    
034    @Override
035    public void service(ServiceManager smanager) throws ServiceException
036    {
037        super.service(smanager);
038        _directoryHelper = (DirectoryHelper) smanager.lookup(DirectoryHelper.ROLE);
039    }
040    
041    @Override
042    public boolean isCacheable(Page currentPage, ZoneItem zoneItem)
043    {
044        ModelAwareDataHolder serviceParameters = zoneItem.getServiceParameters();
045        boolean configurable = serviceParameters.getValue("configurable", false, false);
046        boolean displayUserLinks = serviceParameters.getValue("displayUserLinks", false, false);
047        
048        if (configurable || displayUserLinks)
049        {
050            return false;
051        }
052        
053        if (currentPage != null)
054        {
055            // Get the selected themes ids from the service parameters
056            String[] themes = serviceParameters.hasValue("themes") ? serviceParameters.getValue("themes") : new String[0];
057            
058            // Get the site name and the language from the page
059            String siteName = currentPage.getSiteName();
060            String language = currentPage.getSitemapName();
061            
062            return !_directoryHelper.hasRestrictions(siteName, language, Arrays.asList(themes)) && !_directoryHelper.hasInternalUrl(siteName, language, Arrays.asList(themes));
063        }
064
065        // Current page is null
066        return true;
067    }
068}