ProtocolFactoryManager.java

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

  20. package org.openspcoop2.protocol.engine;

  21. import java.io.InputStream;
  22. import java.net.URL;
  23. import java.util.ArrayList;
  24. import java.util.Enumeration;
  25. import java.util.HashMap;
  26. import java.util.Iterator;
  27. import java.util.List;
  28. import java.util.Map;

  29. import javax.servlet.http.HttpServletRequest;

  30. import org.openspcoop2.message.OpenSPCoop2MessageFactory;
  31. import org.openspcoop2.protocol.basic.BasicFactory;
  32. import org.openspcoop2.protocol.engine.mapping.InformazioniServizioURLMapping;
  33. import org.openspcoop2.protocol.manifest.DefaultIntegrationError;
  34. import org.openspcoop2.protocol.manifest.Integration;
  35. import org.openspcoop2.protocol.manifest.IntegrationConfiguration;
  36. import org.openspcoop2.protocol.manifest.IntegrationConfigurationElementName;
  37. import org.openspcoop2.protocol.manifest.IntegrationConfigurationResourceIdentificationMode;
  38. import org.openspcoop2.protocol.manifest.IntegrationError;
  39. import org.openspcoop2.protocol.manifest.Openspcoop2;
  40. import org.openspcoop2.protocol.manifest.RestConfiguration;
  41. import org.openspcoop2.protocol.manifest.SoapConfiguration;
  42. import org.openspcoop2.protocol.manifest.SubContextMapping;
  43. import org.openspcoop2.protocol.manifest.Web;
  44. import org.openspcoop2.protocol.manifest.constants.ActorType;
  45. import org.openspcoop2.protocol.manifest.constants.Costanti;
  46. import org.openspcoop2.protocol.manifest.constants.FunctionType;
  47. import org.openspcoop2.protocol.manifest.constants.MessageType;
  48. import org.openspcoop2.protocol.manifest.constants.ResourceIdentificationType;
  49. import org.openspcoop2.protocol.manifest.constants.ServiceBinding;
  50. import org.openspcoop2.protocol.manifest.utils.XMLUtils;
  51. import org.openspcoop2.protocol.sdk.ConfigurazionePdD;
  52. import org.openspcoop2.protocol.sdk.IProtocolFactory;
  53. import org.openspcoop2.protocol.sdk.ProtocolException;
  54. import org.openspcoop2.protocol.sdk.config.ITraduttore;
  55. import org.openspcoop2.protocol.sdk.constants.CodiceErroreCooperazione;
  56. import org.openspcoop2.protocol.sdk.constants.ContestoCodificaEccezione;
  57. import org.openspcoop2.protocol.sdk.constants.IDService;
  58. import org.openspcoop2.protocol.sdk.constants.Inoltro;
  59. import org.openspcoop2.protocol.sdk.constants.LivelloRilevanza;
  60. import org.openspcoop2.protocol.sdk.constants.ProfiloDiCollaborazione;
  61. import org.openspcoop2.protocol.sdk.constants.TipoOraRegistrazione;
  62. import org.openspcoop2.protocol.sdk.state.FunctionContextsCustom;
  63. import org.openspcoop2.protocol.sdk.state.URLProtocolContext;
  64. import org.openspcoop2.protocol.utils.EsitiProperties;
  65. import org.openspcoop2.utils.Utilities;
  66. import org.openspcoop2.utils.resources.ClassLoaderUtilities;
  67. import org.openspcoop2.utils.resources.MapReader;
  68. import org.slf4j.Logger;

  69. /**
  70.  * Protocol Factory Manager
  71.  *
  72.  * @author Poli Andrea (apoli@link.it)
  73.  * @author $Author$
  74.  * @version $Rev$, $Date$
  75.  */


  76. public class ProtocolFactoryManager {

  77.     private static ProtocolFactoryManager protocolFactoryManager = null;
  78.     public static synchronized void initialize(Logger log,ConfigurazionePdD configPdD,String protocolDefault) throws ProtocolException {
  79.         if(ProtocolFactoryManager.protocolFactoryManager==null){
  80.             ProtocolFactoryManager.protocolFactoryManager = new ProtocolFactoryManager(log,configPdD,protocolDefault,false);
  81.             // Inizializzo anche Esiti.properties
  82.             EsitiProperties.initialize(configPdD.getConfigurationDir(), log, configPdD.getLoader(), protocolFactoryManager.getProtocolFactories());
  83.             // Inizializzo risorse statiche che avevano bisogno di altre informazioni (es. esiti properties)
  84.             protocolFactoryManager.initStaticInstance();
  85.         }
  86.     }
  87.     public static synchronized void initializeSingleProtocol(Logger log,ConfigurazionePdD configPdD,String protocol) throws ProtocolException {
  88.         if(ProtocolFactoryManager.protocolFactoryManager==null){
  89.             ProtocolFactoryManager.protocolFactoryManager = new ProtocolFactoryManager(log,configPdD,protocol,true);
  90.             // Inizializzo anche Esiti.properties
  91.             EsitiProperties.initialize(configPdD.getConfigurationDir(), log, configPdD.getLoader(), protocolFactoryManager.getProtocolFactories());
  92.             // Inizializzo risorse statiche che avevano bisogno di altre informazioni (es. esiti properties)
  93.             protocolFactoryManager.initStaticInstance();
  94.         }
  95.     }
  96.     public static ProtocolFactoryManager getInstance() throws ProtocolException {
  97.         if(ProtocolFactoryManager.protocolFactoryManager==null){
  98.             // spotbugs warning 'SING_SINGLETON_GETTER_NOT_SYNCHRONIZED': l'istanza viene creata allo startup
  99.             synchronized (ProtocolFactoryManager.class) {
  100.                 throw new ProtocolException("ProtocolFactoryManager not initialized");
  101.             }
  102.         }
  103.         return ProtocolFactoryManager.protocolFactoryManager;
  104.     }
  105.     public static void updateLogger(Logger log){
  106.         if(ProtocolFactoryManager.protocolFactoryManager!=null){
  107.             ProtocolFactoryManager.protocolFactoryManager.log = log;
  108.         }
  109.     }
  110.    
  111.     public boolean isSupportedProtocolLogger(String protocol) throws ProtocolException {
  112.         IProtocolFactory<?> p = this.getProtocolFactoryByName(protocol);
  113.         return p.getManifest().getProtocol().isLogger();
  114.     }
  115.    
  116.     public void initializeAllProtocols() throws ProtocolException{
  117.         /**System.out.println("Init All Factories ["+this.factories.size()+"] ...");*/
  118.         if(this.factories!=null){
  119.             Enumeration<IProtocolFactory<?>> factories = this.factories.elements();
  120.             while (factories.hasMoreElements()) {
  121.                 IProtocolFactory<?> iProtocolFactory = (IProtocolFactory<?>) factories.nextElement();
  122.                 /**System.out.println("Init ["+iProtocolFactory.getProtocol()+"] ...");*/
  123.                
  124.                 // INFO SERVIZIO
  125.                 iProtocolFactory.getProtocol();
  126.                 iProtocolFactory.getInformazioniProtocol();
  127.                 Openspcoop2 manifest = iProtocolFactory.getManifest();
  128.                 iProtocolFactory.getConfigurazionePdD();
  129.                
  130.                 // LOGGER
  131.                 iProtocolFactory.getLogger();
  132.                 if(manifest.getProtocol().isLogger()) {
  133.                     iProtocolFactory.getProtocolLogger();
  134.                 }
  135.                                
  136.                 // PROTOCOL BUILDER
  137.                 iProtocolFactory.createBustaBuilder(null);
  138.                 iProtocolFactory.createErroreApplicativoBuilder();
  139.                 iProtocolFactory.createEsitoBuilder();
  140.                
  141.                 // PROTOCOL VALIDATOR
  142.                 iProtocolFactory.createValidatoreErrori(null);
  143.                 iProtocolFactory.createValidazioneSintattica(null);
  144.                 iProtocolFactory.createValidazioneSemantica(null);
  145.                 iProtocolFactory.createValidazioneConSchema(null);
  146.                 iProtocolFactory.createValidazioneDocumenti();
  147.                 iProtocolFactory.createValidazioneAccordi();
  148.                
  149.                 // DIAGNOSTICI
  150.                 iProtocolFactory.createDiagnosticDriver();
  151.                 iProtocolFactory.createDiagnosticProducer();
  152.                 iProtocolFactory.createDiagnosticSerializer();
  153.                
  154.                 // TRACCE
  155.                 iProtocolFactory.createTracciaDriver();
  156.                 iProtocolFactory.createTracciaProducer();
  157.                 iProtocolFactory.createTracciaSerializer();
  158.                                
  159.                 // ARCHIVE
  160.                 iProtocolFactory.createArchive();
  161.                
  162.                 // CONFIG
  163.                 List<String> versioni = iProtocolFactory.createProtocolConfiguration().getVersioni();
  164.                 for (String version : versioni) {
  165.                     iProtocolFactory.createProtocolVersionManager(version);
  166.                 }
  167.                 iProtocolFactory.createProtocolManager();
  168.                 ITraduttore traduttore = iProtocolFactory.createTraduttore();
  169.                 traduttore.toString(CodiceErroreCooperazione.AZIONE);
  170.                 traduttore.toString(ContestoCodificaEccezione.INTESTAZIONE);
  171.                 traduttore.toString(Inoltro.CON_DUPLICATI);
  172.                 traduttore.toString(LivelloRilevanza.INFO);
  173.                 traduttore.toString(ProfiloDiCollaborazione.SINCRONO);
  174.                 traduttore.toString(TipoOraRegistrazione.LOCALE);
  175.                
  176.                 // CONSOLE
  177.                 iProtocolFactory.createDynamicConfigurationConsole();
  178.                
  179.                 //System.out.println("Init ["+iProtocolFactory.getProtocol()+"] ok");
  180.             }
  181.         }
  182.     }
  183.    
  184.    
  185.     private MapReader<String, Openspcoop2> manifests = null;
  186.     @SuppressWarnings("unused")
  187.     private MapReader<String, URL> manifestURLs = null;
  188.     private MapReader<String, IProtocolFactory<?>> factories = null;
  189.     private StringBuilder protocolLoaded = new StringBuilder();
  190.     private String protocolDefault = null;
  191.     private MapReader<String, List<String>> tipiSoggettiValidi = null;
  192.     private MapReader<String, String> tipiSoggettiDefault = null;
  193.     private MapReader<String, List<String>> tipiServiziValidi_soap = null;
  194.     private MapReader<String, String> tipiServiziDefault_soap = null;
  195.     private MapReader<String, List<String>> tipiServiziValidi_rest = null;
  196.     private MapReader<String, String> tipiServiziDefault_rest = null;
  197.     private MapReader<String, List<String>> versioniValide = null;
  198.     private MapReader<String, String> versioniDefault = null;
  199.     private Logger log = null;
  200.     private ProtocolFactoryManager(Logger log,ConfigurazionePdD configPdD,String protocolDefault, boolean searchSingleManifest) throws ProtocolException {
  201.         try {

  202.             Map<String, Openspcoop2> tmp_manifests = new HashMap<>();
  203.             Map<String, URL> tmp_manifestURLs = new HashMap<>();
  204.             Map<String, IProtocolFactory<?>> tmp_factories = new HashMap<>();
  205.            
  206.             Map<String, List<String>> tmp_tipiSoggettiValidi = new HashMap<>();
  207.             Map<String, String> tmp_tipiSoggettiDefault = new HashMap<>();
  208.            
  209.             Map<String, List<String>> tmp_tipiServiziValidi_soap = new HashMap<>();
  210.             Map<String, String> tmp_tipiServiziDefault_soap = new HashMap<>();
  211.             Map<String, List<String>> tmp_tipiServiziValidi_rest = new HashMap<>();
  212.             Map<String, String> tmp_tipiServiziDefault_rest = new HashMap<>();
  213.            
  214.             Map<String, List<String>> tmp_versioniValide = new HashMap<>();
  215.             Map<String, String> tmp_versioniDefault = new HashMap<>();
  216.            
  217.             this.log = configPdD.getLog();
  218.             this.protocolDefault = protocolDefault;
  219.            
  220.             configPdD.getLog().debug("Init ProtocolFactoryManager ...");
  221.            
  222.             // Loaded Manifest
  223.             if(searchSingleManifest){
  224.                 // Quando si recuper il getClassLoader, nei command line, non viene tornato lo stesso loader delle classi per motivi di sicurezza.
  225.                 // Vedi API del metodo getClassLoader()
  226.                 URL pluginURL = ProtocolFactoryManager.class.getResource("/"+Costanti.MANIFEST_OPENSPCOOP2); // utile nei command line.
  227.                 loadManifest(configPdD, pluginURL, false, tmp_manifests, tmp_manifestURLs);
  228.             }
  229.             else{
  230.                 // 1. Cerco nel classloader (funziona per jboss5.x)
  231.                 Enumeration<URL> en = ProtocolFactoryManager.class.getClassLoader().getResources("/"+Costanti.MANIFEST_OPENSPCOOP2);
  232.                 while(en.hasMoreElements()){
  233.                     URL pluginURL = en.nextElement();
  234.                     loadManifest(configPdD, pluginURL, false, tmp_manifests, tmp_manifestURLs);
  235.                 }
  236.                
  237.                 if(tmp_manifests.size()<=0){
  238.                     // 2. (funziona per jboss4.x) ma vengono forniti jar duplicati, quelli dentro ear e quelli dentro tmp.
  239.                     en = ProtocolFactoryManager.class.getClassLoader().getResources(Costanti.MANIFEST_OPENSPCOOP2);
  240.                     while(en.hasMoreElements()){
  241.                         URL pluginURL = en.nextElement();
  242.                         loadManifest(configPdD, pluginURL, true, tmp_manifests, tmp_manifestURLs);
  243.                     }
  244.                 }
  245.             }
  246.            
  247.             if(tmp_manifests.size()<=0){
  248.                 throw new Exception("Protocol plugins not found");
  249.             }
  250.            
  251.             // Validate Manifest Loaded
  252.             configPdD.getLog().debug("Validate Manifests ...");
  253.             validateProtocolFactoryLoaded(tmp_manifests,
  254.                     tmp_tipiSoggettiValidi,tmp_tipiSoggettiDefault,
  255.                     tmp_tipiServiziValidi_soap, tmp_tipiServiziDefault_soap,
  256.                     tmp_tipiServiziValidi_rest, tmp_tipiServiziDefault_rest,
  257.                     tmp_versioniValide, tmp_versioniDefault);
  258.             configPdD.getLog().debug("Validate Manifests ok");
  259.            
  260.             // Init protocol factory
  261.             for (String protocolManifest : tmp_manifests.keySet()) {

  262.                 configPdD.getLog().debug("Init ProtocolFactory for protocol ["+protocolManifest+"] ...");
  263.                 Openspcoop2 manifestOpenspcoop2 = tmp_manifests.get(protocolManifest);              
  264.                
  265.                 // Factory
  266.                 IProtocolFactory<?> p = this.getProtocolFactoryEngine(manifestOpenspcoop2);
  267.                 p.init(configPdD.getLog(), protocolManifest, configPdD,manifestOpenspcoop2);
  268.                 if(!p.createValidazioneConSchema(null).initialize(OpenSPCoop2MessageFactory.getDefaultMessageFactory())){
  269.                     throw new Exception("[protocol:"+protocolManifest+"] Inizialize with error for ValidazioneConSchema");
  270.                 }
  271.                 tmp_factories.put(protocolManifest, p);
  272.                
  273.                 // Lista di protocolli caricati
  274.                 if(this.protocolLoaded.length()>0){
  275.                     this.protocolLoaded.append(",");
  276.                 }
  277.                 this.protocolLoaded.append(protocolManifest);
  278.                
  279.                 // Carico url-mapping
  280.                 InformazioniServizioURLMapping.initMappingProperties(p);
  281.                
  282.                 // Info di debug
  283.                 StringBuilder context = new StringBuilder();
  284.                 if(manifestOpenspcoop2.getWeb().getEmptyContext()!=null && manifestOpenspcoop2.getWeb().getEmptyContext().getEnabled()){
  285.                     context.append("@EMPTY-CONTEXT@");
  286.                 }
  287.                 for (int i = 0; i < manifestOpenspcoop2.getWeb().sizeContextList(); i++) {
  288.                     if(context.length()>0){
  289.                         context.append(",");
  290.                     }
  291.                     context.append(manifestOpenspcoop2.getWeb().getContext(i).getName());
  292.                 }
  293.                 log.info("Protocol loaded with id["+protocolManifest+"] factory["+manifestOpenspcoop2.getProtocol().getFactory()+"] contexts["+context.toString()+"]");
  294.                
  295.                 configPdD.getLog().debug("Init ProtocolFactory for protocol ["+protocolManifest+"] ok");
  296.             }
  297.            
  298.             // init
  299.             this.manifests = new MapReader<>(tmp_manifests,true);
  300.             this.manifestURLs = new MapReader<>(tmp_manifestURLs,true);
  301.             this.factories = new MapReader<>(tmp_factories,true);
  302.            
  303.             this.tipiSoggettiValidi = new MapReader<>(tmp_tipiSoggettiValidi,true);
  304.             this.tipiSoggettiDefault = new MapReader<>(tmp_tipiSoggettiDefault,true);
  305.            
  306.             this.tipiServiziValidi_soap = new MapReader<>(tmp_tipiServiziValidi_soap,true);
  307.             this.tipiServiziDefault_soap = new MapReader<>(tmp_tipiServiziDefault_soap,true);
  308.            
  309.             this.tipiServiziValidi_rest = new MapReader<>(tmp_tipiServiziValidi_rest,true);
  310.             this.tipiServiziDefault_rest = new MapReader<>(tmp_tipiServiziDefault_rest,true);
  311.            
  312.             this.versioniValide = new MapReader<>(tmp_versioniValide,true);
  313.             this.versioniDefault = new MapReader<>(tmp_versioniDefault,true);
  314.            
  315.         } catch (Exception e) {
  316.             configPdD.getLog().error("Init ProtocolFactoryManager failed: "+e.getMessage(),e);
  317.             throw new ProtocolException("Inizializzazione ProtocolFactoryManager fallita: " + e, e);
  318.         }
  319.     }
  320.    
  321.     private void loadManifest(ConfigurazionePdD configPdD,URL pluginURL,boolean filtraSenzaErroreProtocolloGiaCaricato,
  322.             Map<String, Openspcoop2> tmp_manifests, Map<String, URL> tmp_manifestURLs) throws Exception{
  323.         // Manifest
  324.         configPdD.getLog().debug("Analyze manifest ["+pluginURL.toString()+"] ...");
  325.        
  326.         InputStream openStream = null;
  327.         byte[] manifest = null;
  328.         try{
  329.             openStream = pluginURL.openStream();
  330.             manifest = Utilities.getAsByteArray(openStream);
  331.         }finally{
  332.             try{
  333.                 if(openStream!=null) {
  334.                     openStream.close();
  335.                 }
  336.             }catch(Exception e){
  337.                 // close
  338.             }
  339.         }
  340.         //System.out.println("CARICATO ["+new String(manifest)+"]");

  341.         configPdD.getLog().debug("Analyze manifest ["+pluginURL.toString()+"] convertToOpenSPCoop2Manifest...");
  342.         Openspcoop2 manifestOpenspcoop2 = XMLUtils.getOpenspcoop2Manifest(configPdD.getLog(),manifest);
  343.         String protocollo = manifestOpenspcoop2.getProtocol().getName();
  344.         configPdD.getLog().debug("Analyze manifest ["+pluginURL.toString()+"] with protocolName: ["+protocollo+"]");
  345.         if(tmp_manifests.containsKey(protocollo)){
  346.        
  347.             URL urlGiaPresente = tmp_manifestURLs.get(protocollo);
  348.             if(filtraSenzaErroreProtocolloGiaCaricato){
  349.                 configPdD.getLog().warn("ProtocolName ["+protocollo+"] is same for more plugin ["+pluginURL.toString()+"] and ["+urlGiaPresente.toURI()+"]");
  350.             }
  351.             else{
  352.                 throw new Exception("ProtocolName ["+protocollo+"] is same for more plugin ["+pluginURL.toString()+"] and ["+urlGiaPresente.toURI()+"]");
  353.             }
  354.            
  355.         }
  356.         tmp_manifests.put(protocollo, manifestOpenspcoop2);
  357.         tmp_manifestURLs.put(protocollo, pluginURL);
  358.         configPdD.getLog().debug("Analyze manifest ["+pluginURL.toString()+"] with success");
  359.     }
  360.    
  361.     private void validateProtocolFactoryLoaded(Map<String, Openspcoop2> tmp_manifests,
  362.             Map<String, List<String>> tmp_tipiSoggettiValidi,Map<String, String> tmp_tipiSoggettiDefault,
  363.             Map<String, List<String>> tmp_tipiServiziValidi_soap, Map<String, String> tmp_tipiServiziDefault_soap,
  364.             Map<String, List<String>> tmp_tipiServiziValidi_rest, Map<String, String> tmp_tipiServiziDefault_rest,
  365.             Map<String, List<String>> tmp_versioniValide,Map<String, String> tmp_versioniDefault) throws Exception{
  366.                
  367.         // 1. controllare che solo uno possieda il contesto vuoto
  368.         String protocolContextEmpty = null;
  369.         for (String protocolManifest : tmp_manifests.keySet()) {
  370.             Openspcoop2 manifestOpenspcoop2 = tmp_manifests.get(protocolManifest);
  371.             if(manifestOpenspcoop2.getWeb().getEmptyContext()!=null && manifestOpenspcoop2.getWeb().getEmptyContext().getEnabled()){
  372.                 if(protocolContextEmpty==null){
  373.                     protocolContextEmpty = protocolManifest;
  374.                 }
  375.                 else{
  376.                     throw new Exception("Protocol ["+protocolContextEmpty+"] and ["+protocolManifest+"] with empty context. Only one is permitted");
  377.                 }
  378.             }
  379.            
  380.         }
  381.        
  382.         // 2. controllare che i contesti siano tutti diversi
  383.         Map<String, String> mappingContextToProtocol = new HashMap<>();
  384.         for (String protocolManifest : tmp_manifests.keySet()) {
  385.             Openspcoop2 manifestOpenspcoop2 = tmp_manifests.get(protocolManifest);
  386.            
  387.             for (int i = 0; i < manifestOpenspcoop2.getWeb().sizeContextList(); i++) {
  388.                 String context = manifestOpenspcoop2.getWeb().getContext(i).getName();
  389.                 if(!mappingContextToProtocol.containsKey(context)){
  390.                     mappingContextToProtocol.put(context, protocolManifest);
  391.                 }
  392.                 else{
  393.                     throw new Exception("Protocol ["+mappingContextToProtocol.get(context)+"] and ["+protocolManifest+"] with same context ["+context+"]");
  394.                 }
  395.             }
  396.            
  397.         }
  398.        
  399.         // 2.b. controllare che le label siano tutti diversi
  400.         Map<String, String> mappinLabelToProtocol = new HashMap<>();
  401.         for (String protocolManifest : tmp_manifests.keySet()) {
  402.             Openspcoop2 manifestOpenspcoop2 = tmp_manifests.get(protocolManifest);
  403.            
  404.             String label = manifestOpenspcoop2.getProtocol().getLabel();
  405.             if(!mappinLabelToProtocol.containsKey(label)){
  406.                 mappinLabelToProtocol.put(label, protocolManifest);
  407.             }
  408.             else{
  409.                 throw new Exception("Protocol ["+mappinLabelToProtocol.get(label)+"] and ["+protocolManifest+"] with same label ["+label+"]");
  410.             }
  411.            
  412.         }
  413.        
  414.         // 3. controllare e inizializzare i tipi di soggetti in modo che siano tutti diversi
  415.         Map<String, String> mappingTipiSoggettiToProtocol = new HashMap<>();
  416.         for (String protocolManifest : tmp_manifests.keySet()) {
  417.             Openspcoop2 manifestOpenspcoop2 = tmp_manifests.get(protocolManifest);
  418.            
  419.             int size = manifestOpenspcoop2.getRegistry().getOrganization().getTypes().sizeTypeList();
  420.             if(size<=0){
  421.                 throw new Exception("Organization type not defined for protocol ["+protocolManifest+"]");
  422.             }
  423.                        
  424.             for (int i = 0; i < size; i++) {
  425.                 String tipo = manifestOpenspcoop2.getRegistry().getOrganization().getTypes().getType(i).getName();
  426.                 if(!mappingTipiSoggettiToProtocol.containsKey(tipo)){
  427.                     mappingTipiSoggettiToProtocol.put(tipo, protocolManifest);
  428.                    
  429.                     List<String> tipiSoggettiPerProtocollo = null;
  430.                     if(tmp_tipiSoggettiValidi.containsKey(protocolManifest)){
  431.                         tipiSoggettiPerProtocollo = tmp_tipiSoggettiValidi.remove(protocolManifest);
  432.                     }
  433.                     else{
  434.                         tipiSoggettiPerProtocollo = new ArrayList<>();
  435.                     }
  436.                     tipiSoggettiPerProtocollo.add(tipo);
  437.                     tmp_tipiSoggettiValidi.put(protocolManifest, tipiSoggettiPerProtocollo);
  438.                 }
  439.                 else{
  440.                     throw new Exception("Protocol ["+mappingTipiSoggettiToProtocol.get(tipo)+"] and ["+protocolManifest+"] with same subject type ["+tipo+"]");
  441.                 }
  442.             }
  443.            
  444.             for (int i = 0; i < size; i++) {
  445.                 String protocolType = manifestOpenspcoop2.getRegistry().getOrganization().getTypes().getType(i).getProtocol();
  446.                 if(protocolType!=null) {
  447.                     int count = 0;
  448.                     for (int j = 0; j < size; j++) {
  449.                         String protocolTypeCheck = manifestOpenspcoop2.getRegistry().getOrganization().getTypes().getType(j).getProtocol();
  450.                         if(protocolTypeCheck!=null) {
  451.                             if(protocolTypeCheck.equals(protocolType)) {
  452.                                 count++;
  453.                             }
  454.                         }
  455.                     }
  456.                     if(count>1) {
  457.                         throw new Exception("Protocol ["+protocolManifest+"] with same subject 'protocol conversion type' ["+protocolType+"]");
  458.                     }
  459.                 }
  460.             }
  461.            
  462.             String tipoDefault = manifestOpenspcoop2.getRegistry().getOrganization().getTypes().getType(0).getName();
  463.             if(tmp_tipiSoggettiValidi.get(protocolManifest).contains(tipoDefault)==false){
  464.                 throw new Exception("Unknown default subject type ["+tipoDefault+"] defined in protocol ["+protocolManifest+"]");
  465.             }
  466.             tmp_tipiSoggettiDefault.put(protocolManifest, tipoDefault);
  467.            
  468.         }
  469.        
  470.        
  471.         // 4. controllare e inizializzare i tipi di servizi in modo che siano tutti diversi
  472.         String tipoServizioDefaultSoap = null;
  473.         String tipoServizioDefaultRest = null;
  474.         Map<String, String> mappingTipiServiziToProtocol = new HashMap<>();
  475.         for (String protocolManifest : tmp_manifests.keySet()) {
  476.             Openspcoop2 manifestOpenspcoop2 = tmp_manifests.get(protocolManifest);
  477.            
  478.             int size = manifestOpenspcoop2.getRegistry().getService().getTypes().sizeTypeList();
  479.             if(size<=0){
  480.                 throw new Exception("Service type not defined for protocol ["+protocolManifest+"]");
  481.             }
  482.            
  483.             for (int i = 0; i < size; i++) {
  484.                 String tipo = manifestOpenspcoop2.getRegistry().getService().getTypes().getType(i).getName();
  485.                 ServiceBinding serviceBinding = manifestOpenspcoop2.getRegistry().getService().getTypes().getType(i).getBinding();
  486.                 if(!mappingTipiServiziToProtocol.containsKey(tipo)){
  487.                     mappingTipiServiziToProtocol.put(tipo, protocolManifest);
  488.                    
  489.                     List<String> tipiServiziPerProtocollo_soap = null;
  490.                     if(tmp_tipiServiziValidi_soap.containsKey(protocolManifest)){
  491.                         tipiServiziPerProtocollo_soap = tmp_tipiServiziValidi_soap.get(protocolManifest);
  492.                     }
  493.                     else{
  494.                         tipiServiziPerProtocollo_soap = new ArrayList<>();
  495.                         tmp_tipiServiziValidi_soap.put(protocolManifest, tipiServiziPerProtocollo_soap);
  496.                     }
  497.                    
  498.                     List<String> tipiServiziPerProtocollo_rest = null;
  499.                     if(tmp_tipiServiziValidi_rest.containsKey(protocolManifest)){
  500.                         tipiServiziPerProtocollo_rest = tmp_tipiServiziValidi_rest.get(protocolManifest);
  501.                     }
  502.                     else{
  503.                         tipiServiziPerProtocollo_rest = new ArrayList<>();
  504.                         tmp_tipiServiziValidi_rest.put(protocolManifest, tipiServiziPerProtocollo_rest);
  505.                     }
  506.                    
  507.                     if(serviceBinding==null || ServiceBinding.SOAP.equals(serviceBinding)){
  508.                         tipiServiziPerProtocollo_soap.add(tipo);
  509.                         if(tipoServizioDefaultSoap==null){
  510.                             tipoServizioDefaultSoap = tipo;
  511.                         }
  512.                     }
  513.                    
  514.                     if(serviceBinding==null || ServiceBinding.REST.equals(serviceBinding)){
  515.                         tipiServiziPerProtocollo_rest.add(tipo);
  516.                         if(tipoServizioDefaultRest==null){
  517.                             tipoServizioDefaultRest = tipo;
  518.                         }
  519.                     }
  520.                 }
  521.                 else{
  522.                     throw new Exception("Protocol ["+mappingTipiServiziToProtocol.get(tipo)+"] and ["+protocolManifest+"] with same service type ["+tipo+"]");
  523.                 }
  524.             }
  525.            
  526.             for (int i = 0; i < size; i++) {
  527.                 String protocolType = manifestOpenspcoop2.getRegistry().getService().getTypes().getType(i).getProtocol();
  528.                 if(protocolType!=null) {
  529.                     int count = 0;
  530.                     for (int j = 0; j < size; j++) {
  531.                         String protocolTypeCheck = manifestOpenspcoop2.getRegistry().getService().getTypes().getType(j).getProtocol();
  532.                         if(protocolTypeCheck!=null) {
  533.                             if(protocolTypeCheck.equals(protocolType)) {
  534.                                 count++;
  535.                             }
  536.                         }
  537.                     }
  538.                     if(count>1) {
  539.                         throw new Exception("Protocol ["+protocolManifest+"] with same service 'protocol conversion type' ["+protocolType+"]");
  540.                     }
  541.                 }
  542.             }
  543.            
  544.             if(tipoServizioDefaultSoap!=null){
  545.                 tmp_tipiServiziDefault_soap.put(protocolManifest, tipoServizioDefaultSoap);
  546.             }
  547.             if(tipoServizioDefaultRest!=null){
  548.                 tmp_tipiServiziDefault_rest.put(protocolManifest, tipoServizioDefaultRest);
  549.             }
  550.            
  551.         }
  552.        
  553.        
  554.        
  555.         // 5. controllare e inizializzare le versioni dei protocolli
  556.         for (String protocolManifest : tmp_manifests.keySet()) {
  557.             Openspcoop2 manifestOpenspcoop2 = tmp_manifests.get(protocolManifest);
  558.            
  559.             int size = manifestOpenspcoop2.getRegistry().getVersions().sizeVersionList();
  560.             if(size<=0){
  561.                 throw new Exception("Version not defined for protocol ["+protocolManifest+"]");
  562.             }
  563.            
  564.             for (int i = 0; i < size; i++) {
  565.                 String version = manifestOpenspcoop2.getRegistry().getVersions().getVersion(i).getName();
  566.                
  567.                 List<String> versioniPerProtocollo = null;
  568.                 if(tmp_versioniValide.containsKey(protocolManifest)){
  569.                     versioniPerProtocollo = tmp_versioniValide.remove(protocolManifest);
  570.                 }
  571.                 else{
  572.                     versioniPerProtocollo = new ArrayList<>();
  573.                 }
  574.                 versioniPerProtocollo.add(version);
  575.                 tmp_versioniValide.put(protocolManifest, versioniPerProtocollo);

  576.             }
  577.            
  578.             String versioneDefault = manifestOpenspcoop2.getRegistry().getVersions().getVersion(0).getName();
  579.             if(tmp_versioniValide.get(protocolManifest).contains(versioneDefault)==false){
  580.                 throw new Exception("Unknown default version ["+versioneDefault+"] defined in protocol ["+protocolManifest+"]");
  581.             }
  582.             tmp_versioniDefault.put(protocolManifest, versioneDefault);
  583.         }
  584.        
  585.        
  586.        
  587.         // 6. controllare e inizializzare i service binding di default
  588.         for (String protocolManifest : tmp_manifests.keySet()) {
  589.             Openspcoop2 manifestOpenspcoop2 = tmp_manifests.get(protocolManifest);
  590.            
  591.             if(manifestOpenspcoop2.getBinding()==null){
  592.                 throw new Exception("Binding not defined for protocol ["+protocolManifest+"]");
  593.             }
  594.             if(manifestOpenspcoop2.getBinding().getSoap()==null && manifestOpenspcoop2.getBinding().getRest()==null){
  595.                 throw new Exception("Binding (soap/rest) not defined for protocol ["+protocolManifest+"]");
  596.             }
  597.             if(manifestOpenspcoop2.getBinding().getSoap()!=null && manifestOpenspcoop2.getBinding().getRest()!=null){
  598.                 if(manifestOpenspcoop2.getBinding().getDefault()==null)
  599.                     throw new Exception("Unknown default binding for protocol ["+protocolManifest+"]");
  600.             }
  601.            
  602.             boolean soapEnabled = false;
  603.             if(manifestOpenspcoop2.getBinding().getSoap()!=null){
  604.                 if(!manifestOpenspcoop2.getBinding().getSoap().isSoap11() && !manifestOpenspcoop2.getBinding().getSoap().isSoap12()){
  605.                     throw new Exception("Binding Soap for protocol ["+protocolManifest+"]: none of the soap versions is enabled");
  606.                 }
  607.                 if(manifestOpenspcoop2.getBinding().getSoap().isSoap11Mtom() && !manifestOpenspcoop2.getBinding().getSoap().isSoap11()){
  608.                     throw new Exception("Binding Soap for protocol ["+protocolManifest+"]: if soap 1.1 is not enabled you can not enable MTOM");
  609.                 }
  610.                 if(manifestOpenspcoop2.getBinding().getSoap().isSoap11WithAttachments() && !manifestOpenspcoop2.getBinding().getSoap().isSoap11()){
  611.                     throw new Exception("Binding Soap for protocol ["+protocolManifest+"]: if soap 1.1 is not enabled you can not enable attachments");
  612.                 }
  613.                 if(manifestOpenspcoop2.getBinding().getSoap().isSoap12Mtom() && !manifestOpenspcoop2.getBinding().getSoap().isSoap12()){
  614.                     throw new Exception("Binding Soap for protocol ["+protocolManifest+"]: if soap 1.2 is not enabled you can not enable MTOM");
  615.                 }
  616.                 if(manifestOpenspcoop2.getBinding().getSoap().isSoap12WithAttachments() && !manifestOpenspcoop2.getBinding().getSoap().isSoap12()){
  617.                     throw new Exception("Binding Soap for protocol ["+protocolManifest+"]: if soap 1.2 is not enabled you can not enable attachments");
  618.                 }
  619.                
  620.                 if(manifestOpenspcoop2.getBinding().getSoap().getIntegrationError()==null){
  621.                     throw new Exception("Binding Soap for protocol ["+protocolManifest+"]: it has not defined the configuration for error handling");
  622.                 }
  623.                 if(manifestOpenspcoop2.getBinding().getSoap().getIntegrationError().getInternal()==null){
  624.                     throw new Exception("Binding Soap for protocol ["+protocolManifest+"]: it has not defined the configuration for internal error handling");
  625.                 }
  626.                 if(manifestOpenspcoop2.getBinding().getSoap().getIntegrationError().getInternal().getDefault()==null){
  627.                     throw new Exception("Binding Soap for protocol ["+protocolManifest+"]: it has not defined the default configuration for internal error handling");
  628.                 }
  629.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getSoap().getIntegrationError().getInternal().getDefault(),"default");
  630.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getSoap().getIntegrationError().getInternal().getAuthentication(),"authentication");
  631.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getSoap().getIntegrationError().getInternal().getAuthorization(),"authorization");
  632.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getSoap().getIntegrationError().getInternal().getNotFound(),"notFound");
  633.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getSoap().getIntegrationError().getInternal().getBadRequest(),"badRequest");
  634.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getSoap().getIntegrationError().getInternal().getConflict(),"conflict");
  635.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getSoap().getIntegrationError().getInternal().getLimitExceeded(),"limitExceeded");
  636.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getSoap().getIntegrationError().getInternal().getTooManyRequests(),"tooManyRequests");
  637.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getSoap().getIntegrationError().getInternal().getServiceUnavailable(),"serviceUnavailable");
  638.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getSoap().getIntegrationError().getInternal().getEndpointRequestTimedOut(),"endpointRequestTimedOut");
  639.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getSoap().getIntegrationError().getInternal().getBadResponse(),"badResponse");
  640.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getSoap().getIntegrationError().getInternal().getInternalRequestError(),"internalRequestError");
  641.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getSoap().getIntegrationError().getInternal().getInternalResponseError(),"internalResponseError");
  642.                 if(manifestOpenspcoop2.getBinding().getSoap().getIntegrationError().getExternal()==null){
  643.                     throw new Exception("Binding Soap for protocol ["+protocolManifest+"]: it has not defined the configuration for external error handling");
  644.                 }
  645.                 if(manifestOpenspcoop2.getBinding().getSoap().getIntegrationError().getExternal().getDefault()==null){
  646.                     throw new Exception("Binding Soap for protocol ["+protocolManifest+"]: it has not defined the default configuration for external error handling");
  647.                 }
  648.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getSoap().getIntegrationError().getExternal().getDefault(),"default");
  649.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getSoap().getIntegrationError().getExternal().getAuthentication(),"authentication");
  650.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getSoap().getIntegrationError().getExternal().getAuthorization(),"authorization");
  651.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getSoap().getIntegrationError().getExternal().getNotFound(),"notFound");
  652.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getSoap().getIntegrationError().getExternal().getBadRequest(),"badRequest");
  653.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getSoap().getIntegrationError().getExternal().getConflict(),"conflict");
  654.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getSoap().getIntegrationError().getExternal().getLimitExceeded(),"limitExceeded");
  655.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getSoap().getIntegrationError().getExternal().getTooManyRequests(),"tooManyRequests");
  656.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getSoap().getIntegrationError().getExternal().getServiceUnavailable(),"serviceUnavailable");
  657.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getSoap().getIntegrationError().getExternal().getEndpointRequestTimedOut(),"endpointRequestTimedOut");
  658.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getSoap().getIntegrationError().getExternal().getBadResponse(),"badResponse");
  659.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getSoap().getIntegrationError().getExternal().getInternalRequestError(),"internalRequestError");
  660.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getSoap().getIntegrationError().getExternal().getInternalResponseError(),"internalResponseError");
  661.                
  662.                 if(manifestOpenspcoop2.getBinding().getSoap().getMediaTypeCollection()==null){
  663.                     throw new Exception("Binding Soap for protocol ["+protocolManifest+"]: media type collection undefined");
  664.                 }
  665.                 if(manifestOpenspcoop2.getBinding().getSoap().getMediaTypeCollection().sizeMediaTypeList()<=0 &&
  666.                         manifestOpenspcoop2.getBinding().getSoap().getMediaTypeCollection().getDefault()==null &&
  667.                                 manifestOpenspcoop2.getBinding().getSoap().getMediaTypeCollection().getUndefined()==null){
  668.                     throw new Exception("Binding Soap for protocol ["+protocolManifest+"]: media type collection undefined");
  669.                 }
  670.                
  671.                 soapEnabled = true;
  672.             }
  673.            
  674.             boolean restEnabled = false;
  675.             if(manifestOpenspcoop2.getBinding().getRest()!=null){
  676.                 if(!manifestOpenspcoop2.getBinding().getRest().isBinary() &&
  677.                         !manifestOpenspcoop2.getBinding().getRest().isJson() &&
  678.                         !manifestOpenspcoop2.getBinding().getRest().isXml() &&
  679.                         !manifestOpenspcoop2.getBinding().getRest().isMimeMultipart()){
  680.                     throw new Exception("Binding Rest for protocol ["+protocolManifest+"]: none of the message types is enabled");
  681.                 }
  682.                
  683.                 if(manifestOpenspcoop2.getBinding().getRest().getIntegrationError()==null){
  684.                     throw new Exception("Binding Rest for protocol ["+protocolManifest+"]: it has not defined the configuration for error handling");
  685.                 }
  686.                 if(manifestOpenspcoop2.getBinding().getRest().getIntegrationError().getInternal()==null){
  687.                     throw new Exception("Binding Rest for protocol ["+protocolManifest+"]: it has not defined the configuration for internal error handling");
  688.                 }
  689.                 if(manifestOpenspcoop2.getBinding().getRest().getIntegrationError().getInternal().getDefault()==null){
  690.                     throw new Exception("Binding Rest for protocol ["+protocolManifest+"]: it has not defined the default configuration for internal error handling");
  691.                 }
  692.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getRest().getIntegrationError().getInternal().getDefault(),"default");
  693.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getRest().getIntegrationError().getInternal().getAuthentication(),"authentication");
  694.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getRest().getIntegrationError().getInternal().getAuthorization(),"authorization");
  695.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getRest().getIntegrationError().getInternal().getNotFound(),"notFound");
  696.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getRest().getIntegrationError().getInternal().getBadRequest(),"badRequest");
  697.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getRest().getIntegrationError().getInternal().getConflict(),"conflict");
  698.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getRest().getIntegrationError().getInternal().getLimitExceeded(),"limitExceeded");
  699.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getRest().getIntegrationError().getInternal().getTooManyRequests(),"tooManyRequests");
  700.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getRest().getIntegrationError().getInternal().getServiceUnavailable(),"serviceUnavailable");
  701.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getRest().getIntegrationError().getInternal().getEndpointRequestTimedOut(),"endpointRequestTimedOut");
  702.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getRest().getIntegrationError().getInternal().getBadResponse(),"badResponse");
  703.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getRest().getIntegrationError().getInternal().getInternalRequestError(),"internalRequestError");
  704.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getRest().getIntegrationError().getInternal().getInternalResponseError(),"internalResponseError");
  705.                 if(manifestOpenspcoop2.getBinding().getRest().getIntegrationError().getExternal()==null){
  706.                     throw new Exception("Binding Rest for protocol ["+protocolManifest+"]: it has not defined the configuration for external error handling");
  707.                 }
  708.                 if(manifestOpenspcoop2.getBinding().getRest().getIntegrationError().getExternal().getDefault()==null){
  709.                     throw new Exception("Binding Rest for protocol ["+protocolManifest+"]: it has not defined the default configuration for external error handling");
  710.                 }
  711.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getRest().getIntegrationError().getExternal().getDefault(),"default");
  712.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getRest().getIntegrationError().getExternal().getAuthentication(),"authentication");
  713.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getRest().getIntegrationError().getExternal().getAuthorization(),"authorization");
  714.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getRest().getIntegrationError().getExternal().getNotFound(),"notFound");
  715.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getRest().getIntegrationError().getExternal().getBadRequest(),"badRequest");
  716.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getRest().getIntegrationError().getExternal().getConflict(),"conflict");
  717.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getRest().getIntegrationError().getExternal().getLimitExceeded(),"limitExceeded");
  718.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getRest().getIntegrationError().getExternal().getTooManyRequests(),"tooManyRequests");
  719.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getRest().getIntegrationError().getExternal().getServiceUnavailable(),"serviceUnavailable");
  720.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getRest().getIntegrationError().getExternal().getEndpointRequestTimedOut(),"endpointRequestTimedOut");
  721.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getRest().getIntegrationError().getExternal().getBadResponse(),"badResponse");
  722.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getRest().getIntegrationError().getExternal().getInternalRequestError(),"internalRequestError");
  723.                 checkHttpReturnCode(manifestOpenspcoop2.getBinding().getRest().getIntegrationError().getExternal().getInternalResponseError(),"internalResponseError");
  724.                
  725.                 if(manifestOpenspcoop2.getBinding().getRest().getMediaTypeCollection()==null){
  726.                     throw new Exception("Binding Rest for protocol ["+protocolManifest+"]: media type collection undefined");
  727.                 }
  728.                 if(manifestOpenspcoop2.getBinding().getRest().getMediaTypeCollection().sizeMediaTypeList()<=0 &&
  729.                         manifestOpenspcoop2.getBinding().getRest().getMediaTypeCollection().getDefault()==null &&
  730.                                 manifestOpenspcoop2.getBinding().getRest().getMediaTypeCollection().getUndefined()==null){
  731.                     throw new Exception("Binding Rest for protocol ["+protocolManifest+"]: media type collection undefined");
  732.                 }
  733.                
  734.                 restEnabled = true;
  735.             }
  736.            
  737.             for (int i = 0; i < manifestOpenspcoop2.getWeb().sizeContextList(); i++) {
  738.                 String context = manifestOpenspcoop2.getWeb().getContext(i).getName();
  739.                 ServiceBinding serviceBinding = manifestOpenspcoop2.getWeb().getContext(i).getBinding();
  740.                 if(serviceBinding!=null){
  741.                     if(ServiceBinding.REST.equals(serviceBinding)){
  742.                         if(!restEnabled){
  743.                             throw new Exception("Binding Rest for protocol ["+protocolManifest+"] is disabled: cannot define restriction for web context "+context);
  744.                         }
  745.                     }
  746.                     else{
  747.                         if(!soapEnabled){
  748.                             throw new Exception("Binding Soap for protocol ["+protocolManifest+"] is disabled: cannot define restriction for web context "+context);
  749.                         }
  750.                     }
  751.                 }
  752.                 for (int j = 0; j < manifestOpenspcoop2.getWeb().getContext(i).sizeSubContextList(); j++) {
  753.                     SubContextMapping subContext = manifestOpenspcoop2.getWeb().getContext(i).getSubContext(j);
  754.                     checkMessageType(context,subContext.getBase(),subContext.getMessageType(),
  755.                             manifestOpenspcoop2.getBinding().getSoap(),
  756.                             manifestOpenspcoop2.getBinding().getRest());
  757.                 }
  758.                 if(manifestOpenspcoop2.getWeb().getContext(i).getEmptySubContext()!=null){
  759.                     checkMessageType(context,Costanti.CONTEXT_EMPTY,manifestOpenspcoop2.getWeb().getContext(i).getEmptySubContext().getMessageType(),
  760.                             manifestOpenspcoop2.getBinding().getSoap(),
  761.                             manifestOpenspcoop2.getBinding().getRest());
  762.                 }
  763.                
  764.                 if(manifestOpenspcoop2.getWeb().getContext(i).getSoapMediaTypeCollection()!=null){
  765.                     if(manifestOpenspcoop2.getWeb().getContext(i).getSoapMediaTypeCollection().sizeMediaTypeList()>0 ||
  766.                             manifestOpenspcoop2.getWeb().getContext(i).getSoapMediaTypeCollection().getDefault()!=null ||
  767.                                     manifestOpenspcoop2.getWeb().getContext(i).getSoapMediaTypeCollection().getUndefined()!=null){
  768.                         if(soapEnabled==false){
  769.                             throw new ProtocolException("(Context:"+context+") SoapMediaType not supported; Soap disabled");
  770.                         }
  771.                     }
  772.                 }
  773.                 if(manifestOpenspcoop2.getWeb().getContext(i).getRestMediaTypeCollection()!=null){
  774.                     if(manifestOpenspcoop2.getWeb().getContext(i).getRestMediaTypeCollection().sizeMediaTypeList()>0 ||
  775.                             manifestOpenspcoop2.getWeb().getContext(i).getRestMediaTypeCollection().getDefault()!=null ||
  776.                                     manifestOpenspcoop2.getWeb().getContext(i).getRestMediaTypeCollection().getUndefined()!=null){
  777.                         if(restEnabled==false){
  778.                             throw new ProtocolException("(Context:"+context+") RestMediaType not supported; Rest disabled");
  779.                         }
  780.                     }
  781.                 }
  782.             }
  783.            
  784.             if(manifestOpenspcoop2.getWeb().getEmptyContext()!=null){
  785.                 ServiceBinding serviceBinding = manifestOpenspcoop2.getWeb().getEmptyContext().getBinding();
  786.                 if(serviceBinding!=null){
  787.                     if(ServiceBinding.REST.equals(serviceBinding)){
  788.                         if(!restEnabled){
  789.                             throw new Exception("Binding Rest for protocol ["+protocolManifest+"] is disabled: cannot define restriction for empty web context");
  790.                         }
  791.                     }
  792.                     else{
  793.                         if(!soapEnabled){
  794.                             throw new Exception("Binding Soap for protocol ["+protocolManifest+"] is disabled: cannot define restriction for empty web context");
  795.                         }
  796.                     }
  797.                 }
  798.                 for (int j = 0; j < manifestOpenspcoop2.getWeb().getEmptyContext().sizeSubContextList(); j++) {
  799.                     SubContextMapping subContext = manifestOpenspcoop2.getWeb().getEmptyContext().getSubContext(j);
  800.                     checkMessageType(Costanti.CONTEXT_EMPTY,subContext.getBase(),subContext.getMessageType(),
  801.                             manifestOpenspcoop2.getBinding().getSoap(),
  802.                             manifestOpenspcoop2.getBinding().getRest());
  803.                 }
  804.                 if(manifestOpenspcoop2.getWeb().getEmptyContext().getEmptySubContext()!=null){
  805.                     checkMessageType(Costanti.CONTEXT_EMPTY,Costanti.CONTEXT_EMPTY,manifestOpenspcoop2.getWeb().getEmptyContext().getEmptySubContext().getMessageType(),
  806.                             manifestOpenspcoop2.getBinding().getSoap(),
  807.                             manifestOpenspcoop2.getBinding().getRest());
  808.                 }
  809.                
  810.                 if(manifestOpenspcoop2.getWeb().getEmptyContext().getSoapMediaTypeCollection()!=null){
  811.                     if(manifestOpenspcoop2.getWeb().getEmptyContext().getSoapMediaTypeCollection().sizeMediaTypeList()>0 ||
  812.                             manifestOpenspcoop2.getWeb().getEmptyContext().getSoapMediaTypeCollection().getDefault()!=null ||
  813.                                     manifestOpenspcoop2.getWeb().getEmptyContext().getSoapMediaTypeCollection().getUndefined()!=null){
  814.                         if(soapEnabled==false){
  815.                             throw new ProtocolException("(Context:"+Costanti.CONTEXT_EMPTY+") SoapMediaType not supported; Soap disabled");
  816.                         }
  817.                     }
  818.                 }
  819.                 if(manifestOpenspcoop2.getWeb().getEmptyContext().getRestMediaTypeCollection()!=null){
  820.                     if(manifestOpenspcoop2.getWeb().getEmptyContext().getRestMediaTypeCollection().sizeMediaTypeList()>0 ||
  821.                             manifestOpenspcoop2.getWeb().getEmptyContext().getRestMediaTypeCollection().getDefault()!=null ||
  822.                                     manifestOpenspcoop2.getWeb().getEmptyContext().getRestMediaTypeCollection().getUndefined()!=null){
  823.                         if(restEnabled==false){
  824.                             throw new ProtocolException("(Context:"+Costanti.CONTEXT_EMPTY+") RestMediaType not supported; Rest disabled");
  825.                         }
  826.                     }
  827.                 }
  828.             }
  829.            
  830.             for (int i = 0; i < manifestOpenspcoop2.getRegistry().getService().getTypes().sizeTypeList(); i++) {
  831.                 String serviceType = manifestOpenspcoop2.getRegistry().getService().getTypes().getType(i).getName();
  832.                 ServiceBinding serviceBinding = manifestOpenspcoop2.getRegistry().getService().getTypes().getType(i).getBinding();
  833.                 if(serviceBinding!=null){
  834.                     if(ServiceBinding.REST.equals(serviceBinding)){
  835.                         if(!restEnabled){
  836.                             throw new Exception("Binding Rest for protocol ["+protocolManifest+"] is disabled: cannot define restriction for service type "+serviceType);
  837.                         }
  838.                     }
  839.                     else{
  840.                         if(!soapEnabled){
  841.                             throw new Exception("Binding Soap for protocol ["+protocolManifest+"] is disabled: cannot define restriction for service type "+serviceType);
  842.                         }
  843.                     }
  844.                 }
  845.                
  846.                 if( manifestOpenspcoop2.getRegistry().getService().getTypes().getType(i).getSoapMediaTypeCollection()!=null){
  847.                     if( manifestOpenspcoop2.getRegistry().getService().getTypes().getType(i).getSoapMediaTypeCollection().sizeMediaTypeList()>0 ||
  848.                              manifestOpenspcoop2.getRegistry().getService().getTypes().getType(i).getSoapMediaTypeCollection().getDefault()!=null ||
  849.                                      manifestOpenspcoop2.getRegistry().getService().getTypes().getType(i).getSoapMediaTypeCollection().getUndefined()!=null){
  850.                         if(soapEnabled==false){
  851.                             throw new ProtocolException("(ServiceType:"+serviceType+") SoapMediaType not supported; Soap disabled");
  852.                         }
  853.                     }
  854.                 }
  855.                 if( manifestOpenspcoop2.getRegistry().getService().getTypes().getType(i).getRestMediaTypeCollection()!=null){
  856.                     if( manifestOpenspcoop2.getRegistry().getService().getTypes().getType(i).getRestMediaTypeCollection().sizeMediaTypeList()>0 ||
  857.                              manifestOpenspcoop2.getRegistry().getService().getTypes().getType(i).getRestMediaTypeCollection().getDefault()!=null ||
  858.                                      manifestOpenspcoop2.getRegistry().getService().getTypes().getType(i).getRestMediaTypeCollection().getUndefined()!=null){
  859.                         if(restEnabled==false){
  860.                             throw new ProtocolException("(ServiceType:"+serviceType+") RestMediaType not supported; Rest disabled");
  861.                         }
  862.                     }
  863.                 }
  864.             }
  865.        
  866.             if(soapEnabled){
  867.                 if(tipoServizioDefaultSoap==null){
  868.                     throw new Exception("Service type not defined for protocol ["+protocolManifest+"] compatible with soap");
  869.                 }
  870.             }
  871.             if(restEnabled){
  872.                 if(tipoServizioDefaultRest==null){
  873.                     throw new Exception("Service type not defined for protocol ["+protocolManifest+"] compatible with rest");
  874.                 }
  875.             }
  876.         }
  877.        
  878.         // 7. controllo configurazione dei parametri di integrazione
  879.         for (String protocolManifest : tmp_manifests.keySet()) {
  880.             Openspcoop2 manifestOpenspcoop2 = tmp_manifests.get(protocolManifest);
  881.            
  882.             if(manifestOpenspcoop2.getBinding().getRest()!=null) {
  883.                 if(manifestOpenspcoop2.getBinding().getRest().getIntegration()==null){
  884.                     throw new Exception("BindingIntegrationRest not defined for protocol ["+protocolManifest+"]");
  885.                 }
  886.                 Integration integrationRestConfig = manifestOpenspcoop2.getBinding().getRest().getIntegration();
  887.                 checkIntegration(integrationRestConfig, false, protocolManifest);
  888.             }
  889.            
  890.             if(manifestOpenspcoop2.getBinding().getSoap()!=null) {
  891.                 if(manifestOpenspcoop2.getBinding().getSoap().getIntegration()==null){
  892.                     throw new Exception("BindingIntegrationSoap not defined for protocol ["+protocolManifest+"]");
  893.                 }
  894.                 Integration integrationSoapConfig = manifestOpenspcoop2.getBinding().getSoap().getIntegration();
  895.                 checkIntegration(integrationSoapConfig, true, protocolManifest);
  896.             }
  897.         }
  898.     }
  899.     private void checkIntegration(Integration integrationConfig, boolean soap, String protocolManifest) throws Exception {
  900.         String prefix = soap?"Soap":"Rest";
  901.        
  902.         if(integrationConfig.getImplementation()==null){
  903.             throw new Exception("BindingIntegration"+prefix+", implementation not defined for protocol ["+protocolManifest+"]");
  904.         }
  905.         IntegrationConfiguration integrationConfigImpl = integrationConfig.getImplementation();
  906.         checkIntegration(integrationConfigImpl, soap, true, protocolManifest);
  907.        
  908.         if(integrationConfig.getSubscription()==null){
  909.             throw new Exception("BindingIntegration"+prefix+", subscription not defined for protocol ["+protocolManifest+"]");
  910.         }
  911.         IntegrationConfiguration integrationConfigSub = integrationConfig.getSubscription();
  912.         checkIntegration(integrationConfigSub, soap, false, protocolManifest);
  913.     }
  914.     private void checkIntegration(IntegrationConfiguration integrationConfig, boolean soap, boolean impl, String protocolManifest) throws Exception {
  915.        
  916.         String prefix = soap?"Soap":"Rest";
  917.         prefix = prefix + (impl?"Implementation":"Subscription");
  918.        
  919.         if(integrationConfig.getName()==null || integrationConfig.getName().sizeParamList()<=0){
  920.             throw new Exception("BindingIntegration"+prefix+", name not defined for protocol ["+protocolManifest+"]");
  921.         }
  922.         this.checkIntegrationName(integrationConfig.getName().getParamList(), protocolManifest, prefix, "name", false, false);

  923.         if(integrationConfig.getResourceIdentification()==null) {
  924.             throw new Exception("BindingIntegration"+prefix+", resource identification not defined for protocol ["+protocolManifest+"]");
  925.         }
  926.         if(integrationConfig.getResourceIdentification().getIdentificationModes()==null || integrationConfig.getResourceIdentification().getIdentificationModes().sizeModeList()<=0) {
  927.             throw new Exception("BindingIntegration"+prefix+", resource identification modes not defined for protocol ["+protocolManifest+"]");
  928.         }
  929.         ResourceIdentificationType defaultR = integrationConfig.getResourceIdentification().getIdentificationModes().getDefault();
  930.         if(defaultR==null) {
  931.             if(integrationConfig.getResourceIdentification().getIdentificationModes().sizeModeList()>1) {
  932.                 throw new Exception("BindingIntegration"+prefix+", resource identification default mode undefined for protocol ["+protocolManifest+"]");
  933.             }
  934.             else {
  935.                 defaultR = integrationConfig.getResourceIdentification().getIdentificationModes().getMode(0).getName();
  936.             }
  937.         }
  938.         boolean defaultFound = false;
  939.         for (IntegrationConfigurationResourceIdentificationMode mode : integrationConfig.getResourceIdentification().getIdentificationModes().getModeList()) {
  940.             if(!soap) {
  941.                 if(ResourceIdentificationType.SOAP_ACTION.equals(mode.getName())) {
  942.                     throw new Exception("BindingIntegration"+prefix+", resource identification mode '"+mode.getName()+"' not permit in rest binding, founded in protocol ["+protocolManifest+"]");
  943.                 }
  944.             }
  945.             if(!impl) {
  946.                 if(ResourceIdentificationType.PROTOCOL.equals(mode.getName())) {
  947.                     throw new Exception("BindingIntegration"+prefix+", resource identification mode '"+mode.getName()+"' not permit in subscription binding, founded in protocol ["+protocolManifest+"]");
  948.                 }
  949.             }
  950.             if(defaultR!=null) {
  951.                 if(mode.getName().equals(defaultR)) {
  952.                     defaultFound = true;
  953.                 }
  954.             }
  955.         }
  956.         if(!defaultFound) {
  957.             throw new Exception("BindingIntegration"+prefix+", resource identification default mode '"+defaultR+"' not found in supported modes for protocol ["+protocolManifest+"]");
  958.         }
  959.         if(ResourceIdentificationType.CONTENT.equals(defaultR) ||
  960.                 ResourceIdentificationType.HEADER.equals(defaultR) ||
  961.                 ResourceIdentificationType.URL.equals(defaultR)) {
  962.             if(integrationConfig.getResourceIdentification().getIdentificationParameter()==null ||
  963.                     integrationConfig.getResourceIdentification().getIdentificationParameter().sizeParamList()<=0) {
  964.                 throw new Exception("BindingIntegration"+prefix+", resource identification default mode '"+defaultR+"' require identification parameter for protocol ["+protocolManifest+"]");
  965.             }
  966.             this.checkIntegrationName(integrationConfig.getResourceIdentification().getIdentificationParameter().getParamList(),
  967.                     protocolManifest, prefix, "identificationParameter", true, false);
  968.         }
  969.        
  970.         if(integrationConfig.getResourceIdentification().getSpecificResource()==null) {
  971.             throw new Exception("BindingIntegration"+prefix+", specific resource config not defined for protocol ["+protocolManifest+"]");
  972.         }
  973.         if(integrationConfig.getResourceIdentification().getSpecificResource().getName()==null ||
  974.                 integrationConfig.getResourceIdentification().getSpecificResource().getName().sizeParamList()<=0) {
  975.             throw new Exception("BindingIntegration"+prefix+", specific resource name undefined for protocol ["+protocolManifest+"]");
  976.         }
  977.         this.checkIntegrationName(integrationConfig.getResourceIdentification().getSpecificResource().getName().getParamList(),
  978.                 protocolManifest, prefix, "specific resource name", true, true);
  979.     }
  980.     private void checkIntegrationName(List<IntegrationConfigurationElementName> list, String protocolManifest, String prefix, String object,
  981.             boolean namePermit, boolean ruleNamePermit) throws Exception {
  982.         for (IntegrationConfigurationElementName name : list) {
  983.             if(name==null) {
  984.                 throw new Exception("BindingIntegration"+prefix+", "+object+" not defined for protocol ["+protocolManifest+"]");
  985.             }
  986.             if( (name.getPrefix()==null || "".equals(name.getPrefix())) &&
  987.                     (name.getActor()==null) &&
  988.                     (name.getSuffix()==null || "".equals(name.getSuffix()))) {
  989.                 throw new Exception("BindingIntegration"+prefix+", "+object+" with wrong param (prefix, actor and suffix undefined) for protocol ["+protocolManifest+"]");
  990.             }
  991.             if(ActorType.NAME.equals(name.getActor()) && !namePermit) {
  992.                 throw new Exception("BindingIntegration"+prefix+", "+object+" with wrong param (actor '"+name.getActor()+"' not permit in this context) for protocol ["+protocolManifest+"]");
  993.             }
  994.             if(ActorType.RULE_NAME.equals(name.getActor()) && !ruleNamePermit) {
  995.                 throw new Exception("BindingIntegration"+prefix+", "+object+" with wrong param (actor '"+name.getActor()+"' not permit in this context) for protocol ["+protocolManifest+"]");
  996.             }
  997.            
  998.         }
  999.     }
  1000.    
  1001.     private void checkHttpReturnCode(DefaultIntegrationError ir,String function) throws ProtocolException {
  1002.         if(ir!=null){
  1003.             int httpReturnCode = ir.getErrorCode().getHttp();
  1004.             this.checkHttpReturnCode(httpReturnCode, function+".http");
  1005.            
  1006.             int govwayReturnCode = ir.getErrorCode().getGovway();
  1007.             this.checkHttpReturnCode(govwayReturnCode, function+".govway");
  1008.         }
  1009.     }
  1010.     private void checkHttpReturnCode(IntegrationError ir,String function) throws ProtocolException {
  1011.         if(ir!=null){
  1012.             int httpReturnCode = ir.getErrorCode().getHttp();
  1013.             this.checkHttpReturnCode(httpReturnCode, function+".http");
  1014.            
  1015.             int govwayReturnCode = ir.getErrorCode().getGovway();
  1016.             this.checkHttpReturnCode(govwayReturnCode, function+".rfc7807");
  1017.         }
  1018.     }
  1019.     private void checkHttpReturnCode(int httpReturnCode,String function) throws ProtocolException {
  1020.         String msgError = "The value provided ("+httpReturnCode+") is not used as http return code (use [200,299],[400-599])";
  1021.         if(httpReturnCode<200){
  1022.             throw new ProtocolException(msgError);
  1023.         }
  1024.         if(httpReturnCode>=300 && httpReturnCode<400){
  1025.             throw new ProtocolException(msgError);
  1026.         }
  1027.         if(httpReturnCode>=600){
  1028.             throw new ProtocolException(msgError);
  1029.         }
  1030.     }
  1031.     private void checkMessageType(String context, String subContext, MessageType messageType,SoapConfiguration soapBinding, RestConfiguration restBinding) throws ProtocolException {
  1032.         if(MessageType.XML.equals(messageType)){
  1033.             if(restBinding==null){
  1034.                 throw new ProtocolException("(Context:"+context+" SubContext:"+subContext+") MessageType ["+messageType+"] not supported; Rest disabled");
  1035.             }
  1036.             if(!restBinding.isXml()){
  1037.                 throw new ProtocolException("(Context:"+context+" SubContext:"+subContext+") MessageType ["+messageType+"] not supported in RestBinding; Xml disabled");
  1038.             }
  1039.         }
  1040.         else if(MessageType.JSON.equals(messageType)){
  1041.             if(restBinding==null){
  1042.                 throw new ProtocolException("(Context:"+context+" SubContext:"+subContext+") MessageType ["+messageType+"] not supported; Rest disabled");
  1043.             }
  1044.             if(!restBinding.isJson()){
  1045.                 throw new ProtocolException("(Context:"+context+" SubContext:"+subContext+") MessageType ["+messageType+"] not supported in RestBinding; Json disabled");
  1046.             }
  1047.         }
  1048.         else if(MessageType.BINARY.equals(messageType)){
  1049.             if(restBinding==null){
  1050.                 throw new ProtocolException("(Context:"+context+" SubContext:"+subContext+") MessageType ["+messageType+"] not supported; Rest disabled");
  1051.             }
  1052.             if(!restBinding.isBinary()){
  1053.                 throw new ProtocolException("(Context:"+context+" SubContext:"+subContext+") MessageType ["+messageType+"] not supported in RestBinding; Binary disabled");
  1054.             }
  1055.         }
  1056.         else if(MessageType.MIME_MULTIPART.equals(messageType)){
  1057.             if(restBinding==null){
  1058.                 throw new ProtocolException("(Context:"+context+" SubContext:"+subContext+") MessageType ["+messageType+"] not supported; Rest disabled");
  1059.             }
  1060.             if(!restBinding.isMimeMultipart()){
  1061.                 throw new ProtocolException("(Context:"+context+" SubContext:"+subContext+") MessageType ["+messageType+"] not supported in RestBinding; MimeMultipart disabled");
  1062.             }
  1063.         }
  1064.         else if(MessageType.SOAP_11.equals(messageType)){
  1065.             if(soapBinding==null){
  1066.                 throw new ProtocolException("(Context:"+context+" SubContext:"+subContext+") MessageType ["+messageType+"] not supported; Soap disabled");
  1067.             }
  1068.             if(!soapBinding.isSoap11()){
  1069.                 throw new ProtocolException("(Context:"+context+" SubContext:"+subContext+") MessageType ["+messageType+"] not supported in SoapBinding; Soap11 disabled");
  1070.             }
  1071.         }
  1072.         else if(MessageType.SOAP_12.equals(messageType)){
  1073.             if(soapBinding==null){
  1074.                 throw new ProtocolException("(Context:"+context+" SubContext:"+subContext+") MessageType ["+messageType+"] not supported; Soap disabled");
  1075.             }
  1076.             if(!soapBinding.isSoap12()){
  1077.                 throw new ProtocolException("(Context:"+context+" SubContext:"+subContext+") MessageType ["+messageType+"] not supported in SoapBinding; Soap12 disabled");
  1078.             }
  1079.         }
  1080.         else{
  1081.             throw new ProtocolException("(Context:"+context+" SubContext:"+subContext+") MessageType ["+messageType+"] not supported in Binding");
  1082.         }
  1083.     }
  1084.    
  1085.     private IProtocolFactory<?> getProtocolFactoryEngine(Openspcoop2 openspcoop2Manifest) throws ProtocolException {
  1086.        
  1087.         String factoryClass = null;
  1088.         try{
  1089.             factoryClass = openspcoop2Manifest.getProtocol().getFactory();
  1090.             IProtocolFactory<?> p = (IProtocolFactory<?>) ClassLoaderUtilities.newInstance(factoryClass);
  1091.             return  p;
  1092.         } catch (Exception e) {
  1093.             throw new ProtocolException("Impossibile caricare la factory indicata ["+factoryClass+"] " + e, e);
  1094.         }
  1095.     }
  1096.    
  1097.    
  1098.     public Openspcoop2 getProtocolManifest(HttpServletRequest request, FunctionContextsCustom customContexts) throws ProtocolException {
  1099.        
  1100.         URLProtocolContext urlProtocolContext = null;
  1101.         try {
  1102.             urlProtocolContext = new URLProtocolContextImpl(request, this.log, true, customContexts);
  1103.         } catch (Exception e) {
  1104.             throw new ProtocolException("Impossibile recuperare il nome del contesto dalla request: ServletContext["+request.getContextPath()+"] RequestURI["+request.getRequestURI()+"]",e);
  1105.         }
  1106.         return getProtocolManifest(urlProtocolContext.getProtocolWebContext());
  1107.     }
  1108.    
  1109.    
  1110.     public Openspcoop2 getProtocolManifest(String servletContextName) throws ProtocolException {
  1111.         try {
  1112.            
  1113.             Iterator<Openspcoop2> itProtocols = this.manifests.values().iterator();
  1114.             while (itProtocols.hasNext()) {
  1115.                 Openspcoop2 openspcoop2Manifest = itProtocols.next();
  1116.                 Web webContext = openspcoop2Manifest.getWeb();
  1117.                
  1118.                 if(Costanti.CONTEXT_EMPTY.equals(servletContextName)){
  1119.                     if(webContext.getEmptyContext()!=null && webContext.getEmptyContext().getEnabled()){
  1120.                         return openspcoop2Manifest;
  1121.                     }
  1122.                 }else{
  1123.                     for (int i = 0; i < webContext.sizeContextList(); i++) {
  1124.                         if(webContext.getContext(i).getName().equals(servletContextName)){
  1125.                             return openspcoop2Manifest;
  1126.                         }
  1127.                     }
  1128.                 }
  1129.                
  1130.             }
  1131.            
  1132.             throw new ProtocolException("Contesto [" + servletContextName + "] non assegnato a nessun protocollo");

  1133.         } catch (Exception e) {
  1134.             throw new ProtocolException("Impossibile individuare il protocollo assegnato al contesto [" + servletContextName + "]: " + e, e);
  1135.         }
  1136.     }
  1137.    
  1138.     public IProtocolFactory<?> getProtocolFactoryByServletContext(HttpServletRequest request, FunctionContextsCustom customContexts) throws ProtocolException {
  1139.         Openspcoop2 m = this.getProtocolManifest(request,customContexts);
  1140.         if(this.factories.containsKey(m.getProtocol().getName())){
  1141.             return this.factories.get(m.getProtocol().getName());
  1142.         }
  1143.         else{
  1144.             throw new ProtocolException("ProtocolPlugin with name ["+m.getProtocol().getName()+"] not found");
  1145.         }
  1146.     }
  1147.    
  1148.     public IProtocolFactory<?> getProtocolFactoryByServletContext(String servletContext) throws ProtocolException {
  1149.         Openspcoop2 m = this.getProtocolManifest(servletContext);
  1150.         if(this.factories.containsKey(m.getProtocol().getName())){
  1151.             return this.factories.get(m.getProtocol().getName());
  1152.         }
  1153.         else{
  1154.             throw new ProtocolException("ProtocolPlugin with name ["+m.getProtocol().getName()+"] not found");
  1155.         }
  1156.     }
  1157.    
  1158.     public IProtocolFactory<?> getProtocolFactoryByName(String protocol) throws ProtocolException {
  1159.         if(this.factories.containsKey(protocol)){
  1160.             return this.factories.get(protocol);
  1161.         }
  1162.         else{
  1163.             throw new ProtocolException("ProtocolPlugin with name ["+protocol+"] not found");
  1164.         }
  1165.     }
  1166.    
  1167.     public boolean existsProtocolFactory(String protocol) {
  1168.         return this.factories.containsKey(protocol);
  1169.     }
  1170.    
  1171.     public IProtocolFactory<?> getProtocolFactoryByOrganizationType(String organizationType) throws ProtocolException {
  1172.         String protocol = this.getProtocolByOrganizationType(organizationType);
  1173.         return this.getProtocolFactoryByName(protocol);
  1174.     }
  1175.    
  1176.     public IProtocolFactory<?> getProtocolFactoryByServiceType(String serviceType) throws ProtocolException {
  1177.         String protocol = this.getProtocolByServiceType(serviceType);
  1178.         return this.getProtocolFactoryByName(protocol);
  1179.     }
  1180.    
  1181.     public IProtocolFactory<?> getProtocolFactoryWithEmptyContext() throws ProtocolException {
  1182.         Openspcoop2 m = this.getProtocolManifest(Costanti.CONTEXT_EMPTY);
  1183.         if(this.factories.containsKey(m.getProtocol().getName())){
  1184.             return this.factories.get(m.getProtocol().getName());
  1185.         }
  1186.         return null;
  1187.     }
  1188.     public IDService getDefaultServiceForWebContext(String context) throws ProtocolException {
  1189.         IProtocolFactory<?> pf = this.getProtocolFactoryByServletContext(context);
  1190.         if(pf==null) {
  1191.             return null;
  1192.         }
  1193.         if(pf.getManifest().getWeb().sizeContextList()<=0) {
  1194.             return null;
  1195.         }
  1196.         FunctionType fType = null;
  1197.         for (int i = 0; i < pf.getManifest().getWeb().sizeContextList(); i++) {
  1198.             if(pf.getManifest().getWeb().getContext(i).getName().equals(context)){
  1199.                 fType = pf.getManifest().getWeb().getContext(i).getEmptyFunction();
  1200.                 break;
  1201.             }
  1202.         }
  1203.         if(fType==null) {
  1204.             return null;
  1205.         }
  1206.         switch (fType) {
  1207.         case PD:
  1208.             return IDService.PORTA_DELEGATA;
  1209.         case PA:
  1210.             return IDService.PORTA_APPLICATIVA;
  1211.         case PDTO_SOAP:
  1212.             return IDService.PORTA_DELEGATA_XML_TO_SOAP;
  1213.         default:
  1214.             return null;
  1215.         }
  1216.     }
  1217.     public IDService getDefaultServiceForEmptyContext() throws ProtocolException {
  1218.         IProtocolFactory<?> pf = this.getProtocolFactoryWithEmptyContext();
  1219.         if(pf==null) {
  1220.             return null;
  1221.         }
  1222.         if(pf.getManifest().getWeb().getEmptyContext()==null) {
  1223.             return null;
  1224.         }
  1225.         FunctionType fType = pf.getManifest().getWeb().getEmptyContext().getEmptyFunction();
  1226.         if(fType==null) {
  1227.             return null;
  1228.         }
  1229.         switch (fType) {
  1230.         case PD:
  1231.             return IDService.PORTA_DELEGATA;
  1232.         case PA:
  1233.             return IDService.PORTA_APPLICATIVA;
  1234.         case PDTO_SOAP:
  1235.             return IDService.PORTA_DELEGATA_XML_TO_SOAP;
  1236.         default:
  1237.             return null;
  1238.         }
  1239.     }
  1240.    
  1241.     public IProtocolFactory<?> getDefaultProtocolFactory() throws ProtocolException {
  1242.         try {
  1243.             if(this.factories.size()==1){
  1244.                 return this.factories.get((String)this.factories.keys().nextElement());
  1245.             }
  1246.             else{
  1247.                 if(this.protocolDefault==null){
  1248.                     throw new Exception("Non e' stato definito un protocollo di default e sono stati riscontrati piu' protocolli disponibili (size:"+this.factories.size()+")");
  1249.                 }
  1250.                 else{
  1251.                     if(this.factories.containsKey(this.protocolDefault)){
  1252.                         return this.factories.get(this.protocolDefault);
  1253.                     }else{
  1254.                         throw new Exception("Il protocollo di default ["+this.protocolDefault+"] indicato non corrisponde a nessuno di quelli caricati");
  1255.                     }
  1256.                 }
  1257.             }
  1258.         } catch (Exception e) {
  1259.             throw new ProtocolException("Impossibile individuare il protocollo assegnato al contesto: " + e, e);
  1260.         }
  1261.     }
  1262.    
  1263.    
  1264.     public MapReader<String, List<String>> getOrganizationTypes() {
  1265.         return this.tipiSoggettiValidi;
  1266.     }
  1267.     public String[] getOrganizationTypesAsArray() {
  1268.         Enumeration<List<String>> listeTipiSoggettiValidi = this.tipiSoggettiValidi.elements();
  1269.         List<String> listaTipiSoggetti = new ArrayList<>();
  1270.         while(listeTipiSoggettiValidi.hasMoreElements()){
  1271.             listaTipiSoggetti.addAll(listeTipiSoggettiValidi.nextElement());
  1272.         }
  1273.         return listaTipiSoggetti.toArray(new String[1]);
  1274.     }
  1275.     public List<String> getOrganizationTypesAsList() {
  1276.         Enumeration<List<String>> listeTipiSoggettiValidi = this.tipiSoggettiValidi.elements();
  1277.         List<String> listaTipiSoggetti = new ArrayList<>();
  1278.         while(listeTipiSoggettiValidi.hasMoreElements()){
  1279.             listaTipiSoggetti.addAll(listeTipiSoggettiValidi.nextElement());
  1280.         }
  1281.         return listaTipiSoggetti;
  1282.     }
  1283.     public MapReader<String, String> getDefaultOrganizationTypes() {
  1284.         return this.tipiSoggettiDefault;
  1285.     }
  1286.    
  1287.    
  1288.     public HashMap<String, List<String>> _getServiceTypes() {
  1289.         HashMap<String, List<String>> map = new HashMap<>();
  1290.        
  1291.         MapReader<String, List<String>> rest = this.getServiceTypes(ServiceBinding.REST);
  1292.         if(rest!=null && rest.size()>0) {
  1293.             Enumeration<String> en = rest.keys();
  1294.             while (en.hasMoreElements()) {
  1295.                 String protocollo = (String) en.nextElement();
  1296.                 map.put(protocollo, rest.get(protocollo));
  1297.             }
  1298.         }
  1299.        
  1300.         MapReader<String, List<String>> soap = this.getServiceTypes(ServiceBinding.SOAP);
  1301.         if(soap!=null && soap.size()>0) {
  1302.             Enumeration<String> en = soap.keys();
  1303.             while (en.hasMoreElements()) {
  1304.                 String protocollo = (String) en.nextElement();
  1305.                 if(map.containsKey(protocollo)) {
  1306.                     List<String> restP = map.get(protocollo);
  1307.                     List<String> soapP = soap.get(protocollo);
  1308.                     for (String tipo : soapP) {
  1309.                         if(restP.contains(tipo)==false) {
  1310.                             restP.add(tipo);
  1311.                         }
  1312.                     }
  1313.                 }
  1314.                 else {
  1315.                     map.put(protocollo, soap.get(protocollo));
  1316.                 }
  1317.             }
  1318.         }
  1319.        
  1320.         return map;
  1321.     }
  1322.     public MapReader<String, List<String>> getServiceTypes(ServiceBinding serviceBinding) {
  1323.         if(ServiceBinding.SOAP.equals(serviceBinding)){
  1324.             return this.tipiServiziValidi_soap;
  1325.         }
  1326.         else{
  1327.             return this.tipiServiziValidi_rest;
  1328.         }
  1329.     }
  1330.     public String[] getServiceTypesAsArray(ServiceBinding serviceBinding) {
  1331.         Enumeration<List<String>> listeTipiServiziValidi = null;
  1332.         if(ServiceBinding.SOAP.equals(serviceBinding)){
  1333.             listeTipiServiziValidi = this.tipiServiziValidi_soap.elements();
  1334.         }
  1335.         else{
  1336.             listeTipiServiziValidi = this.tipiServiziValidi_rest.elements();
  1337.         }
  1338.         List<String> listaTipiServizi = new ArrayList<>();
  1339.         while(listeTipiServiziValidi.hasMoreElements()){
  1340.             listaTipiServizi.addAll(listeTipiServiziValidi.nextElement());
  1341.         }
  1342.         return listaTipiServizi.toArray(new String[1]);
  1343.     }
  1344.     public List<String> getServiceTypesAsList(ServiceBinding serviceBinding) {
  1345.         Enumeration<List<String>> listeTipiServiziValidi = null;
  1346.         if(ServiceBinding.SOAP.equals(serviceBinding)){
  1347.             listeTipiServiziValidi = this.tipiServiziValidi_soap.elements();
  1348.         }
  1349.         else{
  1350.             listeTipiServiziValidi = this.tipiServiziValidi_rest.elements();
  1351.         }
  1352.         List<String> listaTipiServizi = new ArrayList<>();
  1353.         while(listeTipiServiziValidi.hasMoreElements()){
  1354.             listaTipiServizi.addAll(listeTipiServiziValidi.nextElement());
  1355.         }
  1356.         return listaTipiServizi;
  1357.     }
  1358.     public MapReader<String, String> getDefaultServiceTypes(ServiceBinding serviceBinding) {
  1359.         if(ServiceBinding.SOAP.equals(serviceBinding)){
  1360.             return this.tipiServiziDefault_soap;
  1361.         }
  1362.         else{
  1363.             return this.tipiServiziDefault_rest;
  1364.         }
  1365.     }
  1366.    
  1367.    
  1368.     public MapReader<String, List<String>> getVersion() {
  1369.         return this.versioniValide;
  1370.     }
  1371.     public String[] getVersionAsArray() {
  1372.         Enumeration<List<String>> listeVersioniValidi = this.versioniValide.elements();
  1373.         List<String> listaVersioni = new ArrayList<>();
  1374.         while(listeVersioniValidi.hasMoreElements()){
  1375.             listaVersioni.addAll(listeVersioniValidi.nextElement());
  1376.         }
  1377.         return listaVersioni.toArray(new String[1]);
  1378.     }
  1379.     public List<String> getVersionAsList() {
  1380.         Enumeration<List<String>> listeVersioniValide = this.versioniValide.elements();
  1381.         List<String> listaVersioni = new ArrayList<>();
  1382.         while(listeVersioniValide.hasMoreElements()){
  1383.             listaVersioni.addAll(listeVersioniValide.nextElement());
  1384.         }
  1385.         return listaVersioni;
  1386.     }
  1387.     public MapReader<String, String> getDefaultVersion() {
  1388.         return this.versioniDefault;
  1389.     }
  1390.    
  1391.    
  1392.     public MapReader<String, IProtocolFactory<?>> getProtocolFactories() {
  1393.         return this.factories;
  1394.     }
  1395.    
  1396.     public Enumeration<String> getProtocolNames(){
  1397.         return this.factories.keys();
  1398.     }
  1399.    
  1400.     public List<String> getProtocolNamesAsList(){
  1401.         List<String> protocolli = new ArrayList<>();
  1402.         Enumeration<String> en = getProtocolNames();
  1403.         while (en.hasMoreElements()) {
  1404.             String protocollo = (String) en.nextElement();
  1405.             protocolli.add(protocollo);
  1406.         }
  1407.         return protocolli;
  1408.     }
  1409.    
  1410.     public void initStaticInstance() throws ProtocolException {
  1411.         if(this.factories!=null && this.factories.size()>0) {
  1412.             Enumeration<IProtocolFactory<?>> en = this.factories.elements();
  1413.             if(en!=null) {
  1414.                 while (en.hasMoreElements()) {
  1415.                     IProtocolFactory<?> iProtocolFactory = (IProtocolFactory<?>) en.nextElement();
  1416.                     if(iProtocolFactory!=null && iProtocolFactory instanceof BasicFactory<?> ) {
  1417.                         ((BasicFactory<?>)iProtocolFactory).initStaticInstance();
  1418.                     }
  1419.                 }
  1420.             }
  1421.         }
  1422.     }
  1423.    
  1424.     // ***** Utilities *****
  1425.    
  1426.     public String getProtocolByOrganizationType(String organizationType) throws ProtocolException {
  1427.        
  1428.         Enumeration<String> protocolli = this.factories.keys();
  1429.         while (protocolli.hasMoreElements()) {
  1430.                
  1431.             String protocollo = protocolli.nextElement();
  1432.             IProtocolFactory<?> protocolFactory = this.factories.get(protocollo);
  1433.             List<String> tipiP = protocolFactory.createProtocolConfiguration().getTipiSoggetti();
  1434.             if(tipiP.contains(organizationType)){
  1435.                 return protocollo;
  1436.             }
  1437.                
  1438.         }
  1439.            
  1440.         throw new ProtocolException("Non esiste un protocollo associato al tipo di soggetto ["+organizationType+"]");
  1441.            
  1442.     }
  1443.    
  1444.     public String getProtocolByServiceType(String serviceType) throws ProtocolException {
  1445.        
  1446.         Enumeration<String> protocolli = this.factories.keys();
  1447.         while (protocolli.hasMoreElements()) {
  1448.                
  1449.             String protocollo = protocolli.nextElement();
  1450.             IProtocolFactory<?> protocolFactory = this.factories.get(protocollo);
  1451.             List<String> tipiP = protocolFactory.createProtocolConfiguration().getTipiServizi(org.openspcoop2.message.constants.ServiceBinding.SOAP);
  1452.             if(tipiP.contains(serviceType)){
  1453.                 return protocollo;
  1454.             }
  1455.             tipiP = protocolFactory.createProtocolConfiguration().getTipiServizi(org.openspcoop2.message.constants.ServiceBinding.REST);
  1456.             if(tipiP.contains(serviceType)){
  1457.                 return protocollo;
  1458.             }
  1459.                
  1460.         }
  1461.            
  1462.         throw new ProtocolException("Non esiste un protocollo associato al tipo di servizio ["+serviceType+"]");
  1463.            
  1464.     }
  1465. }