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.resources;
017
018import java.io.ByteArrayOutputStream;
019import java.io.IOException;
020import java.io.OutputStream;
021import java.io.Serializable;
022import java.net.URISyntaxException;
023import java.nio.charset.StandardCharsets;
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.cocoon.environment.Request;
030import org.apache.excalibur.source.Source;
031
032import org.ametys.core.DevMode;
033import org.ametys.core.DevMode.DEVMODE;
034import org.ametys.plugins.core.ui.resources.css.CSSFileHelper;
035import org.ametys.plugins.core.ui.resources.css.JSASSResourceURIExtensionPoint;
036
037/**
038 * Resource handler for CSS files
039 */
040public class CssResourceHandler extends ExpiresResourceHandler
041{
042    private ProxiedContextPathProvider _proxiedContextPathProvider;
043    private JSASSResourceURIExtensionPoint _jsassResourceURIExtensionPoint;
044    
045    /**
046     * Default constructor
047     */
048    public CssResourceHandler()
049    {
050        super();
051    }
052    
053    /**
054     * Constructor with an already resolved {@link Source}.
055     * @param source the source
056     */
057    public CssResourceHandler(Source source)
058    {
059        super(source);
060    }
061    
062    @Override
063    public void service(ServiceManager manager) throws ServiceException
064    {
065        super.service(manager);
066        
067        _proxiedContextPathProvider = (ProxiedContextPathProvider) manager.lookup(ProxiedContextPathProvider.ROLE);
068        _jsassResourceURIExtensionPoint = (JSASSResourceURIExtensionPoint) manager.lookup(JSASSResourceURIExtensionPoint.ROLE);
069    }
070    
071    @Override
072    public Serializable getKey()
073    {
074        // files are cached by source and by context path
075        return super.getKey() + "*" + _proxiedContextPathProvider.getContextPath();
076    }
077    
078    @Override
079    public void generate(OutputStream out) throws IOException, ProcessingException
080    {
081        Request request = ObjectModelHelper.getRequest(_objectModel);
082        if (DEVMODE.SUPER_DEVELOPPMENT.equals(DevMode.getDeveloperMode(request)))
083        {
084            // Files are not aggregated, thus don't need to be modified
085            super.generate(out);
086            return;
087        }
088        
089        // Replace relative URIs with absolute values, required before aggregating files served through a new location
090        ByteArrayOutputStream baos = new ByteArrayOutputStream();
091        super.generate(baos);
092        String content = baos.toString(StandardCharsets.UTF_8.toString());
093        
094        try
095        {
096            String locationPath = _jsassResourceURIExtensionPoint.resolve(_requestedLocation);
097            content = CSSFileHelper.replaceRelativeUri(content, locationPath, _jsassResourceURIExtensionPoint, _proxiedContextPathProvider.getContextPath(), _proxiedContextPathProvider.getContextPath());
098        }
099        catch (URISyntaxException e)
100        {
101            throw new ProcessingException("Unable to replace relative URIs inside of css file '" + _requestedLocation + "'", e);
102        }
103        
104        out.write(content.getBytes(StandardCharsets.UTF_8));
105    }
106}