FatturaPABehaviour.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.sdi.utils;

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

  24. import org.openspcoop2.core.commons.CoreException;
  25. import org.openspcoop2.core.config.PortaApplicativa;
  26. import org.openspcoop2.generic_project.exception.SerializerException;
  27. import org.openspcoop2.message.OpenSPCoop2Message;
  28. import org.openspcoop2.message.OpenSPCoop2MessageParseResult;
  29. import org.openspcoop2.message.constants.MessageRole;
  30. import org.openspcoop2.message.soap.TunnelSoapUtils;
  31. import org.openspcoop2.pdd.config.OpenSPCoop2Properties;
  32. import org.openspcoop2.pdd.core.GestoreMessaggi;
  33. import org.openspcoop2.pdd.core.behaviour.Behaviour;
  34. import org.openspcoop2.pdd.core.behaviour.BehaviourEmitDiagnosticException;
  35. import org.openspcoop2.pdd.core.behaviour.BehaviourException;
  36. import org.openspcoop2.pdd.core.behaviour.BehaviourForwardTo;
  37. import org.openspcoop2.pdd.core.behaviour.BehaviourForwardToConfiguration;
  38. import org.openspcoop2.pdd.core.behaviour.BehaviourResponseTo;
  39. import org.openspcoop2.pdd.core.behaviour.StatoFunzionalita;
  40. import org.openspcoop2.pdd.core.behaviour.built_in.DefaultBehaviour;
  41. import org.openspcoop2.pdd.logger.OpenSPCoop2Logger;
  42. import org.openspcoop2.protocol.sdi.SDIFactory;
  43. import org.openspcoop2.protocol.sdi.builder.SDIBustaBuilder;
  44. import org.openspcoop2.protocol.sdi.builder.SDIImbustamento;
  45. import org.openspcoop2.protocol.sdi.config.SDIProperties;
  46. import org.openspcoop2.protocol.sdi.constants.SDICostanti;
  47. import org.openspcoop2.protocol.sdi.constants.SDICostantiServizioRicezioneFatture;
  48. import org.openspcoop2.protocol.sdk.Busta;
  49. import org.openspcoop2.protocol.sdk.ProtocolException;
  50. import org.openspcoop2.protocol.sdk.builder.ProprietaManifestAttachments;
  51. import org.openspcoop2.protocol.sdk.constants.FaseSbustamento;
  52. import org.openspcoop2.protocol.sdk.constants.RuoloMessaggio;
  53. import org.openspcoop2.protocol.sdk.state.IState;
  54. import org.openspcoop2.protocol.sdk.state.RequestInfo;
  55. import org.openspcoop2.utils.transport.http.HttpConstants;

  56. /**
  57.  * FatturaPABehaviour
  58.  *
  59.  * @author Andrea Poli (apoli@link.it)
  60.  * @author $Author$
  61.  * @version $Rev$, $Date$
  62.  */
  63. public class FatturaPABehaviour extends DefaultBehaviour {

  64.     private static OpenSPCoop2Properties openspcoop2Properties = null;
  65.     private static SDIProperties sdiProperties = null;
  66.        
  67.     private static synchronized void init() throws ProtocolException{
  68.         if(openspcoop2Properties==null){
  69.             try{
  70.                 openspcoop2Properties = OpenSPCoop2Properties.getInstance();
  71.                 sdiProperties = SDIProperties.getInstance(OpenSPCoop2Logger.getLoggerOpenSPCoopCore());
  72.             }catch(Exception e){
  73.                 throw new ProtocolException(e.getMessage(),e);
  74.             }
  75.         }
  76.     }
  77.    
  78.     @Override
  79.     public Behaviour behaviour(GestoreMessaggi gestoreMessaggioRichiesta, Busta busta,
  80.             PortaApplicativa pa, RequestInfo requestInfo) throws BehaviourException,BehaviourEmitDiagnosticException {
  81.        
  82.         if(SDICostantiServizioRicezioneFatture.RICEZIONE_SERVIZIO_RICEZIONE_FATTURE.equals(busta.getServizio()) &&
  83.                 SDICostantiServizioRicezioneFatture.RICEZIONE_SERVIZIO_RICEZIONE_FATTURE_AZIONE_RICEVI_FATTURE.equals(busta.getAzione())){
  84.            
  85.             try{
  86.                 if(openspcoop2Properties==null){
  87.                     init();
  88.                 }
  89.                
  90.                 IState state = null;
  91.                 if(gestoreMessaggioRichiesta.getOpenspcoopstate()!=null){
  92.                     state = gestoreMessaggioRichiesta.getOpenspcoopstate().getStatoRichiesta();
  93.                 }
  94.                
  95.                 OpenSPCoop2Message msg = gestoreMessaggioRichiesta.getMessage();
  96.                 Object fatturaPAObject = msg.getContextProperty(SDICostanti.SDI_MESSAGE_CONTEXT_FATTURA);
  97.                 byte [] fatturaBytes = null;
  98.                 if(fatturaPAObject==null){
  99.                     //throw new CoreException("FatturaPA behaviour è utilizzabile solo se l'opzione 'org.openspcoop2.protocol.sdi.parse.fattura.saveInContext' è abilitata");
  100.                     // gestisco con lo splitter
  101.                     try{
  102.                         SDIFactory sdiFactory = new SDIFactory();
  103.                         sdiFactory.createBustaBuilder(state).
  104.                             sbustamento(msg, gestoreMessaggioRichiesta.getPdDContext(),
  105.                                     busta, RuoloMessaggio.RICHIESTA, new ProprietaManifestAttachments(),
  106.                                 FaseSbustamento.PRE_CONSEGNA_RICHIESTA,requestInfo.getIntegrationServiceBinding(),requestInfo.getBindingConfig());
  107.                         ByteArrayOutputStream bout = new ByteArrayOutputStream();
  108.                         TunnelSoapUtils.sbustamentoMessaggio(msg,bout);
  109.                         bout.flush();
  110.                         bout.close();
  111.                         fatturaBytes = bout.toByteArray();
  112.                        
  113.                         Object formato = busta.getProperty(SDICostanti.SDI_BUSTA_EXT_FORMATO_ARCHIVIO_INVIO_FATTURA);
  114.                         if(formato!=null && ((String)formato).equals(SDICostanti.SDI_TIPO_FATTURA_P7M) ){
  115.                             P7MInfo p7m = new P7MInfo(fatturaBytes, sdiFactory.getLogger());
  116.                             fatturaBytes = p7m.getXmlDecoded();
  117.                         }
  118.                     }catch(Throwable e){
  119.                         throw new CoreException("FatturaPA behaviour con l'opzione 'org.openspcoop2.protocol.sdi.parse.fattura.saveInContext' disabilitata. Lo sbustamento del messaggio non e' riuscito: "+e.getMessage(),e);  
  120.                     }
  121.                 }
  122.                
  123.                 byte[]metadati = null;
  124.                 if(sdiProperties.isBehaviourCreaProtocolloSDI()){
  125.                     Object metadatiBytesObject = msg.getContextProperty(SDICostanti.SDI_MESSAGE_CONTEXT_FATTURA_METADATI_BYTES);
  126.                     if(metadatiBytesObject==null){
  127.                         throw new CoreException("FatturaPA behaviour con opzione 'org.openspcoop2.protocol.sdi.behaviour.creaProtocolloSDI' è utilizzabile solo se l'opzione 'org.openspcoop2.protocol.sdi.parse.fattura.saveInContext' è abilitata");
  128.                     }
  129.                     metadati = (byte[]) metadatiBytesObject;
  130.                 }
  131.                
  132.                 Behaviour behaviour = new Behaviour();
  133.                
  134.                
  135.                 // responseTo
  136.                
  137.                 BehaviourResponseTo responseTo = new BehaviourResponseTo();
  138.                 responseTo.setResponseTo(true);
  139.                 behaviour.setResponseTo(responseTo);
  140.                 OpenSPCoop2Message replyTo = msg.getFactory().createEmptyMessage(msg.getMessageType(),MessageRole.RESPONSE);
  141.                
  142.                 SDIImbustamento sdiImbustamento = new SDIImbustamento((SDIBustaBuilder) gestoreMessaggioRichiesta.getProtocolFactory().createBustaBuilder(state));
  143.                
  144.                 sdiImbustamento.creaRisposta_ServizioRicezioneFatture_AzioneRiceviFatture(gestoreMessaggioRichiesta.getProtocolFactory(),
  145.                         state, busta, replyTo);
  146.                 behaviour.getResponseTo().setMessage(replyTo);
  147.                

  148.                
  149.                 // forwardTo
  150.                 List<byte[]> listForwardToList = new ArrayList<byte[]>();
  151.                 List<Object> listForwardToObjectList = new ArrayList<>();
  152.                
  153.                 if(fatturaPAObject==null){
  154.                    
  155.                     listForwardToList = SDILottoUtils.splitLotto(fatturaBytes);
  156.                    
  157.                 }
  158.                 else{
  159.                     if(fatturaPAObject instanceof it.gov.fatturapa.sdi.fatturapa.v1_0.FatturaElettronicaType){
  160.                         it.gov.fatturapa.sdi.fatturapa.v1_0.FatturaElettronicaType fattura =
  161.                                 (it.gov.fatturapa.sdi.fatturapa.v1_0.FatturaElettronicaType) fatturaPAObject;
  162.                         for (int i = 0; i < fattura.sizeFatturaElettronicaBodyList(); i++) {
  163.                            
  164.                             it.gov.fatturapa.sdi.fatturapa.v1_0.FatturaElettronicaType fatturaSingola =
  165.                                     new it.gov.fatturapa.sdi.fatturapa.v1_0.FatturaElettronicaType();
  166.                             fatturaSingola.setVersione(fattura.getVersione());
  167.                             fatturaSingola.setFatturaElettronicaHeader(fattura.getFatturaElettronicaHeader());
  168.                             fatturaSingola.addFatturaElettronicaBody(fattura.getFatturaElettronicaBody(i));
  169.                            
  170.                             it.gov.fatturapa.sdi.fatturapa.v1_0.utils.serializer.JaxbSerializer serializer =
  171.                                     new it.gov.fatturapa.sdi.fatturapa.v1_0.utils.serializer.JaxbSerializer();
  172.                             it.gov.fatturapa.sdi.fatturapa.v1_0.ObjectFactory of = new it.gov.fatturapa.sdi.fatturapa.v1_0.ObjectFactory();
  173.                             byte[] xml = serializer.toByteArray(of.createFatturaElettronica(fatturaSingola));
  174.                             listForwardToList.add(xml);
  175.                             listForwardToObjectList.add(fatturaSingola);
  176.                         }
  177.                     }
  178.                     else if(fatturaPAObject instanceof it.gov.fatturapa.sdi.fatturapa.v1_1.FatturaElettronicaType){
  179.                         it.gov.fatturapa.sdi.fatturapa.v1_1.FatturaElettronicaType fattura =
  180.                                 (it.gov.fatturapa.sdi.fatturapa.v1_1.FatturaElettronicaType) fatturaPAObject;
  181.                         for (int i = 0; i < fattura.sizeFatturaElettronicaBodyList(); i++) {
  182.                            
  183.                             it.gov.fatturapa.sdi.fatturapa.v1_1.FatturaElettronicaType fatturaSingola =
  184.                                     new it.gov.fatturapa.sdi.fatturapa.v1_1.FatturaElettronicaType();
  185.                             fatturaSingola.setVersione(fattura.getVersione());
  186.                             fatturaSingola.setFatturaElettronicaHeader(fattura.getFatturaElettronicaHeader());
  187.                             fatturaSingola.addFatturaElettronicaBody(fattura.getFatturaElettronicaBody(i));
  188.                            
  189.                             it.gov.fatturapa.sdi.fatturapa.v1_1.utils.serializer.JaxbSerializer serializer =
  190.                                     new it.gov.fatturapa.sdi.fatturapa.v1_1.utils.serializer.JaxbSerializer();
  191.                             it.gov.fatturapa.sdi.fatturapa.v1_1.ObjectFactory of = new it.gov.fatturapa.sdi.fatturapa.v1_1.ObjectFactory();
  192.                             byte[] xml = serializer.toByteArray(of.createFatturaElettronica(fatturaSingola));
  193.                             listForwardToList.add(xml);
  194.                             listForwardToObjectList.add(fatturaSingola);
  195.                            
  196.                         }
  197.                     }
  198.                     else if(fatturaPAObject instanceof it.gov.agenziaentrate.ivaservizi.docs.xsd.fatture.v1_2.FatturaElettronicaType){
  199.                         it.gov.agenziaentrate.ivaservizi.docs.xsd.fatture.v1_2.FatturaElettronicaType fattura =
  200.                                 (it.gov.agenziaentrate.ivaservizi.docs.xsd.fatture.v1_2.FatturaElettronicaType) fatturaPAObject;
  201.                         for (int i = 0; i < fattura.sizeFatturaElettronicaBodyList(); i++) {
  202.                            
  203.                             it.gov.agenziaentrate.ivaservizi.docs.xsd.fatture.v1_2.FatturaElettronicaType fatturaSingola =
  204.                                     new it.gov.agenziaentrate.ivaservizi.docs.xsd.fatture.v1_2.FatturaElettronicaType();
  205.                             fatturaSingola.setVersione(fattura.getVersione());
  206.                             fatturaSingola.setFatturaElettronicaHeader(fattura.getFatturaElettronicaHeader());
  207.                             fatturaSingola.addFatturaElettronicaBody(fattura.getFatturaElettronicaBody(i));
  208.                            
  209.                             it.gov.agenziaentrate.ivaservizi.docs.xsd.fatture.v1_2.utils.serializer.JaxbSerializer serializer =
  210.                                     new it.gov.agenziaentrate.ivaservizi.docs.xsd.fatture.v1_2.utils.serializer.JaxbSerializer();
  211.                             it.gov.agenziaentrate.ivaservizi.docs.xsd.fatture.v1_2.ObjectFactory of = new it.gov.agenziaentrate.ivaservizi.docs.xsd.fatture.v1_2.ObjectFactory();
  212.                             byte[] xml = serializer.toByteArray(of.createFatturaElettronica(fatturaSingola));
  213.                             listForwardToList.add(xml);
  214.                             listForwardToObjectList.add(fatturaSingola);
  215.                            
  216.                         }
  217.                     }
  218.                     else{
  219.                         throw new CoreException("Tipo ["+fatturaPAObject.getClass().getName()+"] non gestito");
  220.                     }
  221.                 }
  222.                
  223.                 for (int j = 0; j < listForwardToList.size(); j++) {
  224.                     byte[] xml = listForwardToList.get(j);
  225.                    
  226.                     if(sdiProperties.isBehaviourCreaProtocolloSDI()){
  227.                         xml = imbustamentoSDI(busta, xml, metadati);
  228.                     }
  229.                    
  230.                     OpenSPCoop2MessageParseResult pr = msg.getFactory().
  231.                             envelopingMessage(msg.getMessageType(), MessageRole.REQUEST, HttpConstants.CONTENT_TYPE_TEXT_XML,
  232.                                     "OpenSPCoop", xml, null, openspcoop2Properties.getAttachmentsProcessingMode(),
  233.                                     true,
  234.                                     openspcoop2Properties.useSoapMessageReader(), openspcoop2Properties.getSoapMessageReaderBufferThresholdKb());
  235.                     OpenSPCoop2Message msgForwardTo = pr.getMessage_throwParseException();
  236.                    
  237.                     if(listForwardToObjectList!=null && (j<listForwardToObjectList.size())){
  238.                         Object fatturaSingola = listForwardToObjectList.get(j);
  239.                         msgForwardTo.addContextProperty(SDICostanti.SDI_MESSAGE_CONTEXT_FATTURA, fatturaSingola);
  240.                     }
  241.                    
  242.                     BehaviourForwardTo forwardTo = new BehaviourForwardTo();
  243.                     forwardTo.setMessage(msgForwardTo);
  244.                    
  245.                     if(!sdiProperties.isBehaviourCreaProtocolloSDI()){
  246.                         BehaviourForwardToConfiguration config = new BehaviourForwardToConfiguration();
  247.                         config.setSbustamentoInformazioniProtocollo(StatoFunzionalita.DISABILITATA);
  248.                         config.setSbustamentoSoap(StatoFunzionalita.CONFIGURAZIONE_ORIGINALE);
  249.                         forwardTo.setConfig(config);
  250.                     }
  251.                    
  252.                     Busta bustaNewMessaggio = busta.newInstance();
  253.                     int posizione = (j+1);
  254.                     bustaNewMessaggio.setID("Fattura"+posizione+"_"+busta.getID());
  255.                     bustaNewMessaggio.addProperty(SDICostanti.SDI_BUSTA_EXT_POSIZIONE_FATTURA_PA, posizione+"");
  256.                     forwardTo.setDescription("PosizioneFattura:"+posizione);
  257.                     forwardTo.setBusta(bustaNewMessaggio);
  258.                    
  259.                     behaviour.getForwardTo().add(forwardTo);
  260.                    
  261.                 }
  262.                
  263.                 return behaviour;
  264.             }catch(Exception e){
  265.                 throw new BehaviourException(e.getMessage(),e);
  266.             }
  267.            
  268.         }
  269.        
  270.         else{
  271.             return super.behaviour(gestoreMessaggioRichiesta, busta, pa, requestInfo);
  272.         }
  273.        
  274.     }

  275.    
  276.     private byte[] imbustamentoSDI(Busta busta,byte[]xml,byte[]metadati) throws SerializerException{
  277.         it.gov.fatturapa.sdi.ws.ricezione.v1_0.types.ObjectFactory of = new it.gov.fatturapa.sdi.ws.ricezione.v1_0.types.ObjectFactory();
  278.         it.gov.fatturapa.sdi.ws.ricezione.v1_0.types.FileSdIConMetadatiType sdi = new it.gov.fatturapa.sdi.ws.ricezione.v1_0.types.FileSdIConMetadatiType();
  279.         sdi.setIdentificativoSdI(busta.getProperty(SDICostanti.SDI_BUSTA_EXT_IDENTIFICATIVO_SDI));
  280.         sdi.setNomeFile(busta.getProperty(SDICostanti.SDI_BUSTA_EXT_NOME_FILE));
  281.         sdi.setFile(xml);
  282.         sdi.setNomeFileMetadati(busta.getProperty(SDICostanti.SDI_BUSTA_EXT_NOME_FILE_METADATI));
  283.         sdi.setMetadati(metadati);
  284.         it.gov.fatturapa.sdi.ws.ricezione.v1_0.types.utils.serializer.JaxbSerializer serializerWSRicezione = new it.gov.fatturapa.sdi.ws.ricezione.v1_0.types.utils.serializer.JaxbSerializer();
  285.         return serializerWSRicezione.toByteArray(of.createFileSdIConMetadati(sdi));
  286.     }  
  287. }