001/*
002 *  Copyright 2020 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.editor;
017
018import java.io.IOException;
019import java.net.MalformedURLException;
020import java.util.Map;
021
022import org.apache.avalon.framework.context.Context;
023import org.apache.avalon.framework.context.ContextException;
024import org.apache.avalon.framework.context.Contextualizable;
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027import org.apache.avalon.framework.service.Serviceable;
028import org.apache.avalon.framework.thread.ThreadSafe;
029import org.apache.cocoon.components.ContextHelper;
030import org.apache.cocoon.environment.Request;
031import org.apache.excalibur.source.Source;
032import org.apache.excalibur.source.SourceFactory;
033
034import org.ametys.cms.data.Binary;
035import org.ametys.cms.data.BinarySource;
036import org.ametys.core.util.FilenameUtils;
037import org.ametys.runtime.plugin.component.AbstractLogEnabled;
038import org.ametys.web.WebConstants;
039import org.ametys.web.repository.site.Site;
040import org.ametys.web.repository.site.SiteManager;
041
042/**
043 * {@link SourceFactory} for site parameters.
044 */
045public class SiteParameterSourceFactory extends AbstractLogEnabled implements SourceFactory, ThreadSafe, Serviceable, Contextualizable
046{
047    /** source scheme */
048    public static final String SCHEME = "site-parameter";
049    
050    private Context _context;
051    private SiteManager _siteManager;
052    
053    @Override
054    public void contextualize(Context context) throws ContextException
055    {
056        _context = context;
057    }
058    
059    public void service(ServiceManager manager) throws ServiceException
060    {
061        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
062    }
063    
064    public Source getSource(String location, Map parameters) throws IOException, MalformedURLException
065    {
066        String uri = location.substring((SCHEME + "://").length());
067        
068        // uri are like <siteName>@<parameterPath>;<filename>
069        int i = uri.indexOf('@');
070        int j = uri.indexOf(';', i);
071        String siteName = uri.substring(0, i);
072        String parameter = uri.substring(i + 1, j);
073        String filename = uri.substring(j + 1);
074        
075        Request request = ContextHelper.getRequest(_context);
076        Site site = (Site) request.getAttribute(WebConstants.REQUEST_ATTR_SITE);
077        
078        try
079        {
080            if (site == null) 
081            {
082                site = _siteManager.getSite(siteName);
083            }
084            
085            Binary binary = site.getValue(parameter);
086    
087            return new BinarySource(binary, FilenameUtils.decode(filename), location, SCHEME);
088        }
089        catch (Exception e)
090        {
091            getLogger().error("Unable to resolve binary attribute for uri " + location, e);
092            
093            // returns an unexisting Source
094            return new BinarySource(null, FilenameUtils.decode(filename), location, SCHEME);
095        }
096    }
097
098    public void release(Source source)
099    {
100        // nothing to do
101    }
102}