001/*
002 *  Copyright 2023 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.analytics;
017
018
019import java.net.URI;
020import java.net.URISyntaxException;
021
022import org.apache.avalon.framework.configuration.Configurable;
023import org.apache.avalon.framework.configuration.Configuration;
024import org.apache.avalon.framework.configuration.ConfigurationException;
025
026import org.ametys.runtime.i18n.I18nizableText;
027import org.ametys.runtime.plugin.component.AbstractLogEnabled;
028import org.ametys.runtime.plugin.component.PluginAware;
029
030/**
031 * Abstract web analytics provider for static configuration
032 */
033public abstract class AbstractWebAnalyticsProvider extends AbstractLogEnabled implements WebAnalyticsProvider, Configurable, PluginAware
034{
035    /** Token to replace in the url by a random id value */
036    public static final String RANDOM_NUMBER_TOKEN = "#random_number_token#";
037    
038    /** The provider id */
039    protected String _id;
040    
041    /** The provider label */
042    protected I18nizableText _label;
043    
044    /** The XSLT path */
045    protected String _xsltPath;
046    
047    /** The plugin name */
048    protected String _pluginName;
049    
050    public void setPluginInfo(String pluginName, String featureName, String id)
051    {
052        _pluginName = pluginName;
053    }
054    
055    public void configure(Configuration configuration) throws ConfigurationException
056    {
057        _id = configuration.getAttribute("id");
058        _label = I18nizableText.parseI18nizableText(configuration.getChild("label"), "plugin." + _pluginName, "");
059        _xsltPath = configuration.getChild("xsltPath").getValue();
060    }
061    
062    public String getId()
063    {
064        return _id;
065    }
066    
067    public I18nizableText getLabel()
068    {
069        return _label;
070    }
071    
072    /**
073     * Encode a value to use as an identifier component.
074     * @param value the value to encode.
075     * @return the encoded value.
076     * @throws URISyntaxException if an error occurs encoding the value.
077     */
078    protected String _encodeValue(String value) throws URISyntaxException
079    {
080        URI uri = new URI(null, null, null, value, null);
081        return uri.getRawQuery().replace("/", "%2F");
082    }
083    
084    public String getXSLTPath()
085    {
086        return "plugin:" + _pluginName + "://" + _xsltPath;
087    }
088}