SPCoopDiagnosticSerializer.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.spcoop.diagnostica;

  21. import java.io.ByteArrayOutputStream;
  22. import java.util.Date;

  23. import org.openspcoop2.protocol.basic.diagnostica.DiagnosticSerializer;
  24. import org.openspcoop2.protocol.sdk.IProtocolFactory;
  25. import org.openspcoop2.protocol.sdk.ProtocolException;
  26. import org.openspcoop2.protocol.sdk.constants.TipoSerializzazione;
  27. import org.openspcoop2.protocol.sdk.diagnostica.MsgDiagnostico;
  28. import org.openspcoop2.protocol.spcoop.utils.SPCoopUtils;
  29. import org.openspcoop2.utils.date.DateManager;
  30. import org.w3c.dom.Document;
  31. import org.w3c.dom.Element;

  32. /**
  33.  * Classe che implementa, in base al protocollo SPCoop, l'interfaccia {@link org.openspcoop2.protocol.sdk.diagnostica.IDiagnosticSerializer}
  34.  *
  35.  * @author Poli Andrea (apoli@link.it)
  36.  * @author $Author$
  37.  * @version $Rev$, $Date$
  38.  */
  39. public class SPCoopDiagnosticSerializer extends DiagnosticSerializer implements org.openspcoop2.protocol.sdk.diagnostica.IDiagnosticSerializer {

  40.     public SPCoopDiagnosticSerializer(IProtocolFactory<?> protocolFactory) throws ProtocolException{
  41.         super(protocolFactory);
  42.     }

  43.     /* --------------------- MESSAGGI DIAGNOSTICI -----------------------*/

  44.     @Override
  45.     public Element toElement(MsgDiagnostico msgDiag) throws ProtocolException{
  46.         try{
  47.            
  48.             Document doc = this.xmlUtils.newDocument();
  49.             Element diagnostico = doc.createElementNS("http://www.ctrupa.it/schemas/2003/eGovIT/Diag1_0/", "eGov_IT_Diag:MessaggioDiagnostico");
  50.                        
  51.             Element gdoMsgDiag = doc.createElementNS("http://www.ctrupa.it/schemas/2003/eGovIT/Diag1_0/", "eGov_IT_Diag:OraRegistrazione");
  52.             Date gdoD=null;
  53.             if(msgDiag.getGdo()==null){
  54.                 gdoD = new Date(DateManager.getTimeMillis());
  55.             }else{
  56.                 gdoD = new Date(msgDiag.getGdo().getTime());
  57.             }
  58.             gdoMsgDiag.setTextContent(SPCoopUtils.getDate_eGovFormat(gdoD));
  59.             diagnostico.appendChild(gdoMsgDiag);

  60.             Element identificativoPorta = doc.createElementNS("http://www.ctrupa.it/schemas/2003/eGovIT/Diag1_0/", "eGov_IT_Diag:IdentificativoPorta");
  61.             identificativoPorta.setTextContent(msgDiag.getIdSoggetto().getCodicePorta());
  62.             diagnostico.appendChild(identificativoPorta);

  63.             Element identificativoFunzione = doc.createElementNS("http://www.ctrupa.it/schemas/2003/eGovIT/Diag1_0/", "eGov_IT_Diag:IdentificativoFunzione");
  64.             identificativoFunzione.setTextContent(msgDiag.getIdFunzione());
  65.             diagnostico.appendChild(identificativoFunzione);

  66.             Element livelloSev = doc.createElementNS("http://www.ctrupa.it/schemas/2003/eGovIT/Diag1_0/", "eGov_IT_Diag:LivelloDiSeverita");
  67.             String liv = "" + msgDiag.getSeverita();
  68.             livelloSev.setTextContent(liv);
  69.             diagnostico.appendChild(livelloSev);

  70.             Element testo = doc.createElementNS("http://www.ctrupa.it/schemas/2003/eGovIT/Diag1_0/", "eGov_IT_Diag:TestoDiagnostico");
  71.             testo.setTextContent(msgDiag.getMessaggio());
  72.             diagnostico.appendChild(testo);

  73.             return  diagnostico;

  74.         } catch(Exception e) {
  75.             this.log.error("DiagnosticSerializer.toElement error: "+e.getMessage(),e);
  76.             throw new ProtocolException("DiagnosticSerializer.toElement error: "+e.getMessage(),e);
  77.         }
  78.     }
  79.    
  80.     @Override
  81.     protected ByteArrayOutputStream toByteArrayOutputStream(MsgDiagnostico msgDiag, TipoSerializzazione tipoSerializzazione) throws ProtocolException {
  82.        
  83.         try{
  84.                    
  85.             switch (tipoSerializzazione) {
  86.                 case XML:
  87.                 case DEFAULT:
  88.                    
  89.                     ByteArrayOutputStream bout = new ByteArrayOutputStream();
  90.                     bout.write(org.openspcoop2.message.xml.MessageXMLUtils.DEFAULT.toByteArray(this.toElement(msgDiag)));
  91.                     bout.flush();
  92.                     bout.close();
  93.                     return bout;
  94.    
  95.                 default:
  96.                    
  97.                     throw new Exception("Tipo Serializzazione ["+tipoSerializzazione+"] Non gestito");
  98.             }
  99.            
  100.         } catch(Exception e) {
  101.             this.log.error("DiagnosticSerializer.toString error: "+e.getMessage(),e);
  102.             throw new ProtocolException("DiagnosticSerializer.toString error: "+e.getMessage(),e);
  103.         }

  104.     }
  105.    
  106. }