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.core.minimize;
017
018import java.io.IOException;
019import java.util.Map;
020
021import org.apache.avalon.framework.parameters.Parameters;
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.cocoon.ProcessingException;
025import org.apache.cocoon.ResourceNotFoundException;
026import org.apache.excalibur.source.Source;
027
028import org.ametys.core.resources.SimpleResourceHandler;
029
030/**
031 * Resource handler for source map files
032 */
033public abstract class AbstractSourceMapResourceHandler extends SimpleResourceHandler
034{
035    /** SourceMapCache */
036    protected SourceMapCache _sourceMapCache;
037    
038    @Override
039    public void service(ServiceManager manager) throws ServiceException
040    {
041        super.service(manager);
042        _sourceMapCache = (SourceMapCache) manager.lookup(SourceMapCache.ROLE);
043    }
044    
045    @Override
046    public Source setup(String location, Map objectModel, Parameters par, boolean readForDownload) throws IOException, ProcessingException
047    {
048        // 1. if the source map file exists, use it
049        Source source = _getExistingSource(location);
050        if (source == null)
051        {
052            source = _getAlternateSource(location, objectModel);
053
054            if (source == null)
055            {
056                throw new ResourceNotFoundException("Resource not found for URI : " + location);
057            }
058        }
059        
060        _source = source;
061        return source;
062    }
063
064    /**
065     * Find the source at an alternative location
066     * @param location The original location
067     * @param objectModel The object model
068     * @return The source, or null if not found
069     * @throws IOException  If an error occurred
070     * @throws ProcessingException  If an error occurred
071     */
072    protected abstract Source _getAlternateSource(String location, Map objectModel) throws ProcessingException, IOException;
073    
074    /**
075     * Try to retrieve an existing source
076     * @param location The location
077     * @return The source, or null if not found
078     */
079    protected Source _getExistingSource(String location) 
080    {
081        Source source = null;
082        try 
083        {
084            source = _resolver.resolveURI(location);
085        } 
086        catch (IOException e) 
087        {
088            // Nothing
089        }
090        
091        if (source == null || !source.exists())
092        {
093            _resolver.release(source);
094            return null;
095        }
096        
097        return source;
098    }
099}