001/*
002 *  Copyright 2020 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.core.resources;
017
018import java.util.HashSet;
019import java.util.Set;
020
021import org.apache.avalon.framework.configuration.Configuration;
022import org.apache.avalon.framework.configuration.ConfigurationException;
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.commons.lang3.StringUtils;
026import org.apache.commons.lang3.Strings;
027import org.apache.excalibur.source.SourceResolver;
028
029import org.ametys.core.resources.ImageResourceHandler.SizedSource;
030
031/**
032 * {@link ResourceHandlerProvider} for images.
033 */
034public class ImageResourceHandlerProvider extends AbstractSimpleResourceHandlerProvider
035{
036    /** The source resolver */
037    protected SourceResolver _resolver;
038    
039    /** The supported mime types */
040    protected Set<String> _supportedMimeTypes;
041    
042    @Override
043    public void service(ServiceManager manager) throws ServiceException
044    {
045        super.service(manager);
046        _resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE);
047    }
048    
049    @Override
050    public void configure(Configuration configuration) throws ConfigurationException
051    {
052        super.configure(configuration);
053        
054        _supportedMimeTypes = new HashSet<>();
055        
056        Configuration[] mimeTypes = configuration.getChild("mimeTypes").getChildren("mimeType");
057        for (Configuration mimeType : mimeTypes)
058        {
059            _supportedMimeTypes.add(StringUtils.lowerCase(mimeType.getValue()));
060        }
061    }
062    
063    @Override
064    public ResourceHandler getResourceHandler(String location) throws Exception
065    {
066        ResourceHandler resourceHandler = super.getResourceHandler(location);
067        if (resourceHandler != null)
068        {
069            return resourceHandler;
070        }
071        
072        SizedSource sizedSource = ImageResourceHandler._resolveSource(location, _resolver);
073        if (sizedSource != null)
074        {
075            if (_supportedMimeTypes.stream()
076                    .anyMatch(mimeType -> Strings.CI.equals(sizedSource.source().getMimeType(), mimeType)))
077            {
078                return new ImageResourceHandler(sizedSource);
079            }
080            else
081            {
082                _resolver.release(sizedSource.source());
083            }
084        }
085                
086        return null;
087    }
088    
089    @Override
090    protected ResourceHandler createResourceHandler(String source)
091    {
092        return new ImageResourceHandler();
093    }
094}