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.plugins.core.ui.minimize;
017
018import java.io.IOException;
019import java.io.Serializable;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.cocoon.ProcessingException;
024import org.apache.cocoon.caching.CacheableProcessingComponent;
025import org.apache.cocoon.environment.ObjectModelHelper;
026import org.apache.cocoon.environment.Response;
027import org.apache.cocoon.reading.ServiceableReader;
028import org.apache.excalibur.source.SourceResolver;
029import org.apache.excalibur.source.SourceValidity;
030import org.apache.excalibur.source.impl.validity.NOPValidity;
031import org.xml.sax.SAXException;
032
033import org.ametys.core.minimize.SourceMapCache;
034import org.ametys.core.resources.ProxiedContextPathProvider;
035
036/**
037 * Reader for Source map of minimized files
038 */
039public abstract class AbstractMinimizeSourceMapReader extends ServiceableReader implements CacheableProcessingComponent
040{
041    /** SourceMapCache */
042    protected SourceMapCache _sourceMapCache;
043
044    /** SourceResolver */
045    protected SourceResolver _resolver;
046
047    /** Cache for hash files list */
048    protected HashCache _hashCache;
049    
050    private ProxiedContextPathProvider _proxiedContextPathProvider;
051
052    @Override
053    public void service(ServiceManager smanager) throws ServiceException
054    {
055        super.service(smanager);
056        
057        _sourceMapCache = (SourceMapCache) smanager.lookup(SourceMapCache.ROLE);
058        _resolver = (SourceResolver) smanager.lookup(SourceResolver.ROLE);
059        _hashCache = (HashCache) smanager.lookup(HashCache.ROLE);
060        _proxiedContextPathProvider = (ProxiedContextPathProvider) manager.lookup(ProxiedContextPathProvider.ROLE);
061    }
062
063    @Override
064    public Serializable getKey()
065    {
066        return source + "*" + _proxiedContextPathProvider.getContextPath();
067    }
068
069    @Override
070    public SourceValidity getValidity()
071    {
072        return new NOPValidity();
073    }
074
075    @Override
076    public long getLastModified()
077    {
078        return 0; // the smallest second-precision value
079    }
080    
081    @Override
082    public void setup(org.apache.cocoon.environment.SourceResolver res, java.util.Map obj, String src, org.apache.avalon.framework.parameters.Parameters par) throws ProcessingException, SAXException, IOException 
083    {
084        super.setup(res, obj, src, par);
085        Response response = ObjectModelHelper.getResponse(objectModel);
086        //response.setHeader("Cache-Control", "max-age=31536000, immutable"); // one year from now
087    }
088}