001/* 002 * Copyright 2018 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.minimize; 017 018import java.io.IOException; 019import java.io.OutputStream; 020import java.util.Map; 021 022import org.apache.avalon.framework.parameters.Parameters; 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025import org.apache.cocoon.ProcessingException; 026import org.apache.cocoon.ResourceNotFoundException; 027import org.apache.commons.io.IOUtils; 028import org.apache.excalibur.source.Source; 029 030import org.ametys.core.resources.AbstractResourceHandler; 031import org.ametys.plugins.core.ui.minimize.HashCache; 032 033/** 034 * Resource handler for source map files 035 */ 036public abstract class AbstractSourceMapResourceHandler extends AbstractResourceHandler 037{ 038 /** SourceMapCache */ 039 protected SourceMapCache _sourceMapCache; 040 041 /** Hash Cache for Hashed resources */ 042 protected HashCache _hashCache; 043 044 @Override 045 public void service(ServiceManager manager) throws ServiceException 046 { 047 super.service(manager); 048 049 _hashCache = (HashCache) manager.lookup(HashCache.ROLE); 050 _sourceMapCache = (SourceMapCache) manager.lookup(SourceMapCache.ROLE); 051 } 052 053 @Override 054 public String getMimeType(Source source, Parameters par) 055 { 056 return source.getMimeType(); 057 } 058 059 @Override 060 public int getPriority() 061 { 062 return MIN_PRIORITY + 500; 063 } 064 065 @Override 066 public Source setup(String location, Map objectModel, Parameters par, Map<String, Object> additionalParameters) throws IOException, ProcessingException 067 { 068 // 1. if the source map file exists, use it 069 Source source = _getExistingSource(location); 070 if (source == null) 071 { 072 source = _getAlternateSource(location, objectModel); 073 074 if (source == null) 075 { 076 throw new ResourceNotFoundException("Resource not found for URI : " + location); 077 } 078 } 079 080 return source; 081 } 082 083 /** 084 * Find the source at an alternative location 085 * @param location The original location 086 * @param objectModel The object model 087 * @return The source, or null if not found 088 * @throws IOException If an error occurred 089 * @throws ProcessingException If an error occurred 090 */ 091 protected abstract Source _getAlternateSource(String location, Map objectModel) throws ProcessingException, IOException; 092 093 /** 094 * Try to retrieve an existing source 095 * @param location The location 096 * @return The source, or null if not found 097 */ 098 protected Source _getExistingSource(String location) 099 { 100 Source source = null; 101 try 102 { 103 source = _resolver.resolveURI(location); 104 } 105 catch (IOException e) 106 { 107 // Nothing 108 } 109 110 return source != null && source.exists() ? source : null; 111 } 112 113 public void generateResource(Source source, OutputStream out, Map objectModel, Parameters parameters, Map<String, Object> additionalParameters) throws IOException, ProcessingException 114 { 115 IOUtils.copy(source.getInputStream(), out); 116 } 117 118}