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 it.gov.spcoop.sica.wsbl.driver;

  21. import it.cnipa.collprofiles.driver.XMLUtilsException;
  22. import it.gov.spcoop.sica.wsbl.ConceptualBehavior;
  23. import it.gov.spcoop.sica.wsbl.MessageBehavior;
  24. import it.gov.spcoop.sica.wsbl.TransitionType;

  25. import java.io.ByteArrayInputStream;
  26. import java.io.ByteArrayOutputStream;
  27. import java.io.File;
  28. import java.io.FileInputStream;
  29. import java.io.FileOutputStream;
  30. import java.io.InputStream;
  31. import java.io.OutputStream;

  32. import org.slf4j.Logger;
  33. import org.openspcoop2.utils.xml.AbstractXMLUtils;
  34. import org.openspcoop2.utils.xml.JaxbUtils;
  35. import org.openspcoop2.utils.xml.ValidatoreXSD;
  36. import org.w3c.dom.Document;
  37. import org.w3c.dom.Element;


  38. /**
  39.  * Classe utilizzata per lavorare sul documento semiformale che contiene le informazioni eGov dei servizi di un accordo
  40.  *
  41.  *
  42.  * @author Poli Andrea (apoli@link.it)
  43.  * @author $Author$
  44.  * @version $Rev$, $Date$
  45.  */

  46. public class XMLUtils  {

  47.     /** Validatore XSD */
  48.     static ValidatoreXSD validatoreXSD = null;
  49.     public static synchronized ValidatoreXSD getValidatoreXSD(Logger log) throws Exception{
  50.         if(XMLUtils.validatoreXSD==null){
  51.             XMLUtils.validatoreXSD = new ValidatoreXSD(log,XMLUtils.class.getResourceAsStream("/WSBL_originale.xsd"));
  52.         }
  53.         return XMLUtils.validatoreXSD;
  54.     }
  55.     public static boolean validate(ConceptualBehavior conceptual,StringBuilder motivoErroreValidazione){
  56.        
  57.         int size = motivoErroreValidazione.length();
  58.        
  59.         // TODO
  60.        
  61.         if(motivoErroreValidazione.length()!=size)
  62.             return false;
  63.         else
  64.             return true;

  65.     }
  66.     public static boolean validate(MessageBehavior conceptual,StringBuilder motivoErroreValidazione){
  67.        
  68.         int size = motivoErroreValidazione.length();
  69.        
  70.         // TODO
  71.        
  72.         if(motivoErroreValidazione.length()!=size)
  73.             return false;
  74.         else
  75.             return true;

  76.     }
  77.    
  78.    
  79.    
  80.    
  81.     /* ----- Unmarshall ----- */
  82.    
  83.     /**
  84.      * Ritorna la rappresentazione java
  85.      *
  86.      * @param m byte[]
  87.      * @return EgovDeclType
  88.      * @throws XMLUtilsException
  89.      */
  90.     public static ConceptualBehavior getConceptualBehavior(Logger log,byte[] m) throws XMLUtilsException{
  91.         ByteArrayInputStream bin = null;
  92.         try{
  93.             bin = new ByteArrayInputStream(m);
  94.             return XMLUtils.getConceptualBehavior(log,bin);
  95.         }catch(Exception e){
  96.             throw new XMLUtilsException(e.getMessage(),e);
  97.         }finally{
  98.             try{
  99.                 if(bin!=null)
  100.                     bin.close();
  101.             }catch(Exception eClose){
  102.                 // close
  103.             }
  104.         }
  105.     }
  106.    
  107.     /**
  108.      * Ritorna la rappresentazione java
  109.      *
  110.      * @param m File
  111.      * @return EgovDeclType
  112.      * @throws XMLUtilsException
  113.      */
  114.     public static ConceptualBehavior getConceptualBehavior(Logger log,File m) throws XMLUtilsException{
  115.         FileInputStream fin = null;
  116.         try{
  117.             fin = new FileInputStream(m);
  118.             return XMLUtils.getConceptualBehavior(log,fin);
  119.         }catch(Exception e){
  120.             throw new XMLUtilsException(e.getMessage(),e);
  121.         }finally{
  122.             try{
  123.                 if(fin!=null)
  124.                     fin.close();
  125.             }catch(Exception eClose){
  126.                 // close
  127.             }
  128.         }
  129.     }
  130.    
  131.     /**
  132.      * Ritorna la rappresentazione java
  133.      *
  134.      * @param m String
  135.      * @return EgovDeclType
  136.      * @throws XMLUtilsException
  137.      */
  138.     public static ConceptualBehavior getConceptualBehavior(Logger log,String m) throws XMLUtilsException{
  139.         return XMLUtils.getConceptualBehavior(log,m.getBytes());
  140.     }
  141.    
  142.     /**
  143.      * Ritorna la rappresentazione java
  144.      *
  145.      * @param m InputStream
  146.      * @return EgovDeclType
  147.      * @throws XMLUtilsException
  148.      */
  149.     public static ConceptualBehavior getConceptualBehavior(Logger log,InputStream m) throws XMLUtilsException{
  150.         try{
  151.             ByteArrayOutputStream bout = new ByteArrayOutputStream();
  152.             byte[]reads = new byte[1024];
  153.             int letti = 0;
  154.             while( (letti=m.read(reads)) != -1){
  155.                 bout.write(reads, 0, letti);
  156.             }
  157.             bout.flush();
  158.             bout.close();
  159.             byte [] xml = XMLUtils.unescapeXMLConceptualBehaviorForClientSICA(bout.toString());
  160.             ByteArrayInputStream binValidazione = new ByteArrayInputStream(xml);
  161.            
  162.             ValidatoreXSD validatoreXSD = XMLUtils.getValidatoreXSD(log);
  163.             validatoreXSD.valida(binValidazione);
  164.            
  165.             ByteArrayInputStream binTrasformazione = new ByteArrayInputStream(xml);
  166.            
  167.             return (ConceptualBehavior) JaxbUtils.xmlToObj(binTrasformazione, ConceptualBehavior.class);
  168.         }catch(Exception e){
  169.             throw new XMLUtilsException(e.getMessage(),e);
  170.         }
  171.     }
  172.    
  173.     private static byte[] unescapeXMLConceptualBehaviorForClientSICA(String xml) throws Exception{
  174.         String xmlS = new String(xml);
  175.         xmlS = xmlS.replace("<wsbl:ConceptualBehavior","<ConceptualBehavior");
  176.         //xmlS = xmlS.replace("xmlns:wscp=\"http://spcoop.gov.it/sica/wscp\" xmlns","xmlns");
  177.         xmlS = xmlS.replace("xmlns:wsbl","xmlns");
  178.         xmlS = xmlS.replace("</wsbl:ConceptualBehavior","</ConceptualBehavior");
  179.         return xmlS.getBytes();
  180.     }
  181.    
  182.    
  183.    
  184.     /* ----- Marshall Manifest dell'accordo di servizio ----- */
  185.     public static void generateConceptualBehavior(ConceptualBehavior manifest,File out) throws XMLUtilsException{
  186.         FileOutputStream fout = null;
  187.         try{
  188.             fout = new FileOutputStream(out);
  189.             byte[] xml = XMLUtils.escapeXMLConceptualBehaviorForClientSICA(manifest);
  190.             fout.write(xml);
  191.             fout.flush();      
  192.         }catch(Exception e){
  193.             throw new XMLUtilsException(e.getMessage(),e);
  194.         }finally{
  195.             if(fout!=null){
  196.                 try{
  197.                     fout.close();
  198.                 }catch(Exception e){
  199.                     // close
  200.                 }
  201.             }
  202.         }
  203.     }
  204.    
  205.     public static void generateConceptualBehavior(ConceptualBehavior manifest,String fileName) throws XMLUtilsException{
  206.         XMLUtils.generateConceptualBehavior(manifest,new File(fileName));
  207.     }
  208.    
  209.     public static byte[] generateConceptualBehavior(ConceptualBehavior manifest) throws XMLUtilsException{
  210.         try{
  211.             return XMLUtils.escapeXMLConceptualBehaviorForClientSICA(manifest);
  212.         }catch(Exception e){
  213.             throw new XMLUtilsException(e.getMessage(),e);
  214.         }
  215.     }

  216.     public static void generateConceptualBehavior(ConceptualBehavior manifest,OutputStream out) throws XMLUtilsException{
  217.         try{
  218.             byte[] xml =  XMLUtils.escapeXMLConceptualBehaviorForClientSICA(manifest);
  219.             out.write(xml);    
  220.         }catch(Exception e){
  221.             throw new XMLUtilsException(e.getMessage(),e);
  222.         }
  223.     }

  224.     private static byte[] escapeXMLConceptualBehaviorForClientSICA(ConceptualBehavior manifest) throws Exception{
  225.         StringBuilder risultatoValidazione = new StringBuilder();
  226.         if(XMLUtils.validate(manifest, risultatoValidazione)==false){
  227.             throw new Exception(risultatoValidazione.toString());
  228.         }
  229.        
  230.         ByteArrayOutputStream bout = new ByteArrayOutputStream();
  231.         JaxbUtils.objToXml(bout, ConceptualBehavior.class, manifest);
  232.         String xml = bout.toString();
  233.         xml = xml.replace("<ConceptualBehavior", "<wsbl:ConceptualBehavior");
  234.         //xml = xml.replace("xmlns", "xmlns:wscp=\"http://spcoop.gov.it/sica/wscp\" xmlns");
  235.         xml = xml.replace("xmlns", "xmlns:wsbl");
  236.         xml = xml.replace("</ConceptualBehavior", "</wsbl:ConceptualBehavior");
  237.         return xml.getBytes();
  238.     }

  239.    
  240.    
  241.    
  242.    

  243.     public static boolean isConceptualBehavior(byte [] doc){
  244.         try{
  245.             AbstractXMLUtils xmlUtils = org.openspcoop2.message.xml.MessageXMLUtils.DEFAULT;        
  246.             Document docXML = xmlUtils.newDocument(doc);
  247.             Element elemXML = docXML.getDocumentElement();
  248.             //System.out.println("LOCAL["+Costanti.ROOT_LOCAL_NAME+"]vs["+elemXML.getLocalName()+"]  NAMESPACE["+Costanti.TARGET_NAMESPACE+"]vs["+elemXML.getNamespaceURI()+"]");
  249.             if(Costanti.ROOT_CONCEPTUAL_LOCAL_NAME.equals(elemXML.getLocalName()) &&
  250.                     Costanti.TARGET_NAMESPACE.equals(elemXML.getNamespaceURI()) ){
  251.                 return true;
  252.             }
  253.             else{
  254.                 return false;
  255.             }
  256.         }catch(Exception e){
  257.             //System.out.println("NON e' un DOCUMENTO VALIDO: "+e.getMessage());
  258.             return false;
  259.         }
  260.     }
  261.    
  262.    
  263.     public static String[] getOperazioniAsincrone(ConceptualBehavior wsblConcettuale) throws XMLUtilsException{
  264.         try{
  265.             String [] operationsAsincrone = new String[2];
  266.             String statoIniziale = null;
  267.             String statoFinale = null;
  268.             if(wsblConcettuale.getStates()==null){
  269.                 throw new Exception("Non sono presenti stati nel documento WSBL ConceptualBehavior");
  270.             }else{
  271.                 if(wsblConcettuale.getStates().getStateInitial()==null || wsblConcettuale.getStates().getStateInitial().getName()==null){
  272.                     throw new Exception("Stato iniziale non presente nel documento WSBL ConceptualBehavior");
  273.                 }
  274.                 statoIniziale = wsblConcettuale.getStates().getStateInitial().getName();
  275.                 if(wsblConcettuale.getStates().getStateFinal()==null || wsblConcettuale.getStates().getStateFinal().getName()==null){
  276.                     throw new Exception("Stato finale non presente nel documento WSBL ConceptualBehavior");
  277.                 }
  278.                 statoFinale = wsblConcettuale.getStates().getStateFinal().getName();
  279.             }
  280.            
  281.             if(wsblConcettuale.getTransitions()==null || wsblConcettuale.getTransitions().sizeTransitionList()<=0 ){
  282.                 throw new Exception("Non sono presenti transizioni nel documento WSBL ConceptualBehavior");
  283.             }else{
  284.                 for(int i=0; i<wsblConcettuale.getTransitions().sizeTransitionList(); i++){
  285.                     TransitionType tr = wsblConcettuale.getTransitions().getTransition(i);
  286.                     if(tr.getSource()==null){
  287.                         throw new Exception("Presente una transizione con source non definita nel documento WSBL ConceptualBehavior");
  288.                     }
  289.                     if(tr.getTarget()==null){
  290.                         throw new Exception("Presente una transizione con target non definito nel documento WSBL ConceptualBehavior");
  291.                     }
  292.                     if(tr.getSource().equals(statoIniziale)){
  293.                         if(tr.getEvents()==null || tr.getEvents().sizeEventList()<=0){
  294.                             throw new Exception("Presente una transizione senza eventi nel documento WSBL ConceptualBehavior");
  295.                         }
  296.                         operationsAsincrone[0] = tr.getEvents().getEvent(0).getName();
  297.                         if(operationsAsincrone[0]==null){
  298.                             throw new Exception("Presente una transizione con un evento senza nome nel documento WSBL ConceptualBehavior");
  299.                         }
  300.                         continue;
  301.                     }
  302.                     if(tr.getTarget().equals(statoFinale)){
  303.                         if(tr.getEvents()==null || tr.getEvents().sizeEventList()<=0){
  304.                             throw new Exception("Presente una transizione senza eventi nel documento WSBL ConceptualBehavior");
  305.                         }
  306.                         operationsAsincrone[1] = tr.getEvents().getEvent(0).getName();
  307.                         if(operationsAsincrone[1]==null){
  308.                             throw new Exception("Presente una transizione con un evento senza nome nel documento WSBL ConceptualBehavior");
  309.                         }
  310.                         continue;
  311.                     }
  312.                 }
  313.                 if(operationsAsincrone[0]==null){
  314.                     throw new Exception("Non e' stata trovata una transizione che possiede come source lo stato iniziale "+statoIniziale+" nel documento WSBL ConceptualBehavior");
  315.                 }
  316.                 if(operationsAsincrone[1]==null){
  317.                     throw new Exception("Non e' stata trovata una transizione che possiede come target lo stato finale "+statoFinale+" nel documento WSBL ConceptualBehavior");
  318.                 }
  319.                 return operationsAsincrone;
  320.             }
  321.            
  322.         }catch(Exception e){
  323.             throw new XMLUtilsException(e.getMessage(),e);
  324.         }
  325.     }
  326.    
  327.    
  328.    
  329.    
  330.    
  331.    
  332.    
  333.     /* ----- Unmarshall ----- */
  334.    
  335.     /**
  336.      * Ritorna la rappresentazione java
  337.      *
  338.      * @param m byte[]
  339.      * @return EgovDeclType
  340.      * @throws XMLUtilsException
  341.      */
  342.     public static MessageBehavior getMessageBehavior(Logger log,byte[] m) throws XMLUtilsException{
  343.         ByteArrayInputStream bin = null;
  344.         try{
  345.             bin = new ByteArrayInputStream(m);
  346.             return XMLUtils.getMessageBehavior(log,bin);
  347.         }catch(Exception e){
  348.             throw new XMLUtilsException(e.getMessage(),e);
  349.         }finally{
  350.             try{
  351.                 if(bin!=null)
  352.                     bin.close();
  353.             }catch(Exception eClose){
  354.                 // close
  355.             }
  356.         }
  357.     }
  358.    
  359.     /**
  360.      * Ritorna la rappresentazione java
  361.      *
  362.      * @param m File
  363.      * @return EgovDeclType
  364.      * @throws XMLUtilsException
  365.      */
  366.     public static MessageBehavior getMessageBehavior(Logger log,File m) throws XMLUtilsException{
  367.         FileInputStream fin = null;
  368.         try{
  369.             fin = new FileInputStream(m);
  370.             return XMLUtils.getMessageBehavior(log,fin);
  371.         }catch(Exception e){
  372.             throw new XMLUtilsException(e.getMessage(),e);
  373.         }finally{
  374.             try{
  375.                 if(fin!=null)
  376.                     fin.close();
  377.             }catch(Exception eClose){
  378.                 // close
  379.             }
  380.         }
  381.     }
  382.    
  383.     /**
  384.      * Ritorna la rappresentazione java
  385.      *
  386.      * @param m String
  387.      * @return EgovDeclType
  388.      * @throws XMLUtilsException
  389.      */
  390.     public static MessageBehavior getMessageBehavior(Logger log,String m) throws XMLUtilsException{
  391.         return XMLUtils.getMessageBehavior(log,m.getBytes());
  392.     }
  393.    
  394.     /**
  395.      * Ritorna la rappresentazione java
  396.      *
  397.      * @param m InputStream
  398.      * @return EgovDeclType
  399.      * @throws XMLUtilsException
  400.      */
  401.     public static MessageBehavior getMessageBehavior(Logger log,InputStream m) throws XMLUtilsException{
  402.         try{
  403.             ByteArrayOutputStream bout = new ByteArrayOutputStream();
  404.             byte[]reads = new byte[1024];
  405.             int letti = 0;
  406.             while( (letti=m.read(reads)) != -1){
  407.                 bout.write(reads, 0, letti);
  408.             }
  409.             bout.flush();
  410.             bout.close();
  411.             byte [] xml = XMLUtils.unescapeXMLMessageBehaviorForClientSICA(bout.toString());
  412.             ByteArrayInputStream binValidazione = new ByteArrayInputStream(xml);
  413.            
  414.             ValidatoreXSD validatoreXSD = XMLUtils.getValidatoreXSD(log);
  415.             validatoreXSD.valida(binValidazione);
  416.            
  417.             ByteArrayInputStream binTrasformazione = new ByteArrayInputStream(xml);
  418.            
  419.             return (MessageBehavior) JaxbUtils.xmlToObj(binTrasformazione, MessageBehavior.class);
  420.         }catch(Exception e){
  421.             throw new XMLUtilsException(e.getMessage(),e);
  422.         }
  423.     }
  424.    
  425.     private static byte[] unescapeXMLMessageBehaviorForClientSICA(String xml) throws Exception{
  426.         String xmlS = new String(xml);
  427.         xmlS = xmlS.replace("<wsbl:MessageBehavior","<MessageBehavior");
  428.         //xmlS = xmlS.replace("xmlns:wscp=\"http://spcoop.gov.it/sica/wscp\" xmlns","xmlns");
  429.         xmlS = xmlS.replace("xmlns:wsbl","xmlns");
  430.         xmlS = xmlS.replace("</wsbl:MessageBehavior","</MessageBehavior");
  431.         return xmlS.getBytes();
  432.     }
  433.    
  434.    
  435.    
  436.    
  437.    
  438.     /* ----- Marshall Manifest dell'accordo di servizio ----- */
  439.     public static void generateMessageBehavior(MessageBehavior manifest,File out) throws XMLUtilsException{
  440.         FileOutputStream fout = null;
  441.         try{
  442.             fout = new FileOutputStream(out);
  443.             byte[] xml = XMLUtils.escapeXMLMessageBehaviorForClientSICA(manifest);
  444.             fout.write(xml);
  445.             fout.flush();      
  446.         }catch(Exception e){
  447.             throw new XMLUtilsException(e.getMessage(),e);
  448.         }finally{
  449.             if(fout!=null){
  450.                 try{
  451.                     fout.close();
  452.                 }catch(Exception e){
  453.                     // close
  454.                 }
  455.             }
  456.         }
  457.     }
  458.    
  459.     public static void generateMessageBehavior(MessageBehavior manifest,String fileName) throws XMLUtilsException{
  460.         XMLUtils.generateMessageBehavior(manifest,new File(fileName));
  461.     }
  462.    
  463.     public static byte[] generateMessageBehavior(MessageBehavior manifest) throws XMLUtilsException{
  464.         try{
  465.             return XMLUtils.escapeXMLMessageBehaviorForClientSICA(manifest);
  466.         }catch(Exception e){
  467.             throw new XMLUtilsException(e.getMessage(),e);
  468.         }
  469.     }

  470.     public static void generateMessageBehavior(MessageBehavior manifest,OutputStream out) throws XMLUtilsException{
  471.         try{
  472.             byte[] xml = XMLUtils.escapeXMLMessageBehaviorForClientSICA(manifest);
  473.             out.write(xml);    
  474.         }catch(Exception e){
  475.             throw new XMLUtilsException(e.getMessage(),e);
  476.         }
  477.     }
  478.    
  479.     private static byte[] escapeXMLMessageBehaviorForClientSICA(MessageBehavior manifest) throws Exception{
  480.         StringBuilder risultatoValidazione = new StringBuilder();
  481.         if(XMLUtils.validate(manifest, risultatoValidazione)==false){
  482.             throw new Exception(risultatoValidazione.toString());
  483.         }
  484.        
  485.         ByteArrayOutputStream bout = new ByteArrayOutputStream();
  486.         JaxbUtils.objToXml(bout, MessageBehavior.class, manifest);
  487.         String xml = bout.toString();
  488.         xml = xml.replace("<MessageBehavior", "<wsbl:MessageBehavior");
  489.         //xml = xml.replace("xmlns", "xmlns:wscp=\"http://spcoop.gov.it/sica/wscp\" xmlns");
  490.         xml = xml.replace("xmlns", "xmlns:wsbl");
  491.         xml = xml.replace("</MessageBehavior", "</wsbl:MessageBehavior");
  492.         return xml.getBytes();
  493.     }
  494.    
  495.     public static boolean isMessageBehavior(byte [] doc){
  496.         try{
  497.             AbstractXMLUtils xmlUtils = org.openspcoop2.message.xml.MessageXMLUtils.DEFAULT;        
  498.             Document docXML = xmlUtils.newDocument(doc);
  499.             Element elemXML = docXML.getDocumentElement();
  500.             //System.out.println("LOCAL["+Costanti.ROOT_LOCAL_NAME+"]vs["+elemXML.getLocalName()+"]  NAMESPACE["+Costanti.TARGET_NAMESPACE+"]vs["+elemXML.getNamespaceURI()+"]");
  501.             if(Costanti.ROOT_MESSAGE_LOCAL_NAME.equals(elemXML.getLocalName()) &&
  502.                     Costanti.TARGET_NAMESPACE.equals(elemXML.getNamespaceURI()) ){
  503.                 return true;
  504.             }
  505.             else{
  506.                 return false;
  507.             }
  508.         }catch(Exception e){
  509.             //System.out.println("NON e' un DOCUMENTO VALIDO: "+e.getMessage());
  510.             return false;
  511.         }
  512.     }
  513.    
  514.    
  515. }