PddChange.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.pdd;

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

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

  28. import org.apache.struts.action.Action;
  29. import org.apache.struts.action.ActionForm;
  30. import org.apache.struts.action.ActionForward;
  31. import org.apache.struts.action.ActionMapping;
  32. import org.openspcoop2.core.constants.CostantiDB;
  33. import org.openspcoop2.core.constants.TipiConnettore;
  34. import org.openspcoop2.core.registry.Connettore;
  35. import org.openspcoop2.core.registry.Soggetto;
  36. import org.openspcoop2.core.registry.constants.PddTipologia;
  37. import org.openspcoop2.core.registry.constants.StatoFunzionalita;
  38. import org.openspcoop2.web.ctrlstat.core.ConsoleSearch;
  39. import org.openspcoop2.web.ctrlstat.core.ControlStationCore;
  40. import org.openspcoop2.web.ctrlstat.core.ControlStationLogger;
  41. import org.openspcoop2.web.ctrlstat.costanti.CostantiControlStation;
  42. import org.openspcoop2.web.ctrlstat.dao.PdDControlStation;
  43. import org.openspcoop2.web.ctrlstat.dao.SoggettoCtrlStat;
  44. import org.openspcoop2.web.ctrlstat.servlet.GeneralHelper;
  45. import org.openspcoop2.web.ctrlstat.servlet.soggetti.SoggettiCore;
  46. import org.openspcoop2.web.lib.mvc.Costanti;
  47. import org.openspcoop2.web.lib.mvc.DataElement;
  48. import org.openspcoop2.web.lib.mvc.ForwardParams;
  49. import org.openspcoop2.web.lib.mvc.GeneralData;
  50. import org.openspcoop2.web.lib.mvc.PageData;
  51. import org.openspcoop2.web.lib.mvc.ServletUtils;
  52. import org.openspcoop2.web.lib.mvc.TipoOperazione;

  53. /**
  54.  * pddChange
  55.  *
  56.  * @author Andrea Poli (apoli@link.it)
  57.  * @author Stefano Corallo (corallo@link.it)
  58.  * @author Sandra Giangrandi (sandra@link.it)
  59.  * @author $Author$
  60.  * @version $Rev$, $Date$
  61.  *
  62.  */
  63. public final class PddChange extends Action {

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

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

  68.         // Inizializzo PageData
  69.         PageData pd = new PageData();

  70.         GeneralHelper generalHelper = new GeneralHelper(session);

  71.         // Inizializzo GeneralData
  72.         GeneralData gd = generalHelper.initGeneralData(request);

  73.         try {
  74.             PddHelper pddHelper = new PddHelper(request, pd, session);
  75.             PddCore pddCore = new PddCore();
  76.             SoggettiCore soggettiCore = new SoggettiCore(pddCore);
  77.            
  78.             String id = pddHelper.getParameter(PddCostanti.PARAMETRO_PDD_ID);
  79.             int idPdd = Integer.parseInt(id);
  80.             String tipo = pddHelper.getParameter(PddCostanti.PARAMETRO_PDD_TIPOLOGIA);
  81.             String descrizione = pddHelper.getParameter(PddCostanti.PARAMETRO_PDD_DESCRIZIONE);
  82.             String ip = pddHelper.getParameter(PddCostanti.PARAMETRO_PDD_INDIRIZZO_PUBBLICO);
  83.             String ipGestione = pddHelper.getParameter(PddCostanti.PARAMETRO_PDD_INDIRIZZO_GESTIONE);
  84.             String porta = pddHelper.getParameter(PddCostanti.PARAMETRO_PDD_PORTA_PUBBLICA);
  85.             String portaGestione = pddHelper.getParameter(PddCostanti.PARAMETRO_PDD_PORTA_GESTIONE);
  86.             String implementazione = pddHelper.getParameter(PddCostanti.PARAMETRO_PDD_IMPLEMENTAZIONE);
  87.             String subject = pddHelper.getParameter(PddCostanti.PARAMETRO_PDD_SUBJECT);
  88.             String clientAuth = pddHelper.getParameter(PddCostanti.PARAMETRO_PDD_CLIENT_AUTH);
  89.             String protocollo = pddHelper.getParameter(PddCostanti.PARAMETRO_PDD_PROTOCOLLO);
  90.             String protocolloGestione = pddHelper.getParameter(PddCostanti.PARAMETRO_PDD_PROTOCOLLO_GESTIONE);
  91.            
  92.             int portaInt = 0;
  93.             int portaGestioneInt = 0;
  94.             try {
  95.                 portaInt = Integer.parseInt(porta);
  96.             } catch (NumberFormatException e) {
  97.                 portaInt = pddCore.getPortaPubblica();// porta default
  98.             }
  99.             try {
  100.                 portaGestioneInt = Integer.parseInt(portaGestione);
  101.             } catch (NumberFormatException e) {
  102.                 portaGestioneInt = pddCore.getPortaGestione();// porta di default
  103.             }
  104.            
  105.             // Protocolli supportati
  106.            
  107.             String oldTipo = "";
  108.             String oldProtocollo = "";
  109.             String oldIp = "";
  110.             int oldPortaGestione = 0;
  111.             int oldPorta = 0;
  112.             @SuppressWarnings("unused")
  113.             String oldProtocolloGestione = "";
  114.             // Preparo il menu
  115.             pddHelper.makeMenu();
  116.    
  117.             String nomePdd = "";
  118.             PdDControlStation pdd = null;
  119.             try {
  120.    
  121.                 pdd = pddCore.getPdDControlStation(idPdd);
  122.                 nomePdd = pdd.getNome();
  123.                 oldTipo = pdd.getTipo();
  124.                 oldProtocollo = pdd.getProtocollo();
  125.                 oldPorta = pdd.getPorta();
  126.                 oldIp = pdd.getIp();
  127.                 oldProtocolloGestione = pdd.getProtocolloGestione();
  128.                 oldPortaGestione = pdd.getPortaGestione();
  129.             } catch (Exception ex) {
  130.                 return ServletUtils.getStrutsForwardError(ControlStationCore.getLog(), ex, pd, request, session, gd, mapping,
  131.                         PddCostanti.OBJECT_NAME_PDD, ForwardParams.CHANGE());
  132.             }
  133.    
  134.             // Se nomehid = null, devo visualizzare la pagina per la modifica dati
  135.             if(pddHelper.isEditModeInProgress()){
  136.                
  137.                 // setto la barra del titolo
  138.                 ServletUtils.setPageDataTitle_ServletChange(pd, PddCostanti.LABEL_PORTE_DI_DOMINIO,
  139.                         PddCostanti.SERVLET_NAME_PDD_LIST,nomePdd);
  140.    
  141.                 if (descrizione == null) {
  142.                     ip = pdd.getIp();
  143.                     tipo = pdd.getTipo();
  144.                     protocollo = pdd.getProtocollo();
  145.                     protocolloGestione = pdd.getProtocolloGestione();
  146.                     subject = pdd.getSubject();
  147.                     ipGestione = pdd.getIpGestione();
  148.                     descrizione = pdd.getDescrizione();
  149.                     if(pdd.getClientAuth()!=null)
  150.                         clientAuth = pdd.getClientAuth().toString();
  151.                     implementazione = pdd.getImplementazione();
  152.                 }
  153.    
  154.                 // preparo i campi
  155.                 List<DataElement> dati = new ArrayList<>();
  156.    
  157.                 dati.add(ServletUtils.getDataElementForEditModeFinished());
  158.    
  159.                 int portaPUBBLICA = oldPorta;
  160.                 int portaGESTIONE = oldPortaGestione;
  161.                 if(porta!=null && !"".equals(porta)){
  162.                     try{
  163.                         portaPUBBLICA = Integer.parseInt(porta);
  164.                     }catch(Exception e){
  165.                         // ignore
  166.                     }
  167.                 }
  168.                 if(portaGestione!=null && !"".equals(portaGestione)){
  169.                     try{
  170.                         portaGESTIONE = Integer.parseInt(portaGestione);
  171.                     }catch(Exception e){
  172.                         // ignore
  173.                     }
  174.                 }
  175.                
  176.                 dati = pddHelper.addPddToDati(dati, nomePdd, id,
  177.                         ip, subject, ""/* password */, "" /* password */,
  178.                         PddTipologia.toPddTipologia(tipo), TipoOperazione.CHANGE,
  179.                         PddCostanti.getDefaultPddProtocolli(), protocollo, protocolloGestione,
  180.                         portaPUBBLICA, descrizione, ipGestione, portaGESTIONE, implementazione, clientAuth, false);
  181.    
  182.                 pd.setDati(dati);
  183.    
  184.                 ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);

  185.                 return ServletUtils.getStrutsForwardEditModeInProgress(mapping, PddCostanti.OBJECT_NAME_PDD, ForwardParams.CHANGE());
  186.             }
  187.    
  188.             // Controlli sui campi immessi
  189.             boolean isOk = pddHelper.pddCheckData(TipoOperazione.CHANGE, false);
  190.             if (!isOk) {
  191.                
  192.                 // setto la barra del titolo
  193.                 ServletUtils.setPageDataTitle_ServletChange(pd, PddCostanti.LABEL_PORTE_DI_DOMINIO,
  194.                         PddCostanti.SERVLET_NAME_PDD_LIST,nomePdd);
  195.    
  196.                 // preparo i campi
  197.                 List<DataElement> dati = new ArrayList<>();
  198.    
  199.                 dati.add(ServletUtils.getDataElementForEditModeFinished());
  200.    
  201.                 dati = pddHelper.addPddToDati(dati, nomePdd, id,
  202.                         ip, subject, "" /* password */, "" /* confpw */,
  203.                         PddTipologia.toPddTipologia(tipo), TipoOperazione.CHANGE,
  204.                         PddCostanti.getDefaultPddProtocolli(), protocollo, protocolloGestione,  
  205.                         portaInt, descrizione, ipGestione, portaGestioneInt, implementazione, clientAuth, false);
  206.    
  207.                 pd.setDati(dati);
  208.    
  209.                 ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);
  210.                
  211.                 return ServletUtils.getStrutsForwardEditModeCheckError(mapping, PddCostanti.OBJECT_NAME_PDD, ForwardParams.CHANGE());
  212.                
  213.             }
  214.    
  215.             // Modifico i dati del pdd nel db
  216.             pdd.setTipo(tipo);
  217.             if(subject!=null && !"".equals(subject))
  218.                 pdd.setSubject(subject);
  219.             else
  220.                 pdd.setSubject(null);
  221.             pdd.setDescrizione(descrizione);
  222.             pdd.setImplementazione(implementazione);
  223.             pdd.setClientAuth(StatoFunzionalita.toEnumConstant(clientAuth));
  224.             if (tipo.equals(PddTipologia.OPERATIVO.toString()) || tipo.equals(PddTipologia.NONOPERATIVO.toString())) {
  225.                 pdd.setIp(ip);
  226.                 pdd.setProtocollo(protocollo);
  227.                 pdd.setPorta(portaInt);
  228.                 pdd.setIpGestione(ipGestione);
  229.                 pdd.setPortaGestione(portaGestioneInt);
  230.                 pdd.setProtocolloGestione(protocolloGestione);
  231.             }

  232.             String userLogin = ServletUtils.getUserLoginFromSession(session);

  233.             // se e' cambiato il protocollo, l'ip o la porta
  234.             // devo modificare le informazioni del connettore automatico del
  235.             // soggetto
  236.             if (!tipo.equals(PddTipologia.ESTERNO.toString()) && (!protocollo.equals(oldProtocollo) || oldPorta != portaInt || !ip.equals(oldIp))) {
  237.                 List<SoggettoCtrlStat> listaSoggetti = pddCore.soggettiWithServer(pdd.getNome());
  238.                 for (SoggettoCtrlStat soggetto : listaSoggetti) {
  239.                     Soggetto soggReg = soggetto.getSoggettoReg();
  240.                     Connettore connettore = soggReg.getConnettore();
  241.                     // Se il connettore e' http allora devo effettuare la
  242.                     // modifica
  243.                     if (TipiConnettore.HTTP.getNome().equals(connettore.getTipo())) {

  244.                         // Suffisso connettore
  245.                         String msg = "[console] suffissoConnettoreAutomatico impostato al valore: " + pddCore.getSuffissoConnettoreAutomatico();
  246.                         ControlStationLogger.getPddConsoleCoreLogger().info(msg);

  247.                         Map<String, String> properties = connettore.getProperties();
  248.                         String oldHttpLocation = properties.get(CostantiDB.CONNETTORE_HTTP_LOCATION);
  249.                         // se il connettore e' quello creato automaticamente
  250.                         // allora effettuo le modifiche
  251.                         String suffisso = pddCore.getSuffissoConnettoreAutomatico();
  252.                         suffisso = suffisso.replace(CostantiControlStation.PLACEHOLDER_SOGGETTO_ENDPOINT_CREAZIONE_AUTOMATICA,
  253.                                         soggettiCore.getWebContextProtocolAssociatoTipoSoggetto(soggReg.getTipo()));
  254.                         if (oldHttpLocation.equals(oldProtocollo + "://" + oldIp + ":" + oldPorta + "/" + suffisso)) {
  255.                             String newHttpLocation = pdd.getProtocollo() + "://" + pdd.getIp() + ":" + pdd.getPorta() + "/" + suffisso;
  256.                             Map<String, String> newprop = new HashMap<>();
  257.                             newprop.put(CostantiDB.CONNETTORE_HTTP_LOCATION, newHttpLocation);
  258.                             connettore.setProperties(newprop);

  259.                             // scrive lo modifiche sul soggetto
  260.                             pddCore.performUpdateOperation(userLogin, pddHelper.smista(), soggetto);
  261.                         }

  262.                     }
  263.                 }

  264.             }

  265.             // Se passo da non-operativo ad operativo
  266.             // avvio la sincronizzazione della Porta Di Dominio
  267.             if (oldTipo.equals(PddTipologia.NONOPERATIVO.toString()) && tipo.equals(PddTipologia.OPERATIVO.toString()) &&
  268.                 pddCore.isSincronizzazionePddEngineEnabled()) {
  269.                 try {
  270.                     ControlStationCore.logInfo("Avvio sincronizzazione causa passaggio Pdd non-operativo->operativo su Pdd [" + pdd.getNome() + "] da parte dell'utente [" + userLogin + "]");
  271.                     pd.setMessage("Sincronizzazione Porta di Dominio " + pdd.getNome() + " Effettuata correttamente.",Costanti.MESSAGE_TYPE_INFO);

  272.                 } catch (Exception e) {
  273.                     pd.setMessage("Sincronizzazione Porta di Dominio " + pdd.getNome() + " Non effettuata a causa di errori");
  274.                     return ServletUtils.getStrutsForwardGeneralError(mapping, PddCostanti.OBJECT_NAME_PDD, ForwardParams.CHANGE());
  275.                 }
  276.             }
  277.             // se tutto e' andato a buon fine scrivo le modifiche sulla pdd
  278.             pddCore.performUpdateOperation(userLogin, pddHelper.smista(), pdd);

  279.             ConsoleSearch ricerca = (ConsoleSearch) ServletUtils.getSearchObjectFromSession(request, session,ConsoleSearch.class);

  280.             List<PdDControlStation> lista = null;
  281.             if(pddCore.isVisioneOggettiGlobale(userLogin)){
  282.                 lista = pddCore.pddList(null, ricerca);
  283.             }else{
  284.                 lista = pddCore.pddList(userLogin, ricerca);
  285.             }

  286.             pddHelper.preparePddList(lista, ricerca);

  287.             ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);
  288.            
  289.             // Forward control to the specified success URI
  290.             return ServletUtils.getStrutsForwardEditModeFinished(mapping, PddCostanti.OBJECT_NAME_PDD, ForwardParams.CHANGE());

  291.         } catch (Exception e) {
  292.             return ServletUtils.getStrutsForwardError(ControlStationCore.getLog(), e, pd, request, session, gd, mapping,
  293.                     PddCostanti.OBJECT_NAME_PDD, ForwardParams.CHANGE());
  294.         }
  295.     }
  296. }