001/*
002 *  Copyright 2017 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.web.resources.css.sass;
017
018import org.apache.avalon.framework.service.ServiceException;
019import org.apache.avalon.framework.service.ServiceManager;
020import org.apache.avalon.framework.service.Serviceable;
021import org.apache.cocoon.components.ContextHelper;
022import org.apache.cocoon.environment.Request;
023import org.slf4j.Logger;
024
025import org.ametys.plugins.core.ui.resources.css.sass.AbstractAmetysSASSFunctionsProvider;
026import org.ametys.plugins.repository.UnknownAmetysObjectException;
027import org.ametys.runtime.plugin.component.LogEnabled;
028import org.ametys.web.repository.site.Site;
029import org.ametys.web.repository.site.SiteManager;
030
031import io.bit3.jsass.annotation.Name;
032import io.bit3.jsass.type.SassString;
033
034/**
035 * This helper provides function that will be directly injected into Sass file compilation. 
036 * Inherited methods are NOT injected. For this reason, method that must not be accessed from Sass have to be declared in the parent class AbstractAmetysSASSHelper
037 */
038public class AmetysSassFunctionsProvider extends AbstractAmetysSASSFunctionsProvider implements Serviceable, LogEnabled
039{
040    private SiteManager _siteManager;
041    private Logger _logger;
042
043    public void service(ServiceManager manager) throws ServiceException
044    {
045        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
046    }
047    
048    public void setLogger(Logger logger)
049    {
050        _logger = logger;
051    }
052    
053    /**
054     * Resolve url path inside the current skin
055     * @param path the path relative to the current skin
056     * @return the absolute path
057     */
058    public SassString skinUrl(@Name("path") String path)
059    {
060        Request request = ContextHelper.getRequest(_context);
061        
062        String siteName = (String) request.getAttribute("site");
063
064        try
065        {
066            Site site = _siteManager.getSite(siteName);
067            String skinName = site.getSkinId();
068            
069            StringBuilder sb = new StringBuilder(request.getContextPath());
070            sb.append("/skins/")
071                .append(skinName)
072                .append("/resources")
073                .append(path.startsWith("/") ? path : "/" + path);
074            
075            SassString url = new SassString("url(\"" + sb.toString() + "\")", false);
076            return url;
077        }
078        catch (UnknownAmetysObjectException e)
079        {
080            _logger.error("Can not find site {} to resolve path at {}", siteName, path);
081            return null;
082        }
083    }
084}