StatisticsPostOutResponseHandler.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.statistics;

  21. import java.util.Date;

  22. import org.openspcoop2.core.constants.Costanti;
  23. import org.openspcoop2.pdd.config.OpenSPCoop2Properties;
  24. import org.openspcoop2.pdd.core.handlers.HandlerException;
  25. import org.openspcoop2.pdd.core.handlers.PostOutResponseContext;
  26. import org.openspcoop2.pdd.core.handlers.PostOutResponseHandler;
  27. import org.openspcoop2.utils.date.DateManager;
  28. import org.openspcoop2.utils.date.DateUtils;


  29. /**
  30.  * StatisticsPostOutResponseHandler
  31.  *
  32.  * @author Poli Andrea (apoli@link.it)
  33.  * @author $Author$
  34.  * @version $Rev$, $Date$
  35.  */
  36. public class StatisticsPostOutResponseHandler implements PostOutResponseHandler {

  37.     @Override
  38.     public void invoke(PostOutResponseContext context) throws HandlerException{
  39.        
  40.         if(!OpenSPCoop2Properties.getInstance().isStatisticheViaJmx()) {
  41.             return;
  42.         }
  43.        
  44.         // Raccolgo date
  45.         Object dataIngressoRichiesta = context.getPddContext().getObject(StatisticsConstants.DATA_INGRESSO_RICHIESTA);
  46.         long timeMillisIngressoRichiesta = -1;
  47.         if(dataIngressoRichiesta!=null)
  48.             timeMillisIngressoRichiesta = ((Date) dataIngressoRichiesta).getTime();
  49.        
  50.         Object dataUscitaRichiesta = context.getPddContext().getObject(StatisticsConstants.DATA_USCITA_RICHIESTA);
  51.         long timeMillisUscitaRichiesta = -1;
  52.         if(dataUscitaRichiesta!=null){
  53.             timeMillisUscitaRichiesta = ((Date) dataUscitaRichiesta).getTime();
  54.         }
  55.         else{
  56.             // Provo a vedere se c'e' stata una data di presa in carico
  57.             Object dataPresaInCarico = context.getPddContext().getObject(Costanti.DATA_PRESA_IN_CARICO);
  58.             if(dataPresaInCarico!=null){
  59.                 try{
  60.                     dataUscitaRichiesta = DateUtils.getSimpleDateFormatMs().parse((String)dataPresaInCarico);
  61.                     timeMillisUscitaRichiesta = ((Date) dataUscitaRichiesta).getTime();
  62.                 }catch(Exception e){
  63.                     throw new HandlerException(e.getMessage(),e);
  64.                 }
  65.             }
  66.         }
  67.        
  68.         Object dataIngressoRisposta = context.getPddContext().getObject(StatisticsConstants.DATA_INGRESSO_RISPOSTA);
  69.         long timeMillisIngressoRisposta = -1;
  70.         if(dataIngressoRisposta!=null)
  71.             timeMillisIngressoRisposta = ((Date) dataIngressoRisposta).getTime();
  72.         else{
  73.             // Provo a vedere se c'e' stata una data di presa in carico
  74.             Object dataPresaInCarico = context.getPddContext().getObject(Costanti.DATA_PRESA_IN_CARICO);
  75.             if(dataPresaInCarico!=null){
  76.                 try{
  77.                     dataIngressoRisposta = DateUtils.getSimpleDateFormatMs().parse((String)dataPresaInCarico);
  78.                     timeMillisIngressoRisposta = ((Date) dataIngressoRisposta).getTime();
  79.                 }catch(Exception e){
  80.                     throw new HandlerException(e.getMessage(),e);
  81.                 }
  82.             }
  83.         }
  84.        
  85.         long timeMillisUscitaRisposta = DateManager.getTimeMillis();
  86.        
  87.         // Raccolgo dimensioni
  88.         long dimensioneIngressoRichiesta = -1;
  89.         if(context.getInputRequestMessageSize()!=null && context.getInputRequestMessageSize()>0){
  90.             dimensioneIngressoRichiesta = context.getInputRequestMessageSize();
  91.         }
  92.         long dimensioneUscitaRichiesta = -1;
  93.         if(context.getOutputRequestMessageSize()!=null && context.getOutputRequestMessageSize()>0){
  94.             dimensioneUscitaRichiesta = context.getOutputRequestMessageSize();
  95.         }
  96.         long dimensioneIngressoRisposta = -1;
  97.         if(context.getInputResponseMessageSize()!=null && context.getInputResponseMessageSize()>0){
  98.             dimensioneIngressoRisposta = context.getInputResponseMessageSize();
  99.         }
  100.         long dimensioneUscitaRisposta = -1;
  101.         if(context.getOutputResponseMessageSize()!=null && context.getOutputResponseMessageSize()>0){
  102.             dimensioneUscitaRisposta = context.getOutputResponseMessageSize();
  103.         }
  104.        
  105.         Statistic stat =  new Statistic();
  106.         stat.setEsito(context.getEsito());
  107.         stat.setTipoPdD(context.getTipoPorta());
  108.         stat.setProtocollo(context.getProtocolFactory());
  109.         stat.setTimeMillisIngressoRichiesta(timeMillisIngressoRichiesta);
  110.         stat.setTimeMillisIngressoRisposta(timeMillisIngressoRisposta);
  111.         stat.setTimeMillisUscitaRichiesta(timeMillisUscitaRichiesta);
  112.         stat.setTimeMillisUscitaRisposta(timeMillisUscitaRisposta);
  113.         stat.setDimensioneIngressoRichiesta(dimensioneIngressoRichiesta);
  114.         stat.setDimensioneIngressoRisposta(dimensioneIngressoRisposta);
  115.         stat.setDimensioneUscitaRichiesta(dimensioneUscitaRichiesta);
  116.         stat.setDimensioneUscitaRisposta(dimensioneUscitaRisposta);
  117.        
  118.         StatisticsCollection.update(stat);
  119.     }
  120.    
  121. }