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 */
016
017package org.ametys.plugins.core.ui.resources.css.sass;
018
019import java.io.IOException;
020import java.io.OutputStream;
021import java.net.MalformedURLException;
022import java.util.Map;
023
024import org.apache.avalon.framework.parameters.Parameters;
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027import org.apache.cocoon.ProcessingException;
028import org.apache.cocoon.ResourceNotFoundException;
029import org.apache.commons.io.IOUtils;
030import org.apache.excalibur.source.Source;
031import org.apache.excalibur.source.SourceException;
032
033import org.ametys.core.resources.AbstractResourceHandler;
034
035/**
036 * Reader for uncompiled SASS files.
037 */
038public class UncompiledSassResourceHandler extends AbstractResourceHandler
039{
040    @Override
041    public void service(ServiceManager manager) throws ServiceException
042    {
043        super.service(manager);
044    }
045    
046    @Override
047    public int getPriority()
048    {
049        return MIN_PRIORITY + 300;
050    }
051    
052    @Override
053    public Source setup(String location, Map objectModel, Parameters par, Map<String, Object> additionalParameters) throws ProcessingException, IOException
054    {
055        Source source = null;
056        // If the file exists at the original location, return it without any processing
057        source = _getSource(location);
058        if (source == null)
059        {
060            int lastSlashIndex = location.lastIndexOf("/");
061            if (lastSlashIndex + 2 < location.length())
062            {
063                // Appends underscore to the beginning of the file name
064                String underscorelocation = location.substring(0, lastSlashIndex + 1) + "_" + location.substring(lastSlashIndex + 1);
065                source = _getSource(underscorelocation);
066            }
067        }
068        
069        if (source == null)
070        {
071            throw new ResourceNotFoundException("Resource not found for URI : " + location);
072        }
073        
074        return source;
075    }
076
077    private Source _getSource(String location) throws MalformedURLException, IOException
078    {
079        try 
080        {
081            Source source = _resolver.resolveURI(location);
082            return source != null && source.exists() ? source : null;
083        } 
084        catch (SourceException e) 
085        {
086            // Nothing
087        }
088        return null;
089    }
090
091    @Override
092    public String getMimeType(Source source, Parameters par)
093    {
094        return "text/css";
095    }
096
097    @Override
098    public void generateResource(Source source, OutputStream out, Map objectModel, Parameters parameters, Map<String, Object> additionalParameters) throws IOException, ProcessingException
099    {
100        IOUtils.copy(source.getInputStream(), out);
101    }
102}