GestoreErroreConnettore.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.connettori;

  21. import java.sql.Timestamp;
  22. import java.util.ArrayList;
  23. import java.util.Enumeration;
  24. import java.util.HashMap;
  25. import java.util.Iterator;
  26. import java.util.List;
  27. import java.util.Properties;

  28. import javax.xml.namespace.QName;
  29. import javax.xml.soap.SOAPBody;
  30. import javax.xml.soap.SOAPFault;

  31. import org.apache.commons.lang.StringUtils;
  32. import org.openspcoop2.core.config.GestioneErrore;
  33. import org.openspcoop2.core.config.GestioneErroreCodiceTrasporto;
  34. import org.openspcoop2.core.config.GestioneErroreSoapFault;
  35. import org.openspcoop2.core.config.constants.CostantiConfigurazione;
  36. import org.openspcoop2.message.OpenSPCoop2Message;
  37. import org.openspcoop2.message.OpenSPCoop2RestJsonMessage;
  38. import org.openspcoop2.message.OpenSPCoop2RestXmlMessage;
  39. import org.openspcoop2.message.OpenSPCoop2SoapMessage;
  40. import org.openspcoop2.message.constants.MessageType;
  41. import org.openspcoop2.message.constants.ServiceBinding;
  42. import org.openspcoop2.message.exception.MessageException;
  43. import org.openspcoop2.message.exception.MessageNotSupportedException;
  44. import org.openspcoop2.message.soap.SoapUtils;
  45. import org.openspcoop2.pdd.core.GestoreMessaggiException;
  46. import org.openspcoop2.pdd.logger.OpenSPCoop2Logger;
  47. import org.openspcoop2.protocol.sdk.IProtocolFactory;
  48. import org.openspcoop2.protocol.sdk.constants.MessaggiFaultErroreCooperazione;
  49. import org.openspcoop2.utils.SemaphoreLock;
  50. import org.openspcoop2.utils.UtilsException;
  51. import org.openspcoop2.utils.date.DateManager;
  52. import org.openspcoop2.utils.properties.PropertiesUtilities;
  53. import org.openspcoop2.utils.regexp.RegExpNotFoundException;
  54. import org.openspcoop2.utils.regexp.RegularExpressionEngine;
  55. import org.openspcoop2.utils.rest.problem.JsonDeserializer;
  56. import org.openspcoop2.utils.rest.problem.ProblemConstants;
  57. import org.openspcoop2.utils.rest.problem.ProblemRFC7807;
  58. import org.openspcoop2.utils.rest.problem.XmlDeserializer;
  59. import org.slf4j.Logger;


  60. /**
  61.  * Classe utilizzata per gestire il comportamento della PdD in caso di errore di consegna tramite connettore
  62.  *
  63.  * @author Poli Andrea (apoli@link.it)
  64.  * @author $Author$
  65.  * @version $Rev$, $Date$
  66.  */
  67. public class GestoreErroreConnettore {

  68.     /** Indicazione su riconsegna */
  69.     private boolean riconsegna;
  70.     /** Data di rispedizione */
  71.     private java.sql.Timestamp dataRispedizione;
  72.     /** Motivo errore consegna */
  73.     private String errore;
  74.     /** Eventuale FAULT interpretato */
  75.     private SOAPFault fault;
  76.     /** Eventuale Problem interpretato */
  77.     private ProblemRFC7807 problem;

  78.     /** Logger utilizzato per debug. */
  79.     private static Logger log = OpenSPCoop2Logger.getLoggerOpenSPCoopCore();

  80.     private static final String ERRORE_APPLICATIVO_SOAP_FAULT = "errore applicativo SoapFault";
  81.    
  82.     private static final String INTERVALLO_RISPEDIZIONE_NON_IMPOSTATO = "Intervallo di rispedizione non impostato: ";
  83.     private static final String SERIALIZZAZIONE_SOAP_FAULT_NON_RIUSCITA = "Serializzazione SOAPFault non riuscita: ";
  84.     private static final String VERIFICA_ESPRESSIONE_REGOLARE_PREFIX = "Verifica espressione regolare '";
  85.     private static final String FALLITA_SUFFIX = "' fallita: ";

  86.     private static org.openspcoop2.utils.Semaphore semaphore = new org.openspcoop2.utils.Semaphore("GestoreErroreConnettore");
  87.     /** GestoreErrore di default per il componente di integrazione */
  88.     private static HashMap<String, GestioneErrore> gestioneErroreDefaultComponenteIntegrazioneMap = new HashMap<>();
  89.     /** GestoreErrore di default per il componente di cooperazione */
  90.     private static HashMap<String, GestioneErrore> gestioneErroreDefaultComponenteCooperazioneMap = new HashMap<>();


  91.     public static GestioneErrore getGestioneErroreDefaultComponenteIntegrazione(IProtocolFactory<?> protocolFactory, ServiceBinding serviceBinding) {

  92.         String key = protocolFactory.getProtocol()+"_"+serviceBinding;

  93.         if(GestoreErroreConnettore.gestioneErroreDefaultComponenteIntegrazioneMap.containsKey(key)){
  94.             return GestoreErroreConnettore.gestioneErroreDefaultComponenteIntegrazioneMap.get(key);
  95.         }else{

  96.             SemaphoreLock lock = semaphore.acquireThrowRuntime("getGestioneErroreDefaultComponenteIntegrazione");
  97.             try {

  98.                 if(GestoreErroreConnettore.gestioneErroreDefaultComponenteIntegrazioneMap.containsKey(key)){
  99.                     return GestoreErroreConnettore.gestioneErroreDefaultComponenteIntegrazioneMap.get(key);
  100.                 }

  101.                 GestioneErrore gestione = new GestioneErrore();
  102.                 // default si rispedisce
  103.                 gestione.setComportamento(CostantiConfigurazione.GESTIONE_ERRORE_RISPEDISCI_MSG);
  104.                 // si accetta il codice di trasporto 200
  105.                 GestioneErroreCodiceTrasporto codiceTrasporto = new GestioneErroreCodiceTrasporto();
  106.                 codiceTrasporto.setComportamento(CostantiConfigurazione.GESTIONE_ERRORE_ACCETTA_MSG);
  107.                 codiceTrasporto.setValoreMinimo(200);
  108.                 codiceTrasporto.setValoreMassimo(299);
  109.                 boolean isSuccessfulHttpRedirectStatusCode = false;
  110.                 try {
  111.                     isSuccessfulHttpRedirectStatusCode = protocolFactory.createProtocolManager().isSuccessfulHttpRedirectStatusCode(serviceBinding);
  112.                 }catch(Exception e) {
  113.                     log.error(e.getMessage(),e);
  114.                 }
  115.                 if(isSuccessfulHttpRedirectStatusCode) {
  116.                     codiceTrasporto.setValoreMassimo(399);
  117.                 }
  118.                 gestione.addCodiceTrasporto(codiceTrasporto);


  119.                 // Qualsiasi fault si accetta, senza effettuare un re-invio.
  120.                 GestioneErroreSoapFault faultAccetta = new GestioneErroreSoapFault();
  121.                 faultAccetta.setComportamento(CostantiConfigurazione.GESTIONE_ERRORE_ACCETTA_MSG);
  122.                 gestione.addSoapFault(faultAccetta);


  123.                 GestoreErroreConnettore.gestioneErroreDefaultComponenteIntegrazioneMap.put(key, gestione);
  124.                 return gestione;

  125.             }finally{
  126.                 semaphore.release(lock, "getGestioneErroreDefaultComponenteIntegrazione");
  127.             }
  128.         }
  129.     }

  130.     public static GestioneErrore getGestioneErroreDefaultComponenteCooperazione(IProtocolFactory<?> protocolFactory, ServiceBinding serviceBinding) {

  131.         String key = protocolFactory.getProtocol()+"_"+serviceBinding;

  132.         if(GestoreErroreConnettore.gestioneErroreDefaultComponenteCooperazioneMap.containsKey(key)){
  133.             return GestoreErroreConnettore.gestioneErroreDefaultComponenteCooperazioneMap.get(key);
  134.         }else{

  135.             SemaphoreLock lock = semaphore.acquireThrowRuntime("getGestioneErroreDefaultComponenteCooperazione");
  136.             try {

  137.                 if(GestoreErroreConnettore.gestioneErroreDefaultComponenteCooperazioneMap.containsKey(key)){
  138.                     return GestoreErroreConnettore.gestioneErroreDefaultComponenteCooperazioneMap.get(key);
  139.                 }

  140.                 GestioneErrore gestione = new GestioneErrore();
  141.                 // default si rispedisce
  142.                 gestione.setComportamento(CostantiConfigurazione.GESTIONE_ERRORE_RISPEDISCI_MSG);
  143.                 // si accetta il codice di trasporto 200-299
  144.                 GestioneErroreCodiceTrasporto codiceTrasporto = new GestioneErroreCodiceTrasporto();
  145.                 codiceTrasporto.setComportamento(CostantiConfigurazione.GESTIONE_ERRORE_ACCETTA_MSG);
  146.                 codiceTrasporto.setValoreMinimo(200);
  147.                 boolean isSuccessfulHttpRedirectStatusCode = false;
  148.                 try {
  149.                     isSuccessfulHttpRedirectStatusCode = protocolFactory.createProtocolManager().isSuccessfulHttpRedirectStatusCode(serviceBinding);
  150.                 }catch(Exception e) {
  151.                     log.error(e.getMessage(),e);
  152.                 }
  153.                 if(isSuccessfulHttpRedirectStatusCode) {
  154.                     codiceTrasporto.setValoreMassimo(399);
  155.                 }
  156.                 else {
  157.                     codiceTrasporto.setValoreMassimo(299);
  158.                 }
  159.                 gestione.addCodiceTrasporto(codiceTrasporto);


  160.                 // Per qualsiasi fault si effettua una rispedizione, a meno di fault
  161.                 // integrati in buste errore:

  162.                 // BUSTE ERRORE DI VALIDAZIONE
  163.                 // SoapActor == null
  164.                 // SoapFaultCode == soap:Client
  165.                 // SoapFaultString contain "*_001 - Formato Busta non corretto"

  166.                 org.openspcoop2.protocol.sdk.config.ITraduttore trasl = null;
  167.                 try {
  168.                     trasl = protocolFactory.createTraduttore();
  169.                 }catch(Exception e) {
  170.                     log.error(e.getMessage(),e);
  171.                 }

  172.                 if(trasl!=null) {
  173.                     // per gestire actor Client senza soap:
  174.                     GestioneErroreSoapFault faultClient = new GestioneErroreSoapFault();
  175.                     faultClient.setFaultCode("Client");
  176.                     faultClient.setFaultString(trasl.toString(MessaggiFaultErroreCooperazione.FAULT_STRING_VALIDAZIONE));
  177.                     faultClient.setComportamento(CostantiConfigurazione.GESTIONE_ERRORE_ACCETTA_MSG);
  178.                     gestione.addSoapFault(faultClient);
  179.                     // per gestire actor Client con soap:
  180.                     /**             GestioneErroreSoapFault faultSoapClient = new GestioneErroreSoapFault();
  181.                     //              faultSoapClient.setFaultCode("soap:Client");
  182.                     //              faultSoapClient.setFaultString(trasl.toString(MessaggiFaultErroreCooperazione.FAULT_STRING_VALIDAZIONE));
  183.                     //              faultSoapClient.setComportamento(CostantiConfigurazione.GESTIONE_ERRORE_ACCETTA_MSG);
  184.                     //              gestione.addSoapFault(faultSoapClient);*/

  185.                     // BUSTE ERRORE DI PROCESSAMENTO
  186.                     // SoapActor == null
  187.                     // SoapFaultCode == soap:Server
  188.                     // SoapFaultString contain "*_300 - Errore nel processamento del messaggio"

  189.                     // per gestire actor Server senza soap:
  190.                     GestioneErroreSoapFault faultServer = new GestioneErroreSoapFault();
  191.                     faultServer.setFaultCode("Server");
  192.                     faultServer.setFaultString(trasl.toString(MessaggiFaultErroreCooperazione.FAULT_STRING_PROCESSAMENTO));
  193.                     faultServer.setComportamento(CostantiConfigurazione.GESTIONE_ERRORE_RISPEDISCI_MSG);
  194.                     gestione.addSoapFault(faultServer);
  195.                     // per gestire actor Server con soap:
  196.                     /**             GestioneErroreSoapFault faultSoapServer = new GestioneErroreSoapFault();
  197.                     //              faultSoapServer.setFaultCode("soap:Server");
  198.                     //              faultSoapServer.setFaultString(trasl.toString(MessaggiFaultErroreCooperazione.FAULT_STRING_PROCESSAMENTO));
  199.                     //              faultSoapServer.setComportamento(CostantiConfigurazione.GESTIONE_ERRORE_RISPEDISCI_MSG);
  200.                     //              gestione.addSoapFault(faultSoapServer);*/
  201.                 }

  202.                 GestoreErroreConnettore.gestioneErroreDefaultComponenteCooperazioneMap.put(key, gestione);
  203.                 return gestione;

  204.             }finally {
  205.                 semaphore.release(lock, "getGestioneErroreDefaultComponenteCooperazione");
  206.             }
  207.         }
  208.     }




  209.     /**
  210.      * Controlla se la consegna ha generato un errore dovuto al connettore/codiceTrasporto/SoapFault
  211.      * Se vi e' stato un errore controlla se deve essere rieffettuata la consegna del messaggio.
  212.      * In tal caso imposta il messaggio sulla riconsegna (RISPEDIZIONE).
  213.      * Viene anche impostato il msg di errore dovuto ad una consegna errata.
  214.      *
  215.      * @param gestioneErrore Gestione Errore
  216.      * @param msgErroreConnettore Messaggio di errore fornito con il connettore
  217.      * @param connectorSender connettore
  218.      * @return true se la consegna e' stata effettuata con successo, false altrimenti.
  219.      */
  220.     public boolean verificaConsegna(GestioneErrore gestioneErrore,String msgErroreConnettore,Exception eccezioneErroreConnettore,
  221.             IConnettore connectorSender) throws GestoreMessaggiException,UtilsException{    

  222.         if(eccezioneErroreConnettore!=null) {
  223.             // nop
  224.         }
  225.        
  226.         long codiceTrasporto = connectorSender.getCodiceTrasporto();
  227.         OpenSPCoop2Message messageResponse = connectorSender.getResponse();
  228.         String protocolloConnettore = connectorSender.getProtocollo();
  229.        
  230.         // ESITO CONNETTORE:
  231.         // Se ho un errore sul connettore:
  232.         // - la consegna non e' andata a buon fine (return false)
  233.         // - errore = errore del connettore
  234.         // - viene effettuata la riconsegna solo se this.comportamento=rispedisci

  235.         // ESITO SOAP FAULT:
  236.         // Verifico che esista un SoapFault ed in tal caso un match
  237.         // Se trovo un match:
  238.         // a) la consegna e' andata a buon fine se gestore.comportamento=accetta (return true)
  239.         // b) la consegna non e' andata a buon fine se gestore.comportamento=rispedisci (return false)
  240.         //    - errore = SoapFault codice: xxx actor: xxx
  241.         //    - viene effettuata la riconsegna solo se gestore.comportamento=rispedisci
  242.         // Se non trovo un match controllo il codice di trasporto.

  243.         // ESITO CODICE TRASPORTO:
  244.         // Altrimenti verifico che esista un match sul codice trasporto.
  245.         // Se trovo un match:
  246.         // a) la consegna e' andata a buon fine se gestore.comportamento=accetta (return true)
  247.         // b) la consegna non e' andata a buon fine se gestore.comportamento=rispedisci (return false)
  248.         //    - errore = erroreTrasporto codice: xxx
  249.         //    - viene effettuata la riconsegna solo se gestore.comportamento=rispedisci
  250.         // Se non trovo un match adotto la politica di default:
  251.         // a) la consegna e' andata a buon fine se this.comportamento=accetta (return true)
  252.         // b) la consegna non e' andata a buon fine se this.comportamento=rispedisci (return false)
  253.         //    - errore = erroreTrasporto codice: xxx
  254.         //    - viene effettuata la riconsegna solo se this.comportamento=rispedisci


  255.         // La data di rispedizione sara' rimpiazzata con una vera data se serve.
  256.         java.util.Date now = DateManager.getDate();
  257.         this.dataRispedizione = new Timestamp(now.getTime());


  258.         //  ESITO CONNETTORE:
  259.         if(msgErroreConnettore!=null){
  260.             this.errore = msgErroreConnettore;
  261.             if(CostantiConfigurazione.GESTIONE_ERRORE_RISPEDISCI_MSG.equals(gestioneErrore.getComportamento())){
  262.                 this.riconsegna = true;
  263.                 if(gestioneErrore.getCadenzaRispedizione()!=null){
  264.                     try{
  265.                         int cadenzaRispedizione = Integer.parseInt(gestioneErrore.getCadenzaRispedizione());
  266.                         this.dataRispedizione = new java.sql.Timestamp(now.getTime()+(cadenzaRispedizione*60*1000));
  267.                     }catch(Exception e) {
  268.                         GestoreErroreConnettore.log.error(INTERVALLO_RISPEDIZIONE_NON_IMPOSTATO+e.getMessage(),e);
  269.                     }
  270.                 }
  271.             }else{
  272.                 this.riconsegna = false;
  273.             }
  274.             return false;
  275.         }


  276.         // ESITO SOAP FAULT:
  277.         SOAPBody bodyConFault = null;
  278.         MessageType messageType = null;

  279.         if(messageResponse instanceof OpenSPCoop2SoapMessage ){
  280.             try{
  281.                 messageType = messageResponse.getMessageType();
  282.                 if(messageResponse.castAsSoap().hasSOAPFault()) {
  283.                     bodyConFault = messageResponse.castAsSoap().getSOAPBody();
  284.                 }
  285.             }catch(Exception e){
  286.                 throw new GestoreMessaggiException(e.getMessage(),e);
  287.             }
  288.         }

  289.         if(bodyConFault!=null){
  290.             this.fault = bodyConFault.getFault();
  291.             for(int i=0; i<gestioneErrore.sizeSoapFaultList();i++){
  292.                 GestioneErroreSoapFault gestore = gestioneErrore.getSoapFault(i);
  293.                 boolean match = true;
  294.                 if(gestore.getFaultCode()!=null){
  295.                     String codice = null;
  296.                     String namespaceCodice = null;
  297.                     if(this.fault.getFaultCodeAsQName()!=null){
  298.                         codice = this.fault.getFaultCodeAsQName().getLocalPart();  
  299.                         namespaceCodice = this.fault.getFaultCodeAsQName().getNamespaceURI();
  300.                     }
  301.                     else{
  302.                         codice = this.fault.getFaultCode();
  303.                     }
  304.                     if(namespaceCodice!=null) {
  305.                         // nop
  306.                     }
  307.                    
  308.                     List<String> subCodiceSoap12 = new ArrayList<>();
  309.                     List<String> subCodiceNamespaceSoap12 = new ArrayList<>();
  310.                     if(MessageType.SOAP_12.equals(messageType) &&
  311.                         this.fault.getFaultSubcodes()!=null) {
  312.                         Iterator<QName> it = this.fault.getFaultSubcodes();
  313.                         while (it.hasNext()) {
  314.                             QName qName = it.next();
  315.                             if(qName.getLocalPart()!=null) {
  316.                                 subCodiceSoap12.add(qName.getLocalPart());
  317.                                 subCodiceNamespaceSoap12.add(qName.getNamespaceURI()!=null ? qName.getNamespaceURI() : "");
  318.                             }
  319.                         }
  320.                     }
  321.                            

  322.                     if( !gestore.getFaultCode().equalsIgnoreCase(codice)) {

  323.                         // provo a verificare con regular expr

  324.                         boolean matchRegExpr = false;
  325.                         try {
  326.                             matchRegExpr = RegularExpressionEngine.isMatch(codice, gestore.getFaultCode());
  327.                         }catch(RegExpNotFoundException notFound) {
  328.                             // ignore
  329.                         }
  330.                         catch(Exception e)  {
  331.                             GestoreErroreConnettore.log.error(VERIFICA_ESPRESSIONE_REGOLARE_PREFIX+gestore.getFaultCode()+"' per fault code '"+codice+FALLITA_SUFFIX+e.getMessage(),e);
  332.                         }

  333.                         if(!matchRegExpr) {
  334.                            
  335.                             boolean matchSoap12 = false;
  336.                             if(!subCodiceSoap12.isEmpty()) {
  337.                                 for (String code12 : subCodiceSoap12) {
  338.                                     if( gestore.getFaultCode().equalsIgnoreCase(code12) ) {
  339.                                         matchSoap12 = true;
  340.                                         break;
  341.                                     }
  342.                                     else {
  343.                                         boolean matchRegExprSoap12 = false;
  344.                                         try {
  345.                                             matchRegExprSoap12 = RegularExpressionEngine.isMatch(code12, gestore.getFaultCode());
  346.                                         }catch(RegExpNotFoundException notFound) {
  347.                                             // ignore
  348.                                         }
  349.                                         catch(Exception e)  {
  350.                                             GestoreErroreConnettore.log.error(VERIFICA_ESPRESSIONE_REGOLARE_PREFIX+gestore.getFaultCode()+"' per fault subcode 1.2 '"+code12+FALLITA_SUFFIX+e.getMessage(),e);
  351.                                         }
  352.                                         if(matchRegExprSoap12) {
  353.                                             matchSoap12 = true;
  354.                                             break;
  355.                                         }
  356.                                     }
  357.                                 }
  358.                             }
  359.                            
  360.                             if(!matchSoap12) {
  361.                                 match = false; // non ha il codice definito
  362.                             }
  363.                         }
  364.                     }

  365.                 }
  366.                 if(gestore.getFaultActor()!=null){

  367.                     String actor = this.fault.getFaultActor();

  368.                     if( !gestore.getFaultActor().equalsIgnoreCase(actor)) {

  369.                         // provo a verificare con regular expr

  370.                         boolean matchRegExpr = false;
  371.                         try {
  372.                             matchRegExpr = RegularExpressionEngine.isMatch(actor, gestore.getFaultActor());
  373.                         }catch(RegExpNotFoundException notFound) {
  374.                             // ignore
  375.                         }
  376.                         catch(Exception e)  {
  377.                             GestoreErroreConnettore.log.error(VERIFICA_ESPRESSIONE_REGOLARE_PREFIX+gestore.getFaultActor()+"' per fault actor '"+actor+FALLITA_SUFFIX+e.getMessage(),e);
  378.                         }

  379.                         if(!matchRegExpr) {
  380.                             match = false; // non ha l'actor definito
  381.                         }
  382.                     }

  383.                 }
  384.                 if(gestore.getFaultString()!=null){

  385.                     String faultString = this.fault.getFaultString();

  386.                     if( (faultString==null) ||
  387.                             (
  388.                                     (!faultString.contains(gestore.getFaultString())) &&
  389.                                     (!faultString.contains(gestore.getFaultString().toLowerCase())) &&
  390.                                     (!faultString.contains(gestore.getFaultString().toUpperCase()))
  391.                                     )
  392.                             ) {

  393.                         // provo a verificare con regular expr

  394.                         boolean matchRegExpr = false;
  395.                         try {
  396.                             matchRegExpr = RegularExpressionEngine.isMatch(faultString, gestore.getFaultString());
  397.                         }catch(RegExpNotFoundException notFound) {
  398.                             // ignore
  399.                         }
  400.                         catch(Exception e)  {
  401.                             GestoreErroreConnettore.log.error(VERIFICA_ESPRESSIONE_REGOLARE_PREFIX+gestore.getFaultString()+"' per fault string '"+faultString+FALLITA_SUFFIX+e.getMessage(),e);
  402.                         }

  403.                         if(!matchRegExpr) {
  404.                             match = false; // non ha il fault string definito
  405.                         }
  406.                     }          
  407.                 }

  408.                 if( match ){
  409.                     if(CostantiConfigurazione.GESTIONE_ERRORE_RISPEDISCI_MSG.equals(gestore.getComportamento())){
  410.                         try{
  411.                             this.errore = "errore applicativo, "+SoapUtils.safe_toString(messageResponse.getFactory(), this.fault, GestoreErroreConnettore.log);
  412.                         }catch(Exception e){
  413.                             this.errore = ERRORE_APPLICATIVO_SOAP_FAULT;
  414.                             GestoreErroreConnettore.log.error(SERIALIZZAZIONE_SOAP_FAULT_NON_RIUSCITA+e.getMessage(),e);
  415.                         }
  416.                         this.riconsegna = true;
  417.                         if(gestore.getCadenzaRispedizione()!=null){
  418.                             try{
  419.                                 int cadenzaRispedizione = Integer.parseInt(gestore.getCadenzaRispedizione());
  420.                                 this.dataRispedizione = new java.sql.Timestamp(now.getTime()+(cadenzaRispedizione*60*1000));
  421.                             }catch(Exception e) {
  422.                                 GestoreErroreConnettore.log.error("Intervallo di rispedizione non impostato (soap fault): "+e.getMessage(),e);
  423.                             }
  424.                         }else if(gestioneErrore.getCadenzaRispedizione()!=null){
  425.                             try{
  426.                                 int cadenzaRispedizione = Integer.parseInt(gestioneErrore.getCadenzaRispedizione());
  427.                                 this.dataRispedizione = new java.sql.Timestamp(now.getTime()+(cadenzaRispedizione*60*1000));
  428.                             }catch(Exception e) {
  429.                                 GestoreErroreConnettore.log.error(INTERVALLO_RISPEDIZIONE_NON_IMPOSTATO+e.getMessage(),e);
  430.                             }
  431.                         }
  432.                         return false;
  433.                     }else{
  434.                         this.riconsegna = false;
  435.                         return true;
  436.                     }  
  437.                 }
  438.             }
  439.         }



  440.         // ESITO PROBLEM DETAIL
  441.         ProblemRFC7807 problemReceived = null;

  442.         if(messageResponse!=null) {
  443.             if(messageResponse instanceof OpenSPCoop2RestJsonMessage ){
  444.                 try{
  445.                     OpenSPCoop2RestJsonMessage msg = messageResponse.castAsRestJson();
  446.                     if(msg.hasContent() && msg.isProblemDetailsForHttpApis_RFC7807()) {
  447.                         problemReceived = parseJsonProblemRFC7807(msg);
  448.                     }
  449.                 }catch(Exception e){
  450.                     throw new GestoreMessaggiException(e.getMessage(),e);
  451.                 }
  452.             }
  453.             else if(messageResponse instanceof OpenSPCoop2RestXmlMessage ){
  454.                 try{
  455.                     OpenSPCoop2RestXmlMessage msg = messageResponse.castAsRestXml();
  456.                     if(msg.hasContent() && msg.isProblemDetailsForHttpApis_RFC7807()) {
  457.                         problemReceived = parseXmlProblemRFC7807(msg);
  458.                     }
  459.                 }catch(Exception e){
  460.                     throw new GestoreMessaggiException(e.getMessage(),e);
  461.                 }
  462.             }
  463.         }

  464.         if(problemReceived!=null){
  465.             this.problem = problemReceived;
  466.             for(int i=0; i<gestioneErrore.sizeSoapFaultList();i++){
  467.                 GestioneErroreSoapFault gestore = gestioneErrore.getSoapFault(i);
  468.                 boolean match = true;

  469.                 if(gestore.getFaultCode()!=null){ // status in problem
  470.                     String codice = problemReceived.getStatus()+"";

  471.                     if( !gestore.getFaultCode().equalsIgnoreCase(codice)) {

  472.                         // provo a verificare con regular expr

  473.                         boolean matchRegExpr = false;
  474.                         try {
  475.                             matchRegExpr = RegularExpressionEngine.isMatch(codice, gestore.getFaultCode());
  476.                         }catch(RegExpNotFoundException notFound) {
  477.                             // ignore
  478.                         }
  479.                         catch(Exception e)  {
  480.                             GestoreErroreConnettore.log.error(VERIFICA_ESPRESSIONE_REGOLARE_PREFIX+gestore.getFaultCode()+"' per problem status '"+codice+FALLITA_SUFFIX+e.getMessage(),e);
  481.                         }

  482.                         if(!matchRegExpr) {
  483.                             match = false; // non ha il codice definito
  484.                         }
  485.                     }

  486.                 }

  487.                 if(gestore.getFaultActor()!=null){ // type in problem
  488.                     String actor = problemReceived.getType();

  489.                     if( !gestore.getFaultActor().equalsIgnoreCase(actor)) {

  490.                         // provo a verificare con regular expr

  491.                         boolean matchRegExpr = false;
  492.                         try {
  493.                             matchRegExpr = RegularExpressionEngine.isMatch(actor, gestore.getFaultActor());
  494.                         }catch(RegExpNotFoundException notFound) {
  495.                             // ignore
  496.                         }
  497.                         catch(Exception e)  {
  498.                             GestoreErroreConnettore.log.error(VERIFICA_ESPRESSIONE_REGOLARE_PREFIX+gestore.getFaultActor()+"' per problem type '"+actor+FALLITA_SUFFIX+e.getMessage(),e);
  499.                         }

  500.                         if(!matchRegExpr) {
  501.                             match = false; // non ha l'actor definito
  502.                         }
  503.                     }

  504.                 }

  505.                 if(gestore.getFaultString()!=null) { // claims

  506.                     Properties properties = PropertiesUtilities.convertTextToProperties(gestore.getFaultString());
  507.                     if(properties!=null && properties.size()>0) {

  508.                         Enumeration<?> en = properties.keys();
  509.                         while (en.hasMoreElements()) {
  510.                             String key = (String) en.nextElement();
  511.                             String value = properties.getProperty(key);

  512.                             if(ProblemConstants.CLAIM_TYPE.equals(key)) {
  513.                                 if(problemReceived.getType()==null || "".equals(problemReceived.getType())) {
  514.                                     match = false;
  515.                                     break;
  516.                                 }
  517.                                 else {
  518.                                     if( !problemReceived.getType().equalsIgnoreCase(value)) {
  519.                                         // provo a verificare con regular expr
  520.                                         boolean matchRegExpr = false;
  521.                                         try {
  522.                                             matchRegExpr = RegularExpressionEngine.isMatch(problemReceived.getType(), value);
  523.                                         }catch(RegExpNotFoundException notFound) {
  524.                                             // ignore
  525.                                         }
  526.                                         catch(Exception e)  {
  527.                                             GestoreErroreConnettore.log.error(VERIFICA_ESPRESSIONE_REGOLARE_PREFIX+value+"' per problem type '"+problemReceived.getType()+FALLITA_SUFFIX+e.getMessage(),e);
  528.                                         }
  529.                                         if(!matchRegExpr) {
  530.                                             match = false; // non ha il valore definito
  531.                                             break;
  532.                                         }
  533.                                     }
  534.                                 }
  535.                             }

  536.                             else if(ProblemConstants.CLAIM_TITLE.equals(key)) {
  537.                                 if(problemReceived.getTitle()==null || "".equals(problemReceived.getTitle())) {
  538.                                     match = false;
  539.                                     break;
  540.                                 }
  541.                                 else {
  542.                                     if( !problemReceived.getTitle().equalsIgnoreCase(value)) {
  543.                                         // provo a verificare con regular expr
  544.                                         boolean matchRegExpr = false;
  545.                                         try {
  546.                                             matchRegExpr = RegularExpressionEngine.isMatch(problemReceived.getTitle(), value);
  547.                                         }catch(RegExpNotFoundException notFound) {
  548.                                             // ignore
  549.                                         }
  550.                                         catch(Exception e)  {
  551.                                             GestoreErroreConnettore.log.error(VERIFICA_ESPRESSIONE_REGOLARE_PREFIX+value+"' per problem title '"+problemReceived.getTitle()+FALLITA_SUFFIX+e.getMessage(),e);
  552.                                         }
  553.                                         if(!matchRegExpr) {
  554.                                             match = false; // non ha il valore definito
  555.                                             break;
  556.                                         }
  557.                                     }
  558.                                 }
  559.                             }

  560.                             else if(ProblemConstants.CLAIM_STATUS.equals(key)) {
  561.                                 if(problemReceived.getStatus()==null) {
  562.                                     match = false;
  563.                                     break;
  564.                                 }
  565.                                 else {
  566.                                     if( !problemReceived.getStatus().toString().equalsIgnoreCase(value)) {
  567.                                         // provo a verificare con regular expr
  568.                                         boolean matchRegExpr = false;
  569.                                         try {
  570.                                             matchRegExpr = RegularExpressionEngine.isMatch(problemReceived.getStatus().toString(), value);
  571.                                         }catch(RegExpNotFoundException notFound) {
  572.                                             // ignore
  573.                                         }
  574.                                         catch(Exception e)  {
  575.                                             GestoreErroreConnettore.log.error(VERIFICA_ESPRESSIONE_REGOLARE_PREFIX+value+"' per problem status '"+problemReceived.getStatus().toString()+FALLITA_SUFFIX+e.getMessage(),e);
  576.                                         }
  577.                                         if(!matchRegExpr) {
  578.                                             match = false; // non ha il valore definito
  579.                                             break;
  580.                                         }
  581.                                     }
  582.                                 }
  583.                             }

  584.                             else if(ProblemConstants.CLAIM_DETAIL.equals(key)) {
  585.                                 if(problemReceived.getDetail()==null || "".equals(problemReceived.getDetail())) {
  586.                                     match = false;
  587.                                     break;
  588.                                 }
  589.                                 else {
  590.                                     if( !problemReceived.getDetail().equalsIgnoreCase(value)) {
  591.                                         // provo a verificare con regular expr
  592.                                         boolean matchRegExpr = false;
  593.                                         try {
  594.                                             matchRegExpr = RegularExpressionEngine.isMatch(problemReceived.getDetail(), value);
  595.                                         }catch(RegExpNotFoundException notFound) {
  596.                                             // ignore
  597.                                         }
  598.                                         catch(Exception e)  {
  599.                                             GestoreErroreConnettore.log.error(VERIFICA_ESPRESSIONE_REGOLARE_PREFIX+value+"' per problem detail '"+problemReceived.getDetail()+FALLITA_SUFFIX+e.getMessage(),e);
  600.                                         }
  601.                                         if(!matchRegExpr) {
  602.                                             match = false; // non ha il valore definito
  603.                                             break;
  604.                                         }
  605.                                     }
  606.                                 }
  607.                             }

  608.                             else if(ProblemConstants.CLAIM_INSTANCE.equals(key)) {
  609.                                 if(problemReceived.getInstance()==null || "".equals(problemReceived.getInstance())) {
  610.                                     match = false;
  611.                                     break;
  612.                                 }
  613.                                 else {
  614.                                     if( !problemReceived.getInstance().equalsIgnoreCase(value)) {
  615.                                         // provo a verificare con regular expr
  616.                                         boolean matchRegExpr = false;
  617.                                         try {
  618.                                             matchRegExpr = RegularExpressionEngine.isMatch(problemReceived.getInstance(), value);
  619.                                         }catch(RegExpNotFoundException notFound) {
  620.                                             // ignore
  621.                                         }
  622.                                         catch(Exception e)  {
  623.                                             GestoreErroreConnettore.log.error(VERIFICA_ESPRESSIONE_REGOLARE_PREFIX+value+"' per problem instance '"+problemReceived.getInstance()+FALLITA_SUFFIX+e.getMessage(),e);
  624.                                         }
  625.                                         if(!matchRegExpr) {
  626.                                             match = false; // non ha il valore definito
  627.                                             break;
  628.                                         }
  629.                                     }
  630.                                 }
  631.                             }

  632.                             else {

  633.                                 if(problemReceived.getCustom()==null || problemReceived.getCustom().isEmpty() || !problemReceived.getCustom().containsKey(key)) {
  634.                                     match = false;
  635.                                     break;
  636.                                 }
  637.                                 else {
  638.                                     Object valoreClaim = problemReceived.getCustom().get(key);
  639.                                     if(valoreClaim==null) {
  640.                                         match = false;
  641.                                         break;
  642.                                     }
  643.                                     else {
  644.                                         String v = valoreClaim.toString();
  645.                                         if( !v.equalsIgnoreCase(value)) {
  646.                                             // provo a verificare con regular expr
  647.                                             boolean matchRegExpr = false;
  648.                                             try {
  649.                                                 matchRegExpr = RegularExpressionEngine.isMatch(v, value);
  650.                                             }catch(RegExpNotFoundException notFound) {
  651.                                                 // ignore
  652.                                             }
  653.                                             catch(Exception e)  {
  654.                                                 GestoreErroreConnettore.log.error(VERIFICA_ESPRESSIONE_REGOLARE_PREFIX+value+"' per problem '"+key+"' '"+v+FALLITA_SUFFIX+e.getMessage(),e);
  655.                                             }
  656.                                             if(!matchRegExpr) {
  657.                                                 match = false; // non ha il valore definito
  658.                                                 break;
  659.                                             }
  660.                                         }
  661.                                     }
  662.                                 }

  663.                             }
  664.                         }

  665.                     }
  666.                 }

  667.                 if( match ){
  668.                     if(CostantiConfigurazione.GESTIONE_ERRORE_RISPEDISCI_MSG.equals(gestore.getComportamento())){
  669.                         try{
  670.                             this.errore = "errore applicativo, "+problemReceived.getRaw();
  671.                         }catch(Exception e){
  672.                             this.errore = "errore applicativo (Problem Detail RFC 7807)";
  673.                             GestoreErroreConnettore.log.error("Serializzazione Problem Detail RFC 7807 non riuscita: "+e.getMessage(),e);
  674.                         }
  675.                         this.riconsegna = true;
  676.                         if(gestore.getCadenzaRispedizione()!=null){
  677.                             try{
  678.                                 int cadenzaRispedizione = Integer.parseInt(gestore.getCadenzaRispedizione());
  679.                                 this.dataRispedizione = new java.sql.Timestamp(now.getTime()+(cadenzaRispedizione*60*1000));
  680.                             }catch(Exception e) {
  681.                                 GestoreErroreConnettore.log.error("Intervallo di rispedizione non impostato (problem detail): "+e.getMessage(),e);
  682.                             }
  683.                         }else if(gestioneErrore.getCadenzaRispedizione()!=null){
  684.                             try{
  685.                                 int cadenzaRispedizione = Integer.parseInt(gestioneErrore.getCadenzaRispedizione());
  686.                                 this.dataRispedizione = new java.sql.Timestamp(now.getTime()+(cadenzaRispedizione*60*1000));
  687.                             }catch(Exception e) {
  688.                                 GestoreErroreConnettore.log.error(INTERVALLO_RISPEDIZIONE_NON_IMPOSTATO+e.getMessage(),e);
  689.                             }
  690.                         }
  691.                         return false;
  692.                     }else{
  693.                         this.riconsegna = false;
  694.                         return true;
  695.                     }  
  696.                 }

  697.             }
  698.         }



  699.         // ESITO CODICE TRASPORTO:
  700.         for(int i=0; i<gestioneErrore.sizeCodiceTrasportoList();i++){

  701.             GestioneErroreCodiceTrasporto gestore = gestioneErrore.getCodiceTrasporto(i);
  702.             long valoreMinimo = Long.MIN_VALUE;
  703.             long valoreMassimo = Long.MAX_VALUE;
  704.             if(gestore.getValoreMinimo()!=null)
  705.                 valoreMinimo = gestore.getValoreMinimo().longValue();
  706.             if(gestore.getValoreMassimo()!=null)
  707.                 valoreMassimo = gestore.getValoreMassimo().longValue();

  708.             if(codiceTrasporto>=valoreMinimo &&
  709.                     codiceTrasporto<=valoreMassimo){

  710.                 // match
  711.                 if(CostantiConfigurazione.GESTIONE_ERRORE_RISPEDISCI_MSG.equals(gestore.getComportamento())){
  712.                     /**this.errore = "errore di trasporto, codice "+codiceTrasporto;*/
  713.                     this.errore = "errore "+formatProtocolloConnettore(protocolloConnettore)+codiceTrasporto;
  714.                     if(this.fault!=null){
  715.                         try{
  716.                             this.errore = this.errore + " (" +SoapUtils.safe_toString(messageResponse.getFactory(), this.fault, GestoreErroreConnettore.log)+ ")";
  717.                         }catch(Exception e){
  718.                             this.errore = ERRORE_APPLICATIVO_SOAP_FAULT;
  719.                             GestoreErroreConnettore.log.error(SERIALIZZAZIONE_SOAP_FAULT_NON_RIUSCITA+e.getMessage(),e);
  720.                         }
  721.                     }
  722.                     this.riconsegna = true;
  723.                     if(gestore.getCadenzaRispedizione()!=null){
  724.                         try{
  725.                             int cadenzaRispedizione = Integer.parseInt(gestore.getCadenzaRispedizione());
  726.                             this.dataRispedizione = new java.sql.Timestamp(now.getTime()+(cadenzaRispedizione*60*1000));
  727.                         }catch(Exception e) {
  728.                             GestoreErroreConnettore.log.error("Intervallo di rispedizione non impostato (codice di trasporto): "+e.getMessage(),e);
  729.                         }
  730.                     }else if(gestioneErrore.getCadenzaRispedizione()!=null){
  731.                         try{
  732.                             int cadenzaRispedizione = Integer.parseInt(gestioneErrore.getCadenzaRispedizione());
  733.                             this.dataRispedizione = new java.sql.Timestamp(now.getTime()+(cadenzaRispedizione*60*1000));
  734.                         }catch(Exception e) {
  735.                             GestoreErroreConnettore.log.error(INTERVALLO_RISPEDIZIONE_NON_IMPOSTATO+e.getMessage(),e);
  736.                         }
  737.                     }
  738.                     return false;
  739.                 }else{
  740.                     this.riconsegna = false;
  741.                     return true;
  742.                 }
  743.             }
  744.         }


  745.         // Match non trovato, assumo comportamento di default
  746.         if(CostantiConfigurazione.GESTIONE_ERRORE_RISPEDISCI_MSG.equals(gestioneErrore.getComportamento())){
  747.             /**this.errore = "errore di trasporto, codice "+codiceTrasporto;*/
  748.             this.errore = "errore "+formatProtocolloConnettore(protocolloConnettore)+codiceTrasporto;
  749.             if(this.fault!=null){
  750.                 try{
  751.                     this.errore = this.errore + " (" +SoapUtils.safe_toString(messageResponse.getFactory(), this.fault, GestoreErroreConnettore.log)+ ")";
  752.                 }catch(Exception e){
  753.                     this.errore = ERRORE_APPLICATIVO_SOAP_FAULT;
  754.                     GestoreErroreConnettore.log.error(SERIALIZZAZIONE_SOAP_FAULT_NON_RIUSCITA+e.getMessage(),e);
  755.                 }
  756.             }
  757.             this.riconsegna = true;
  758.             if(gestioneErrore.getCadenzaRispedizione()!=null){
  759.                 try{
  760.                     int cadenzaRispedizione = Integer.parseInt(gestioneErrore.getCadenzaRispedizione());
  761.                     this.dataRispedizione = new java.sql.Timestamp(now.getTime()+(cadenzaRispedizione*60*1000));
  762.                 }catch(Exception e) {
  763.                     GestoreErroreConnettore.log.error(INTERVALLO_RISPEDIZIONE_NON_IMPOSTATO+e.getMessage(),e);
  764.                 }
  765.             }
  766.             return false;
  767.         }else{
  768.             this.riconsegna = false;
  769.             return true;
  770.         }


  771.     }

  772.     private ProblemRFC7807 parseJsonProblemRFC7807 (OpenSPCoop2RestJsonMessage msg) throws MessageException, MessageNotSupportedException{
  773.         try {
  774.             JsonDeserializer deserializer = new JsonDeserializer();
  775.             return deserializer.fromString(msg.getContent(), false);
  776.         }catch(Exception e) {
  777.             GestoreErroreConnettore.log.error("Parsing problem details (RFC7807) JSON:["+msg.getContent()+"] fallita: "+e.getMessage(),e);
  778.             return null;
  779.         }
  780.     }
  781.     private ProblemRFC7807 parseXmlProblemRFC7807 (OpenSPCoop2RestXmlMessage msg) throws MessageException, MessageNotSupportedException{
  782.         XmlDeserializer deserializer = new XmlDeserializer();
  783.         try {
  784.             return deserializer.fromNode(msg.getContent(), false);
  785.         }catch(Exception e) {
  786.             GestoreErroreConnettore.log.error("Parsing problem details (RFC7807) XML["+msg.getContent()+"] fallita: "+e.getMessage(),e);
  787.             return null;
  788.         }
  789.     }
  790.    
  791.     /**
  792.      * Motivo errore consegna
  793.      *
  794.      * @return motivo errore consegna
  795.      */
  796.     public String getErrore() {
  797.         return this.errore;
  798.     }
  799.     /**
  800.      * Indicazione se il msg deve essere riconsegnato o meno
  801.      *
  802.      * @return true se il msg deve essere riconsegnato
  803.      */
  804.     public boolean isRiconsegna() {
  805.         return this.riconsegna;
  806.     }

  807.     /**
  808.      * Data di Rispedizione
  809.      *
  810.      * @return data di spedizione se il msg deve essere riconsegnato
  811.      */
  812.     public java.sql.Timestamp getDataRispedizione() {
  813.         return this.dataRispedizione;
  814.     }

  815.     /**
  816.      * @return the fault
  817.      */
  818.     public SOAPFault getFault() {
  819.         return this.fault;
  820.     }

  821.     /**
  822.      * @return the problem
  823.      */
  824.     public ProblemRFC7807 getProblem() {
  825.         return this.problem;
  826.     }


  827.     public static String formatProtocolloConnettore(String protocollo) {
  828.         if(protocollo==null || StringUtils.isEmpty(protocollo)) {
  829.             return "";
  830.         }
  831.         return protocollo+" ";
  832.     }
  833. }