ConfigurazionePolicyGestioneTokenAdd.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */
  20. package org.openspcoop2.web.ctrlstat.servlet.config;

  21. import java.util.ArrayList;
  22. import java.util.HashMap;
  23. import java.util.List;
  24. import java.util.Map;
  25. import java.util.Properties;

  26. import javax.servlet.http.HttpServletRequest;
  27. import javax.servlet.http.HttpServletResponse;
  28. import javax.servlet.http.HttpSession;

  29. import org.apache.struts.action.Action;
  30. import org.apache.struts.action.ActionForm;
  31. import org.apache.struts.action.ActionForward;
  32. import org.apache.struts.action.ActionMapping;
  33. import org.openspcoop2.core.commons.Liste;
  34. import org.openspcoop2.core.config.GenericProperties;
  35. import org.openspcoop2.core.config.Property;
  36. import org.openspcoop2.core.mvc.properties.Config;
  37. import org.openspcoop2.core.mvc.properties.utils.ConfigManager;
  38. import org.openspcoop2.core.mvc.properties.utils.DBPropertiesUtils;
  39. import org.openspcoop2.core.mvc.properties.utils.PropertiesSourceConfiguration;
  40. import org.openspcoop2.web.ctrlstat.core.ConsoleSearch;
  41. import org.openspcoop2.web.ctrlstat.core.ControlStationCore;
  42. import org.openspcoop2.web.ctrlstat.costanti.CostantiControlStation;
  43. import org.openspcoop2.web.ctrlstat.servlet.GeneralHelper;
  44. import org.openspcoop2.web.lib.mvc.DataElement;
  45. import org.openspcoop2.web.lib.mvc.ForwardParams;
  46. import org.openspcoop2.web.lib.mvc.GeneralData;
  47. import org.openspcoop2.web.lib.mvc.PageData;
  48. import org.openspcoop2.web.lib.mvc.Parameter;
  49. import org.openspcoop2.web.lib.mvc.ServletUtils;
  50. import org.openspcoop2.web.lib.mvc.TipoOperazione;
  51. import org.openspcoop2.web.lib.mvc.properties.beans.ConfigBean;


  52. /**    
  53.  * ConfigurazionePolicyGestioneTokenAdd
  54.  *
  55.  * @author Pintori Giuliano (pintori@link.it)
  56.  * @author $Author$
  57.  * @version $Rev$, $Date$
  58.  */
  59. public class ConfigurazionePolicyGestioneTokenAdd extends Action {

  60.     @Override
  61.     public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

  62.         HttpSession session = request.getSession(true);

  63.         // Inizializzo PageData
  64.         PageData pd = new PageData();

  65.         GeneralHelper generalHelper = new GeneralHelper(session);

  66.         // Inizializzo GeneralData
  67.         GeneralData gd = generalHelper.initGeneralData(request);

  68.         String userLogin = ServletUtils.getUserLoginFromSession(session);  
  69.        
  70.         TipoOperazione tipoOperazione = TipoOperazione.ADD;
  71.        
  72.         try {
  73.             ConfigurazioneHelper confHelper = new ConfigurazioneHelper(request, pd, session);
  74.             // Preparo il menu
  75.             confHelper.makeMenu();
  76.            
  77.             String id = "";
  78.             String nome = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_GESTORE_POLICY_TOKEN_NOME);
  79.             String descrizione = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_GESTORE_POLICY_TOKEN_DESCRIZIONE);
  80.             String tipo = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_GESTORE_POLICY_TOKEN_TIPO);
  81.            
  82.             String infoType = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_TOKEN_POLICY_TIPOLOGIA_INFORMAZIONE);
  83.             if(infoType==null) {
  84.                 infoType = ServletUtils.getObjectFromSession(request, session, String.class, ConfigurazioneCostanti.PARAMETRO_TOKEN_POLICY_TIPOLOGIA_INFORMAZIONE);
  85.             }
  86.             boolean attributeAuthority = ConfigurazioneCostanti.isConfigurazioneAttributeAuthority(infoType);
  87.            
  88.             ConfigurazioneCore confCore = new ConfigurazioneCore();
  89.            
  90.             boolean forceIdEnabled = attributeAuthority ?
  91.                     confCore.isAttributeAuthorityForceIdEnabled() :
  92.                     confCore.isTokenPolicyForceIdEnabled() ;
  93.             Properties mapId = attributeAuthority ?
  94.                     confCore.getAttributeAuthorityTipologia() :
  95.                     confCore.getTokenPolicyTipologia();
  96.             String forceId = null;
  97.             if(forceIdEnabled) {
  98.                 forceId = attributeAuthority ?
  99.                         confCore.getAttributeAuthorityForceId() :
  100.                         confCore.getTokenPolicyForceId();
  101.             }
  102.            
  103.             PropertiesSourceConfiguration propertiesSourceConfiguration = attributeAuthority ?
  104.                     confCore.getAttributeAuthorityPropertiesSourceConfiguration() :
  105.                     confCore.getPolicyGestioneTokenPropertiesSourceConfiguration();
  106.            
  107.             ConfigManager configManager = ConfigManager.getinstance(ControlStationCore.getLog());
  108.             configManager.leggiConfigurazioni(propertiesSourceConfiguration, true);

  109.             List<String> nomiConfigurazioniPolicyGestioneToken = configManager.getNomiConfigurazioni(propertiesSourceConfiguration);
  110.             List<String> labelConfigurazioniPolicyGestioneToken = configManager.convertToLabel(propertiesSourceConfiguration, nomiConfigurazioniPolicyGestioneToken);
  111.            
  112.             List<String> propConfigPolicyGestioneTokenLabelListTmp = new ArrayList<>();
  113.             propConfigPolicyGestioneTokenLabelListTmp.add(CostantiControlStation.DEFAULT_VALUE_NON_SELEZIONATO);
  114.             propConfigPolicyGestioneTokenLabelListTmp.addAll(labelConfigurazioniPolicyGestioneToken);
  115.            
  116.             List<String>  propConfigPolicyGestioneTokenListTmp = new ArrayList<>();
  117.             propConfigPolicyGestioneTokenListTmp.add(CostantiControlStation.DEFAULT_VALUE_NON_SELEZIONATO);
  118.             propConfigPolicyGestioneTokenListTmp.addAll(nomiConfigurazioniPolicyGestioneToken);
  119.            
  120.             String[] propConfigPolicyGestioneTokenLabelList = propConfigPolicyGestioneTokenLabelListTmp.toArray(new String[propConfigPolicyGestioneTokenLabelListTmp.size()]);
  121.             String[] propConfigPolicyGestioneTokenList= propConfigPolicyGestioneTokenListTmp.toArray(new String[propConfigPolicyGestioneTokenListTmp.size()]);
  122.            
  123.             String postBackElementName = confHelper.getPostBackElementName();
  124.            
  125.             Config configurazione = null;
  126.             ConfigBean configurazioneBean = null;
  127.            
  128.             if(tipo == null) {
  129.                 if(forceIdEnabled)
  130.                     tipo = forceId;
  131.             }
  132.            
  133.             String tipologia = null;
  134.             if(forceIdEnabled) {
  135.                 tipologia = mapId.getProperty(forceId);
  136.             }
  137.             if(tipo != null && !tipo.equals(CostantiControlStation.DEFAULT_VALUE_NON_SELEZIONATO)) {
  138.                 configurazione = configManager.getConfigurazione(propertiesSourceConfiguration, tipo);
  139.            
  140.                 configurazioneBean = confCore.leggiConfigurazione(configurazione, null);
  141.                
  142.                 if(postBackElementName != null && postBackElementName.equals(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_GESTORE_POLICY_TOKEN_TIPO)) {
  143.                     // reset di eventuali configurazioni salvate in sessione
  144.                     ServletUtils.removeConfigurazioneBeanFromSession(request, session, configurazioneBean.getId());
  145.                 }
  146.                
  147.                 confHelper.aggiornaConfigurazioneProperties(configurazioneBean);
  148.                
  149.                 configurazioneBean.updateConfigurazione(configurazione);
  150.                 ServletUtils.saveConfigurazioneBeanIntoSession(request, session, configurazioneBean, configurazioneBean.getId());
  151.            
  152.                 if(!forceIdEnabled) {
  153.                     tipologia = mapId.getProperty(tipo);
  154.                     if(tipologia==null) {
  155.                         throw new Exception("Mapping tipologia token per tipo '"+tipo+"' inesistente");
  156.                     }
  157.                 }
  158.             }
  159.            
  160.             // setto la barra del titolo
  161.             List<Parameter> lstParam = new ArrayList<>();

  162.             String label = attributeAuthority ?
  163.                     ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_ATTRIBUTE_AUTHORITY :
  164.                     ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_POLICY_GESTIONE_TOKEN;
  165.            
  166.             lstParam.add(new Parameter(label, ConfigurazioneCostanti.SERVLET_NAME_CONFIGURAZIONE_POLICY_GESTIONE_TOKEN_LIST));
  167.             lstParam.add(ServletUtils.getParameterAggiungi());
  168.            
  169.             // Se tipo = null, devo visualizzare la pagina per l'inserimento
  170.             // dati
  171.             if (confHelper.isEditModeInProgress()) {
  172.                 ServletUtils.setPageDataTitle(pd, lstParam);
  173.                
  174.                 if(nome == null) {
  175.                     nome = "";
  176.                     descrizione = "";
  177.                    
  178.                     if(tipo == null) {
  179.                         if(!forceIdEnabled)
  180.                             tipo = CostantiControlStation.DEFAULT_VALUE_NON_SELEZIONATO;
  181.                         else
  182.                             tipo = forceId;
  183.                     }
  184.                 }
  185.                
  186.                 // preparo i campi
  187.                 List<DataElement> dati = new ArrayList<>();
  188.                 dati.add(ServletUtils.getDataElementForEditModeFinished());
  189.                
  190.                 dati = confHelper.addPolicyGestioneTokenToDati(tipoOperazione,dati,id,nome,descrizione,tipo,propConfigPolicyGestioneTokenLabelList,propConfigPolicyGestioneTokenList,
  191.                         attributeAuthority, null);
  192.                
  193.                 dati = confHelper.addPropertiesConfigToDati(tipoOperazione,dati, tipo, configurazioneBean,false);
  194.                
  195.                 pd.setDati(dati);

  196.                 ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);

  197.                 return ServletUtils.getStrutsForwardEditModeInProgress(mapping,
  198.                         ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_POLICY_GESTIONE_TOKEN,
  199.                         ForwardParams.ADD());
  200.             }
  201.            
  202.             // Controlli sui campi immessi
  203.             boolean isOk = confHelper.policyGestioneTokenCheckData(tipoOperazione, nome,descrizione,tipo,tipologia);
  204.            
  205.             if (isOk) {
  206.                 isOk = confHelper.checkPropertiesConfigurationData(tipoOperazione, configurazioneBean, nome,descrizione,configurazione);
  207.             }
  208.             if (!isOk) {
  209.                
  210.                 ServletUtils.setPageDataTitle(pd, lstParam);
  211.                
  212.                 // preparo i campi
  213.                 List<DataElement> dati = new ArrayList<>();
  214.                 dati.add(ServletUtils.getDataElementForEditModeFinished());
  215.                
  216.                 dati = confHelper.addPolicyGestioneTokenToDati(tipoOperazione,dati,id,nome,descrizione,tipo,propConfigPolicyGestioneTokenLabelList,propConfigPolicyGestioneTokenList,
  217.                         attributeAuthority, null);
  218.                        
  219.                 dati = confHelper.addPropertiesConfigToDati(tipoOperazione,dati, tipo, configurazioneBean,false);
  220.                
  221.                 pd.setDati(dati);

  222.                 ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);
  223.                
  224.                 return ServletUtils.getStrutsForwardEditModeCheckError(mapping, ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_POLICY_GESTIONE_TOKEN, ForwardParams.ADD());
  225.             }

  226.             // insert sul db
  227.             GenericProperties policy = new GenericProperties();
  228.             policy.setNome(nome);
  229.             policy.setDescrizione(descrizione);
  230.             policy.setTipologia(tipologia);
  231.             policy.setTipo(tipo);
  232.            
  233.             Map<String, Properties> mappaDestinazione = configurazioneBean!=null ? configurazioneBean.getPropertiesMap() : new HashMap<>();
  234.             Map<String, String> map = DBPropertiesUtils.toMap(mappaDestinazione);
  235.            
  236.             for (String propKey : map.keySet()) {
  237.                 Property property = new Property();
  238.                 property.setNome(propKey);
  239.                 property.setValore(map.get(propKey));
  240.                
  241.                 policy.getPropertyList().add(property);
  242.             }
  243.            
  244.             confCore.performCreateOperation(userLogin, confHelper.smista(), policy);
  245.            
  246.             // Preparo la lista
  247.             ConsoleSearch ricerca = (ConsoleSearch) ServletUtils.getSearchObjectFromSession(request, session, ConsoleSearch.class);

  248.             int idLista = attributeAuthority ? Liste.CONFIGURAZIONE_GESTIONE_ATTRIBUTE_AUTHORITY : Liste.CONFIGURAZIONE_GESTIONE_POLICY_TOKEN;
  249.            
  250.             ricerca = confHelper.checkSearchParameters(idLista, ricerca);

  251.             List<String> tipologie = new ArrayList<>();
  252.             if(attributeAuthority) {
  253.                 tipologie.add(ConfigurazioneCostanti.DEFAULT_VALUE_PARAMETRO_CONFIGURAZIONE_GESTORE_POLICY_TOKEN_TIPOLOGIA_ATTRIBUTE_AUTHORITY);
  254.             }
  255.             else {
  256.                 tipologie.add(ConfigurazioneCostanti.DEFAULT_VALUE_PARAMETRO_CONFIGURAZIONE_GESTORE_POLICY_TOKEN_TIPOLOGIA_GESTIONE_POLICY_TOKEN);
  257.                 tipologie.add(ConfigurazioneCostanti.DEFAULT_VALUE_PARAMETRO_CONFIGURAZIONE_GESTORE_POLICY_TOKEN_TIPOLOGIA_RETRIEVE_POLICY_TOKEN);
  258.             }
  259.            
  260.             List<GenericProperties> lista = confCore.gestorePolicyTokenList(idLista, tipologie, ricerca);
  261.            
  262.             confHelper.prepareGestorePolicyTokenList(ricerca, lista, idLista);
  263.            
  264.             // reset di eventuali configurazioni salvate in sessione
  265.             if(configurazioneBean!=null) {
  266.                 ServletUtils.removeConfigurazioneBeanFromSession(request, session, configurazioneBean.getId());
  267.             }
  268.            
  269.             // salvo l'oggetto ricerca nella sessione
  270.             ServletUtils.setSearchObjectIntoSession(request, session, ricerca);
  271.             ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);
  272.            
  273.             // Forward control to the specified success URI
  274.             return ServletUtils.getStrutsForwardEditModeFinished(mapping, ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_POLICY_GESTIONE_TOKEN, ForwardParams.ADD());
  275.         } catch (Exception e) {
  276.             return ServletUtils.getStrutsForwardError(ControlStationCore.getLog(), e, pd, request, session, gd, mapping,
  277.                     ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_POLICY_GESTIONE_TOKEN, ForwardParams.ADD());
  278.         }  
  279.     }
  280. }