XMLUtils.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.manifest.utils;

  21. import java.io.ByteArrayInputStream;
  22. import java.io.ByteArrayOutputStream;
  23. import java.io.File;
  24. import java.io.FileInputStream;
  25. import java.io.InputStream;
  26. import java.io.OutputStream;

  27. import org.slf4j.Logger;
  28. import org.openspcoop2.message.OpenSPCoop2MessageFactory;
  29. import org.openspcoop2.message.xml.ValidatoreXSD;
  30. import org.openspcoop2.protocol.manifest.Openspcoop2;
  31. import org.openspcoop2.protocol.manifest.Registry;
  32. import org.openspcoop2.protocol.manifest.Web;
  33. import org.openspcoop2.protocol.manifest.constants.Costanti;
  34. import org.w3c.dom.Document;
  35. import org.w3c.dom.Element;


  36. /**
  37.  * Classe utilizzata per la generazione degli element Details inseriti in un SOAPFault generato dalla PdD in caso di errore di processamento
  38.  *
  39.  *
  40.  * @author Poli Andrea (apoli@link.it)
  41.  * @author $Author$
  42.  * @version $Rev$, $Date$
  43.  */

  44. public class XMLUtils  {

  45.     /** Validatore XSD */
  46.     static ValidatoreXSD validatoreXSD = null;
  47.     public static synchronized void initValidatoreXSD(Logger log) throws Exception{
  48.         if(XMLUtils.validatoreXSD==null){
  49.             XMLUtils.validatoreXSD = new ValidatoreXSD(OpenSPCoop2MessageFactory.getDefaultMessageFactory(),
  50.                     log,XMLUtils.class.getResourceAsStream("/protocolManifest.xsd"));
  51.         }
  52.     }
  53.     public static ValidatoreXSD getValidatoreXSD(Logger log) throws Exception{
  54.         if(XMLUtils.validatoreXSD==null){
  55.             initValidatoreXSD(log);
  56.         }
  57.         return XMLUtils.validatoreXSD;
  58.     }
  59.     public static boolean validate(Openspcoop2 openspcoop2,StringBuilder motivoErroreValidazione){
  60.        
  61.         int size = motivoErroreValidazione.length();
  62.        
  63.         if(openspcoop2.getProtocol()==null){
  64.             motivoErroreValidazione.append("Protocol non definito\n");
  65.         }
  66.         if(openspcoop2.getProtocol().getName()==null){
  67.             motivoErroreValidazione.append("Protocol.name non definito\n");
  68.         }
  69.         if(openspcoop2.getProtocol().getFactory()==null){
  70.             motivoErroreValidazione.append("Protocol.factory non definito\n");
  71.         }
  72.                
  73.         if(openspcoop2.getBinding()==null){
  74.             motivoErroreValidazione.append("Binding non definito\n");
  75.         }
  76.        
  77.         if(openspcoop2.getWeb()==null){
  78.             motivoErroreValidazione.append("Web non definito\n");
  79.         }
  80.         else{
  81.             if(openspcoop2.getWeb().sizeContextList()<=0 &&
  82.                     (openspcoop2.getWeb().getEmptyContext()==null || !openspcoop2.getWeb().getEmptyContext().getEnabled())){
  83.                 motivoErroreValidazione.append("WebContext non definito ne tramite un context ne tramite un empty context\n");
  84.             }
  85.         }
  86.        
  87.         if(openspcoop2.getRegistry()==null){
  88.             motivoErroreValidazione.append("Registry non definito\n");
  89.         }
  90.         else{
  91.             Registry reg = openspcoop2.getRegistry();
  92.            
  93.             if(reg.getOrganization()==null){
  94.                 motivoErroreValidazione.append("Registry.Organization non definito\n");
  95.             }
  96.             else{
  97.                 if(reg.getOrganization().getTypes()==null ||  reg.getOrganization().getTypes().sizeTypeList()<=0){
  98.                     motivoErroreValidazione.append("Registry.Organization.types non definito\n");
  99.                 }
  100.             }
  101.            
  102.             if(reg.getService()==null){
  103.                 motivoErroreValidazione.append("Registry.Service non definito\n");
  104.             }
  105.             else{
  106.                 if(reg.getService().getTypes()==null ||  reg.getService().getTypes().sizeTypeList()<=0){
  107.                     motivoErroreValidazione.append("Registry.Service.types non definito\n");
  108.                 }
  109.             }
  110.                        
  111.             if(reg.getVersions()==null){
  112.                 motivoErroreValidazione.append("Registry.Versions non definito\n");
  113.             }
  114.             else{
  115.                 if(reg.getVersions().sizeVersionList()<=0){
  116.                     motivoErroreValidazione.append("Registry.Versions.version non definito\n");
  117.                 }
  118.             }
  119.         }
  120.        
  121.        
  122.         if(motivoErroreValidazione.length()!=size)
  123.             return false;
  124.         else
  125.             return true;

  126.     }
  127.    
  128.    
  129.    
  130.    
  131.     /* ----- Unmarshall ----- */
  132.    
  133.     /**
  134.      * Ritorna la rappresentazione java
  135.      *
  136.      * @param m byte[]
  137.      * @return Openspcoop2
  138.      * @throws XMLUtilsException
  139.      */
  140.     public static Openspcoop2 getOpenspcoop2Manifest(Logger log,byte[] m) throws XMLUtilsException{
  141.         ByteArrayInputStream bin = null;
  142.         try{
  143.             bin = new ByteArrayInputStream(m);
  144.             return XMLUtils.getOpenspcoop2Manifest(log,bin);
  145.         }catch(Exception e){
  146.             throw new XMLUtilsException(e.getMessage(),e);
  147.         }finally{
  148.             try{
  149.                 if(bin!=null)
  150.                     bin.close();
  151.             }catch(Exception eClose){
  152.                 // close
  153.             }
  154.         }
  155.     }
  156.    
  157.     /**
  158.      * Ritorna la rappresentazione java
  159.      *
  160.      * @param m File
  161.      * @return Openspcoop2
  162.      * @throws XMLUtilsException
  163.      */
  164.     public static Openspcoop2 getOpenspcoop2Manifest(Logger log,File m) throws XMLUtilsException{
  165.         FileInputStream fin = null;
  166.         try{
  167.             fin = new FileInputStream(m);
  168.             return XMLUtils.getOpenspcoop2Manifest(log,fin);
  169.         }catch(Exception e){
  170.             throw new XMLUtilsException(e.getMessage(),e);
  171.         }finally{
  172.             try{
  173.                 if(fin!=null)
  174.                     fin.close();
  175.             }catch(Exception eClose){
  176.                 // close
  177.             }
  178.         }
  179.     }
  180.    
  181.     /**
  182.      * Ritorna la rappresentazione java
  183.      *
  184.      * @param m String
  185.      * @return Openspcoop2
  186.      * @throws XMLUtilsException
  187.      */
  188.     public static Openspcoop2 getOpenspcoop2Manifest(Logger log,String m) throws XMLUtilsException{
  189.         return XMLUtils.getOpenspcoop2Manifest(log,m.getBytes());
  190.     }
  191.    
  192.     /**
  193.      * Ritorna la rappresentazione java
  194.      *
  195.      * @param m InputStream
  196.      * @return Openspcoop2
  197.      * @throws XMLUtilsException
  198.      */
  199.     public static Openspcoop2 getOpenspcoop2Manifest(Logger log,InputStream m) throws XMLUtilsException{
  200.         try{
  201.             ByteArrayOutputStream bout = new ByteArrayOutputStream();
  202.             byte[]reads = new byte[1024];
  203.             int letti = 0;
  204.             while( (letti=m.read(reads)) != -1){
  205.                 bout.write(reads, 0, letti);
  206.             }
  207.             bout.flush();
  208.             bout.close();
  209.             byte [] xml = bout.toByteArray();
  210.            
  211.             // Validazione XSD
  212.             ByteArrayInputStream binValidazione = new ByteArrayInputStream(xml);
  213.             ValidatoreXSD validatoreXSD = XMLUtils.getValidatoreXSD(log);
  214.             validatoreXSD.valida(binValidazione);
  215.            
  216.             // trasformazione in oggetto Openspcoop2
  217.             ByteArrayInputStream binTrasformazione = new ByteArrayInputStream(xml);
  218.             return (Openspcoop2) org.openspcoop2.utils.xml.JaxbUtils.xmlToObj(binTrasformazione, Openspcoop2.class);
  219.            
  220.         }catch(Exception e){
  221.             throw new XMLUtilsException(e.getMessage(),e);
  222.         }
  223.     }

  224.    
  225.    
  226.    
  227.    
  228.     /* ----- Marshall ----- */
  229.     public static void generateOpenspcoop2Manifest(Openspcoop2 manifest,File out) throws XMLUtilsException{
  230.         try{
  231.             StringBuilder risultatoValidazione = new StringBuilder();
  232.             if(XMLUtils.validate(manifest, risultatoValidazione)==false){
  233.                 throw new Exception(risultatoValidazione.toString());
  234.             }
  235.             org.openspcoop2.utils.xml.JaxbUtils.objToXml(out.getName(),XMLUtils.generateOpenspcoop2Manifest_engine(manifest));
  236.         }catch(Exception e){
  237.             throw new XMLUtilsException(e.getMessage(),e);
  238.         }
  239.     }
  240.    
  241.     public static void generateOpenspcoop2Manifest(Openspcoop2 manifest,String fileName) throws XMLUtilsException{
  242.         try{
  243.             StringBuilder risultatoValidazione = new StringBuilder();
  244.             if(XMLUtils.validate(manifest, risultatoValidazione)==false){
  245.                 throw new Exception(risultatoValidazione.toString());
  246.             }
  247.             org.openspcoop2.utils.xml.JaxbUtils.objToXml(fileName,XMLUtils.generateOpenspcoop2Manifest_engine(manifest));
  248.         }catch(Exception e){
  249.             throw new XMLUtilsException(e.getMessage(),e);
  250.         }
  251.     }
  252.    
  253.     public static byte[] generateOpenspcoop2Manifest(Openspcoop2 manifest) throws XMLUtilsException{
  254.         try{
  255.             StringBuilder risultatoValidazione = new StringBuilder();
  256.             if(XMLUtils.validate(manifest, risultatoValidazione)==false){
  257.                 throw new Exception(risultatoValidazione.toString());
  258.             }
  259.             return XMLUtils.generateOpenspcoop2Manifest_engine(manifest);
  260.         }catch(Exception e){
  261.             throw new XMLUtilsException(e.getMessage(),e);
  262.         }
  263.     }

  264.     public static void generateOpenspcoop2Manifest(Openspcoop2 manifest,OutputStream out) throws XMLUtilsException{
  265.         try{
  266.             StringBuilder risultatoValidazione = new StringBuilder();
  267.             if(XMLUtils.validate(manifest, risultatoValidazione)==false){
  268.                 throw new Exception(risultatoValidazione.toString());
  269.             }
  270.             out.write(XMLUtils.generateOpenspcoop2Manifest_engine(manifest));
  271.             out.flush();
  272.             out.close();
  273.         }catch(Exception e){
  274.             throw new XMLUtilsException(e.getMessage(),e);
  275.         }
  276.     }
  277.    
  278.     private static byte[] generateOpenspcoop2Manifest_engine(Openspcoop2 manifest) throws XMLUtilsException{
  279.         try{
  280.             ByteArrayOutputStream bout = new ByteArrayOutputStream();
  281.             org.openspcoop2.utils.xml.JaxbUtils.objToXml(bout, Openspcoop2.class, manifest);
  282.             byte[] dichiarazione = bout.toByteArray();
  283.             return dichiarazione;
  284.         }catch(Exception e){
  285.             throw new XMLUtilsException(e.getMessage(),e);
  286.         }
  287.     }
  288.    
  289.    
  290.    
  291.    
  292.    
  293.     public static boolean isOpenspcoop2Manifest(byte [] doc){
  294.         try{
  295.             org.openspcoop2.message.xml.MessageXMLUtils xmlUtils = org.openspcoop2.message.xml.MessageXMLUtils.DEFAULT;
  296.             Document docXML = xmlUtils.newDocument(doc);
  297.             Element elemXML = docXML.getDocumentElement();
  298.             return XMLUtils.isOpenspcoop2Manifest(elemXML);
  299.         }catch(Exception e){
  300.             //System.out.println("NON e' un DOCUMENTO VALIDO: "+e.getMessage());
  301.             return false;
  302.         }
  303.     }
  304.     public static boolean isOpenspcoop2Manifest(Document docXML){
  305.         try{
  306.             Element elemXML = docXML.getDocumentElement();
  307.             return XMLUtils.isOpenspcoop2Manifest(elemXML);
  308.         }catch(Exception e){
  309.             //System.out.println("NON e' un DOCUMENTO VALIDO: "+e.getMessage());
  310.             return false;
  311.         }
  312.     }
  313.     public static boolean isOpenspcoop2Manifest(Element elemXML){
  314.         try{
  315.             //System.out.println("LOCAL["+Costanti.ROOT_LOCAL_NAME_DETTAGLIO_ECCEZIONE+"]vs["+elemXML.getLocalName()+"]  NAMESPACE["+Costanti.TARGET_NAMESPACE+"]vs["+elemXML.getNamespaceURI()+"]");
  316.             if(Costanti.ROOT_LOCAL_NAME_OPENSPCOOP2.equals(elemXML.getLocalName()) &&
  317.                     Costanti.TARGET_NAMESPACE.equals(elemXML.getNamespaceURI() )
  318.                 ){
  319.                 return true;
  320.             }
  321.             else{
  322.                 return false;
  323.             }
  324.         }catch(Exception e){
  325.             //System.out.println("NON e' un DOCUMENTO VALIDO: "+e.getMessage());
  326.             return false;
  327.         }
  328.     }
  329.    
  330.    

  331.    
  332.    
  333.    
  334.    
  335.     public static String toString(Openspcoop2 manifest){
  336.         StringBuilder bf = new StringBuilder();
  337.        
  338.         bf.append(" Protocollo "+manifest.getProtocol().getName()+" \n");
  339.        
  340.         bf.append("Factory["+manifest.getProtocol().getFactory()+"]\n");
  341.        
  342.         bf.append("Default Binding ["+manifest.getBinding().getDefault()+"]");
  343.         bf.append("Binding SOAP ["+(manifest.getBinding().getSoap()!=null)+"]\n");
  344.         bf.append("Binding REST ["+(manifest.getBinding().getRest()!=null)+"]\n");
  345.        
  346.         Web web = manifest.getWeb();
  347.         for (int i = 0; i < web.sizeContextList(); i++) {
  348.             bf.append("Context["+i+"]=["+web.getContext(i).getName()+"]\n");
  349.         }
  350.         if(web.getEmptyContext()!=null){
  351.             bf.append("EmptyContext=["+web.getEmptyContext().getEnabled()+"]\n");
  352.         }
  353.         else{
  354.             bf.append("EmptyContext not defined\n");
  355.         }
  356.        
  357.         Registry reg = manifest.getRegistry();
  358.        
  359.         if(reg.getOrganization()!=null && reg.getOrganization().getTypes()!=null){
  360.             for (int i = 0; i < reg.getOrganization().getTypes().sizeTypeList(); i++) {
  361.                 bf.append("Organization.type["+i+"]=["+reg.getOrganization().getTypes().getType(i).getName()+"]\n");
  362.             }
  363.         }
  364.        
  365.         if(reg.getService()!=null && reg.getService().getTypes()!=null){
  366.             for (int i = 0; i < reg.getService().getTypes().sizeTypeList(); i++) {
  367.                 bf.append("Services.type["+i+"]=["+reg.getService().getTypes().getType(i).getName()+"]\n");
  368.             }
  369.         }
  370.                
  371.         if(reg.getVersions()!=null){
  372.             for (int i = 0; i < reg.getVersions().sizeVersionList(); i++) {
  373.                 bf.append("Versions["+i+"]=["+reg.getVersions().getVersion(i).getName()+"]\n");
  374.             }
  375.         }
  376.        
  377.         return bf.toString();
  378.     }
  379. }