TimeoutNotifier.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.controllo_traffico;

  21. import java.util.Date;

  22. import org.openspcoop2.core.constants.Costanti;
  23. import org.openspcoop2.core.eventi.constants.CodiceEventoControlloTraffico;
  24. import org.openspcoop2.core.eventi.constants.TipoEvento;
  25. import org.openspcoop2.pdd.core.transazioni.Transaction;
  26. import org.openspcoop2.pdd.core.transazioni.TransactionContext;
  27. import org.openspcoop2.pdd.core.transazioni.TransactionNotExistsException;
  28. import org.openspcoop2.protocol.sdk.Context;
  29. import org.openspcoop2.protocol.sdk.IProtocolFactory;
  30. import org.openspcoop2.protocol.sdk.constants.EsitoTransazioneName;
  31. import org.openspcoop2.protocol.utils.EsitiProperties;
  32. import org.openspcoop2.utils.ITimeoutNotifier;
  33. import org.openspcoop2.utils.date.DateManager;
  34. import org.slf4j.Logger;

  35. /**
  36.  * TimeoutNotifier
  37.  *
  38.  *
  39.  * @author Poli Andrea (apoli@link.it)
  40.  * @author $Author$
  41.  * @version $Rev$, $Date$
  42.  */
  43. public class TimeoutNotifier implements ITimeoutNotifier{

  44.     private Context context;
  45.     private SogliaReadTimeout soglia;
  46.     private TimeoutNotifierType type;
  47.     private Logger log;
  48.     private IProtocolFactory<?> protocolFactory;
  49.     private boolean saveInContext;
  50.     public TimeoutNotifier(Context context, IProtocolFactory<?> protocolFactory,
  51.             SogliaReadTimeout soglia, TimeoutNotifierType type, Logger log,
  52.             boolean saveInContext) {
  53.         this.context = context;
  54.         this.protocolFactory = protocolFactory;
  55.         this.soglia = soglia;
  56.         this.type = type;
  57.         this.log = log;
  58.         this.saveInContext = saveInContext;
  59.     }
  60.    
  61.     @Override
  62.     public void notify(long count) {
  63.         if(this.context!=null) {
  64.            
  65.             if(this.saveInContext) {
  66.                 boolean alreadyExists = false;
  67.                 if(this.type!=null) {
  68.                     switch (this.type) {
  69.                     case CONNECTION:
  70.                         alreadyExists = !GeneratoreMessaggiErrore.addPddContextInfoControlloTrafficoConnectionTimeout(this.context);
  71.                         break;
  72.                     case REQUEST:
  73.                         alreadyExists = !GeneratoreMessaggiErrore.addPddContextInfoControlloTrafficoReadRequestTimeout(this.context, true);
  74.                         break;
  75.                     case WAIT_RESPONSE:
  76.                         alreadyExists = !GeneratoreMessaggiErrore.addPddContextInfoControlloTrafficoReadTimeout(this.context, true);
  77.                         break;
  78.                     case RECEIVE_RESPONSE:
  79.                         alreadyExists = !GeneratoreMessaggiErrore.addPddContextInfoControlloTrafficoReadResponseTimeout(this.context, true);
  80.                         break;
  81.                     }
  82.                 }else {
  83.                     alreadyExists = !GeneratoreMessaggiErrore.addPddContextInfoControlloTrafficoReadTimeout(this.context, true);
  84.                 }
  85.                
  86.                 if(alreadyExists) {
  87.                     return; // giĆ  registrato (succede in caso di timeout mentre si sta consegnando verso il client con metodo disconnect())
  88.                 }
  89.             }
  90.            
  91.             registraEvento();
  92.         }
  93.        
  94.         Date dataEventoPolicyViolated = DateManager.getDate();
  95.        
  96.         CategoriaEventoControlloTraffico tipoEvento = TimeoutNotifierType.toCategoriaEventoControlloTraffico(this.type);
  97.                    
  98.         try {
  99.            
  100.             EsitiProperties esitiProperties = EsitiProperties.getInstance(this.log, this.protocolFactory);
  101.             EsitoTransazioneName esito = TimeoutNotifierType.toEsitoTransazioneName(this.type);
  102.             String descriptionPolicyViolated = esitiProperties.getEsitoDescription(esitiProperties.convertoToCode(esito));
  103.             descriptionPolicyViolated = descriptionPolicyViolated+" (soglia:"+this.soglia.getSogliaMs()+" ms)";
  104.            
  105.             NotificatoreEventi.getInstance().log(tipoEvento,
  106.                     this.soglia.getIdConfigurazione(), this.soglia.getConfigurazione(),
  107.                     dataEventoPolicyViolated, descriptionPolicyViolated);
  108.         }catch(Exception t) {
  109.             this.log.error("Emissione evento non riuscito: "+t.getMessage(),t);
  110.         }
  111.     }

  112.     private void registraEvento() {
  113.         String idTransazione = (String) this.context.getObject(Costanti.ID_TRANSAZIONE);
  114.         if(idTransazione!=null) {
  115.             Transaction tr = null;
  116.             try {
  117.                 tr = TransactionContext.getTransaction(idTransazione);
  118.             }catch(TransactionNotExistsException notExists) {
  119.                 // ignore
  120.             }
  121.             if(tr!=null) {
  122.                 TipoEvento tipoEvento = TimeoutNotifierType.toTipoEvento(this.type);
  123.                 try {
  124.                     tr.addEventoGestione(tipoEvento.getValue()
  125.                             +"_"+
  126.                             CodiceEventoControlloTraffico.VIOLAZIONE.getValue()
  127.                             +"_"+
  128.                             this.soglia.getIdConfigurazione()
  129.                             );
  130.                 }catch(Exception t) {
  131.                     this.log.error("Associazione evento alla transazione non riuscita: "+t.getMessage(),t);
  132.                 }
  133.             }
  134.         }
  135.     }
  136. }