001/* 002 * Copyright 2020 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.util.HashSet; 019import java.util.Set; 020 021import org.apache.avalon.framework.configuration.Configurable; 022import org.apache.avalon.framework.configuration.Configuration; 023import org.apache.avalon.framework.configuration.ConfigurationException; 024import org.apache.commons.lang3.StringUtils; 025import org.apache.commons.lang3.Strings; 026 027/** 028 * Simple {@link ResourceHandlerProvider} based on source suffixes (mainly file extensions). 029 */ 030public abstract class AbstractSimpleResourceHandlerProvider extends AbstractResourceHandlerProvider implements Configurable 031{ 032 /** The supported suffixes */ 033 protected Set<String> _supportedSuffixes; 034 035 @Override 036 public void configure(Configuration configuration) throws ConfigurationException 037 { 038 _supportedSuffixes = new HashSet<>(); 039 040 Configuration[] suffixConf = configuration.getChild("suffixes").getChildren("suffix"); 041 for (Configuration suffix : suffixConf) 042 { 043 _supportedSuffixes.add(StringUtils.lowerCase(suffix.getValue())); 044 } 045 } 046 047 public ResourceHandler getResourceHandler(String source) throws Exception 048 { 049 String lcSource = StringUtils.lowerCase(source); 050 051 if (_supportedSuffixes.stream().anyMatch(suffix -> Strings.CS.endsWith(lcSource, suffix))) 052 { 053 return setup(createResourceHandler(source)); 054 } 055 056 return null; 057 } 058 059 /** 060 * Actually creates the {@link ResourceHandler}. 061 * @param source the requested resource. 062 * @return the created {@link ResourceHandler}. 063 */ 064 protected abstract ResourceHandler createResourceHandler(String source); 065}