001/*
002 *  Copyright 2016 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.cocoon;
017
018import java.io.IOException;
019import java.io.Serializable;
020import java.util.Map;
021
022import org.apache.avalon.framework.parameters.Parameters;
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.cocoon.ProcessingException;
026import org.apache.cocoon.caching.CacheableProcessingComponent;
027import org.apache.cocoon.environment.ObjectModelHelper;
028import org.apache.cocoon.reading.ServiceableReader;
029import org.apache.excalibur.source.Source;
030import org.apache.excalibur.source.SourceResolver;
031import org.apache.excalibur.source.SourceValidity;
032import org.xml.sax.SAXException;
033
034/**
035 * Default resource reader, that handle different resources type using the ResourcesExtensionPoint.
036 */
037public class ResourceReader extends ServiceableReader implements CacheableProcessingComponent
038{
039    /** last modified parameter name for resources parameters */
040    public static final String LAST_MODIFIED = "lastModified";
041    
042    private ResourceHandlerExtensionPoint _resourcesHandlerEP;
043    private ResourceHandler _resourceHandler;
044
045    private SourceResolver _resolver;
046
047    private Source _source;
048    
049    @Override
050    public void service(ServiceManager sManager) throws ServiceException
051    {
052        super.service(sManager);
053        _resourcesHandlerEP = (ResourceHandlerExtensionPoint) sManager.lookup(ResourceHandlerExtensionPoint.ROLE);
054        _resolver = (SourceResolver) sManager.lookup(SourceResolver.ROLE);
055    }
056    
057    @Override
058    public void setup(org.apache.cocoon.environment.SourceResolver res, Map objModel, String src, Parameters par) throws ProcessingException, SAXException, IOException
059    {
060        super.setup(res, objModel, src, par);
061        
062        _resourceHandler = _resourcesHandlerEP.getResourceHandler(source);
063        
064        if (_resourceHandler == null)
065        {
066            throw new ProcessingException("No resource handler found for current resource '" + this.source + "'");
067        }
068        
069        _source = _resourceHandler.setup(source, parameters);
070        
071        // Minimizer does not receive the real lastModified throught sitemap source 
072        @SuppressWarnings("unchecked")
073        Map<String, Object> params = (Map<String, Object>) objectModel.get(ObjectModelHelper.PARENT_CONTEXT);
074        if (params != null)
075        {
076            params.put(LAST_MODIFIED, getLastModified());
077        }
078    }
079
080    @Override
081    public void generate() throws IOException, ProcessingException
082    {
083        _resourceHandler.generateResource(_source, out, parameters);
084        out.flush();
085    }
086
087    @Override
088    public Serializable getKey()
089    {
090        return _resourceHandler != null ? _resourceHandler.getKey(_source, parameters) : null;
091    }
092    
093    @Override
094    public SourceValidity getValidity()
095    {
096        return _resourceHandler != null ? _resourceHandler.getValidity(_source, parameters) : null;
097    }
098
099    @Override
100    public void recycle()
101    {
102        super.recycle();
103        _resolver.release(_source);
104        _resourceHandler = null;
105    }
106    
107    @Override
108    public String getMimeType()
109    {
110        return _resourceHandler != null ? _resourceHandler.getMimeType(_source, parameters) : super.getMimeType();
111    }
112    
113    @Override
114    public long getLastModified()
115    {
116        return _resourceHandler != null ? _resourceHandler.getLastModified(_source, parameters) : super.getLastModified();
117    }
118}