001/* 002 * Copyright 2025 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 018import org.apache.avalon.framework.component.Component; 019import org.apache.avalon.framework.context.Context; 020import org.apache.avalon.framework.context.ContextException; 021import org.apache.avalon.framework.context.Contextualizable; 022import org.apache.avalon.framework.service.ServiceException; 023import org.apache.avalon.framework.service.ServiceManager; 024import org.apache.avalon.framework.service.Serviceable; 025import org.apache.cocoon.components.ContextHelper; 026import org.apache.cocoon.environment.Request; 027 028import org.ametys.runtime.config.Config; 029import org.ametys.web.WebHelper; 030import org.ametys.web.content.GetSiteAction; 031import org.ametys.web.repository.site.Site; 032import org.ametys.web.repository.site.SiteManager; 033 034/** 035 * Web analytics provider interface 036 */ 037public class WebAnalyticsHelper implements Component, Serviceable, Contextualizable 038{ 039 /** Avalon Role */ 040 public static final String ROLE = WebAnalyticsHelper.class.getName(); 041 042 /** The web analytics empty id */ 043 public static final String WEB_ANALYTICS_EMPTY_ID = "web.analytics.empty"; 044 045 /** The web analytics provider extension point */ 046 protected WebAnalyticsProviderExtensionPoint _webAnalyticsProviderEP; 047 048 /** The site manager */ 049 protected SiteManager _siteManager; 050 051 /** The context */ 052 protected Context _context; 053 054 public void service(ServiceManager manager) throws ServiceException 055 { 056 _webAnalyticsProviderEP = (WebAnalyticsProviderExtensionPoint) manager.lookup(WebAnalyticsProviderExtensionPoint.ROLE); 057 _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE); 058 } 059 060 public void contextualize(Context context) throws ContextException 061 { 062 _context = context; 063 } 064 065 /** 066 * Get the current web analytics provider 067 * @return the current web analytics provider 068 */ 069 public WebAnalyticsProvider getSelectedProvider() 070 { 071 Request request = ContextHelper.getRequest(_context); 072 073 // Issue CMS-3391 074 @SuppressWarnings("deprecation") 075 String siteName = (String) request.getAttribute(GetSiteAction.OVERRIDE_SITE_REQUEST_ATTR); 076 if (siteName == null) 077 { 078 siteName = WebHelper.getSiteName(request); 079 } 080 081 Site site = _siteManager.getSite(siteName); 082 return getSelectedProvider(site); 083 } 084 085 /** 086 * Get the current web analytics provider 087 * @param site the site 088 * @return the current web analytics provider 089 */ 090 public WebAnalyticsProvider getSelectedProvider(Site site) 091 { 092 String siteTrackingProviderId = site != null ? site.getValue("tracking-provider", true, null) : null; 093 String trackingProviderId = WebAnalyticsProviderExtensionPoint.DEFAULT_CONFIGURATION_ID.equals(siteTrackingProviderId) 094 ? Config.getInstance().getValue("plugins.web.tracking.provider", true, null) 095 : siteTrackingProviderId; 096 097 return _webAnalyticsProviderEP.hasExtension(trackingProviderId) 098 ? _webAnalyticsProviderEP.getExtension(trackingProviderId) 099 : _webAnalyticsProviderEP.getExtension(WEB_ANALYTICS_EMPTY_ID); 100 101 } 102 103}