UtilitiesIntegrazione.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.integrazione;


  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 java.util.Properties;

  27. import javax.xml.namespace.QName;
  28. import javax.xml.soap.SOAPElement;
  29. import javax.xml.soap.SOAPHeader;
  30. import javax.xml.soap.SOAPHeaderElement;

  31. import org.openspcoop2.core.config.PortaApplicativa;
  32. import org.openspcoop2.core.config.PortaDelegata;
  33. import org.openspcoop2.core.config.Proprieta;
  34. import org.openspcoop2.core.constants.Costanti;
  35. import org.openspcoop2.core.id.IDPortaApplicativa;
  36. import org.openspcoop2.core.id.IDPortaDelegata;
  37. import org.openspcoop2.core.id.IDServizio;
  38. import org.openspcoop2.core.id.IDSoggetto;
  39. import org.openspcoop2.message.OpenSPCoop2MessageFactory;
  40. import org.openspcoop2.message.OpenSPCoop2MessageProperties;
  41. import org.openspcoop2.message.OpenSPCoop2SoapMessage;
  42. import org.openspcoop2.message.constants.MessageType;
  43. import org.openspcoop2.message.constants.ServiceBinding;
  44. import org.openspcoop2.message.exception.MessageNotSupportedException;
  45. import org.openspcoop2.message.soap.SoapUtils;
  46. import org.openspcoop2.message.xml.ValidatoreXSD;
  47. import org.openspcoop2.pdd.config.ConfigurazionePdDManager;
  48. import org.openspcoop2.pdd.config.CostantiProprieta;
  49. import org.openspcoop2.pdd.config.OpenSPCoop2Properties;
  50. import org.openspcoop2.pdd.core.CostantiPdD;
  51. import org.openspcoop2.pdd.core.PdDContext;
  52. import org.openspcoop2.pdd.logger.OpenSPCoop2Logger;
  53. import org.openspcoop2.protocol.sdk.state.RequestInfo;
  54. import org.openspcoop2.utils.MapKey;
  55. import org.openspcoop2.utils.transport.TransportUtils;
  56. import org.openspcoop2.utils.transport.http.HttpConstants;
  57. import org.openspcoop2.utils.xml.XSDResourceResolver;
  58. import org.slf4j.Logger;


  59. /**
  60.  * Classe contenenti utilities per le integrazioni.
  61.  *
  62.  * @author Poli Andrea (apoli@link.it)
  63.  * @author $Author$
  64.  * @version $Rev$, $Date$
  65.  */
  66. public class UtilitiesIntegrazione {


  67.     // ***** STATIC *****

  68.     private static final boolean PORTA_DELEGATA = true;
  69.     private static final boolean PORTA_APPLICATIVA = false;
  70.     private static final boolean REQUEST = true;
  71.     private static final boolean RESPONSE = false;
  72.    
  73.     private static UtilitiesIntegrazione utilitiesIntegrazionePDRequest = null;
  74.     private static UtilitiesIntegrazione utilitiesIntegrazionePDResponse = null;
  75.     private static UtilitiesIntegrazione utilitiesIntegrazionePARequest = null;
  76.     private static UtilitiesIntegrazione utilitiesIntegrazionePAResponse = null;
  77.     public static UtilitiesIntegrazione getInstancePDRequest(Logger log){
  78.         if(UtilitiesIntegrazione.utilitiesIntegrazionePDRequest==null){
  79.             // spotbugs warning 'SING_SINGLETON_GETTER_NOT_SYNCHRONIZED': l'istanza viene creata allo startup
  80.             synchronized (UtilitiesIntegrazione.class) {
  81.                 UtilitiesIntegrazione.initialize(log,PORTA_DELEGATA,REQUEST);
  82.             }
  83.         }
  84.         return UtilitiesIntegrazione.utilitiesIntegrazionePDRequest;
  85.     }
  86.     public static UtilitiesIntegrazione getInstancePDResponse(Logger log){
  87.         if(UtilitiesIntegrazione.utilitiesIntegrazionePDResponse==null){
  88.             // spotbugs warning 'SING_SINGLETON_GETTER_NOT_SYNCHRONIZED': l'istanza viene creata allo startup
  89.             synchronized (UtilitiesIntegrazione.class) {
  90.                 UtilitiesIntegrazione.initialize(log,PORTA_DELEGATA,RESPONSE);
  91.             }
  92.         }
  93.         return UtilitiesIntegrazione.utilitiesIntegrazionePDResponse;
  94.     }
  95.     public static UtilitiesIntegrazione getInstancePARequest(Logger log){
  96.         if(UtilitiesIntegrazione.utilitiesIntegrazionePARequest==null){
  97.             // spotbugs warning 'SING_SINGLETON_GETTER_NOT_SYNCHRONIZED': l'istanza viene creata allo startup
  98.             synchronized (UtilitiesIntegrazione.class) {
  99.                 UtilitiesIntegrazione.initialize(log,PORTA_APPLICATIVA,REQUEST);
  100.             }
  101.         }
  102.         return UtilitiesIntegrazione.utilitiesIntegrazionePARequest;
  103.     }
  104.     public static UtilitiesIntegrazione getInstancePAResponse(Logger log){
  105.         if(UtilitiesIntegrazione.utilitiesIntegrazionePAResponse==null){
  106.             // spotbugs warning 'SING_SINGLETON_GETTER_NOT_SYNCHRONIZED': l'istanza viene creata allo startup
  107.             synchronized (UtilitiesIntegrazione.class) {
  108.                 UtilitiesIntegrazione.initialize(log,PORTA_APPLICATIVA,RESPONSE);
  109.             }
  110.         }
  111.         return UtilitiesIntegrazione.utilitiesIntegrazionePAResponse;
  112.     }

  113.     private static synchronized void initialize(Logger log,boolean portaDelegata, boolean request){
  114.         if(portaDelegata) {
  115.             if(request) {
  116.                 if(UtilitiesIntegrazione.utilitiesIntegrazionePDRequest==null){
  117.                     UtilitiesIntegrazione.utilitiesIntegrazionePDRequest = new UtilitiesIntegrazione(log, portaDelegata, request);
  118.                 }
  119.             }
  120.             else {
  121.                 if(UtilitiesIntegrazione.utilitiesIntegrazionePDResponse==null){
  122.                     UtilitiesIntegrazione.utilitiesIntegrazionePDResponse = new UtilitiesIntegrazione(log, portaDelegata, request);
  123.                 }
  124.             }
  125.         }
  126.         else {
  127.             if(request) {
  128.                 if(UtilitiesIntegrazione.utilitiesIntegrazionePARequest==null){
  129.                     UtilitiesIntegrazione.utilitiesIntegrazionePARequest = new UtilitiesIntegrazione(log, portaDelegata, request);
  130.                 }
  131.             }
  132.             else {
  133.                 if(UtilitiesIntegrazione.utilitiesIntegrazionePAResponse==null){
  134.                     UtilitiesIntegrazione.utilitiesIntegrazionePAResponse = new UtilitiesIntegrazione(log, portaDelegata, request);
  135.                 }
  136.             }
  137.         }
  138.     }

  139.    
  140.    
  141.    
  142.     // ***** INSTANCE *****

  143.     private List<MapKey<String>> keywordsIntegrazione = null;
  144.    
  145.     private Map<MapKey<String>,String> keyValueIntegrazioneTrasporto = null;
  146.     private Map<MapKey<String>, Boolean> keySetEnabled_HeaderIntegrazioneTrasporto = null;
  147.     private Map<MapKey<String>, Boolean> keyReadEnabled_HeaderIntegrazioneTrasporto = null;
  148.    
  149.     private Map<MapKey<String>,String> keyValueIntegrazioneUrlBased = null;
  150.     private Map<MapKey<String>, Boolean> keySetEnabled_HeaderIntegrazioneUrlBased = null;
  151.     private Map<MapKey<String>, Boolean> keyReadEnabled_HeaderIntegrazioneUrlBased = null;
  152.    
  153.     private Map<MapKey<String>,String> keyValueIntegrazioneSoap = null;
  154.     private Map<MapKey<String>, Boolean> keySetEnabled_HeaderIntegrazioneSoap = null;
  155.     private Map<MapKey<String>, Boolean> keyReadEnabled_HeaderIntegrazioneSoap = null;
  156.    
  157.     private OpenSPCoop2Properties openspcoopProperties = null;
  158.     private Map<String, ValidatoreXSD> validatoreXSD_soap11_map = new HashMap<String, ValidatoreXSD>();
  159.     private Map<String, ValidatoreXSD> validatoreXSD_soap12_map = new HashMap<String, ValidatoreXSD>();
  160.    
  161.     private boolean portaDelegata;
  162.    
  163.     private boolean request;
  164.    
  165.     private Logger log;
  166.    
  167.     private UtilitiesIntegrazione(Logger log, boolean portaDelegata, boolean request){
  168.        
  169.         this.log = log;
  170.        
  171.         this.openspcoopProperties = OpenSPCoop2Properties.getInstance();
  172.        
  173.         this.portaDelegata = portaDelegata;
  174.        
  175.         this.request = request;
  176.        
  177.         this.keywordsIntegrazione = this.openspcoopProperties.getKeywordsIntegrazione();
  178.        
  179.         this.keyValueIntegrazioneTrasporto = this.openspcoopProperties.getKeyValue_HeaderIntegrazioneTrasporto();
  180.         try{
  181.             if(portaDelegata) {
  182.                 this.keySetEnabled_HeaderIntegrazioneTrasporto = this.openspcoopProperties.getKeyPDSetEnabled_HeaderIntegrazioneTrasporto(request);
  183.                 this.keyReadEnabled_HeaderIntegrazioneTrasporto = this.openspcoopProperties.getKeyPDReadEnabled_HeaderIntegrazioneTrasporto();
  184.             }
  185.             else {
  186.                 this.keySetEnabled_HeaderIntegrazioneTrasporto = this.openspcoopProperties.getKeyPASetEnabled_HeaderIntegrazioneTrasporto(request);
  187.                 this.keyReadEnabled_HeaderIntegrazioneTrasporto = this.openspcoopProperties.getKeyPAReadEnabled_HeaderIntegrazioneTrasporto();
  188.             }
  189.         }catch(Exception e){
  190.             log.error("Integrazione, errore durante la lettura del file di configurazione: "+e.getMessage(),e);
  191.         }
  192.        
  193.         this.keyValueIntegrazioneUrlBased = this.openspcoopProperties.getKeyValue_HeaderIntegrazioneUrlBased();
  194.         try{
  195.             if(portaDelegata) {
  196.                 this.keySetEnabled_HeaderIntegrazioneUrlBased = this.openspcoopProperties.getKeyPDSetEnabled_HeaderIntegrazioneUrlBased();
  197.                 this.keyReadEnabled_HeaderIntegrazioneUrlBased = this.openspcoopProperties.getKeyPDReadEnabled_HeaderIntegrazioneUrlBased();
  198.             }
  199.             else {
  200.                 this.keySetEnabled_HeaderIntegrazioneUrlBased = this.openspcoopProperties.getKeyPASetEnabled_HeaderIntegrazioneUrlBased();
  201.                 this.keyReadEnabled_HeaderIntegrazioneUrlBased = this.openspcoopProperties.getKeyPAReadEnabled_HeaderIntegrazioneUrlBased();
  202.             }
  203.         }catch(Exception e){
  204.             log.error("Integrazione, errore durante la lettura del file di configurazione: "+e.getMessage(),e);
  205.         }
  206.        
  207.         this.keyValueIntegrazioneSoap = this.openspcoopProperties.getKeyValue_HeaderIntegrazioneSoap();
  208.         try{
  209.             if(portaDelegata) {
  210.                 this.keySetEnabled_HeaderIntegrazioneSoap = this.openspcoopProperties.getKeyPDSetEnabled_HeaderIntegrazioneSoap(request);
  211.                 this.keyReadEnabled_HeaderIntegrazioneSoap = this.openspcoopProperties.getKeyPDReadEnabled_HeaderIntegrazioneSoap();
  212.             }
  213.             else {
  214.                 this.keySetEnabled_HeaderIntegrazioneSoap = this.openspcoopProperties.getKeyPASetEnabled_HeaderIntegrazioneSoap(request);
  215.                 this.keyReadEnabled_HeaderIntegrazioneSoap = this.openspcoopProperties.getKeyPAReadEnabled_HeaderIntegrazioneSoap();
  216.             }
  217.         }catch(Exception e){
  218.             log.error("Integrazione, errore durante la lettura del file di configurazione: "+e.getMessage(),e);
  219.         }
  220.        
  221.     }
  222.    
  223.     private synchronized void initValidatoreXSD(OpenSPCoop2MessageFactory messageFactory) {
  224.         String key = messageFactory.getClass().getName();
  225.            
  226.         if(!this.validatoreXSD_soap11_map.containsKey(key)) {
  227.             try{
  228.                 XSDResourceResolver xsdResourceResolver_soap11 = new XSDResourceResolver();
  229.                 xsdResourceResolver_soap11.addResource("soapEnvelope.xsd", UtilitiesIntegrazione.class.getResourceAsStream("/soapEnvelope.xsd"));
  230.                 this.validatoreXSD_soap11_map.put(key, new ValidatoreXSD(messageFactory, this.log,xsdResourceResolver_soap11,UtilitiesIntegrazione.class.getResourceAsStream("/integrazione_soap11.xsd")));
  231.             }catch(Exception e){
  232.                 this.log.error("Integrazione.xsd, errore durante la costruzione del validatore xsd per Soap11: "+e.getMessage(),e);
  233.             }
  234.         }
  235.        
  236.         if(!this.validatoreXSD_soap12_map.containsKey(key)) {
  237.             try{
  238.                 XSDResourceResolver xsdResourceResolver_soap12 = new XSDResourceResolver();
  239.                 xsdResourceResolver_soap12.addResource("soapEnvelope12.xsd", UtilitiesIntegrazione.class.getResourceAsStream("/soapEnvelope12.xsd"));
  240.                 xsdResourceResolver_soap12.addResource("xml.xsd", UtilitiesIntegrazione.class.getResourceAsStream("/xml.xsd"));
  241.                 this.validatoreXSD_soap12_map.put(key, new ValidatoreXSD(messageFactory, this.log,xsdResourceResolver_soap12,UtilitiesIntegrazione.class.getResourceAsStream("/integrazione_soap12.xsd")));
  242.             }catch(Exception e){
  243.                 this.log.error("Integrazione.xsd, errore durante la costruzione del validatore xsd per Soap12: "+e.getMessage(),e);
  244.             }
  245.         }
  246.        
  247.     }
  248.     private void checkInitValidatoreXSD(OpenSPCoop2MessageFactory messageFactory) {
  249.         String key = messageFactory.getClass().getName();
  250.         if(!this.validatoreXSD_soap11_map.containsKey(key)) {
  251.             initValidatoreXSD(messageFactory);
  252.         }
  253.         if(!this.validatoreXSD_soap12_map.containsKey(key)) {
  254.             initValidatoreXSD(messageFactory);
  255.         }
  256.     }
  257.     private ValidatoreXSD getValidatoreXSD(boolean soap12, OpenSPCoop2MessageFactory messageFactory) {
  258.        
  259.         checkInitValidatoreXSD(messageFactory);
  260.        
  261.         String key = messageFactory.getClass().getName();
  262.         if(soap12) {
  263.             return this.validatoreXSD_soap12_map.get(key);
  264.         }
  265.         else {
  266.             return this.validatoreXSD_soap11_map.get(key);
  267.         }
  268.        
  269.     }
  270.        
  271.     public void readTransportProperties(Map<String, List<String>> prop,
  272.             HeaderIntegrazione integrazione) throws HeaderIntegrazioneException{
  273.         try{
  274.             if(prop!=null && integrazione!=null){
  275.                                
  276.                 // Ricerca tra l'header del trasporto
  277.                 Iterator<String> keys = prop.keySet().iterator();
  278.                 while (keys.hasNext()) {
  279.                     String key = (String) keys.next();
  280.                    
  281.                     if(key!=null){
  282.                        
  283.                         for (MapKey<String> keywordIntegrazione : this.keywordsIntegrazione) {
  284.                             if(key.equalsIgnoreCase((String)this.keyValueIntegrazioneTrasporto.get(keywordIntegrazione))) {
  285.                                
  286.                                 if(this.keyReadEnabled_HeaderIntegrazioneTrasporto.get(keywordIntegrazione)) {
  287.                                
  288.                                     // Busta
  289.                                     if(CostantiPdD.HEADER_INTEGRAZIONE_TIPO_MITTENTE.equals(keywordIntegrazione)) {
  290.                                         integrazione.getBusta().setTipoMittente(TransportUtils.getFirstValue(prop,key));    
  291.                                     }
  292.                                     else if(CostantiPdD.HEADER_INTEGRAZIONE_MITTENTE.equals(keywordIntegrazione)) {
  293.                                         integrazione.getBusta().setMittente(TransportUtils.getFirstValue(prop,key));    
  294.                                     }
  295.                                     else if(CostantiPdD.HEADER_INTEGRAZIONE_TIPO_DESTINATARIO.equals(keywordIntegrazione)) {
  296.                                         integrazione.getBusta().setTipoDestinatario(TransportUtils.getFirstValue(prop,key));    
  297.                                     }
  298.                                     else if(CostantiPdD.HEADER_INTEGRAZIONE_DESTINATARIO.equals(keywordIntegrazione)) {
  299.                                         integrazione.getBusta().setDestinatario(TransportUtils.getFirstValue(prop,key));    
  300.                                     }
  301.                                     else if(CostantiPdD.HEADER_INTEGRAZIONE_TIPO_SERVIZIO.equals(keywordIntegrazione)) {
  302.                                         integrazione.getBusta().setTipoServizio(TransportUtils.getFirstValue(prop,key));    
  303.                                     }
  304.                                     else if(CostantiPdD.HEADER_INTEGRAZIONE_SERVIZIO.equals(keywordIntegrazione)) {
  305.                                         integrazione.getBusta().setServizio(TransportUtils.getFirstValue(prop,key));    
  306.                                     }
  307.                                     else if(CostantiPdD.HEADER_INTEGRAZIONE_VERSIONE_SERVIZIO.equals(keywordIntegrazione)) {
  308.                                         String v = TransportUtils.getFirstValue(prop,key);
  309.                                         try{
  310.                                             if(v!=null) {
  311.                                                 integrazione.getBusta().setVersioneServizio(Integer.parseInt(v));
  312.                                             }
  313.                                         }catch(Exception e){
  314.                                             throw new Exception("Formato versione ["+v+"] non corretto: "+e.getMessage(),e);
  315.                                         }
  316.                                     }
  317.                                     else if(CostantiPdD.HEADER_INTEGRAZIONE_AZIONE.equals(keywordIntegrazione)) {
  318.                                         integrazione.getBusta().setAzione(TransportUtils.getFirstValue(prop,key));  
  319.                                     }
  320.                                     else if(CostantiPdD.HEADER_INTEGRAZIONE_ID_MESSAGGIO.equals(keywordIntegrazione)) {
  321.                                         integrazione.getBusta().setID(TransportUtils.getFirstValue(prop,key));  
  322.                                     }
  323.                                     else if(CostantiPdD.HEADER_INTEGRAZIONE_RIFERIMENTO_MESSAGGIO.equals(keywordIntegrazione)) {
  324.                                         integrazione.getBusta().setRiferimentoMessaggio(TransportUtils.getFirstValue(prop,key));    
  325.                                     }
  326.                                     else if(CostantiPdD.HEADER_INTEGRAZIONE_COLLABORAZIONE.equals(keywordIntegrazione)) {
  327.                                         integrazione.getBusta().setIdCollaborazione(TransportUtils.getFirstValue(prop,key));    
  328.                                     }
  329.                                    
  330.                                     // id e servizio applicativo
  331.                                     else if(CostantiPdD.HEADER_INTEGRAZIONE_ID_APPLICATIVO.equals(keywordIntegrazione)) {
  332.                                         integrazione.setIdApplicativo(TransportUtils.getFirstValue(prop,key));  
  333.                                     }
  334.                                     else if(CostantiPdD.HEADER_INTEGRAZIONE_SERVIZIO_APPLICATIVO.equals(keywordIntegrazione)) {
  335.                                         integrazione.setServizioApplicativo(TransportUtils.getFirstValue(prop,key));    
  336.                                     }
  337.                                     else if(CostantiPdD.HEADER_INTEGRAZIONE_TIPO_MITTENTE_TOKEN.equals(keywordIntegrazione)) {
  338.                                         integrazione.setTipoSoggettoProprietarioApplicativoToken(TransportUtils.getFirstValue(prop,key));  
  339.                                     }
  340.                                     else if(CostantiPdD.HEADER_INTEGRAZIONE_MITTENTE_TOKEN.equals(keywordIntegrazione)) {
  341.                                         integrazione.setNomeSoggettoProprietarioApplicativoToken(TransportUtils.getFirstValue(prop,key));  
  342.                                     }
  343.                                     else if(CostantiPdD.HEADER_INTEGRAZIONE_SERVIZIO_APPLICATIVO_TOKEN.equals(keywordIntegrazione)) {
  344.                                         integrazione.setServizioApplicativoToken(TransportUtils.getFirstValue(prop,key));  
  345.                                     }
  346.                                     else if(CostantiPdD.HEADER_INTEGRAZIONE_ID_TRANSAZIONE.equals(keywordIntegrazione)) {
  347.                                         integrazione.setIdTransazione(TransportUtils.getFirstValue(prop,key));  
  348.                                     }
  349.                                 }
  350.                                 break;
  351.                             }
  352.                         }
  353.                        
  354.                     }
  355.                 }
  356.             }
  357.         }catch(Exception e){
  358.             throw new HeaderIntegrazioneException("UtilitiesIntegrazione, lettura dell'header non riuscita: "+e.getMessage(),e);
  359.         }
  360.     }
  361.    
  362.     public void readUrlProperties(Map<String, List<String>> prop,
  363.             HeaderIntegrazione integrazione) throws HeaderIntegrazioneException{
  364.         try{
  365.             if(prop!=null && integrazione!=null){
  366.            
  367.                 // Ricerca tra le proprieta' dell'url
  368.                 Iterator<String> keys = prop.keySet().iterator();
  369.                 while (keys.hasNext()) {
  370.                     String key = (String) keys.next();

  371.                     if(key!=null){
  372.                        
  373.                         for (MapKey<String> keywordIntegrazione : this.keywordsIntegrazione) {
  374.                             if(key.equalsIgnoreCase((String)this.keyValueIntegrazioneUrlBased.get(keywordIntegrazione))) {
  375.                                
  376.                                 if(this.keyReadEnabled_HeaderIntegrazioneUrlBased.get(keywordIntegrazione)) {
  377.                                
  378.                                     // Busta
  379.                                     if(CostantiPdD.HEADER_INTEGRAZIONE_TIPO_MITTENTE.equals(keywordIntegrazione)) {
  380.                                         integrazione.getBusta().setTipoMittente(TransportUtils.getFirstValue(prop,key));    
  381.                                     }
  382.                                     else if(CostantiPdD.HEADER_INTEGRAZIONE_MITTENTE.equals(keywordIntegrazione)) {
  383.                                         integrazione.getBusta().setMittente(TransportUtils.getFirstValue(prop,key));    
  384.                                     }
  385.                                     else if(CostantiPdD.HEADER_INTEGRAZIONE_TIPO_DESTINATARIO.equals(keywordIntegrazione)) {
  386.                                         integrazione.getBusta().setTipoDestinatario(TransportUtils.getFirstValue(prop,key));    
  387.                                     }
  388.                                     else if(CostantiPdD.HEADER_INTEGRAZIONE_DESTINATARIO.equals(keywordIntegrazione)) {
  389.                                         integrazione.getBusta().setDestinatario(TransportUtils.getFirstValue(prop,key));    
  390.                                     }
  391.                                     else if(CostantiPdD.HEADER_INTEGRAZIONE_TIPO_SERVIZIO.equals(keywordIntegrazione)) {
  392.                                         integrazione.getBusta().setTipoServizio(TransportUtils.getFirstValue(prop,key));    
  393.                                     }
  394.                                     else if(CostantiPdD.HEADER_INTEGRAZIONE_SERVIZIO.equals(keywordIntegrazione)) {
  395.                                         integrazione.getBusta().setServizio(TransportUtils.getFirstValue(prop,key));    
  396.                                     }
  397.                                     else if(CostantiPdD.HEADER_INTEGRAZIONE_VERSIONE_SERVIZIO.equals(keywordIntegrazione)) {
  398.                                         String v = TransportUtils.getFirstValue(prop,key);
  399.                                         try{
  400.                                             if(v!=null) {
  401.                                                 integrazione.getBusta().setVersioneServizio(Integer.parseInt(v));
  402.                                             }
  403.                                         }catch(Exception e){
  404.                                             throw new Exception("Formato versione ["+v+"] non corretto: "+e.getMessage(),e);
  405.                                         }
  406.                                     }
  407.                                     else if(CostantiPdD.HEADER_INTEGRAZIONE_AZIONE.equals(keywordIntegrazione)) {
  408.                                         integrazione.getBusta().setAzione(TransportUtils.getFirstValue(prop,key));  
  409.                                     }
  410.                                     else if(CostantiPdD.HEADER_INTEGRAZIONE_ID_MESSAGGIO.equals(keywordIntegrazione)) {
  411.                                         integrazione.getBusta().setID(TransportUtils.getFirstValue(prop,key));  
  412.                                     }
  413.                                     else if(CostantiPdD.HEADER_INTEGRAZIONE_RIFERIMENTO_MESSAGGIO.equals(keywordIntegrazione)) {
  414.                                         integrazione.getBusta().setRiferimentoMessaggio(TransportUtils.getFirstValue(prop,key));    
  415.                                     }
  416.                                     else if(CostantiPdD.HEADER_INTEGRAZIONE_COLLABORAZIONE.equals(keywordIntegrazione)) {
  417.                                         integrazione.getBusta().setIdCollaborazione(TransportUtils.getFirstValue(prop,key));    
  418.                                     }
  419.                                    
  420.                                     // id e servizio applicativo
  421.                                     else if(CostantiPdD.HEADER_INTEGRAZIONE_ID_APPLICATIVO.equals(keywordIntegrazione)) {
  422.                                         integrazione.setIdApplicativo(TransportUtils.getFirstValue(prop,key));  
  423.                                     }
  424.                                     else if(CostantiPdD.HEADER_INTEGRAZIONE_SERVIZIO_APPLICATIVO.equals(keywordIntegrazione)) {
  425.                                         integrazione.setServizioApplicativo(TransportUtils.getFirstValue(prop,key));    
  426.                                     }
  427.                                     else if(CostantiPdD.HEADER_INTEGRAZIONE_TIPO_MITTENTE_TOKEN.equals(keywordIntegrazione)) {
  428.                                         integrazione.setTipoSoggettoProprietarioApplicativoToken(TransportUtils.getFirstValue(prop,key));  
  429.                                     }
  430.                                     else if(CostantiPdD.HEADER_INTEGRAZIONE_MITTENTE_TOKEN.equals(keywordIntegrazione)) {
  431.                                         integrazione.setNomeSoggettoProprietarioApplicativoToken(TransportUtils.getFirstValue(prop,key));  
  432.                                     }
  433.                                     else if(CostantiPdD.HEADER_INTEGRAZIONE_SERVIZIO_APPLICATIVO_TOKEN.equals(keywordIntegrazione)) {
  434.                                         integrazione.setServizioApplicativoToken(TransportUtils.getFirstValue(prop,key));  
  435.                                     }
  436.                                     else if(CostantiPdD.HEADER_INTEGRAZIONE_ID_TRANSAZIONE.equals(keywordIntegrazione)) {
  437.                                         integrazione.setIdTransazione(TransportUtils.getFirstValue(prop,key));  
  438.                                     }
  439.                                 }
  440.                                 break;
  441.                             }
  442.                         }
  443.                        
  444.                     }
  445.                 }
  446.             }
  447.         }catch(Exception e){
  448.             throw new HeaderIntegrazioneException("UtilitiesIntegrazione, lettura dell'header non riuscita: "+e.getMessage(),e);
  449.         }
  450.     }
  451.    


  452.     public void setUrlProperties(HeaderIntegrazione integrazione,
  453.             Map<String, List<String>> properties,
  454.             Map<String, String> protocolInfos) throws HeaderIntegrazioneException{

  455.         try{
  456.             if(properties!=null && integrazione!=null){
  457.                 if(integrazione.getBusta()!=null){              
  458.                     if(integrazione.getBusta().getTipoMittente()!=null) {
  459.                         if(this.keySetEnabled_HeaderIntegrazioneUrlBased.get(CostantiPdD.HEADER_INTEGRAZIONE_TIPO_MITTENTE)) {
  460.                             TransportUtils.setParameter(properties,this.keyValueIntegrazioneUrlBased.get(CostantiPdD.HEADER_INTEGRAZIONE_TIPO_MITTENTE), integrazione.getBusta().getTipoMittente());
  461.                         }
  462.                     }
  463.                     if(integrazione.getBusta().getMittente()!=null) {
  464.                         if(this.keySetEnabled_HeaderIntegrazioneUrlBased.get(CostantiPdD.HEADER_INTEGRAZIONE_MITTENTE)) {
  465.                             TransportUtils.setParameter(properties,this.keyValueIntegrazioneUrlBased.get(CostantiPdD.HEADER_INTEGRAZIONE_MITTENTE), integrazione.getBusta().getMittente());
  466.                         }
  467.                     }
  468.                     if(integrazione.getBusta().getTipoDestinatario()!=null) {
  469.                         if(this.keySetEnabled_HeaderIntegrazioneUrlBased.get(CostantiPdD.HEADER_INTEGRAZIONE_TIPO_DESTINATARIO)) {
  470.                             TransportUtils.setParameter(properties,this.keyValueIntegrazioneUrlBased.get(CostantiPdD.HEADER_INTEGRAZIONE_TIPO_DESTINATARIO), integrazione.getBusta().getTipoDestinatario());
  471.                         }
  472.                     }
  473.                     if(integrazione.getBusta().getDestinatario()!=null) {
  474.                         if(this.keySetEnabled_HeaderIntegrazioneUrlBased.get(CostantiPdD.HEADER_INTEGRAZIONE_DESTINATARIO)) {
  475.                             TransportUtils.setParameter(properties,this.keyValueIntegrazioneUrlBased.get(CostantiPdD.HEADER_INTEGRAZIONE_DESTINATARIO), integrazione.getBusta().getDestinatario());
  476.                         }
  477.                     }
  478.                     if(integrazione.getBusta().getTipoServizio()!=null) {
  479.                         if(this.keySetEnabled_HeaderIntegrazioneUrlBased.get(CostantiPdD.HEADER_INTEGRAZIONE_TIPO_SERVIZIO)) {
  480.                             TransportUtils.setParameter(properties,this.keyValueIntegrazioneUrlBased.get(CostantiPdD.HEADER_INTEGRAZIONE_TIPO_SERVIZIO), integrazione.getBusta().getTipoServizio());
  481.                         }
  482.                     }
  483.                     if(integrazione.getBusta().getServizio()!=null) {
  484.                         if(this.keySetEnabled_HeaderIntegrazioneUrlBased.get(CostantiPdD.HEADER_INTEGRAZIONE_SERVIZIO)) {
  485.                             TransportUtils.setParameter(properties,this.keyValueIntegrazioneUrlBased.get(CostantiPdD.HEADER_INTEGRAZIONE_SERVIZIO), integrazione.getBusta().getServizio());
  486.                         }
  487.                     }
  488.                     if(integrazione.getBusta().getVersioneServizio()!=null) {
  489.                         if(this.keySetEnabled_HeaderIntegrazioneUrlBased.get(CostantiPdD.HEADER_INTEGRAZIONE_VERSIONE_SERVIZIO)) {
  490.                             TransportUtils.setParameter(properties,this.keyValueIntegrazioneUrlBased.get(CostantiPdD.HEADER_INTEGRAZIONE_VERSIONE_SERVIZIO), integrazione.getBusta().getVersioneServizio().intValue()+"");
  491.                         }
  492.                     }
  493.                     if(integrazione.getBusta().getAzione()!=null) {
  494.                         if(this.keySetEnabled_HeaderIntegrazioneUrlBased.get(CostantiPdD.HEADER_INTEGRAZIONE_AZIONE)) {
  495.                             TransportUtils.setParameter(properties,this.keyValueIntegrazioneUrlBased.get(CostantiPdD.HEADER_INTEGRAZIONE_AZIONE), integrazione.getBusta().getAzione());
  496.                         }
  497.                     }
  498.                     if(integrazione.getBusta().getID()!=null) {
  499.                         if(this.keySetEnabled_HeaderIntegrazioneUrlBased.get(CostantiPdD.HEADER_INTEGRAZIONE_ID_MESSAGGIO)) {
  500.                             TransportUtils.setParameter(properties,this.keyValueIntegrazioneUrlBased.get(CostantiPdD.HEADER_INTEGRAZIONE_ID_MESSAGGIO), integrazione.getBusta().getID());
  501.                         }
  502.                     }
  503.                     if(integrazione.getBusta().getRiferimentoMessaggio()!=null) {
  504.                         if(this.keySetEnabled_HeaderIntegrazioneUrlBased.get(CostantiPdD.HEADER_INTEGRAZIONE_RIFERIMENTO_MESSAGGIO)) {
  505.                             TransportUtils.setParameter(properties,this.keyValueIntegrazioneUrlBased.get(CostantiPdD.HEADER_INTEGRAZIONE_RIFERIMENTO_MESSAGGIO), integrazione.getBusta().getRiferimentoMessaggio());
  506.                         }
  507.                     }
  508.                     if(integrazione.getBusta().getIdCollaborazione()!=null) {
  509.                         if(this.keySetEnabled_HeaderIntegrazioneUrlBased.get(CostantiPdD.HEADER_INTEGRAZIONE_COLLABORAZIONE)) {
  510.                             TransportUtils.setParameter(properties,this.keyValueIntegrazioneUrlBased.get(CostantiPdD.HEADER_INTEGRAZIONE_COLLABORAZIONE), integrazione.getBusta().getIdCollaborazione());
  511.                         }
  512.                     }
  513.                 }
  514.                 if(integrazione.getIdApplicativo()!=null) {
  515.                     if(this.keySetEnabled_HeaderIntegrazioneUrlBased.get(CostantiPdD.HEADER_INTEGRAZIONE_ID_APPLICATIVO)) {
  516.                         TransportUtils.setParameter(properties,this.keyValueIntegrazioneUrlBased.get(CostantiPdD.HEADER_INTEGRAZIONE_ID_APPLICATIVO), integrazione.getIdApplicativo());
  517.                     }
  518.                 }
  519.                 if(integrazione.getServizioApplicativo()!=null) {
  520.                     if(this.keySetEnabled_HeaderIntegrazioneUrlBased.get(CostantiPdD.HEADER_INTEGRAZIONE_SERVIZIO_APPLICATIVO)) {
  521.                         TransportUtils.setParameter(properties,this.keyValueIntegrazioneUrlBased.get(CostantiPdD.HEADER_INTEGRAZIONE_SERVIZIO_APPLICATIVO), integrazione.getServizioApplicativo());
  522.                     }
  523.                 }
  524.                 if(integrazione.getTipoSoggettoProprietarioApplicativoToken()!=null) {
  525.                     if(this.keySetEnabled_HeaderIntegrazioneUrlBased.get(CostantiPdD.HEADER_INTEGRAZIONE_TIPO_MITTENTE_TOKEN)) {
  526.                         TransportUtils.setParameter(properties,this.keyValueIntegrazioneUrlBased.get(CostantiPdD.HEADER_INTEGRAZIONE_TIPO_MITTENTE_TOKEN), integrazione.getTipoSoggettoProprietarioApplicativoToken());
  527.                     }
  528.                 }
  529.                 if(integrazione.getNomeSoggettoProprietarioApplicativoToken()!=null) {
  530.                     if(this.keySetEnabled_HeaderIntegrazioneUrlBased.get(CostantiPdD.HEADER_INTEGRAZIONE_MITTENTE_TOKEN)) {
  531.                         TransportUtils.setParameter(properties,this.keyValueIntegrazioneUrlBased.get(CostantiPdD.HEADER_INTEGRAZIONE_MITTENTE_TOKEN), integrazione.getNomeSoggettoProprietarioApplicativoToken());
  532.                     }
  533.                 }
  534.                 if(integrazione.getServizioApplicativoToken()!=null) {
  535.                     if(this.keySetEnabled_HeaderIntegrazioneUrlBased.get(CostantiPdD.HEADER_INTEGRAZIONE_SERVIZIO_APPLICATIVO_TOKEN)) {
  536.                         TransportUtils.setParameter(properties,this.keyValueIntegrazioneUrlBased.get(CostantiPdD.HEADER_INTEGRAZIONE_SERVIZIO_APPLICATIVO_TOKEN), integrazione.getServizioApplicativoToken());
  537.                     }
  538.                 }
  539.                 if(integrazione.getIdTransazione()!=null) {
  540.                     if(this.keySetEnabled_HeaderIntegrazioneUrlBased.get(CostantiPdD.HEADER_INTEGRAZIONE_ID_TRANSAZIONE)) {
  541.                         TransportUtils.setParameter(properties,this.keyValueIntegrazioneUrlBased.get(CostantiPdD.HEADER_INTEGRAZIONE_ID_TRANSAZIONE), integrazione.getIdTransazione());
  542.                     }
  543.                 }
  544.             }
  545.             if(properties!=null){
  546. //              properties.put(CostantiPdD.URL_BASED_PDD,URLEncoder.encode(this.openspcoopProperties.getHttpServer(),"UTF-8"));
  547. //              if(this.openspcoopProperties.getHttpXPdDDetails()!=null && !"".equals(this.openspcoopProperties.getHttpXPdDDetails())){
  548. //                  properties.put(CostantiPdD.URL_BASED_PDD_DETAILS,URLEncoder.encode(this.openspcoopProperties.getHttpXPdDDetails(),"UTF-8"));
  549. //              }
  550.                 // Non deve essere effettuato a questo livello l'URLEncoder altrimenti si ottiene una doppia codifica, essendo poi fatta anche per tutti i valori in ConnettoreUtils.
  551.                
  552.                 if(this.keySetEnabled_HeaderIntegrazioneUrlBased.get(CostantiPdD.HEADER_INTEGRAZIONE_INFO)) {
  553.                     TransportUtils.setParameter(properties,CostantiPdD.URL_BASED_PDD,this.openspcoopProperties.getHttpServer());
  554.                     if(this.openspcoopProperties.getHttpXPdDDetails()!=null && !"".equals(this.openspcoopProperties.getHttpXPdDDetails())){
  555.                         TransportUtils.setParameter(properties,CostantiPdD.URL_BASED_PDD_DETAILS,this.openspcoopProperties.getHttpXPdDDetails());
  556.                     }
  557.                 }

  558.                 if(this.keySetEnabled_HeaderIntegrazioneUrlBased.get(CostantiPdD.HEADER_INTEGRAZIONE_PROTOCOL_INFO)) {
  559.                     // protocol info
  560.                     if(protocolInfos!=null && protocolInfos.size()>0){
  561.                        
  562.                         String prefixProtocolInfo = this.keyValueIntegrazioneUrlBased.get(CostantiPdD.HEADER_INTEGRAZIONE_PROTOCOL_INFO);
  563.                        
  564.                         Iterator<String> itProtocolInfos = protocolInfos.keySet().iterator();
  565.                         while (itProtocolInfos.hasNext()) {
  566.                             String name = (String) itProtocolInfos.next();
  567.                             String value = protocolInfos.get(name);
  568.                             String nameWithPrefix = (prefixProtocolInfo!=null) ? prefixProtocolInfo.trim()+name : name;
  569.                             TransportUtils.setParameter(properties,nameWithPrefix,value);
  570.                         }
  571.                     }
  572.                 }
  573.             }
  574.            
  575.         }catch(Exception e){
  576.             throw new HeaderIntegrazioneException("UtilitiesIntegrazione, creazione delle proprieta' dell'header non riuscita: "+e.getMessage(),e);
  577.         }
  578.     }
  579.    
  580.     public void setSecurityHeaders(ServiceBinding serviceBinding, RequestInfo requestInfo, Map<String, List<String>> properties, OpenSPCoop2MessageProperties forwardHeader) throws HeaderIntegrazioneException{
  581.        
  582.         try {
  583.             List<Proprieta> proprieta = null;
  584.             if(requestInfo!=null && requestInfo.getProtocolContext()!=null && requestInfo.getProtocolContext().getInterfaceName()!=null) {
  585.                 if(this.portaDelegata) {
  586.                     IDPortaDelegata idPD = new IDPortaDelegata();
  587.                     idPD.setNome(requestInfo.getProtocolContext().getInterfaceName());
  588.                     PortaDelegata pd = ConfigurazionePdDManager.getInstance(null).getPortaDelegataSafeMethod(idPD, requestInfo);
  589.                     if(pd!=null) {
  590.                         proprieta = pd.getProprietaList();
  591.                     }
  592.                 }
  593.                 else {
  594.                     IDPortaApplicativa idPA = new IDPortaApplicativa();
  595.                     idPA.setNome(requestInfo.getProtocolContext().getInterfaceName());
  596.                     PortaApplicativa pa = ConfigurazionePdDManager.getInstance(null).getPortaApplicativaSafeMethod(idPA, requestInfo);
  597.                     if(pa!=null) {
  598.                         proprieta = pa.getProprietaList();
  599.                     }
  600.                 }
  601.             }
  602.            
  603.             boolean enabled = false;
  604.             if(this.portaDelegata) {
  605.                 if(ServiceBinding.REST.equals(serviceBinding)) {
  606.                     enabled = this.openspcoopProperties.isRESTServices_inoltroBuste_response_securityHeaders();
  607.                 }
  608.                 else {
  609.                     enabled = this.openspcoopProperties.isSOAPServices_inoltroBuste_response_securityHeaders();
  610.                 }
  611.             }
  612.             else {
  613.                 if(ServiceBinding.REST.equals(serviceBinding)) {
  614.                     enabled = this.openspcoopProperties.isRESTServices_consegnaContenutiApplicativi_response_securityHeaders();
  615.                 }
  616.                 else {
  617.                     enabled = this.openspcoopProperties.isSOAPServices_consegnaContenutiApplicativi_response_securityHeaders();
  618.                 }
  619.             }
  620.             enabled = CostantiProprieta.isSecurityHeadersEnabled(proprieta, enabled);
  621.            
  622.             if(enabled) {
  623.                
  624.                 Properties pDefault = null;
  625.                 if(this.portaDelegata) {
  626.                     if(ServiceBinding.REST.equals(serviceBinding)) {
  627.                         pDefault = this.openspcoopProperties.getRESTServices_inoltroBuste_response_securityHeaders();
  628.                     }
  629.                     else {
  630.                         pDefault = this.openspcoopProperties.getSOAPServices_inoltroBuste_response_securityHeaders();
  631.                     }
  632.                 }
  633.                 else {
  634.                     if(ServiceBinding.REST.equals(serviceBinding)) {
  635.                         pDefault = this.openspcoopProperties.getRESTServices_consegnaContenutiApplicativi_response_securityHeaders();
  636.                     }
  637.                     else {
  638.                         pDefault = this.openspcoopProperties.getSOAPServices_consegnaContenutiApplicativi_response_securityHeaders();
  639.                     }
  640.                 }
  641.                
  642.                 Map<String, String> securityHeaders = CostantiProprieta.getSecurityHeaders(proprieta, pDefault);
  643.                 if(securityHeaders!=null && !securityHeaders.isEmpty()) {
  644.                    
  645.                     boolean headerCachePresenteRispostaBackend = false;
  646.                     // Gli header per la cache vengono gestiti in blocco
  647.                     for (String hdrName : securityHeaders.keySet()) {
  648.                         if(HttpConstants.isCacheStatusHeader(hdrName)) {
  649.                             if( (TransportUtils.containsKey(properties, hdrName))
  650.                                     ||
  651.                                     (forwardHeader!=null && forwardHeader.containsKey(hdrName))
  652.                                 ) {
  653.                                 headerCachePresenteRispostaBackend = true;  
  654.                                 break;
  655.                             }
  656.                         }
  657.                     }
  658.                    
  659.                     for (String hdrName : securityHeaders.keySet()) {
  660.                        
  661.                         if(HttpConstants.isCacheStatusHeader(hdrName) && headerCachePresenteRispostaBackend) {
  662.                             continue;
  663.                         }
  664.                        
  665.                         if( (!TransportUtils.containsKey(properties, hdrName))
  666.                                 &&
  667.                                 (forwardHeader==null || !forwardHeader.containsKey(hdrName))
  668.                             ) { // altrimenti è stato definito dal backend e lascio quello
  669.                             String hdrValue = securityHeaders.get(hdrName);
  670.                             if(hdrValue!=null) {
  671.                                 TransportUtils.setHeader(properties, hdrName, hdrValue);
  672.                             }
  673.                         }
  674.                     }
  675.                 }
  676.                
  677.             }
  678.            
  679.         }catch(Exception e) {
  680.             throw new HeaderIntegrazioneException("UtilitiesIntegrazione, creazione securityHeaders non riuscita: "+e.getMessage(),e);
  681.         }
  682.        
  683.     }
  684.    
  685.     public void setInfoProductTransportProperties(Map<String, List<String>> properties) throws HeaderIntegrazioneException{
  686.         setTransportProperties(null, properties, null);
  687.     }
  688.     public void setTransportProperties(HeaderIntegrazione integrazione,
  689.             Map<String, List<String>> properties,
  690.             Map<String, String> protocolInfos) throws HeaderIntegrazioneException{

  691.         try{
  692.             if(properties!=null && integrazione!=null){
  693.                 if(integrazione.getBusta()!=null){
  694.                     if(integrazione.getBusta().getTipoMittente()!=null) {
  695.                         if(this.keySetEnabled_HeaderIntegrazioneTrasporto.get(CostantiPdD.HEADER_INTEGRAZIONE_TIPO_MITTENTE)) {
  696.                             TransportUtils.setHeader(properties,this.keyValueIntegrazioneTrasporto.get(CostantiPdD.HEADER_INTEGRAZIONE_TIPO_MITTENTE), integrazione.getBusta().getTipoMittente());
  697.                         }
  698.                     }
  699.                     if(integrazione.getBusta().getMittente()!=null) {
  700.                         if(this.keySetEnabled_HeaderIntegrazioneTrasporto.get(CostantiPdD.HEADER_INTEGRAZIONE_MITTENTE)) {
  701.                             TransportUtils.setHeader(properties,this.keyValueIntegrazioneTrasporto.get(CostantiPdD.HEADER_INTEGRAZIONE_MITTENTE), integrazione.getBusta().getMittente());
  702.                         }
  703.                     }
  704.                     if(integrazione.getBusta().getTipoDestinatario()!=null) {
  705.                         if(this.keySetEnabled_HeaderIntegrazioneTrasporto.get(CostantiPdD.HEADER_INTEGRAZIONE_TIPO_DESTINATARIO)) {
  706.                             TransportUtils.setHeader(properties,this.keyValueIntegrazioneTrasporto.get(CostantiPdD.HEADER_INTEGRAZIONE_TIPO_DESTINATARIO), integrazione.getBusta().getTipoDestinatario());
  707.                         }
  708.                     }
  709.                     if(integrazione.getBusta().getDestinatario()!=null) {
  710.                         if(this.keySetEnabled_HeaderIntegrazioneTrasporto.get(CostantiPdD.HEADER_INTEGRAZIONE_DESTINATARIO)) {
  711.                             TransportUtils.setHeader(properties,this.keyValueIntegrazioneTrasporto.get(CostantiPdD.HEADER_INTEGRAZIONE_DESTINATARIO), integrazione.getBusta().getDestinatario());
  712.                         }
  713.                     }
  714.                     if(integrazione.getBusta().getTipoServizio()!=null) {
  715.                         if(this.keySetEnabled_HeaderIntegrazioneTrasporto.get(CostantiPdD.HEADER_INTEGRAZIONE_TIPO_SERVIZIO)) {
  716.                             TransportUtils.setHeader(properties,this.keyValueIntegrazioneTrasporto.get(CostantiPdD.HEADER_INTEGRAZIONE_TIPO_SERVIZIO), integrazione.getBusta().getTipoServizio());
  717.                         }
  718.                     }
  719.                     if(integrazione.getBusta().getServizio()!=null) {
  720.                         if(this.keySetEnabled_HeaderIntegrazioneTrasporto.get(CostantiPdD.HEADER_INTEGRAZIONE_SERVIZIO)) {
  721.                             TransportUtils.setHeader(properties,this.keyValueIntegrazioneTrasporto.get(CostantiPdD.HEADER_INTEGRAZIONE_SERVIZIO), integrazione.getBusta().getServizio());
  722.                         }
  723.                     }
  724.                     if(integrazione.getBusta().getVersioneServizio()!=null) {
  725.                         if(this.keySetEnabled_HeaderIntegrazioneTrasporto.get(CostantiPdD.HEADER_INTEGRAZIONE_VERSIONE_SERVIZIO)) {
  726.                             TransportUtils.setHeader(properties,this.keyValueIntegrazioneTrasporto.get(CostantiPdD.HEADER_INTEGRAZIONE_VERSIONE_SERVIZIO), integrazione.getBusta().getVersioneServizio().intValue()+"");
  727.                         }
  728.                     }
  729.                     if(integrazione.getBusta().getAzione()!=null) {
  730.                         if(this.keySetEnabled_HeaderIntegrazioneTrasporto.get(CostantiPdD.HEADER_INTEGRAZIONE_AZIONE)) {
  731.                             TransportUtils.setHeader(properties,this.keyValueIntegrazioneTrasporto.get(CostantiPdD.HEADER_INTEGRAZIONE_AZIONE), integrazione.getBusta().getAzione());
  732.                         }
  733.                     }
  734.                     if(integrazione.getBusta().getID()!=null) {
  735.                         if(this.keySetEnabled_HeaderIntegrazioneTrasporto.get(CostantiPdD.HEADER_INTEGRAZIONE_ID_MESSAGGIO)) {
  736.                             TransportUtils.setHeader(properties,this.keyValueIntegrazioneTrasporto.get(CostantiPdD.HEADER_INTEGRAZIONE_ID_MESSAGGIO), integrazione.getBusta().getID());
  737.                         }
  738.                     }
  739.                     if(integrazione.getBusta().getRiferimentoMessaggio()!=null) {
  740.                         if(this.keySetEnabled_HeaderIntegrazioneTrasporto.get(CostantiPdD.HEADER_INTEGRAZIONE_RIFERIMENTO_MESSAGGIO)) {
  741.                             TransportUtils.setHeader(properties,this.keyValueIntegrazioneTrasporto.get(CostantiPdD.HEADER_INTEGRAZIONE_RIFERIMENTO_MESSAGGIO), integrazione.getBusta().getRiferimentoMessaggio());
  742.                         }
  743.                     }
  744.                     if(integrazione.getBusta().getIdCollaborazione()!=null) {
  745.                         if(this.keySetEnabled_HeaderIntegrazioneTrasporto.get(CostantiPdD.HEADER_INTEGRAZIONE_COLLABORAZIONE)) {
  746.                             TransportUtils.setHeader(properties,this.keyValueIntegrazioneTrasporto.get(CostantiPdD.HEADER_INTEGRAZIONE_COLLABORAZIONE), integrazione.getBusta().getIdCollaborazione());
  747.                         }
  748.                     }
  749.                 }
  750.                 if(integrazione.getIdApplicativo()!=null) {
  751.                     if(this.keySetEnabled_HeaderIntegrazioneTrasporto.get(CostantiPdD.HEADER_INTEGRAZIONE_ID_APPLICATIVO)) {
  752.                         TransportUtils.setHeader(properties,this.keyValueIntegrazioneTrasporto.get(CostantiPdD.HEADER_INTEGRAZIONE_ID_APPLICATIVO), integrazione.getIdApplicativo());
  753.                     }
  754.                 }
  755.                 if(integrazione.getServizioApplicativo()!=null) {
  756.                     if(this.keySetEnabled_HeaderIntegrazioneTrasporto.get(CostantiPdD.HEADER_INTEGRAZIONE_SERVIZIO_APPLICATIVO)) {
  757.                         TransportUtils.setHeader(properties,this.keyValueIntegrazioneTrasporto.get(CostantiPdD.HEADER_INTEGRAZIONE_SERVIZIO_APPLICATIVO), integrazione.getServizioApplicativo());
  758.                     }
  759.                 }
  760.                 if(integrazione.getTipoSoggettoProprietarioApplicativoToken()!=null) {
  761.                     if(this.keySetEnabled_HeaderIntegrazioneTrasporto.get(CostantiPdD.HEADER_INTEGRAZIONE_TIPO_MITTENTE_TOKEN)) {
  762.                         TransportUtils.setParameter(properties,this.keyValueIntegrazioneTrasporto.get(CostantiPdD.HEADER_INTEGRAZIONE_TIPO_MITTENTE_TOKEN), integrazione.getTipoSoggettoProprietarioApplicativoToken());
  763.                     }
  764.                 }
  765.                 if(integrazione.getNomeSoggettoProprietarioApplicativoToken()!=null) {
  766.                     if(this.keySetEnabled_HeaderIntegrazioneTrasporto.get(CostantiPdD.HEADER_INTEGRAZIONE_MITTENTE_TOKEN)) {
  767.                         TransportUtils.setParameter(properties,this.keyValueIntegrazioneTrasporto.get(CostantiPdD.HEADER_INTEGRAZIONE_MITTENTE_TOKEN), integrazione.getNomeSoggettoProprietarioApplicativoToken());
  768.                     }
  769.                 }
  770.                 if(integrazione.getServizioApplicativoToken()!=null) {
  771.                     if(this.keySetEnabled_HeaderIntegrazioneTrasporto.get(CostantiPdD.HEADER_INTEGRAZIONE_SERVIZIO_APPLICATIVO_TOKEN)) {
  772.                         TransportUtils.setParameter(properties,this.keyValueIntegrazioneTrasporto.get(CostantiPdD.HEADER_INTEGRAZIONE_SERVIZIO_APPLICATIVO_TOKEN), integrazione.getServizioApplicativoToken());
  773.                     }
  774.                 }
  775.                 if(integrazione.getIdTransazione()!=null) {
  776.                     if(this.keySetEnabled_HeaderIntegrazioneTrasporto.get(CostantiPdD.HEADER_INTEGRAZIONE_ID_TRANSAZIONE)) {
  777.                         TransportUtils.setHeader(properties,this.keyValueIntegrazioneTrasporto.get(CostantiPdD.HEADER_INTEGRAZIONE_ID_TRANSAZIONE), integrazione.getIdTransazione());
  778.                     }
  779.                 }
  780.             }
  781.             if(properties!=null){
  782.                
  783.                 boolean infoProduct = this.keySetEnabled_HeaderIntegrazioneTrasporto.get(CostantiPdD.HEADER_INTEGRAZIONE_INFO);
  784.                 if(infoProduct) {
  785.                     if(properties.containsKey(CostantiPdD.HEADER_HTTP_X_PDD)==false) {
  786.                         TransportUtils.setHeader(properties,CostantiPdD.HEADER_HTTP_X_PDD,this.openspcoopProperties.getHttpServer());
  787.                     }
  788.                     if(this.openspcoopProperties.getHttpXPdDDetails()!=null && !"".equals(this.openspcoopProperties.getHttpXPdDDetails())){
  789.                         if(properties.containsKey(CostantiPdD.HEADER_HTTP_X_PDD_DETAILS)==false) {
  790.                             TransportUtils.setHeader(properties,CostantiPdD.HEADER_HTTP_X_PDD_DETAILS,this.openspcoopProperties.getHttpXPdDDetails());
  791.                         }
  792.                     }
  793.                 }
  794.                
  795.                 boolean userAgent = this.keySetEnabled_HeaderIntegrazioneTrasporto.get(CostantiPdD.HEADER_INTEGRAZIONE_USER_AGENT);
  796.                 if(userAgent) {
  797.                     if(this.request) {
  798.                         if(properties.containsKey(HttpConstants.USER_AGENT)==false) {
  799.                             TransportUtils.setHeader(properties,HttpConstants.USER_AGENT,this.openspcoopProperties.getHttpUserAgent());
  800.                         }
  801.                     }
  802.                     else {
  803.                         if(properties.containsKey(HttpConstants.SERVER)==false) {
  804.                             TransportUtils.setHeader(properties,HttpConstants.SERVER,this.openspcoopProperties.getHttpUserAgent());
  805.                         }
  806.                     }
  807.                 }
  808.                
  809.                 if(this.keySetEnabled_HeaderIntegrazioneTrasporto.get(CostantiPdD.HEADER_INTEGRAZIONE_PROTOCOL_INFO)) {
  810.                     if(protocolInfos!=null && protocolInfos.size()>0){
  811.                        
  812.                         String prefixProtocolInfo = this.keyValueIntegrazioneTrasporto.get(CostantiPdD.HEADER_INTEGRAZIONE_PROTOCOL_INFO);
  813.                        
  814.                         Iterator<String> itProtocolInfos = protocolInfos.keySet().iterator();
  815.                         while (itProtocolInfos.hasNext()) {
  816.                             String name = (String) itProtocolInfos.next();
  817.                             String value = protocolInfos.get(name);
  818.                             String nameWithPrefix = (prefixProtocolInfo!=null) ? prefixProtocolInfo.trim()+name : name;
  819.                             TransportUtils.setHeader(properties,nameWithPrefix,value);
  820.                         }
  821.                     }
  822.                 }
  823.                
  824.             }
  825.         }catch(Exception e){
  826.             throw new HeaderIntegrazioneException("UtilitiesIntegrazione, creazione delle proprieta' dell'header non riuscita: "+e.getMessage(),e);
  827.         }
  828.     }
  829.    
  830.    

  831.     public void readHeader(OpenSPCoop2SoapMessage message,HeaderIntegrazione integrazione,
  832.             String actorIntegrazione) throws HeaderIntegrazioneException{
  833.        
  834.        
  835.         try{            
  836.             if(actorIntegrazione==null)
  837.                 throw new Exception("Actor non definito");
  838.             SOAPHeader header = message.getSOAPHeader();
  839.             if(header==null){
  840.                 OpenSPCoop2Logger.getLoggerOpenSPCoopCore().debug("SOAPHeader non presente");
  841.                 return;
  842.             }
  843.             SOAPHeaderElement headerElement = null;
  844.             java.util.Iterator<?> it = header.examineAllHeaderElements();
  845.             while( it.hasNext()  ){
  846.                 // Test Header Element
  847.                 headerElement = (SOAPHeaderElement) it.next();
  848.                 //Controllo Actor
  849.                 String actorCheck = SoapUtils.getSoapActor(headerElement, message.getMessageType());
  850.                 if( actorIntegrazione.equals(actorCheck) ){
  851.                     break;
  852.                 }else{
  853.                     headerElement = null;
  854.                 }
  855.             }
  856.             if(headerElement==null){
  857.                 OpenSPCoop2Logger.getLoggerOpenSPCoopCore().debug("Header di integrazione non presente");
  858.                 return;
  859.             }
  860.            
  861.             // validazione XSD
  862.             if(MessageType.SOAP_11.equals(message.getMessageType())){
  863.                 ValidatoreXSD validatoreXSD_soap11 = getValidatoreXSD(false, message.getFactory());
  864.                 if(validatoreXSD_soap11==null)
  865.                     throw new Exception("Validatore XSD (Soap11) non istanziato");
  866.                 validatoreXSD_soap11.valida(new java.io.ByteArrayInputStream(message.getAsByte(headerElement, false)));
  867.             }
  868.             else if(MessageType.SOAP_12.equals(message.getMessageType())){
  869.                 ValidatoreXSD validatoreXSD_soap12 = getValidatoreXSD(false, message.getFactory());
  870.                 if(validatoreXSD_soap12==null)
  871.                     throw new Exception("Validatore XSD (Soap12) non istanziato");
  872.                 validatoreXSD_soap12.valida(new java.io.ByteArrayInputStream(message.getAsByte(headerElement, false)));
  873.             }
  874.             else{
  875.                 throw MessageNotSupportedException.newMessageNotSupportedException(message.getMessageType());
  876.             }

  877.            
  878.             // Ricerca tra gli attributi dell'header SOAP
  879.             String tipoMittente = null;
  880.             try{
  881.                 if(this.keyReadEnabled_HeaderIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_TIPO_MITTENTE)) {
  882.                     tipoMittente = headerElement.getAttribute((String)this.keyValueIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_TIPO_MITTENTE));
  883.                 }
  884.             }catch(Exception e){}
  885.             if(tipoMittente!=null && tipoMittente.compareTo("")!=0)
  886.                 integrazione.getBusta().setTipoMittente(tipoMittente);
  887.            
  888.             String mittente = null;
  889.             try{
  890.                 if(this.keyReadEnabled_HeaderIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_MITTENTE)) {
  891.                     mittente = headerElement.getAttribute((String)this.keyValueIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_MITTENTE));
  892.                 }
  893.             }catch(Exception e){}
  894.             if(mittente!=null && mittente.compareTo("")!=0)
  895.                 integrazione.getBusta().setMittente(mittente);
  896.            
  897.             String tipoDestinatario = null;
  898.             try{
  899.                 if(this.keyReadEnabled_HeaderIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_TIPO_DESTINATARIO)) {
  900.                     tipoDestinatario = headerElement.getAttribute((String)this.keyValueIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_TIPO_DESTINATARIO));
  901.                 }
  902.             }catch(Exception e){}
  903.             if(tipoDestinatario!=null && tipoDestinatario.compareTo("")!=0)
  904.                 integrazione.getBusta().setTipoDestinatario(tipoDestinatario);
  905.            
  906.             String destinatario = null;
  907.             try{
  908.                 if(this.keyReadEnabled_HeaderIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_DESTINATARIO)) {
  909.                     destinatario = headerElement.getAttribute((String)this.keyValueIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_DESTINATARIO));
  910.                 }
  911.             }catch(Exception e){}
  912.             if(destinatario!=null && destinatario.compareTo("")!=0)
  913.                 integrazione.getBusta().setDestinatario(destinatario);

  914.             String tipoServizio = null;
  915.             try{
  916.                 if(this.keyReadEnabled_HeaderIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_TIPO_SERVIZIO)) {
  917.                     tipoServizio = headerElement.getAttribute((String)this.keyValueIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_TIPO_SERVIZIO));
  918.                 }
  919.             }catch(Exception e){}
  920.             if(tipoServizio!=null && tipoServizio.compareTo("")!=0)
  921.                 integrazione.getBusta().setTipoServizio(tipoServizio);
  922.            
  923.             String servizio = null;
  924.             try{
  925.                 if(this.keyReadEnabled_HeaderIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_SERVIZIO)) {
  926.                     servizio = headerElement.getAttribute((String)this.keyValueIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_SERVIZIO));
  927.                 }
  928.             }catch(Exception e){}
  929.             if(servizio!=null && servizio.compareTo("")!=0)
  930.                 integrazione.getBusta().setServizio(servizio);
  931.            
  932.             String versioneServizio = null;
  933.             try{
  934.                 if(this.keyReadEnabled_HeaderIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_VERSIONE_SERVIZIO)) {
  935.                     versioneServizio = headerElement.getAttribute((String)this.keyValueIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_VERSIONE_SERVIZIO));
  936.                 }
  937.             }catch(Exception e){}
  938.             if(versioneServizio!=null && versioneServizio.compareTo("")!=0){
  939.                 try{
  940.                     integrazione.getBusta().setVersioneServizio(Integer.parseInt(versioneServizio));
  941.                 }catch(Exception e){
  942.                     throw new Exception("Formato versione ["+versioneServizio+"] non corretto: "+e.getMessage(),e);
  943.                 }
  944.             }

  945.             String azione= null;
  946.             try{
  947.                 if(this.keyReadEnabled_HeaderIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_AZIONE)) {
  948.                     azione= headerElement.getAttribute((String)this.keyValueIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_AZIONE));
  949.                 }
  950.             }catch(Exception e){}
  951.             if(azione!=null && azione.compareTo("")!=0)
  952.                 integrazione.getBusta().setAzione(azione);

  953.             String idBusta = null;
  954.             try{
  955.                 if(this.keyReadEnabled_HeaderIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_ID_MESSAGGIO)) {
  956.                     idBusta = headerElement.getAttribute((String)this.keyValueIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_ID_MESSAGGIO));
  957.                 }
  958.             }catch(Exception e){}
  959.             if(idBusta!=null && idBusta.compareTo("")!=0)
  960.                 integrazione.getBusta().setID(idBusta);

  961.             String riferimentoMessaggio = null;
  962.             try{
  963.                 if(this.keyReadEnabled_HeaderIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_RIFERIMENTO_MESSAGGIO)) {
  964.                     riferimentoMessaggio = headerElement.getAttribute((String)this.keyValueIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_RIFERIMENTO_MESSAGGIO));
  965.                 }
  966.             }catch(Exception e){}
  967.             if(riferimentoMessaggio!=null && riferimentoMessaggio.compareTo("")!=0)
  968.                 integrazione.getBusta().setRiferimentoMessaggio(riferimentoMessaggio);

  969.             String collaborazione = null;
  970.             try{
  971.                 if(this.keyReadEnabled_HeaderIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_COLLABORAZIONE)) {
  972.                     collaborazione = headerElement.getAttribute((String)this.keyValueIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_COLLABORAZIONE));
  973.                 }
  974.             }catch(Exception e){}
  975.             if(collaborazione!=null && collaborazione.compareTo("")!=0)
  976.                 integrazione.getBusta().setIdCollaborazione(collaborazione);

  977.             String idApplicativo = null;
  978.             try{
  979.                 if(this.keyReadEnabled_HeaderIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_ID_APPLICATIVO)) {
  980.                     idApplicativo = headerElement.getAttribute((String)this.keyValueIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_ID_APPLICATIVO));
  981.                 }
  982.             }catch(Exception e){}
  983.             if(idApplicativo!=null && idApplicativo.compareTo("")!=0)
  984.                 integrazione.setIdApplicativo(idApplicativo);

  985.             String sa = null;
  986.             try{
  987.                 if(this.keyReadEnabled_HeaderIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_SERVIZIO_APPLICATIVO)) {
  988.                     sa = headerElement.getAttribute((String)this.keyValueIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_SERVIZIO_APPLICATIVO));
  989.                 }
  990.             }catch(Exception e){}
  991.             if(sa!=null && sa.compareTo("")!=0)
  992.                 integrazione.setServizioApplicativo(sa);
  993.            
  994.             String tipoMittenteToken = null;
  995.             try{
  996.                 if(this.keyReadEnabled_HeaderIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_TIPO_MITTENTE_TOKEN)) {
  997.                     tipoMittenteToken = headerElement.getAttribute((String)this.keyValueIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_TIPO_MITTENTE_TOKEN));
  998.                 }
  999.             }catch(Exception e){}
  1000.             if(tipoMittenteToken!=null && tipoMittenteToken.compareTo("")!=0)
  1001.                 integrazione.setTipoSoggettoProprietarioApplicativoToken(tipoMittenteToken);
  1002.            
  1003.             String mittenteToken = null;
  1004.             try{
  1005.                 if(this.keyReadEnabled_HeaderIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_MITTENTE_TOKEN)) {
  1006.                     mittenteToken = headerElement.getAttribute((String)this.keyValueIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_MITTENTE_TOKEN));
  1007.                 }
  1008.             }catch(Exception e){}
  1009.             if(mittenteToken!=null && mittenteToken.compareTo("")!=0)
  1010.                 integrazione.setNomeSoggettoProprietarioApplicativoToken(mittenteToken);
  1011.            
  1012.             String saToken = null;
  1013.             try{
  1014.                 if(this.keyReadEnabled_HeaderIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_SERVIZIO_APPLICATIVO_TOKEN)) {
  1015.                     saToken = headerElement.getAttribute((String)this.keyValueIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_SERVIZIO_APPLICATIVO_TOKEN));
  1016.                 }
  1017.             }catch(Exception e){}
  1018.             if(sa!=null && sa.compareTo("")!=0)
  1019.                 integrazione.setServizioApplicativoToken(saToken);
  1020.            
  1021.             String idTransazione = null;
  1022.             try{
  1023.                 if(this.keyReadEnabled_HeaderIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_ID_TRANSAZIONE)) {
  1024.                     idTransazione = headerElement.getAttribute((String)this.keyValueIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_ID_TRANSAZIONE));
  1025.                 }
  1026.             }catch(Exception e){}
  1027.             if(idTransazione!=null && idTransazione.compareTo("")!=0)
  1028.                 integrazione.setIdTransazione(idTransazione);
  1029.            
  1030.         }catch(Exception e){
  1031.             throw new HeaderIntegrazioneException("UtilitiesIntegrazione, lettura dell'header soap non riuscita: "+e.getMessage(),e);
  1032.         }
  1033.     }

  1034.     public void updateHeader(OpenSPCoop2SoapMessage message,IDSoggetto soggettoFruitore,IDServizio idServizio,
  1035.             String idBusta,String servizioApplicativo,
  1036.             String correlazioneApplicativa,String riferimentoCorrelazioneApplicativaRichiesta, String idTransazione,
  1037.             String actorIntegrazione,String nomeElemento,String prefix,String namespace,
  1038.             String proprietaProtocolloNomeElemento,String proprietaProtocolloNomeTipoElemento,
  1039.             Map<String, String> protocolInfos) throws Exception{
  1040.         updateHeader(message, soggettoFruitore, idServizio, idBusta, null,
  1041.                 servizioApplicativo, correlazioneApplicativa, riferimentoCorrelazioneApplicativaRichiesta, idTransazione,
  1042.                 actorIntegrazione, nomeElemento, prefix, namespace,
  1043.                 proprietaProtocolloNomeElemento, proprietaProtocolloNomeTipoElemento, protocolInfos);
  1044.     }
  1045.    
  1046.     public void updateHeader(OpenSPCoop2SoapMessage message,IDSoggetto soggettoFruitore,IDServizio idServizio,
  1047.             String idBusta,String idBustaRisposta,String servizioApplicativo,
  1048.             String correlazioneApplicativa,String riferimentoCorrelazioneApplicativaRichiesta, String idTransazione,
  1049.             String actorIntegrazione,String nomeElemento,String prefix,String namespace,
  1050.             String proprietaProtocolloNomeElemento,String proprietaProtocolloNomeTipoElemento,
  1051.             Map<String, String> protocolInfos) throws Exception{
  1052.        
  1053.         HeaderIntegrazione integrazione = new HeaderIntegrazione(idTransazione);
  1054.         integrazione.setIdApplicativo(correlazioneApplicativa);
  1055.         integrazione.setServizioApplicativo(servizioApplicativo);
  1056.         HeaderIntegrazioneBusta busta = new HeaderIntegrazioneBusta();
  1057.         busta.setTipoMittente(soggettoFruitore.getTipo());
  1058.         busta.setMittente(soggettoFruitore.getNome());
  1059.         busta.setTipoDestinatario(idServizio.getSoggettoErogatore().getTipo());
  1060.         busta.setDestinatario(idServizio.getSoggettoErogatore().getNome());
  1061.         busta.setTipoServizio(idServizio.getTipo());
  1062.         busta.setServizio(idServizio.getNome());
  1063.         busta.setVersioneServizio(idServizio.getVersione());
  1064.         busta.setAzione(idServizio.getAzione());
  1065.         if(idBustaRisposta==null){
  1066.             busta.setID(idBusta);
  1067.         }
  1068.         else{
  1069.             busta.setID(idBustaRisposta);
  1070.             busta.setRiferimentoMessaggio(idBusta);
  1071.         }
  1072.         integrazione.setBusta(busta);
  1073.        
  1074.         this.updateHeader(message, integrazione, actorIntegrazione, nomeElemento, prefix, namespace,
  1075.                 proprietaProtocolloNomeElemento, proprietaProtocolloNomeTipoElemento, protocolInfos);
  1076.     }
  1077.        
  1078.     public void updateHeader(OpenSPCoop2SoapMessage message,HeaderIntegrazione integrazione,
  1079.             String actorIntegrazione,String nomeElemento,String prefix,String namespace,
  1080.             String proprietaProtocolloNomeElemento,String proprietaProtocolloNomeTipoElemento,
  1081.             Map<String, String> protocolInfos) throws Exception{
  1082.        
  1083.         if(actorIntegrazione==null)
  1084.             throw new Exception("Actor non definito");
  1085.         SOAPHeader header = message.getSOAPHeader();
  1086.         SOAPHeaderElement headerIntegrazione = null;
  1087.         if(header==null){
  1088.            
  1089.             // Creo soap header
  1090.             OpenSPCoop2Logger.getLoggerOpenSPCoopCore().debug("SOAPHeader non presente: add soapHeader");
  1091.             header = message.getSOAPPart().getEnvelope().addHeader();
  1092.            
  1093.         }else{

  1094.             // cerco soap di integrazione
  1095.             java.util.Iterator<?> it = header.examineAllHeaderElements();
  1096.             while( it.hasNext()  ){
  1097.                 // Test Header Element
  1098.                 headerIntegrazione = (SOAPHeaderElement) it.next();
  1099.                 //Controllo Actor
  1100.                 String actorCheck = SoapUtils.getSoapActor(headerIntegrazione, message.getMessageType());
  1101.                 if( actorIntegrazione.equals(actorCheck) ){
  1102.                     break;
  1103.                 }else{
  1104.                     headerIntegrazione = null;
  1105.                 }
  1106.             }
  1107.             if(headerIntegrazione==null){
  1108.                 OpenSPCoop2Logger.getLoggerOpenSPCoopCore().debug("Header di integrazione non presente, lo creo");
  1109.             }
  1110.         }

  1111.         List<SOAPElement> v = new ArrayList<SOAPElement>(); // mantengo eventuali message element presenti
  1112.         if(headerIntegrazione!=null){
  1113.            
  1114.             java.util.Iterator<?> it = headerIntegrazione.getChildElements();
  1115.             if(it.hasNext()){
  1116.                 SOAPElement tmp = (SOAPElement) it.next();
  1117.                 //System.out.println("CONSERVO MSG ELEMENT["+tmp.getLocalName()+"]");
  1118.                 v.add(tmp);
  1119.             }
  1120.              
  1121.             header.removeChild(headerIntegrazione);
  1122.         }
  1123.        
  1124.         // creo header da nuovo
  1125.         SOAPHeaderElement headerIntegrazioneNEW = this.buildHeader(integrazione, nomeElemento, prefix, namespace,
  1126.                 actorIntegrazione, message,
  1127.                 proprietaProtocolloNomeElemento, proprietaProtocolloNomeTipoElemento, protocolInfos);  
  1128.        
  1129.         // Riaggiungo eventuali elementi interni
  1130.         while(v.size()>0){
  1131.             SOAPElement tmp = v.remove(0);
  1132.             //System.out.println("RIAGGIUNGO MSG ELEMENT["+tmp.getLocalName()+"]");
  1133.             headerIntegrazioneNEW.addChildElement(tmp);
  1134.         }
  1135.        
  1136.         //System.out.println("OTTENGO ["+headerIntegrazioneNEW.getAsString()+"]");
  1137.        
  1138.         // aggiungo header element al SOAP Header
  1139.         //header.addChildElement(headerIntegrazioneNEW);
  1140.         message.addHeaderElement(header, headerIntegrazioneNEW);

  1141.     }
  1142.    
  1143.     public SOAPHeaderElement buildHeader(HeaderIntegrazione integrazione,String nomeElemento,
  1144.             String prefix,String namespace, String actor,
  1145.             OpenSPCoop2SoapMessage m,
  1146.             String proprietaProtocolloNomeElemento,String proprietaProtocolloNomeTipoElemento,
  1147.             Map<String, String> protocolInfos) throws HeaderIntegrazioneException{

  1148.         try{
  1149.             SOAPHeader soapHeader = m.getSOAPHeader();
  1150.             if(soapHeader==null){
  1151.                 soapHeader = m.getSOAPPart().getEnvelope().addHeader();
  1152.             }
  1153.             SOAPHeaderElement header = m.newSOAPHeaderElement(soapHeader, new QName(namespace,nomeElemento,prefix));

  1154.             header.setActor(actor);
  1155.             header.setMustUnderstand(false);
  1156.            
  1157.             setAttributes(integrazione,header);
  1158.            
  1159.             if(this.keySetEnabled_HeaderIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_PROTOCOL_INFO)) {
  1160.                 if(protocolInfos!=null && protocolInfos.size()>0){
  1161.                    
  1162.                     String prefixProtocolInfo = this.keyValueIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_PROTOCOL_INFO);
  1163.                    
  1164.                     Iterator<String> itProtocolInfos = protocolInfos.keySet().iterator();
  1165.                     while (itProtocolInfos.hasNext()) {
  1166.                         String name = (String) itProtocolInfos.next();
  1167.                         String value = protocolInfos.get(name);
  1168.                         String nameWithPrefix = (prefixProtocolInfo!=null) ? prefixProtocolInfo.trim()+name : name;
  1169.                         SOAPElement element = header.addChildElement(new QName(namespace,proprietaProtocolloNomeElemento,prefix));
  1170.                         element.setTextContent(value);
  1171.                         @SuppressWarnings("unused")
  1172.                         SOAPElement attribute = element.addAttribute(new QName(proprietaProtocolloNomeTipoElemento),nameWithPrefix);
  1173.                     }
  1174.                 }
  1175.             }
  1176.            
  1177.             return header;

  1178.         }catch(Exception e){
  1179.             throw new HeaderIntegrazioneException("UtilitiesIntegrazione, creazione dell'header soap non riuscita: "+e.getMessage(),e);
  1180.         }
  1181.     }
  1182.    
  1183.    
  1184.     public void setAttributes(HeaderIntegrazione integrazione, SOAPHeaderElement header){
  1185.         if(integrazione.getBusta()!=null){

  1186.             if(integrazione.getBusta().getTipoMittente()!=null){
  1187.                 if(this.keySetEnabled_HeaderIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_TIPO_MITTENTE)) {
  1188.                     header.setAttribute((String)this.keyValueIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_TIPO_MITTENTE), integrazione.getBusta().getTipoMittente());
  1189.                 }
  1190.             }
  1191.             if(integrazione.getBusta().getMittente()!=null){
  1192.                 if(this.keySetEnabled_HeaderIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_MITTENTE)) {
  1193.                     header.setAttribute((String)this.keyValueIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_MITTENTE), integrazione.getBusta().getMittente());
  1194.                 }
  1195.             }

  1196.             if(integrazione.getBusta().getTipoDestinatario()!=null){
  1197.                 if(this.keySetEnabled_HeaderIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_TIPO_DESTINATARIO)) {
  1198.                     header.setAttribute((String)this.keyValueIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_TIPO_DESTINATARIO), integrazione.getBusta().getTipoDestinatario());
  1199.                 }
  1200.             }
  1201.             if(integrazione.getBusta().getDestinatario()!=null){
  1202.                 if(this.keySetEnabled_HeaderIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_DESTINATARIO)) {
  1203.                     header.setAttribute((String)this.keyValueIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_DESTINATARIO), integrazione.getBusta().getDestinatario());
  1204.                 }
  1205.             }

  1206.             if(integrazione.getBusta().getTipoServizio()!=null){
  1207.                 if(this.keySetEnabled_HeaderIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_TIPO_SERVIZIO)) {
  1208.                     header.setAttribute((String)this.keyValueIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_TIPO_SERVIZIO), integrazione.getBusta().getTipoServizio());
  1209.                 }
  1210.             }
  1211.             if(integrazione.getBusta().getServizio()!=null){
  1212.                 if(this.keySetEnabled_HeaderIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_SERVIZIO)) {
  1213.                     header.setAttribute((String)this.keyValueIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_SERVIZIO), integrazione.getBusta().getServizio());
  1214.                 }
  1215.             }
  1216.             if(integrazione.getBusta().getVersioneServizio()!=null){
  1217.                 if(this.keySetEnabled_HeaderIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_VERSIONE_SERVIZIO)) {
  1218.                     header.setAttribute((String)this.keyValueIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_VERSIONE_SERVIZIO), integrazione.getBusta().getVersioneServizio().intValue()+"");
  1219.                 }
  1220.             }

  1221.             if(integrazione.getBusta().getAzione()!=null){
  1222.                 if(this.keySetEnabled_HeaderIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_AZIONE)) {
  1223.                     header.setAttribute((String)this.keyValueIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_AZIONE), integrazione.getBusta().getAzione());
  1224.                 }
  1225.             }

  1226.             if(integrazione.getBusta().getID()!=null){
  1227.                 if(this.keySetEnabled_HeaderIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_ID_MESSAGGIO)) {
  1228.                     header.setAttribute((String)this.keyValueIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_ID_MESSAGGIO), integrazione.getBusta().getID());
  1229.                 }
  1230.             }

  1231.             if(integrazione.getBusta().getRiferimentoMessaggio()!=null){
  1232.                 if(this.keySetEnabled_HeaderIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_RIFERIMENTO_MESSAGGIO)) {
  1233.                     header.setAttribute((String)this.keyValueIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_RIFERIMENTO_MESSAGGIO), integrazione.getBusta().getRiferimentoMessaggio());
  1234.                 }
  1235.             }

  1236.             if(integrazione.getBusta().getIdCollaborazione()!=null){
  1237.                 if(this.keySetEnabled_HeaderIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_COLLABORAZIONE)) {
  1238.                     header.setAttribute((String)this.keyValueIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_COLLABORAZIONE), integrazione.getBusta().getIdCollaborazione());
  1239.                 }
  1240.             }
  1241.         }

  1242.         if(integrazione.getIdApplicativo()!=null){
  1243.             if(this.keySetEnabled_HeaderIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_ID_APPLICATIVO)) {
  1244.                 header.setAttribute((String)this.keyValueIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_ID_APPLICATIVO), integrazione.getIdApplicativo());
  1245.             }
  1246.         }
  1247.        
  1248.         if(integrazione.getServizioApplicativo()!=null){
  1249.             if(this.keySetEnabled_HeaderIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_SERVIZIO_APPLICATIVO)) {
  1250.                 header.setAttribute((String)this.keyValueIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_SERVIZIO_APPLICATIVO), integrazione.getServizioApplicativo());
  1251.             }
  1252.         }
  1253.        
  1254.         if(integrazione.getTipoSoggettoProprietarioApplicativoToken()!=null){
  1255.             if(this.keySetEnabled_HeaderIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_TIPO_MITTENTE_TOKEN)) {
  1256.                 header.setAttribute((String)this.keyValueIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_TIPO_MITTENTE_TOKEN), integrazione.getTipoSoggettoProprietarioApplicativoToken());
  1257.             }
  1258.         }
  1259.         if(integrazione.getNomeSoggettoProprietarioApplicativoToken()!=null){
  1260.             if(this.keySetEnabled_HeaderIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_MITTENTE_TOKEN)) {
  1261.                 header.setAttribute((String)this.keyValueIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_MITTENTE_TOKEN), integrazione.getNomeSoggettoProprietarioApplicativoToken());
  1262.             }
  1263.         }
  1264.        
  1265.         if(integrazione.getServizioApplicativoToken()!=null){
  1266.             if(this.keySetEnabled_HeaderIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_SERVIZIO_APPLICATIVO_TOKEN)) {
  1267.                 header.setAttribute((String)this.keyValueIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_SERVIZIO_APPLICATIVO_TOKEN), integrazione.getServizioApplicativoToken());
  1268.             }
  1269.         }
  1270.        
  1271.         if(integrazione.getIdTransazione()!=null){
  1272.             if(this.keySetEnabled_HeaderIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_ID_TRANSAZIONE)) {
  1273.                 header.setAttribute((String)this.keyValueIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_ID_TRANSAZIONE), integrazione.getIdTransazione());
  1274.             }
  1275.         }
  1276.        
  1277.         if(this.keySetEnabled_HeaderIntegrazioneSoap.get(CostantiPdD.HEADER_INTEGRAZIONE_INFO)) {
  1278.            
  1279.             header.setAttribute(CostantiPdD.HEADER_INTEGRAZIONE_SOAP_PDD_VERSION, this.openspcoopProperties.getHeaderIntegrazioneSOAPPdDVersione());
  1280.             if(this.openspcoopProperties.getHeaderIntegrazioneSOAPPdDDetails()!=null && !"".equals(this.openspcoopProperties.getHeaderIntegrazioneSOAPPdDDetails())){
  1281.                 header.setAttribute(CostantiPdD.HEADER_INTEGRAZIONE_SOAP_PDD_DETAILS, this.openspcoopProperties.getHeaderIntegrazioneSOAPPdDDetails());
  1282.             }
  1283.            
  1284.         }

  1285.     }
  1286.    
  1287.    
  1288.     public void deleteHeader(OpenSPCoop2SoapMessage message,String actorIntegrazione) throws HeaderIntegrazioneException{

  1289.         try{

  1290.             if(actorIntegrazione==null)
  1291.                 throw new Exception("Actor non definito");
  1292.             SOAPHeader header = message.getSOAPHeader();
  1293.             if(header==null){
  1294.                 OpenSPCoop2Logger.getLoggerOpenSPCoopCore().debug("SOAPHeader non presente");
  1295.                 return;
  1296.             }
  1297.             SOAPHeaderElement headerElement = null;
  1298.             java.util.Iterator<?> it = header.examineAllHeaderElements();
  1299.             while( it.hasNext()  ){
  1300.                 // Test Header Element
  1301.                 headerElement = (SOAPHeaderElement) it.next();
  1302.                 //Controllo Actor
  1303.                 String actorCheck = SoapUtils.getSoapActor(headerElement, message.getMessageType());
  1304.                 if( actorIntegrazione.equals(actorCheck) ){
  1305.                     break;
  1306.                 }else{
  1307.                     headerElement = null;
  1308.                 }
  1309.             }
  1310.             if(headerElement==null){
  1311.                 OpenSPCoop2Logger.getLoggerOpenSPCoopCore().debug("Header di integrazione non presente");
  1312.                 return;
  1313.             }
  1314.            
  1315.             header.removeChild(headerElement);
  1316.            
  1317.         }catch(Exception e){
  1318.             throw new HeaderIntegrazioneException("UtilitiesIntegrazione, eliminazione dell'header soap non riuscita: "+e.getMessage(),e);
  1319.         }
  1320.     }
  1321.    
  1322.     public static String getIdTransazione(PdDContext context){
  1323.         if(context!=null){
  1324.             return (String) context.getObject(Costanti.ID_TRANSAZIONE);
  1325.         }
  1326.         else{
  1327.             return null;
  1328.         }
  1329.     }
  1330. }