001/*
002 *  Copyright 2022 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.core.captcha;
017
018import java.util.Collection;
019import java.util.HashSet;
020import java.util.Set;
021
022import org.apache.avalon.framework.configuration.Configurable;
023import org.apache.avalon.framework.configuration.Configuration;
024import org.apache.avalon.framework.configuration.ConfigurationException;
025import org.apache.excalibur.source.SourceException;
026
027import org.ametys.runtime.plugin.Feature;
028import org.ametys.runtime.plugin.PluginsManager;
029import org.ametys.runtime.plugin.component.AbstractLogEnabled;
030import org.ametys.runtime.plugin.component.PluginAware;
031
032/**
033 * Abstract class for Captchas to handle common methods such as retrieving wanted xsl file
034 */
035public abstract class AbstractCaptcha extends AbstractLogEnabled implements PluginAware, Configurable, Captcha
036{
037    private String _pluginName;
038    private String _id;
039    private String _xslPath;
040    private String _scssPath;
041    private String _featureName;
042    
043    public void setPluginInfo(String pluginName, String featureName, String id)
044    {
045        _pluginName = pluginName;
046        _id = id;
047        _featureName = featureName;
048    }
049    
050    public String getId()
051    {
052        return _id;
053    }
054    
055    public void configure(Configuration configuration) throws ConfigurationException
056    {
057        _xslPath = configuration.getChild("xslPath").getValue();
058        _scssPath = configuration.getChild("scssPath").getValue();
059    }
060
061    public Collection<String> getConfigParameters()
062    {
063        Feature feature = PluginsManager.getInstance().getFeatures().get(_pluginName + "/" + _featureName);
064        Set<String> configParametersDeclared = feature.getConfigParameters().keySet();
065        Collection<String> configParametersReferences = feature.getConfigParametersReferences();
066        
067        Set<String> configParameters = new HashSet<>();
068        configParameters.addAll(configParametersDeclared);
069        configParameters.addAll(configParametersReferences);
070        return configParameters;
071    }
072    
073    public String getXSLHelperURL() throws SourceException
074    {
075        return "plugin:" + _pluginName + "://" + _xslPath;
076    }
077    
078    public String getLoginSCSSURL() throws SourceException
079    {
080        return "plugin:" + _pluginName + "://" + _scssPath;
081    }
082}