LimitExceededNotifier.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.utils.ILimitExceededNotifier;
  30. import org.openspcoop2.utils.date.DateManager;
  31. import org.slf4j.Logger;

  32. /**
  33.  * LimitExceededNotifier
  34.  *
  35.  *
  36.  * @author Poli Andrea (apoli@link.it)
  37.  * @author $Author$
  38.  * @version $Rev$, $Date$
  39.  */
  40. public class LimitExceededNotifier implements ILimitExceededNotifier{

  41.     private Context context;
  42.     private SogliaDimensioneMessaggio soglia;
  43.     private Logger log;
  44.     public LimitExceededNotifier(Context context, SogliaDimensioneMessaggio soglia, Logger log) {
  45.         this.context = context;
  46.         this.soglia = soglia;
  47.         this.log = log;
  48.     }
  49.    
  50.     @Override
  51.     public void notify(long count, boolean contentLengthExceeded) {
  52.         if(this.context!=null) {
  53.             GeneratoreMessaggiErrore.addContextInfoControlloTrafficoPolicyViolated(this.context, false);
  54.            
  55.             registraEvento();
  56.         }
  57.        
  58.         Date dataEventoPolicyViolated = DateManager.getDate();
  59.        
  60.         CategoriaEventoControlloTraffico tipoEvento = null;
  61.         if(this.soglia.isPolicyGlobale()) {
  62.             tipoEvento = CategoriaEventoControlloTraffico.POLICY_GLOBALE;
  63.         }
  64.         else {
  65.             tipoEvento = CategoriaEventoControlloTraffico.POLICY_API;
  66.         }
  67.        
  68.         String descriptionPolicyViolated =
  69.                 contentLengthExceeded ?
  70.                 "Rilevato messaggio con dimensione indicata nel Content-Length superiore al limite consentito" :
  71.                 "Rilevato messaggio con dimensione superiore al limite consentito";
  72.         descriptionPolicyViolated+=" (soglia:"+this.soglia.getSogliaKb()+" kb)";
  73.            
  74.         try {
  75.             NotificatoreEventi.getInstance().log(tipoEvento,
  76.                     this.soglia.getIdPolicyConGruppo(), this.soglia.getConfigurazione(),
  77.                     dataEventoPolicyViolated, descriptionPolicyViolated);
  78.         }catch(Exception t) {
  79.             this.log.error("Emissione evento non riuscito: "+t.getMessage(),t);
  80.         }
  81.     }
  82.    
  83.     private void registraEvento() {
  84.         String idTransazione = (String) this.context.getObject(Costanti.ID_TRANSAZIONE);
  85.         if(idTransazione!=null) {
  86.             Transaction tr = null;
  87.             try {
  88.                 tr = TransactionContext.getTransaction(idTransazione);
  89.             }catch(TransactionNotExistsException notExists) {
  90.                 // ignore
  91.             }
  92.             if(tr!=null) {
  93.                 TipoEvento tipoEvento = null;
  94.                 if(this.soglia.isPolicyGlobale()) {
  95.                     tipoEvento = TipoEvento.RATE_LIMITING_POLICY_GLOBALE;
  96.                 }
  97.                 else {
  98.                     tipoEvento = TipoEvento.RATE_LIMITING_POLICY_API;
  99.                 }
  100.                 try {
  101.                     tr.addEventoGestione(tipoEvento.getValue()
  102.                             +"_"+
  103.                             CodiceEventoControlloTraffico.VIOLAZIONE.getValue()
  104.                             +"_"+
  105.                             this.soglia.getNomePolicy()
  106.                             );
  107.                 }catch(Exception t) {
  108.                     this.log.error("Associazione evento alla transazione non riuscita: "+t.getMessage(),t);
  109.                 }
  110.             }
  111.         }
  112.     }

  113. }