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