GestoreCorrelazioneApplicativaPSUtilities.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;

  21. import java.sql.Connection;
  22. import java.sql.PreparedStatement;
  23. import java.sql.Timestamp;

  24. import org.openspcoop2.core.config.CorrelazioneApplicativa;
  25. import org.openspcoop2.protocol.sdk.ProtocolException;
  26. import org.openspcoop2.protocol.sdk.constants.CodiceErroreIntegrazione;
  27. import org.openspcoop2.protocol.sdk.constants.ErroriIntegrazione;
  28. import org.openspcoop2.protocol.sdk.state.StateMessage;
  29. import org.openspcoop2.utils.date.DateManager;

  30. /**
  31.  * GestoreCorrelazioneApplicativaPSUtilities
  32.  *
  33.  * @author Andrea Poli (apoli@link.it)
  34.  * @author $Author$
  35.  * @version $Rev$, $Date$
  36.  */

  37. public class GestoreCorrelazioneApplicativaPSUtilities {

  38.     // In questa classe vengono registrati i metodi per cui vengono collezionate le prepared statement
  39.    
  40.     public static void applicaCorrelazione(GestoreCorrelazioneApplicativa gestore,
  41.             CorrelazioneApplicativa correlazioneApplicativa,String idApplicativo,String idBustaRequest) throws GestoreMessaggiException, ProtocolException{

  42.         if(correlazioneApplicativa==null){
  43.             gestore.errore = ErroriIntegrazione.ERRORE_416_CORRELAZIONE_APPLICATIVA_RICHIESTA_ERRORE.
  44.                     getErrore416_CorrelazioneApplicativaRichiesta("dati per l'identificazione dell'id di correlazione non presenti");
  45.             throw new GestoreMessaggiException(gestore.errore.getDescrizione(gestore.protocolFactory));
  46.         }
  47.         if(idBustaRequest==null){
  48.             gestore.errore = ErroriIntegrazione.ERRORE_416_CORRELAZIONE_APPLICATIVA_RICHIESTA_ERRORE.
  49.                     getErrore416_CorrelazioneApplicativaRichiesta("identificativo non presente tra i parametri di invocazione");
  50.             throw new GestoreMessaggiException(gestore.errore.getDescrizione(gestore.protocolFactory));
  51.         }
  52.         if(idApplicativo==null){
  53.             gestore.errore = ErroriIntegrazione.ERRORE_416_CORRELAZIONE_APPLICATIVA_RICHIESTA_ERRORE.
  54.                     getErrore416_CorrelazioneApplicativaRichiesta("identificativo applicativo non presente tra i parametri di invocazione");
  55.             throw new GestoreMessaggiException(gestore.errore.getDescrizione(gestore.protocolFactory));
  56.         }

  57.         Timestamp scadenzaCorrelazioneT = null;
  58.         if(correlazioneApplicativa.getScadenza()!=null){
  59.             try{
  60.                 long scadenza = Long.parseLong(correlazioneApplicativa.getScadenza());
  61.                 scadenzaCorrelazioneT = new Timestamp(DateManager.getTimeMillis()+(scadenza*60*1000));
  62.             }catch(Exception e){
  63.                 gestore.errore = ErroriIntegrazione.ERRORE_5XX_GENERICO_PROCESSAMENTO_MESSAGGIO.
  64.                         get5XX_ErroreProcessamento(CodiceErroreIntegrazione.CODICE_529_CORRELAZIONE_APPLICATIVA_RICHIESTA_NON_RIUSCITA);
  65.                 throw new GestoreMessaggiException("Scadenza impostata per la correlazione applicativa non corretta: "+e.getMessage(),e);
  66.             }
  67.         }
  68.         // E' stata introdotta l'ora di registrazione per gestire le scadenze null
  69. //      else{
  70. //          scadenzaCorrelazioneT = new Timestamp(DateManager.getTimeMillis()+(this.scadenzaDefault*60*1000));
  71. //      }

  72.         /** Fase di verifica dell'id di correlazione con l'id */
  73.         PreparedStatement pstmtInsert = null;
  74.         try{
  75.             StateMessage stateMSG = (StateMessage)gestore.state;
  76.             Connection connectionDB = stateMSG.getConnectionDB();

  77.             // nuova correlazione
  78.             StringBuilder queryInsert = new StringBuilder();
  79.             queryInsert.append("INSERT INTO "+GestoreCorrelazioneApplicativa.CORRELAZIONE_APPLICATIVA);
  80.             queryInsert.append(" (ID_MESSAGGIO,ID_APPLICATIVO,SERVIZIO_APPLICATIVO,TIPO_MITTENTE,MITTENTE,TIPO_DESTINATARIO,DESTINATARIO,TIPO_SERVIZIO,SERVIZIO,VERSIONE_SERVIZIO,AZIONE ");
  81.             if(scadenzaCorrelazioneT!=null){
  82.                 queryInsert.append(",SCADENZA");
  83.             }
  84.             queryInsert.append(") VALUES (?,?,?,?,?,?,?,?,?,?,?");
  85.             if(scadenzaCorrelazioneT!=null){
  86.                 queryInsert.append(",?");
  87.             }
  88.             queryInsert.append(")");
  89.             pstmtInsert = connectionDB.prepareStatement(queryInsert.toString());
  90.             int index = 1;
  91.             pstmtInsert.setString(index++,idBustaRequest);
  92.             pstmtInsert.setString(index++,idApplicativo);
  93.             pstmtInsert.setString(index++,gestore.servizioApplicativo);
  94.             pstmtInsert.setString(index++,gestore.soggettoFruitore.getTipo());
  95.             pstmtInsert.setString(index++,gestore.soggettoFruitore.getNome());
  96.             pstmtInsert.setString(index++,gestore.idServizio.getSoggettoErogatore().getTipo());
  97.             pstmtInsert.setString(index++,gestore.idServizio.getSoggettoErogatore().getNome());
  98.             pstmtInsert.setString(index++,gestore.idServizio.getTipo());
  99.             pstmtInsert.setString(index++,gestore.idServizio.getNome());
  100.             pstmtInsert.setInt(index++,gestore.idServizio.getVersione());
  101.             pstmtInsert.setString(index++,gestore.idServizio.getAzione());
  102.             if(scadenzaCorrelazioneT!=null){
  103.                 pstmtInsert.setTimestamp(index++,scadenzaCorrelazioneT);
  104.             }

  105.             //  Add PreparedStatement
  106.             String valoreAzione = "N.D.";
  107.             if( (gestore.idServizio.getAzione()!=null) && ("".equals(gestore.idServizio.getAzione())==false) ){
  108.                 valoreAzione = gestore.idServizio.getAzione();
  109.             }
  110.             stateMSG.getPreparedStatement().put("INSERT CorrelazioneApplicativa_"+idBustaRequest+"_"+idApplicativo+"_"+gestore.soggettoFruitore.getTipo()+gestore.soggettoFruitore.getNome()+
  111.                     "_"+gestore.idServizio.getSoggettoErogatore().getTipo()+gestore.idServizio.getSoggettoErogatore().getNome()+"_"+
  112.                     gestore.idServizio.getTipo()+gestore.idServizio.getNome()+":"+gestore.idServizio.getVersione()+"_"+valoreAzione,pstmtInsert);

  113.         }catch(Exception er){
  114.             gestore.errore = ErroriIntegrazione.ERRORE_5XX_GENERICO_PROCESSAMENTO_MESSAGGIO.
  115.                     get5XX_ErroreProcessamento(CodiceErroreIntegrazione.CODICE_529_CORRELAZIONE_APPLICATIVA_RICHIESTA_NON_RIUSCITA);
  116.             gestore.log.error("Correlazione IDApplicativo - ID non riuscita: "+er.getMessage());
  117.             throw new GestoreMessaggiException("Correlazione IDApplicativo - ID non riuscita: "+er.getMessage(),er);
  118.         }

  119.     }

  120. }