DiagnosticSerializer.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.diagnostica;

  21. import java.io.ByteArrayOutputStream;

  22. import org.openspcoop2.core.diagnostica.utils.XMLUtils;
  23. import org.openspcoop2.protocol.basic.BasicComponentFactory;
  24. import org.openspcoop2.protocol.sdk.IProtocolFactory;
  25. import org.openspcoop2.protocol.sdk.ProtocolException;
  26. import org.openspcoop2.protocol.sdk.XMLRootElement;
  27. import org.openspcoop2.protocol.sdk.constants.TipoSerializzazione;
  28. import org.openspcoop2.protocol.sdk.diagnostica.MsgDiagnostico;
  29. import org.w3c.dom.Element;

  30. /**
  31.  * XMLDiagnosticoBuilder
  32.  *
  33.  * @author Andrea Poli (apoli@link.it)
  34.  * @author $Author$
  35.  * @version $Rev$, $Date$
  36.  */
  37. public class DiagnosticSerializer extends BasicComponentFactory implements org.openspcoop2.protocol.sdk.diagnostica.IDiagnosticSerializer {

  38.     protected org.openspcoop2.message.xml.MessageXMLUtils xmlUtils;

  39.     private boolean prettyDocument = false;
  40.     private boolean omitXmlDeclaration = false;

  41.     public DiagnosticSerializer(IProtocolFactory<?> protocolFactory) throws ProtocolException{
  42.         super(protocolFactory);
  43.         this.xmlUtils = org.openspcoop2.message.xml.MessageXMLUtils.DEFAULT;
  44.     }
  45.    
  46.    
  47.     @Override
  48.     public void setPrettyDocument(boolean v) {
  49.         this.prettyDocument = v;
  50.     }
  51.     @Override
  52.     public boolean isPrettyDocument() {
  53.         return this.prettyDocument;
  54.     }
  55.     @Override
  56.     public void setOmitXmlDeclaration(boolean v) {
  57.         this.omitXmlDeclaration = v;
  58.     }
  59.     @Override
  60.     public boolean isOmitXmlDeclaration() {
  61.         return this.omitXmlDeclaration;
  62.     }
  63.    
  64.    

  65.     /* --------------------- MESSAGGI DIAGNOSTICI -----------------------*/

  66.     @Override
  67.     public Element toElement(MsgDiagnostico msgDiag) throws ProtocolException{
  68.         Element el = null;
  69.         String tmpId = null;
  70.         try{
  71.                        
  72.             if(msgDiag.sizeProperties()>0){
  73.                 tmpId = msgDiag.removeProperty(DiagnosticDriver.IDDIAGNOSTICI); // non deve essere serializzato
  74.             }
  75.            
  76.             // serializzazione
  77.             byte[] xmlDiagnostico = XMLUtils.generateMessaggioDiagnostico(msgDiag.getMessaggioDiagnostico());
  78.             el = this.xmlUtils.newElement(xmlDiagnostico);

  79.         } catch(Exception e) {
  80.             logAndThrowError(e, "DiagnosticSerializer.toElement error");
  81.         }
  82.         finally{
  83.             if(tmpId!=null && msgDiag!=null){
  84.                 msgDiag.addProperty(DiagnosticDriver.IDDIAGNOSTICI, tmpId);
  85.             }
  86.         }
  87.         return el;
  88.     }
  89.    
  90.     private void logAndThrowError(Exception e, String msg) throws ProtocolException {
  91.         String er = msg+": "+e.getMessage();
  92.         this.log.error(er,e);
  93.         throw new ProtocolException(er,e);
  94.     }

  95.     @Override
  96.     public String toString(MsgDiagnostico msgDiag, TipoSerializzazione tipoSerializzazione) throws ProtocolException {
  97.         return this.toByteArrayOutputStream(msgDiag, tipoSerializzazione).toString();
  98.     }

  99.     @Override
  100.     public byte[] toByteArray(MsgDiagnostico msgDiag, TipoSerializzazione tipoSerializzazione) throws ProtocolException {
  101.         return this.toByteArrayOutputStream(msgDiag, tipoSerializzazione).toByteArray();
  102.     }
  103.    
  104.     protected ByteArrayOutputStream toByteArrayOutputStream(MsgDiagnostico msgDiag, TipoSerializzazione tipoSerializzazione) throws ProtocolException {
  105.        
  106.         ByteArrayOutputStream ret = null;
  107.         String tmpId = null;
  108.         try{
  109.        
  110.             if(msgDiag.sizeProperties()>0){
  111.                 tmpId = msgDiag.removeProperty(DiagnosticDriver.IDDIAGNOSTICI); // non deve essere serializzato
  112.             }
  113.            
  114.             switch (tipoSerializzazione) {
  115.                 case XML:
  116.                 case DEFAULT:
  117.                    
  118.                     ByteArrayOutputStream bout = new ByteArrayOutputStream();
  119.                     XMLUtils.generateMessaggioDiagnostico(msgDiag.getMessaggioDiagnostico(),bout,this.prettyDocument,this.omitXmlDeclaration);
  120.                     bout.flush();
  121.                     bout.close();
  122.                     ret = bout;
  123.                     break;
  124.    
  125.                 case JSON:
  126.                    
  127.                     bout = new ByteArrayOutputStream();
  128.                     String s = XMLUtils.generateMessaggioDiagnosticoAsJson(msgDiag.getMessaggioDiagnostico(),this.prettyDocument);
  129.                     bout.write(s.getBytes());
  130.                     bout.flush();
  131.                     bout.close();
  132.                     ret = bout;
  133.                     break;
  134.             }
  135.            
  136.             if(ret==null) {
  137.                 throw new ProtocolException("Tipo ["+tipoSerializzazione+"] Non gestito");
  138.             }
  139.            
  140.         } catch(Exception e) {
  141.             logAndThrowError(e, "DiagnosticSerializer.toString error");
  142.         }
  143.         finally{
  144.             if(tmpId!=null && msgDiag!=null){
  145.                 msgDiag.addProperty(DiagnosticDriver.IDDIAGNOSTICI, tmpId);
  146.             }
  147.         }
  148.        
  149.         return ret;
  150.     }

  151.     @Override
  152.     public XMLRootElement getXMLRootElement() throws ProtocolException {
  153.         return new DiagnosticXMLRootElement();
  154.     }
  155. }