DumpRaw.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.services;

  21. import java.security.cert.X509Certificate;
  22. import java.text.SimpleDateFormat;
  23. import java.util.ArrayList;
  24. import java.util.Date;
  25. import java.util.Iterator;
  26. import java.util.List;
  27. import java.util.Map;

  28. import org.openspcoop2.core.config.DumpConfigurazione;
  29. import org.openspcoop2.core.config.constants.StatoFunzionalita;
  30. import org.openspcoop2.core.constants.TipoPdD;
  31. import org.openspcoop2.core.id.IDSoggetto;
  32. import org.openspcoop2.message.constants.MessageType;
  33. import org.openspcoop2.pdd.config.OpenSPCoop2Properties;
  34. import org.openspcoop2.pdd.core.PdDContext;
  35. import org.openspcoop2.pdd.logger.Dump;
  36. import org.openspcoop2.pdd.logger.OpenSPCoop2Logger;
  37. import org.openspcoop2.pdd.services.connector.ConnectorException;
  38. import org.openspcoop2.pdd.services.connector.messages.DumpRawConnectorInMessage;
  39. import org.openspcoop2.pdd.services.connector.messages.DumpRawConnectorOutMessage;
  40. import org.openspcoop2.pdd.services.core.AbstractContext;
  41. import org.openspcoop2.protocol.sdk.constants.IDService;
  42. import org.openspcoop2.protocol.sdk.dump.DumpException;
  43. import org.openspcoop2.protocol.sdk.state.URLProtocolContext;
  44. import org.openspcoop2.utils.certificate.CertificateInfo;
  45. import org.openspcoop2.utils.certificate.CertificateUtils;
  46. import org.openspcoop2.utils.date.DateUtils;
  47. import org.openspcoop2.utils.io.DumpByteArrayOutputStream;
  48. import org.openspcoop2.utils.io.notifier.NotifierInputStreamParams;
  49. import org.openspcoop2.utils.transport.Credential;
  50. import org.openspcoop2.utils.transport.TransportUtils;
  51. import org.openspcoop2.utils.transport.http.HttpConstants;
  52. import org.slf4j.Logger;

  53. /**
  54.  * DumpRaw
  55.  *
  56.  * @author Poli Andrea (apoli@link.it)
  57.  * @author $Author$
  58.  * @version $Rev$, $Date$
  59.  */
  60. public class DumpRaw {

  61.     private StringBuilder bfContext = new StringBuilder();
  62.     private StringBuilder bfRequest = new StringBuilder();
  63.     private StringBuilder bfResponse = new StringBuilder();
  64.     private Logger log;
  65.     private boolean pd;
  66.     private String idTransaction;
  67.     private Logger logDump;
  68.     private Dump dump;
  69.     private boolean dumpBinario;
  70.     private boolean dumpBinario_registrazioneDatabase = false;
  71.     private DumpConfigurazione dumpDatabaseConfigurazione;
  72.     private boolean dumpDatabaseRichiestaIngressoHeaders;
  73.     private boolean dumpDatabaseRichiestaIngressoPayload;
  74.     private boolean dumpDatabaseRispostaUscitaHeaders;
  75.     private boolean dumpDatabaseRispostaUscitaPayload;
  76.     private boolean onlyLogFileTraceRichiestaIngressoHeaders = false;
  77.     private boolean onlyLogFileTraceRichiestaIngressoPayload = false;
  78.     private boolean onlyLogFileTraceRispostaUscitaHeaders = false;
  79.     private boolean onlyLogFileTraceRispostaUscitaPayload = false;
  80.     private URLProtocolContext urlProtocolContext;
  81.    
  82.     private IDSoggetto dominio;
  83.     private String modulo;
  84.     private TipoPdD tipoPdD;
  85.    
  86.     public DumpRaw(Logger log,IDSoggetto dominio,String modulo,TipoPdD tipoPdD,
  87.             boolean dumpBinario,
  88.             DumpConfigurazione dumpConfigurazione,
  89.             boolean fileTraceHeaders, boolean fileTraceBody) throws ConnectorException{
  90.         this.log = log;
  91.        
  92.         switch (tipoPdD) {
  93.         case DELEGATA:
  94.             this.pd = true;
  95.             break;
  96.         default:
  97.             this.pd = false;
  98.             break;
  99.         }
  100.        
  101.         this.dumpBinario = dumpBinario;
  102.         if(this.dumpBinario) {
  103.             this.dumpBinario_registrazioneDatabase = OpenSPCoop2Properties.getInstance().isDumpBinarioRegistrazioneDatabase();
  104.         }
  105.        
  106.         if(dumpConfigurazione!=null) {
  107.             if(dumpConfigurazione.getRichiestaIngresso()!=null) {
  108.                 // il parsing deve essere gestito con l'altra modalità, che dovrà gestire anche gli header, senno sul db si troveranno 2 messaggi
  109.                 boolean payloadParsing = StatoFunzionalita.ABILITATO.equals(dumpConfigurazione.getRichiestaIngresso().getPayload()) &&
  110.                         StatoFunzionalita.ABILITATO.equals(dumpConfigurazione.getRichiestaIngresso().getPayloadParsing());
  111.                 if(!payloadParsing) {
  112.                     if(StatoFunzionalita.ABILITATO.equals(dumpConfigurazione.getRichiestaIngresso().getHeaders())){
  113.                         this.dumpDatabaseRichiestaIngressoHeaders = true;
  114.                     }
  115.                     if(StatoFunzionalita.ABILITATO.equals(dumpConfigurazione.getRichiestaIngresso().getPayload())){
  116.                         this.dumpDatabaseRichiestaIngressoPayload = true;
  117.                     }
  118.                 }
  119.             }
  120.             if(dumpConfigurazione.getRispostaUscita()!=null) {
  121.                 // il parsing deve essere gestito con l'altra modalità, che dovrà gestire anche gli header, senno sul db si troveranno 2 messaggi
  122.                 boolean payloadParsing = StatoFunzionalita.ABILITATO.equals(dumpConfigurazione.getRispostaUscita().getPayload()) &&
  123.                         StatoFunzionalita.ABILITATO.equals(dumpConfigurazione.getRispostaUscita().getPayloadParsing());
  124.                 if(!payloadParsing) {
  125.                     if(StatoFunzionalita.ABILITATO.equals(dumpConfigurazione.getRispostaUscita().getHeaders())){
  126.                         this.dumpDatabaseRispostaUscitaHeaders = true;
  127.                     }
  128.                     if(StatoFunzionalita.ABILITATO.equals(dumpConfigurazione.getRispostaUscita().getPayload())){
  129.                         this.dumpDatabaseRispostaUscitaPayload = true;
  130.                     }
  131.                 }
  132.             }
  133.             this.dumpDatabaseConfigurazione = dumpConfigurazione;
  134.         }
  135.        
  136.         if(!this.dumpDatabaseRichiestaIngressoHeaders && (!dumpBinario || !this.dumpBinario_registrazioneDatabase) ) {
  137.             this.onlyLogFileTraceRichiestaIngressoHeaders = fileTraceHeaders;
  138.         }
  139.         if(!this.dumpDatabaseRichiestaIngressoPayload && (!dumpBinario || !this.dumpBinario_registrazioneDatabase) ) {
  140.             this.onlyLogFileTraceRichiestaIngressoPayload = fileTraceBody;
  141.         }
  142.         if(!this.dumpDatabaseRispostaUscitaHeaders && (!dumpBinario || !this.dumpBinario_registrazioneDatabase) ) {
  143.             this.onlyLogFileTraceRispostaUscitaHeaders = fileTraceHeaders;
  144.         }
  145.         if(!this.dumpDatabaseRispostaUscitaPayload && (!dumpBinario || !this.dumpBinario_registrazioneDatabase) ) {
  146.             this.onlyLogFileTraceRispostaUscitaPayload = fileTraceBody;
  147.         }
  148.        
  149.         if(this.dumpBinario) {
  150.             if(this.pd){
  151.                 this.logDump = OpenSPCoop2Logger.getLoggerOpenSPCoopDumpBinarioPD();
  152.             }
  153.             else{
  154.                 this.logDump = OpenSPCoop2Logger.getLoggerOpenSPCoopDumpBinarioPA();
  155.             }
  156.             if(this.logDump==null){
  157.                 throw new ConnectorException("Logger per la registrazione dei dati binari non inizializzato");
  158.             }
  159.         }
  160.        
  161.         this.dominio = dominio;
  162.         this.modulo = modulo;
  163.         this.tipoPdD = tipoPdD;
  164.     }
  165.    
  166.     public boolean isActiveDump() {
  167.         return this.dumpBinario || isRegistrazioneDatabase() || onlyLogFileTrace();
  168.     }
  169.     public boolean isActiveDumpRichiesta() {
  170.         return this.dumpBinario || isRegistrazioneDatabaseRichiesta() || onlyLogFileTraceRichiesta();
  171.     }
  172.     public boolean isActiveDumpRisposta() {
  173.         return this.dumpBinario || isRegistrazioneDatabaseRisposta() || onlyLogFileTraceRisposta();
  174.     }
  175.    
  176.     private boolean isRegistrazioneDatabase() {
  177.         return this.isRegistrazioneDatabaseRichiesta()
  178.                 ||
  179.                this.isRegistrazioneDatabaseRisposta()
  180.                ;
  181.     }
  182.     private boolean isRegistrazioneDatabaseRichiesta() {
  183.         return this.dumpDatabaseRichiestaIngressoHeaders
  184.                 ||
  185.                this.dumpDatabaseRichiestaIngressoPayload
  186.                ;
  187.     }
  188.     private boolean isRegistrazioneDatabaseRisposta() {
  189.         return this.dumpDatabaseRispostaUscitaHeaders
  190.                 ||
  191.                this.dumpDatabaseRispostaUscitaPayload
  192.                ;
  193.     }
  194.    
  195.     private boolean onlyLogFileTrace() {
  196.         return this.onlyLogFileTraceRichiesta()
  197.                 ||
  198.                this.onlyLogFileTraceRisposta()
  199.                ;
  200.     }
  201.     private boolean onlyLogFileTraceRichiesta() {
  202.         return this.onlyLogFileTraceRichiestaIngressoHeaders
  203.                 ||
  204.                this.onlyLogFileTraceRichiestaIngressoPayload
  205.                ;
  206.     }
  207.     private boolean onlyLogFileTraceRisposta() {
  208.         return this.onlyLogFileTraceRispostaUscitaHeaders
  209.                 ||
  210.                this.onlyLogFileTraceRispostaUscitaPayload
  211.                ;
  212.     }
  213.    
  214.     private boolean isDumpBinarioRegistrazioneDatabase() {
  215.         return this.dumpBinario && this.dumpBinario_registrazioneDatabase;
  216.     }
  217.    
  218.     public void setPddContext(String interfaceName, PdDContext pddContext) throws DumpException {
  219.        
  220.         if(!isActiveDump()) {
  221.             return;
  222.         }
  223.        
  224.         if(this.isRegistrazioneDatabase() || this.isDumpBinarioRegistrazioneDatabase() || this.onlyLogFileTrace()) {
  225.             this.dump = new Dump(this.dominio, this.modulo, this.tipoPdD, interfaceName, pddContext, this.dumpDatabaseConfigurazione);
  226.         }  
  227.     }
  228.        
  229.     public void serializeContext(AbstractContext context,String protocol){
  230.        
  231.         if(!isActiveDump()) {
  232.             return;
  233.         }
  234.        
  235.         try{
  236.             Date dataAccettazioneRichiesta = context.getDataAccettazioneRichiesta();
  237.             Date dataIngressoRichiesta = context.getDataIngressoRichiesta();
  238.             String idTransazione = null;
  239.             IDService serviceType = context.getIdModuloAsIDService();
  240.             TipoPdD tipoPdD = context.getTipoPorta();
  241.             if(context.getPddContext()!=null){
  242.                 Object tmp = context.getPddContext().getObject(org.openspcoop2.core.constants.Costanti.ID_TRANSAZIONE);
  243.                 if(tmp!=null){
  244.                     idTransazione = (String) tmp;
  245.                 }
  246.             }
  247.             this.serializeContext(dataAccettazioneRichiesta, dataIngressoRichiesta, idTransazione, serviceType, tipoPdD, protocol);
  248.            
  249.         }catch(Throwable t){
  250.             this.bfContext.append("SerializeContext (AbstractContext) error: "+t.getMessage()+"\n");
  251.             this.log.error("SerializeContext (AbstractContext) error: "+t.getMessage(),t);
  252.         }
  253.     }
  254.     public void serializeContext(Date dataAccettazioneRichiesta,Date dataIngressoRichiesta, String idTransazione, IDService serviceType, TipoPdD tipoPdD,String protocol){
  255.        
  256.         if(!isActiveDump()) {
  257.             return;
  258.         }
  259.        
  260.         if(!this.dumpBinario) {
  261.             return;
  262.         }
  263.        
  264.         this.bfContext.append("------ RequestContext ("+idTransazione+") ------\n");
  265.         this.idTransaction = idTransazione;
  266.        
  267.         try{
  268.             if(dataAccettazioneRichiesta!=null){
  269.                 SimpleDateFormat dateformat = DateUtils.getSimpleDateFormatMs();
  270.                 this.bfContext.append("Date (Accept Request): ");
  271.                 this.bfContext.append(dateformat.format(dataAccettazioneRichiesta));
  272.                 this.bfContext.append("\n");
  273.             }
  274.             if(dataIngressoRichiesta!=null){
  275.                 SimpleDateFormat dateformat = DateUtils.getSimpleDateFormatMs();
  276.                 this.bfContext.append("Date (Received Request): ");
  277.                 this.bfContext.append(dateformat.format(dataIngressoRichiesta));
  278.                 this.bfContext.append("\n");
  279.             }
  280.             if(idTransazione!=null){
  281.                 this.bfContext.append("IdTransaction: ");
  282.                 this.bfContext.append(idTransazione);
  283.                 this.bfContext.append("\n");
  284.             }
  285.             if(protocol!=null){
  286.                 this.bfContext.append("Protocol: ");
  287.                 this.bfContext.append(protocol);
  288.                 this.bfContext.append("\n");
  289.             }
  290.             if(serviceType!=null){
  291.                 this.bfContext.append("Service: ");
  292.                 this.bfContext.append(serviceType.getValue());
  293.                 this.bfContext.append("\n");
  294.             }
  295.             if(tipoPdD!=null){
  296.                 this.bfContext.append("PddType: ");
  297.                 this.bfContext.append(tipoPdD.getTipo());
  298.                 this.bfContext.append("\n");
  299.             }
  300.         }catch(Throwable t){
  301.             this.bfContext.append("SerializeContext error: "+t.getMessage()+"\n");
  302.             this.log.error("SerializeContext error: "+t.getMessage(),t);
  303.         }
  304.        
  305.         this.bfContext.append("------ End-RequestContext ("+this.idTransaction+") ------\n\n");
  306.        
  307.         this.logDump.info(this.bfContext.toString());
  308.        
  309.     }
  310.    
  311.     public void serializeRequest(DumpRawConnectorInMessage req, boolean buildOpenSPCoopMessage, NotifierInputStreamParams notifierInputStreamParams) {
  312.        
  313.         if(!isActiveDumpRichiesta()) {
  314.             return;
  315.         }
  316.        
  317.         String contentType = null;
  318.         try{
  319.             contentType = req.getContentType();
  320.         }catch(Throwable t){
  321.             this.bfRequest.append("Request.getContentType error: "+t.getMessage()+"\n");
  322.             this.log.error("Request.getContentType error: "+t.getMessage(),t);
  323.         }
  324.        
  325.         Integer contentLength =null;
  326.         try{
  327.             contentLength = req.getContentLength();
  328.         }catch(Throwable t){
  329.             this.bfRequest.append("Request.getContentLength error: "+t.getMessage()+"\n");
  330.             this.log.error("Request.getContentLength error: "+t.getMessage(),t);
  331.         }
  332.        
  333.         Credential credential = null;
  334.         try{
  335.             credential = req.getCredential();
  336.         }catch(Throwable t){
  337.             this.bfRequest.append("Request.getIdentity error: "+t.getMessage()+"\n");
  338.             this.log.error("Request.getIdentity error: "+t.getMessage(),t);
  339.         }
  340.        
  341.         this.urlProtocolContext = null;
  342.         try{
  343.             this.urlProtocolContext = req.getURLProtocolContext();
  344.         }catch(Throwable t){
  345.             this.bfRequest.append("Request.getURLProtocolContext error: "+t.getMessage()+"\n");
  346.             this.log.error("Request.getURLProtocolContext error: "+t.getMessage(),t);
  347.         }
  348.        
  349.         if(this.dump!=null && (this.isRegistrazioneDatabaseRichiesta() || this.onlyLogFileTraceRichiesta())) {
  350.             boolean onlyFileTrace = !isRegistrazioneDatabaseRichiesta();
  351.             this.dump.emitDiagnosticStartDumpBinarioRichiestaIngresso(onlyFileTrace);
  352.         }
  353.        
  354.         try{
  355.             // forzo la scrittura nel buffer dell'oggetto DumpRawConnectorInMessage
  356.             if(buildOpenSPCoopMessage){
  357.                 req.getRequest(notifierInputStreamParams);
  358.             }
  359.             else{
  360.                 req.getRequest();
  361.             }
  362.         }catch(Throwable t){
  363.             this.bfRequest.append("Request.getURLProtocolContext error: "+t.getMessage()+"\n");
  364.             this.log.error("Request.getURLProtocolContext error: "+t.getMessage(),t);
  365.         }
  366.        
  367.         DumpByteArrayOutputStream dumpOUT = req.getDumpByteArrayOutputStream();  
  368.         try {
  369.        
  370.             if(this.dumpBinario) {
  371.                 this.serializeRequest(contentType, contentLength, credential, this.urlProtocolContext,
  372.                         dumpOUT!=null ? dumpOUT.toString() : null,
  373.                         req.getParsingRequestErrorAsString());
  374.             }
  375.            
  376.             if(this.dump!=null && (this.isRegistrazioneDatabaseRichiesta() || this.onlyLogFileTraceRichiesta())) {
  377.                 MessageType messageType = req.getRequestMessageType();
  378.                 //if(rawMessage!=null){ // devono essere registrati anche solamente gli header
  379.                 try {
  380.                     this.dump.dumpBinarioRichiestaIngresso(this.isDumpBinarioRegistrazioneDatabase(), this.onlyLogFileTraceRichiestaIngressoHeaders, this.onlyLogFileTraceRichiestaIngressoPayload,
  381.                             dumpOUT, messageType,
  382.                             this.urlProtocolContext);
  383.                 }catch(Throwable t){
  384.                     this.log.error("Log DumpBinarioRichiestaIngresso error: "+t.getMessage(),t);
  385.                 }
  386.                 //}
  387.             }
  388.            
  389.         }finally {
  390.             try {
  391.                 if(dumpOUT!=null) {
  392.                     dumpOUT.clearResources();
  393.                 }
  394.             }catch(Throwable t){
  395.                 this.log.error("Log DumpBinarioRichiestaIngresso error (clearResources): "+t.getMessage(),t);
  396.             }
  397.         }

  398.     }
  399.    
  400.     public void serializeRequest(String contentType, Integer contentLength, Credential credential, URLProtocolContext urlProtocolContext, String rawMessage,
  401.             String parsingError) {

  402.         if(!isActiveDumpRichiesta() || !this.dumpBinario) {
  403.             return;
  404.         }

  405.         this.bfRequest.append("------ Request ("+this.idTransaction+") ------\n");
  406.        
  407.         try{
  408.             if(contentType!=null){
  409.                 this.bfRequest.append("ContentType: ");
  410.                 this.bfRequest.append(contentType);
  411.                 this.bfRequest.append("\n");
  412.             }
  413.         }catch(Throwable t){
  414.             this.bfRequest.append("Request.getContentType error: "+t.getMessage());
  415.             this.log.error("Request.getContentType error: "+t.getMessage(),t);
  416.         }
  417.        
  418.         try{
  419.             if(contentLength!=null && contentLength>=0){
  420.                 this.bfRequest.append("ContentLength: ");
  421.                 this.bfRequest.append(contentLength);
  422.                 this.bfRequest.append("\n");
  423.             }
  424.         }catch(Throwable t){
  425.             this.bfRequest.append("Request.getContentLength error: "+t.getMessage()+"\n");
  426.             this.log.error("Request.getContentLength error: "+t.getMessage(),t);
  427.         }
  428.        
  429.         try{
  430.             if(credential!=null){
  431.            
  432.                 String principal = credential.getPrincipal();
  433.                 if(principal!=null){
  434.                     this.bfRequest.append("Principal: ");
  435.                     this.bfRequest.append(principal);
  436.                     this.bfRequest.append("\n");
  437.                 }
  438.                
  439.                 String username = credential.getUsername();
  440.                 if(username!=null){
  441.                     this.bfRequest.append("Username: ");
  442.                     this.bfRequest.append(username);
  443.                     this.bfRequest.append("\n");
  444.                 }
  445.                
  446.                 String password = credential.getPassword();
  447.                 if(password!=null){
  448.                     this.bfRequest.append("Password: ");
  449.                     this.bfRequest.append(password);
  450.                     this.bfRequest.append("\n");
  451.                 }
  452.                
  453.                 String subjectX509 = credential.getSubject();
  454.                 if(subjectX509!=null){
  455.                     this.bfRequest.append("X509.Subject: ");
  456.                     this.bfRequest.append(subjectX509);
  457.                     this.bfRequest.append("\n");
  458.                 }
  459.                
  460.                 if(credential.getCertificate()!=null){
  461.                     List<X509Certificate> certificates = new ArrayList<>();
  462.                     if(credential.getCertificate().getCertificate()!=null && credential.getCertificate().getCertificate().getCertificate()!=null) {
  463.                         certificates.add(credential.getCertificate().getCertificate().getCertificate());
  464.                     }
  465.                     if(credential.getCertificate().getCertificateChain()!=null && credential.getCertificate().getCertificateChain().size()>0) {
  466.                         for (CertificateInfo cInfo : credential.getCertificate().getCertificateChain()) {
  467.                             if(cInfo.getCertificate()!=null) {
  468.                                 certificates.add(cInfo.getCertificate());
  469.                             }
  470.                         }
  471.                     }
  472.                     if(!certificates.isEmpty()) {
  473.                         CertificateUtils.printCertificate(this.bfRequest, certificates);
  474.                     }
  475.                 }
  476.             }
  477.            
  478.         }catch(Throwable t){
  479.             this.bfRequest.append("Request.getIdentity error: "+t.getMessage()+"\n");
  480.             this.log.error("Request.getIdentity error: "+t.getMessage(),t);
  481.         }
  482.        
  483.         try{
  484.             if(urlProtocolContext!=null){
  485.            
  486.                 String urlInvocazione = urlProtocolContext.getUrlInvocazione_formBased();
  487.                 if(urlInvocazione!=null){
  488.                     this.bfRequest.append("URLInvocazione: ");
  489.                     this.bfRequest.append(urlInvocazione);
  490.                     this.bfRequest.append("\n");
  491.                 }
  492.                
  493.                 String function = urlProtocolContext.getFunction();
  494.                 if(function!=null){
  495.                     this.bfRequest.append("Function: ");
  496.                     this.bfRequest.append(function);
  497.                     this.bfRequest.append("\n");
  498.                 }
  499.                
  500. //              String webContext = urlProtocolContext.getWebContext();
  501. //              if(webContext!=null){
  502. //                  this.bfRequest.append("WEBContext: ");
  503. //                  this.bfRequest.append(function);
  504. //                  this.bfRequest.append("\n");
  505. //              }
  506.                
  507.                 Map<String, List<String>> transportHeader = urlProtocolContext.getHeaders();
  508.                 if(transportHeader!=null && transportHeader.size()>0){
  509.                     this.bfRequest.append("TransportHeaders: "+transportHeader.size()+"\n");
  510.                     Iterator<String> keys = transportHeader.keySet().iterator();
  511.                     while (keys.hasNext()) {
  512.                         String key = (String) keys.next();
  513.                         List<String> values = transportHeader.get(key);
  514.                         if(values!=null && !values.isEmpty()) {
  515.                             for (String value : values) {
  516.                                 this.bfRequest.append("\t"+key+"="+value+"\n");
  517.                             }
  518.                         }
  519.                     }
  520.                 }
  521.                 else{
  522.                     this.bfRequest.append("TransportHeaders: "+0+"\n");
  523.                 }
  524.                
  525.                 Map<String, List<String>> parameterUrl = urlProtocolContext.getParameters();
  526.                 if(parameterUrl!=null && parameterUrl.size()>0){
  527.                     this.bfRequest.append("URLParameters: "+parameterUrl.size()+"\n");
  528.                     Iterator<String> keys = parameterUrl.keySet().iterator();
  529.                     while (keys.hasNext()) {
  530.                         String key = (String) keys.next();
  531.                         List<String> values = parameterUrl.get(key);
  532.                         if(values!=null && !values.isEmpty()) {
  533.                             for (String value : values) {
  534.                                 this.bfRequest.append("\t"+key+"="+value+"\n");
  535.                             }
  536.                         }
  537.                     }
  538.                 }
  539.                 else{
  540.                     this.bfRequest.append("URLParameters: "+0+"\n");
  541.                 }
  542.                
  543.             }
  544.            
  545.         }catch(Throwable t){
  546.             this.bfRequest.append("Request.getURLProtocolContext error: "+t.getMessage()+"\n");
  547.             this.log.error("Request.getURLProtocolContext error: "+t.getMessage(),t);
  548.         }
  549.                
  550.         if(rawMessage!=null){
  551.             this.bfRequest.append("Binary Request Length: ");
  552.             this.bfRequest.append(rawMessage.length());
  553.             this.bfRequest.append("\n");
  554.            
  555.             this.bfRequest.append("Binary Request: \n");
  556.             this.bfRequest.append(rawMessage);
  557.             this.bfRequest.append("\n");
  558.            
  559.         }
  560.         else if(parsingError!=null){
  561.             this.bfRequest.append("Parsing Request Exception: \n");
  562.             this.bfRequest.append(parsingError);
  563.             this.bfRequest.append("\n");
  564.         }
  565.         else{
  566.             this.bfRequest.append("Binary Request Length: ");
  567.             this.bfRequest.append(0);
  568.             this.bfRequest.append("\n");
  569.            
  570.             this.bfRequest.append("Binary Request: \n");
  571.             this.bfRequest.append("Nessuna contenuto presente nel payload");
  572.             this.bfRequest.append("\n");
  573.         }
  574.        
  575.         this.bfRequest.append("------ End-Request ("+this.idTransaction+") ------\n\n");
  576.        
  577.         this.logDump.info(this.bfRequest.toString());
  578.                
  579.     }
  580.    
  581.     public void emitDiagnosticStartDumpBinarioRispostaUscita() {
  582.         if(this.dump!=null && (this.isRegistrazioneDatabaseRisposta() || this.onlyLogFileTraceRisposta())) {
  583.             boolean onlyFileTrace = !isRegistrazioneDatabaseRisposta();
  584.             this.dump.emitDiagnosticStartDumpBinarioRispostaUscita(onlyFileTrace);
  585.         }
  586.     }
  587.    
  588.     public void serializeResponse(DumpRawConnectorOutMessage res) {
  589.        
  590.         if(!isActiveDumpRisposta()) {
  591.             return;
  592.         }
  593.        
  594.         DumpByteArrayOutputStream dumpOUT = res.getDumpByteArrayOutputStream();  
  595.         try {
  596.        
  597.             if(this.dumpBinario) {
  598.                 this.serializeResponse(dumpOUT!=null ? dumpOUT.toString() : null,
  599.                         res.getParsingResponseErrorAsString(),
  600.                         res.getTrasporto(),res.getContentLenght(),res.getContentType(),res.getStatus());
  601.             }
  602.            
  603.             if(this.dump!=null && (this.isRegistrazioneDatabaseRisposta() || this.onlyLogFileTraceRisposta())) {
  604.                 MessageType messageType = res.getMessageType();
  605.                 //if(rawMessage!=null){ // devono essere registrati anche solamente gli header
  606.                 try {
  607.                     if(res.getContentType()!=null) {
  608.                         TransportUtils.setHeader(res.getTrasporto(),HttpConstants.CONTENT_TYPE,res.getContentType());
  609.                     }
  610.                     this.dump.dumpBinarioRispostaUscita(this.isDumpBinarioRegistrazioneDatabase(), this.onlyLogFileTraceRispostaUscitaHeaders, this.onlyLogFileTraceRispostaUscitaPayload,
  611.                             dumpOUT, messageType,
  612.                             this.urlProtocolContext, res.getTrasporto());
  613.                 }catch(Throwable t){
  614.                     this.log.error("Log DumpBinarioRispostaUscita error: "+t.getMessage(),t);
  615.                 }
  616.                 //}
  617.             }
  618.         }finally {
  619.             try {
  620.                 if(dumpOUT!=null) {
  621.                     dumpOUT.clearResources();
  622.                 }
  623.             }catch(Throwable t){
  624.                 this.log.error("Log DumpBinarioRispostaUscita error (clearResources): "+t.getMessage(),t);
  625.             }
  626.         }
  627.     }
  628.    
  629.     public void serializeResponse(String rawMessage,String parsingError,Map<String, List<String>> transportHeader,Integer contentLength, String contentType, Integer status) {
  630.        
  631.         if(!isActiveDumpRisposta() || !this.dumpBinario) {
  632.             return;
  633.         }
  634.        
  635.         this.bfResponse.append("------ Response ("+this.idTransaction+") ------\n");
  636.        
  637.         try{
  638.             if(contentType!=null){
  639.                 this.bfResponse.append("ContentType: ");
  640.                 this.bfResponse.append(contentType);
  641.                 this.bfResponse.append("\n");
  642.             }
  643.         }catch(Throwable t){
  644.             this.bfResponse.append("Response.contentType error: "+t.getMessage());
  645.             this.log.error("Response.contentType error: "+t.getMessage(),t);
  646.         }
  647.        
  648.         try{
  649.             if(contentLength!=null && contentLength>=0){
  650.                 this.bfResponse.append("ContentLength: ");
  651.                 this.bfResponse.append(contentLength);
  652.                 this.bfResponse.append("\n");
  653.             }
  654.         }catch(Throwable t){
  655.             this.bfResponse.append("Response.contentLength error: "+t.getMessage()+"\n");
  656.             this.log.error("Response.contentLength error: "+t.getMessage(),t);
  657.         }
  658.        
  659.         try{
  660.             if(status!=null){
  661.                 this.bfResponse.append("Status: ");
  662.                 this.bfResponse.append(status);
  663.                 this.bfResponse.append("\n");
  664.             }
  665.         }catch(Throwable t){
  666.             this.bfResponse.append("Response.status error: "+t.getMessage()+"\n");
  667.             this.log.error("Response.status error: "+t.getMessage(),t);
  668.         }
  669.        
  670.         if(transportHeader!=null && transportHeader.size()>0){
  671.             this.bfResponse.append("TransportHeaders: "+transportHeader.size()+"\n");
  672.             Iterator<String> keys = transportHeader.keySet().iterator();
  673.             while (keys.hasNext()) {
  674.                 String key = (String) keys.next();
  675.                 List<String> values = transportHeader.get(key);
  676.                 if(values!=null && !values.isEmpty()) {
  677.                     for (String value : values) {
  678.                         this.bfResponse.append("\t"+key+"="+value+"\n");
  679.                     }
  680.                 }
  681.             }
  682.         }
  683.         else{
  684.             this.bfResponse.append("TransportHeaders: "+0+"\n");
  685.         }
  686.        
  687.         if(rawMessage!=null){
  688.             this.bfResponse.append("Binary Response Length: ");
  689.             this.bfResponse.append(rawMessage.length());
  690.             this.bfResponse.append("\n");
  691.            
  692.             this.bfResponse.append("Binary Response: \n");
  693.             this.bfResponse.append(rawMessage);
  694.             this.bfResponse.append("\n");
  695.            
  696.         }
  697.         else if(parsingError!=null){
  698.             this.bfRequest.append("Parsing Response Exception: \n");
  699.             this.bfRequest.append(parsingError);
  700.             this.bfRequest.append("\n");
  701.         }
  702.         else{
  703.             this.bfResponse.append("Binary Response Length: ");
  704.             this.bfResponse.append(0);
  705.             this.bfResponse.append("\n");
  706.            
  707.             this.bfResponse.append("Binary Response: \n");
  708.             this.bfResponse.append("Nessuna contenuto presente nel payload");
  709.             this.bfResponse.append("\n");
  710.         }
  711.        
  712.         this.bfResponse.append("------ End-Response ("+this.idTransaction+") ------\n\n");
  713.        
  714.         this.logDump.info(this.bfResponse.toString());
  715.     }
  716.    
  717. }