EsitoRichiestaXMLUtils.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.integrazione.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.core.integrazione.EsitoRichiesta;
  29. import org.openspcoop2.core.integrazione.constants.Costanti;
  30. import org.openspcoop2.message.OpenSPCoop2MessageFactory;
  31. import org.openspcoop2.message.xml.ValidatoreXSD;
  32. import org.openspcoop2.utils.beans.WriteToSerializerType;
  33. import org.openspcoop2.utils.xml.XSDResourceResolver;
  34. import org.w3c.dom.Document;
  35. import org.w3c.dom.Element;
  36. import org.w3c.dom.Node;


  37. /**
  38.  * Classe utilizzata per la generazione dei dati di esitoRichiesta
  39.  *
  40.  *
  41.  * @author Poli Andrea (apoli@link.it)
  42.  * @author $Author$
  43.  * @version $Rev$, $Date$
  44.  */

  45. public class EsitoRichiestaXMLUtils  {

  46.     /** Validatore XSD */
  47.     static ValidatoreXSD validatoreXSD = null;
  48.     private static synchronized void initValidatoreXSD(Logger log) throws Exception{
  49.         if(EsitoRichiestaXMLUtils.validatoreXSD==null){
  50.             XSDResourceResolver xsdResourceResolver = new XSDResourceResolver();
  51.             xsdResourceResolver.addResource("soapEnvelope.xsd", EsitoRichiestaXMLUtils.class.getResourceAsStream("/soapEnvelope.xsd"));
  52.             EsitoRichiestaXMLUtils.validatoreXSD = new ValidatoreXSD(OpenSPCoop2MessageFactory.getDefaultMessageFactory(), log,xsdResourceResolver,EsitoRichiestaXMLUtils.class.getResourceAsStream("/openspcoopPresaInCarico.xsd"));
  53.         }
  54.     }
  55.     public static ValidatoreXSD getValidatoreXSD(Logger log) throws Exception{
  56.         if(EsitoRichiestaXMLUtils.validatoreXSD==null){
  57.             initValidatoreXSD(log);
  58.         }
  59.         return EsitoRichiestaXMLUtils.validatoreXSD;
  60.     }
  61.    
  62.     public static boolean validate(EsitoRichiesta esito,StringBuilder motivoErroreValidazione){
  63.         int size = motivoErroreValidazione.length();
  64.        
  65.         if(esito.getMessageId()==null){
  66.             motivoErroreValidazione.append("Identificativo Messaggio non definito\n");
  67.         }
  68.         if(esito.getState()==null){
  69.             motivoErroreValidazione.append("Stato non definito\n");
  70.         }
  71.        
  72.         if(motivoErroreValidazione.length()!=size)
  73.             return false;
  74.         else
  75.             return true;
  76.     }
  77.    
  78.    
  79.    
  80.     /* ----- Unmarshall ----- */
  81.    
  82.     /**
  83.      * Ritorna la rappresentazione java
  84.      *
  85.      * @param m byte[]
  86.      * @return DettaglioEccezione
  87.      * @throws XMLUtilsException
  88.      */
  89.     public static EsitoRichiesta getEsitoRichiesta(Logger log,byte[] m) throws XMLUtilsException{
  90.         ByteArrayInputStream bin = null;
  91.         try{
  92.             bin = new ByteArrayInputStream(m);
  93.             return EsitoRichiestaXMLUtils.getEsitoRichiesta(log,bin);
  94.         }catch(Exception e){
  95.             throw new XMLUtilsException(e.getMessage(),e);
  96.         }finally{
  97.             try{
  98.                 if(bin!=null)
  99.                     bin.close();
  100.             }catch(Exception eClose){
  101.                 // close
  102.             }
  103.         }
  104.     }
  105.    
  106.     /**
  107.      * Ritorna la rappresentazione java
  108.      *
  109.      * @param m File
  110.      * @return DettaglioEccezione
  111.      * @throws XMLUtilsException
  112.      */
  113.     public static EsitoRichiesta getEsitoRichiesta(Logger log,File m) throws XMLUtilsException{
  114.         FileInputStream fin = null;
  115.         try{
  116.             fin = new FileInputStream(m);
  117.             return EsitoRichiestaXMLUtils.getEsitoRichiesta(log,fin);
  118.         }catch(Exception e){
  119.             throw new XMLUtilsException(e.getMessage(),e);
  120.         }finally{
  121.             try{
  122.                 if(fin!=null)
  123.                     fin.close();
  124.             }catch(Exception eClose){
  125.                 // close
  126.             }
  127.         }
  128.     }
  129.    
  130.     /**
  131.      * Ritorna la rappresentazione java
  132.      *
  133.      * @param m String
  134.      * @return DettaglioEccezione
  135.      * @throws XMLUtilsException
  136.      */
  137.     public static EsitoRichiesta getEsitoRichiesta(Logger log,String m) throws XMLUtilsException{
  138.         return EsitoRichiestaXMLUtils.getEsitoRichiesta(log,m.getBytes());
  139.     }
  140.    
  141.     /**
  142.      * Ritorna la rappresentazione java
  143.      *
  144.      * @param m InputStream
  145.      * @return DettaglioEccezione
  146.      * @throws XMLUtilsException
  147.      */
  148.     public static EsitoRichiesta getEsitoRichiesta(Logger log,InputStream m) throws XMLUtilsException{
  149.         try{
  150.             ByteArrayOutputStream bout = new ByteArrayOutputStream();
  151.             byte[]reads = new byte[1024];
  152.             int letti = 0;
  153.             while( (letti=m.read(reads)) != -1){
  154.                 bout.write(reads, 0, letti);
  155.             }
  156.             bout.flush();
  157.             bout.close();
  158.             byte [] xml = bout.toByteArray();
  159.            
  160.             // Validazione XSD
  161.             ByteArrayInputStream binValidazione = new ByteArrayInputStream(xml);
  162.             ValidatoreXSD validatoreXSD = EsitoRichiestaXMLUtils.getValidatoreXSD(log);
  163.             validatoreXSD.valida(binValidazione);
  164.            
  165.             // trasformazione in oggetto DettaglioEccezione
  166.             ByteArrayInputStream binTrasformazione = new ByteArrayInputStream(xml);
  167.             return (EsitoRichiesta) org.openspcoop2.utils.xml.JaxbUtils.xmlToObj(binTrasformazione, EsitoRichiesta.class);
  168.            
  169.         }catch(Exception e){
  170.             throw new XMLUtilsException(e.getMessage(),e);
  171.         }
  172.     }

  173.    
  174.    
  175.    
  176.    
  177.     /* ----- Marshall ----- */
  178.     public static void generateEsitoRichiesta(EsitoRichiesta esitoRichiesta,File out) throws XMLUtilsException{
  179.         try{
  180.             StringBuilder risultatoValidazione = new StringBuilder();
  181.             if(EsitoRichiestaXMLUtils.validate(esitoRichiesta, risultatoValidazione)==false){
  182.                 throw new Exception(risultatoValidazione.toString());
  183.             }
  184.             org.openspcoop2.utils.xml.JaxbUtils.objToXml(out.getName(),EsitoRichiestaXMLUtils.generateEsitoRichiesta_engine(esitoRichiesta));
  185.         }catch(Exception e){
  186.             throw new XMLUtilsException(e.getMessage(),e);
  187.         }
  188.     }
  189.    
  190.     public static void generateEsitoRichiesta(EsitoRichiesta esitoRichiesta,String fileName) throws XMLUtilsException{
  191.         try{
  192.             StringBuilder risultatoValidazione = new StringBuilder();
  193.             if(EsitoRichiestaXMLUtils.validate(esitoRichiesta, risultatoValidazione)==false){
  194.                 throw new Exception(risultatoValidazione.toString());
  195.             }
  196.             org.openspcoop2.utils.xml.JaxbUtils.objToXml(fileName,EsitoRichiestaXMLUtils.generateEsitoRichiesta_engine(esitoRichiesta));
  197.         }catch(Exception e){
  198.             throw new XMLUtilsException(e.getMessage(),e);
  199.         }
  200.     }
  201.    
  202.     public static byte[] generateEsitoRichiesta(EsitoRichiesta esitoRichiesta) throws XMLUtilsException{
  203.         try{
  204.             StringBuilder risultatoValidazione = new StringBuilder();
  205.             if(EsitoRichiestaXMLUtils.validate(esitoRichiesta, risultatoValidazione)==false){
  206.                 throw new Exception(risultatoValidazione.toString());
  207.             }
  208.             return EsitoRichiestaXMLUtils.generateEsitoRichiesta_engine(esitoRichiesta);
  209.         }catch(Exception e){
  210.             throw new XMLUtilsException(e.getMessage(),e);
  211.         }
  212.     }

  213.     public static void generateEsitoRichiesta(EsitoRichiesta esitoRichiesta,OutputStream out) throws XMLUtilsException{
  214.         try{
  215.             StringBuilder risultatoValidazione = new StringBuilder();
  216.             if(EsitoRichiestaXMLUtils.validate(esitoRichiesta, risultatoValidazione)==false){
  217.                 throw new Exception(risultatoValidazione.toString());
  218.             }
  219.             out.write(EsitoRichiestaXMLUtils.generateEsitoRichiesta_engine(esitoRichiesta));
  220.             out.flush();
  221.             out.close();
  222.         }catch(Exception e){
  223.             throw new XMLUtilsException(e.getMessage(),e);
  224.         }
  225.     }
  226.    
  227.     public static String generateEsitoRichiestaAsJson(EsitoRichiesta esitoRichiesta) throws XMLUtilsException{
  228.         try{
  229.             StringBuilder risultatoValidazione = new StringBuilder();
  230.             if(EsitoRichiestaXMLUtils.validate(esitoRichiesta, risultatoValidazione)==false){
  231.                 throw new Exception(risultatoValidazione.toString());
  232.             }
  233.             return EsitoRichiestaXMLUtils.generateEsitoRichiestaAsJson_engine(esitoRichiesta);
  234.         }catch(Exception e){
  235.             throw new XMLUtilsException(e.getMessage(),e);
  236.         }
  237.     }
  238.    
  239.     private static byte[] generateEsitoRichiesta_engine(EsitoRichiesta esitoRichiesta) throws XMLUtilsException{
  240.         try{
  241.             ByteArrayOutputStream bout = new ByteArrayOutputStream();
  242.             org.openspcoop2.utils.xml.JaxbUtils.objToXml(bout, EsitoRichiesta.class, esitoRichiesta);
  243.             byte[] dichiarazione = bout.toByteArray();
  244.             return dichiarazione;
  245.         }catch(Exception e){
  246.             throw new XMLUtilsException(e.getMessage(),e);
  247.         }
  248.     }
  249.    
  250.     private static String generateEsitoRichiestaAsJson_engine(EsitoRichiesta esitoRichiesta) throws XMLUtilsException{
  251.         try{
  252.             ByteArrayOutputStream bout = new ByteArrayOutputStream();
  253.             esitoRichiesta.writeTo(bout, WriteToSerializerType.JSON_JACKSON);
  254.             bout.flush();
  255.             bout.close();
  256.             return bout.toString();
  257.         }catch(Exception e){
  258.             throw new XMLUtilsException(e.getMessage(),e);
  259.         }
  260.     }
  261.    
  262.    
  263.    
  264.    
  265.    
  266.     public static boolean isEsitoRichiesta(byte [] doc){
  267.         try{
  268.             org.openspcoop2.message.xml.MessageXMLUtils xmlUtils = org.openspcoop2.message.xml.MessageXMLUtils.DEFAULT;
  269.             Document docXML = xmlUtils.newDocument(doc);
  270.             Element elemXML = docXML.getDocumentElement();
  271.             return EsitoRichiestaXMLUtils.isEsitoRichiesta_engine(elemXML);
  272.         }catch(Exception e){
  273.             //System.out.println("NON e' un DOCUMENTO VALIDO: "+e.getMessage());
  274.             return false;
  275.         }
  276.     }
  277.     public static boolean isEsitoRichiesta(Document docXML){
  278.         try{
  279.             Element elemXML = docXML.getDocumentElement();
  280.             return EsitoRichiestaXMLUtils.isEsitoRichiesta_engine(elemXML);
  281.         }catch(Exception e){
  282.             //System.out.println("NON e' un DOCUMENTO VALIDO: "+e.getMessage());
  283.             return false;
  284.         }
  285.     }
  286.     public static boolean isEsitoRichiesta(Element elemXML){
  287.         return isEsitoRichiesta_engine(elemXML);
  288.     }
  289.     public static boolean isEsitoRichiesta(Node nodeXml){
  290.         return isEsitoRichiesta_engine(nodeXml);
  291.     }
  292.     private static boolean isEsitoRichiesta_engine(Node nodeXml){
  293.         try{
  294.             //System.out.println("LOCAL["+Costanti.ROOT_LOCAL_NAME_DETTAGLIO_ECCEZIONE+"]vs["+elemXML.getLocalName()+"]  NAMESPACE["+Costanti.TARGET_NAMESPACE+"]vs["+elemXML.getNamespaceURI()+"]");
  295.             if(Costanti.ROOT_LOCAL_NAME_ESITO_RICHIESTA.equals(nodeXml.getLocalName()) &&
  296.                     Costanti.TARGET_NAMESPACE.equals(nodeXml.getNamespaceURI() )
  297.                 ){
  298.                 return true;
  299.             }
  300.             else{
  301.                 return false;
  302.             }
  303.         }catch(Exception e){
  304.             //System.out.println("NON e' un DOCUMENTO VALIDO: "+e.getMessage());
  305.             return false;
  306.         }
  307.     }
  308. }