AuditLog4JAppender.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.lib.audit.appender;

  21. import java.io.ByteArrayOutputStream;
  22. import java.io.File;
  23. import java.io.InputStream;
  24. import java.util.Date;
  25. import java.util.Enumeration;
  26. import java.util.Properties;

  27. import org.slf4j.Logger;
  28. import org.openspcoop2.pdd.core.CostantiPdD;
  29. import org.openspcoop2.utils.LoggerWrapperFactory;
  30. import org.openspcoop2.utils.date.DateManager;
  31. import org.openspcoop2.utils.properties.CollectionProperties;
  32. import org.openspcoop2.utils.properties.PropertiesUtilities;
  33. import org.openspcoop2.utils.xml.JaxbUtils;
  34. import org.openspcoop2.web.lib.audit.AuditException;
  35. import org.openspcoop2.web.lib.audit.log.Operation;
  36. import org.openspcoop2.web.lib.audit.log.constants.Stato;
  37. import org.openspcoop2.web.lib.audit.log.utils.CleanerOpenSPCoop2Extensions;

  38. /**
  39.  * Appender per registrare operazione di audit
  40.  *
  41.  *
  42.  * @author Andrea Poli (apoli@link.it)
  43.  * @author Stefano Corallo (corallo@link.it)
  44.  * @author Sandra Giangrandi (sandra@link.it)
  45.  * @author $Author$
  46.  * @version $Rev$, $Date$
  47.  *
  48.  */
  49. public class AuditLog4JAppender implements IAuditAppender {

  50.    
  51.     private String nomeAppender = null;
  52.     private Logger logger = null;
  53.     private boolean xml = true;
  54.    
  55.     @Override
  56.     public void initAppender(String nomeAppender,Properties properties) throws AuditException{
  57.    
  58.         java.util.Properties loggerProperties = new java.util.Properties();
  59.         try{
  60.             this.nomeAppender = nomeAppender;
  61.        
  62.             String fileConfigurazione = properties.getProperty("fileConfigurazione");
  63.             if(fileConfigurazione==null){
  64.                 throw new Exception("Proprieta' 'fileConfigurazione' non definita");
  65.             }
  66.             fileConfigurazione = fileConfigurazione.trim();
  67.            
  68.             String nomeFileLoaderInstance = properties.getProperty("nomeFileLoaderInstance");
  69.             if(nomeFileLoaderInstance!=null){
  70.                 nomeFileLoaderInstance = nomeFileLoaderInstance.trim();
  71.             }
  72.            
  73.             String nomeProprietaLoaderInstance = properties.getProperty("nomeProprietaLoaderInstance");
  74.             if(nomeProprietaLoaderInstance!=null){
  75.                 nomeProprietaLoaderInstance = nomeProprietaLoaderInstance.trim();
  76.             }
  77.            
  78.             String confDirectory = properties.getProperty("confDirectory");
  79.             if(confDirectory!=null){
  80.                 confDirectory = confDirectory.trim();
  81.             }
  82.            
  83.            
  84.            
  85.             // Configurazione file
  86.             File fConf = new File(fileConfigurazione);
  87.             if(fConf.exists()){
  88.                 try(java.io.FileInputStream fin = new java.io.FileInputStream(fConf)){
  89.                     loggerProperties.load(fin);
  90.                 }
  91.             }else{
  92.                 InputStream is = null;
  93.                 try{
  94.                     try{
  95.                         is = AuditLog4JAppender.class.getResourceAsStream(fileConfigurazione);
  96.                     }catch(Exception e){
  97.                         if(fileConfigurazione.startsWith("/")){
  98.                             throw e;
  99.                         }
  100.                     }
  101.                     if(is==null && fileConfigurazione.startsWith("/")==false){
  102.                         try{
  103.                             is = AuditLog4JAppender.class.getResourceAsStream("/"+fileConfigurazione);
  104.                         }catch(Exception e){
  105.                             throw e;
  106.                         }
  107.                     }
  108.                     if(is==null){
  109.                         throw new Exception("InputStream ["+fileConfigurazione+"] non trovato");
  110.                     }
  111.                     loggerProperties.load(is);
  112.                 }finally{
  113.                     try{
  114.                         if(is!=null){
  115.                             is.close();
  116.                         }
  117.                     }catch(Exception eClose){
  118.                         // close
  119.                     }
  120.                 }
  121.             }
  122.            
  123.            
  124.             // File Local Implementation
  125.             if(nomeFileLoaderInstance!=null && nomeProprietaLoaderInstance!=null){
  126.                 CollectionProperties loggerPropertiesRidefinito =  
  127.                         PropertiesUtilities.searchLocalImplementation(CostantiPdD.OPENSPCOOP2_LOCAL_HOME,LoggerWrapperFactory.getLogger(AuditLog4JAppender.class),
  128.                                 nomeProprietaLoaderInstance, nomeFileLoaderInstance, confDirectory);
  129.                 if(loggerPropertiesRidefinito!=null && loggerPropertiesRidefinito.size()>0){
  130.                     Enumeration<?> ridefinito = loggerPropertiesRidefinito.keys();
  131.                     while (ridefinito.hasMoreElements()) {
  132.                         String key = (String) ridefinito.nextElement();
  133.                         String value = (String) loggerPropertiesRidefinito.get(key);
  134.                         if(loggerProperties.containsKey(key)){
  135.                             //Object o =
  136.                             loggerProperties.remove(key);
  137.                         }
  138.                         loggerProperties.put(key, value);
  139.                         //System.out.println("CHECK NUOVO VALORE: "+loggerProperties.get(key));
  140.                     }
  141.                 }
  142.             }
  143.            
  144.             LoggerWrapperFactory.setLogConfiguration(loggerProperties,true);
  145.            
  146.             // Logger
  147.             String category = properties.getProperty("category");
  148.             if(category==null){
  149.                 throw new Exception("Proprieta' 'category' non definita");
  150.             }
  151.             category = category.trim();
  152.             this.logger = LoggerWrapperFactory.getLogger(category);
  153.            
  154.             // xml format
  155.             String xml = properties.getProperty("xml");
  156.             if(xml==null){
  157.                 throw new Exception("Proprieta' 'xml' non definita");
  158.             }
  159.             xml = xml.trim();
  160.             this.xml = Boolean.parseBoolean(xml);
  161.            
  162.            
  163.         }catch(Exception e){
  164.             throw new AuditException("Inizializzazione appender["+this.nomeAppender+"] non riuscita:"+e.getMessage(),e);
  165.         }
  166.        
  167.     }
  168.    
  169.     @Override
  170.     public Object registraOperazioneInFaseDiElaborazione(Operation operation) throws AuditException{
  171.         try{
  172.             if(this.xml){
  173.                 CleanerOpenSPCoop2Extensions cleaner = new CleanerOpenSPCoop2Extensions();
  174.                 cleaner.clean(operation, true); // clono poich' l'oggetto viene usato anche in altri appender
  175.                 ByteArrayOutputStream bout = new ByteArrayOutputStream();
  176.                 JaxbUtils.objToXml(bout, Operation.class, operation);
  177.                 bout.flush();
  178.                 bout.close();
  179.                 this.logger.info(bout.toString());
  180.             }else{
  181.                 if(operation.getInterfaceMsg()==null)
  182.                     throw new Exception("InterfaceMsg non fornito");
  183.                 else
  184.                     this.logger.info("<"+new Date()+"> "+ operation.getInterfaceMsg()+"\n");
  185.             }
  186.             return operation;
  187.         }catch(Exception e){
  188.             throw new AuditException("Appender["+this.nomeAppender+"] Errore durante la registrazione dell'operazione: "+e.getMessage(),e);
  189.         }
  190.     }
  191.    
  192.     @Override
  193.     public void registraOperazioneCompletataConSuccesso(Object idOperation) throws AuditException{
  194.         try{
  195.             Operation operation = (Operation)idOperation;
  196.            
  197.             // Aggiorno stato
  198.             operation.setStato(Stato.COMPLETED);
  199.            
  200.             // Aggiorno tempo di esecuzione
  201.             operation.setTimeExecute(DateManager.getDate());
  202.        
  203.             if(this.xml){
  204.                 CleanerOpenSPCoop2Extensions cleaner = new CleanerOpenSPCoop2Extensions();
  205.                 cleaner.clean(operation, true); // clono poich' l'oggetto viene usato anche in altri appender
  206.                 ByteArrayOutputStream bout = new ByteArrayOutputStream();
  207.                 JaxbUtils.objToXml(bout, Operation.class, operation);
  208.                 bout.flush();
  209.                 bout.close();
  210.                 this.logger.info(bout.toString());
  211.             }else{
  212.                 if(operation.getInterfaceMsg()==null)
  213.                     throw new Exception("InterfaceMsg non fornito");
  214.                 else
  215.                     this.logger.info("<"+new Date()+"> "+ operation.getInterfaceMsg()+"\n");
  216.             }
  217.            
  218.         }catch(Exception e){
  219.             throw new AuditException("Appender["+this.nomeAppender+"] Errore durante la registrazione dell'operazione: "+e.getMessage(),e);
  220.         }
  221.     }
  222.    
  223.     @Override
  224.     public void registraOperazioneTerminataConErrore(Object idOperation,String motivoErrore) throws AuditException{
  225.         try{
  226.             Operation operation = (Operation)idOperation;
  227.            
  228.             // Aggiorno stato
  229.             operation.setStato(Stato.ERROR);
  230.             operation.setError(motivoErrore);
  231.            
  232.             // Aggiorno tempo di esecuzione
  233.             operation.setTimeExecute(DateManager.getDate());
  234.            
  235.             if(this.xml){
  236.                 CleanerOpenSPCoop2Extensions cleaner = new CleanerOpenSPCoop2Extensions();
  237.                 cleaner.clean(operation, true); // clono poich' l'oggetto viene usato anche in altri appender
  238.                 ByteArrayOutputStream bout = new ByteArrayOutputStream();
  239.                 JaxbUtils.objToXml(bout, Operation.class, operation);
  240.                 bout.flush();
  241.                 bout.close();
  242.                 this.logger.info(bout.toString());
  243.             }else{
  244.                 if(operation.getInterfaceMsg()==null)
  245.                     throw new Exception("InterfaceMsg non fornito");
  246.                 else
  247.                     this.logger.info("<"+new Date()+"> "+ operation.getInterfaceMsg()+"\n");
  248.             }
  249.            
  250.         }catch(Exception e){
  251.             throw new AuditException("Appender["+this.nomeAppender+"] Errore durante la registrazione dell'operazione: "+e.getMessage(),e);
  252.         }
  253.     }
  254.    
  255. }