001/* 002 * Copyright 2021 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.InputStream; 020import java.io.OutputStream; 021import java.io.Serializable; 022import java.util.Map; 023 024import org.apache.avalon.framework.parameters.Parameters; 025import org.apache.avalon.framework.service.ServiceException; 026import org.apache.avalon.framework.service.ServiceManager; 027import org.apache.avalon.framework.service.Serviceable; 028import org.apache.cocoon.ProcessingException; 029import org.apache.cocoon.ResourceNotFoundException; 030import org.apache.commons.io.IOUtils; 031import org.apache.excalibur.source.Source; 032import org.apache.excalibur.source.SourceResolver; 033import org.apache.excalibur.source.SourceValidity; 034 035import org.ametys.runtime.plugin.component.AbstractLogEnabled; 036 037/** 038 * Simple common ancestor for all {@link ResourceHandler} implementations, delegating all methods to the underlying {@link Source}. 039 */ 040public class SimpleResourceHandler extends AbstractLogEnabled implements ResourceHandler, Serviceable 041{ 042 /** The source resolver */ 043 protected SourceResolver _resolver; 044 045 /** The resolved source */ 046 protected Source _source; 047 048 /** The sitemap parameters */ 049 protected Parameters _parameters; 050 051 /** The object model */ 052 protected Map _objectModel; 053 054 /** if the resource has been request for download */ 055 protected boolean _readForDownload; 056 057 /** The initially requested resource location */ 058 protected String _requestedLocation; 059 060 /** 061 * Default constructor. 062 */ 063 public SimpleResourceHandler() 064 { 065 // empty constructor 066 } 067 068 /** 069 * If the {@link Source} is already resolved by the {@link ResourceHandlerProvider}, 070 * it may provide it through the constructor to avoid resolving it again. 071 * @param source the source. 072 */ 073 public SimpleResourceHandler(Source source) 074 { 075 _source = source; 076 } 077 078 @Override 079 public void service(ServiceManager manager) throws ServiceException 080 { 081 _resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE); 082 } 083 084 @Override 085 public Source setup(String location, Map objectModel, Parameters parameters, boolean readForDownload) throws IOException, ProcessingException 086 { 087 _requestedLocation = location; 088 _objectModel = objectModel; 089 _parameters = parameters; 090 _readForDownload = readForDownload; 091 092 // If the source has already been given through constructor, no need to resolve again 093 if (_source == null) 094 { 095 _source = _resolver.resolveURI(location); 096 097 if (!_source.exists()) 098 { 099 throw new ResourceNotFoundException("Resource not found for URI : " + _source.getURI()); 100 } 101 } 102 103 return _source; 104 } 105 106 @Override 107 public void generate(OutputStream out) throws IOException, ProcessingException 108 { 109 try (InputStream is = _source.getInputStream()) 110 { 111 IOUtils.copy(is, out); 112 } 113 } 114 115 @Override 116 public Serializable getKey() 117 { 118 return _source.getURI(); 119 } 120 121 @Override 122 public SourceValidity getValidity() 123 { 124 return _source.getValidity(); 125 } 126 127 @Override 128 public long getLastModified() 129 { 130 return _source.getLastModified(); 131 } 132 133 public String getMimeType() 134 { 135 return _source.getMimeType(); 136 } 137}