Deserializer.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.abstraction.csv;

  21. import java.io.ByteArrayInputStream;
  22. import java.io.InputStream;
  23. import java.util.ArrayList;
  24. import java.util.HashMap;
  25. import java.util.List;
  26. import java.util.Map;
  27. import java.util.Properties;

  28. import org.apache.commons.lang.CharEncoding;
  29. import org.openspcoop2.protocol.abstraction.Erogazione;
  30. import org.openspcoop2.protocol.abstraction.Fruizione;
  31. import org.openspcoop2.protocol.sdk.ProtocolException;
  32. import org.openspcoop2.protocol.utils.ManagerUtils;
  33. import org.openspcoop2.utils.Utilities;
  34. import org.openspcoop2.utils.UtilsException;
  35. import org.openspcoop2.utils.csv.Format;
  36. import org.openspcoop2.utils.csv.FormatReader;
  37. import org.openspcoop2.utils.csv.Parser;
  38. import org.openspcoop2.utils.csv.ParserResult;
  39. import org.openspcoop2.utils.csv.Record;
  40. import org.openspcoop2.utils.resources.TemplateUtils;
  41. import org.slf4j.Logger;

  42. import freemarker.template.Template;

  43. /**    
  44.  * Deserializer
  45.  *
  46.  * @author Poli Andrea (poli@link.it)
  47.  * @author $Author$
  48.  * @version $Rev$, $Date$
  49.  */
  50. public class Deserializer {

  51.     private static final String PROTOCOLLO = "protocollo";
  52.     private static final String DEFAULT_SUBJECT_TYPE = "_DefaultSubjectType_";
  53.    
  54.    
  55.     private org.openspcoop2.protocol.abstraction.utils.serializer.JaxbDeserializer jaxbAbstractionDeserializer = null;
  56.     private boolean validate;
  57.     private Logger log;
  58.     public Deserializer(boolean validate,Logger log) throws ProtocolException{
  59.         // -- unmarshall
  60.         this.jaxbAbstractionDeserializer = new org.openspcoop2.protocol.abstraction.utils.serializer.JaxbDeserializer();
  61.         this.validate = validate;
  62.         this.log = log;
  63.     }


  64.    

  65.    
  66.    
  67.    
  68.    
  69.    
  70.    

  71.     // *********** EROGAZIONE ******************

  72.     public List<Erogazione> toErogazione(byte[] erogazioneTemplateParam, byte[] formatParam, byte[] mappingParam, byte[] csv) throws ProtocolException{
  73.         return this.toErogazione(erogazioneTemplateParam, formatParam, mappingParam, csv, CharEncoding.UTF_8);
  74.     }
  75.     public List<Erogazione> toErogazione(byte[] erogazioneTemplateParam, byte[] formatParam, byte[] mappingParam, byte[] csv, String charset) throws ProtocolException{
  76.         Properties format = null;
  77.         if(formatParam!=null){
  78.             try {
  79.                 format = Utilities.getAsProperties(formatParam);
  80.             } catch (UtilsException e) {
  81.                 throw new ProtocolException(e.getMessage(),e);
  82.             }
  83.         }  
  84.         Properties mapping = null;
  85.         if(mappingParam!=null){
  86.             try {
  87.                 mapping = Utilities.getAsProperties(mappingParam);
  88.             } catch (UtilsException e) {
  89.                 throw new ProtocolException(e.getMessage(),e);
  90.             }
  91.         }  
  92.         return toErogazione(erogazioneTemplateParam, format, mapping, csv, charset);
  93.     }
  94.     public List<Erogazione> toErogazione(byte[] erogazioneTemplateParam, Properties formatParam, Properties mappingParam, byte[] csvParam) throws ProtocolException{
  95.         return this.toErogazione(erogazioneTemplateParam, formatParam, mappingParam, csvParam, CharEncoding.UTF_8);
  96.     }
  97.     public List<Erogazione> toErogazione(byte[] erogazioneTemplateParam, Properties formatParam, Properties mappingParam, byte[] csvParam, String charset) throws ProtocolException{
  98.         String csv = null;
  99.         if(csvParam==null){
  100.             throw new ProtocolException("CSV non disponibile");
  101.         }
  102.         if(charset==null){
  103.             charset = CharEncoding.UTF_8;
  104.         }
  105.         try{
  106.             csv = new String(csvParam, charset);
  107.         }catch(Exception e){
  108.             throw new ProtocolException("CSV con formato errato [encoding:"+charset+"]: "+e.getMessage());
  109.         }
  110.         return this.toErogazione(erogazioneTemplateParam, formatParam, mappingParam, csv);
  111.     }

  112.     public List<Erogazione> toErogazione(byte[] erogazioneTemplateParam, byte[] formatParam, byte[] mappingParam, String csv) throws ProtocolException{
  113.         Properties format = null;
  114.         if(formatParam!=null){
  115.             try {
  116.                 format = Utilities.getAsProperties(formatParam);
  117.             } catch (UtilsException e) {
  118.                 throw new ProtocolException(e.getMessage(),e);
  119.             }
  120.         }  
  121.         Properties mapping = null;
  122.         if(mappingParam!=null){
  123.             try {
  124.                 mapping = Utilities.getAsProperties(mappingParam);
  125.             } catch (UtilsException e) {
  126.                 throw new ProtocolException(e.getMessage(),e);
  127.             }
  128.         }
  129.         return this.toErogazione(erogazioneTemplateParam, format, mapping, csv);
  130.     }
  131.     public List<Erogazione> toErogazione(byte[] erogazioneTemplateParam, Properties formatParam, Properties mappingParam, String csv) throws ProtocolException{

  132.         try{
  133.        
  134.             byte[] erogazioneTemplate = erogazioneTemplateParam;
  135.             Template template = null;
  136.             if(erogazioneTemplate==null){
  137.                 erogazioneTemplate = this.readBytes("/templates/csv/Erogazione.ftl");
  138.             }
  139.             try{
  140.                 template = TemplateUtils.buildTemplate("Erogazione", erogazioneTemplate);
  141.             }catch(Exception e){
  142.                 throw new ProtocolException("Build Template error: "+e.getMessage());
  143.             }
  144.    
  145.             Properties format = formatParam;
  146.             if(format==null){
  147.                 format = this.readProperties("/templates/csv/format.properties");
  148.             }
  149.    
  150.             Properties mapping = mappingParam;
  151.             if(mapping==null){
  152.                 mapping = this.readProperties("/templates/csv/erogazione.properties");
  153.             }
  154.    
  155.             List<Erogazione> listErogazioni = new ArrayList<Erogazione>();
  156.    
  157.             // Deserializzo CSV per avere la mappa
  158.             FormatReader formatReader = new FormatReader(format);
  159.             Format f = formatReader.getFormat();
  160.             Parser p = new Parser(mapping, true);
  161.             ParserResult pr = p.parseCsvFile(f, csv);
  162.             for (Record record : pr.getRecords()) {
  163.    
  164.                 // Costruisco Mappa
  165.                 Map<String, Object> mapFreemarker = new HashMap<>();
  166.                 if(record.getMap().size()<=0){
  167.                     throw new ProtocolException("Csv Record at line ["+record.getCsvLine()+"] not correct, mapping produce 0 fields");
  168.                 }
  169.                 List<String> keys = record.getMap().keys();
  170.                 for (String key : keys) {
  171.                     String s = record.getMap().get(key);
  172.                     if(s!=null)
  173.                         mapFreemarker.put(key, s);
  174.                 }
  175.                
  176.                 // TipoSoggettoDefault
  177.                 String protocollo = record.getMap().get(PROTOCOLLO);
  178.                 if(protocollo == null){
  179.                     protocollo = ManagerUtils.getDefaultProtocol();
  180.                 }
  181.                 mapFreemarker.put(DEFAULT_SUBJECT_TYPE, ManagerUtils.getDefaultOrganizationType(protocollo));
  182.                
  183.                 // Creo xml della singola erogazione
  184.                 byte[] xmlErogazione = null;
  185.                 try{
  186.                     xmlErogazione = TemplateUtils.toByteArray(template, mapFreemarker);
  187.                 }catch(Exception e){
  188.                     throw new ProtocolException("Csv Record at line ["+record.getCsvLine()+"] not correct (Build xml from template failed): "+e.getMessage());
  189.                 }
  190.                 ByteArrayInputStream bin = null;
  191.                 try{
  192.                     if(this.validate){
  193.                         bin = new ByteArrayInputStream(xmlErogazione);
  194.                         org.openspcoop2.protocol.abstraction.utils.XSDValidator.getXSDValidator(this.log).valida(bin);
  195.                     }
  196.                     Erogazione erogazione = this.jaxbAbstractionDeserializer.readErogazione(xmlErogazione);
  197.                     listErogazioni.add(erogazione);
  198.                 }catch(Exception e){
  199.                     throw new ProtocolException("Csv Record at line ["+record.getCsvLine()+"] not correct. Marshall Xml ["+new String(xmlErogazione)+"]\n"+e.getMessage(),e);
  200.                 }
  201.                 finally{
  202.                     try{
  203.                         if(bin!=null)
  204.                             bin.close();
  205.                     }catch(Exception e){
  206.                         // close
  207.                     }
  208.                 }
  209.             }
  210.    
  211.             return listErogazioni;
  212.            
  213.         } catch (UtilsException e) {
  214.             throw new ProtocolException(e.getMessage(),e);
  215.         }

  216.     }






  217.     // *********** FRUIZIONE ******************

  218.     public List<Fruizione> toFruizione(byte[] fruizioneTemplateParam, byte[] formatParam, byte[] mappingParam, byte[] csv) throws ProtocolException{
  219.         return this.toFruizione(fruizioneTemplateParam, formatParam, mappingParam, csv, CharEncoding.UTF_8);
  220.     }
  221.     public List<Fruizione> toFruizione(byte[] fruizioneTemplateParam, byte[] formatParam, byte[] mappingParam, byte[] csv, String charset) throws ProtocolException{
  222.         Properties format = null;
  223.         if(formatParam!=null){
  224.             try{
  225.                 format = Utilities.getAsProperties(formatParam);
  226.             } catch (UtilsException e) {
  227.                 throw new ProtocolException(e.getMessage(),e);
  228.             }
  229.         }  
  230.         Properties mapping = null;
  231.         if(mappingParam!=null){
  232.             try{
  233.                 mapping = Utilities.getAsProperties(mappingParam);
  234.             } catch (UtilsException e) {
  235.                 throw new ProtocolException(e.getMessage(),e);
  236.             }
  237.         }  
  238.         return toFruizione(fruizioneTemplateParam, format, mapping, csv, charset);
  239.     }
  240.     public List<Fruizione> toFruizione(byte[] fruizioneTemplateParam, Properties formatParam, Properties mappingParam, byte[] csvParam) throws ProtocolException{
  241.         return this.toFruizione(fruizioneTemplateParam, formatParam, mappingParam, csvParam, CharEncoding.UTF_8);
  242.     }
  243.     public List<Fruizione> toFruizione(byte[] fruizioneTemplateParam, Properties formatParam, Properties mappingParam, byte[] csvParam, String charset) throws ProtocolException{
  244.         String csv = null;
  245.         if(csvParam==null){
  246.             throw new ProtocolException("CSV non disponibile");
  247.         }
  248.         if(charset==null){
  249.             charset = CharEncoding.UTF_8;
  250.         }
  251.         try{
  252.             csv = new String(csvParam, charset);
  253.         }catch(Exception e){
  254.             throw new ProtocolException("CSV con formato errato [encoding:"+charset+"]: "+e.getMessage());
  255.         }
  256.         return this.toFruizione(fruizioneTemplateParam, formatParam, mappingParam, csv);
  257.     }

  258.     public List<Fruizione> toFruizione(byte[] fruizioneTemplateParam, byte[] formatParam, byte[] mappingParam, String csv) throws ProtocolException{
  259.         Properties format = null;
  260.         if(formatParam!=null){
  261.             try{
  262.                 format = Utilities.getAsProperties(formatParam);
  263.             } catch (UtilsException e) {
  264.                 throw new ProtocolException(e.getMessage(),e);
  265.             }
  266.         }  
  267.         Properties mapping = null;
  268.         if(mappingParam!=null){
  269.             try{
  270.                 mapping = Utilities.getAsProperties(mappingParam);
  271.             } catch (UtilsException e) {
  272.                 throw new ProtocolException(e.getMessage(),e);
  273.             }
  274.         }
  275.         return this.toFruizione(fruizioneTemplateParam, format, mapping, csv);
  276.     }
  277.     public List<Fruizione> toFruizione(byte[] fruizioneTemplateParam, Properties formatParam, Properties mappingParam, String csv) throws ProtocolException{

  278.         try{
  279.        
  280.             byte[] fruizioneTemplate = fruizioneTemplateParam;
  281.             Template template = null;
  282.             if(fruizioneTemplate==null){
  283.                 fruizioneTemplate = this.readBytes("/templates/csv/Fruizione.ftl");
  284.             }
  285.             try{
  286.                 template = TemplateUtils.buildTemplate("Fruizione", fruizioneTemplate);
  287.             }catch(Exception e){
  288.                 throw new ProtocolException("Build Template error: "+e.getMessage());
  289.             }
  290.    
  291.             Properties format = formatParam;
  292.             if(format==null){
  293.                 format = this.readProperties("/templates/csv/format.properties");
  294.             }
  295.    
  296.             Properties mapping = mappingParam;
  297.             if(mapping==null){
  298.                 mapping = this.readProperties("/templates/csv/fruizione.properties");
  299.             }
  300.    
  301.             List<Fruizione> listFruizioni = new ArrayList<Fruizione>();
  302.    
  303.             // Deserializzo CSV per avere la mappa
  304.             FormatReader formatReader = new FormatReader(format);
  305.             Format f = formatReader.getFormat();
  306.             Parser p = new Parser(mapping, true);
  307.             ParserResult pr = p.parseCsvFile(f, csv);
  308.             for (Record record : pr.getRecords()) {
  309.    
  310.                 // Costruisco Mappa
  311.                 Map<String, Object> mapFreemarker = new HashMap<>();
  312.                 if(record.getMap().size()<=0){
  313.                     throw new ProtocolException("Csv Record at line ["+record.getCsvLine()+"] not correct, mapping produce 0 fields");
  314.                 }
  315.                 List<String> keys = record.getMap().keys();
  316.                 for (String key : keys) {
  317.                     String s = record.getMap().get(key);
  318.                     if(s!=null)
  319.                         mapFreemarker.put(key, s);
  320.                 }
  321.                
  322.                 // TipoSoggettoDefault
  323.                 String protocollo = record.getMap().get(PROTOCOLLO);
  324.                 if(protocollo == null){
  325.                     protocollo = ManagerUtils.getDefaultProtocol();
  326.                 }
  327.                 mapFreemarker.put(DEFAULT_SUBJECT_TYPE, ManagerUtils.getDefaultOrganizationType(protocollo));
  328.                
  329.                 // Creo xml della singola fruizione
  330.                 byte[] xmlFruizione = null;
  331.                 try{
  332.                     xmlFruizione = TemplateUtils.toByteArray(template, mapFreemarker);
  333.                 }catch(Exception e){
  334.                     throw new ProtocolException("Csv Record at line ["+record.getCsvLine()+"] not correct (Build xml from template failed): "+e.getMessage());
  335.                 }
  336.                 ByteArrayInputStream bin = null;
  337.                 try{
  338.                     if(this.validate){
  339.                         bin = new ByteArrayInputStream(xmlFruizione);
  340.                         org.openspcoop2.protocol.abstraction.utils.XSDValidator.getXSDValidator(this.log).valida(bin);
  341.                     }
  342.                     Fruizione fruizione = this.jaxbAbstractionDeserializer.readFruizione(xmlFruizione);
  343.                     listFruizioni.add(fruizione);
  344.                 }catch(Exception e){
  345.                     throw new ProtocolException("Csv Record at line ["+record.getCsvLine()+"] not correct. Marshall Xml ["+new String(xmlFruizione)+"]\n"+e.getMessage(),e);
  346.                 }
  347.                 finally{
  348.                     try{
  349.                         if(bin!=null)
  350.                             bin.close();
  351.                     }catch(Exception e){
  352.                         // close
  353.                     }
  354.                 }
  355.             }
  356.    
  357.             return listFruizioni;
  358.            
  359.         } catch (UtilsException e) {
  360.             throw new ProtocolException(e.getMessage(),e);
  361.         }

  362.     }









  363.     // *********** UTILS ******************

  364.     private byte[] readBytes(String resourceName) throws UtilsException{
  365.         InputStream is = null;
  366.         try{
  367.             is = Deserializer.class.getResourceAsStream(resourceName);
  368.             if(is==null){
  369.                 throw new UtilsException("["+resourceName+"] non disponibile");
  370.             }
  371.             return Utilities.getAsByteArray(is);
  372.         }
  373.         catch(Exception e){
  374.             throw new UtilsException(e.getMessage(),e);
  375.         }finally{
  376.             try{
  377.                 if(is!=null){
  378.                     is.close();
  379.                 }
  380.             }catch(Exception eClose){
  381.                 // close
  382.             }
  383.         }  
  384.     }

  385.     private Properties readProperties(String resourceName) throws UtilsException{
  386.         InputStream isProp = null;
  387.         try{
  388.             isProp = Deserializer.class.getResourceAsStream(resourceName);
  389.             if(isProp==null){
  390.                 throw new UtilsException("Format non disponibile");
  391.             }
  392.             Properties p = new Properties();
  393.             p.load(isProp);
  394.             return p;
  395.         }
  396.         catch(Exception e){
  397.             throw new UtilsException(e.getMessage(),e);
  398.         }finally{
  399.             try{
  400.                 if(isProp!=null){
  401.                     isProp.close();
  402.                 }
  403.             }catch(Exception eClose){
  404.                 // close
  405.             }
  406.         }
  407.     }

  408. }