SPCoopTracciaSerializer.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.tracciamento;

  21. import java.io.ByteArrayOutputStream;

  22. import javax.xml.soap.SOAPElement;
  23. import javax.xml.soap.SOAPFactory;
  24. import javax.xml.soap.SOAPHeaderElement;

  25. import org.openspcoop2.message.OpenSPCoop2MessageFactory;
  26. import org.openspcoop2.message.constants.MessageType;
  27. import org.openspcoop2.message.soap.SoapUtils;
  28. import org.openspcoop2.protocol.basic.tracciamento.TracciaSerializer;
  29. import org.openspcoop2.protocol.sdk.Busta;
  30. import org.openspcoop2.protocol.sdk.BustaRawContent;
  31. import org.openspcoop2.protocol.sdk.IProtocolFactory;
  32. import org.openspcoop2.protocol.sdk.ProtocolException;
  33. import org.openspcoop2.protocol.sdk.constants.TipoSerializzazione;
  34. import org.openspcoop2.protocol.sdk.tracciamento.ITracciaSerializer;
  35. import org.openspcoop2.protocol.sdk.tracciamento.Traccia;
  36. import org.openspcoop2.protocol.spcoop.SPCoopBustaRawContent;
  37. import org.openspcoop2.protocol.spcoop.builder.SPCoopImbustamento;

  38. /**
  39.  * Classe che implementa, in base al protocollo SPCoop, l'interfaccia {@link org.openspcoop2.protocol.sdk.tracciamento.ITracciaSerializer}
  40.  *
  41.  * @author Poli Andrea (apoli@link.it)
  42.  * @author $Author$
  43.  * @version $Rev$, $Date$
  44.  */
  45. public class SPCoopTracciaSerializer extends TracciaSerializer implements ITracciaSerializer {
  46.    
  47.     private SPCoopImbustamento imbustamento = null;
  48.    
  49.     public SPCoopTracciaSerializer(IProtocolFactory<SOAPHeaderElement> factory) throws ProtocolException{
  50.         super(factory);
  51.         this.imbustamento = new SPCoopImbustamento(factory,null);
  52.     }
  53.    
  54.     @Override
  55.     public SOAPElement toElement(Traccia tracciaObject)
  56.             throws ProtocolException {
  57.         try{
  58.            
  59.             SOAPFactory sf = SoapUtils.getSoapFactory(OpenSPCoop2MessageFactory.getDefaultMessageFactory(), MessageType.SOAP_11);
  60.             SOAPElement traccia = sf.createElement("traccia","eGov_IT_Trac","http://www.cnipa.it/schemas/2003/eGovIT/Tracciamento1_0/");
  61.            
  62.             SOAPElement GDO =  traccia.addChildElement("GDO","eGov_IT_Trac","http://www.cnipa.it/schemas/2003/eGovIT/Tracciamento1_0/");
  63.             if(tracciaObject.getGdo()==null){
  64.                 GDO.setValue(this.protocolFactory.createTraduttore().getDate_protocolFormat());
  65.             }else{
  66.                 GDO.setValue(this.protocolFactory.createTraduttore().getDate_protocolFormat(tracciaObject.getGdo()));
  67.             }


  68.             SOAPElement IdentificativoPorta = traccia.addChildElement("IdentificativoPorta","eGov_IT_Trac","http://www.cnipa.it/schemas/2003/eGovIT/Tracciamento1_0/");
  69.             IdentificativoPorta.setValue(tracciaObject.getIdSoggetto().getCodicePorta());

  70.             SOAPElement TipoMessaggio = traccia.addChildElement("TipoMessaggio","eGov_IT_Trac","http://www.cnipa.it/schemas/2003/eGovIT/Tracciamento1_0/");
  71.             TipoMessaggio.setValue(tracciaObject.getTipoMessaggio().toString());


  72.             SOAPElement hdrEGov = null;
  73.             Busta busta = tracciaObject.getBusta();
  74.             BustaRawContent<?> bustaInDom = tracciaObject.getBustaAsRawContent();
  75.             String bustaAsString = tracciaObject.getBustaAsString();
  76.             byte[] bustaInByte = tracciaObject.getBustaAsByteArray();
  77.             if(bustaInDom!=null){
  78.                 // Tracciamento dall'oggetto dom
  79.                 hdrEGov = ((SPCoopBustaRawContent) bustaInDom).getElement();
  80.             }else if(bustaAsString != null) {
  81.                 // Tracciamento dai byte di una Busta
  82.                 hdrEGov = OpenSPCoop2MessageFactory.createSOAPElement(OpenSPCoop2MessageFactory.getDefaultMessageFactory(),MessageType.SOAP_11,bustaAsString.getBytes());
  83.             }else if(bustaInByte != null) {
  84.                 // Tracciamento dai byte di una Busta
  85.                 hdrEGov = OpenSPCoop2MessageFactory.createSOAPElement(OpenSPCoop2MessageFactory.getDefaultMessageFactory(),MessageType.SOAP_11,bustaInByte);
  86.             }else if(busta!=null){
  87.                 // Tracciamento dall'oggetto Busta
  88.                 hdrEGov = this.imbustamento.build_eGovHeader(null, busta, false, true);
  89.             }
  90.             else{
  91.                 throw new NullPointerException("Busta non fornita in alcun modo");
  92.             }
  93.             if(hdrEGov == null){
  94.                 throw new ProtocolException("XMLBuilder.buildElement_Tracciamento fallito");
  95.             }
  96.             traccia.addChildElement(hdrEGov);

  97.             return  traccia;

  98.         } catch(Exception e) {
  99.             this.log.error("TracciaSerializer.toElement error: "+e.getMessage(),e);
  100.             throw new ProtocolException("TracciaSerializer.toElement error: "+e.getMessage(),e);
  101.         }
  102.     }
  103.    
  104.    
  105.     @Override
  106.     protected ByteArrayOutputStream toByteArrayOutputStream(Traccia traccia, TipoSerializzazione tipoSerializzazione) throws ProtocolException {
  107.        
  108.         try{
  109.                    
  110.             switch (tipoSerializzazione) {
  111.                 case XML:
  112.                 case DEFAULT:
  113.                    
  114.                     ByteArrayOutputStream bout = new ByteArrayOutputStream();
  115.                     bout.write(org.openspcoop2.message.xml.MessageXMLUtils.DEFAULT.toByteArray(this.toElement(traccia)));
  116.                     bout.flush();
  117.                     bout.close();
  118.                     return bout;
  119.    
  120.                 default:
  121.                    
  122.                     throw new Exception("Tipo Serializzazione ["+tipoSerializzazione+"] Non gestito");
  123.             }
  124.            
  125.         } catch(Exception e) {
  126.             this.log.error("DiagnosticSerializer.toString error: "+e.getMessage(),e);
  127.             throw new ProtocolException("DiagnosticSerializer.toString error: "+e.getMessage(),e);
  128.         }

  129.     }
  130.    
  131. }