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