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 
042    /** CSS minimize manager */
043    protected MinimizeCSSManager _cssMinimizeManager;
044    
045    private JSASSResourceURIExtensionPoint _jsassResourceURIExtensionPoint;
046    
047    @Override
048    public void service(ServiceManager manager) throws ServiceException
049    {
050        super.service(manager);
051        
052        _cssMinimizeManager = (MinimizeCSSManager) manager.lookup(MinimizeCSSManager.ROLE);
053        _jsassResourceURIExtensionPoint = (JSASSResourceURIExtensionPoint) manager.lookup(JSASSResourceURIExtensionPoint.ROLE);
054    }
055
056
057    @Override
058    protected Source _getAlternateSource(String location, Map objectModel) throws IOException
059    {
060        Source source = null;
061        // 1. check if the source map is in cache
062        try
063        {
064            String resolvedLocation = _jsassResourceURIExtensionPoint.resolve(location);
065            source = _sourceMapCache.get(resolvedLocation != null ? resolvedLocation : location);
066        }
067        catch (URISyntaxException e)
068        {
069            getLogger().error("Unable to resolve the uri because the syntax '" + location + "' is not supported ", e);
070        }
071
072        if (source == null)
073        {
074            // 2. generate the source map and store it in cache
075            source = _generateCssSourceMap(objectModel, location);
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                // Stores the source of the sourceMap in the cache
092                String sourceMapKey = _jsassResourceURIExtensionPoint.resolve(location);
093                _cssMinimizeManager.minimizeCss(baos.toString(StandardCharsets.UTF_8.toString()), originalUri, sourceMapKey, sourceResolved.getLastModified());
094                return _sourceMapCache.get(sourceMapKey);
095            }
096            catch (ProcessingException | URISyntaxException e)
097            {
098                getLogger().error("Unable to compile CSS file", e);
099            }
100        }
101        
102        return null;
103    }
104
105}