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 component */
043    protected GDPRComponent _gdprComponent;
044    
045    /** The GDPR services extension point */
046    protected GDPRServicesExtensionPoint _gdprServicesEP;
047    
048    /** The source resolver */
049    protected SourceResolver _sourceResolver;
050    
051    public void service(ServiceManager manager) throws ServiceException
052    {
053        _gdprComponent = (GDPRComponent) manager.lookup(GDPRComponent.ROLE);
054        _gdprServicesEP = (GDPRServicesExtensionPoint) manager.lookup(GDPRServicesExtensionPoint.ROLE);
055        _sourceResolver = (SourceResolver) manager.lookup(SourceResolver.ROLE);
056    }
057
058    public Source getSource(String location, Map parameters) throws IOException, MalformedURLException
059    {
060        if (__GDPR_SCHEME_INIT.equals(location))
061        {
062            return _getSource(location, _gdprComponent.getInitXSLTPath());
063        }
064        
065        // Location is like SCHEME:service
066        String service = StringUtils.substringAfter(location, __GDPR_SCHEME + ":");
067        return _getSource(location, _gdprServicesEP.getServiceXSLTPath(service));
068    }
069
070    private Source _getSource(String location, String path) throws IOException
071    {
072        if (getLogger().isInfoEnabled())
073        {
074            getLogger().info("For protocol '" + location + "', try to resolve the source with URI '" + path + "'");
075        }
076        return _sourceResolver.resolveURI(path);
077    }
078    
079    public void release(Source source)
080    {
081        // Nothing to do
082        
083    }
084    
085}