ConfigurazioneRouting.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.struts.action.Action;
  27. import org.apache.struts.action.ActionForm;
  28. import org.apache.struts.action.ActionForward;
  29. import org.apache.struts.action.ActionMapping;
  30. import org.openspcoop2.core.config.AccessoRegistroRegistro;
  31. import org.openspcoop2.core.config.Route;
  32. import org.openspcoop2.core.config.RouteGateway;
  33. import org.openspcoop2.core.config.RouteRegistro;
  34. import org.openspcoop2.core.config.RoutingTable;
  35. import org.openspcoop2.core.config.RoutingTableDefault;
  36. import org.openspcoop2.web.ctrlstat.core.ControlStationCore;
  37. import org.openspcoop2.web.ctrlstat.core.ConsoleSearch;
  38. import org.openspcoop2.web.ctrlstat.servlet.GeneralHelper;
  39. import org.openspcoop2.web.ctrlstat.servlet.soggetti.SoggettiCore;
  40. import org.openspcoop2.web.lib.mvc.Costanti;
  41. import org.openspcoop2.web.lib.mvc.DataElement;
  42. import org.openspcoop2.web.lib.mvc.GeneralData;
  43. import org.openspcoop2.web.lib.mvc.PageData;
  44. import org.openspcoop2.web.lib.mvc.Parameter;
  45. import org.openspcoop2.web.lib.mvc.ServletUtils;
  46. import org.openspcoop2.web.lib.mvc.TipoOperazione;

  47. /**
  48.  * routing
  49.  *
  50.  * @author Andrea Poli (apoli@link.it)
  51.  * @author Stefano Corallo (corallo@link.it)
  52.  * @author Sandra Giangrandi (sandra@link.it)
  53.  * @author $Author$
  54.  * @version $Rev$, $Date$
  55.  *
  56.  */
  57. public final class ConfigurazioneRouting extends Action {

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

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

  61.         // Inizializzo PageData
  62.         PageData pd = new PageData();

  63.         GeneralHelper generalHelper = new GeneralHelper(session);

  64.         // Inizializzo GeneralData
  65.         GeneralData gd = generalHelper.initGeneralData(request);

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

  67.         try {
  68.             ConfigurazioneHelper confHelper = new ConfigurazioneHelper(request, pd, session);

  69.             String tiporotta = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_TIPO_ROTTA);
  70.             if (tiporotta == null) {
  71.                 tiporotta = ConfigurazioneCostanti.LABEL_PARAMETRO_CONFIGURAZIONE_TIPO_ROTTA_GATEWAY;
  72.             }
  73.             String tiposoggrotta = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_TIPO_SOGGETTO_ROTTA);
  74.             String nomesoggrotta = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_NOME_SOGGETTO_ROTTA);
  75.             String registrorotta = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_REGISTRO_ROTTA);
  76.             String rottaenabled = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_ROTTA_ENABLED);
  77.             String applicaModificaS = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_APPLICA_MODIFICA);
  78.             boolean applicaModifica = ServletUtils.isCheckBoxEnabled(applicaModificaS);

  79.             ConfigurazioneCore confCore = new ConfigurazioneCore();
  80.             SoggettiCore soggettiCore = new SoggettiCore(confCore);

  81.             // Preparo il menu
  82.             confHelper.makeMenu();

  83.             // Prendo la lista di registri e la metto in un array
  84.             // aggiungendo il campo "all"
  85.             List<AccessoRegistroRegistro> list = confCore.registriList(new ConsoleSearch(true));
  86.             int totReg = list.size();
  87.             totReg++;
  88.             String[] registriList = new String[totReg];
  89.             String[] registriListLabel = new String[totReg];
  90.             registriList[0] = "0";
  91.             registriListLabel[0] = ConfigurazioneCostanti.LABEL_PARAMETRO_CONFIGURAZIONE_REGISTRO_ROTTA_ALL;
  92.             int i = 1;
  93.             for (AccessoRegistroRegistro arr : list) {
  94.                 registriList[i] = arr.getNome();
  95.                 registriListLabel[i] = arr.getNome();
  96.                 i++;
  97.             }

  98.             // Soggetti
  99.             List<String> tipiSoggetti = new ArrayList<>();
  100.             tipiSoggetti.add("-");
  101.             tipiSoggetti.addAll(soggettiCore.getTipiSoggettiGestiti());
  102.             String[] tipiSoggettiLabel = tipiSoggetti.toArray(new String[1]);

  103.             // setto la barra del titolo
  104.             List<Parameter> lstParam = new ArrayList<>();

  105.             lstParam.add(new Parameter(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_GENERALE, ConfigurazioneCostanti.SERVLET_NAME_CONFIGURAZIONE_GENERALE));
  106.             lstParam.add(new Parameter(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_TABELLA_DI_ROUTING, null));

  107.             ServletUtils.setPageDataTitle(pd, lstParam);

  108.             String postBackElementName = confHelper.getPostBackElementName();

  109.             // Controllo se ho modificato il protocollo, resetto il referente
  110.             if(postBackElementName != null &&
  111.                 postBackElementName.equalsIgnoreCase(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_ROTTA_ENABLED)){
  112.                 applicaModifica = false;
  113.             }


  114.             // Se tiporottahid != null, modifico i dati della porta di dominio
  115.             // nel
  116.             // db
  117.             if (!(confHelper.isEditModeInProgress() && !applicaModifica)) {
  118.                 // Controlli sui campi immessi
  119.                 boolean isOk = confHelper.routingCheckData(registriList);
  120.                 if (!isOk) {
  121.                     // preparo i campi
  122.                     List<DataElement> dati = new ArrayList<>();

  123.                     dati.add(ServletUtils.getDataElementForEditModeFinished());

  124.                     dati = confHelper.addRoutingToDati(TipoOperazione.OTHER, tiporotta, tiposoggrotta,
  125.                             nomesoggrotta, registrorotta, rottaenabled,
  126.                             registriList, registriListLabel, tipiSoggettiLabel, dati);


  127.                     pd.setDati(dati);

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

  129.                     return ServletUtils.getStrutsForwardEditModeCheckError(mapping,
  130.                             ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_ROUTING,
  131.                             ConfigurazioneCostanti.TIPO_OPERAZIONE_CONFIGURAZIONE_ROUTING);
  132.                 }

  133.                 // Modifico i dati della porta di dominio nel db
  134.                 RoutingTable rt = confCore.getRoutingTable();
  135.                 if ((rottaenabled == null) || !rottaenabled.equals(ConfigurazioneCostanti.DEFAULT_VALUE_ABILITATO)) {
  136.                     rt.setAbilitata(false);
  137.                 } else {
  138.                     rt.setAbilitata(true);

  139.                     Route defaultRoute = null;
  140.                     if ( rt.getDefault()!=null && rt.getDefault().sizeRouteList() > 0) {
  141.                         defaultRoute = rt.getDefault().removeRoute(0);
  142.                     } else
  143.                         defaultRoute = new Route();

  144.                     if (tiporotta.equals(ConfigurazioneCostanti.LABEL_PARAMETRO_CONFIGURAZIONE_TIPO_ROTTA_GATEWAY)) {
  145.                         RouteGateway rg = new RouteGateway();
  146.                         rg.setTipo(tiposoggrotta);
  147.                         rg.setNome(nomesoggrotta);
  148.                         defaultRoute.setGateway(rg);
  149.                         defaultRoute.setRegistro(null);
  150.                     } else {

  151.                         RouteRegistro rr = new RouteRegistro();
  152.                         rr.setNome(registrorotta);

  153.                         defaultRoute.setRegistro(rr);
  154.                         defaultRoute.setGateway(null);
  155.                     }

  156.                     rt.setDefault(new RoutingTableDefault());
  157.                     rt.getDefault().addRoute(defaultRoute);

  158.                 }

  159.                 confCore.performUpdateOperation(userLogin, confHelper.smista(), rt);

  160.                 List<DataElement> dati = new ArrayList<>();

  161.                 dati = confHelper.addRoutingToDati(TipoOperazione.OTHER, tiporotta, tiposoggrotta, nomesoggrotta,
  162.                         registrorotta, rottaenabled,  registriList, registriListLabel, tipiSoggettiLabel, dati);


  163.                 pd.setDati(dati);

  164.                 pd.setMessage(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_TABELLA_ROUTING_MODIFICATA_CON_SUCCESSO, Costanti.MESSAGE_TYPE_INFO);

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

  166.                 return ServletUtils.getStrutsForwardEditModeFinished(mapping,
  167.                         ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_ROUTING,
  168.                         ConfigurazioneCostanti.TIPO_OPERAZIONE_CONFIGURAZIONE_ROUTING
  169.                         );

  170.             }

  171.             if (rottaenabled == null) {
  172.                 RoutingTable rt = confCore.getRoutingTable();

  173.                 if ((rt == null) || (rt.getAbilitata()==null || !rt.getAbilitata())) {
  174.                     rottaenabled = ConfigurazioneCostanti.DEFAULT_VALUE_DISABILITATO;
  175.                     tiporotta = ConfigurazioneCostanti.LABEL_PARAMETRO_CONFIGURAZIONE_TIPO_ROTTA_GATEWAY;
  176.                     tiposoggrotta = "";
  177.                     nomesoggrotta = "";
  178.                     registrorotta = "0";
  179.                 } else {
  180.                     rottaenabled = ConfigurazioneCostanti.DEFAULT_VALUE_ABILITATO;
  181.                     Route r = null;
  182.                     if(rt.getDefault()!=null && rt.getDefault().sizeRouteList()>0){
  183.                         r = rt.getDefault().getRoute(0);
  184.                     }
  185.                     RouteGateway rg = r != null ? r.getGateway() : null;
  186.                     if (rg != null) {
  187.                         tiporotta = ConfigurazioneCostanti.LABEL_PARAMETRO_CONFIGURAZIONE_TIPO_ROTTA_GATEWAY;
  188.                         tiposoggrotta = rg.getTipo();
  189.                         nomesoggrotta = rg.getNome();
  190.                         registrorotta = "";
  191.                     }
  192.                     RouteRegistro rr = r != null ? r.getRegistro() : null;
  193.                     if (rr != null) {
  194.                         tiporotta = ConfigurazioneCostanti.LABEL_PARAMETRO_CONFIGURAZIONE_TIPO_ROTTA_REGISTRO;
  195.                         tiposoggrotta = "";
  196.                         nomesoggrotta = "";
  197.                         registrorotta = rr.getNome();
  198.                     }
  199.                 }
  200.             }

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

  203.             dati.add(ServletUtils.getDataElementForEditModeFinished());

  204.             dati = confHelper.addRoutingToDati(TipoOperazione.OTHER, tiporotta, tiposoggrotta, nomesoggrotta,
  205.                     registrorotta, rottaenabled,  registriList, registriListLabel, tipiSoggettiLabel, dati);


  206.             pd.setDati(dati);

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

  208.             return ServletUtils.getStrutsForwardEditModeInProgress(mapping,
  209.                     ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_ROUTING,
  210.                     ConfigurazioneCostanti.TIPO_OPERAZIONE_CONFIGURAZIONE_ROUTING
  211.                     );
  212.         } catch (Exception e) {
  213.             return ServletUtils.getStrutsForwardError(ControlStationCore.getLog(), e, pd, request, session, gd, mapping,
  214.                     ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_ROUTING,
  215.                     ConfigurazioneCostanti.TIPO_OPERAZIONE_CONFIGURAZIONE_ROUTING);
  216.         }
  217.     }


  218. }