ConfigurazionePolicyGestioneTokenChange.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.text.MessageFormat;
  22. import java.util.ArrayList;
  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.  * ConfigurazionePolicyGestioneTokenChange
  54.  *
  55.  * @author Pintori Giuliano (pintori@link.it)
  56.  * @author $Author$
  57.  * @version $Rev$, $Date$
  58.  */
  59. public class ConfigurazionePolicyGestioneTokenChange 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.CHANGE;
  71.        
  72.         try {
  73.             ConfigurazioneHelper confHelper = new ConfigurazioneHelper(request, pd, session);
  74.             // Preparo il menu
  75.             confHelper.makeMenu();
  76.            
  77.             String id = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_GESTORE_POLICY_TOKEN_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.             String resetElementoCacheS = confHelper.getParameter(CostantiControlStation.PARAMETRO_ELIMINA_ELEMENTO_DALLA_CACHE);
  89.             boolean resetElementoCache = ServletUtils.isCheckBoxEnabled(resetElementoCacheS);
  90.            
  91.             ConfigurazioneCore confCore = new ConfigurazioneCore();
  92.            
  93.             Properties mapId = attributeAuthority ?
  94.                     confCore.getAttributeAuthorityTipologia() :
  95.                     confCore.getTokenPolicyTipologia();
  96.            
  97.             GenericProperties genericProperties = confCore.getGenericProperties(Long.parseLong(id));
  98.            
  99.             PropertiesSourceConfiguration propertiesSourceConfiguration = attributeAuthority ?
  100.                     confCore.getAttributeAuthorityPropertiesSourceConfiguration() :
  101.                     confCore.getPolicyGestioneTokenPropertiesSourceConfiguration();
  102.            
  103.             ConfigManager configManager = ConfigManager.getinstance(ControlStationCore.getLog());
  104.             configManager.leggiConfigurazioni(propertiesSourceConfiguration, true);

  105.             List<String> nomiConfigurazioniPolicyGestioneToken = configManager.getNomiConfigurazioni(propertiesSourceConfiguration);
  106.             List<String> labelConfigurazioniPolicyGestioneToken = configManager.convertToLabel(propertiesSourceConfiguration, nomiConfigurazioniPolicyGestioneToken);
  107.            
  108.             String[] propConfigPolicyGestioneTokenLabelList = labelConfigurazioniPolicyGestioneToken.toArray(new String[labelConfigurazioniPolicyGestioneToken.size()]);
  109.             String[] propConfigPolicyGestioneTokenList= nomiConfigurazioniPolicyGestioneToken.toArray(new String[nomiConfigurazioniPolicyGestioneToken.size()]);
  110.            
  111.             Config configurazione = configManager.getConfigurazione(propertiesSourceConfiguration, genericProperties.getTipo());
  112.            
  113.             Map<String, Properties> mappaDB = confCore.readGestorePolicyTokenPropertiesConfiguration(genericProperties.getId());
  114.            
  115.             ConfigBean configurazioneBean = confCore.leggiConfigurazione(configurazione, mappaDB);
  116.            
  117.             if(nome != null) { // la prima volta non sovrascrivo la configurazione con i valori letti dai parametri
  118.                 confHelper.aggiornaConfigurazioneProperties(configurazioneBean);
  119.             } else {
  120.                 // reset di eventuali configurazioni salvate in sessione
  121.                 ServletUtils.removeConfigurazioneBeanFromSession(request, session, configurazioneBean.getId());
  122.             }
  123.            
  124.             configurazioneBean.updateConfigurazione(configurazione);
  125.             ServletUtils.saveConfigurazioneBeanIntoSession(request, session, configurazioneBean, configurazioneBean.getId());
  126.            
  127.             // reset elemento dalla cache
  128.             if(resetElementoCache) {
  129.                
  130.                 // Uso lo stessoAlias
  131.                 List<String> aliases = confCore.getJmxPdDAliases();
  132.                 String alias = null;
  133.                 if(aliases!=null && !aliases.isEmpty()) {
  134.                     alias = aliases.get(0);
  135.                 }
  136.                
  137.                 String metodo = null;
  138.                 if(attributeAuthority) {
  139.                     metodo = confCore.getJmxPdDConfigurazioneSistemaNomeMetodoRipulisciRiferimentiCacheAttributeAuthority(alias);
  140.                 }
  141.                 else {
  142.                     boolean validazione = ConfigurazioneCostanti.DEFAULT_VALUE_PARAMETRO_CONFIGURAZIONE_GESTORE_POLICY_TOKEN_TIPOLOGIA_GESTIONE_POLICY_TOKEN.equals(genericProperties.getTipologia());
  143.                     boolean negoziazione = ConfigurazioneCostanti.DEFAULT_VALUE_PARAMETRO_CONFIGURAZIONE_GESTORE_POLICY_TOKEN_TIPOLOGIA_RETRIEVE_POLICY_TOKEN.equals(genericProperties.getTipologia());
  144.                     if(validazione) {
  145.                         metodo = confCore.getJmxPdDConfigurazioneSistemaNomeMetodoRipulisciRiferimentiCacheTokenPolicyValidazione(alias);
  146.                     }
  147.                     else if(negoziazione) {
  148.                         metodo = confCore.getJmxPdDConfigurazioneSistemaNomeMetodoRipulisciRiferimentiCacheTokenPolicyNegoziazione(alias);
  149.                     }
  150.                     else {
  151.                         throw new Exception("Tipologia '"+genericProperties.getTipologia()+"' non supportata");
  152.                     }
  153.                 }
  154.                
  155.                 String labelPolicy = genericProperties.getNome();
  156.                 confCore.invokeJmxMethodAllNodesAndSetResult(pd, confCore.getJmxPdDConfigurazioneSistemaNomeRisorsaConfigurazionePdD(alias),
  157.                         metodo,
  158.                         MessageFormat.format(CostantiControlStation.LABEL_ELIMINATO_CACHE_SUCCESSO,labelPolicy),
  159.                         MessageFormat.format(CostantiControlStation.LABEL_ELIMINATO_CACHE_FALLITO_PREFIX,labelPolicy),
  160.                         genericProperties.getId());            
  161.                
  162.                 String resetFromLista = confHelper.getParameter(CostantiControlStation.PARAMETRO_RESET_CACHE_FROM_LISTA);
  163.                 boolean arrivoDaLista = "true".equalsIgnoreCase(resetFromLista);
  164.                
  165.                 if(arrivoDaLista) {
  166.                    
  167.                     // preparo lista
  168.                
  169.                     ConsoleSearch ricerca = (ConsoleSearch) ServletUtils.getSearchObjectFromSession(request, session, ConsoleSearch.class);

  170.                     String infoTypeA = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_TOKEN_POLICY_TIPOLOGIA_INFORMAZIONE);
  171.                     String infoTypeSession = ServletUtils.getObjectFromSession(request, session, String.class, ConfigurazioneCostanti.PARAMETRO_TOKEN_POLICY_TIPOLOGIA_INFORMAZIONE);
  172.                     if(infoTypeA==null) {
  173.                         infoTypeA = infoTypeSession;
  174.                     }
  175.                     else {
  176.                         ServletUtils.setObjectIntoSession(request, session, infoTypeA, ConfigurazioneCostanti.PARAMETRO_TOKEN_POLICY_TIPOLOGIA_INFORMAZIONE);
  177.                     }
  178.                     attributeAuthority = ConfigurazioneCostanti.isConfigurazioneAttributeAuthority(infoTypeA);
  179.                    
  180.                     // reset di eventuali configurazioni salvate in sessione
  181.                     mapId = attributeAuthority ?
  182.                             confCore.getAttributeAuthorityTipologia() :
  183.                             confCore.getTokenPolicyTipologia();
  184.                     if(mapId!=null && !mapId.isEmpty()) {
  185.                         propertiesSourceConfiguration = attributeAuthority ?
  186.                                 confCore.getAttributeAuthorityPropertiesSourceConfiguration() :
  187.                                 confCore.getPolicyGestioneTokenPropertiesSourceConfiguration();
  188.                         configManager.leggiConfigurazioni(propertiesSourceConfiguration, true);
  189.                         for (Object oTipo : mapId.keySet()) {
  190.                             if(oTipo instanceof String) {
  191.                                 String ti = (String) oTipo;
  192.                                 Config config = configManager.getConfigurazione(propertiesSourceConfiguration, ti);
  193.                                 ServletUtils.removeConfigurazioneBeanFromSession(request, session, config.getId());
  194.                             }
  195.                         }
  196.                     }
  197.                    
  198.                     int idLista = attributeAuthority ? Liste.CONFIGURAZIONE_GESTIONE_ATTRIBUTE_AUTHORITY : Liste.CONFIGURAZIONE_GESTIONE_POLICY_TOKEN;
  199.                    
  200.                     ricerca = confHelper.checkSearchParameters(idLista, ricerca);

  201.                     List<String> tipologie = new ArrayList<>();
  202.                     if(attributeAuthority) {
  203.                         tipologie.add(ConfigurazioneCostanti.DEFAULT_VALUE_PARAMETRO_CONFIGURAZIONE_GESTORE_POLICY_TOKEN_TIPOLOGIA_ATTRIBUTE_AUTHORITY);
  204.                     }
  205.                     else {
  206.                         tipologie.add(ConfigurazioneCostanti.DEFAULT_VALUE_PARAMETRO_CONFIGURAZIONE_GESTORE_POLICY_TOKEN_TIPOLOGIA_GESTIONE_POLICY_TOKEN);
  207.                         tipologie.add(ConfigurazioneCostanti.DEFAULT_VALUE_PARAMETRO_CONFIGURAZIONE_GESTORE_POLICY_TOKEN_TIPOLOGIA_RETRIEVE_POLICY_TOKEN);
  208.                     }
  209.                    
  210.                     List<GenericProperties> lista = confCore.gestorePolicyTokenList(idLista, tipologie, ricerca);
  211.                    
  212.                     confHelper.prepareGestorePolicyTokenList(ricerca, lista, idLista);
  213.                    
  214.                     // salvo l'oggetto ricerca nella sessione
  215.                     ServletUtils.setSearchObjectIntoSession(request, session, ricerca);
  216.                    
  217.                     ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);
  218.                
  219.                     // Forward control to the specified success URI
  220.                     return ServletUtils.getStrutsForwardEditModeFinished(mapping, ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_POLICY_GESTIONE_TOKEN, ForwardParams.CHANGE());
  221.                    
  222.                 }
  223.             }
  224.            
  225.             // setto la barra del titolo
  226.             List<Parameter> lstParam = new ArrayList<>();

  227.             String label = attributeAuthority ?
  228.                     ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_ATTRIBUTE_AUTHORITY :
  229.                     ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_POLICY_GESTIONE_TOKEN;
  230.            
  231.             lstParam.add(new Parameter(label, ConfigurazioneCostanti.SERVLET_NAME_CONFIGURAZIONE_POLICY_GESTIONE_TOKEN_LIST));
  232.             lstParam.add(new Parameter(genericProperties.getNome(),null));
  233.        
  234.             // Se tipo = null, devo visualizzare la pagina per l'inserimento
  235.             // dati
  236.             if (confHelper.isEditModeInProgress()) {
  237.                 ServletUtils.setPageDataTitle(pd, lstParam);
  238.                
  239.                 if(nome == null) {
  240.                     nome = genericProperties.getNome();
  241.                     descrizione = genericProperties.getDescrizione();
  242.                     tipo = genericProperties.getTipo();
  243.                 }
  244.                
  245.                 // preparo i campi
  246.                 List<DataElement> dati = new ArrayList<>();
  247.                 dati.add(ServletUtils.getDataElementForEditModeFinished());
  248.                
  249.                 dati = confHelper.addPolicyGestioneTokenToDati(tipoOperazione,dati,id,nome,descrizione,tipo,propConfigPolicyGestioneTokenLabelList,propConfigPolicyGestioneTokenList,
  250.                         attributeAuthority, genericProperties);
  251.                
  252.                 dati = confHelper.addPropertiesConfigToDati(tipoOperazione,dati, tipo, configurazioneBean,false);
  253.                
  254.                 pd.setDati(dati);

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

  256.                 return ServletUtils.getStrutsForwardEditModeInProgress(mapping, ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_POLICY_GESTIONE_TOKEN,ForwardParams.CHANGE());
  257.             }
  258.            
  259.             // Controlli sui campi immessi
  260.             String tipologia = mapId.getProperty(tipo);
  261.             boolean isOk = confHelper.policyGestioneTokenCheckData(tipoOperazione, nome,descrizione,tipo,tipologia);
  262.            
  263.             if (isOk) {
  264.                 isOk = confHelper.checkPropertiesConfigurationData(tipoOperazione, configurazioneBean, nome,descrizione,configurazione);
  265.             }
  266.             if (!isOk) {
  267.                
  268.                 ServletUtils.setPageDataTitle(pd, lstParam);
  269.                
  270.                 // preparo i campi
  271.                 List<DataElement> dati = new ArrayList<>();
  272.                 dati.add(ServletUtils.getDataElementForEditModeFinished());
  273.                
  274.                 dati = confHelper.addPolicyGestioneTokenToDati(tipoOperazione,dati,id,nome,descrizione,tipo,propConfigPolicyGestioneTokenLabelList,propConfigPolicyGestioneTokenList,
  275.                         attributeAuthority, genericProperties);
  276.                        
  277.                 dati = confHelper.addPropertiesConfigToDati(tipoOperazione,dati, tipo, configurazioneBean,false);
  278.                
  279.                 pd.setDati(dati);

  280.                 ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);
  281.                
  282.                 return ServletUtils.getStrutsForwardEditModeCheckError(mapping, ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_POLICY_GESTIONE_TOKEN, ForwardParams.CHANGE());
  283.             }
  284.            
  285.             // update sul db
  286.             GenericProperties policy = new GenericProperties();
  287.             policy.setNome(nome);
  288.             policy.setDescrizione(descrizione);
  289.             policy.setTipologia(tipologia);
  290.             policy.setTipo(tipo);
  291.             policy.setId(genericProperties.getId());
  292.            
  293.             policy.setProprietaOggetto(genericProperties.getProprietaOggetto());
  294.            
  295.             Map<String, Properties> mappaDestinazione = configurazioneBean.getPropertiesMap();  
  296.             Map<String, String> map = DBPropertiesUtils.toMap(mappaDestinazione);
  297.            
  298.             for (String propKey : map.keySet()) {
  299.                 Property property = new Property();
  300.                 property.setNome(propKey);
  301.                 property.setValore(map.get(propKey));
  302.                
  303.                 policy.getPropertyList().add(property);
  304.             }
  305.            
  306.             confCore.performUpdateOperation(userLogin, confHelper.smista(), policy);
  307.            
  308.             // Preparo la lista
  309.             ConsoleSearch ricerca = (ConsoleSearch) ServletUtils.getSearchObjectFromSession(request, session, ConsoleSearch.class);

  310.             int idLista = attributeAuthority ? Liste.CONFIGURAZIONE_GESTIONE_ATTRIBUTE_AUTHORITY : Liste.CONFIGURAZIONE_GESTIONE_POLICY_TOKEN;
  311.            
  312.             ricerca = confHelper.checkSearchParameters(idLista, ricerca);

  313.             List<String> tipologie = new ArrayList<>();
  314.             if(attributeAuthority) {
  315.                 tipologie.add(ConfigurazioneCostanti.DEFAULT_VALUE_PARAMETRO_CONFIGURAZIONE_GESTORE_POLICY_TOKEN_TIPOLOGIA_ATTRIBUTE_AUTHORITY);
  316.             }
  317.             else {
  318.                 tipologie.add(ConfigurazioneCostanti.DEFAULT_VALUE_PARAMETRO_CONFIGURAZIONE_GESTORE_POLICY_TOKEN_TIPOLOGIA_GESTIONE_POLICY_TOKEN);
  319.                 tipologie.add(ConfigurazioneCostanti.DEFAULT_VALUE_PARAMETRO_CONFIGURAZIONE_GESTORE_POLICY_TOKEN_TIPOLOGIA_RETRIEVE_POLICY_TOKEN);
  320.             }
  321.            
  322.             List<GenericProperties> lista = confCore.gestorePolicyTokenList(idLista, tipologie, ricerca);
  323.            
  324.             confHelper.prepareGestorePolicyTokenList(ricerca, lista, idLista);
  325.            
  326.             // reset di eventuali configurazioni salvate in sessione
  327.             ServletUtils.removeConfigurazioneBeanFromSession(request, session, configurazioneBean.getId());
  328.            
  329.             // salvo l'oggetto ricerca nella sessione
  330.             ServletUtils.setSearchObjectIntoSession(request, session, ricerca);
  331.             ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);
  332.            
  333.             // Forward control to the specified success URI
  334.             return ServletUtils.getStrutsForwardEditModeFinished(mapping, ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_POLICY_GESTIONE_TOKEN, ForwardParams.CHANGE());
  335.         } catch (Exception e) {
  336.             return ServletUtils.getStrutsForwardError(ControlStationCore.getLog(), e, pd, request, session, gd, mapping,
  337.                     ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_POLICY_GESTIONE_TOKEN, ForwardParams.CHANGE());
  338.         }  
  339.     }
  340. }