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    /** Name of service's parameter holding the display user links option */
033    public static final String DISPLAY_USER_LINKS_PARAMETER = "displayUserLinks";
034    /** Name of service's parameter holding the configurable option */
035    public static final String CONFIGURABLE_LINKS_PARAMETER = "configurable";
036    
037    private DirectoryHelper _directoryHelper;
038    
039    @Override
040    public void service(ServiceManager smanager) throws ServiceException
041    {
042        super.service(smanager);
043        _directoryHelper = (DirectoryHelper) smanager.lookup(DirectoryHelper.ROLE);
044    }
045    
046    @Override
047    public boolean isCacheable(Page currentPage, ZoneItem zoneItem)
048    {
049        ModelAwareDataHolder serviceParameters = zoneItem.getServiceParameters();
050        boolean configurable = serviceParameters.getValue(CONFIGURABLE_LINKS_PARAMETER, false, false);
051        boolean displayUserLinks = serviceParameters.getValue(DISPLAY_USER_LINKS_PARAMETER, false, false);
052        
053        if (configurable || displayUserLinks)
054        {
055            return false;
056        }
057        
058        if (currentPage != null)
059        {
060            // Get the selected themes ids from the service parameters
061            String[] themes = serviceParameters.hasValue("themes") ? serviceParameters.getValue("themes") : new String[0];
062            
063            // Get the site name and the language from the page
064            String siteName = currentPage.getSiteName();
065            String language = currentPage.getSitemapName();
066            
067            return !_directoryHelper.hasRestrictions(siteName, language, Arrays.asList(themes)) && !_directoryHelper.hasInternalUrl(siteName, language, Arrays.asList(themes));
068        }
069
070        // Current page is null
071        return true;
072    }
073}