ConfigurazioneResponseCachingConfigurazioneRegolaAdd.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.ResponseCachingConfigurazione;
  34. import org.openspcoop2.core.config.ResponseCachingConfigurazioneGenerale;
  35. import org.openspcoop2.core.config.ResponseCachingConfigurazioneRegola;
  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.  * ConfigurazioneResponseCachingConfigurazioneRegolaAdd
  50.  *
  51.  * @author Giuliano Pintori (pintori@link.it)
  52.  * @author $Author$
  53.  * @version $Rev$, $Date$
  54.  *
  55.  */
  56. public final class ConfigurazioneResponseCachingConfigurazioneRegolaAdd extends Action {

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

  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.            
  69.             String returnCode = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_RESPONSE_CACHING_CONFIGURAZIONE_REGOLA_RETURN_CODE);
  70.             if(returnCode == null)
  71.                 returnCode = CostantiControlStation.VALUE_PARAMETRO_CONFIGURAZIONE_RETURN_CODE_QUALSIASI;
  72.            
  73.             String statusMin = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_RESPONSE_CACHING_CONFIGURAZIONE_REGOLA_RETURN_CODE_MIN);
  74.             if(statusMin == null)
  75.                 statusMin = "";
  76.             String statusMax = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_RESPONSE_CACHING_CONFIGURAZIONE_REGOLA_RETURN_CODE_MAX);
  77.             if(statusMax == null)
  78.                 statusMax = "";
  79.             String fault = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_RESPONSE_CACHING_CONFIGURAZIONE_REGOLA_FAULT);
  80.             String cacheSeconds = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_RESPONSE_CACHING_CONFIGURAZIONE_REGOLA_CACHE_TIMEOUT_SECONDS);
  81.             if(cacheSeconds == null)
  82.                 cacheSeconds = "";

  83.             ConfigurazioneCore confCore = new ConfigurazioneCore();

  84.             // Preparo il menu
  85.             confHelper.makeMenu();
  86.             List<Parameter> lstParam = new ArrayList<>();
  87.            
  88.             String postBackElementName = confHelper.getPostBackElementName();
  89.            
  90.             // se ho modificato il soggetto ricalcolo il servizio e il service binding
  91.             if (postBackElementName != null &&
  92.                 postBackElementName.equals(CostantiControlStation.PARAMETRO_CONFIGURAZIONE_RESPONSE_CACHING_CONFIGURAZIONE_REGOLA_RETURN_CODE)) {
  93.                 statusMin = "";
  94.                 statusMax = "";
  95.             }

  96.             // setto la barra del titolo
  97.             lstParam.add(new Parameter(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_GENERALE, ConfigurazioneCostanti.SERVLET_NAME_CONFIGURAZIONE_GENERALE));
  98.             lstParam.add(new Parameter(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_RESPONSE_CACHING_CONFIGURAZIONE_REGOLE, ConfigurazioneCostanti.SERVLET_NAME_CONFIGURAZIONE_RESPONSE_CACHING_CONFIGURAZIONE_REGOLA_LIST));
  99.             lstParam.add(ServletUtils.getParameterAggiungi());
  100.             ServletUtils.setPageDataTitle(pd, lstParam);

  101.             // Se nomehid = null, devo visualizzare la pagina per l'inserimento
  102.             // dati
  103.             if (confHelper.isEditModeInProgress()) {
  104.                 // preparo i campi
  105.                 List<DataElement> dati = new ArrayList<>();
  106.                 dati.add(ServletUtils.getDataElementForEditModeFinished());

  107.                 dati = confHelper.addResponseCachingConfigurazioneRegola(TipoOperazione.ADD, returnCode, statusMin, statusMax, fault, cacheSeconds, dati);

  108.                 pd.setDati(dati);

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

  110.                 return ServletUtils.getStrutsForwardEditModeInProgress(mapping,
  111.                         ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_RESPONSE_CACHING_CONFIGURAZIONE_REGOLA,
  112.                         ForwardParams.ADD());
  113.             }

  114.             // Controlli sui campi immessi
  115.             boolean isOk = confHelper.responseCachingConfigurazioneRegolaCheckData(TipoOperazione.ADD);
  116.             if (!isOk) {

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

  119.                 dati.add(ServletUtils.getDataElementForEditModeFinished());
  120.                
  121.                 dati = confHelper.addResponseCachingConfigurazioneRegola(TipoOperazione.ADD, returnCode, statusMin, statusMax, fault, cacheSeconds, dati);

  122.                 pd.setDati(dati);

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

  124.                 return ServletUtils.getStrutsForwardEditModeCheckError(mapping,
  125.                         ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_RESPONSE_CACHING_CONFIGURAZIONE_REGOLA,
  126.                         ForwardParams.ADD());
  127.             }

  128.             // salvataggio regola
  129.             ResponseCachingConfigurazioneRegola regola = new ResponseCachingConfigurazioneRegola();
  130.            
  131.             if(returnCode.equals(CostantiControlStation.VALUE_PARAMETRO_CONFIGURAZIONE_RETURN_CODE_QUALSIASI)) {
  132.                 regola.setReturnCodeMin(null);
  133.                 regola.setReturnCodeMax(null);
  134.             } else if(returnCode.equals(CostantiControlStation.VALUE_PARAMETRO_CONFIGURAZIONE_RETURN_CODE_ESATTO)) {
  135.                 regola.setReturnCodeMin(StringUtils.isNotEmpty(statusMin) ? Integer.parseInt(statusMin) : null);
  136.                 regola.setReturnCodeMax(StringUtils.isNotEmpty(statusMin) ? Integer.parseInt(statusMin) : null);
  137.             } else { // intervallo
  138.                 regola.setReturnCodeMin(StringUtils.isNotEmpty(statusMin) ? Integer.parseInt(statusMin) : null);
  139.                 regola.setReturnCodeMax(StringUtils.isNotEmpty(statusMax) ? Integer.parseInt(statusMax) : null);
  140.             }

  141.             regola.setFault(ServletUtils.isCheckBoxEnabled(fault));
  142.             regola.setCacheTimeoutSeconds(StringUtils.isNotEmpty(cacheSeconds) ? Integer.parseInt(cacheSeconds) : null);
  143.            
  144.             Configurazione configurazioneGenerale = confCore.getConfigurazioneGenerale();
  145.             ResponseCachingConfigurazioneGenerale responseCaching = configurazioneGenerale.getResponseCaching();
  146.             ResponseCachingConfigurazione configurazione = responseCaching.getConfigurazione();
  147.             configurazione.addRegola(regola);
  148.            
  149.             confCore.performUpdateOperation(userLogin, confHelper.smista(), configurazioneGenerale);
  150.            
  151.             // Preparo la lista
  152.             ConsoleSearch ricerca = (ConsoleSearch) ServletUtils.getSearchObjectFromSession(request, session, ConsoleSearch.class);

  153.             int idLista = Liste.CONFIGURAZIONE_RESPONSE_CACHING_CONFIGURAZIONE_REGOLA;

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

  155.             List<ResponseCachingConfigurazioneRegola> lista = confCore.responseCachingConfigurazioneRegolaList(ricerca);

  156.             confHelper.prepareResponseCachingConfigurazioneRegolaList(ricerca, lista, configurazione.getCacheTimeoutSeconds());
  157.                        
  158.             pd.setMessage(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_RESPONSE_CACHING_CONFIGURAZIONE_REGOLA_MODIFICATA_CON_SUCCESSO, Costanti.MESSAGE_TYPE_INFO);

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

  160.             return ServletUtils.getStrutsForwardEditModeFinished(mapping,
  161.                     ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_RESPONSE_CACHING_CONFIGURAZIONE_REGOLA,
  162.                     ForwardParams.ADD());
  163.         } catch (Exception e) {
  164.             return ServletUtils.getStrutsForwardError(ControlStationCore.getLog(), e, pd, request, session, gd, mapping,
  165.                     ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_RESPONSE_CACHING_CONFIGURAZIONE_REGOLA, ForwardParams.ADD());
  166.         }
  167.     }


  168. }