ConfigurazioneControlloTraffico.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.commons.Filtri;
  31. import org.openspcoop2.core.commons.Liste;
  32. import org.openspcoop2.core.controllo_traffico.Cache;
  33. import org.openspcoop2.core.controllo_traffico.ConfigurazioneRateLimiting;
  34. import org.openspcoop2.core.controllo_traffico.TempiRispostaErogazione;
  35. import org.openspcoop2.core.controllo_traffico.TempiRispostaFruizione;
  36. import org.openspcoop2.web.ctrlstat.core.ControlStationCore;
  37. import org.openspcoop2.web.ctrlstat.core.ConsoleSearch;
  38. import org.openspcoop2.web.ctrlstat.costanti.CostantiControlStation;
  39. import org.openspcoop2.web.ctrlstat.servlet.GeneralHelper;
  40. import org.openspcoop2.web.lib.mvc.Costanti;
  41. import org.openspcoop2.web.lib.mvc.DataElement;
  42. import org.openspcoop2.web.lib.mvc.ForwardParams;
  43. import org.openspcoop2.web.lib.mvc.GeneralData;
  44. import org.openspcoop2.web.lib.mvc.PageData;
  45. import org.openspcoop2.web.lib.mvc.Parameter;
  46. import org.openspcoop2.web.lib.mvc.ServletUtils;
  47. import org.openspcoop2.web.lib.mvc.TipoOperazione;

  48. /**    
  49.  * ConfigurazioneControlloTraffico
  50.  *
  51.  * @author Pintori Giuliano (pintori@link.it)
  52.  * @author $Author$
  53.  * @version $Rev$, $Date$
  54.  */
  55. public class ConfigurazioneControlloTraffico extends Action {

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

  58.         TipoOperazione tipoOperazione = TipoOperazione.OTHER;

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

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

  62.         GeneralHelper generalHelper = new GeneralHelper(session);

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

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

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

  68.             // Preparo il menu
  69.             confHelper.makeMenu();

  70.             ConfigurazioneCore confCore = new ConfigurazioneCore();
  71.            

  72.             org.openspcoop2.core.controllo_traffico.ConfigurazioneGenerale configurazioneControlloTraffico = confCore.getConfigurazioneControlloTraffico();
  73.            
  74.             // annullo eventuale ricerca precedente
  75.             ConsoleSearch ricerca = (ConsoleSearch) ServletUtils.getSearchObjectFromSession(request, session, ConsoleSearch.class);
  76.             if(ricerca!=null) {
  77.                 ricerca.addFilter(Liste.CONFIGURAZIONE_CONTROLLO_TRAFFICO_CONFIGURAZIONE_POLICY, Filtri.FILTRO_TIPO_POLICY, CostantiControlStation.PARAMETRO_CONFIGURAZIONE_CONTROLLO_TRAFFICO_POLICY_TIPO_UTENTE);
  78.             }
  79.            
  80.             // conto policy
  81.             ConsoleSearch searchPolicyUtente = new ConsoleSearch(true);
  82.             searchPolicyUtente.addFilter( Liste.CONFIGURAZIONE_CONTROLLO_TRAFFICO_CONFIGURAZIONE_POLICY, Filtri.FILTRO_TIPO_POLICY,
  83.                     CostantiControlStation.PARAMETRO_CONFIGURAZIONE_CONTROLLO_TRAFFICO_POLICY_TIPO_UTENTE);
  84.             long sizePolicy = confCore.countConfigurazionePolicy(searchPolicyUtente);
  85.            
  86.             // conto policy attivate globalmente
  87.             long sizeGlobalPolicy = confCore.countAttivazionePolicy(null,null,null);
  88.            
  89.             // Stato [si usa per capire se sono entrato per la prima volta nella schermata]    
  90.             boolean first = confHelper.isFirstTimeFromHttpParameters(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_CONTROLLO_TRAFFICO_FIRST_TIME);
  91.            
  92.             if(configurazioneControlloTraffico.getControlloTraffico()==null){
  93.                 configurazioneControlloTraffico.setControlloTraffico(new org.openspcoop2.core.controllo_traffico.ConfigurazioneControlloTraffico());
  94.             }
  95.             if(configurazioneControlloTraffico.getRateLimiting()==null){
  96.                 configurazioneControlloTraffico.setRateLimiting(new ConfigurazioneRateLimiting());
  97.             }
  98.             if(configurazioneControlloTraffico.getCache() ==null){
  99.                 configurazioneControlloTraffico.setCache(new Cache());
  100.             }
  101.             if(configurazioneControlloTraffico.getTempiRispostaErogazione() ==null){
  102.                 configurazioneControlloTraffico.setTempiRispostaErogazione(new TempiRispostaErogazione());
  103.             }
  104.             if(configurazioneControlloTraffico.getTempiRispostaFruizione() ==null){
  105.                 configurazioneControlloTraffico.setTempiRispostaFruizione(new TempiRispostaFruizione());
  106.             }
  107.            
  108.             StringBuilder sbParsingError = new StringBuilder();
  109.             // Limitazioni
  110.            
  111.             String errorControlloTraffico = confHelper.readConfigurazioneControlloTrafficoFromHttpParameters(configurazioneControlloTraffico.getControlloTraffico(), first);
  112.             if(errorControlloTraffico!=null){
  113.                 confHelper.addParsingError(sbParsingError,errorControlloTraffico);
  114.             }
  115.            
  116.            
  117.             // RateLimiting
  118.            
  119.             String errorRateLimiting = confHelper.readConfigurazioneRateLimitingFromHttpParameters(configurazioneControlloTraffico.getRateLimiting(), first);
  120.             if(errorRateLimiting!=null){
  121.                 confHelper.addParsingError(sbParsingError,errorRateLimiting);
  122.             }
  123.            

  124.             // Tempi di Risposta Fruizione
  125.            
  126.             String errorTempiRispostaFruizione = confHelper.readTempiRispostaFruizioneFromHttpParameters(configurazioneControlloTraffico.getTempiRispostaFruizione(), first);
  127.             if(errorTempiRispostaFruizione!=null){
  128.                 confHelper.addParsingError(sbParsingError,errorTempiRispostaFruizione);
  129.             }
  130.            
  131.            
  132.             // Tempi di Risposta Erogazione
  133.            
  134.             String errorTempiRispostaErogazione = confHelper.readTempiRispostaErogazioneFromHttpParameters(configurazioneControlloTraffico.getTempiRispostaErogazione(),  first);
  135.             if(errorTempiRispostaErogazione!=null){
  136.                 confHelper.addParsingError(sbParsingError,errorTempiRispostaErogazione);
  137.             }

  138.            
  139.             // Cache
  140.            
  141.             String errorCache = confHelper.readConfigurazioneCacheFromHttpParameters(configurazioneControlloTraffico.getCache(), first);
  142.             if(errorCache!=null){
  143.                 confHelper.addParsingError(sbParsingError,errorCache);
  144.             }
  145.            
  146.             // setto la barra del titolo
  147.             List<Parameter> lstParam = new ArrayList<>();
  148.             lstParam.add(new Parameter(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_CONTROLLO_TRAFFICO, null));

  149.             // edit in progress
  150.             if (confHelper.isEditModeInProgress()) {
  151.                 ServletUtils.setPageDataTitle(pd, lstParam);

  152.                 // preparo i campi
  153.                 List<DataElement> dati = new ArrayList<>();
  154.                 dati.add(ServletUtils.getDataElementForEditModeFinished());

  155.                 // Set First is false
  156.                 confHelper.addToDatiFirstTimeDisabled(dati,ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_CONTROLLO_TRAFFICO_FIRST_TIME);

  157.                 confHelper.addConfigurazionControlloTrafficoToDati(dati, tipoOperazione, configurazioneControlloTraffico, sizePolicy, sizeGlobalPolicy, false);

  158.                 pd.setDati(dati);

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

  160.                 return ServletUtils.getStrutsForwardEditModeInProgress(mapping, ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_CONTROLLO_TRAFFICO,   ForwardParams.OTHER(""));
  161.             }

  162.             // Controlli sui campi immessi
  163.             boolean isOk = confHelper.checkDatiConfigurazioneControlloTraffico(tipoOperazione, sbParsingError, configurazioneControlloTraffico);
  164.             if (!isOk) {

  165.                 ServletUtils.setPageDataTitle(pd, lstParam);

  166.                 // preparo i campi
  167.                 List<DataElement> dati = new ArrayList<>();
  168.                 dati.add(ServletUtils.getDataElementForEditModeFinished());

  169.                 // Set First is false
  170.                 confHelper.addToDatiFirstTimeDisabled(dati,ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_CONTROLLO_TRAFFICO_FIRST_TIME);

  171.                 confHelper.addConfigurazionControlloTrafficoToDati(dati, tipoOperazione, configurazioneControlloTraffico, sizePolicy, sizeGlobalPolicy, false);

  172.                 pd.setDati(dati);

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

  174.                 return ServletUtils.getStrutsForwardEditModeCheckError(mapping, ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_CONTROLLO_TRAFFICO,   ForwardParams.OTHER(""));
  175.             }

  176.             confCore.performUpdateOperation(userLogin, confHelper.smista(), configurazioneControlloTraffico);

  177.             // Preparo la lista
  178.             pd.setMessage(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_CONTROLLO_TRAFFICO_MODIFICATA_CON_SUCCESSO_RIAVVIO_RICHIESTO , Costanti.MESSAGE_TYPE_INFO);

  179.             ServletUtils.setPageDataTitle(pd, lstParam);

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

  182.             // ricarico la configurazione
  183.             configurazioneControlloTraffico = confCore.getConfigurazioneControlloTraffico();

  184.             // Set First is false
  185.             confHelper.addToDatiFirstTimeDisabled(dati,ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_FIRST_TIME);

  186.             confHelper.addConfigurazionControlloTrafficoToDati(dati, tipoOperazione, configurazioneControlloTraffico, sizePolicy, sizeGlobalPolicy, true);

  187.             pd.setDati(dati);
  188.             pd.disableEditMode();

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

  190.             return ServletUtils.getStrutsForwardEditModeFinished(mapping, ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_CONTROLLO_TRAFFICO, ForwardParams.OTHER(""));
  191.         } catch (Exception e) {
  192.             return ServletUtils.getStrutsForwardError(ControlStationCore.getLog(), e, pd, request, session, gd, mapping,
  193.                     ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_CONTROLLO_TRAFFICO, ForwardParams.OTHER(""));
  194.         }
  195.     }
  196. }