001/* 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 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; 027import org.apache.commons.lang3.StringUtils; 028 029import org.ametys.runtime.config.Config; 030import org.ametys.web.WebHelper; 031import org.ametys.web.content.GetSiteAction; 032import org.ametys.web.repository.site.Site; 033import org.ametys.web.repository.site.SiteManager; 034 035/** 036 * Helper for GDPR component 037 */ 038public class GDPRComponentHelper implements Component, Serviceable, Contextualizable 039{ 040 /** The Avalon role */ 041 public static final String ROLE = GDPRComponentHelper.class.getName(); 042 043 /** The value for the site configuration to have the general configuration */ 044 public static final String SITE_CONFIGURATION_RGPD_GENERAL_CONFIGURATION_VALUE = "_ametys_general_configuration"; 045 046 /** The id of the ametys gdpr component */ 047 public static final String AMETYS_GDPR_COMPONENT_ID = "gdpr.web"; 048 049 /** The site configuration gdpr component id */ 050 public static final String SITE_CONFIGURATION_GDPR_COMPONENT_ID = "gdpr-component-choice"; 051 052 /** The general configuration gdpr component id */ 053 public static final String GENERAL_CONFIGURATION_GDPR_COMPONENT_ID = "plugins.web.gdpr.choice"; 054 055 /** The GDPR component EP */ 056 protected GDPRComponentExtensionPoint _gdprComponentEP; 057 058 /** The site manager */ 059 protected SiteManager _siteManager; 060 061 /** The context */ 062 protected Context _context; 063 064 public void service(ServiceManager manager) throws ServiceException 065 { 066 _gdprComponentEP = (GDPRComponentExtensionPoint) manager.lookup(GDPRComponentExtensionPoint.ROLE); 067 _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE); 068 } 069 070 public void contextualize(Context context) throws ContextException 071 { 072 _context = context; 073 } 074 075 /** 076 * Get the selected GDPR component 077 * @return the selected GDPR component 078 */ 079 public GDPRComponent getSelectedGDPRComponent() 080 { 081 Request request = ContextHelper.getRequest(_context); 082 083 // Issue CMS-3391 084 @SuppressWarnings("deprecation") 085 String siteName = (String) request.getAttribute(GetSiteAction.OVERRIDE_SITE_REQUEST_ATTR); 086 if (siteName == null) 087 { 088 siteName = WebHelper.getSiteName(request); 089 } 090 091 Site site = _siteManager.getSite(siteName); 092 093 String gdprComponentId = site != null ? site.getValue(SITE_CONFIGURATION_GDPR_COMPONENT_ID) : null; 094 if (StringUtils.isBlank(gdprComponentId) || SITE_CONFIGURATION_RGPD_GENERAL_CONFIGURATION_VALUE.equals(gdprComponentId)) 095 { 096 gdprComponentId = Config.getInstance().getValue(GENERAL_CONFIGURATION_GDPR_COMPONENT_ID); 097 } 098 099 return _gdprComponentEP.hasExtension(gdprComponentId) ? _gdprComponentEP.getExtension(gdprComponentId) : _gdprComponentEP.getExtension(AMETYS_GDPR_COMPONENT_ID); 100 } 101}