ConfigurazioneProxyPassRegolaAdd.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.List;

  23. import javax.servlet.http.HttpServletRequest;
  24. import javax.servlet.http.HttpServletResponse;
  25. import javax.servlet.http.HttpSession;

  26. import org.apache.commons.lang.StringUtils;
  27. import org.apache.struts.action.Action;
  28. import org.apache.struts.action.ActionForm;
  29. import org.apache.struts.action.ActionForward;
  30. import org.apache.struts.action.ActionMapping;
  31. import org.openspcoop2.core.commons.Liste;
  32. import org.openspcoop2.core.config.Configurazione;
  33. import org.openspcoop2.core.config.ConfigurazioneMultitenant;
  34. import org.openspcoop2.core.config.ConfigurazioneUrlInvocazione;
  35. import org.openspcoop2.core.config.ConfigurazioneUrlInvocazioneRegola;
  36. import org.openspcoop2.core.config.IdSoggetto;
  37. import org.openspcoop2.core.config.constants.RuoloContesto;
  38. import org.openspcoop2.core.config.constants.ServiceBinding;
  39. import org.openspcoop2.core.config.constants.StatoFunzionalita;
  40. import org.openspcoop2.core.id.IDSoggetto;
  41. import org.openspcoop2.web.ctrlstat.core.ControlStationCore;
  42. import org.openspcoop2.web.ctrlstat.core.ConsoleSearch;
  43. import org.openspcoop2.web.ctrlstat.costanti.CostantiControlStation;
  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.Parameter;
  52. import org.openspcoop2.web.lib.mvc.ServletUtils;
  53. import org.openspcoop2.web.lib.mvc.TipoOperazione;

  54. /**
  55.  * ConfigurazioneProxyPassRegolaAdd
  56.  *
  57.  * @author Giuliano Pintori (pintori@link.it)
  58.  * @author $Author$
  59.  * @version $Rev$, $Date$
  60.  *
  61.  */
  62. public final class ConfigurazioneProxyPassRegolaAdd extends Action {

  63.    

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

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

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

  69.         GeneralHelper generalHelper = new GeneralHelper(session);

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

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

  73.         try {
  74.             ConfigurazioneHelper confHelper = new ConfigurazioneHelper(request, pd, session);
  75.            
  76.             String idRegolaS = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_PROXY_PASS_ID_REGOLA);
  77.             String nome = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_PROXY_PASS_REGOLA_NOME);
  78.             String descrizione = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_PROXY_PASS_REGOLA_DESCRIZIONE);
  79.             String stato = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_PROXY_PASS_REGOLA_STATO);
  80.             String regExprS = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_PROXY_PASS_REGOLA_REG_EXPR);
  81.             boolean regExpr = regExprS != null ? ServletUtils.isCheckBoxEnabled(regExprS) : false;
  82.             String regolaText = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_PROXY_PASS_REGOLA_REGOLA_TEXT);
  83.             String contestoEsterno = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_PROXY_PASS_REGOLA_CONTESTO_ESTERNO);
  84.             String baseUrl = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_PROXY_PASS_REGOLA_BASE_URL);
  85.             String protocollo = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_PROXY_PASS_REGOLA_PROFILO);
  86.             String soggetto = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_PROXY_PASS_REGOLA_SOGGETTO);
  87.             String ruolo = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_PROXY_PASS_REGOLA_RUOLO);
  88.             String serviceBinding = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_PROXY_PASS_REGOLA_SERVICE_BINDING);
  89.            
  90.             ConfigurazioneCore confCore = new ConfigurazioneCore();
  91.             SoggettiCore soggettiCore = new SoggettiCore(confCore);
  92.             Configurazione configurazioneGenerale = confCore.getConfigurazioneGenerale();
  93.             ConfigurazioneMultitenant configurazioneMultitenant = configurazioneGenerale.getMultitenant();
  94.             boolean multiTenant = false;
  95.             if(configurazioneMultitenant != null) {
  96.                 StatoFunzionalita statoMultitenant = configurazioneMultitenant.getStato();
  97.                 multiTenant = StatoFunzionalita.ABILITATO.equals(statoMultitenant);
  98.             }

  99.             // Preparo il menu
  100.             confHelper.makeMenu();
  101.             List<Parameter> lstParam = new ArrayList<>();
  102.            
  103.            
  104.             List<String> protocolli = confCore.getProtocolli();
  105.             List<IDSoggetto> soggetti = new ArrayList<>();
  106.             // soggetti per profilo
  107.             if(StringUtils.isNotEmpty(protocollo)) {
  108.                 soggetti = soggettiCore.getIdSoggettiOperativi(protocollo);
  109.             }
  110.            
  111.             // setto la barra del titolo
  112.             lstParam.add(new Parameter(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_GENERALE, ConfigurazioneCostanti.SERVLET_NAME_CONFIGURAZIONE_GENERALE));
  113.             lstParam.add(new Parameter(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_PROXY_PASS_REGOLE, ConfigurazioneCostanti.SERVLET_NAME_CONFIGURAZIONE_PROXY_PASS_REGOLA_LIST));
  114.             lstParam.add(ServletUtils.getParameterAggiungi());
  115.             ServletUtils.setPageDataTitle(pd, lstParam);

  116.             // Se nomehid = null, devo visualizzare la pagina per l'inserimento
  117.             // dati
  118.             if (confHelper.isEditModeInProgress()) {
  119.                
  120.                 if(nome == null) {
  121.                     nome = "";
  122.                     descrizione = "";
  123.                     stato = StatoFunzionalita.ABILITATO.toString();
  124.                     regExpr = false;
  125.                     regolaText = "";
  126.                     contestoEsterno = "";
  127.                     baseUrl = "";
  128.                     protocollo = "";
  129.                     soggetto = "";
  130.                     ruolo = CostantiControlStation.DEFAULT_VALUE_PARAMETRO_PROXY_PASS_REGOLA_RUOLO_QUALSIASI;
  131.                     serviceBinding = CostantiControlStation.DEFAULT_VALUE_PARAMETRO_SERVICE_BINDING_QUALSIASI;
  132.                 }
  133.                
  134.                 // preparo i campi
  135.                 List<DataElement> dati = new ArrayList<>();
  136.                 dati.add(ServletUtils.getDataElementForEditModeFinished());

  137.                 dati = confHelper.addProxyPassConfigurazioneRegola(TipoOperazione.ADD, dati, idRegolaS, nome, descrizione, stato, regExpr, regolaText, contestoEsterno, baseUrl, protocollo, protocolli, soggetto, soggetti, ruolo, serviceBinding, multiTenant);

  138.                 pd.setDati(dati);

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

  140.                 return ServletUtils.getStrutsForwardEditModeInProgress(mapping,
  141.                         ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_PROXY_PASS_REGOLA,
  142.                         ForwardParams.ADD());
  143.             }

  144.             // Controlli sui campi immessi
  145.             boolean isOk = confHelper.proxyPassConfigurazioneRegolaCheckData(TipoOperazione.ADD, null);
  146.             if (!isOk) {

  147.                 // preparo i campi
  148.                 List<DataElement> dati = new ArrayList<>();

  149.                 dati.add(ServletUtils.getDataElementForEditModeFinished());
  150.                
  151.                 dati = confHelper.addProxyPassConfigurazioneRegola(TipoOperazione.ADD, dati, idRegolaS, nome, descrizione, stato, regExpr, regolaText, contestoEsterno, baseUrl, protocollo, protocolli, soggetto, soggetti, ruolo, serviceBinding, multiTenant);

  152.                 pd.setDati(dati);

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

  154.                 return ServletUtils.getStrutsForwardEditModeCheckError(mapping,
  155.                         ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_PROXY_PASS_REGOLA,
  156.                         ForwardParams.ADD());
  157.             }

  158.             // rileggo la configurazione
  159.             configurazioneGenerale = confCore.getConfigurazioneGenerale();
  160.            
  161.             if(configurazioneGenerale.getUrlInvocazione() == null)
  162.                 configurazioneGenerale.setUrlInvocazione(new ConfigurazioneUrlInvocazione());
  163.            
  164.             // calcolo prossima posizione
  165.             int posizione = ConfigurazioneUtilities.getProssimaPosizioneUrlInvocazioneRegola(configurazioneGenerale);
  166.            
  167.             // salvataggio regola
  168.             ConfigurazioneUrlInvocazioneRegola regola = new ConfigurazioneUrlInvocazioneRegola();
  169.            
  170.             regola.setNome(nome);
  171.             if(descrizione!=null && !"".equals(descrizione)) {
  172.                 regola.setDescrizione(descrizione);
  173.             }
  174.             if(baseUrl!=null && !"".equals(baseUrl)) {
  175.                 regola.setBaseUrl(baseUrl);
  176.             }
  177.             if(contestoEsterno!=null && !"".equals(contestoEsterno)) {
  178.                 regola.setContestoEsterno(contestoEsterno);
  179.             }
  180.             else {
  181.                 regola.setContestoEsterno("");
  182.             }
  183.             regola.setRegexpr(regExpr);
  184.             regola.setRegola(regolaText);
  185.             if(StringUtils.isNotEmpty(protocollo))
  186.                 regola.setProtocollo(protocollo);
  187.             if(StringUtils.isNotEmpty(soggetto)) {
  188.                 IDSoggetto selezionatoToID = confCore.convertSoggettoSelezionatoToID(soggetto);
  189.                 IdSoggetto idSoggetto = new IdSoggetto();
  190.                 idSoggetto.setTipo(selezionatoToID.getTipo());
  191.                 idSoggetto.setNome(selezionatoToID.getNome());

  192.                 // set valori
  193.                 regola.setSoggetto(idSoggetto);
  194.             }
  195.             if(StringUtils.isNotEmpty(ruolo)) {
  196.                 if(ruolo.equals(CostantiControlStation.DEFAULT_VALUE_PARAMETRO_PROXY_PASS_REGOLA_RUOLO_EROGAZIONE))
  197.                     regola.setRuolo(RuoloContesto.PORTA_APPLICATIVA);
  198.                 else if(ruolo.equals(CostantiControlStation.DEFAULT_VALUE_PARAMETRO_PROXY_PASS_REGOLA_RUOLO_FRUIZIONE))
  199.                     regola.setRuolo(RuoloContesto.PORTA_DELEGATA);
  200.             }
  201.             if(StringUtils.isNotEmpty(serviceBinding)) {
  202.                 if(serviceBinding.equals(CostantiControlStation.DEFAULT_VALUE_PARAMETRO_SERVICE_BINDING_SOAP))
  203.                     regola.setServiceBinding(ServiceBinding.SOAP);
  204.                 else if(serviceBinding.equals(CostantiControlStation.DEFAULT_VALUE_PARAMETRO_SERVICE_BINDING_REST))
  205.                     regola.setServiceBinding(ServiceBinding.REST);
  206.                
  207.             }
  208.             if(stato.equals(StatoFunzionalita.ABILITATO.getValue()))
  209.                 regola.setStato(StatoFunzionalita.ABILITATO);
  210.             else
  211.                 regola.setStato(StatoFunzionalita.DISABILITATO);
  212.            
  213.             regola.setPosizione(posizione);
  214.                        
  215.             confCore.performCreateOperation(userLogin, confHelper.smista(), regola);
  216.            
  217.             // Preparo la lista
  218.             ConsoleSearch ricerca = (ConsoleSearch) ServletUtils.getSearchObjectFromSession(request, session, ConsoleSearch.class);

  219.             int idLista = Liste.CONFIGURAZIONE_PROXY_PASS_REGOLA;

  220.             ricerca = confHelper.checkSearchParameters(idLista, ricerca);

  221.             List<ConfigurazioneUrlInvocazioneRegola> lista = confCore.proxyPassConfigurazioneRegolaList(ricerca);
  222.            
  223.             confHelper.prepareProxyPassConfigurazioneRegolaList(ricerca, lista);
  224.                        
  225.             pd.setMessage(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_PROPRIETA_SISTEMA_MODIFICATA_CON_SUCCESSO, Costanti.MESSAGE_TYPE_INFO);

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

  227.             return ServletUtils.getStrutsForwardEditModeFinished(mapping,
  228.                     ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_PROXY_PASS_REGOLA,
  229.                     ForwardParams.ADD());
  230.         } catch (Exception e) {
  231.             return ServletUtils.getStrutsForwardError(ControlStationCore.getLog(), e, pd, request, session, gd, mapping,
  232.                     ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_PROXY_PASS_REGOLA, ForwardParams.ADD());
  233.         }
  234.     }


  235. }