001/*
002 *  Copyright 2018 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.resources;
017
018import java.util.Arrays;
019import java.util.List;
020import java.util.function.Function;
021import java.util.function.Predicate;
022import java.util.stream.Collectors;
023
024import org.apache.avalon.framework.configuration.Configurable;
025import org.apache.avalon.framework.configuration.Configuration;
026import org.apache.avalon.framework.configuration.ConfigurationException;
027import org.apache.commons.lang3.StringUtils;
028
029/**
030 * Configurable default implementation of {@link FrontCacheableResource}
031 */
032public class ConfigurableFrontCacheableResource implements FrontCacheableResource, Configurable
033{
034    private List<String> _notCacheableExtensions;
035    
036    @Override
037    public void configure(Configuration configuration) throws ConfigurationException
038    {
039        Function<Configuration, String> getValue = conf -> conf.getValue(null);
040        
041        _notCacheableExtensions = Arrays.stream(configuration.getChildren("not-cacheable"))
042                .map(getValue)
043                .filter(StringUtils::isNotEmpty)
044                .collect(Collectors.toList());
045    }
046    
047    @Override
048    public boolean isNotCacheable(String uri)
049    {
050        Predicate<String> notCacheableExtension = ext -> uri.endsWith(ext);
051        
052        return _notCacheableExtensions.stream()
053                .filter(notCacheableExtension)
054                .findAny()
055                .isPresent();
056    }
057    
058}