EsitoBuilder.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.protocol.basic.builder;

  21. import java.util.List;

  22. import javax.xml.soap.SOAPBody;
  23. import javax.xml.soap.SOAPFault;

  24. import org.openspcoop2.core.eccezione.errore_applicativo.Eccezione;
  25. import org.openspcoop2.core.eccezione.errore_applicativo.ErroreApplicativo;
  26. import org.openspcoop2.core.eccezione.errore_applicativo.constants.TipoEccezione;
  27. import org.openspcoop2.core.eccezione.errore_applicativo.utils.XMLUtils;
  28. import org.openspcoop2.message.OpenSPCoop2Message;
  29. import org.openspcoop2.message.OpenSPCoop2MessageFactory;
  30. import org.openspcoop2.message.OpenSPCoop2SoapMessage;
  31. import org.openspcoop2.message.constants.MessageRole;
  32. import org.openspcoop2.message.constants.ServiceBinding;
  33. import org.openspcoop2.message.soap.SoapUtils;
  34. import org.openspcoop2.protocol.basic.BasicComponentFactory;
  35. import org.openspcoop2.protocol.basic.Costanti;
  36. import org.openspcoop2.protocol.sdk.IProtocolFactory;
  37. import org.openspcoop2.protocol.sdk.ProtocolException;
  38. import org.openspcoop2.protocol.sdk.builder.EsitoTransazione;
  39. import org.openspcoop2.protocol.sdk.builder.InformazioniErroriInfrastrutturali;
  40. import org.openspcoop2.protocol.sdk.builder.ProprietaErroreApplicativo;
  41. import org.openspcoop2.protocol.sdk.config.ITraduttore;
  42. import org.openspcoop2.protocol.sdk.constants.CostantiProtocollo;
  43. import org.openspcoop2.protocol.sdk.constants.EsitoTransazioneName;
  44. import org.openspcoop2.protocol.sdk.constants.MessaggiFaultErroreCooperazione;
  45. import org.openspcoop2.protocol.utils.EsitiProperties;
  46. import org.openspcoop2.protocol.utils.EsitoIdentificationModeContextProperty;
  47. import org.openspcoop2.protocol.utils.EsitoIdentificationModeSoapFault;
  48. import org.openspcoop2.protocol.utils.EsitoTransportContextIdentification;
  49. import org.openspcoop2.utils.LimitedInputStream;
  50. import org.openspcoop2.utils.Map;
  51. import org.openspcoop2.utils.TimeoutInputStream;
  52. import org.openspcoop2.utils.rest.problem.JsonDeserializer;
  53. import org.openspcoop2.utils.rest.problem.ProblemRFC7807;
  54. import org.openspcoop2.utils.rest.problem.XmlDeserializer;
  55. import org.openspcoop2.utils.transport.TransportRequestContext;
  56. import org.openspcoop2.utils.xml2json.JsonXmlPathExpressionEngine;
  57. import org.slf4j.Logger;
  58. import org.w3c.dom.Element;
  59. import org.w3c.dom.Node;

  60. /**
  61.  * EsitoBuilder
  62.  *
  63.  * @author Poli Andrea (apoli@link.it)
  64.  * @author $Author$
  65.  * @version $Rev$, $Date$
  66.  */
  67. public class EsitoBuilder extends BasicComponentFactory implements org.openspcoop2.protocol.sdk.builder.IEsitoBuilder {
  68.    
  69.     protected EsitiProperties esitiProperties;
  70.     protected boolean erroreProtocollo = false;
  71.     protected boolean envelopeErroreProtocollo = true;
  72.     protected boolean faultEsterno = false;
  73.    
  74.     public EsitoBuilder(IProtocolFactory<?> protocolFactory) throws ProtocolException{
  75.         super(protocolFactory);
  76.         this.esitiProperties = EsitiProperties.getInstance(this.log, protocolFactory);
  77.         this.erroreProtocollo = this.esitiProperties.isErroreProtocollo();
  78.         this.envelopeErroreProtocollo = this.esitiProperties.isEnvelopeErroreProtocollo();
  79.         this.faultEsterno = this.esitiProperties.isFaultEsterno();
  80.     }


  81.     protected String getTipoContext(TransportRequestContext transportRequestContext) throws ProtocolException{
  82.         return getTipoContext(transportRequestContext, this.esitiProperties, this.log);
  83.     }
  84.     public static String getTipoContext(TransportRequestContext transportRequestContext, EsitiProperties esitiProperties, Logger log) throws ProtocolException{
  85.         String tipoContext = CostantiProtocollo.ESITO_TRANSACTION_CONTEXT_STANDARD;
  86.    
  87.         if(esitiProperties.isSingleTransactionContextCode()) {
  88.             return tipoContext;
  89.         }
  90.        
  91.         if(transportRequestContext!=null){
  92.        
  93.             if(transportRequestContext.getHeaders()!=null && transportRequestContext.getHeaders().size()>0){
  94.                 List<EsitoTransportContextIdentification> list = esitiProperties.getEsitoTransactionContextHeaderTrasportoDynamicIdentification();
  95.                 if(list!=null && !list.isEmpty()){
  96.                     for (EsitoTransportContextIdentification esitoTransportContextIdentification : list) {
  97.                         if(esitoTransportContextIdentification.match(transportRequestContext.getHeaders())){
  98.                             tipoContext = esitoTransportContextIdentification.getType();
  99.                             break;
  100.                         }
  101.                     }
  102.                 }
  103.             }
  104.            
  105.             // urlBased eventualmente sovrascrive l'header
  106.             if(transportRequestContext.getParameters()!=null && transportRequestContext.getParameters().size()>0){
  107.                 List<EsitoTransportContextIdentification> list = esitiProperties.getEsitoTransactionContextHeaderFormBasedDynamicIdentification();
  108.                 if(list!=null && !list.isEmpty()){
  109.                     for (EsitoTransportContextIdentification esitoTransportContextIdentification : list) {
  110.                         if(esitoTransportContextIdentification.match(transportRequestContext.getParameters())){
  111.                             tipoContext = esitoTransportContextIdentification.getType();
  112.                             break;
  113.                         }
  114.                     }
  115.                 }
  116.             }
  117.            
  118.             // trasporto con header openspcoop sovrascrive un valore trovato in precedenza
  119.             if(transportRequestContext.getHeaders()!=null && transportRequestContext.getHeaders().size()>0){
  120.                 String headerName = esitiProperties.getEsitoTransactionContextHeaderTrasportoName();
  121.                 String value = transportRequestContext.getHeaderFirstValue(headerName);
  122.                 if(value!=null){
  123.                     if(!esitiProperties.getEsitiTransactionContextCode().contains(value)){
  124.                         String msg = "Trovato nell'header http un header con nome ["+headerName+"] il cui valore ["+value+"] non rientra tra i tipi di contesto supportati";
  125.                         log.error(msg);
  126.                     }
  127.                     else{
  128.                         tipoContext = value;
  129.                     }
  130.                 }
  131.             }
  132.    
  133.             // urlBased eventualmente sovrascrive l'header
  134.             if(transportRequestContext.getParameters()!=null && transportRequestContext.getParameters().size()>0){
  135.                 String propertyName = esitiProperties.getEsitoTransactionContextFormBasedPropertyName();
  136.                 String value = transportRequestContext.getParameterFirstValue(propertyName);
  137.                 if(value!=null){
  138.                     if(!esitiProperties.getEsitiTransactionContextCode().contains(value)){
  139.                         String msg = "Trovato nella url una proprietà con nome ["+propertyName+"] il cui valore ["+value+"] non rientra tra i tipi di contesto supportati";
  140.                         log.error(msg);
  141.                     }
  142.                     else{
  143.                         tipoContext = value;
  144.                     }
  145.                 }
  146.             }
  147.            
  148.         }
  149.        
  150.         return tipoContext;
  151.     }
  152.    
  153.     private EsitoTransazione getEsitoErroreGenerale(InformazioniErroriInfrastrutturali informazioniErroriInfrastrutturali, String tipoContext) throws ProtocolException{
  154.         if(informazioniErroriInfrastrutturali.isRicevutoSoapFaultServerPortaDelegata()){
  155.             if(this.faultEsterno) {
  156.                 return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_SERVER, tipoContext);
  157.             }
  158.             else {
  159.                 return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_PROCESSAMENTO_PDD_5XX, tipoContext);
  160.             }
  161.         }
  162.         else{
  163.             return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_PROCESSAMENTO_PDD_5XX, tipoContext);
  164.         }
  165.     }
  166.    
  167.     @Override
  168.     public EsitoTransazione getEsito(TransportRequestContext transportRequestContext, EsitoTransazioneName name) {
  169.         String tipoContext = null;
  170.         try{
  171.             tipoContext = this.getTipoContext(transportRequestContext);
  172.             return this.esitiProperties.convertToEsitoTransazione(name, tipoContext);
  173.         }catch(Exception e){
  174.             this.log.error("Errore durante la trasformazione in oggetto EsitoTransazione (utilizzo lo standard): "+e.getMessage(),e);
  175.             Integer code = null;
  176.             EsitoTransazioneName tmp = null;
  177.             try{
  178.                 if(tipoContext==null){
  179.                     tipoContext = CostantiProtocollo.ESITO_TRANSACTION_CONTEXT_STANDARD;
  180.                 }
  181.                 code = this.esitiProperties.convertoToCode(name);
  182.                 tmp = name;
  183.             }catch(Exception eInternal){
  184.                 if(tipoContext!=null){
  185.                     // se è uguale a null, l'errore sollevato prima è lo stesso di questo interno
  186.                     this.log.error("Errore durante la trasformazione interna per il codice: "+eInternal.getMessage(),eInternal);
  187.                 }
  188.                 return EsitoTransazione.ESITO_TRANSAZIONE_ERROR;
  189.             }
  190.             try{
  191.                 return new EsitoTransazione(tmp,  code, tipoContext);
  192.             }catch(Exception eInternal){
  193.                 this.log.error("Errore durante la init EsitoTransazione",eInternal);
  194.                 return EsitoTransazione.ESITO_TRANSAZIONE_ERROR;
  195.             }
  196.         }
  197.     }
  198.    
  199.     @Override
  200.     public EsitoTransazione getEsito(TransportRequestContext transportRequestContext,
  201.             int returnCode, ServiceBinding serviceBinding,  
  202.             OpenSPCoop2Message message,
  203.             InformazioniErroriInfrastrutturali informazioniErroriInfrastrutturali, Map<Object> context) throws ProtocolException {
  204.         return getEsito(transportRequestContext,returnCode,serviceBinding,message,null,informazioniErroriInfrastrutturali,context);
  205.     }

  206.     @Override
  207.     public EsitoTransazione getEsito(TransportRequestContext transportRequestContext,
  208.             int returnCode, ServiceBinding serviceBinding,  
  209.             OpenSPCoop2Message message,
  210.             ProprietaErroreApplicativo erroreApplicativo,
  211.             InformazioniErroriInfrastrutturali informazioniErroriInfrastrutturali, Map<Object> context)
  212.             throws ProtocolException {
  213.         try{
  214.            
  215.                    
  216.             OpenSPCoop2SoapMessage soapMessage = null;
  217.             if(message!=null && ServiceBinding.SOAP.equals(message.getServiceBinding())
  218.                     && !message.isForcedEmptyResponse() && message.getForcedResponse()==null){
  219.                 soapMessage = message.castAsSoap();
  220.             }
  221.                        
  222.             if(informazioniErroriInfrastrutturali==null){
  223.                 // inizializzo con valori di default
  224.                 informazioniErroriInfrastrutturali = new InformazioniErroriInfrastrutturali();
  225.             }
  226.                        
  227.             String tipoContext = this.getTipoContext(transportRequestContext);
  228.            
  229.             // Emissione diagnostici di livello error
  230.             boolean emissioneDiagnosticiError = (context!=null && context.containsKey(org.openspcoop2.core.constants.Costanti.EMESSI_DIAGNOSTICI_ERRORE));
  231.             boolean corsPreflightRequestViaGateway = (context!=null && context.containsKey(org.openspcoop2.core.constants.Costanti.CORS_PREFLIGHT_REQUEST_VIA_GATEWAY));
  232.             boolean corsPreflightRequestTrasparente = (context!=null && context.containsKey(org.openspcoop2.core.constants.Costanti.CORS_PREFLIGHT_REQUEST_TRASPARENTE));
  233.            
  234.             // Tipo di esito OK
  235.             EsitoTransazione returnEsitoOk = null;
  236.             if(emissioneDiagnosticiError){
  237.                 returnEsitoOk = this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.OK_PRESENZA_ANOMALIE, tipoContext);
  238.             }
  239.             else if(corsPreflightRequestViaGateway) {
  240.                 returnEsitoOk = this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.CORS_PREFLIGHT_REQUEST_VIA_GATEWAY, tipoContext);
  241.             }
  242.             else if(corsPreflightRequestTrasparente) {
  243.                 returnEsitoOk = this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.CORS_PREFLIGHT_REQUEST_TRASPARENTE, tipoContext);
  244.             }
  245.             else {
  246.                 returnEsitoOk = this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.OK, tipoContext);
  247.             }
  248.            
  249.             // Esito 4xx
  250.             EsitoTransazione esitoErrore4xx = this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_PROCESSAMENTO_PDD_4XX, tipoContext);
  251.            
  252.             // Esito 5xx
  253.             EsitoTransazione esitoErrore5xx = this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_PROCESSAMENTO_PDD_5XX, tipoContext);
  254.            
  255.             // Una richiesta o risposta malformata deve essere immediatamente riconosciuta prima di analizzare il contesto
  256.             // Ancora prima però deve essere riconosciuto un caso di LimitedStream e il caso di ReadTimeout che altrimeni rientra in un contenuto malformato
  257.             if(context!=null){
  258.                 if(context.containsKey(LimitedInputStream.ERROR_MSG_KEY)){
  259.                     String limitedExceededMessage = (String) context.get(LimitedInputStream.ERROR_MSG_KEY);
  260.                     if(
  261.                         (limitedExceededMessage!=null && limitedExceededMessage.startsWith(org.openspcoop2.core.constants.Costanti.PREFIX_LIMITED_REQUEST))
  262.                         ||
  263.                         (limitedExceededMessage!=null && limitedExceededMessage.startsWith(org.openspcoop2.core.constants.Costanti.PREFIX_LIMITED_RESPONSE))
  264.                     ) {
  265.                         return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.CONTROLLO_TRAFFICO_POLICY_VIOLATA, tipoContext);
  266.                     }
  267.                 }
  268.                 else if(context.containsKey(org.openspcoop2.core.controllo_traffico.constants.Costanti.PDD_CONTEXT_NAME_CONTROLLO_TRAFFICO_VIOLAZIONE) || context.containsKey(TimeoutInputStream.ERROR_MSG_KEY)){

  269.                     String timeoutExceededMessage = null;
  270.                     if(context.containsKey(org.openspcoop2.core.controllo_traffico.constants.Costanti.PDD_CONTEXT_NAME_CONTROLLO_TRAFFICO_VIOLAZIONE)) {
  271.                         timeoutExceededMessage = (String) context.get(org.openspcoop2.core.controllo_traffico.constants.Costanti.PDD_CONTEXT_NAME_CONTROLLO_TRAFFICO_VIOLAZIONE);
  272.                         if(org.openspcoop2.core.controllo_traffico.constants.Costanti.PDD_CONTEXT_VALUE_CONNECTION_TIMEOUT.equals(timeoutExceededMessage)) {
  273.                             return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_CONNECTION_TIMEOUT, tipoContext);
  274.                         }
  275.                         else if(org.openspcoop2.core.controllo_traffico.constants.Costanti.PDD_CONTEXT_VALUE_REQUEST_READ_TIMEOUT.equals(timeoutExceededMessage)) {
  276.                             return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_REQUEST_TIMEOUT, tipoContext);
  277.                         }
  278.                         else if(org.openspcoop2.core.controllo_traffico.constants.Costanti.PDD_CONTEXT_VALUE_RESPONSE_READ_TIMEOUT.equals(timeoutExceededMessage) ||
  279.                                 org.openspcoop2.core.controllo_traffico.constants.Costanti.PDD_CONTEXT_VALUE_READ_TIMEOUT.equals(timeoutExceededMessage)) {
  280.                             return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_RESPONSE_TIMEOUT, tipoContext);
  281.                         }
  282.                         else if(org.openspcoop2.core.controllo_traffico.constants.Costanti.PDD_CONTEXT_VALUE_POLICY_IN_ERRORE.equals(timeoutExceededMessage)) {
  283.                             if(context.containsKey(org.openspcoop2.core.controllo_traffico.constants.Costanti.PDD_CONTEXT_VALUE_POLICY_IN_ERROR_ESITO)) {
  284.                                 EsitoTransazioneName esito = (EsitoTransazioneName) context.get(org.openspcoop2.core.controllo_traffico.constants.Costanti.PDD_CONTEXT_VALUE_POLICY_IN_ERROR_ESITO);
  285.                                 return this.esitiProperties.convertToEsitoTransazione(esito, tipoContext);
  286.                             }
  287.                             else {
  288.                                 return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_PROCESSAMENTO_PDD_5XX, tipoContext);
  289.                             }
  290.                         }
  291.                     }
  292.                     timeoutExceededMessage = (String) context.get(LimitedInputStream.ERROR_MSG_KEY);
  293.                     if(timeoutExceededMessage!=null && timeoutExceededMessage.startsWith(org.openspcoop2.core.constants.Costanti.PREFIX_TIMEOUT_REQUEST)) {
  294.                         return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_REQUEST_TIMEOUT, tipoContext);
  295.                     }
  296.                     else if(timeoutExceededMessage!=null && timeoutExceededMessage.startsWith(org.openspcoop2.core.constants.Costanti.PREFIX_TIMEOUT_RESPONSE)) {
  297.                         return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_RESPONSE_TIMEOUT, tipoContext);
  298.                     }
  299.                 }
  300.             }
  301.             if(informazioniErroriInfrastrutturali.isContenutoRichiestaNonRiconosciuto()){
  302.                 return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.CONTENUTO_RICHIESTA_NON_RICONOSCIUTO, tipoContext);
  303.             }
  304.             else if(informazioniErroriInfrastrutturali.isContenutoRispostaNonRiconosciuto()){
  305.                 return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.CONTENUTO_RISPOSTA_NON_RICONOSCIUTO, tipoContext);
  306.             }
  307.            
  308.             // Devo riconoscere eventuali codifiche custom inserite nel contesto
  309.             if(context!=null){
  310.                 List<Integer> customCodeForContextProperty = this.esitiProperties.getEsitiCodeForContextPropertyIdentificationMode();
  311.                 if(customCodeForContextProperty!=null && !customCodeForContextProperty.isEmpty()){
  312.                     for (Integer customCode : customCodeForContextProperty) {
  313.                         List<EsitoIdentificationModeContextProperty> l = this.esitiProperties.getEsitoIdentificationModeContextPropertyList(customCode);
  314.                         if(l!=null && !l.isEmpty()){
  315.                             for (EsitoIdentificationModeContextProperty esitoIdentificationModeContextProperty : l) {
  316.                                 try{
  317.                                     Object p = context.get(esitoIdentificationModeContextProperty.getMapKey());
  318.                                     if(p instanceof String){
  319.                                         String pS = (String) p;
  320.                                         if(esitoIdentificationModeContextProperty.getValue()==null ||
  321.                                                 esitoIdentificationModeContextProperty.getValue().equals(pS)){
  322.                                             // match
  323.                                            
  324.                                             EsitoTransazioneName esitoTransazioneName = esitoIdentificationModeContextProperty.getEsito();
  325.                                             if(esitoTransazioneName==null) {
  326.                                                 esitoTransazioneName = EsitoTransazioneName.CUSTOM;
  327.                                             }
  328.                                            
  329.                                             EsitoTransazione esito = this.esitiProperties.convertToEsitoTransazione(esitoTransazioneName, customCode, tipoContext);
  330.                                            
  331.                                             // ritorno immediatamente se l'esito e' un esito non 'ok'
  332.                                             // altrimenti verifico se primo ho avuto un errore che altrimenti ha priorita' rispetto al marcare la transazione con questo esito
  333.                                             List<Integer> esitiOk = this.esitiProperties.getEsitiCodeOk();
  334.                                             boolean found = false;
  335.                                             for (Integer intValue : esitiOk) {
  336.                                                 if(intValue.intValue() == customCode.intValue()) {
  337.                                                     found = true;
  338.                                                 }
  339.                                             }
  340.                                             if(!found) {
  341.                                                 return esito;
  342.                                             }
  343.                                             else {
  344.                                                 returnEsitoOk = esito;
  345.                                             }
  346.                                         }
  347.                                     }
  348.                                 }catch(Exception t){
  349.                                     String msgError = "Errore durante l'identificazione dell'esito: "+t.getMessage();
  350.                                     this.log.error(msgError,t);
  351.                                 }
  352.                             }
  353.                         }
  354.                     }
  355.                 }
  356.                
  357.                 // Eventuale possibilità di definire un errore generico sulle classi custom (es. handler)
  358.                 Object p = context.get(org.openspcoop2.core.constants.Costanti.ERRORE_GENERICO);
  359.                 if(p instanceof String){
  360.                     String pS = (String) p;
  361.                     if("true".equalsIgnoreCase(pS)) {
  362.                         return esitoErrore5xx;
  363.                     }
  364.                 }
  365.             }
  366.            
  367.            
  368.             // viene verificato prima del contesto, poiche' una richiesta o risposta malformata deve essere segnalata anche se avviene all'interno delle altre funzionalità
  369. /**         if(informazioniErroriInfrastrutturali.isContenutoRichiestaNonRiconosciuto()){
  370. //              return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.CONTENUTO_RICHIESTA_NON_RICONOSCIUTO, tipoContext);
  371. //          }
  372. //          else if(informazioniErroriInfrastrutturali.isContenutoRispostaNonRiconosciuto()){
  373. //              return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.CONTENUTO_RISPOSTA_NON_RICONOSCIUTO, tipoContext);
  374. //          }
  375. //          else*/
  376.             if(informazioniErroriInfrastrutturali.isErroreUtilizzoConnettore()){
  377.                 return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_INVOCAZIONE, tipoContext);
  378.             }
  379.             else if(soapMessage!=null && soapMessage.hasSOAPFault()){
  380.                
  381.                 Object erroreGovwayObject = message.getContextProperty(org.openspcoop2.message.constants.Costanti.ERRORE_GOVWAY);
  382.                 String erroreGovway = null;
  383.                 if(erroreGovwayObject instanceof String) {
  384.                     erroreGovway = (String) erroreGovwayObject;
  385.                 }
  386.                
  387.                 if(erroreGovway!=null && !"".equals(erroreGovway)) {
  388.                     // verifico se fosse stato registrato un errore di protocollo
  389.                     boolean erroreProtocolloInContext = false;
  390.                     if(this.erroreProtocollo) {
  391.                         Object o = message.getContextProperty(org.openspcoop2.core.constants.Costanti.ERRORE_VALIDAZIONE_PROTOCOLLO);
  392.                         if(o!=null) {
  393.                             if(o instanceof String) {
  394.                                 erroreProtocolloInContext = "true".equalsIgnoreCase((String)o);
  395.                             }
  396.                             else if(o instanceof Boolean) {
  397.                                 erroreProtocolloInContext = (Boolean) o;
  398.                             }
  399.                         }
  400.                     }
  401.                     if(erroreProtocolloInContext) {
  402.                         return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_PROTOCOLLO, tipoContext);
  403.                     }
  404.                 }
  405.                
  406.                 return getEsitoSoapFault(message, soapMessage.getSOAPBody(), erroreApplicativo, informazioniErroriInfrastrutturali, tipoContext);
  407.             }
  408.             else if(message!=null) {
  409.                
  410.                 boolean checkElementSeContieneFaultPdD = true; // fix
  411.                 // bisogna controllare il messaggio xml, json o soap fault per vedere se porta al suo interno un errore di protocollo
  412.                 // e' necessario farlo pero' solo se siamo in un caso FAULT.
  413.                
  414.                 Object erroreGovwayObject = message.getContextProperty(org.openspcoop2.message.constants.Costanti.ERRORE_GOVWAY);
  415.                 String erroreGovway = null;
  416.                 if(erroreGovwayObject instanceof String) {
  417.                     erroreGovway = (String) erroreGovwayObject;
  418.                 }
  419.                
  420.                 Object internalErrorCodeGovWayObject = message.getContextProperty(org.openspcoop2.message.constants.Costanti.ERRORE_GOVWAY_CODE);
  421.                 String internalErrorCodeGovWay = null;
  422.                 if(internalErrorCodeGovWayObject!=null){
  423.                     internalErrorCodeGovWay = (String) internalErrorCodeGovWayObject;
  424.                 }
  425.                
  426.                 if(erroreGovway!=null && !"".equals(erroreGovway)) {
  427.                     // verifico se fosse stato registrato un errore di protocollo
  428.                     boolean erroreProtocolloInContext = false;
  429.                     if(this.erroreProtocollo) {
  430.                         Object o = message.getContextProperty(org.openspcoop2.core.constants.Costanti.ERRORE_VALIDAZIONE_PROTOCOLLO);
  431.                         if(o!=null) {
  432.                             if(o instanceof String) {
  433.                                 erroreProtocolloInContext = "true".equalsIgnoreCase((String)o);
  434.                             }
  435.                             else if(o instanceof Boolean) {
  436.                                 erroreProtocolloInContext = (Boolean) o;
  437.                             }
  438.                         }
  439.                     }
  440.                     if(erroreProtocolloInContext) {
  441.                         return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_PROTOCOLLO, tipoContext);
  442.                     }
  443.                 }
  444.                
  445.                 boolean checkReturnCodeForFault = false;
  446.                 switch (message.getMessageType()) {
  447.                 case SOAP_11:
  448.                 case SOAP_12:
  449.                     // body letto precedentemente
  450.                     if(MessageRole.FAULT.equals(message.getMessageRole())) {
  451.                         if(checkElementSeContieneFaultPdD &&
  452.                             soapMessage!=null) {
  453.                             EsitoTransazione esitoErrore = getEsitoMessaggioApplicativo(message.getFactory(),
  454.                                     erroreApplicativo, soapMessage.getSOAPBody(), tipoContext, erroreGovway, internalErrorCodeGovWay);
  455.                             if(esitoErrore!=null) {
  456.                                 return esitoErrore;
  457.                             }
  458.                         }
  459.                         checkReturnCodeForFault = true;
  460.                     }
  461.                     break;
  462.                 case XML:  
  463.                     if(message.castAsRestXml().isProblemDetailsForHttpApis_RFC7807() && erroreGovway==null) {
  464.                         return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_APPLICATIVO, tipoContext);
  465.                     }
  466.                     if(MessageRole.FAULT.equals(message.getMessageRole())) {
  467.                         if(checkElementSeContieneFaultPdD &&
  468.                             message.castAsRestXml().hasContent()) {
  469.                             EsitoTransazione esitoErrore = getEsitoMessaggioApplicativo(message.getFactory(),
  470.                                     erroreApplicativo, message.castAsRestXml().getContent(), tipoContext, erroreGovway, internalErrorCodeGovWay);
  471.                             if(esitoErrore!=null) {
  472.                                 return esitoErrore;
  473.                             }
  474.                         }
  475.                         checkReturnCodeForFault = true;
  476.                     }
  477.                     break;
  478.                 case JSON:  
  479.                     if(message.castAsRestJson().isProblemDetailsForHttpApis_RFC7807() && erroreGovway==null) {
  480.                         return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_APPLICATIVO, tipoContext);
  481.                     }
  482.                     if(MessageRole.FAULT.equals(message.getMessageRole())) {
  483.                         if(checkElementSeContieneFaultPdD &&
  484.                             message.castAsRestJson().hasContent()) {
  485.                             EsitoTransazione esitoErrore = getEsitoMessaggioApplicativo(erroreApplicativo, message.castAsRestJson().getContent(), tipoContext, erroreGovway, internalErrorCodeGovWay);
  486.                             if(esitoErrore!=null) {
  487.                                 return esitoErrore;
  488.                             }
  489.                         }
  490.                         checkReturnCodeForFault = true;
  491.                     }
  492.                     break;
  493.                 default:
  494.                     if(MessageRole.FAULT.equals(message.getMessageRole())) {
  495.                         checkReturnCodeForFault = true;
  496.                     }
  497.                     break;
  498.                 }
  499.                 if(checkReturnCodeForFault) {
  500.                     if(returnCode>=400 && returnCode<=499) {
  501.                         return esitoErrore4xx;
  502.                     }
  503.                     else {
  504.                         return esitoErrore5xx;
  505.                     }
  506.                 }
  507.                 else {
  508.                     if(returnCode>=200 && returnCode<=299) {
  509.                         return returnEsitoOk;
  510.                     }
  511.                     else if(returnCode>=300 && returnCode<=399) {
  512.                         return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.HTTP_3xx, tipoContext);
  513.                     }
  514.                     else if(returnCode>=400 && returnCode<=499) {
  515.                         return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.HTTP_4xx, tipoContext);
  516.                     }
  517.                     else {
  518.                         return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.HTTP_5xx, tipoContext);
  519.                     }
  520.                 }
  521.             }
  522.             else {
  523.                 if(returnCode>=200 && returnCode<=299) {
  524.                     return returnEsitoOk;
  525.                 }
  526.                 else if(returnCode>=300 && returnCode<=399) {
  527.                     return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.HTTP_3xx, tipoContext);
  528.                 }
  529.                 else if(returnCode>=400 && returnCode<=499) {
  530.                     return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.HTTP_4xx, tipoContext);
  531.                 }
  532.                 else {
  533.                     return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.HTTP_5xx, tipoContext);
  534.                 }
  535.             }
  536.         }catch(Exception e){
  537.             throw new ProtocolException("Identificazione stato non riuscita: "+e.getMessage(),e);
  538.         }
  539.     }

  540.     private EsitoTransazione getEsitoSoapFault(OpenSPCoop2Message message, SOAPBody soapBody,
  541.             ProprietaErroreApplicativo erroreApplicativo, InformazioniErroriInfrastrutturali informazioniErroriInfrastrutturali,
  542.             String tipoContext) throws ProtocolException {
  543.         SOAPFault fault = soapBody.getFault();
  544.         String actor = fault.getFaultActor();
  545.         String reason = fault.getFaultString();
  546.         String codice = null;
  547.         String namespaceCodice = null;
  548.         if(fault.getFaultCodeAsQName()!=null){
  549.             codice = fault.getFaultCodeAsQName().getLocalPart();    
  550.             namespaceCodice = fault.getFaultCodeAsQName().getNamespaceURI();
  551.         }
  552.         else{
  553.             codice = fault.getFaultCode();
  554.         }
  555.         /**System.out.println("ACTOR["+actor+"] REASON["+reason+"] CODICE["+codice+"] namespaceCodice["+namespaceCodice+"]");*/

  556.         Object backwardCompatibilityActorObject = message.getContextProperty(CostantiProtocollo.BACKWARD_COMPATIBILITY_ACTOR);
  557.         String backwardCompatibilityActor = null;
  558.         if(backwardCompatibilityActorObject!=null){
  559.             backwardCompatibilityActor = (String) backwardCompatibilityActorObject;
  560.         }
  561.        
  562.         Object customActorObject = message.getContextProperty(CostantiProtocollo.CUSTOM_ACTOR);
  563.         String customActor = null;
  564.         if(customActorObject!=null){
  565.             customActor = (String) customActorObject;
  566.         }
  567.        
  568.         boolean faultInternalError = actor!=null && org.openspcoop2.message.constants.Costanti.DEFAULT_SOAP_FAULT_ACTOR.equals(actor);
  569.         if(faultInternalError) {
  570.             return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_PROCESSAMENTO_PDD_5XX, tipoContext);
  571.         }
  572.        
  573.         Object erroreGovwayObject = message.getContextProperty(org.openspcoop2.message.constants.Costanti.ERRORE_GOVWAY);
  574.         String erroreGovway = null;
  575.         if(erroreGovwayObject instanceof String) {
  576.             erroreGovway = (String) erroreGovwayObject;
  577.         }
  578.        
  579.         boolean faultActorOpenSPCoopV2 = (erroreApplicativo!=null &&
  580.                 erroreApplicativo.getFaultActor()!=null &&
  581.                 erroreApplicativo.getFaultActor().equals(actor));
  582.        
  583.         boolean faultActorBackwardCompatibility = (backwardCompatibilityActor!=null &&
  584.                 backwardCompatibilityActor.equals(actor));
  585.        
  586.         boolean faultActorCustom = (customActor!=null &&
  587.                 customActor.equals(actor));
  588.        
  589.         if(faultActorOpenSPCoopV2 || faultActorBackwardCompatibility || faultActorCustom){

  590.             // L'errore puo' essere generato da GovWay, l'errore puo' essere un :
  591.             // msg di errore 4XX
  592.             // msg di errore 5xx
  593.             // msg dovuto alla ricezione di una busta.

  594.             if(org.openspcoop2.message.constants.Costanti.TIPO_RFC7807.equals(erroreGovway) && MessageRole.FAULT.equals(message.getMessageRole()) &&
  595.                 fault.getDetail()!=null) {
  596.                 List<Node> list = SoapUtils.getNotEmptyChildNodes(message.getFactory(), fault.getDetail(), false);
  597.                 if(list!=null && !list.isEmpty()) {
  598.                     for (Node node : list) {
  599.                         XmlDeserializer xmlDeserializer = new XmlDeserializer();
  600.                         if(xmlDeserializer.isProblemRFC7807(node) && node instanceof Element) {
  601.                             try{
  602.                                 ProblemRFC7807 problem = xmlDeserializer.fromNode(node, false);
  603.                                 Integer status = problem.getStatus();
  604.                                 if(status!=null) {
  605.                                     int valueInt = status.intValue();
  606.                                     if(valueInt>=400 && valueInt<=499){
  607.                                         return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_PROCESSAMENTO_PDD_4XX, tipoContext);
  608.                                     }else if(valueInt>=500 && valueInt<=599){
  609.                                         return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_PROCESSAMENTO_PDD_5XX, tipoContext);
  610.                                     }else{
  611.                                         return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_PROCESSAMENTO_PDD_5XX, tipoContext);
  612.                                     }
  613.                                 }
  614.                                 else {
  615.                                     return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_PROCESSAMENTO_PDD_5XX, tipoContext); // ???
  616.                                 }
  617.                             }catch(Exception e) {
  618.                                 this.log.error("Errore durante l'identificazione dell'esito: "+e.getMessage(),e);
  619.                             }
  620.                         }          
  621.                     }
  622.                 }
  623.             }
  624.            
  625.             if(codice==null){
  626.                 // CASO NON PREVISTO ???
  627.                 return getEsitoErroreGenerale(informazioniErroriInfrastrutturali, tipoContext);
  628.             }
  629.             else{
  630.                 String prefixFaultCode = erroreApplicativo.getFaultPrefixCode();
  631.                 if(prefixFaultCode==null){
  632.                     prefixFaultCode=org.openspcoop2.protocol.basic.Costanti.ERRORE_INTEGRAZIONE_PREFIX_CODE;
  633.                 }
  634.                 boolean prefixOpv2 = codice.startsWith(prefixFaultCode);
  635.                
  636.                 Object backwardCompatibilityPrefixObject = message.getContextProperty(CostantiProtocollo.BACKWARD_COMPATIBILITY_PREFIX_FAULT_CODE);
  637.                 String backwardCompatibilityPrefix = null;
  638.                 if(backwardCompatibilityPrefixObject!=null){
  639.                     backwardCompatibilityPrefix = (String) backwardCompatibilityPrefixObject;
  640.                 }
  641.                 boolean prefixBackwardCompatibility = (backwardCompatibilityPrefix!=null && codice.startsWith(backwardCompatibilityPrefix));
  642.                
  643.                 boolean genericErrorGovWay = false;
  644.                 Object govwayCodeInContextProperty = message.getContextProperty(org.openspcoop2.message.constants.Costanti.ERRORE_GOVWAY_CODE);
  645.                 String govwayInternalErrorCode = null;
  646.                 if(govwayCodeInContextProperty!=null){
  647.                     String internalErrorCode = (String) govwayCodeInContextProperty;
  648.                     genericErrorGovWay = internalErrorCode.startsWith(prefixFaultCode);
  649.                     if(genericErrorGovWay) {
  650.                         govwayInternalErrorCode = internalErrorCode;
  651.                     }
  652.                 }
  653.                
  654.                 if(prefixOpv2 || prefixBackwardCompatibility || genericErrorGovWay){
  655.                     // EccezioneProcessamento
  656.                     String value = null;
  657.                     if(prefixOpv2){
  658.                         value = codice.substring(prefixFaultCode.length());
  659.                     }
  660.                     else if(genericErrorGovWay){
  661.                         value = govwayInternalErrorCode.substring(prefixFaultCode.length());
  662.                     }
  663.                     else{
  664.                         value = codice.substring(backwardCompatibilityPrefix.length());
  665.                     }
  666.                     try{
  667.                         int valueInt = Integer.parseInt(value);
  668.                         if(valueInt>=400 && valueInt<=499){
  669.                             return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_PROCESSAMENTO_PDD_4XX, tipoContext);
  670.                         }else if(valueInt>=500 && valueInt<=599){
  671.                             return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_PROCESSAMENTO_PDD_5XX, tipoContext);
  672.                         }else{
  673.                             return getEsitoErroreGenerale(informazioniErroriInfrastrutturali, tipoContext);
  674.                         }
  675.                     }catch(Throwable t){
  676.                         String error = "Errore calcolato da codice["+codice+"] prefixOpv2["+prefixOpv2+"] prefixFaultCode["+
  677.                                 prefixFaultCode+"] prefixBackwardCompatibility["+prefixBackwardCompatibility+
  678.                                 "] prefixBackwardCompatibility["+prefixBackwardCompatibility+"] value["+value+"]";
  679.                         if(this.log!=null)
  680.                             this.log.error(error+": "+t.getMessage(),t);
  681.                         else{
  682.                             System.err.print(error);
  683.                             t.printStackTrace(System.err);
  684.                         }
  685.                         return getEsitoErroreGenerale(informazioniErroriInfrastrutturali, tipoContext);
  686.                     }
  687.                 }else{
  688.                     // EccezioneBusta
  689.                     if(this.erroreProtocollo) {
  690.                         if(this.envelopeErroreProtocollo) {
  691.                             return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_PROTOCOLLO, tipoContext);
  692.                         }
  693.                         else {
  694.                             return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_APPLICATIVO, tipoContext);
  695.                         }
  696.                     }
  697.                     else {
  698.                         return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_PROCESSAMENTO_PDD_5XX, tipoContext);
  699.                     }
  700.                 }
  701.             }

  702.         }
  703.         else{

  704.             ITraduttore trasl = this.protocolFactory.createTraduttore();
  705.            
  706.             if("Client".equals(codice) && trasl.toString(MessaggiFaultErroreCooperazione.FAULT_STRING_VALIDAZIONE).equals(reason) ){
  707.                 if(this.erroreProtocollo && this.envelopeErroreProtocollo) {
  708.                     return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_PROTOCOLLO, tipoContext);
  709.                 }
  710.                 else {
  711.                     return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_APPLICATIVO, tipoContext);
  712.                 }
  713.             }
  714.             else if("Server".equals(codice) && trasl.toString(MessaggiFaultErroreCooperazione.FAULT_STRING_PROCESSAMENTO).equals(reason) ){
  715.                 if(this.erroreProtocollo && this.envelopeErroreProtocollo) {
  716.                     return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_PROTOCOLLO, tipoContext);
  717.                 }
  718.                 else {
  719.                     return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_APPLICATIVO, tipoContext);
  720.                 }
  721.             }
  722.             else{
  723.                
  724.                 // Devo riconoscere eventuali altre codifiche custom
  725.                 List<Integer> customCodeForSoapFault = this.esitiProperties.getEsitiCodeForSoapFaultIdentificationMode();
  726.                 if(customCodeForSoapFault!=null && !customCodeForSoapFault.isEmpty()){
  727.                     for (Integer customCodeSF : customCodeForSoapFault) {
  728.                         List<EsitoIdentificationModeSoapFault> l = this.esitiProperties.getEsitoIdentificationModeSoapFaultList(customCodeSF);
  729.                         for (int i = 0; i < l.size(); i++) {
  730.                             EsitoIdentificationModeSoapFault e = l.get(i);
  731.                             if(e.getFaultCode()!=null &&
  732.                                 !e.getFaultCode().equals(codice)){
  733.                                 continue;
  734.                             }
  735.                             if(e.getFaultNamespaceCode()!=null &&
  736.                                 !e.getFaultNamespaceCode().equals(namespaceCodice)){
  737.                                 continue;
  738.                             }
  739.                             if(e.getFaultReason()!=null){
  740.                                 if(e.getFaultReasonContains()!=null && e.getFaultReasonContains()){
  741.                                     if(reason==null || !reason.contains(e.getFaultReason())){
  742.                                         continue;
  743.                                     }
  744.                                 }
  745.                                 else{
  746.                                     if(!e.getFaultReason().equals(reason)){
  747.                                         continue;
  748.                                     }
  749.                                 }
  750.                             }
  751.                             if(e.getFaultActor()!=null &&
  752.                                 !e.getFaultActor().equals(actor)){
  753.                                 continue;
  754.                             }
  755.                             if(e.getFaultActorNotDefined()!=null && e.getFaultActorNotDefined() &&
  756.                                 actor!=null){
  757.                                 continue;
  758.                             }
  759.                             // match
  760.                             return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.CUSTOM, customCodeSF, tipoContext);
  761.                         }
  762.                     }
  763.                 }
  764.                
  765.                 if(informazioniErroriInfrastrutturali.isRicevutoSoapFaultServerPortaDelegata()){
  766.                     if(this.faultEsterno) {
  767.                         return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_SERVER, tipoContext);
  768.                     }
  769.                     else {
  770.                         return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_APPLICATIVO, tipoContext);
  771.                     }
  772.                 }
  773.                 else{
  774.                     return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_APPLICATIVO, tipoContext);
  775.                 }
  776.             }

  777.         }
  778.     }
  779.    
  780.     protected EsitoTransazione getEsitoMessaggioApplicativo(OpenSPCoop2MessageFactory messageFactory,
  781.             ProprietaErroreApplicativo erroreApplicativo,SOAPBody body,String tipoContext, String erroreGovway, String internalErrorCodeGovWay) throws ProtocolException{
  782.         if(erroreApplicativo!=null){
  783.             Node childNode = body.getFirstChild();
  784.             if(childNode!=null){
  785.                 return getEsitoMessaggioApplicativo(messageFactory, erroreApplicativo, childNode, tipoContext, erroreGovway, internalErrorCodeGovWay);
  786.             }
  787.         }

  788.         return null;

  789.     }
  790.    
  791.     protected EsitoTransazione getEsitoMessaggioApplicativo(OpenSPCoop2MessageFactory messageFactory,
  792.             ProprietaErroreApplicativo erroreApplicativo,Node childNode,String tipoContext, String erroreGovway, String internalErrorCodeGovWay) throws ProtocolException{
  793.         if(childNode!=null &&
  794.             childNode.getNextSibling()==null){
  795.                
  796.             if(org.openspcoop2.message.constants.Costanti.TIPO_RFC7807.equals(erroreGovway)) {
  797.                 if(childNode instanceof Element) {
  798.                     try{
  799.                         XmlDeserializer xmlDeserializer = new XmlDeserializer();
  800.                         ProblemRFC7807 problem = xmlDeserializer.fromNode(childNode, false);
  801.                         Integer status = problem.getStatus();
  802.                         if(status!=null) {
  803.                             int valueInt = status.intValue();
  804.                             if(valueInt>=400 && valueInt<=499){
  805.                                 return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_PROCESSAMENTO_PDD_4XX, tipoContext);
  806.                             }else if(valueInt>=500 && valueInt<=599){
  807.                                 return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_PROCESSAMENTO_PDD_5XX, tipoContext);
  808.                             }else{
  809.                                 return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_PROCESSAMENTO_PDD_5XX, tipoContext);
  810.                             }
  811.                         }
  812.                         else {
  813.                             return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_PROCESSAMENTO_PDD_5XX, tipoContext); // ???
  814.                         }
  815.                     }catch(Exception e) {
  816.                         this.log.error("Errore durante l'identificazione dell'esito: "+e.getMessage(),e);
  817.                     }
  818.                 }
  819.             }
  820.             else {
  821.                 if(XMLUtils.isErroreApplicativo(childNode)){
  822.                    
  823.                     try{
  824.                         byte[] xml = org.openspcoop2.message.xml.MessageXMLUtils.getInstance(messageFactory).toByteArray(childNode,true);
  825.                         ErroreApplicativo erroreApplicativoObject = XMLUtils.getErroreApplicativo(this.log, xml);
  826.                         Eccezione ecc = erroreApplicativoObject.getException();
  827.                         if(TipoEccezione.PROTOCOL.equals(ecc.getType())){
  828.                             if(this.erroreProtocollo && this.envelopeErroreProtocollo) {
  829.                                 return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_PROTOCOLLO, tipoContext);
  830.                             }
  831.                             else {
  832.                                 return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_PROCESSAMENTO_PDD_5XX, tipoContext);
  833.                             }
  834.                         }
  835.                         else{
  836.                             String value = ecc.getCode().getBase();
  837.                             String prefixFaultCode = erroreApplicativo.getFaultPrefixCode();
  838.                             if(prefixFaultCode==null){
  839.                                 prefixFaultCode=Costanti.ERRORE_INTEGRAZIONE_PREFIX_CODE;
  840.                             }
  841.                             boolean prefixOpv2 = value.startsWith(prefixFaultCode);
  842.                            
  843.                             if(!prefixOpv2 && internalErrorCodeGovWay!=null) {
  844.                                 if(internalErrorCodeGovWay.startsWith(prefixFaultCode)) {
  845.                                     prefixOpv2 = true;
  846.                                     value = internalErrorCodeGovWay;
  847.                                 }                                  
  848.                             }

  849.                             if(prefixOpv2){
  850.                                 value = value.substring(prefixFaultCode.length());
  851.                                 int valueInt = Integer.parseInt(value);
  852.                                 if(valueInt>=400 && valueInt<=499){
  853.                                     return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_PROCESSAMENTO_PDD_4XX, tipoContext);
  854.                                 }else if(valueInt>=500 && valueInt<=599){
  855.                                     return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_PROCESSAMENTO_PDD_5XX, tipoContext);
  856.                                 }else{
  857.                                     return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_PROCESSAMENTO_PDD_5XX, tipoContext);
  858.                                 }
  859.                             }else{
  860.                                 return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_PROCESSAMENTO_PDD_5XX, tipoContext); // ???
  861.                             }
  862.                         }
  863.                        
  864.                     }catch(Exception e){
  865.                         this.log.error("Errore durante l'identificazione dell'esito: "+e.getMessage(),e);
  866.                     }
  867.                    
  868.                 }
  869.                
  870.                 // se arrivo qua provo a vedere se siamo nel caso di un internal Error
  871.                 else if(OpenSPCoop2MessageFactory.isFaultXmlMessage(childNode)) {
  872.                     try{
  873.                         return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_PROCESSAMENTO_PDD_5XX, tipoContext);
  874.                     }catch(Exception e){
  875.                         this.log.error("Errore durante l'identificazione dell'esito: "+e.getMessage(),e);
  876.                     }
  877.                 }
  878.             }
  879.        
  880.         }

  881.         return null;

  882.     }
  883.    
  884.     protected EsitoTransazione getEsitoMessaggioApplicativo(ProprietaErroreApplicativo erroreApplicativo,String jsonBody,String tipoContext, String erroreGovway, String internalErrorCodeGovWay) throws ProtocolException{
  885.        
  886.         if(org.openspcoop2.message.constants.Costanti.TIPO_RFC7807.equals(erroreGovway)) {
  887.             try{
  888.                 JsonDeserializer jsonDeserializer = new JsonDeserializer();
  889.                 ProblemRFC7807 problem = jsonDeserializer.fromString(jsonBody, false);
  890.                 Integer status = problem.getStatus();
  891.                 if(status!=null) {
  892.                     int valueInt = status.intValue();
  893.                     if(valueInt>=400 && valueInt<=499){
  894.                         return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_PROCESSAMENTO_PDD_4XX, tipoContext);
  895.                     }else if(valueInt>=500 && valueInt<=599){
  896.                         return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_PROCESSAMENTO_PDD_5XX, tipoContext);
  897.                     }else{
  898.                         return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_PROCESSAMENTO_PDD_5XX, tipoContext);
  899.                     }
  900.                 }
  901.                 else {
  902.                     return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_PROCESSAMENTO_PDD_5XX, tipoContext); // ???
  903.                 }
  904.             }catch(Exception e) {
  905.                 this.log.error("Errore durante l'identificazione dell'esito: "+e.getMessage(),e);
  906.             }
  907.         }
  908.         else {
  909.        
  910.             // tipo dell'errore
  911.             // $.exception.type
  912.            
  913.             // codice
  914.             //$.exception.code.value
  915.             try {
  916.                 String tipo = JsonXmlPathExpressionEngine.extractAndConvertResultAsString(jsonBody, "$.exception.type", this.log);
  917.                 if(TipoEccezione.PROTOCOL.getValue().equals(tipo)){
  918.                     if(this.erroreProtocollo && this.envelopeErroreProtocollo) {
  919.                         return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_PROTOCOLLO, tipoContext);
  920.                     }
  921.                     else {
  922.                         return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_PROCESSAMENTO_PDD_5XX, tipoContext);
  923.                     }
  924.                 }
  925.                 else{
  926.                     String value = JsonXmlPathExpressionEngine.extractAndConvertResultAsString(jsonBody, "$.exception.code.value", this.log);
  927.                     String prefixFaultCode = erroreApplicativo.getFaultPrefixCode();
  928.                     if(prefixFaultCode==null){
  929.                         prefixFaultCode=Costanti.ERRORE_INTEGRAZIONE_PREFIX_CODE;
  930.                     }
  931.                     boolean prefixOpv2 = value.startsWith(prefixFaultCode);
  932.                    
  933.                     if(!prefixOpv2 && internalErrorCodeGovWay!=null &&
  934.                         internalErrorCodeGovWay.startsWith(prefixFaultCode)) {
  935.                         prefixOpv2 = true;
  936.                         value = internalErrorCodeGovWay;
  937.                     }

  938.                     if(prefixOpv2){
  939.                         value = value.substring(prefixFaultCode.length());
  940.                         int valueInt = Integer.parseInt(value);
  941.                         if(valueInt>=400 && valueInt<=499){
  942.                             return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_PROCESSAMENTO_PDD_4XX, tipoContext);
  943.                         }else if(valueInt>=500 && valueInt<=599){
  944.                             return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_PROCESSAMENTO_PDD_5XX, tipoContext);
  945.                         }else{
  946.                             return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_PROCESSAMENTO_PDD_5XX, tipoContext);
  947.                         }
  948.                     }else{
  949.                         return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_PROCESSAMENTO_PDD_5XX, tipoContext); // ???
  950.                     }
  951.                 }
  952.                
  953.             }catch(Exception e){
  954.                 this.log.error("Errore durante l'identificazione dell'esito: "+e.getMessage(),e);
  955.             }
  956.            
  957.             // se arrivo qua provo a vedere se siamo nel caso di un internal Error
  958.             try{
  959.                 if(OpenSPCoop2MessageFactory.isFaultJsonMessage(jsonBody, this.log)) {
  960.                     return this.esitiProperties.convertToEsitoTransazione(EsitoTransazioneName.ERRORE_PROCESSAMENTO_PDD_5XX, tipoContext);
  961.                 }
  962.             }catch(Exception e){
  963.                 this.log.error("Errore durante l'identificazione dell'esito: "+e.getMessage(),e);
  964.             }
  965.            
  966.         }
  967.            
  968.         return null;
  969.     }
  970.    
  971. }