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.HashSet; 021import java.util.Map; 022import java.util.Set; 023 024import org.apache.avalon.framework.configuration.Configurable; 025import org.apache.avalon.framework.configuration.Configuration; 026import org.apache.avalon.framework.configuration.ConfigurationException; 027import org.apache.avalon.framework.context.Context; 028import org.apache.avalon.framework.context.ContextException; 029import org.apache.avalon.framework.context.Contextualizable; 030import org.apache.avalon.framework.parameters.Parameters; 031import org.apache.avalon.framework.service.ServiceException; 032import org.apache.avalon.framework.service.ServiceManager; 033import org.apache.avalon.framework.service.Serviceable; 034import org.apache.cocoon.Constants; 035import org.apache.cocoon.ProcessingException; 036import org.apache.cocoon.ResourceNotFoundException; 037import org.apache.commons.lang3.StringUtils; 038import org.apache.excalibur.source.Source; 039import org.apache.excalibur.source.SourceResolver; 040import org.apache.excalibur.source.SourceValidity; 041 042import org.ametys.runtime.plugin.component.AbstractLogEnabled; 043 044/** 045 * Abstract resource handler that supports the "suffixes" configuration 046 */ 047public abstract class AbstractResourceHandler extends AbstractLogEnabled implements ResourceHandler, Serviceable, Configurable, Contextualizable 048{ 049 /** The supported suffixes */ 050 protected Set<String> _supportedSuffixes; 051 052 /** The source resolver */ 053 protected SourceResolver _resolver; 054 055 /** The context */ 056 protected Context _context; 057 /** The cocoon context */ 058 protected org.apache.cocoon.environment.Context _cocoonContext; 059 060 @Override 061 public void service(ServiceManager manager) throws ServiceException 062 { 063 _resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE); 064 } 065 066 @Override 067 public void contextualize(Context context) throws ContextException 068 { 069 _context = context; 070 _cocoonContext = (org.apache.cocoon.environment.Context) context.get(Constants.CONTEXT_ENVIRONMENT_CONTEXT); 071 } 072 073 @Override 074 public void configure(Configuration configuration) throws ConfigurationException 075 { 076 _supportedSuffixes = new HashSet<>(); 077 078 Configuration[] suffixConf = configuration.getChild("suffixes").getChildren("suffix"); 079 for (Configuration suffix : suffixConf) 080 { 081 _supportedSuffixes.add(StringUtils.lowerCase(suffix.getValue())); 082 } 083 } 084 085 @Override 086 public Source setup(String location, Map objectModel, Parameters par, Map<String, Object> additionalParameters) throws IOException, ProcessingException 087 { 088 Source src = _resolver.resolveURI(location); 089 090 if (!src.exists()) 091 { 092 throw new ResourceNotFoundException("Resource not found for URI : " + src.getURI()); 093 } 094 095 return src; 096 } 097 098 @Override 099 public boolean isSupported(String source) 100 { 101 String lcSource = StringUtils.lowerCase(source); 102 103 for (String supportedSuffix : _supportedSuffixes) 104 { 105 if (StringUtils.endsWith(lcSource, supportedSuffix)) 106 { 107 return true; 108 } 109 } 110 111 return false; 112 } 113 114 @Override 115 public int getPriority() 116 { 117 return MIN_PRIORITY + 500; 118 } 119 120 @Override 121 public String getMimeType(Source source, Parameters parameters) 122 { 123 if (_cocoonContext != null) 124 { 125 final String mimeType = _cocoonContext.getMimeType(source.getURI()); 126 127 if (mimeType != null) 128 { 129 return mimeType; 130 } 131 } 132 return source.getMimeType(); 133 } 134 135 @Override 136 public Serializable getKey(Source source, Map objectModel, Parameters parameters, Map<String, Object> additionalParameters) 137 { 138 return source.getURI(); 139 } 140 141 @Override 142 public SourceValidity getValidity(Source source, Map objectModel, Parameters parameters, Map<String, Object> additionalParameters) 143 { 144 return source.getValidity(); 145 } 146 147 @Override 148 public long getSize(Source source, Parameters parameters) 149 { 150 return source.getContentLength(); 151 } 152 153 @Override 154 public long getLastModified(Source source, Parameters parameters) 155 { 156 return source.getLastModified(); 157 } 158}