ConfigurazioneHandlersRichiestaAdd.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 javax.servlet.http.HttpServletRequest;
  25. import javax.servlet.http.HttpServletResponse;
  26. import javax.servlet.http.HttpSession;

  27. import org.apache.commons.lang.StringUtils;
  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.commons.Liste;
  33. import org.openspcoop2.core.config.Configurazione;
  34. import org.openspcoop2.core.config.ConfigurazioneGeneraleHandler;
  35. import org.openspcoop2.core.config.ConfigurazioneHandler;
  36. import org.openspcoop2.core.config.ConfigurazioneMessageHandlers;
  37. import org.openspcoop2.core.config.ConfigurazionePortaHandler;
  38. import org.openspcoop2.core.config.PortaApplicativa;
  39. import org.openspcoop2.core.config.PortaDelegata;
  40. import org.openspcoop2.core.config.constants.FaseMessageHandler;
  41. import org.openspcoop2.core.config.constants.StatoFunzionalita;
  42. import org.openspcoop2.core.constants.TipoPdD;
  43. import org.openspcoop2.core.plugins.Plugin;
  44. import org.openspcoop2.core.plugins.constants.TipoPlugin;
  45. import org.openspcoop2.core.plugins.utils.handlers.ConfigurazioneHandlerBean;
  46. import org.openspcoop2.message.constants.ServiceBinding;
  47. import org.openspcoop2.web.ctrlstat.core.ControlStationCore;
  48. import org.openspcoop2.web.ctrlstat.core.ConsoleSearch;
  49. import org.openspcoop2.web.ctrlstat.servlet.GeneralHelper;
  50. import org.openspcoop2.web.ctrlstat.servlet.pa.PorteApplicativeCore;
  51. import org.openspcoop2.web.ctrlstat.servlet.pd.PorteDelegateCore;
  52. import org.openspcoop2.web.lib.mvc.Costanti;
  53. import org.openspcoop2.web.lib.mvc.DataElement;
  54. import org.openspcoop2.web.lib.mvc.ForwardParams;
  55. import org.openspcoop2.web.lib.mvc.GeneralData;
  56. import org.openspcoop2.web.lib.mvc.PageData;
  57. import org.openspcoop2.web.lib.mvc.Parameter;
  58. import org.openspcoop2.web.lib.mvc.ServletUtils;
  59. import org.openspcoop2.web.lib.mvc.TipoOperazione;

  60. /**    
  61.  * ConfigurazioneHandlersRichiestaAdd
  62.  *
  63.  * @author Pintori Giuliano (pintori@link.it)
  64.  * @author $Author$
  65.  * @version $Rev$, $Date$
  66.  */
  67. public class ConfigurazioneHandlersRichiestaAdd extends Action {

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

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

  71.         // Inizializzo PageData
  72.         PageData pd = new PageData();

  73.         GeneralHelper generalHelper = new GeneralHelper(session);

  74.         // Inizializzo GeneralData
  75.         GeneralData gd = generalHelper.initGeneralData(request);

  76.         String userLogin = ServletUtils.getUserLoginFromSession(session);  
  77.        
  78.         TipoOperazione tipoOperazione = TipoOperazione.ADD;
  79.        
  80.         try {
  81.             ConfigurazioneHelper confHelper = new ConfigurazioneHelper(request, pd, session);
  82.            
  83.             // controllo primo accesso
  84.             boolean first = confHelper.isFirstTimeFromHttpParameters(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_HANDLERS_FIRST_TIME);
  85.                    
  86.             String ruoloPortaParam = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_HANDLERS_RUOLO_PORTA);
  87.             TipoPdD ruoloPorta = null;
  88.             if(ruoloPortaParam!=null) {
  89.                 ruoloPorta = TipoPdD.toTipoPdD(ruoloPortaParam);
  90.             }
  91.             String idPortaS = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_HANDLERS_ID_PORTA);
  92.             Long idPorta = null;
  93.             if(StringUtils.isNotBlank(idPortaS)) {
  94.                 idPorta = Long.parseLong(idPortaS);
  95.             }
  96.            
  97.             ServiceBinding serviceBinding = null;
  98.             String serviceBindingParam = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_HANDLERS_SERVICE_BINDING);
  99.             if(serviceBindingParam!=null && !"".equals(serviceBindingParam)) {
  100.                 serviceBinding = ServiceBinding.valueOf(serviceBindingParam);
  101.             }
  102.             String nomePlugin = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_HANDLERS_PLUGIN);
  103.             String fase = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_HANDLERS_FASE);
  104.             String stato = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_HANDLERS_STATO);
  105.            
  106.             FaseMessageHandler faseMH = FaseMessageHandler.toEnumConstant(fase);
  107.            
  108.             ConfigurazioneCore confCore = new ConfigurazioneCore();
  109.            
  110.             List<String> tipiPluginGiaUtilizzati = null;
  111.             ConsoleSearch ricercaPluginUilizzati = new ConsoleSearch(true);
  112.             List<ConfigurazioneHandlerBean> listaEsistenti = confCore.handlersRichiestaList(ricercaPluginUilizzati, fase, ruoloPorta, idPorta);
  113.             if(listaEsistenti != null && !listaEsistenti.isEmpty()) {
  114.                 tipiPluginGiaUtilizzati = new ArrayList<>();
  115.                
  116.                 for (ConfigurazioneHandlerBean configurazioneHandlerBean : listaEsistenti) {
  117.                     tipiPluginGiaUtilizzati.add(configurazioneHandlerBean.getPlugin().getTipo());
  118.                 }
  119.             }
  120.            
  121.            
  122.             // Preparo il menu
  123.             confHelper.makeMenu();
  124.                        
  125.             List<Parameter> lstParamSession = new ArrayList<>();

  126.             Parameter parRuoloPorta = null;
  127.             if(ruoloPorta!=null) {
  128.                 parRuoloPorta = new Parameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_HANDLERS_RUOLO_PORTA, ruoloPorta.getTipo());
  129.                 lstParamSession.add(parRuoloPorta);
  130.             }
  131.             Parameter parIdPorta = null;
  132.             if(idPortaS!=null) {
  133.                 parIdPorta = new Parameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_HANDLERS_ID_PORTA, idPortaS);
  134.                 lstParamSession.add(parIdPorta);
  135.             }
  136.             Parameter parServiceBinding = null;
  137.             if(serviceBinding!=null) {
  138.                 parServiceBinding = new Parameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_HANDLERS_SERVICE_BINDING, serviceBinding.name());
  139.                 lstParamSession.add(parServiceBinding);
  140.             }
  141.             Parameter parTipologia = null;
  142.             if(fase!=null) {
  143.                 parTipologia = new Parameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_HANDLERS_FASE, fase);
  144.                 lstParamSession.add(parTipologia);
  145.             }
  146.            
  147.             String labelHandler = confHelper.getLabelTipologiaFromFaseMessageHandler(fase,true);
  148.             String labelHandlerDi = labelHandler + " di ";
  149.             String servletListURL = ConfigurazioneCostanti.SERVLET_NAME_CONFIGURAZIONE_HANDLERS_RICHIESTA_LIST;
  150.             String messaggioValoriNonDisponibili = MessageFormat.format(ConfigurazioneCostanti.LABEL_HANDLER_PLUGIN_NON_DISPONIBILI_PER_LA_FASE, labelHandler);
  151.             String messaggioHandlerRichiestaDuplicato = null;
  152.             String tipo = null;
  153.             Plugin plugin = null;
  154.            
  155.             if(nomePlugin != null) {
  156.                 plugin = confCore.getPlugin(TipoPlugin.MESSAGE_HANDLER, nomePlugin, false);
  157.             }
  158.            
  159.             if(plugin != null) {
  160.                 messaggioHandlerRichiestaDuplicato = MessageFormat.format(ConfigurazioneCostanti.MESSAGGIO_HANDLER_RICHIESTA_DUPLICATO, labelHandler, plugin.getLabel());
  161.                 tipo = plugin.getTipo();
  162.             }
  163.            
  164.             List<Parameter> lstParamPorta = null;
  165.             if(ruoloPorta!=null) {
  166.                 lstParamPorta = confHelper.getTitleListHandler(fase, ruoloPorta, idPortaS, serviceBinding, Costanti.PAGE_DATA_TITLE_LABEL_AGGIUNGI,
  167.                         servletListURL, labelHandlerDi, labelHandler);
  168.             }
  169.            
  170.             // setto la barra del titolo
  171.             List<Parameter> lstParam = null;
  172.             if(lstParamPorta!=null) {
  173.                 lstParam = lstParamPorta;
  174.             }
  175.             else {
  176.                 lstParam = new ArrayList<>();
  177.                
  178.                 if(!lstParamSession.isEmpty()) {
  179.                     lstParam.add(new Parameter(labelHandler, servletListURL, lstParamSession.toArray(new Parameter[lstParamSession.size()])));
  180.                 } else {
  181.                     lstParam.add(new Parameter(labelHandler, servletListURL));
  182.                 }
  183.                 lstParam.add(ServletUtils.getParameterAggiungi());
  184.             }
  185.            
  186.             if(ruoloPorta == null) {
  187.                 lstParam.add(0,new Parameter(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_GENERALE, ConfigurazioneCostanti.SERVLET_NAME_CONFIGURAZIONE_GENERALE));
  188.             }
  189.            
  190.             // Se tipo = null, devo visualizzare la pagina per l'inserimento
  191.             // dati
  192.             if (confHelper.isEditModeInProgress()) {
  193.                 ServletUtils.setPageDataTitle(pd, lstParam);
  194.                
  195.                 if(first) {
  196.                     nomePlugin = "";
  197.                     stato = StatoFunzionalita.ABILITATO.getValue();
  198.                 }
  199.                
  200.                 // preparo i campi
  201.                 List<DataElement> dati = new ArrayList<>();
  202.                 dati.add(ServletUtils.getDataElementForEditModeFinished());
  203.                
  204.                 confHelper.addHandlerRichiestaToDati(dati, tipoOperazione, null, nomePlugin, stato, ruoloPorta, idPortaS, serviceBinding, fase, tipiPluginGiaUtilizzati, messaggioValoriNonDisponibili);
  205.                
  206.                 if(pd.getMessage() != null && pd.isDisableEditMode()) {
  207.                     dati = new ArrayList<>();
  208.                 }
  209.                
  210.                 // Set First is false
  211.                 confHelper.addToDatiFirstTimeDisabled(dati,ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_HANDLERS_FIRST_TIME);
  212.                
  213.                 pd.setDati(dati);

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

  215.                 return ServletUtils.getStrutsForwardEditModeInProgress(mapping, ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_HANDLERS_RICHIESTA, ForwardParams.ADD());
  216.             }
  217.            
  218.             // Controlli sui campi immessi
  219.             boolean isOk = confHelper.handlerRichiestaCheckData(TipoOperazione.ADD, null, nomePlugin, stato, ruoloPorta, idPorta, fase, tipo, messaggioHandlerRichiestaDuplicato);
  220.             if (!isOk) {
  221.                 ServletUtils.setPageDataTitle(pd, lstParam);
  222.                
  223.                 // preparo i campi
  224.                 List<DataElement> dati = new ArrayList<>();
  225.                 dati.add(ServletUtils.getDataElementForEditModeFinished());
  226.                
  227.                 confHelper.addHandlerRichiestaToDati(dati, tipoOperazione, null, nomePlugin, stato, ruoloPorta, idPortaS, serviceBinding, fase, tipiPluginGiaUtilizzati, messaggioValoriNonDisponibili);
  228.                
  229.                 // Set First is false
  230.                 confHelper.addToDatiFirstTimeDisabled(dati,ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_HANDLERS_FIRST_TIME);
  231.                
  232.                 pd.setDati(dati);

  233.                 ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);
  234.                
  235.                 return ServletUtils.getStrutsForwardEditModeCheckError(mapping,
  236.                         ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_HANDLERS_RICHIESTA,
  237.                         ForwardParams.ADD());
  238.             }
  239.            
  240.             ConfigurazioneHandler handler = new ConfigurazioneHandler();
  241.            
  242.             handler.setTipo(plugin.getTipo());
  243.             handler.setStato(StatoFunzionalita.toEnumConstant(stato));
  244.             int posizione = confCore.getMaxPosizioneHandlersRichiesta(fase,ruoloPorta,idPorta) + 1;
  245.             handler.setPosizione(posizione);
  246.            
  247.             List<Object> oggettiDaAggiornare = new ArrayList<>();
  248.             if(ruoloPorta !=null) {
  249.                 if(ruoloPorta.equals(TipoPdD.DELEGATA)) {
  250.                     PorteDelegateCore porteDelegateCore = new PorteDelegateCore(confCore);
  251.                     PortaDelegata portaDelegata = porteDelegateCore.getPortaDelegata(idPorta);
  252.                    
  253.                     if(portaDelegata.getConfigurazioneHandler() == null)
  254.                         portaDelegata.setConfigurazioneHandler(new ConfigurazionePortaHandler());
  255.                    
  256.                     if(portaDelegata.getConfigurazioneHandler().getRequest() == null)
  257.                         portaDelegata.getConfigurazioneHandler().setRequest(new ConfigurazioneMessageHandlers());
  258.                    
  259.                    
  260.                     switch (faseMH) {
  261.                     case IN:
  262.                         portaDelegata.getConfigurazioneHandler().getRequest().addIn(handler);
  263.                         break;
  264.                     case IN_PROTOCOL_INFO:
  265.                         portaDelegata.getConfigurazioneHandler().getRequest().addInProtocolInfo(handler);
  266.                         break;
  267.                     case OUT:
  268.                         portaDelegata.getConfigurazioneHandler().getRequest().addOut(handler);
  269.                         break;
  270.                     case POST_OUT:
  271.                         portaDelegata.getConfigurazioneHandler().getRequest().addPostOut(handler);
  272.                         break;
  273.                     case PRE_IN:
  274.                         portaDelegata.getConfigurazioneHandler().getRequest().addPreIn(handler);
  275.                         break;
  276.                     }
  277.                     oggettiDaAggiornare.add(portaDelegata);
  278.                 }
  279.                 else if(ruoloPorta.equals(TipoPdD.APPLICATIVA)) {
  280.                     PorteApplicativeCore porteApplicativeCore = new PorteApplicativeCore(confCore);
  281.                     PortaApplicativa portaApplicativa = porteApplicativeCore.getPortaApplicativa(idPorta);
  282.                    
  283.                     if(portaApplicativa.getConfigurazioneHandler() == null)
  284.                         portaApplicativa.setConfigurazioneHandler(new ConfigurazionePortaHandler());
  285.                    
  286.                     if(portaApplicativa.getConfigurazioneHandler().getRequest() == null)
  287.                         portaApplicativa.getConfigurazioneHandler().setRequest(new ConfigurazioneMessageHandlers());
  288.                    
  289.                     switch (faseMH) {
  290.                     case IN:
  291.                         portaApplicativa.getConfigurazioneHandler().getRequest().addIn(handler);
  292.                         break;
  293.                     case IN_PROTOCOL_INFO:
  294.                         portaApplicativa.getConfigurazioneHandler().getRequest().addInProtocolInfo(handler);
  295.                         break;
  296.                     case OUT:
  297.                         portaApplicativa.getConfigurazioneHandler().getRequest().addOut(handler);
  298.                         break;
  299.                     case POST_OUT:
  300.                         portaApplicativa.getConfigurazioneHandler().getRequest().addPostOut(handler);
  301.                         break;
  302.                     case PRE_IN:
  303.                         portaApplicativa.getConfigurazioneHandler().getRequest().addPreIn(handler);
  304.                         break;
  305.                     }
  306.                     oggettiDaAggiornare.add(portaApplicativa);
  307.                 }
  308.             } else {
  309.                 Configurazione configurazione = confCore.getConfigurazioneGenerale();
  310.                
  311.                 if(configurazione.getConfigurazioneHandler() == null)
  312.                     configurazione.setConfigurazioneHandler(new ConfigurazioneGeneraleHandler());
  313.                
  314.                 if(configurazione.getConfigurazioneHandler().getRequest() == null)
  315.                     configurazione.getConfigurazioneHandler().setRequest(new ConfigurazioneMessageHandlers());
  316.                
  317.                 switch (faseMH) {
  318.                 case IN:
  319.                     configurazione.getConfigurazioneHandler().getRequest().addIn(handler);
  320.                     break;
  321.                 case IN_PROTOCOL_INFO:
  322.                     configurazione.getConfigurazioneHandler().getRequest().addInProtocolInfo(handler);
  323.                     break;
  324.                 case OUT:
  325.                     configurazione.getConfigurazioneHandler().getRequest().addOut(handler);
  326.                     break;
  327.                 case POST_OUT:
  328.                     configurazione.getConfigurazioneHandler().getRequest().addPostOut(handler);
  329.                     break;
  330.                 case PRE_IN:
  331.                     configurazione.getConfigurazioneHandler().getRequest().addPreIn(handler);
  332.                     break;
  333.                 }
  334.                 oggettiDaAggiornare.add(configurazione);
  335.             }
  336.            
  337.             // insert sul db
  338.             confCore.performUpdateOperation(userLogin, confHelper.smista(), oggettiDaAggiornare.toArray(new Object[oggettiDaAggiornare.size()]));
  339.            
  340.             // Preparo la lista
  341.             int idLista = Liste.CONFIGURAZIONE_HANDLERS_RICHIESTA;
  342.            
  343.             ConsoleSearch ricerca = (ConsoleSearch) ServletUtils.getSearchObjectFromSession(request, session, ConsoleSearch.class);
  344.            
  345.             ricerca = confHelper.checkSearchParameters(idLista, ricerca);

  346.             List<ConfigurazioneHandlerBean> lista = confCore.handlersRichiestaList(ricerca, fase, ruoloPorta, idPorta);
  347.            
  348.             confHelper.prepareHandlersRichiestaList(ricerca, lista, ruoloPorta, idPortaS, serviceBinding, fase);
  349.            
  350.             if(ruoloPorta == null) {
  351.                 pd.setMessage(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_MESSAGE_HANDLERS_MODIFICATA_CON_SUCCESSO, Costanti.MESSAGE_TYPE_INFO);
  352.             }
  353.            
  354.             // salvo l'oggetto ricerca nella sessione
  355.             ServletUtils.setSearchObjectIntoSession(request, session, ricerca);
  356.             ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);
  357.            
  358.             // Forward control to the specified success URI
  359.             return ServletUtils.getStrutsForwardEditModeFinished(mapping, ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_HANDLERS_RICHIESTA, ForwardParams.ADD());
  360.         } catch (Exception e) {
  361.             return ServletUtils.getStrutsForwardError(ControlStationCore.getLog(), e, pd, request, session, gd, mapping,
  362.                     ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_HANDLERS_RICHIESTA, ForwardParams.ADD());
  363.         }  
  364.     }
  365. }