ConnettoreNULL.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */




  20. package org.openspcoop2.pdd.core.connettori;

  21. import java.util.ArrayList;
  22. import java.util.HashMap;
  23. import java.util.Iterator;
  24. import java.util.List;
  25. import java.util.Map;

  26. import org.openspcoop2.core.config.ResponseCachingConfigurazione;
  27. import org.openspcoop2.core.transazioni.constants.TipoMessaggio;
  28. import org.openspcoop2.message.OpenSPCoop2MessageFactory;
  29. import org.openspcoop2.message.OpenSPCoop2SoapMessage;
  30. import org.openspcoop2.message.constants.Costanti;
  31. import org.openspcoop2.message.constants.MessageRole;
  32. import org.openspcoop2.message.constants.MessageType;
  33. import org.openspcoop2.message.soap.TunnelSoapUtils;
  34. import org.openspcoop2.pdd.core.Utilities;
  35. import org.openspcoop2.utils.date.DateManager;
  36. import org.openspcoop2.utils.io.DumpByteArrayOutputStream;
  37. import org.openspcoop2.utils.transport.TransportUtils;
  38. import org.openspcoop2.utils.transport.http.HttpConstants;


  39. /**
  40.  * Classe utilizzata per effettuare consegne di messaggi Soap, attraverso
  41.  * l'invocazione di un server http.
  42.  *
  43.  *
  44.  * @author Poli Andrea (apoli@link.it)
  45.  * @author $Author$
  46.  * @version $Rev$, $Date$
  47.  */

  48. public class ConnettoreNULL extends ConnettoreBase {
  49.    
  50.     @Override
  51.     public String getProtocollo() {
  52.         return "";
  53.     }
  54.    
  55.     public static final String LOCATION = "govway://dev/null";
  56.    
  57.    


  58.     /* ********  METODI  ******** */

  59.     @Override
  60.     protected boolean initializePreSend(ResponseCachingConfigurazione responseCachingConfig, ConnettoreMsg request) {
  61.        
  62.         if(this.initialize(request, false, responseCachingConfig)==false){
  63.             return false;
  64.         }
  65.        
  66.         return true;
  67.        
  68.     }
  69.    
  70.     @Override
  71.     protected boolean send(ConnettoreMsg request) {
  72.        
  73.         this.codice = 200;
  74.        
  75.         try{
  76.            
  77.             // Tipologia di servizio
  78.             MessageType requestMessageType = this.requestMsg.getMessageType();
  79.             OpenSPCoop2SoapMessage soapMessageRequest = null;
  80.             if(this.debug)
  81.                 this.logger.debug("Tipologia Servizio: "+this.requestMsg.getServiceBinding());
  82.             if(this.isSoap){
  83.                 soapMessageRequest = this.requestMsg.castAsSoap();
  84.             }
  85.            
  86.            
  87.             // Collezione header di trasporto per dump
  88.             Map<String, List<String>> propertiesTrasportoDebug = null;
  89.             if(this.isDumpBinarioRichiesta()) {
  90.                 propertiesTrasportoDebug = new HashMap<>();
  91.             }
  92.            
  93.            
  94.             // Impostazione Content-Type
  95.             String contentTypeRichiesta = null;
  96.             if(this.debug)
  97.                 this.logger.debug("Impostazione content type...");
  98.             if(this.isSoap){
  99.                 if(this.sbustamentoSoap && soapMessageRequest.countAttachments()>0 && TunnelSoapUtils.isTunnelOpenSPCoopSoap(soapMessageRequest)){
  100.                     contentTypeRichiesta = TunnelSoapUtils.getContentTypeTunnelOpenSPCoopSoap(soapMessageRequest.getSOAPBody());
  101.                 }else{
  102.                     contentTypeRichiesta = this.requestMsg.getContentType();
  103.                 }
  104.                 if(contentTypeRichiesta==null){
  105.                     throw new Exception("Content-Type del messaggio da spedire non definito");
  106.                 }
  107.             }
  108.             else{
  109.                 contentTypeRichiesta = this.requestMsg.getContentType();
  110.                 // Content-Type non obbligatorio in REST
  111.             }
  112.             if(this.debug)
  113.                 this.logger.info("Impostazione Content-Type ["+contentTypeRichiesta+"]",false);
  114.             if(contentTypeRichiesta!=null){
  115.                 setRequestHeader(HttpConstants.CONTENT_TYPE, contentTypeRichiesta, this.logger, propertiesTrasportoDebug);
  116.             }
  117.            
  118.                        
  119.             // Impostazione Proprieta del trasporto
  120.             if(this.debug)
  121.                 this.logger.debug("Impostazione header di trasporto...");
  122.             this.forwardHttpRequestHeader();
  123.             if(this.propertiesTrasporto != null){
  124.                 Iterator<String> keys = this.propertiesTrasporto.keySet().iterator();
  125.                 while (keys.hasNext()) {
  126.                     String key = (String) keys.next();
  127.                     List<String> values = this.propertiesTrasporto.get(key);
  128.                     if(this.debug) {
  129.                         if(values!=null && !values.isEmpty()) {
  130.                             for (String value : values) {
  131.                                 this.logger.info("Set Transport Header ["+key+"]=["+value+"]",false);
  132.                             }
  133.                         }
  134.                     }
  135.                    
  136.                     setRequestHeader(key, values, this.logger, propertiesTrasportoDebug);
  137.                 }
  138.             }
  139.            
  140.            
  141.             // Aggiunga del SoapAction Header in caso di richiesta SOAP
  142.             // spostato sotto il forwardHeader per consentire alle trasformazioni di modificarla
  143.             if(this.isSoap && this.sbustamentoSoap == false){
  144.                 if(this.debug)
  145.                     this.logger.debug("Impostazione soap action...");
  146.                 boolean existsTransportProperties = false;
  147.                 if(TransportUtils.containsKey(this.propertiesTrasporto, Costanti.SOAP11_MANDATORY_HEADER_HTTP_SOAP_ACTION)){
  148.                     this.soapAction = TransportUtils.getFirstValue(this.propertiesTrasporto, Costanti.SOAP11_MANDATORY_HEADER_HTTP_SOAP_ACTION);
  149.                     existsTransportProperties = (this.soapAction!=null);
  150.                 }
  151.                 if(!existsTransportProperties) {
  152.                     this.soapAction = soapMessageRequest.getSoapAction();
  153.                 }
  154.                 if(this.soapAction==null){
  155.                     this.soapAction="\"OpenSPCoop\"";
  156.                 }
  157.                 if(MessageType.SOAP_11.equals(this.requestMsg.getMessageType()) && !existsTransportProperties){
  158.                     // NOTA non quotare la soap action, per mantenere la trasparenza della PdD
  159.                     setRequestHeader(Costanti.SOAP11_MANDATORY_HEADER_HTTP_SOAP_ACTION,this.soapAction, propertiesTrasportoDebug);
  160.                 }
  161.                 if(this.debug)
  162.                     this.logger.info("SOAP Action inviata ["+this.soapAction+"]",false);
  163.             }
  164.            
  165.            
  166.            
  167.             // SIMULAZIONE WRITE_TO
  168.             boolean consumeRequestMessage = true;
  169.             if(this.debug)
  170.                 this.logger.debug("Serializzazione (consume-request-message:"+consumeRequestMessage+")...");
  171.             if(this.isDumpBinarioRichiesta()) {
  172.                 DumpByteArrayOutputStream bout = null;
  173.                 boolean close = true;
  174.                 try {
  175.                     bout = new DumpByteArrayOutputStream(this.dumpBinario_soglia, this.dumpBinario_repositoryFile, this.idTransazione,
  176.                             TipoMessaggio.RICHIESTA_USCITA_DUMP_BINARIO.getValue());
  177.                    
  178.                     this.emitDiagnosticStartDumpBinarioRichiestaUscita();
  179.                    
  180.                     if(this.isSoap && this.sbustamentoSoap){
  181.                         this.logger.debug("Sbustamento...");
  182.                         TunnelSoapUtils.sbustamentoMessaggio(soapMessageRequest,bout);
  183.                     }else{
  184.                         this.requestMsg.writeTo(bout, consumeRequestMessage);
  185.                     }
  186.                     bout.flush();
  187.                     bout.close();
  188.                     close = false;
  189.                                
  190.                     this.dataRichiestaInoltrata = DateManager.getDate();
  191.                    
  192.                     this.dumpBinarioRichiestaUscita(bout, requestMessageType, contentTypeRichiesta, this.location, propertiesTrasportoDebug);
  193.                 }finally {
  194.                     try {
  195.                         if(close && bout!=null) {
  196.                             bout.close();
  197.                         }
  198.                     }catch(Throwable t) {
  199.                         // ignore
  200.                     }
  201.                     try {
  202.                         if(bout!=null) {
  203.                             bout.clearResources();
  204.                         }
  205.                     }catch(Throwable t) {
  206.                         this.logger.error("Release resources failed: "+t.getMessage(),t);
  207.                     }
  208.                 }
  209.             }
  210.             else {
  211.                 org.apache.commons.io.output.NullOutputStream nullOutputStream = org.apache.commons.io.output.NullOutputStream.INSTANCE;
  212.                 if(this.isSoap && this.sbustamentoSoap){
  213.                     this.logger.debug("Sbustamento...");
  214.                     TunnelSoapUtils.sbustamentoMessaggio(soapMessageRequest,nullOutputStream);
  215.                 }else{
  216.                     this.requestMsg.writeTo(nullOutputStream, consumeRequestMessage);
  217.                 }
  218.                 nullOutputStream.flush();
  219.                 nullOutputStream.close();
  220.                
  221.                 this.dataRichiestaInoltrata = DateManager.getDate();
  222.             }
  223.        
  224.         }catch(Exception e){
  225.             this.eccezioneProcessamento = e;
  226.             this.logger.error("Riscontrato errore durante la writeTo",e);
  227.             this.errore = "Riscontrato errore durante la writeTo: " +this.readExceptionMessageFromException(e);
  228.             return false;
  229.         }
  230.        
  231.         try{
  232.            
  233.             /* ------------  PostOutRequestHandler ------------- */
  234.             this.postOutRequest();
  235.            
  236.         }catch(Exception e){
  237.             this.eccezioneProcessamento = e;
  238.             this.logger.error("Riscontrato errore durante la gestione PostOutRequestHandler",e);
  239.             this.errore = "Riscontrato errore durante la gestione PostOutRequestHandler: " +this.readExceptionMessageFromException(e);
  240.             return false;
  241.         }
  242.            
  243. //      org.openspcoop2.utils.io.notifier.NotifierInputStreamParams notifierInputStreamParams = null;
  244. //      try{
  245. //          
  246. //          /* ------------  PreInResponseHandler ------------- */
  247. //          this.preInResponse();
  248. //          
  249. //          // Lettura risposta parametri NotifierInputStream per la risposta
  250. //          if(this.preInResponseContext!=null){
  251. //              notifierInputStreamParams = this.preInResponseContext.getNotifierInputStreamParams();
  252. //          }
  253. //          
  254. //      }catch(Exception e){
  255. //          this.eccezioneProcessamento = e;
  256. //          ConnettoreNULL.log.error("Riscontrato errore durante la gestione PreInResponseHandler",e);
  257. //          this.errore = "Riscontrato errore durante la gestione PreInResponseHandler: " +e.getMessage();
  258. //          return false;
  259. //      }
  260.        
  261.         try{
  262.                    
  263.             OpenSPCoop2MessageFactory messageFactory = Utilities.getOpenspcoop2MessageFactory(this.logger.getLogger(),this.requestMsg, this.requestInfo, MessageRole.RESPONSE);
  264.             this.responseMsg = messageFactory.createEmptyMessage(this.requestMsg.getMessageType(),MessageRole.RESPONSE);
  265.            
  266.         }catch(Exception e){
  267.             this.eccezioneProcessamento = e;
  268.             this.logger.error("Riscontrato errore durante la generazione di un msg SoapVuoto",e);
  269.             this.errore = "Riscontrato errore durante la generazione di un msg SoapVuoto: " +this.readExceptionMessageFromException(e);
  270.             return false;
  271.         }
  272.        
  273.         return true;
  274.     }

  275.    
  276.     private void setRequestHeader(String key, String value, ConnettoreLogger logger, Map<String, List<String>> propertiesTrasportoDebug) throws Exception {
  277.         List<String> list = new ArrayList<>();
  278.         list.add(value);
  279.         this.setRequestHeader(key, list, logger, propertiesTrasportoDebug);
  280.     }
  281.     private void setRequestHeader(String key, List<String> values, ConnettoreLogger logger, Map<String, List<String>> propertiesTrasportoDebug) throws Exception {
  282.        
  283.         if(this.debug) {
  284.             if(values!=null && !values.isEmpty()) {
  285.                 for (String value : values) {
  286.                     this.logger.info("Set proprietà trasporto ["+key+"]=["+value+"]",false);        
  287.                 }
  288.             }
  289.         }
  290.         setRequestHeader(key, values, propertiesTrasportoDebug);
  291.        
  292.     }
  293.     @Override
  294.     protected void setRequestHeader(String key,List<String> values) throws Exception {
  295.         // nop
  296.     }
  297.    
  298.     /**
  299.      * Ritorna l'informazione su dove il connettore sta spedendo il messaggio
  300.      *
  301.      * @return location di inoltro del messaggio
  302.      * @throws ConnettoreException
  303.      */
  304.     @Override
  305.     public String getLocation() throws ConnettoreException{
  306.         // Il connettore NULL ho possiede possibilita' di consegnare su di una url
  307.         //return ConnettoreUtils.buildLocationWithURLBasedParameter(this.requestMsg, this.propertiesUrlBased, LOCATION);
  308.         return LOCATION;
  309.     }
  310.    
  311. }