AccordoServizioUtils.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.core.registry.driver;

  21. import java.io.File;
  22. import java.net.URL;
  23. import java.util.ArrayList;
  24. import java.util.HashMap;
  25. import java.util.List;
  26. import java.util.Map;

  27. import javax.xml.validation.Schema;

  28. import org.slf4j.Logger;
  29. import org.openspcoop2.core.registry.AccordoServizioParteComune;
  30. import org.openspcoop2.core.registry.Documento;
  31. import org.openspcoop2.core.registry.constants.CostantiRegistroServizi;
  32. import org.openspcoop2.message.OpenSPCoop2MessageFactory;
  33. import org.openspcoop2.message.xml.MessageXMLUtils;
  34. import org.openspcoop2.utils.LoggerWrapperFactory;
  35. import org.openspcoop2.utils.resources.FileSystemUtilities;
  36. import org.openspcoop2.utils.transport.http.HttpUtilities;
  37. import org.openspcoop2.utils.wsdl.WSDLUtilities;
  38. import org.openspcoop2.utils.xml.AbstractXMLUtils;
  39. import org.openspcoop2.utils.xml.XSDSchemaCollection;
  40. import org.openspcoop2.utils.xml.XSDUtils;
  41. import org.w3c.dom.Document;
  42. import org.w3c.dom.Element;
  43. import org.w3c.dom.Node;

  44. /**
  45.  * AccordoServizioUtils
  46.  *
  47.  * @author Andrea Poli (apoli@link.it)
  48.  * @author $Author$
  49.  * @version $Rev$, $Date$
  50.  *
  51.  */
  52. public class AccordoServizioUtils {

  53.     private Logger logger = null;
  54.     private AbstractXMLUtils xmlUtils = null;
  55.     private XSDUtils xsdUtils = null;
  56.     private OpenSPCoop2MessageFactory messageFactory;
  57.     public AccordoServizioUtils(OpenSPCoop2MessageFactory messageFactory, Logger log){
  58.         if(log!=null)
  59.             this.logger = log;
  60.         else
  61.             this.logger = LoggerWrapperFactory.getLogger(AccordoServizioUtils.class);
  62.         this.messageFactory = messageFactory;
  63.         this.xmlUtils = MessageXMLUtils.getInstance(this.messageFactory);
  64.         this.xsdUtils = new XSDUtils(this.xmlUtils);
  65.     }
  66.    
  67.    


  68.    
  69.     public Schema buildSchema(AccordoServizioParteComune as,boolean fromBytes) throws DriverRegistroServiziException {
  70.         XSDSchemaCollection schemaCollections = this.buildSchemaCollection(as, fromBytes);
  71.         try{
  72.             return schemaCollections.buildSchema(this.logger);
  73.         }catch(Exception e){
  74.             throw new DriverRegistroServiziException(e.getMessage(),e);
  75.         }
  76.     }
  77.     public XSDSchemaCollection buildSchemaCollection(AccordoServizioParteComune as,boolean fromBytes) throws DriverRegistroServiziException {

  78.         boolean definitorioPresente = false;
  79.         if(fromBytes){
  80.             definitorioPresente = as.getByteWsdlDefinitorio()!=null;
  81.         }else{
  82.             definitorioPresente = as.getWsdlDefinitorio()!=null;
  83.         }
  84.        
  85.         List<Node> schemiInWsdl = new ArrayList<Node>();
  86.         List<Node> schemiInWsdlFruitore = new ArrayList<Node>();
  87.         WSDLUtilities wsdlUtilities = null;
  88.         Document dWsdl = null;
  89.         Document dWsdlFruitore = null;
  90.         byte [] wsdl = null;
  91.         byte [] wsdlFruitore = null;
  92.         if(fromBytes){
  93.             if(as.getByteWsdlConcettuale()!=null){
  94.                 wsdl = as.getByteWsdlConcettuale();
  95.             }
  96.             else{
  97.                 if(as.getByteWsdlLogicoErogatore()!=null){
  98.                     wsdl = as.getByteWsdlLogicoErogatore();
  99.                 }
  100.                 if(as.getByteWsdlLogicoFruitore()!=null){
  101.                     wsdlFruitore = as.getByteWsdlLogicoFruitore();
  102.                 }
  103.             }
  104.         }
  105.         else{
  106.             if(as.getWsdlConcettuale()!=null){
  107.                 try{
  108.                     String location = as.getWsdlConcettuale();
  109.                     if(location.startsWith("http://") || location.startsWith("file://")){
  110.                         URL url = new URL(location);
  111.                         wsdl = HttpUtilities.requestHTTPFile(url.toString());
  112.                     }
  113.                     else{
  114.                         File f = new File(location);
  115.                         wsdl = FileSystemUtilities.readBytesFromFile(f);
  116.                     }
  117.                 }catch(Exception e){
  118.                     throw new DriverRegistroServiziException("L'accordo di servizio parte comune contiene un wsdl concettuale corrotto: "+e.getMessage(),e);
  119.                 }
  120.             }
  121.             else{
  122.                 if(as.getWsdlLogicoErogatore()!=null){
  123.                     try{
  124.                         String location = as.getWsdlLogicoErogatore();
  125.                         if(location.startsWith("http://") || location.startsWith("file://")){
  126.                             URL url = new URL(location);
  127.                             wsdl = HttpUtilities.requestHTTPFile(url.toString());
  128.                         }
  129.                         else{
  130.                             File f = new File(location);
  131.                             wsdl = FileSystemUtilities.readBytesFromFile(f);
  132.                         }
  133.                     }catch(Exception e){
  134.                         throw new DriverRegistroServiziException("L'accordo di servizio parte comune contiene un wsdl logico erogatore corrotto: "+e.getMessage(),e);
  135.                     }
  136.                 }
  137.                 if(as.getWsdlLogicoFruitore()!=null){
  138.                     try{
  139.                         String location = as.getWsdlLogicoFruitore();
  140.                         if(location.startsWith("http://") || location.startsWith("file://")){
  141.                             URL url = new URL(location);
  142.                             wsdlFruitore = HttpUtilities.requestHTTPFile(url.toString());
  143.                         }
  144.                         else{
  145.                             File f = new File(location);
  146.                             wsdlFruitore = FileSystemUtilities.readBytesFromFile(f);
  147.                         }
  148.                     }catch(Exception e){
  149.                         throw new DriverRegistroServiziException("L'accordo di servizio parte comune contiene un wsdl logico fruitore corrotto: "+e.getMessage(),e);
  150.                     }
  151.                 }
  152.             }
  153.         }
  154.         if(wsdl!=null){
  155.             try{
  156.                 wsdlUtilities = new WSDLUtilities(this.xmlUtils);
  157.                 dWsdl = this.xmlUtils.newDocument(wsdl);
  158.                 List<Node> schemi = wsdlUtilities.getSchemiXSD(dWsdl);
  159.                 if(schemi!=null && schemi.size()>0){
  160.                     for (Node schema : schemi) {
  161.                         boolean schemaWithOnlyImport = this.xsdUtils.isSchemaWithOnlyImports(schema);
  162.                         if(schemaWithOnlyImport==false){
  163.                             schemiInWsdl.add(schema);      
  164.                         }
  165.                     }
  166.                 }
  167.             }catch(Exception e){
  168.                 throw new DriverRegistroServiziException("L'accordo di servizio parte comune contiene un wsdl corrotto: "+e.getMessage(),e);
  169.             }
  170.         }
  171.         if(wsdlFruitore!=null){
  172.             try{
  173.                 wsdlUtilities = new WSDLUtilities(this.xmlUtils);
  174.                 dWsdlFruitore = this.xmlUtils.newDocument(wsdlFruitore);
  175.                 List<Node> schemi = wsdlUtilities.getSchemiXSD(dWsdlFruitore);
  176.                 if(schemi!=null && schemi.size()>0){
  177.                     for (Node schema : schemi) {
  178.                         boolean schemaWithOnlyImport = this.xsdUtils.isSchemaWithOnlyImports(schema);
  179.                         if(schemaWithOnlyImport==false){
  180.                             schemiInWsdlFruitore.add(schema);      
  181.                         }
  182.                     }
  183.                 }
  184.             }catch(Exception e){
  185.                 throw new DriverRegistroServiziException("L'accordo di servizio parte comune contiene un wsdl fruitore corrotto: "+e.getMessage(),e);
  186.             }
  187.         }
  188.        
  189.        
  190.         if( (schemiInWsdl.size()<=0) && (schemiInWsdlFruitore.size()<=0) && (!definitorioPresente) && as.sizeAllegatoList()==0 && as.sizeSpecificaSemiformaleList()==0){
  191.             throw new DriverRegistroServiziException("L'accordo di servizio parte comune non contiene schemi XSD");
  192.         }

  193.         Map<String, byte[]> resources = new HashMap<String, byte[]>();
  194.         Map<String, List<String>> mappingNamespaceLocations = new HashMap<>();

  195.        
  196.        
  197.         // --------- WSDL CONCETTUALE/LOGICO EROGATORE ---------------------
  198.         if(schemiInWsdl.size()>0){

  199.             Map<String, String> declarationNamespacesWSDL = null;
  200.             try{
  201.                 declarationNamespacesWSDL = this.xmlUtils.getNamespaceDeclaration(dWsdl.getDocumentElement());
  202.             }catch(Exception e){
  203.                 throw new DriverRegistroServiziException("L'accordo di servizio parte comune contiene un wsdl corrotto (Analisi namespace declarations): "+e.getMessage(),e);
  204.             }
  205.             for (int i = 0; i < schemiInWsdl.size(); i++) {
  206.                 Node schema = schemiInWsdl.get(i);
  207.                 try{
  208.                     if(declarationNamespacesWSDL!=null && declarationNamespacesWSDL.size()>0){
  209.                         this.xmlUtils.addNamespaceDeclaration(declarationNamespacesWSDL, (Element) schema);
  210.                     }
  211.                     byte[] resource = this.xmlUtils.toByteArray(schema);
  212.                     String systemId = "internalWsdlTypes_"+(i+1)+".xsd";
  213.                    
  214.                     // registro risorsa con systemid
  215.                     //System.out.println("ADD CONCETTUALE ["+systemId+"] ");
  216.                     resources.put(systemId, resource);

  217.                     // registro namespace
  218.                     this.xsdUtils.registraMappingNamespaceLocations(resource, systemId, mappingNamespaceLocations);
  219.                    
  220.                 }catch(Exception e){
  221.                     throw new DriverRegistroServiziException("L'accordo di servizio parte comune contiene un wsdl corrotto (Estrazione schema): "+e.getMessage(),e);
  222.                 }
  223.             }
  224.         }
  225.        
  226.         // --------- WSDL CONCETTUALE/LOGICO EROGATORE ---------------------
  227.         if(schemiInWsdlFruitore.size()>0){

  228.             Map<String, String> declarationNamespacesWSDL = null;
  229.             try{
  230.                 if(dWsdlFruitore!=null) {
  231.                     declarationNamespacesWSDL = this.xmlUtils.getNamespaceDeclaration(dWsdlFruitore.getDocumentElement());
  232.                 }
  233.             }catch(Exception e){
  234.                 throw new DriverRegistroServiziException("L'accordo di servizio parte comune contiene un wsdl corrotto (Analisi namespace declarations): "+e.getMessage(),e);
  235.             }
  236.             for (int i = 0; i < schemiInWsdlFruitore.size(); i++) {
  237.                 Node schema = schemiInWsdlFruitore.get(i);
  238.                 try{
  239.                     if(declarationNamespacesWSDL!=null && declarationNamespacesWSDL.size()>0){
  240.                         this.xmlUtils.addNamespaceDeclaration(declarationNamespacesWSDL, (Element) schema);
  241.                     }
  242.                     byte[] resource = this.xmlUtils.toByteArray(schema);
  243.                     String systemId = "internalWsdlFruitoreTypes_"+(i+1)+".xsd";
  244.                    
  245.                     // registro risorsa con systemid
  246.                     //System.out.println("ADD CONCETTUALE ["+systemId+"] ");
  247.                     resources.put(systemId, resource);

  248.                     // registro namespace
  249.                     this.xsdUtils.registraMappingNamespaceLocations(resource, systemId, mappingNamespaceLocations);
  250.                    
  251.                 }catch(Exception e){
  252.                     throw new DriverRegistroServiziException("L'accordo di servizio parte comune contiene un wsdl corrotto (Estrazione schema): "+e.getMessage(),e);
  253.                 }
  254.             }
  255.         }

  256.         // --------- WSDL DEFINITORIO ---------------------
  257.         if( (fromBytes && as.getByteWsdlDefinitorio()!=null) ||
  258.                 (!fromBytes && as.getWsdlDefinitorio()!=null)
  259.                 ){

  260.             byte[] resource = null;
  261.             String systemId = null;
  262.             if(fromBytes){
  263.                 systemId = CostantiRegistroServizi.ALLEGATO_DEFINITORIO_XSD;
  264.                 resource = as.getByteWsdlDefinitorio();
  265.             }else{
  266.                 String location = as.getWsdlDefinitorio();
  267.                 try{
  268.                     if(location.startsWith("http://") || location.startsWith("file://")){
  269.                         URL url = new URL(location);
  270.                         resource = HttpUtilities.requestHTTPFile(url.toString());
  271.                         systemId = (new File(url.getFile())).getName();
  272.                     }
  273.                     else{
  274.                         File f = new File(location);
  275.                         resource = FileSystemUtilities.readBytesFromFile(f);
  276.                         systemId = f.getName();
  277.                     }
  278.                 }catch(Exception e){
  279.                     throw new DriverRegistroServiziException("L'accordo di servizio parte comune indirizza un wsdl definitorio ["+location+"] non esistente");
  280.                 }
  281.             }

  282.             try{
  283.                 if(this.xsdUtils.isXSDSchema(resource)){

  284.                     // registro risorsa con systemid
  285.                     //System.out.println("ADD DEFINITIORIO ["+systemId+"] ");
  286.                     resources.put(systemId, resource);

  287.                     // registro namespace
  288.                     this.xsdUtils.registraMappingNamespaceLocations(resource, systemId, mappingNamespaceLocations);
  289.                 }
  290.             }catch(Exception e){
  291.                 throw new DriverRegistroServiziException("La lettura del wsdl definitorio ha causato un errore: "+e.getMessage(),e);
  292.             }
  293.         }

  294.         // --------- ALLEGATI ---------
  295.         if(as.sizeAllegatoList()>0){
  296.             for(int i=0; i<as.sizeAllegatoList();i++){

  297.                 Documento allegato = as.getAllegato(i);
  298.                 byte[] resource = null;
  299.                 String systemId = null;
  300.                 //String location = null;
  301.                 if(fromBytes){
  302.                     //location = allegato.getFile();
  303.                     systemId = allegato.getFile();
  304.                     resource = allegato.getByteContenuto();
  305.                     if(resource==null){
  306.                         throw new DriverRegistroServiziException("Allegato ["+systemId+"] non contiene i bytes che ne definiscono il contenuto");
  307.                     }
  308.                 }
  309.                 else{
  310.                     String location = allegato.getFile();
  311.                     try{
  312.                         if(location.startsWith("http://") || location.startsWith("file://")){
  313.                             URL url = new URL(location);
  314.                             resource = HttpUtilities.requestHTTPFile(url.toString());
  315.                             systemId = (new File(url.getFile())).getName();
  316.                         }
  317.                         else{
  318.                             File f = new File(location);
  319.                             resource = FileSystemUtilities.readBytesFromFile(f);
  320.                             systemId = f.getName();
  321.                         }  
  322.                     }catch(Exception e){
  323.                         throw new DriverRegistroServiziException("Allegato ["+location+"] indirizza un documento non esistente");
  324.                     }
  325.                 }

  326.                 try{
  327.                     if(this.xsdUtils.isXSDSchema(resource)){

  328.                         if(resources.containsKey(systemId)){
  329.                             throw new Exception("Esiste più di un documento xsd, registrato tra allegati e specifiche semiformali, con nome ["+systemId+"] (La validazione xsd di OpenSPCoop richiede l'utilizzo di nomi diversi)");
  330.                         }
  331.                         resources.put(systemId,resource);

  332.                         // registro namespace
  333.                         this.xsdUtils.registraMappingNamespaceLocations(resource, systemId, mappingNamespaceLocations);
  334.                     }
  335.                 }catch(Exception e){
  336.                     throw new DriverRegistroServiziException("La lettura dell'allegato ["+systemId+"] ha causato un errore: "+e.getMessage(),e);
  337.                 }

  338.             }
  339.         }


  340.         // --------- SPECIFICHE SEMIFORMALI ---------
  341.         if(as.sizeSpecificaSemiformaleList()>0){
  342.             for(int i=0; i<as.sizeSpecificaSemiformaleList();i++){

  343.                 Documento specificaSemiformale = as.getSpecificaSemiformale(i);
  344.                 byte[] resource = null;
  345.                 String systemId = null;
  346.                 //String location = null;
  347.                 if(fromBytes){
  348.                     //location = specificaSemiformale.getFile();
  349.                     systemId = specificaSemiformale.getFile();
  350.                     resource = specificaSemiformale.getByteContenuto();
  351.                     if(resource==null){
  352.                         throw new DriverRegistroServiziException("Specifica Semiformale ["+systemId+"] non contiene i bytes che ne definiscono il contenuto");
  353.                     }
  354.                 }
  355.                 else{
  356.                     String location = specificaSemiformale.getFile();
  357.                     try{
  358.                         if(location.startsWith("http://") || location.startsWith("file://")){
  359.                             URL url = new URL(location);
  360.                             resource = HttpUtilities.requestHTTPFile(url.toString());
  361.                             systemId = (new File(url.getFile())).getName();
  362.                         }
  363.                         else{
  364.                             File f = new File(location);
  365.                             resource = FileSystemUtilities.readBytesFromFile(f);
  366.                             systemId = f.getName();
  367.                         }  
  368.                     }catch(Exception e){
  369.                         throw new DriverRegistroServiziException("Specifica Semiformale ["+location+"] indirizza un documento non esistente");
  370.                     }
  371.                 }

  372.                 try{
  373.                     if(this.xsdUtils.isXSDSchema(resource)){

  374.                         if(resources.containsKey(systemId)){
  375.                             throw new Exception("Esiste più di un documento xsd, registrato tra allegati e specifiche semiformali, con nome ["+systemId+"] (La validazione xsd di OpenSPCoop richiede l'utilizzo di nomi diversi)");
  376.                         }
  377.                         resources.put(systemId,resource);

  378.                         // registro namespace
  379.                         this.xsdUtils.registraMappingNamespaceLocations(resource, systemId, mappingNamespaceLocations);
  380.                     }
  381.                 }catch(Exception e){
  382.                     throw new DriverRegistroServiziException("La lettura della specifica semiformale ["+systemId+"] ha causato un errore: "+e.getMessage(),e);
  383.                 }

  384.             }
  385.         }

  386.         try{
  387.             return this.xsdUtils.buildSchemaCollection(resources, mappingNamespaceLocations, this.logger);
  388.         }catch(Exception e){
  389.             throw new DriverRegistroServiziException(e.getMessage(),e);
  390.         }
  391.            
  392.     }

  393.    
  394. }