001/*__SKIN_SCHEME
002 *  Copyright 2021 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.gdpr;
017
018import java.io.IOException;
019import java.net.MalformedURLException;
020import java.util.Map;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.avalon.framework.service.Serviceable;
025import org.apache.commons.lang.StringUtils;
026import org.apache.excalibur.source.Source;
027import org.apache.excalibur.source.SourceFactory;
028import org.apache.excalibur.source.SourceResolver;
029
030import org.ametys.runtime.plugin.component.AbstractLogEnabled;
031
032/**
033 * Resolve the protocol gdpr:// seeking xsl file to render GDPR services
034 */
035public class GDPRSourceFactory extends AbstractLogEnabled implements SourceFactory, Serviceable
036{
037    /** Scheme for skin */
038    protected static final String __GDPR_SCHEME = "gdpr";
039    /** Scheme to init gdpr */
040    protected static final String __GDPR_SCHEME_INIT = __GDPR_SCHEME + ":init";
041
042    /** The GDPR services extension point */
043    protected GDPRServicesExtensionPoint _gdprServicesEP;
044    
045    /** The source resolver */
046    protected SourceResolver _sourceResolver;
047    
048    /** The GDPR component helper */
049    protected GDPRComponentHelper _gdprComponentHelper;
050    
051    public void service(ServiceManager manager) throws ServiceException
052    {
053        _gdprServicesEP = (GDPRServicesExtensionPoint) manager.lookup(GDPRServicesExtensionPoint.ROLE);
054        _sourceResolver = (SourceResolver) manager.lookup(SourceResolver.ROLE);
055        _gdprComponentHelper = (GDPRComponentHelper) manager.lookup(GDPRComponentHelper.ROLE);
056    }
057    
058    public Source getSource(String location, Map parameters) throws IOException, MalformedURLException
059    {
060        GDPRComponent gdprComponent = _gdprComponentHelper.getSelectedGDPRComponent();
061        
062        if (__GDPR_SCHEME_INIT.equals(location))
063        {
064            return _getSource(location, gdprComponent.getInitXSLTPath());
065        }
066        
067        // Location is like SCHEME:service
068        String service = StringUtils.substringAfter(location, __GDPR_SCHEME + ":");
069        return _getSource(location, _gdprServicesEP.getServiceXSLTPath(service));
070    }
071
072    private Source _getSource(String location, String path) throws IOException
073    {
074        if (getLogger().isInfoEnabled())
075        {
076            getLogger().info("For protocol '" + location + "', try to resolve the source with URI '" + path + "'");
077        }
078        return _sourceResolver.resolveURI(path);
079    }
080    
081    public void release(Source source)
082    {
083        // Nothing to do
084        
085    }
086    
087}