TracciaSerializer.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.basic.tracciamento;

  21. import java.io.ByteArrayOutputStream;
  22. import java.util.ArrayList;
  23. import java.util.List;

  24. import org.openspcoop2.core.tracciamento.Eccezione;
  25. import org.openspcoop2.core.tracciamento.Proprieta;
  26. import org.openspcoop2.core.tracciamento.Riscontro;
  27. import org.openspcoop2.core.tracciamento.Trasmissione;
  28. import org.openspcoop2.core.tracciamento.constants.TipoCodificaEccezione;
  29. import org.openspcoop2.core.tracciamento.constants.TipoInoltro;
  30. import org.openspcoop2.core.tracciamento.constants.TipoProfiloCollaborazione;
  31. import org.openspcoop2.core.tracciamento.constants.TipoRilevanzaEccezione;
  32. import org.openspcoop2.core.tracciamento.constants.TipoTempo;
  33. import org.openspcoop2.message.constants.ServiceBinding;
  34. import org.openspcoop2.protocol.basic.BasicComponentFactory;
  35. import org.openspcoop2.protocol.sdk.Busta;
  36. import org.openspcoop2.protocol.sdk.IProtocolFactory;
  37. import org.openspcoop2.protocol.sdk.ProtocolException;
  38. import org.openspcoop2.protocol.sdk.XMLRootElement;
  39. import org.openspcoop2.protocol.sdk.config.ITraduttore;
  40. import org.openspcoop2.protocol.sdk.constants.CodiceErroreCooperazione;
  41. import org.openspcoop2.protocol.sdk.constants.Inoltro;
  42. import org.openspcoop2.protocol.sdk.constants.ProfiloDiCollaborazione;
  43. import org.openspcoop2.protocol.sdk.constants.SubCodiceErrore;
  44. import org.openspcoop2.protocol.sdk.constants.TipoOraRegistrazione;
  45. import org.openspcoop2.protocol.sdk.constants.TipoSerializzazione;
  46. import org.openspcoop2.protocol.sdk.tracciamento.Traccia;
  47. import org.openspcoop2.protocol.sdk.tracciamento.TracciaExtInfo;
  48. import org.openspcoop2.protocol.sdk.tracciamento.TracciaExtInfoDefinition;
  49. import org.openspcoop2.utils.xml.AbstractXMLUtils;
  50. import org.w3c.dom.Element;

  51. /**
  52.  * TracciaSerializer
  53.  *
  54.  * @author Andrea Poli (apoli@link.it)
  55.  * @author $Author$
  56.  * @version $Rev$, $Date$
  57.  */
  58. public class TracciaSerializer extends BasicComponentFactory implements org.openspcoop2.protocol.sdk.tracciamento.ITracciaSerializer {
  59.    
  60.     protected AbstractXMLUtils xmlUtils;
  61.    
  62.     private boolean prettyDocument = false;
  63.     private boolean omitXmlDeclaration = false;

  64.     public TracciaSerializer(IProtocolFactory<?> factory) throws ProtocolException {
  65.         super(factory);
  66.         this.xmlUtils = org.openspcoop2.message.xml.MessageXMLUtils.DEFAULT;
  67.     }

  68.    
  69.     @Override
  70.     public void setPrettyDocument(boolean v) {
  71.         this.prettyDocument = v;
  72.     }
  73.     @Override
  74.     public boolean isPrettyDocument() {
  75.         return this.prettyDocument;
  76.     }
  77.     @Override
  78.     public void setOmitXmlDeclaration(boolean v) {
  79.         this.omitXmlDeclaration = v;
  80.     }
  81.     @Override
  82.     public boolean isOmitXmlDeclaration() {
  83.         return this.omitXmlDeclaration;
  84.     }

  85.    
  86.     private org.openspcoop2.core.tracciamento.Traccia toTraccia(Traccia tracciaObject)
  87.             throws ProtocolException {
  88.         String tmpId = null;
  89.         org.openspcoop2.core.tracciamento.Traccia tracciaBase = null;
  90.         try{
  91.                        
  92.             if(tracciaObject.sizeProperties()>0){
  93.                 tmpId = tracciaObject.removeProperty(TracciaDriver.IDTRACCIA); // non deve essere serializzato
  94.             }      
  95.             tracciaBase = tracciaObject.getTraccia();
  96.            
  97.             // xml
  98.             if(tracciaBase!=null &&
  99.                 tracciaBase.getBustaRaw()==null){
  100.                 if(tracciaObject.getBustaAsByteArray()!=null)
  101.                     tracciaBase.setBustaRaw(new String(tracciaObject.getBustaAsByteArray()));
  102.                 else if(tracciaObject.getBustaAsRawContent()!=null){            
  103.                     try{
  104.                         tracciaBase.setBustaRaw(tracciaObject.getBustaAsRawContent().toString(TipoSerializzazione.DEFAULT));
  105.                     }catch(Exception e){
  106.                         throw new ProtocolException("Serializzazione RawContent non riuscita: "+e.getMessage(),e);
  107.                     }
  108.                 }
  109.             }
  110.            
  111.             // Traduzioni da factory
  112.             ITraduttore protocolTraduttore = this.protocolFactory.createTraduttore();
  113.             if(tracciaBase!=null &&
  114.                 tracciaBase.getBusta()!=null){
  115.                 if(tracciaBase.getBusta().getProfiloCollaborazione()!=null &&
  116.                         tracciaBase.getBusta().getProfiloCollaborazione().getBase()==null &&
  117.                         tracciaBase.getBusta().getProfiloCollaborazione().getTipo()!=null){
  118.                     tracciaBase.getBusta().getProfiloCollaborazione().setBase(this.getBaseValueProfiloCollaborazione(protocolTraduttore,tracciaBase.getBusta().getProfiloCollaborazione().getTipo()));
  119.                 }
  120.                 if(tracciaBase.getBusta().getProfiloTrasmissione()!=null &&
  121.                         tracciaBase.getBusta().getProfiloTrasmissione().getInoltro()!=null &&
  122.                         tracciaBase.getBusta().getProfiloTrasmissione().getInoltro().getBase()==null &&
  123.                         tracciaBase.getBusta().getProfiloTrasmissione().getInoltro().getTipo()!=null){
  124.                     tracciaBase.getBusta().getProfiloTrasmissione().getInoltro().setBase(this.getBaseValueInoltro(protocolTraduttore,tracciaBase.getBusta().getProfiloTrasmissione().getInoltro().getTipo()));
  125.                 }
  126.                 if(tracciaBase.getBusta().getOraRegistrazione()!=null &&
  127.                         tracciaBase.getBusta().getOraRegistrazione().getSorgente()!=null &&
  128.                         tracciaBase.getBusta().getOraRegistrazione().getSorgente().getBase()==null &&
  129.                         tracciaBase.getBusta().getOraRegistrazione().getSorgente().getTipo()!=null){
  130.                     tracciaBase.getBusta().getOraRegistrazione().getSorgente().setBase(this.getBaseValueTipoTempo(protocolTraduttore,tracciaBase.getBusta().getOraRegistrazione().getSorgente().getTipo()));
  131.                 }
  132.                 if(tracciaBase.getBusta().getTrasmissioni()!=null && tracciaBase.getBusta().getTrasmissioni().sizeTrasmissioneList()>0){
  133.                     for (Trasmissione trasmissione : tracciaBase.getBusta().getTrasmissioni().getTrasmissioneList()) {
  134.                         if(trasmissione.getOraRegistrazione()!=null &&
  135.                                 trasmissione.getOraRegistrazione().getSorgente()!=null &&
  136.                                 trasmissione.getOraRegistrazione().getSorgente().getBase()==null &&
  137.                                 trasmissione.getOraRegistrazione().getSorgente().getTipo()!=null){
  138.                             trasmissione.getOraRegistrazione().getSorgente().setBase(this.getBaseValueTipoTempo(protocolTraduttore,trasmissione.getOraRegistrazione().getSorgente().getTipo()));
  139.                         }
  140.                     }
  141.                 }
  142.                 if(tracciaBase.getBusta().getRiscontri()!=null && tracciaBase.getBusta().getRiscontri().sizeRiscontroList()>0){
  143.                     for (Riscontro riscontro : tracciaBase.getBusta().getRiscontri().getRiscontroList()) {
  144.                         if(riscontro.getOraRegistrazione()!=null &&
  145.                             riscontro.getOraRegistrazione().getSorgente()!=null &&
  146.                             riscontro.getOraRegistrazione().getSorgente().getBase()==null &&
  147.                             riscontro.getOraRegistrazione().getSorgente().getTipo()!=null){
  148.                             riscontro.getOraRegistrazione().getSorgente().setBase(this.getBaseValueTipoTempo(protocolTraduttore,riscontro.getOraRegistrazione().getSorgente().getTipo()));
  149.                         }
  150.                     }
  151.                 }
  152.                 if(tracciaBase.getBusta().getEccezioni()!=null && tracciaBase.getBusta().getEccezioni().sizeEccezioneList()>0){
  153.                     for (Eccezione eccezione : tracciaBase.getBusta().getEccezioni().getEccezioneList()) {
  154.                         if(eccezione.getCodice()!=null &&
  155.                             eccezione.getCodice().getBase()==null && eccezione.getCodice().getTipo()!=null){
  156.                             eccezione.getCodice().setBase(this.getBaseValueCodiceEccezione(protocolTraduttore,eccezione.getCodice().getTipo(), eccezione.getCodice().getSottotipo()));
  157.                         }
  158.                         if(eccezione.getContestoCodifica()!=null &&
  159.                             eccezione.getContestoCodifica().getBase()==null &&
  160.                             eccezione.getContestoCodifica().getTipo()!=null){
  161.                             eccezione.getContestoCodifica().setBase(this.getBaseValueContestoCodifica(protocolTraduttore,eccezione.getContestoCodifica().getTipo()));
  162.                         }
  163.                         if(eccezione.getRilevanza()!=null &&
  164.                             eccezione.getRilevanza().getBase()==null &&
  165.                             eccezione.getRilevanza().getTipo()!=null){
  166.                             eccezione.getRilevanza().setBase(this.getBaseValueRilevanzaEccezione(protocolTraduttore,eccezione.getRilevanza().getTipo()));
  167.                         }
  168.                     }
  169.                 }
  170.             }
  171.                            
  172.         } catch(Exception e) {
  173.             logAndThrowError(e, "XMLBuilder.buildElement_Tracciamento error");
  174.         }
  175.         finally{
  176.             if(tmpId!=null && tracciaObject!=null){
  177.                 tracciaObject.addProperty(TracciaDriver.IDTRACCIA, tmpId);
  178.             }
  179.         }
  180.        
  181.         return tracciaBase;
  182.     }
  183.    
  184.     private void logAndThrowError(Exception e, String msg) throws ProtocolException {
  185.         String er = msg+": "+e.getMessage();
  186.         this.log.error(er,e);
  187.         throw new ProtocolException(er,e);
  188.     }

  189.     @Override
  190.     public Element toElement(Traccia tracciaObject)
  191.             throws ProtocolException {
  192.         byte[] traccia = this.toByteArray(tracciaObject,TipoSerializzazione.XML);
  193.         Element el = null;
  194.         try{
  195.             el = this.xmlUtils.newElement(traccia);
  196.         } catch(Exception e) {
  197.             logAndThrowError(e, "TracciaSerializer.toElement error");
  198.         }
  199.         return el;
  200.     }

  201.     @Override
  202.     public String toString(Traccia traccia, TipoSerializzazione tipoSerializzazione) throws ProtocolException {
  203.         return this.toByteArrayOutputStream(traccia, tipoSerializzazione).toString();
  204.     }

  205.     @Override
  206.     public byte[] toByteArray(Traccia traccia, TipoSerializzazione tipoSerializzazione) throws ProtocolException {
  207.         return this.toByteArrayOutputStream(traccia, tipoSerializzazione).toByteArray();
  208.     }
  209.    
  210.     protected ByteArrayOutputStream toByteArrayOutputStream(Traccia traccia, TipoSerializzazione tipoSerializzazione) throws ProtocolException {
  211.        
  212.         ByteArrayOutputStream ret = null;
  213.         try{
  214.        
  215.             org.openspcoop2.core.tracciamento.Traccia tracciaBase = this.toTraccia(traccia);
  216.            
  217.             switch (tipoSerializzazione) {
  218.                 case XML:
  219.                 case DEFAULT:
  220.                    
  221.                     ByteArrayOutputStream bout = new ByteArrayOutputStream();
  222.                     org.openspcoop2.core.tracciamento.utils.XMLUtils.generateTraccia(tracciaBase,bout,this.prettyDocument,this.omitXmlDeclaration);
  223.                     bout.flush();
  224.                     bout.close();
  225.                     ret =  bout;
  226.                     break;
  227.    
  228.                 case JSON:
  229.                    
  230.                     bout = new ByteArrayOutputStream();
  231.                     String s = org.openspcoop2.core.tracciamento.utils.XMLUtils.generateTracciaAsJson(tracciaBase,this.prettyDocument);
  232.                     bout.write(s.getBytes());
  233.                     bout.flush();
  234.                     bout.close();
  235.                     ret =  bout;
  236.                     break;
  237.             }
  238.            
  239.             if(ret==null) {
  240.                 throw new ProtocolException("Tipo ["+tipoSerializzazione+"] Non gestito");
  241.             }
  242.            
  243.         } catch(Exception e) {
  244.             logAndThrowError(e, "TracciaSerializer.toString error");
  245.         }
  246.        
  247.         return ret;
  248.     }

  249.    
  250.    
  251.     // UTILITIES
  252.    
  253.     private String getBaseValueProfiloCollaborazione(ITraduttore protocolTraduttore,TipoProfiloCollaborazione tipoProfiloCollaborazione){
  254.         switch (tipoProfiloCollaborazione) {
  255.         case ONEWAY:
  256.             return protocolTraduttore.toString(ProfiloDiCollaborazione.ONEWAY);
  257.         case SINCRONO:
  258.             return protocolTraduttore.toString(ProfiloDiCollaborazione.SINCRONO);
  259.         case ASINCRONO_ASIMMETRICO:
  260.             return protocolTraduttore.toString(ProfiloDiCollaborazione.ASINCRONO_ASIMMETRICO);
  261.         case ASINCRONO_SIMMETRICO:
  262.             return protocolTraduttore.toString(ProfiloDiCollaborazione.ASINCRONO_SIMMETRICO);
  263.         case SCONOSCIUTO:
  264.             return protocolTraduttore.toString(ProfiloDiCollaborazione.UNKNOWN);
  265.         }
  266.         return null;
  267.     }
  268.    
  269.     private String getBaseValueInoltro(ITraduttore protocolTraduttore,TipoInoltro tipoInoltro){
  270.         switch (tipoInoltro) {
  271.         case INOLTRO_CON_DUPLICATI:
  272.             return protocolTraduttore.toString(Inoltro.CON_DUPLICATI);
  273.         case INOLTRO_SENZA_DUPLICATI:
  274.             return protocolTraduttore.toString(Inoltro.SENZA_DUPLICATI);
  275.         case SCONOSCIUTO:
  276.             return protocolTraduttore.toString(Inoltro.UNKNOWN);
  277.         }
  278.         return null;
  279.     }
  280.    
  281.     private String getBaseValueTipoTempo(ITraduttore protocolTraduttore,TipoTempo tipoTempo){
  282.         switch (tipoTempo) {
  283.         case LOCALE:
  284.             return protocolTraduttore.toString(TipoOraRegistrazione.LOCALE);
  285.         case SCONOSCIUTO:
  286.             return protocolTraduttore.toString(TipoOraRegistrazione.UNKNOWN);
  287.         case SINCRONIZZATO:
  288.             return protocolTraduttore.toString(TipoOraRegistrazione.SINCRONIZZATO);
  289.         }
  290.         return null;
  291.     }
  292.    
  293.     private String getBaseValueCodiceEccezione(ITraduttore protocolTraduttore,Integer codice, Integer subCodice){
  294.         CodiceErroreCooperazione errore = CodiceErroreCooperazione.toCodiceErroreCooperazione(codice);
  295.         if(subCodice==null){
  296.             return protocolTraduttore.toString(errore);
  297.         }
  298.         else{
  299.             SubCodiceErrore sub = new SubCodiceErrore();
  300.             sub.setSubCodice(subCodice);
  301.             return protocolTraduttore.toString(errore,sub);
  302.         }
  303.     }
  304.    
  305.     private String getBaseValueContestoCodifica(ITraduttore protocolTraduttore,TipoCodificaEccezione codifica){
  306.         switch (codifica) {
  307.         case ECCEZIONE_PROCESSAMENTO:
  308.             return protocolTraduttore.toString(org.openspcoop2.protocol.sdk.constants.ContestoCodificaEccezione.PROCESSAMENTO);
  309.         case ECCEZIONE_VALIDAZIONE_PROTOCOLLO:
  310.             return protocolTraduttore.toString(org.openspcoop2.protocol.sdk.constants.ContestoCodificaEccezione.INTESTAZIONE);
  311.         case SCONOSCIUTO:
  312.             return null;
  313.         }
  314.         return null;
  315.     }
  316.    
  317.     private String getBaseValueRilevanzaEccezione(ITraduttore protocolTraduttore,TipoRilevanzaEccezione codifica){
  318.         switch (codifica) {
  319.         case DEBUG:
  320.             return protocolTraduttore.toString(org.openspcoop2.protocol.sdk.constants.LivelloRilevanza.DEBUG);
  321.         case ERROR:
  322.             return protocolTraduttore.toString(org.openspcoop2.protocol.sdk.constants.LivelloRilevanza.ERROR);
  323.         case FATAL:
  324.             return protocolTraduttore.toString(org.openspcoop2.protocol.sdk.constants.LivelloRilevanza.FATAL);
  325.         case INFO:
  326.             return protocolTraduttore.toString(org.openspcoop2.protocol.sdk.constants.LivelloRilevanza.INFO);
  327.         case WARN:
  328.             return protocolTraduttore.toString(org.openspcoop2.protocol.sdk.constants.LivelloRilevanza.WARN);
  329.         case SCONOSCIUTO:
  330.             return protocolTraduttore.toString(org.openspcoop2.protocol.sdk.constants.LivelloRilevanza.UNKNOWN);
  331.         }
  332.         return null;
  333.     }



  334.     @Override
  335.     public XMLRootElement getXMLRootElement() throws ProtocolException {
  336.         return new TracciaXMLRootElement();
  337.     }
  338.    
  339.    
  340.     @Override
  341.     public List<TracciaExtInfoDefinition> getExtInfoDefinition(){
  342.         return null;
  343.     }
  344.     @Override
  345.     public List<TracciaExtInfo> extractExtInfo(Busta busta, ServiceBinding tipoApi){
  346.         List<TracciaExtInfoDefinition> extInfoDefinitionList = this.getExtInfoDefinition();
  347.         List<TracciaExtInfo> list = null;
  348.         if(extInfoDefinitionList!=null && !extInfoDefinitionList.isEmpty()) {
  349.            
  350.             list = new ArrayList<>();
  351.             TracciaExtInfo extInfoNoPrefix = new TracciaExtInfo();
  352.             extInfoNoPrefix.setEmpty(true);
  353.             list.add(extInfoNoPrefix);
  354.            
  355.             List<String> propertyNamesEsistentiNellaBusta = new ArrayList<>();
  356.             for (String pName : busta.getPropertiesNames()) {
  357.                 propertyNamesEsistentiNellaBusta.add(pName);
  358.             }
  359.            
  360.             for (TracciaExtInfoDefinition tracciaExtInfoDefinition : extInfoDefinitionList) {
  361.                
  362.                 TracciaExtInfo extInfo = new TracciaExtInfo();
  363.                 extInfo.setLabel(tracciaExtInfoDefinition.getLabel());
  364.                
  365.                 List<String> proprietaDaRimuovere = new ArrayList<>();
  366.                
  367.                 if(!propertyNamesEsistentiNellaBusta.isEmpty()) {
  368.                     for (String pName : propertyNamesEsistentiNellaBusta) {
  369.                         if(pName.startsWith(tracciaExtInfoDefinition.getPrefixId())) {
  370.                             String pValue = busta.getProperty(pName);
  371.                             String pNameWithoutPrefix = pName.substring(tracciaExtInfoDefinition.getPrefixId().length());
  372.                             Proprieta proprieta = new Proprieta();
  373.                             proprieta.setNome(pNameWithoutPrefix);
  374.                             proprieta.setValore(pValue);
  375.                             extInfo.getProprieta().add(proprieta);
  376.                            
  377.                             proprietaDaRimuovere.add(pName);
  378.                         }
  379.                     }
  380.                    
  381.                 }
  382.                
  383.                 if(!proprietaDaRimuovere.isEmpty()) {
  384.                     for (String pName : proprietaDaRimuovere) {
  385.                         propertyNamesEsistentiNellaBusta.remove(pName);
  386.                     }
  387.                 }
  388.                
  389.                 if(!extInfo.getProprieta().isEmpty()) {
  390.                    
  391.                     if(tracciaExtInfoDefinition.isOrder()) {
  392.                        
  393.                         java.util.ArrayList<String> listKeys = new ArrayList<>();
  394.                         for (Proprieta proprieta : extInfo.getProprieta()) {
  395.                             listKeys.add(proprieta.getNome());
  396.                         }
  397.                         java.util.Collections.sort(listKeys);
  398.                         List<Proprieta> proprietaOrdinate = new ArrayList<>();
  399.                         for (String key : listKeys) {  
  400.                             Proprieta p = extInfo.getProprieta(key);
  401.                             proprietaOrdinate.add(p);
  402.                         }
  403.                         extInfo.setProprieta(proprietaOrdinate);
  404.                        
  405.                     }
  406.                    
  407.                     list.add(extInfo);
  408.                 }
  409.                
  410.             }
  411.            
  412.             if(!propertyNamesEsistentiNellaBusta.isEmpty()) {
  413.                 for (String pName : propertyNamesEsistentiNellaBusta) {
  414.                     Proprieta proprieta = new Proprieta();
  415.                     proprieta.setNome(pName);
  416.                     proprieta.setValore(busta.getProperty(pName));
  417.                     extInfoNoPrefix.getProprieta().add(proprieta);
  418.                 }
  419.             }
  420.            
  421.             if(list.get(0).getProprieta().isEmpty()) {
  422.                 list.remove(0); // noPrefix
  423.             }
  424.        
  425.         }
  426.         return list;
  427.     }
  428. }