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.css;
017
018import java.io.ByteArrayOutputStream;
019import java.io.IOException;
020import java.io.InputStream;
021import java.net.URISyntaxException;
022import java.nio.charset.StandardCharsets;
023import java.util.Map;
024
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027import org.apache.cocoon.ProcessingException;
028import org.apache.cocoon.environment.ObjectModelHelper;
029import org.apache.commons.io.IOUtils;
030import org.apache.commons.lang3.StringUtils;
031import org.apache.excalibur.source.Source;
032
033import org.ametys.core.minimize.AbstractSourceMapResourceHandler;
034import org.ametys.plugins.core.ui.resources.css.JSASSResourceURIExtensionPoint;
035
036/**
037 * Resource handler for source map files
038 */
039public class CSSSourceMapResourceHandler extends AbstractSourceMapResourceHandler
040{
041    /** CSS minimize manager */
042    protected MinimizeCSSManager _cssMinimizeManager;
043    
044    private JSASSResourceURIExtensionPoint _jsassResourceURIExtensionPoint;
045    
046    @Override
047    public void service(ServiceManager manager) throws ServiceException
048    {
049        super.service(manager);
050        
051        _cssMinimizeManager = (MinimizeCSSManager) manager.lookup(MinimizeCSSManager.ROLE);
052        _jsassResourceURIExtensionPoint = (JSASSResourceURIExtensionPoint) manager.lookup(JSASSResourceURIExtensionPoint.ROLE);
053    }
054
055    @Override
056    protected Source _getAlternateSource(String location, Map objectModel) throws IOException
057    {
058        Source source = null;
059        
060        // 1. check if the source map is in cache
061        try
062        {
063            String resolvedLocation = _jsassResourceURIExtensionPoint.resolve(location);
064            source = _sourceMapCache.get(resolvedLocation != null ? resolvedLocation : location);
065        }
066        catch (URISyntaxException e)
067        {
068            getLogger().error("Unable to resolve the uri because the syntax '" + location + "' is not supported ", e);
069        }
070
071        if (source == null)
072        {
073            // 2. generate the source map and store it in cache
074            source = _generateCssSourceMap(objectModel, location);
075        }
076        
077        return source;
078    }
079
080    private Source _generateCssSourceMap(Map objectModel, String location) throws IOException
081    {
082        String originalUri = "/" + StringUtils.removeEnd(ObjectModelHelper.getRequest(objectModel).getSitemapURI(), ".map");
083        
084        Source sourceResolved = _resolver.resolveURI("cocoon:" + originalUri);
085        if (sourceResolved != null && sourceResolved.exists())
086        {
087            try (InputStream inputStream = sourceResolved.getInputStream())
088            {
089                ByteArrayOutputStream baos = new ByteArrayOutputStream();
090                IOUtils.copy(inputStream, baos);
091                
092                // Stores the source of the sourceMap in the cache
093                String sourceMapKey = _jsassResourceURIExtensionPoint.resolve(location);
094                _cssMinimizeManager.minimizeCss(baos.toString(StandardCharsets.UTF_8.toString()), originalUri, sourceMapKey, sourceResolved.getLastModified());
095                
096                return _sourceMapCache.get(sourceMapKey);
097            }
098            catch (ProcessingException | URISyntaxException e)
099            {
100                getLogger().error("Unable to compile CSS file", e);
101            }
102        }
103        else
104        {
105            _resolver.release(sourceResolved);
106        }
107        
108        return null;
109    }
110}