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

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

  25. import org.apache.struts.action.Action;
  26. import org.apache.struts.action.ActionForm;
  27. import org.apache.struts.action.ActionForward;
  28. import org.apache.struts.action.ActionMapping;
  29. import org.openspcoop2.web.ctrlstat.core.ControlStationCore;
  30. import org.openspcoop2.web.ctrlstat.servlet.GeneralHelper;
  31. import org.openspcoop2.web.ctrlstat.servlet.audit.AuditingCore;
  32. import org.openspcoop2.web.lib.audit.dao.Appender;
  33. import org.openspcoop2.web.lib.audit.dao.AppenderProperty;
  34. import org.openspcoop2.web.lib.audit.dao.Configurazione;
  35. import org.openspcoop2.web.lib.audit.web.AuditCostanti;
  36. import org.openspcoop2.web.lib.audit.web.AuditHelper;
  37. import org.openspcoop2.web.lib.mvc.Costanti;
  38. import org.openspcoop2.web.lib.mvc.DataElement;
  39. import org.openspcoop2.web.lib.mvc.GeneralData;
  40. import org.openspcoop2.web.lib.mvc.PageData;
  41. import org.openspcoop2.web.lib.mvc.ServletUtils;

  42. /**
  43.  * auditing
  44.  *
  45.  * @author Andrea Poli (apoli@link.it)
  46.  * @author Stefano Corallo (corallo@link.it)
  47.  * @author Sandra Giangrandi (sandra@link.it)
  48.  * @author $Author$
  49.  * @version $Rev$, $Date$
  50.  *
  51.  */
  52. public final class ConfigurazioneAuditing extends Action {

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

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

  56.         // Inizializzo PageData
  57.         PageData pd = new PageData();

  58.         GeneralHelper generalHelper = new GeneralHelper(session);

  59.         // Inizializzo GeneralData
  60.         GeneralData gd = generalHelper.initGeneralData(request);

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

  62.         try {
  63.             ConfigurazioneHelper confHelper = new ConfigurazioneHelper(request, pd, session);

  64.             String statoaudit = confHelper.getParameter( AuditCostanti.PARAMETRO_AUDIT_STATO_AUDIT);
  65.             String stato = confHelper.getParameter(AuditCostanti.PARAMETRO_AUDIT_STATO);
  66.             String dump = confHelper.getParameter(AuditCostanti.PARAMETRO_AUDIT_DUMP);
  67.             String formatodump = confHelper.getParameter(AuditCostanti.PARAMETRO_AUDIT_FORMATO_DUMP);
  68.             String log4j = confHelper.getParameter(AuditCostanti.PARAMETRO_AUDIT_LOG4J);
  69.            
  70.             ConfigurazioneCore confCore = new ConfigurazioneCore();
  71.             AuditHelper ah = new AuditHelper(request, pd, session);
  72.             AuditingCore auditingCore = new AuditingCore(confCore);

  73.             Configurazione c = auditingCore.getConfigurazioneAudit();
  74.             // Preparo il menu
  75.             confHelper.makeMenu();

  76.             // setto la barra del titolo
  77.             ServletUtils.setPageDataTitle_ServletFirst(pd, AuditCostanti.LABEL_AUDIT, AuditCostanti.SERVLET_NAME_AUDIT);

  78.             // Se idhid != null, modifico i dati dell'audit nel db
  79.             if (!confHelper.isEditModeInProgress()) {
  80.                 // Controlli sui campi immessi
  81.                 String msg = ah.auditCheckData(statoaudit, stato, dump, formatodump, log4j);
  82.                 if (!msg.equals("")) {
  83.                     pd.setMessage(msg);

  84.                     Configurazione tmpConf = auditingCore.getConfigurazioneAudit();

  85.                     // preparo i campi
  86.                     List<DataElement> dati = ah.addAuditingToDati(
  87.                             statoaudit, stato, dump, formatodump, log4j,
  88.                             tmpConf.sizeFiltri());

  89.                     pd.setDati(dati);

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

  91.                     return ServletUtils.getStrutsForwardEditModeCheckError(mapping,
  92.                             AuditCostanti.OBJECT_NAME_CONFIGURAZIONE_AUDITING, AuditCostanti.TIPO_OPERAZIONE_CONFIGURAZIONE_AUDITING
  93.                             );
  94.                 }

  95.                 // Modifico i dati dell'audit nel db
  96.                 Configurazione newConfigurazione = auditingCore.getConfigurazioneAudit();

  97.                 newConfigurazione.setAuditEngineEnabled(
  98.                         statoaudit.equals(AuditCostanti.DEFAULT_VALUE_ABILITATO));
  99.                 newConfigurazione.setAuditEnabled(
  100.                         stato.equals(AuditCostanti.DEFAULT_VALUE_ABILITATO));
  101.                 newConfigurazione.setDumpEnabled(
  102.                         dump.equals(AuditCostanti.DEFAULT_VALUE_ABILITATO));
  103.                 newConfigurazione.setDumpFormat(formatodump);
  104.                 if (log4j.equals(AuditCostanti.DEFAULT_VALUE_ABILITATO)) {
  105.                     boolean giaPresente = false;
  106.                     for(int i=0; i<newConfigurazione.sizeAppender(); i++){
  107.                         if(AuditCostanti.DEFAULT_VALUE_PARAMETRO_AUDIT_APPENDER_NOME.equals(newConfigurazione.getAppender(i).getNome())){
  108.                             giaPresente=true;
  109.                             break;
  110.                         }
  111.                     }
  112.                     if (!giaPresente) {
  113.                         Appender appender = new Appender();
  114.                         appender.setNome(AuditCostanti.DEFAULT_VALUE_PARAMETRO_AUDIT_APPENDER_NOME);
  115.                         appender.setClassName(AuditCostanti.DEFAULT_VALUE_PARAMETRO_AUDIT_APPENDER_CLASS_NAME);
  116.                         AppenderProperty pr1 = new AppenderProperty();
  117.                         pr1.setName(AuditCostanti.DEFAULT_VALUE_PARAMETRO_AUDIT_APPENDER_PROPERTY_FILE_CONFIGURAZIONE_NAME);
  118.                         pr1.setValue(AuditCostanti.DEFAULT_VALUE_PARAMETRO_AUDIT_APPENDER_PROPERTY_FILE_CONFIGURAZIONE_VALUE);
  119.                         appender.addProperty(pr1);
  120.                         AppenderProperty pr2 = new AppenderProperty();
  121.                         pr2.setName(AuditCostanti.DEFAULT_VALUE_PARAMETRO_AUDIT_APPENDER_PROPERTY_CATEGORY_NAME);
  122.                         pr2.setValue(AuditCostanti.DEFAULT_VALUE_PARAMETRO_AUDIT_APPENDER_PROPERTY_CATEGORY_VALUE);
  123.                         appender.addProperty(pr2);
  124.                         AppenderProperty pr3 = new AppenderProperty();
  125.                         pr3.setName(AuditCostanti.DEFAULT_VALUE_PARAMETRO_AUDIT_APPENDER_PROPERTY_XML_NAME);
  126.                         pr3.setValue(AuditCostanti.DEFAULT_VALUE_PARAMETRO_AUDIT_APPENDER_PROPERTY_XML_VALUE);
  127.                         appender.addProperty(pr3);
  128.                         newConfigurazione.addAppender(appender);
  129.                     }
  130.                 } else {
  131.                     for(int i=0; i<newConfigurazione.sizeAppender(); i++){
  132.                         if(AuditCostanti.DEFAULT_VALUE_PARAMETRO_AUDIT_APPENDER_NOME.equals(newConfigurazione.getAppender(i).getNome())){
  133.                             newConfigurazione.removeAppender(i);
  134.                             break;
  135.                         }
  136.                     }
  137.                 }

  138.                 confCore.performUpdateOperation(userLogin, confHelper.smista(), newConfigurazione);


  139.                 // preparo i campi
  140.                 List<DataElement> dati = ah.addAuditingToDati(
  141.                         statoaudit, stato, dump, formatodump, log4j,
  142.                         c.sizeFiltri());

  143.                 dati.add(ServletUtils.getDataElementForEditModeFinished());
  144.                
  145.                 pd.setDati(dati);

  146.                 pd.setMessage(AuditCostanti.LABEL_AUDIT_CONFIGURAZIONE_MODIFICATA, Costanti.MESSAGE_TYPE_INFO);

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

  148.                 return ServletUtils.getStrutsForwardEditModeFinished(mapping,
  149.                         AuditCostanti.OBJECT_NAME_CONFIGURAZIONE_AUDITING, AuditCostanti.TIPO_OPERAZIONE_CONFIGURAZIONE_AUDITING);
  150.             }


  151.             if (statoaudit == null)
  152.                 statoaudit = c.isAuditEngineEnabled() ? AuditCostanti.DEFAULT_VALUE_ABILITATO :  AuditCostanti.DEFAULT_VALUE_DISABILITATO;
  153.             stato = c.isAuditEnabled() ? AuditCostanti.DEFAULT_VALUE_ABILITATO : AuditCostanti.DEFAULT_VALUE_DISABILITATO;
  154.             dump = c.isDumpEnabled() ? AuditCostanti.DEFAULT_VALUE_ABILITATO :AuditCostanti.DEFAULT_VALUE_DISABILITATO;
  155.             formatodump = c.getDumpFormat();

  156.             log4j = AuditCostanti.DEFAULT_VALUE_DISABILITATO;
  157.             if(c.sizeAppender()>0){
  158.                 for(int i=0; i<c.sizeAppender(); i++){
  159.                     if(AuditCostanti.DEFAULT_VALUE_PARAMETRO_AUDIT_APPENDER_NOME.equals(c.getAppender(i).getNome())){
  160.                         log4j = ConfigurazioneCostanti.DEFAULT_VALUE_ABILITATO;
  161.                         break;
  162.                     }
  163.                 }
  164.             }

  165.             // preparo i campi
  166.             List<DataElement> dati = ah.addAuditingToDati(
  167.                     statoaudit, stato, dump, formatodump, log4j,
  168.                     c.sizeFiltri());

  169.             dati.add(ServletUtils.getDataElementForEditModeFinished());
  170.             pd.setDati(dati);

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

  172.             return ServletUtils.getStrutsForwardEditModeInProgress(mapping,
  173.                     AuditCostanti.OBJECT_NAME_CONFIGURAZIONE_AUDITING, AuditCostanti.TIPO_OPERAZIONE_CONFIGURAZIONE_AUDITING);
  174.         } catch (Exception e) {
  175.             return ServletUtils.getStrutsForwardError(ControlStationCore.getLog(), e, pd, request, session, gd, mapping,
  176.                     AuditCostanti.OBJECT_NAME_CONFIGURAZIONE_AUDITING, AuditCostanti.TIPO_OPERAZIONE_CONFIGURAZIONE_AUDITING);
  177.         }
  178.     }
  179. }