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