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.core.eccezione.router_details.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 java.util.Iterator;

  28. import javax.xml.namespace.QName;
  29. import javax.xml.soap.SOAPBody;
  30. import javax.xml.soap.SOAPElement;
  31. import javax.xml.soap.SOAPFault;

  32. import org.openspcoop2.core.eccezione.router_details.DettaglioRouting;
  33. import org.openspcoop2.core.eccezione.router_details.Dominio;
  34. import org.openspcoop2.core.eccezione.router_details.constants.Costanti;
  35. import org.openspcoop2.message.OpenSPCoop2MessageFactory;
  36. import org.openspcoop2.message.OpenSPCoop2SoapMessage;
  37. import org.openspcoop2.message.xml.ValidatoreXSD;
  38. import org.slf4j.Logger;
  39. import org.w3c.dom.Document;
  40. import org.w3c.dom.Element;
  41. import org.w3c.dom.Node;


  42. /**
  43.  * Classe utilizzata per la generazione il detail errore-applicativo inseriti in un SOAPFault generato dalla PdD in caso di errore di processamento
  44.  *
  45.  *
  46.  * @author Poli Andrea (apoli@link.it)
  47.  * @author $Author$
  48.  * @version $Rev$, $Date$
  49.  */

  50. public class XMLUtils  {

  51.     /** Validatore XSD */
  52.     static ValidatoreXSD validatoreXSD = null;
  53.     private static synchronized void initValidatoreXSD(Logger log) throws Exception{
  54.         if(XMLUtils.validatoreXSD==null){
  55.             XMLUtils.validatoreXSD = new ValidatoreXSD(OpenSPCoop2MessageFactory.getDefaultMessageFactory(), log,XMLUtils.class.getResourceAsStream("/openspcoopRouterDetail.xsd"));
  56.         }
  57.     }
  58.     public static ValidatoreXSD getValidatoreXSD(Logger log) throws Exception{
  59.         if(XMLUtils.validatoreXSD==null){
  60.             initValidatoreXSD(log);
  61.         }
  62.         return XMLUtils.validatoreXSD;
  63.     }
  64.     public static boolean validate(DettaglioRouting dettaglioRouting,StringBuilder motivoErroreValidazione){
  65.        
  66.         int size = motivoErroreValidazione.length();
  67.        
  68.         if(dettaglioRouting.getDomain()==null){
  69.             motivoErroreValidazione.append("Dominio non definito\n");
  70.         }
  71.         else{
  72.             validate(dettaglioRouting.getDomain(), motivoErroreValidazione);
  73.         }
  74.        
  75.         if(dettaglioRouting.getTimestamp()==null){
  76.             motivoErroreValidazione.append("OraRegistrazione non definita\n");
  77.         }
  78.        
  79.         if(dettaglioRouting.getDetail()==null){
  80.             motivoErroreValidazione.append("Dettaglio non definita\n");
  81.         }
  82.         else{
  83.             if(dettaglioRouting.getDetail().getDescription()==null){
  84.                 motivoErroreValidazione.append("Dettaglio.descrizione non definita\n");
  85.             }
  86.             if(dettaglioRouting.getDetail().getState()==null){
  87.                 motivoErroreValidazione.append("Dettaglio.esito non definita\n");
  88.             }
  89.         }
  90.        
  91.         if(motivoErroreValidazione.length()!=size)
  92.             return false;
  93.         else
  94.             return true;

  95.     }
  96.     private static void validate(Dominio dominio,StringBuilder motivoErroreValidazione){
  97.         if(dominio.getId()==null){
  98.             motivoErroreValidazione.append("Dominio.identificativoPorta non definito\n");
  99.         }
  100.         if(dominio.getModule()==null){
  101.             motivoErroreValidazione.append("Dominio.modulo non definito\n");
  102.         }
  103.         if(dominio.getOrganization()==null){
  104.             motivoErroreValidazione.append("Dominio.soggetto non definita\n");
  105.         }
  106.         else{
  107.             if(dominio.getOrganization().getType()==null){
  108.                 motivoErroreValidazione.append("Dominio.soggetto.tipo non definita\n");
  109.             }
  110.             if(dominio.getOrganization().getBase()==null){
  111.                 motivoErroreValidazione.append("Dominio.soggetto.nome non definita\n");
  112.             }
  113.         }
  114.     }
  115.    
  116.    
  117.    
  118.     /* ----- Unmarshall ----- */
  119.    
  120.     /**
  121.      * Ritorna la rappresentazione java
  122.      *
  123.      * @param m byte[]
  124.      * @return DettaglioRouting
  125.      * @throws XMLUtilsException
  126.      */
  127.     public static DettaglioRouting getDettaglioRouting(Logger log,byte[] m) throws XMLUtilsException{
  128.         ByteArrayInputStream bin = null;
  129.         try{
  130.             bin = new ByteArrayInputStream(m);
  131.             return XMLUtils.getDettaglioRouting(log,bin);
  132.         }catch(Exception e){
  133.             throw new XMLUtilsException(e.getMessage(),e);
  134.         }finally{
  135.             try{
  136.                 if(bin!=null)
  137.                     bin.close();
  138.             }catch(Exception eClose){
  139.                 // close
  140.             }
  141.         }
  142.     }
  143.    
  144.     /**
  145.      * Ritorna la rappresentazione java
  146.      *
  147.      * @param m File
  148.      * @return DettaglioRouting
  149.      * @throws XMLUtilsException
  150.      */
  151.     public static DettaglioRouting getDettaglioRouting(Logger log,File m) throws XMLUtilsException{
  152.         FileInputStream fin = null;
  153.         try{
  154.             fin = new FileInputStream(m);
  155.             return XMLUtils.getDettaglioRouting(log,fin);
  156.         }catch(Exception e){
  157.             throw new XMLUtilsException(e.getMessage(),e);
  158.         }finally{
  159.             try{
  160.                 if(fin!=null)
  161.                     fin.close();
  162.             }catch(Exception eClose){
  163.                 // close
  164.             }
  165.         }
  166.     }
  167.    
  168.     /**
  169.      * Ritorna la rappresentazione java
  170.      *
  171.      * @param m String
  172.      * @return DettaglioRouting
  173.      * @throws XMLUtilsException
  174.      */
  175.     public static DettaglioRouting getDettaglioRouting(Logger log,String m) throws XMLUtilsException{
  176.         return XMLUtils.getDettaglioRouting(log,m.getBytes());
  177.     }
  178.    
  179.     /**
  180.      * Ritorna la rappresentazione java
  181.      *
  182.      * @param m InputStream
  183.      * @return DettaglioRouting
  184.      * @throws XMLUtilsException
  185.      */
  186.     public static DettaglioRouting getDettaglioRouting(Logger log,InputStream m) throws XMLUtilsException{
  187.         try{
  188.             ByteArrayOutputStream bout = new ByteArrayOutputStream();
  189.             byte[]reads = new byte[1024];
  190.             int letti = 0;
  191.             while( (letti=m.read(reads)) != -1){
  192.                 bout.write(reads, 0, letti);
  193.             }
  194.             bout.flush();
  195.             bout.close();
  196.             byte [] xml = bout.toByteArray();
  197.            
  198.             // Validazione XSD
  199.             ByteArrayInputStream binValidazione = new ByteArrayInputStream(xml);
  200.             ValidatoreXSD validatoreXSD = XMLUtils.getValidatoreXSD(log);
  201.             validatoreXSD.valida(binValidazione);
  202.            
  203.             // trasformazione in oggetto DettaglioRouting
  204.             ByteArrayInputStream binTrasformazione = new ByteArrayInputStream(xml);
  205.             return (DettaglioRouting) org.openspcoop2.utils.xml.JaxbUtils.xmlToObj(binTrasformazione, DettaglioRouting.class);
  206.            
  207.         }catch(Exception e){
  208.             throw new XMLUtilsException(e.getMessage(),e);
  209.         }
  210.     }

  211.    
  212.    
  213.    
  214.    
  215.     /* ----- Marshall ----- */
  216.     public static void generateDettaglioRouting(DettaglioRouting eccezione,File out) throws XMLUtilsException{
  217.         try{
  218.             StringBuilder risultatoValidazione = new StringBuilder();
  219.             if(XMLUtils.validate(eccezione, risultatoValidazione)==false){
  220.                 throw new Exception(risultatoValidazione.toString());
  221.             }
  222.             org.openspcoop2.utils.xml.JaxbUtils.objToXml(out.getName(),XMLUtils.generateDettaglioRouting_engine(eccezione));
  223.         }catch(Exception e){
  224.             throw new XMLUtilsException(e.getMessage(),e);
  225.         }
  226.     }
  227.    
  228.     public static void generateDettaglioRouting(DettaglioRouting eccezione,String fileName) throws XMLUtilsException{
  229.         try{
  230.             StringBuilder risultatoValidazione = new StringBuilder();
  231.             if(XMLUtils.validate(eccezione, risultatoValidazione)==false){
  232.                 throw new Exception(risultatoValidazione.toString());
  233.             }
  234.             org.openspcoop2.utils.xml.JaxbUtils.objToXml(fileName,XMLUtils.generateDettaglioRouting_engine(eccezione));
  235.         }catch(Exception e){
  236.             throw new XMLUtilsException(e.getMessage(),e);
  237.         }
  238.     }
  239.    
  240.     public static byte[] generateDettaglioRouting(DettaglioRouting eccezione) throws XMLUtilsException{
  241.         try{
  242.             StringBuilder risultatoValidazione = new StringBuilder();
  243.             if(XMLUtils.validate(eccezione, risultatoValidazione)==false){
  244.                 throw new Exception(risultatoValidazione.toString());
  245.             }
  246.             return XMLUtils.generateDettaglioRouting_engine(eccezione);
  247.         }catch(Exception e){
  248.             throw new XMLUtilsException(e.getMessage(),e);
  249.         }
  250.     }

  251.     public static void generateDettaglioRouting(DettaglioRouting eccezione,OutputStream out) throws XMLUtilsException{
  252.         try{
  253.             StringBuilder risultatoValidazione = new StringBuilder();
  254.             if(XMLUtils.validate(eccezione, risultatoValidazione)==false){
  255.                 throw new Exception(risultatoValidazione.toString());
  256.             }
  257.             out.write(XMLUtils.generateDettaglioRouting_engine(eccezione));
  258.             out.flush();
  259.             out.close();
  260.         }catch(Exception e){
  261.             throw new XMLUtilsException(e.getMessage(),e);
  262.         }
  263.     }
  264.    
  265.     private static byte[] generateDettaglioRouting_engine(DettaglioRouting eccezione) throws XMLUtilsException{
  266.         try{
  267.             ByteArrayOutputStream bout = new ByteArrayOutputStream();
  268.             org.openspcoop2.utils.xml.JaxbUtils.objToXml(bout, DettaglioRouting.class, eccezione);
  269.             byte[] dichiarazione = bout.toByteArray();
  270.             return dichiarazione;
  271.         }catch(Exception e){
  272.             throw new XMLUtilsException(e.getMessage(),e);
  273.         }
  274.     }
  275.    
  276.    
  277.    
  278.    
  279.     public static DettaglioRouting getDettaglioRouting(Logger log,OpenSPCoop2SoapMessage msg)throws XMLUtilsException{
  280.         try{
  281.             if(msg==null)
  282.                 throw new XMLUtilsException("Messaggio non presente");
  283.             SOAPBody soapBody = msg.getSOAPBody();
  284.             if(soapBody==null)
  285.                 throw new XMLUtilsException("SOAPBody non presente");
  286.             SOAPFault faultOriginale = null;
  287.             if(soapBody.hasFault()==false)
  288.                 return null; // casi di buste mal formate, verranno segnalate dal validatore
  289.             else
  290.                 faultOriginale = soapBody.getFault();
  291.             if(faultOriginale==null)
  292.                 throw new XMLUtilsException("SOAPFault is null");
  293.            
  294.             QName nameDetail = new QName("detail");
  295.             Iterator<?> itSF = faultOriginale.getChildElements(nameDetail);
  296.             SOAPElement detailsFaultOriginale = null;
  297.             if(itSF.hasNext()){
  298.                 detailsFaultOriginale = (SOAPElement) itSF.next();
  299.             }
  300.                
  301.             msg.saveChanges();
  302.                
  303.             if(detailsFaultOriginale!=null){
  304.                 Iterator<?> it = detailsFaultOriginale.getChildElements();
  305.                 while (it.hasNext()) {
  306.                     Object o = it.next();
  307.                     if(o instanceof SOAPElement){
  308.                         SOAPElement elem = (SOAPElement) o;
  309.                         try{
  310.                             if(XMLUtils.isDettaglioRouting(elem)){
  311.                                 //System.out.println("ITEM ["+elem.getLocalName()+"] TROVATO");
  312.                                 org.openspcoop2.message.xml.MessageXMLUtils xmlUtils = org.openspcoop2.message.xml.MessageXMLUtils.DEFAULT;
  313.                                 byte [] xml = xmlUtils.toByteArray(elem);
  314.                                 //System.out.println("XML S: "+new String(xml));
  315.                                 DettaglioRouting de = XMLUtils.getDettaglioRouting(log,xml);
  316.                                 return de;
  317.                             }
  318.                         }catch(Exception e){
  319.                             e.printStackTrace(System.out);
  320.                         }
  321.                     }
  322.                 }
  323.             }
  324.                
  325.             return null;
  326.         }catch(Exception e){
  327.             throw new XMLUtilsException(e.getMessage(),e);
  328.         }
  329.     }
  330.    
  331.     public static boolean isDettaglioRouting(byte [] doc){
  332.         try{
  333.             org.openspcoop2.message.xml.MessageXMLUtils xmlUtils = org.openspcoop2.message.xml.MessageXMLUtils.DEFAULT;
  334.             Document docXML = xmlUtils.newDocument(doc);
  335.             Element elemXML = docXML.getDocumentElement();
  336.             return XMLUtils.isDettaglioRouting_engine(elemXML);
  337.         }catch(Exception e){
  338.             //System.out.println("NON e' un DOCUMENTO VALIDO: "+e.getMessage());
  339.             return false;
  340.         }
  341.     }
  342.     public static boolean isDettaglioRouting(Document docXML){
  343.         try{
  344.             Element elemXML = docXML.getDocumentElement();
  345.             return XMLUtils.isDettaglioRouting_engine(elemXML);
  346.         }catch(Exception e){
  347.             //System.out.println("NON e' un DOCUMENTO VALIDO: "+e.getMessage());
  348.             return false;
  349.         }
  350.     }
  351.     public static boolean isDettaglioRouting(Element elemXML){
  352.         return isDettaglioRouting_engine(elemXML);
  353.     }
  354.     public static boolean isDettaglioRouting(Node nodeXml){
  355.         return isDettaglioRouting_engine(nodeXml);
  356.     }
  357.     private static boolean isDettaglioRouting_engine(Node nodeXml){
  358.         try{
  359.             //System.out.println("LOCAL["+Costanti.ROOT_LOCAL_NAME_DETTAGLIO_ECCEZIONE+"]vs["+elemXML.getLocalName()+"]  NAMESPACE["+Costanti.TARGET_NAMESPACE+"]vs["+elemXML.getNamespaceURI()+"]");
  360.             if(Costanti.ROOT_LOCAL_NAME_DETTAGLIO_ROUTING.equals(nodeXml.getLocalName()) &&
  361.                     Costanti.TARGET_NAMESPACE.equals(nodeXml.getNamespaceURI() )
  362.                 ){
  363.                 return true;
  364.             }
  365.             else{
  366.                 return false;
  367.             }
  368.         }catch(Exception e){
  369.             //System.out.println("NON e' un DOCUMENTO VALIDO: "+e.getMessage());
  370.             return false;
  371.         }
  372.     }
  373.    
  374. }