UtilitiesIntegrazioneWSAddressing.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.Iterator;

  22. import javax.xml.soap.SOAPElement;
  23. import javax.xml.soap.SOAPHeader;
  24. import javax.xml.soap.SOAPHeaderElement;

  25. import org.openspcoop2.core.id.IDServizio;
  26. import org.openspcoop2.core.id.IDSoggetto;
  27. import org.openspcoop2.message.OpenSPCoop2SoapMessage;
  28. import org.openspcoop2.message.soap.wsaddressing.Costanti;
  29. import org.openspcoop2.message.soap.wsaddressing.WSAddressingHeader;
  30. import org.openspcoop2.message.soap.wsaddressing.WSAddressingUtilities;
  31. import org.openspcoop2.pdd.logger.OpenSPCoop2Logger;
  32. import org.slf4j.Logger;


  33. /**
  34.  * Classe contenenti utilities per le integrazioni.
  35.  *
  36.  * @author Poli Andrea (apoli@link.it)
  37.  * @author $Author$
  38.  * @version $Rev$, $Date$
  39.  */
  40. public class UtilitiesIntegrazioneWSAddressing {

  41.     // ***** STATIC *****
  42.    
  43.     /**
  44.        SOAPHeaderElement wsaTO        ->     http://<providerType>_<provider>.govway.org/services/<serviceType>_<service>/<serviceVersion>
  45.        SOAPHeaderElement wsaFROM      ->     http://[<application>.]<senderType>_<sender>.govway.org
  46.        SOAPHeaderElement wsaAction    ->     http://<providerType>_<provider>.govway.org/services/<serviceType>_<service>/<serviceVersion>/<action>
  47.        SOAPHeaderElement wsaID        ->     uuid:<messageId> in caso di Messaggio di Protocollo (restituzione di una risposto lato PD o in caso di consegna tramite PA
  48.                                              uuid:<applicationMessageId> in caso di Messaggio di Integrazione (invocazione lato PD o lettura risposta lato PA, es. per correlazione applicativa)
  49.        SOAPHeaderElement wsaRelatesTo ->     uuid:<messageId> equivale al riferimento messaggio
  50.      */
  51.     private static final String WSA_TO_FORMAT = "http://<providerType>_<provider>.govway.org/services/<serviceType>_<service>/<serviceVersion>";
  52.     private static final String WSA_FROM_FORMAT = "http://[<application>.]<senderType>_<sender>.govway.org";
  53.     private static final String WSA_ACTION_FORMAT = "http://<providerType>_<provider>.govway.org/services/<serviceType>_<service>/<serviceVersion>/<action>";
  54.     private static final String WSA_ID_FORMAT = "uuid:<id>";
  55.     private static final String WSA_RELATES_TO_FORMAT = "uuid:<id>";
  56.    
  57.     public static final boolean INTERPRETA_COME_ID_BUSTA = true;
  58.     public static final boolean INTERPRETA_COME_ID_APPLICATIVO = false;
  59.    
  60.     private static final boolean MUST_UNDERSTAND = false;
  61.    
  62.    
  63.     public static void _readDatiWSAToOrAction(String wsaValue,String format, HeaderIntegrazione integrazione) throws HeaderIntegrazioneException{
  64.        
  65.         String type = null;
  66.         boolean action = false;
  67.         if(WSA_TO_FORMAT.equals(format)){
  68.             type = "WSAddressingTo";    
  69.         }
  70.         else if(WSA_ACTION_FORMAT.equals(format)){
  71.             type = "WSAddressingAction";    
  72.             action = true;
  73.         }
  74.         else{
  75.             throw new HeaderIntegrazioneException("Format ["+format+"] Not Supported");
  76.         }
  77.        
  78.         if(wsaValue==null)
  79.             throw new HeaderIntegrazioneException(type+" value is null");
  80.         wsaValue = wsaValue.trim();
  81.         if(wsaValue.startsWith("http://")==false)
  82.             throw new HeaderIntegrazioneException(type+" Value is not valid: "+format);
  83.         if(wsaValue.contains(".govway.org/services/")==false)
  84.             throw new HeaderIntegrazioneException(type+" Value is not valid: "+format);
  85.        
  86.         wsaValue = wsaValue.substring(7, wsaValue.length());
  87.        
  88.         // soggetto
  89.         int indexSoggetto = wsaValue.indexOf(".govway.org/services/");
  90.         String soggetto = wsaValue.substring(0, indexSoggetto);
  91.         if(soggetto==null){
  92.             throw new HeaderIntegrazioneException(type+" Value is not valid (Soggetto non identificabile): "+format);
  93.         }
  94.         if(soggetto.contains("_")==false){
  95.             throw new HeaderIntegrazioneException(type+" Value is not valid (Soggetto non identificabile, '_' non trovato ): "+format);
  96.         }
  97.         String [] soggetto_split = soggetto.split("_");
  98.         if(soggetto_split==null || soggetto_split.length<2){
  99.             throw new HeaderIntegrazioneException(type+" Value is not valid (Soggetto non identificabile, formato errato ): "+format);
  100.         }
  101.        
  102.         String tipoSoggetto = soggetto_split[0];
  103.         if(tipoSoggetto!=null){
  104.             tipoSoggetto = tipoSoggetto.trim();
  105.         }
  106.         if(tipoSoggetto==null || "".equals(tipoSoggetto)){
  107.             throw new HeaderIntegrazioneException(type+" Value is not valid (TipoSoggetto non identificabile): "+format);
  108.         }
  109.        
  110.         String nomeSoggetto = soggetto.substring((tipoSoggetto+"_").length());
  111.         if(nomeSoggetto!=null){
  112.             nomeSoggetto = nomeSoggetto.trim();
  113.         }
  114.         if(nomeSoggetto==null || "".equals(nomeSoggetto)){
  115.             throw new HeaderIntegrazioneException(type+" Value is not valid (NomeSoggetto non identificabile): "+format);
  116.         }

  117.         if(!action){
  118.             integrazione.getBusta().setTipoDestinatario(tipoSoggetto);
  119.             integrazione.getBusta().setDestinatario(nomeSoggetto);
  120.         }
  121.        
  122.         // servizio
  123.         String servizio = wsaValue.substring((indexSoggetto+".govway.org/services/".length()), wsaValue.length());
  124.         if(servizio==null){
  125.             throw new HeaderIntegrazioneException(type+" Value is not valid (Servizio non identificabile): "+format);
  126.         }
  127.         if(servizio.contains("_")==false){
  128.             throw new HeaderIntegrazioneException(type+" Value is not valid (Servizio non identificabile, '_' non trovato ): "+format);
  129.         }
  130.         String [] servizio_split = servizio.split("_");
  131.         if(servizio_split==null || servizio_split.length<2){
  132.             throw new HeaderIntegrazioneException(type+" Value is not valid (Servizio non identificabile, formato errato ): "+format);
  133.         }
  134.         String tipoServizio = servizio_split[0];
  135.         if(tipoServizio!=null){
  136.             tipoServizio = tipoServizio.trim();
  137.         }
  138.         if(tipoServizio==null || "".equals(tipoServizio)){
  139.             throw new HeaderIntegrazioneException(type+" Value is not valid (TipoServizio non identificabile): "+format);
  140.         }
  141.        
  142.         String nomeVersioneServizio = servizio.substring((tipoServizio+"_").length());
  143.         if(nomeVersioneServizio!=null){
  144.             nomeVersioneServizio = nomeVersioneServizio.trim();
  145.         }
  146.         if(nomeVersioneServizio==null || "".equals(nomeVersioneServizio)){
  147.             throw new HeaderIntegrazioneException(type+" Value is not valid (Nome e VersioneServizio non identificabile): "+format);
  148.         }
  149.         if(nomeVersioneServizio.contains("/")==false){
  150.             throw new HeaderIntegrazioneException(type+" Value is not valid (Nome e VersioneServizio non identificabile, '/' non trovato ): "+format);
  151.         }
  152.        
  153.         if(action){
  154.             // gli slash sono due
  155.             // nomeVersioneServizio contiene anche l'azione
  156.            
  157.             int indexAzione = nomeVersioneServizio.lastIndexOf("/");
  158.             if(indexAzione<=0){
  159.                 throw new HeaderIntegrazioneException(type+" Value is not valid (Azione non identificabile, '/' non trovato, index("+indexAzione+") ): "+format);
  160.             }
  161.            
  162.             String azione = nomeVersioneServizio.substring(indexAzione+1);
  163.             if(azione!=null){
  164.                 azione = azione.trim();
  165.             }
  166.             if(azione==null  || "".equals(azione)){
  167.                 throw new HeaderIntegrazioneException(type+" Value is not valid (Azione non identificabile): "+format);
  168.             }
  169.            
  170.             integrazione.getBusta().setAzione(azione);
  171.            
  172.             nomeVersioneServizio =  nomeVersioneServizio.substring(0, indexAzione);
  173.             if(nomeVersioneServizio!=null){
  174.                 nomeVersioneServizio = nomeVersioneServizio.trim();
  175.             }
  176.             if(nomeVersioneServizio==null || "".equals(nomeVersioneServizio)){
  177.                 throw new HeaderIntegrazioneException(type+" Value is not valid (Nome e VersioneServizio non identificabile non identificabile prima dell'Azione): "+format);
  178.             }
  179.         }
  180.        
  181.            
  182.         int indexVersioneServizio = nomeVersioneServizio.lastIndexOf("/");
  183.         if(indexVersioneServizio<=0){
  184.             throw new HeaderIntegrazioneException(type+" Value is not valid (VersioneServizio non identificabile, '/' non trovato, index("+indexVersioneServizio+") ): "+format);
  185.         }
  186.        
  187.         String nomeServizio = nomeVersioneServizio.substring(0, indexVersioneServizio);
  188.         if(nomeServizio!=null){
  189.             nomeServizio = nomeServizio.trim();
  190.         }
  191.         if(nomeServizio==null || "".equals(nomeServizio)){
  192.             throw new HeaderIntegrazioneException(type+" Value is not valid (NomeServizio non identificabile): "+format);
  193.         }
  194.        
  195.         String tmpVersioneServizio = nomeVersioneServizio.substring(indexVersioneServizio+1);
  196.         if(tmpVersioneServizio!=null){
  197.             tmpVersioneServizio = tmpVersioneServizio.trim();
  198.         }
  199.         if(tmpVersioneServizio==null  || "".equals(tmpVersioneServizio)){
  200.             throw new HeaderIntegrazioneException(type+" Value is not valid (VersioneServizio non identificabile): "+format);
  201.         }
  202.         Integer versioneServizio = null;
  203.         try{
  204.             versioneServizio = Integer.parseInt(tmpVersioneServizio);
  205.         }catch(Exception e){
  206.             throw new HeaderIntegrazioneException(type+" Value is not valid (VersioneServizio non identificabile, formato errato "+e.getMessage()+"): "+format);
  207.         }
  208.        
  209.         if(!action){
  210.             integrazione.getBusta().setTipoServizio(tipoServizio);
  211.             integrazione.getBusta().setServizio(nomeServizio);
  212.             integrazione.getBusta().setVersioneServizio(versioneServizio);
  213.         }
  214.        
  215.     }
  216.    
  217.    
  218.     public static void readDatiWSATo(String wsaTO,HeaderIntegrazione integrazione) throws HeaderIntegrazioneException{
  219.         _readDatiWSAToOrAction(wsaTO, WSA_TO_FORMAT, integrazione);
  220.     }
  221.     public static String buildDatiWSATo(String tipoSoggettoErogatore,String nomeSoggettoErogatore,String tipoServizio,String nomeServizio,Integer versioneServizio){
  222.         return "http://"+tipoSoggettoErogatore+"_"+nomeSoggettoErogatore+".govway.org/services/"+tipoServizio+"_"+nomeServizio+"/"+versioneServizio;
  223.     }
  224.     public static SOAPHeaderElement buildWSATo(OpenSPCoop2SoapMessage msg,String actor,String tipoSoggettoErogatore,String nomeSoggettoErogatore,String tipoServizio,String nomeServizio,Integer versioneServizio) throws Exception{
  225.         return WSAddressingUtilities.buildWSATo(msg, actor, MUST_UNDERSTAND,
  226.                 UtilitiesIntegrazioneWSAddressing.buildDatiWSATo(tipoSoggettoErogatore,nomeSoggettoErogatore,tipoServizio,nomeServizio,versioneServizio));
  227.     }
  228.    
  229.    
  230.     public static void readDatiWSAFrom(String wsaFrom,HeaderIntegrazione integrazione) throws HeaderIntegrazioneException{
  231.         if(wsaFrom==null)
  232.             throw new HeaderIntegrazioneException("WSAFrom value is null");
  233.         wsaFrom = wsaFrom.trim();
  234.         if(wsaFrom.startsWith("http://")==false)
  235.             throw new HeaderIntegrazioneException("WSAFrom Value is not valid: "+WSA_FROM_FORMAT);
  236.         if(wsaFrom.contains(".govway.org")==false)
  237.             throw new HeaderIntegrazioneException("WSAFrom Value is not valid: "+WSA_FROM_FORMAT);
  238.        
  239.         wsaFrom = wsaFrom.substring(7, wsaFrom.length());
  240.        
  241.         int indexSoggetto = wsaFrom.indexOf(".govway.org");
  242.         String soggetto = wsaFrom.substring(0, indexSoggetto);
  243.         if(soggetto==null){
  244.             throw new HeaderIntegrazioneException("WSAFrom Value is not valid (Soggetto non identificabile): "+WSA_FROM_FORMAT);
  245.         }  
  246.         if(soggetto.contains("_")==false){
  247.             throw new HeaderIntegrazioneException("WSAFrom Value is not valid (Soggetto non identificabile, '_' non trovato ): "+WSA_FROM_FORMAT);
  248.         }
  249.         String [] soggetto_split = soggetto.split("_");
  250.         if(soggetto_split==null || soggetto_split.length<2){
  251.             throw new HeaderIntegrazioneException("WSAFrom Value is not valid (Soggetto non identificabile, formato errato ): "+WSA_FROM_FORMAT);
  252.         }
  253.        
  254.         String tipoSoggetto = soggetto_split[0];
  255.         if(tipoSoggetto!=null){
  256.             tipoSoggetto = tipoSoggetto.trim();
  257.         }
  258.         if(tipoSoggetto==null || "".equals(tipoSoggetto)){
  259.             throw new HeaderIntegrazioneException("WSAFrom Value is not valid (TipoSoggetto non identificabile): "+WSA_FROM_FORMAT);
  260.         }
  261.        
  262.         String nomeSoggetto = soggetto.substring((tipoSoggetto+"_").length());
  263.         if(nomeSoggetto!=null){
  264.             nomeSoggetto = nomeSoggetto.trim();
  265.         }
  266.         if(nomeSoggetto==null || "".equals(nomeSoggetto)){
  267.             throw new HeaderIntegrazioneException("WSAFrom Value is not valid (NomeSoggetto non identificabile): "+WSA_FROM_FORMAT);
  268.         }
  269.        
  270.         if(tipoSoggetto.contains(".")){
  271.             String[] split = tipoSoggetto.split("\\.");
  272.             tipoSoggetto = split[1];
  273.             String sa = split[0];
  274.             if(tipoSoggetto==null){
  275.                 throw new HeaderIntegrazioneException("WSAFrom Value is not valid (TipoSoggetto non identificabile dopo parsing ServizioApplicativo): "+WSA_FROM_FORMAT);
  276.             }  
  277.             if(sa==null){
  278.                 throw new HeaderIntegrazioneException("WSAFrom Value is not valid (ServizioApplicativo non identificabile): "+WSA_FROM_FORMAT);
  279.             }
  280.             tipoSoggetto = tipoSoggetto.trim();
  281.             sa = sa.trim();
  282.             integrazione.setServizioApplicativo(sa);
  283.         }
  284.        
  285.         integrazione.getBusta().setTipoMittente(tipoSoggetto);
  286.         integrazione.getBusta().setMittente(nomeSoggetto);
  287.     }
  288.     public static String buildDatiWSAFrom(String servizioApplicativoFruitore,String tipoSoggetto,String nomeSoggetto){
  289.         if(servizioApplicativoFruitore==null)
  290.             return "http://"+tipoSoggetto+"_"+nomeSoggetto+".govway.org";
  291.         else
  292.             return "http://"+servizioApplicativoFruitore+"."+tipoSoggetto+"_"+nomeSoggetto+".govway.org";
  293.     }
  294.     public static SOAPHeaderElement buildWSAFrom(OpenSPCoop2SoapMessage msg,String actor,String servizioApplicativoFruitore,String tipoSoggetto,String nomeSoggetto) throws Exception{
  295.         return WSAddressingUtilities.buildWSAFrom(msg, actor, MUST_UNDERSTAND,
  296.                 UtilitiesIntegrazioneWSAddressing.buildDatiWSAFrom(servizioApplicativoFruitore,tipoSoggetto,nomeSoggetto));
  297.     }
  298.    
  299.    
  300.     public static void readDatiWSAAction(String wsaAction,HeaderIntegrazione integrazione) throws HeaderIntegrazioneException{  
  301.         _readDatiWSAToOrAction(wsaAction, WSA_ACTION_FORMAT, integrazione);
  302.     }
  303.     public static String buildDatiWSAAction(String tipoSoggettoErogatore,String nomeSoggettoErogatore,String tipoServizio,String nomeServizio,Integer versioneServizio,String azione){
  304.         return "http://"+tipoSoggettoErogatore+"_"+nomeSoggettoErogatore+".govway.org/services/"+tipoServizio+"_"+nomeServizio+"/"+versioneServizio+"/"+azione;
  305.     }
  306.     public static SOAPHeaderElement buildWSAAction(OpenSPCoop2SoapMessage msg,String actor,String tipoSoggettoErogatore,String nomeSoggettoErogatore,String tipoServizio,String nomeServizio,Integer versioneServizio,String azione) throws Exception{
  307.         return WSAddressingUtilities.buildWSAAction(msg, actor, MUST_UNDERSTAND,
  308.                 UtilitiesIntegrazioneWSAddressing.buildDatiWSAAction(tipoSoggettoErogatore,nomeSoggettoErogatore,tipoServizio,nomeServizio,versioneServizio,azione));
  309.     }
  310.    
  311.    
  312.     public static void readDatiWSAID(String wsaID,HeaderIntegrazione integrazione,boolean interpretaComeIDBusta) throws HeaderIntegrazioneException{
  313.         if(wsaID==null)
  314.             throw new HeaderIntegrazioneException("WSAID value is null");
  315.         wsaID = wsaID.trim();
  316.         if(wsaID.startsWith("uuid:")==false)
  317.             throw new HeaderIntegrazioneException("WSAID Value is not valid: "+WSA_ID_FORMAT);
  318.        
  319.         wsaID = wsaID.substring(5, wsaID.length());
  320.        
  321.         if(interpretaComeIDBusta){
  322.             integrazione.getBusta().setID(wsaID);
  323.         }else{
  324.             integrazione.setIdApplicativo(wsaID);
  325.         }
  326.     }
  327.     public static String buildDatiWSAID(String id){
  328.         return "uuid:"+id;
  329.     }
  330.     public static SOAPHeaderElement buildWSAID(OpenSPCoop2SoapMessage msg,String actor,String wsaID) throws Exception{
  331.         return WSAddressingUtilities.buildWSAID(msg, actor, MUST_UNDERSTAND,
  332.                 UtilitiesIntegrazioneWSAddressing.buildDatiWSAID(wsaID));
  333.     }
  334.    
  335.    
  336.     public static void readDatiWSARelatesTo(String wsaRelatesTo,HeaderIntegrazione integrazione) throws HeaderIntegrazioneException{
  337.         if(wsaRelatesTo==null)
  338.             throw new HeaderIntegrazioneException("WSARelatesTo value is null");
  339.         wsaRelatesTo = wsaRelatesTo.trim();
  340.         if(wsaRelatesTo.startsWith("uuid:")==false)
  341.             throw new HeaderIntegrazioneException("WSARelatesTo Value is not valid: "+WSA_RELATES_TO_FORMAT);
  342.        
  343.         wsaRelatesTo = wsaRelatesTo.substring(5, wsaRelatesTo.length());
  344.        
  345.         integrazione.getBusta().setRiferimentoMessaggio(wsaRelatesTo);
  346.        
  347.     }
  348.     public static String buildDatiWSARelatesTo(String id){
  349.         return "uuid:"+id;
  350.     }
  351.     public static SOAPHeaderElement buildWSARelatesTo(OpenSPCoop2SoapMessage msg,String actor, String id) throws Exception{
  352.         return WSAddressingUtilities.buildWSARelatesTo(msg, actor, MUST_UNDERSTAND,
  353.                 UtilitiesIntegrazioneWSAddressing.buildDatiWSARelatesTo(id));
  354.     }

  355.    
  356.    
  357.     private static UtilitiesIntegrazioneWSAddressing utilitiesIntegrazione = null;
  358.     public static UtilitiesIntegrazioneWSAddressing getInstance(Logger log){
  359.         if(UtilitiesIntegrazioneWSAddressing.utilitiesIntegrazione==null){
  360.             // spotbugs warning 'SING_SINGLETON_GETTER_NOT_SYNCHRONIZED': l'istanza viene creata allo startup
  361.             synchronized (UtilitiesIntegrazioneWSAddressing.class) {
  362.                 UtilitiesIntegrazioneWSAddressing.initialize(log);
  363.             }
  364.         }
  365.         return UtilitiesIntegrazioneWSAddressing.utilitiesIntegrazione;
  366.     }

  367.     private static synchronized void initialize(Logger log){
  368.         if(UtilitiesIntegrazioneWSAddressing.utilitiesIntegrazione==null){
  369.             UtilitiesIntegrazioneWSAddressing.utilitiesIntegrazione = new UtilitiesIntegrazioneWSAddressing(log);
  370.         }
  371.     }


  372.    
  373.    
  374.    
  375.    
  376.     // ***** INSTANCE *****
  377.    
  378.     private UtilitiesIntegrazioneWSAddressing(Logger log){
  379.         if(log!=null) {
  380.             // unused
  381.         }
  382.     }
  383.    
  384.     public void readHeader(OpenSPCoop2SoapMessage message,HeaderIntegrazione integrazione,
  385.             boolean interpretaIDComeIDBusta,String actorIntegrazione) throws HeaderIntegrazioneException{

  386.         try{

  387.             if(actorIntegrazione==null)
  388.                 throw new Exception("Actor non definito");
  389.            
  390.             Logger log = OpenSPCoop2Logger.getLoggerOpenSPCoopCore();
  391.            
  392.             SOAPHeader header = message.getSOAPHeader();
  393.             if(header==null){
  394.                 OpenSPCoop2Logger.getLoggerOpenSPCoopCore().debug("SOAPHeader non presente");
  395.                 return;
  396.             }
  397.            
  398.             SOAPHeaderElement wsaTO = null;
  399.             SOAPHeaderElement wsaFROM = null;
  400.             SOAPHeaderElement wsaAction = null;
  401.             SOAPHeaderElement wsaID = null;
  402.             SOAPHeaderElement wsaRelatesTo = null;
  403.            
  404.             WSAddressingUtilities wsaddressingUtilities = new WSAddressingUtilities(log);
  405.             WSAddressingHeader hdrReaded = wsaddressingUtilities.read(message, actorIntegrazione, false);
  406.             if(hdrReaded!=null) {
  407.                 wsaTO = hdrReaded.getTo();
  408.                 wsaFROM = hdrReaded.getFrom();
  409.                 wsaAction = hdrReaded.getAction();
  410.                 wsaID = hdrReaded.getId();
  411.                 wsaRelatesTo = hdrReaded.getRelatesTo();
  412.             }
  413.             if(wsaTO==null && wsaFROM==null  &&  wsaAction==null && wsaID==null  &&  wsaRelatesTo==null){
  414.                 log.debug("Header di integrazione non presente");
  415.                 return;
  416.             }
  417.            
  418.             // ValidazioneXSD
  419.             log.debug("Validazione XSD...");
  420.             wsaddressingUtilities.validate(message, hdrReaded);
  421.             log.debug("Validazione XSD effettuate");
  422.            
  423.             // delete
  424.             if(wsaTO!=null){
  425.                 log.debug("Read dati da WSATo...");
  426.                 UtilitiesIntegrazioneWSAddressing.readDatiWSATo(hdrReaded.getToValue(), integrazione);
  427.             }
  428.             if(wsaFROM!=null){
  429.                 log.debug("Read dati da WSAFrom...");
  430.                 UtilitiesIntegrazioneWSAddressing.readDatiWSAFrom(hdrReaded.getFromValue(), integrazione);
  431.             }
  432.             if(wsaAction!=null){
  433.                 log.debug("Read dati da WSAAction...");
  434.                 UtilitiesIntegrazioneWSAddressing.readDatiWSAAction(hdrReaded.getActionValue(), integrazione);
  435.             }
  436.             if(wsaID!=null){
  437.                 log.debug("Read dati da WSAId...");
  438.                 UtilitiesIntegrazioneWSAddressing.readDatiWSAID(hdrReaded.getIdValue(), integrazione, interpretaIDComeIDBusta);
  439.             }  
  440.             if(wsaRelatesTo!=null){
  441.                 log.debug("Read dati da WSARelatesTo...");
  442.                 UtilitiesIntegrazioneWSAddressing.readDatiWSARelatesTo(hdrReaded.getRelatesToValue(), integrazione);
  443.             }
  444.        
  445.         }catch(Exception e){
  446.             throw new HeaderIntegrazioneException("UtilitiesIntegrazione, lettura dell'header soap non riuscita: "+e.getMessage(),e);
  447.         }
  448.     }
  449.    
  450.    
  451.     public void updateHeader(OpenSPCoop2SoapMessage message,IDSoggetto soggettoFruitore,IDServizio idServizio,
  452.             String idBusta,String servizioApplicativo,
  453.             String correlazioneApplicativa, String idTransazione,String actorIntegrazione) throws Exception{
  454.         updateHeader(message, soggettoFruitore, idServizio, idBusta, null,
  455.                 servizioApplicativo, correlazioneApplicativa, idTransazione, actorIntegrazione);
  456.     }
  457.     public void updateHeader(OpenSPCoop2SoapMessage message,IDSoggetto soggettoFruitore,IDServizio idServizio,
  458.             String idBusta,String idBustaRisposta,String servizioApplicativo,
  459.             String correlazioneApplicativa, String idTransazione,String actorIntegrazione) throws Exception{
  460.        
  461.         HeaderIntegrazione integrazione = new HeaderIntegrazione(idTransazione);
  462.         integrazione.setIdApplicativo(correlazioneApplicativa);
  463.         integrazione.setServizioApplicativo(servizioApplicativo);
  464.         HeaderIntegrazioneBusta busta = new HeaderIntegrazioneBusta();
  465.         busta.setTipoMittente(soggettoFruitore.getTipo());
  466.         busta.setMittente(soggettoFruitore.getNome());
  467.         busta.setTipoDestinatario(idServizio.getSoggettoErogatore().getTipo());
  468.         busta.setDestinatario(idServizio.getSoggettoErogatore().getNome());
  469.         busta.setTipoServizio(idServizio.getTipo());
  470.         busta.setServizio(idServizio.getNome());
  471.         busta.setVersioneServizio(idServizio.getVersione());
  472.         busta.setAzione(idServizio.getAzione());
  473.         if(idBustaRisposta==null){
  474.             busta.setID(idBusta);
  475.         }
  476.         else{
  477.             busta.setID(idBustaRisposta);
  478.             busta.setRiferimentoMessaggio(idBusta);
  479.         }
  480.         integrazione.setBusta(busta);
  481.        
  482.         this.updateHeader(message, integrazione, actorIntegrazione);
  483.     }
  484.        
  485.     public void updateHeader(OpenSPCoop2SoapMessage message,HeaderIntegrazione integrazione,String actorIntegrazione) throws Exception{
  486.        
  487.         SOAPHeader header = message.getSOAPHeader();
  488.        
  489.         SOAPHeaderElement wsaTO = null;
  490.         SOAPHeaderElement wsaFROM = null;
  491.         SOAPHeaderElement wsaAction = null;
  492.         SOAPHeaderElement wsaID = null;
  493.         SOAPHeaderElement wsaRelatesTo = null;
  494.        
  495.         Logger log = OpenSPCoop2Logger.getLoggerOpenSPCoopCore();
  496.        
  497.         if(header==null){
  498.            
  499.             // Creo soap header
  500.             OpenSPCoop2Logger.getLoggerOpenSPCoopCore().debug("SOAPHeader non presente: add soapHeader");
  501.             header = message.getSOAPPart().getEnvelope().addHeader();
  502.            
  503.         }else{

  504.             // cerco soap di integrazione
  505.            
  506.             WSAddressingUtilities wsaddressingUtilities = new WSAddressingUtilities(log);
  507.             WSAddressingHeader hdrReaded = wsaddressingUtilities.read(message, actorIntegrazione, false);
  508.             if(hdrReaded!=null) {
  509.                 wsaTO = hdrReaded.getTo();
  510.                 wsaFROM = hdrReaded.getFrom();
  511.                 wsaAction = hdrReaded.getAction();
  512.                 wsaID = hdrReaded.getId();
  513.                 wsaRelatesTo = hdrReaded.getRelatesTo();
  514.             }
  515.            
  516.         }

  517.         if(integrazione.getBusta()!=null){
  518.            
  519.             HeaderIntegrazioneBusta hBusta = integrazione.getBusta();
  520.                
  521.             if(hBusta.getDestinatario()!=null && hBusta.getServizio()!=null){
  522.                
  523.                 // To
  524.                 if(wsaTO!=null){
  525.                     // aggiorno
  526.                     wsaTO.setValue(UtilitiesIntegrazioneWSAddressing.buildDatiWSATo(hBusta.getTipoDestinatario(), hBusta.getDestinatario(),
  527.                             hBusta.getTipoServizio() , hBusta.getServizio(), hBusta.getVersioneServizio()));
  528.                 }
  529.                 else{
  530.                     wsaTO = UtilitiesIntegrazioneWSAddressing.buildWSATo(message,actorIntegrazione,hBusta.getTipoDestinatario(), hBusta.getDestinatario(),
  531.                             hBusta.getTipoServizio() , hBusta.getServizio(), hBusta.getVersioneServizio());
  532.                     //header.addChildElement(wsaTO);
  533.                     message.addHeaderElement(header, wsaTO);
  534.                 }
  535.                
  536.                
  537.                 // Action
  538.                 if(hBusta.getAzione()!=null){
  539.                     if(wsaAction!=null){
  540.                         // aggiorno
  541.                         wsaAction.setValue(UtilitiesIntegrazioneWSAddressing.buildDatiWSAAction(hBusta.getTipoDestinatario(), hBusta.getDestinatario(),
  542.                                 hBusta.getTipoServizio() , hBusta.getServizio(), hBusta.getVersioneServizio(), hBusta.getAzione()));
  543.                     }
  544.                     else{
  545.                         wsaAction = UtilitiesIntegrazioneWSAddressing.buildWSAAction(message,actorIntegrazione,hBusta.getTipoDestinatario(), hBusta.getDestinatario(),
  546.                                 hBusta.getTipoServizio() , hBusta.getServizio(), hBusta.getVersioneServizio(), hBusta.getAzione());
  547.                         //header.addChildElement(wsaTO);
  548.                         message.addHeaderElement(header, wsaTO);
  549.                     }
  550.                 }
  551.             }
  552.            
  553.             if(hBusta.getMittente()!=null){
  554.                 if(wsaFROM!=null){
  555.                     // aggiorno
  556.                     Iterator<?> itFROM = wsaFROM.getChildElements();
  557.                     while (itFROM.hasNext()) {
  558.                         Object o = itFROM.next();
  559.                         if(o!=null && (o instanceof SOAPElement) ){
  560.                             SOAPElement s = (SOAPElement) o;
  561.                             if(Costanti.WSA_SOAP_HEADER_EPR_ADDRESS.equals(s.getLocalName())){
  562.                                 s.setValue(buildDatiWSAFrom(integrazione.getServizioApplicativo(),hBusta.getTipoMittente(),hBusta.getMittente()));
  563.                                 break;
  564.                             }
  565.                         }
  566.                     }
  567.                 }
  568.                 else{
  569.                     wsaFROM = UtilitiesIntegrazioneWSAddressing.buildWSAFrom(message,actorIntegrazione,integrazione.getServizioApplicativo(),hBusta.getTipoMittente(),hBusta.getMittente());
  570.                     //header.addChildElement(wsaFROM);
  571.                     message.addHeaderElement(header, wsaFROM);
  572.                 }
  573.             }
  574.                
  575.             if(hBusta.getID()!=null){
  576.                 if(wsaID!=null){
  577.                     // aggiorno
  578.                     wsaID.setValue(UtilitiesIntegrazioneWSAddressing.buildDatiWSAID(hBusta.getID()));
  579.                 }
  580.                 else{
  581.                     wsaID = UtilitiesIntegrazioneWSAddressing.buildWSAID(message,actorIntegrazione,hBusta.getID());
  582.                     //header.addChildElement(wsaID);
  583.                     message.addHeaderElement(header, wsaID);
  584.                 }
  585.             }
  586.            
  587.             if(hBusta.getRiferimentoMessaggio()!=null || hBusta.getIdCollaborazione()!=null){
  588.                 String rif = hBusta.getRiferimentoMessaggio();
  589.                 if(rif==null){
  590.                     rif = hBusta.getIdCollaborazione();
  591.                 }
  592.                 if(wsaRelatesTo!=null){
  593.                     // aggiorno
  594.                     wsaRelatesTo.setValue(UtilitiesIntegrazioneWSAddressing.buildDatiWSARelatesTo(rif));
  595.                 }
  596.                 else{
  597.                     wsaRelatesTo = UtilitiesIntegrazioneWSAddressing.buildWSARelatesTo(message,actorIntegrazione,rif);
  598.                     //header.addChildElement(wsaRelatesTo);
  599.                     message.addHeaderElement(header, wsaRelatesTo);
  600.                 }
  601.             }
  602.         }
  603.     }

  604.    
  605.     public void deleteHeader(OpenSPCoop2SoapMessage message,String actorIntegrazione) throws HeaderIntegrazioneException{

  606.         try{

  607.             if(actorIntegrazione==null)
  608.                 throw new Exception("Actor non definito");
  609.            
  610.             Logger log = OpenSPCoop2Logger.getLoggerOpenSPCoopCore();
  611.            
  612.             SOAPHeader header = message.getSOAPHeader();
  613.             if(header==null){
  614.                 OpenSPCoop2Logger.getLoggerOpenSPCoopCore().debug("SOAPHeader non presente");
  615.                 return;
  616.             }
  617.            
  618.             WSAddressingUtilities wsaddressingUtilities = new WSAddressingUtilities(log);
  619.             wsaddressingUtilities.delete(message, actorIntegrazione);
  620.        
  621.         }catch(Exception e){
  622.             throw new HeaderIntegrazioneException("UtilitiesIntegrazione, lettura dell'header soap non riuscita: "+e.getMessage(),e);
  623.         }
  624.     }
  625. }