NotifierStreamingHandler.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.pdd.core.handlers.notifier.engine;

  21. import java.io.File;
  22. import java.io.FileOutputStream;
  23. import java.util.List;
  24. import java.util.Map;

  25. import org.openspcoop2.core.id.IDSoggetto;
  26. import org.openspcoop2.core.transazioni.constants.TipoMessaggio;
  27. import org.openspcoop2.pdd.config.OpenSPCoop2Properties;
  28. import org.openspcoop2.utils.UtilsException;
  29. import org.openspcoop2.utils.io.notifier.unblocked.AbstractStreamingHandler;
  30. import org.openspcoop2.utils.io.notifier.unblocked.ResultStreamingHandler;
  31. import org.slf4j.Logger;

  32. /**    
  33.  * NotifierStreamingHandler
  34.  *
  35.  * @author Poli Andrea (poli@link.it)
  36.  * @author $Author$
  37.  * @version $Rev$, $Date$
  38.  */
  39. public class NotifierStreamingHandler extends AbstractStreamingHandler {

  40.     private Throwable exception = null;
  41.     private String error = null;
  42.    
  43.     private String idTransazione;
  44.     private TipoMessaggio tipoMessaggio;
  45.     private Map<String, List<String>> headerTrasporto;
  46.     private long idDumpConfigurazione;
  47.     private String contentType;
  48.     private NotifierCallback notifierCallback;
  49.     private IDSoggetto dominio;
  50.    
  51.     public NotifierStreamingHandler(NotifierCallback notifierCallback, String idTransazione, TipoMessaggio tipoMessaggio,
  52.             Map<String, List<String>> headerTrasporto,
  53.             long idDumpConfigurazione,
  54.             String contentType, Logger log,
  55.             IDSoggetto dominio) throws Exception{
  56.         super(log, OpenSPCoop2Properties.getInstance().getDumpNonRealtimeInMemoryThreshold());
  57.         this.notifierCallback = notifierCallback; // Per i log
  58.         this.idTransazione = idTransazione;
  59.         this.tipoMessaggio = tipoMessaggio;
  60.         this.headerTrasporto = headerTrasporto;
  61.         this.contentType = contentType;
  62.         this.idDumpConfigurazione = idDumpConfigurazione;
  63.         this.dominio = dominio;
  64.     }
  65.    
  66.     @Override
  67.     public boolean isPrematureEnd() throws UtilsException {
  68.         return (this.exception!=null);
  69.     }

  70.     @Override
  71.     public String getError() {
  72.         return this.error;
  73.     }

  74.     @Override
  75.     public Throwable getException() {
  76.         return this.exception;
  77.     }

  78.     @Override
  79.     public ResultStreamingHandler call() throws UtilsException {
  80.         try{
  81.             NotifierResultStreamingHandler result = new NotifierResultStreamingHandler();
  82.            
  83.             OpenSPCoop2Properties op2Properties = OpenSPCoop2Properties.getInstance();
  84.            
  85.             if(op2Properties.isDumpNonRealtimeDatabaseMode()){
  86.                
  87.                 this.notifierCallback.debug("Save on database.....");
  88.                
  89.                 // save on database
  90.                 NotifierDump notifierDump = NotifierDump.getInstance();
  91.                
  92.                 this.notifierCallback.debug("Get Instance.....");
  93.                
  94.                 int executeUpdate = notifierDump.saveOnDatabase(this.notifierCallback, this.idTransazione, this.tipoMessaggio,
  95.                         this.headerTrasporto,
  96.                         this.idDumpConfigurazione, this.contentType, this, this.dominio);
  97.                
  98.                 this.notifierCallback.debug("Execute: "+executeUpdate);
  99.                
  100.                 result.setSaveOnFileSystem(false);
  101.                 result.setExecuteUpdateRow(executeUpdate);
  102.                
  103.                 this.notifierCallback.debug("Save on database execute with row: "+executeUpdate);
  104.                
  105.             }
  106.             else{
  107.                
  108.                 this.notifierCallback.debug("Save on fs.....");
  109.                
  110.                 // save on fs
  111.                
  112.                 // directory
  113.                 File fDir =op2Properties.getDumpNonRealtimeRepository();
  114.                 if(fDir.exists()==false){
  115.                     throw new Exception("Directory ["+fDir.getAbsolutePath()+"] not exists");
  116.                 }
  117.                 if(fDir.canRead()==false){
  118.                     throw new Exception("Directory ["+fDir.getAbsolutePath()+"] not readable");
  119.                 }
  120.                 if(fDir.canWrite()==false){
  121.                     throw new Exception("Directory ["+fDir.getAbsolutePath()+"] not writable");
  122.                 }
  123.                
  124.                 // messaggio
  125.                 File f = new File(fDir, this.idTransazione+"_"+this.tipoMessaggio.toString()+".bin");
  126.                 FileOutputStream fout = null;
  127.                 //org.apache.commons.io.output.NullOutputStream fout = null;
  128.                 try{
  129.                     fout = new FileOutputStream(f);
  130.                     //fout = new org.apache.commons.io.output.NullOutputStream();
  131.                    
  132.                     // lettura e scrittura su file
  133.                     byte[]buffer = new byte[4096];
  134.                     int letti = 0;
  135.                     while( (letti=this.read(buffer)) != -1 ){
  136.                         fout.write(buffer, 0, letti);
  137.                     }
  138.                    
  139.                 }finally{
  140.                     try{
  141.                         if(fout!=null){
  142.                             fout.flush();
  143.                         }
  144.                     }catch(Exception eClose){
  145.                         // close
  146.                     }
  147.                     try{
  148.                         if(fout!=null){
  149.                             fout.close();
  150.                         }
  151.                     }catch(Exception eClose){
  152.                         // close
  153.                     }
  154.                 }
  155.                
  156.                 // Risposta
  157.                 result.setSaveOnFileSystem(true);
  158.                 result.setFile(f);
  159.                
  160.                 this.notifierCallback.debug("Save on fs execute: "+f.getAbsolutePath());
  161.             }
  162.            
  163.             return result;
  164.         }catch(Throwable e){
  165.            
  166.             //this.notifierCallback.error("ERRORE HANDLER STREAMING :"+e.getMessage(),e);
  167.            
  168.             this.exception = e;
  169.             this.error = e.getMessage();
  170.             throw new UtilsException(this.error,this.exception);
  171.         }
  172.     }



  173. }