SDISbustamento.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.builder;

  21. import java.util.Iterator;
  22. import java.util.List;

  23. import javax.xml.soap.AttachmentPart;
  24. import javax.xml.soap.MimeHeaders;
  25. import javax.xml.soap.SOAPBody;
  26. import javax.xml.soap.SOAPElement;

  27. import org.openspcoop2.message.OpenSPCoop2Message;
  28. import org.openspcoop2.message.OpenSPCoop2MessageFactory;
  29. import org.openspcoop2.message.OpenSPCoop2SoapMessage;
  30. import org.openspcoop2.message.exception.MessageException;
  31. import org.openspcoop2.message.exception.MessageNotSupportedException;
  32. import org.openspcoop2.message.soap.DumpSoapMessageUtils;
  33. import org.openspcoop2.message.soap.SoapUtils;
  34. import org.openspcoop2.message.soap.TunnelSoapUtils;
  35. import org.openspcoop2.message.soap.mtom.MTOMUtilities;
  36. import org.openspcoop2.message.utils.MessageUtilities;
  37. import org.openspcoop2.protocol.sdi.config.SDIProperties;
  38. import org.openspcoop2.protocol.sdi.constants.SDICostanti;
  39. import org.openspcoop2.protocol.sdi.constants.SDICostantiServizioRiceviNotifica;
  40. import org.openspcoop2.protocol.sdi.constants.SDICostantiServizioRicezioneFatture;
  41. import org.openspcoop2.protocol.sdi.constants.SDICostantiServizioTrasmissioneFatture;
  42. import org.openspcoop2.protocol.sdi.utils.SDIUtils;
  43. import org.openspcoop2.protocol.sdk.Busta;
  44. import org.openspcoop2.protocol.sdk.ProtocolException;
  45. import org.openspcoop2.utils.Utilities;
  46. import org.openspcoop2.utils.dch.MailcapActivationReader;
  47. import org.openspcoop2.utils.io.Base64Utilities;
  48. import org.openspcoop2.utils.transport.http.HttpConstants;
  49. import org.w3c.dom.Element;
  50. import org.w3c.dom.Node;

  51. import it.gov.fatturapa.sdi.messaggi.v1_0.constants.TipiMessaggi;
  52. import it.gov.fatturapa.sdi.ws.ricezione.v1_0.types.constants.EsitoNotificaType;

  53. /**
  54.  * SDISbustamento
  55.  *
  56.  * @author Andrea Poli (apoli@link.it)
  57.  * @author $Author$
  58.  * @version $Rev$, $Date$
  59.  */
  60. public class SDISbustamento {

  61.     private SDIBustaBuilder bustaBuilder = null;
  62.     @SuppressWarnings("unused")
  63.     public SDISbustamento(SDIBustaBuilder bustaBuilder){
  64.         this.bustaBuilder = bustaBuilder;
  65.     }
  66.    
  67.     public SOAPElement sbustamentoRisposta_ServizioSdIRiceviFile_AzioneRiceviFile(Busta busta,OpenSPCoop2Message msgParam) throws ProtocolException{
  68.         try{
  69.             OpenSPCoop2SoapMessage msg = msgParam.castAsSoap();
  70.            
  71.             SOAPElement element = null;
  72.                        
  73.             SOAPBody soapBody = msg.getSOAPBody();
  74.            
  75.             // estraggo header
  76.             element = SDIUtils.readHeader(msg);
  77.                    
  78.             // detach body
  79.             soapBody.removeContents();
  80.            
  81.             // se sono presenti degli attachments li elimino
  82.             if(msg.countAttachments()>0){
  83.                 msg.removeAllAttachments();
  84.             }
  85.            
  86.             // imposto content type
  87.             msg.setContentType(MessageUtilities.getDefaultContentType(msg.getMessageType()));
  88.            
  89.             return element;
  90.            
  91.         }catch(Exception e){
  92.             throw new ProtocolException(e.getMessage(),e);
  93.         }
  94.     }
  95.    
  96.     public SOAPElement sbustamentoRichiesta_ServizioRicezioneFatture_AzioneRiceviFatture(Busta busta,OpenSPCoop2Message msgParam) throws ProtocolException{
  97.        
  98.         try{
  99.             OpenSPCoop2SoapMessage msg = msgParam.castAsSoap();
  100.             OpenSPCoop2MessageFactory messageFactory = msgParam.getFactory();
  101.            
  102.             SOAPElement element = null;
  103.             Object ctxFatturaPA = msg.removeContextProperty(SDICostanti.SDI_MESSAGE_CONTEXT_FATTURA);
  104.             String versioneFattura = busta.getProperty(SDICostanti.SDI_BUSTA_EXT_VERSIONE_FATTURA_PA);
  105.                        
  106.             SOAPBody soapBody = msg.getSOAPBody();
  107.            
  108.             // estraggo header
  109.             element = SDIUtils.readHeader(msg);
  110.            
  111.             // Leggo fattura
  112.             byte [] xmlFattura = null;
  113.             boolean p7m = false;
  114.             if(ctxFatturaPA!=null){
  115.                
  116.                 //System.out.println("OTTIMIZZATO");
  117.                 if(SDICostanti.SDI_VERSIONE_FATTURA_PA_10.equals(versioneFattura)){
  118.                     it.gov.fatturapa.sdi.fatturapa.v1_0.FatturaElettronicaType fattura = (it.gov.fatturapa.sdi.fatturapa.v1_0.FatturaElettronicaType) ctxFatturaPA;
  119.                     it.gov.fatturapa.sdi.fatturapa.v1_0.ObjectFactory of = new it.gov.fatturapa.sdi.fatturapa.v1_0.ObjectFactory();
  120.                     it.gov.fatturapa.sdi.fatturapa.v1_0.utils.serializer.JaxbSerializer serializer = new it.gov.fatturapa.sdi.fatturapa.v1_0.utils.serializer.JaxbSerializer();
  121.                     xmlFattura = serializer.toByteArray(of.createFatturaElettronica(fattura));
  122.                 }
  123.                 else if(SDICostanti.SDI_VERSIONE_FATTURA_PA_11.equals(versioneFattura)){
  124.                     it.gov.fatturapa.sdi.fatturapa.v1_1.FatturaElettronicaType fattura = (it.gov.fatturapa.sdi.fatturapa.v1_1.FatturaElettronicaType) ctxFatturaPA;
  125.                     it.gov.fatturapa.sdi.fatturapa.v1_1.ObjectFactory of = new it.gov.fatturapa.sdi.fatturapa.v1_1.ObjectFactory();
  126.                     it.gov.fatturapa.sdi.fatturapa.v1_1.utils.serializer.JaxbSerializer serializer = new it.gov.fatturapa.sdi.fatturapa.v1_1.utils.serializer.JaxbSerializer();
  127.                     xmlFattura = serializer.toByteArray(of.createFatturaElettronica(fattura));
  128.                 }
  129.                 else if(SDICostanti.SDI_VERSIONE_FATTURA_SEMPLIFICATA_10.equals(versioneFattura)){
  130.                     it.gov.agenziaentrate.ivaservizi.docs.xsd.fatture.v1_0.FatturaElettronicaType fattura = (it.gov.agenziaentrate.ivaservizi.docs.xsd.fatture.v1_0.FatturaElettronicaType) ctxFatturaPA;
  131.                     it.gov.agenziaentrate.ivaservizi.docs.xsd.fatture.v1_0.ObjectFactory of = new it.gov.agenziaentrate.ivaservizi.docs.xsd.fatture.v1_0.ObjectFactory();
  132.                     it.gov.agenziaentrate.ivaservizi.docs.xsd.fatture.v1_0.utils.serializer.JaxbSerializer serializer = new it.gov.agenziaentrate.ivaservizi.docs.xsd.fatture.v1_0.utils.serializer.JaxbSerializer();
  133.                     xmlFattura = serializer.toByteArray(of.createFatturaElettronicaSemplificata(fattura));
  134.                 }
  135.                 else{
  136.                     it.gov.agenziaentrate.ivaservizi.docs.xsd.fatture.v1_2.FatturaElettronicaType fattura = (it.gov.agenziaentrate.ivaservizi.docs.xsd.fatture.v1_2.FatturaElettronicaType) ctxFatturaPA;
  137.                     it.gov.agenziaentrate.ivaservizi.docs.xsd.fatture.v1_2.ObjectFactory of = new it.gov.agenziaentrate.ivaservizi.docs.xsd.fatture.v1_2.ObjectFactory();
  138.                     it.gov.agenziaentrate.ivaservizi.docs.xsd.fatture.v1_2.utils.serializer.JaxbSerializer serializer = new it.gov.agenziaentrate.ivaservizi.docs.xsd.fatture.v1_2.utils.serializer.JaxbSerializer();
  139.                     xmlFattura = serializer.toByteArray(of.createFatturaElettronica(fattura));
  140.                 }
  141.                
  142.             }else{
  143.                
  144.                 //System.out.println("NON OTTIMIZZATO");
  145.                 Element elementBody = SoapUtils.getNotEmptyFirstChildSOAPElement(soapBody);
  146.                 List<Node> childs = SoapUtils.getNotEmptyChildNodes(messageFactory, elementBody, false);
  147.                 for (int i = 0; i < childs.size(); i++) {
  148.                     Node child = childs.get(i);
  149.                     if(SDICostantiServizioRicezioneFatture.RICEVI_FATTURE_RICHIESTA_ELEMENT_FILE.equals(child.getLocalName())){
  150.                         Element e = MTOMUtilities.getIfExistsXomReference(messageFactory, (Element)child);
  151.                         if(e!=null){
  152.                             //System.out.println("NON OTTIMIZZATO MTOM");
  153.                             // mtom
  154.                             AttachmentPart ap = this.getAttachmentPart(msg, e);
  155.                             if(ap!=null) {
  156.                                 xmlFattura = Utilities.getAsByteArray(ap.getDataHandler().getInputStream());
  157.                             }
  158.                         }else{
  159.                             //System.out.println("NON OTTIMIZZATO NO MTOM");
  160.                             // no mtom
  161.                             xmlFattura = Base64Utilities.decode(child.getTextContent());
  162.                         }
  163.                     }
  164.                 }
  165.                
  166.                 // Se la fattura e' un P7M non e' un xml.
  167.                 // Il formato viene compreso durante la validazione sintattica
  168.                 Object formato = busta.getProperty(SDICostanti.SDI_BUSTA_EXT_FORMATO_ARCHIVIO_INVIO_FATTURA);
  169.                 if(formato!=null && ((String)formato).equals(SDICostanti.SDI_TIPO_FATTURA_P7M) ){
  170.                     p7m = true;
  171.                 }
  172.             }
  173.            
  174.                    
  175.             // detach body
  176.             soapBody.removeContents();
  177.            
  178.             // se sono presenti degli attachments li elimino
  179.             if(msg.countAttachments()>0){
  180.                 msg.removeAllAttachments();
  181.             }
  182.                
  183.             // imposto content type
  184.             msg.setContentType(MessageUtilities.getDefaultContentType(msg.getMessageType()));
  185.            
  186.             // add Fattura as body
  187.             if(p7m){
  188.                 org.openspcoop2.utils.mime.MimeTypes mimeTypes = org.openspcoop2.utils.mime.MimeTypes.getInstance();
  189.                 TunnelSoapUtils.
  190.                     imbustamentoMessaggioConAttachment(msg,xmlFattura,HttpConstants.CONTENT_TYPE_OPENSPCOOP2_TUNNEL_SOAP,
  191.                             MailcapActivationReader.existsDataContentHandler(HttpConstants.CONTENT_TYPE_OPENSPCOOP2_TUNNEL_SOAP),
  192.                             mimeTypes.getMimeType(SDICostanti.SDI_TIPO_FATTURA_P7M), SDICostanti.SDI_PROTOCOL_NAMESPACE);
  193.                
  194.                 // Serve per forzare il tunnel SOAP che altrimenti non viene abilitato ('tunnel openspcoop2')
  195.                 DumpSoapMessageUtils.dumpMessage(msg, true);

  196.             }else{
  197.                 //soapBody.addChildElement(SoapUtils.getSoapFactory(msg.getMessageType()).createElement(this.xmlUtils.newElement(xmlFattura)));
  198.                 // Bug Fix: OP-752
  199.                
  200.                 // USO Comunque il tunnel SOAP altrimenti l'xml viene modificato e la firma non e' piu' valida
  201.                 // Uso esattamente il codice sopra utilizzando pero' come content type text/xml:
  202.                 TunnelSoapUtils.
  203.                 imbustamentoMessaggioConAttachment(msg,xmlFattura,HttpConstants.CONTENT_TYPE_OPENSPCOOP2_TUNNEL_SOAP,
  204.                         MailcapActivationReader.existsDataContentHandler(HttpConstants.CONTENT_TYPE_OPENSPCOOP2_TUNNEL_SOAP),
  205.                         HttpConstants.CONTENT_TYPE_TEXT_XML, SDICostanti.SDI_PROTOCOL_NAMESPACE);
  206.            
  207.                 // Serve per forzare il tunnel SOAP che altrimenti non viene abilitato ('tunnel openspcoop2')
  208.                 DumpSoapMessageUtils.dumpMessage(msg, true);
  209.             }
  210.            
  211.            
  212.             return element;
  213.            
  214.         }catch(Exception e){
  215.             throw new ProtocolException(e.getMessage(),e);
  216.         }
  217.        
  218.     }
  219.    
  220.     public SOAPElement sbustamentoRisposta_ServizioSdIRiceviNotifica_AzioneNotificaEsito(Busta busta,OpenSPCoop2Message msgParam) throws ProtocolException{
  221.         try{
  222.            
  223.             OpenSPCoop2SoapMessage msg = msgParam.castAsSoap();
  224.             OpenSPCoop2MessageFactory messageFactory = msgParam.getFactory();
  225.            
  226.             SOAPElement element = null;
  227.             Object ctxNotificaScartoEsitoCommittente = msg.removeContextProperty(SDICostanti.SDI_MESSAGE_CONTEXT_MESSAGGIO_SERVIZIO_SDI);
  228.                        
  229.             SOAPBody soapBody = msg.getSOAPBody();
  230.            
  231.             // estraggo header
  232.             element = SDIUtils.readHeader(msg);
  233.            
  234.             // Leggo se presente una notifica scarto esito committente
  235.             byte [] xmlNotificaScartoEsitoCommittente = null;
  236.             if(ctxNotificaScartoEsitoCommittente!=null){
  237.                 //System.out.println("OTTIMIZZATO");
  238.                 it.gov.fatturapa.sdi.messaggi.v1_0.ScartoEsitoCommittenteType scarto = (it.gov.fatturapa.sdi.messaggi.v1_0.ScartoEsitoCommittenteType) ctxNotificaScartoEsitoCommittente;
  239.                 it.gov.fatturapa.sdi.messaggi.v1_0.ObjectFactory of = new it.gov.fatturapa.sdi.messaggi.v1_0.ObjectFactory();
  240.                 it.gov.fatturapa.sdi.messaggi.v1_0.utils.serializer.JaxbSerializer serializer = new it.gov.fatturapa.sdi.messaggi.v1_0.utils.serializer.JaxbSerializer();
  241.                 xmlNotificaScartoEsitoCommittente = serializer.toByteArray(of.createScartoEsitoCommittente(scarto));
  242.                
  243.             }else{
  244.                
  245.                 //System.out.println("NON OTTIMIZZATO");
  246.                 Element elementBody = SoapUtils.getNotEmptyFirstChildSOAPElement(soapBody);
  247.                 List<Node> childs = SoapUtils.getNotEmptyChildNodes(messageFactory, elementBody, false);
  248.                 for (int i = 0; i < childs.size(); i++) {
  249.                     Node child = childs.get(i);
  250.                     if(SDICostantiServizioRiceviNotifica.NOTIFICA_ESITO_RISPOSTA_ELEMENT_SCARTO_ESITO.equals(child.getLocalName())){
  251.                        
  252.                         List<Node> elementScartoChilds = SoapUtils.getNotEmptyChildNodes(messageFactory, child, false);
  253.                         if(elementScartoChilds!=null){
  254.                             for (int j = 0; j < elementScartoChilds.size(); j++) {
  255.                                 Node scartoChild = elementScartoChilds.get(j);
  256.                        
  257.                                 if(SDICostantiServizioRiceviNotifica.NOTIFICA_ESITO_RISPOSTA_ELEMENT_SCARTO_ESITO_FILE.equals(scartoChild.getLocalName())){
  258.                                    
  259.                                     Element e = MTOMUtilities.getIfExistsXomReference(messageFactory, (Element)scartoChild);
  260.                                     if(e!=null){
  261.                                         //System.out.println("NON OTTIMIZZATO MTOM");
  262.                                         // mtom
  263.                                         AttachmentPart ap = this.getAttachmentPart(msg, e);
  264.                                         if(ap!=null) {
  265.                                             xmlNotificaScartoEsitoCommittente = Utilities.getAsByteArray(ap.getDataHandler().getInputStream());
  266.                                         }
  267.                                     }else{
  268.                                         //System.out.println("NON OTTIMIZZATO NO MTOM");
  269.                                         // no mtom
  270.                                         xmlNotificaScartoEsitoCommittente = Base64Utilities.decode(scartoChild.getTextContent());
  271.                                     }
  272.                                    
  273.                                 }
  274.                             }
  275.                         }
  276.                        
  277.                     }
  278.                 }
  279.             }
  280.                    
  281.             // detach body
  282.             soapBody.removeContents();
  283.            
  284.             // se sono presenti degli attachments li elimino
  285.             if(msg.countAttachments()>0){
  286.                 msg.removeAllAttachments();
  287.             }
  288.            
  289.             // imposto content type
  290.             msg.setContentType(MessageUtilities.getDefaultContentType(msg.getMessageType()));
  291.            
  292.             // se esiste uno scarto committente lo aggiungo come body
  293.             if(xmlNotificaScartoEsitoCommittente!=null){
  294.                 //soapBody.addChildElement(SoapUtils.getSoapFactory(msg.getMessageType()).createElement(this.xmlUtils.newElement(xmlNotificaScartoEsitoCommittente)));
  295.                 // Bug Fix: OP-752
  296.                
  297.                 // USO Comunque il tunnel SOAP altrimenti l'xml viene modificato e la firma non e' piu' valida
  298.                 // Uso esattamente il codice sopra utilizzando pero' come content type text/xml:
  299.                 TunnelSoapUtils.
  300.                 imbustamentoMessaggioConAttachment(msg,xmlNotificaScartoEsitoCommittente,HttpConstants.CONTENT_TYPE_OPENSPCOOP2_TUNNEL_SOAP,
  301.                         MailcapActivationReader.existsDataContentHandler(HttpConstants.CONTENT_TYPE_OPENSPCOOP2_TUNNEL_SOAP),
  302.                         HttpConstants.CONTENT_TYPE_TEXT_XML, SDICostanti.SDI_PROTOCOL_NAMESPACE);
  303.            
  304.                 // Serve per forzare il tunnel SOAP che altrimenti non viene abilitato ('tunnel openspcoop2')
  305.                 DumpSoapMessageUtils.dumpMessage(msg, true);
  306.             }
  307.            
  308.             // se lo sdi ha restituito un esito non ok imposto 500 come codice di trasporto verso il client
  309.             String esitoNotifica = busta.getProperty(SDICostanti.SDI_BUSTA_EXT_ESITO_NOTIFICA);
  310.             // ES01 = NOTIFICA ACCETTATA
  311.             if(EsitoNotificaType.ES01.name().equals(esitoNotifica)){
  312.                 msg.setForcedResponseCode("202");
  313.             }
  314.             else{
  315.                 // ES00 = NOTIFICA NON ACCETTATA
  316.                 if(EsitoNotificaType.ES00.name().equals(esitoNotifica)){
  317.                     msg.setForcedResponseCode("200");
  318.                 }
  319.             }
  320.            
  321.             return element;
  322.            
  323.         }catch(Exception e){
  324.             throw new ProtocolException(e.getMessage(),e);
  325.         }
  326.     }
  327.    
  328.    
  329.    
  330.     public SOAPElement sbustamentoRichiesta_ServizioTrasmissioneFatture_Notifiche(TipiMessaggi tipoMessaggio, Busta busta,OpenSPCoop2Message msgParam) throws ProtocolException{
  331.        
  332.         try{
  333.        
  334.             OpenSPCoop2SoapMessage msg = msgParam.castAsSoap();
  335.             OpenSPCoop2MessageFactory messageFactory = msgParam.getFactory();
  336.            
  337.             SOAPElement element = null;
  338.             Object ctxMessaggio = msg.removeContextProperty(SDICostanti.SDI_MESSAGE_CONTEXT_MESSAGGIO_SERVIZIO_SDI);
  339.                        
  340.             SOAPBody soapBody = msg.getSOAPBody();
  341.            
  342.             // estraggo header
  343.             element = SDIUtils.readHeader(msg);
  344.            
  345.             // Leggo fattura
  346.             byte [] xmlNotifica = null;
  347.             byte [] zip = null;
  348.             if(ctxMessaggio!=null){
  349.                
  350.                 //System.out.println("OTTIMIZZATO");
  351.                
  352.                 it.gov.fatturapa.sdi.messaggi.v1_0.ObjectFactory of = new it.gov.fatturapa.sdi.messaggi.v1_0.ObjectFactory();
  353.                 it.gov.fatturapa.sdi.messaggi.v1_0.utils.serializer.JaxbSerializer serializer = new it.gov.fatturapa.sdi.messaggi.v1_0.utils.serializer.JaxbSerializer();
  354.                            
  355.                 switch (tipoMessaggio) {
  356.                 case RC:
  357.                     it.gov.fatturapa.sdi.messaggi.v1_0.RicevutaConsegnaType rc = (it.gov.fatturapa.sdi.messaggi.v1_0.RicevutaConsegnaType) ctxMessaggio;
  358.                     xmlNotifica = serializer.toByteArray(of.createRicevutaConsegna(rc));
  359.                     break;
  360.                    
  361.                 case MC:
  362.                     it.gov.fatturapa.sdi.messaggi.v1_0.NotificaMancataConsegnaType mc = (it.gov.fatturapa.sdi.messaggi.v1_0.NotificaMancataConsegnaType) ctxMessaggio;
  363.                     xmlNotifica = serializer.toByteArray(of.createNotificaMancataConsegna(mc));
  364.                     break;
  365.                    
  366.                 case NS:
  367.                     it.gov.fatturapa.sdi.messaggi.v1_0.NotificaScartoType ns = (it.gov.fatturapa.sdi.messaggi.v1_0.NotificaScartoType) ctxMessaggio;
  368.                     xmlNotifica = serializer.toByteArray(of.createNotificaScarto(ns));
  369.                     break;
  370.                    
  371.                 case NE:
  372.                     it.gov.fatturapa.sdi.messaggi.v1_0.NotificaEsitoType ne = (it.gov.fatturapa.sdi.messaggi.v1_0.NotificaEsitoType) ctxMessaggio;
  373.                     xmlNotifica = serializer.toByteArray(of.createNotificaEsito(ne));
  374.                     break;
  375.                    
  376.                 case DT:
  377.                     it.gov.fatturapa.sdi.messaggi.v1_0.NotificaDecorrenzaTerminiType dt = (it.gov.fatturapa.sdi.messaggi.v1_0.NotificaDecorrenzaTerminiType) ctxMessaggio;
  378.                     xmlNotifica = serializer.toByteArray(of.createNotificaDecorrenzaTermini(dt));
  379.                     break;
  380.                    
  381.                 case AT:
  382.                     it.gov.fatturapa.sdi.messaggi.v1_0.AttestazioneTrasmissioneFatturaType at = (it.gov.fatturapa.sdi.messaggi.v1_0.AttestazioneTrasmissioneFatturaType) ctxMessaggio;
  383.                     xmlNotifica = serializer.toByteArray(of.createAttestazioneTrasmissioneFattura(at));
  384.                     break;

  385.                 default:
  386.                     break;
  387.                 }
  388.                
  389.             }else{
  390.                
  391.                 if(TipiMessaggi.AT.equals(tipoMessaggio) && SDIProperties.getInstance(this.bustaBuilder.getLog()).isEnableAccessoMessaggi()){
  392.                    
  393.                     Object oZip = msg.removeContextProperty(SDICostanti.SDI_MESSAGE_CONTEXT_AT_ARCHIVIO_ZIP);
  394.                     if(oZip!=null){
  395.                         zip = (byte[]) oZip;
  396.                     }
  397.                     else{
  398.                         Object oXml = msg.removeContextProperty(SDICostanti.SDI_MESSAGE_CONTEXT_AT_ARCHIVIO_XML);
  399.                         if(oXml!=null){
  400.                             xmlNotifica = (byte[]) oXml;
  401.                         }
  402.                         else{
  403.                             throw new Exception("Contenuto della notifica di Attestazione Trasmissione per Impossibilità di Recapito");
  404.                         }
  405.                     }
  406.                    
  407.                 }
  408.                 else{
  409.                     //System.out.println("NON OTTIMIZZATO");
  410.                     Element elementBody = SoapUtils.getNotEmptyFirstChildSOAPElement(soapBody);
  411.                     List<Node> childs = SoapUtils.getNotEmptyChildNodes(messageFactory, elementBody, false);
  412.                     for (int i = 0; i < childs.size(); i++) {
  413.                         Node child = childs.get(i);
  414.                         if(SDICostantiServizioTrasmissioneFatture.FILE_SDI_TYPE_CONSEGNA_RICHIESTA_ELEMENT_FILE.equals(child.getLocalName())){
  415.                             Element e = MTOMUtilities.getIfExistsXomReference(messageFactory, (Element)child);
  416.                             if(e!=null){
  417.                                 //System.out.println("NON OTTIMIZZATO MTOM");
  418.                                 // mtom
  419.                                 AttachmentPart ap = this.getAttachmentPart(msg, e);
  420.                                 if(ap!=null) {
  421.                                     xmlNotifica = Utilities.getAsByteArray(ap.getDataHandler().getInputStream());
  422.                                 }
  423.                             }else{
  424.                                 //System.out.println("NON OTTIMIZZATO NO MTOM");
  425.                                 // no mtom
  426.                                 xmlNotifica = Base64Utilities.decode(child.getTextContent());
  427.                             }
  428.                         }
  429.                     }
  430.                    
  431.                     if(TipiMessaggi.AT.equals(tipoMessaggio)) {
  432.                         // xmlNotifica contiene uno zip file poiche' non l'ho trattato
  433.                         zip = xmlNotifica;
  434.                     }
  435.                 }
  436.             }
  437.            
  438.                    
  439.             // detach body
  440.             soapBody.removeContents();
  441.            
  442.             // se sono presenti degli attachments li elimino
  443.             if(msg.countAttachments()>0){
  444.                 msg.removeAllAttachments();
  445.             }
  446.            
  447.             // imposto content type
  448.             msg.setContentType(MessageUtilities.getDefaultContentType(msg.getMessageType()));
  449.            
  450.             // add Fattura as body
  451.             if(zip!=null){
  452.                 TunnelSoapUtils.
  453.                     imbustamentoMessaggioConAttachment(msg,zip,HttpConstants.CONTENT_TYPE_OPENSPCOOP2_TUNNEL_SOAP,
  454.                             MailcapActivationReader.existsDataContentHandler(HttpConstants.CONTENT_TYPE_OPENSPCOOP2_TUNNEL_SOAP),
  455.                             HttpConstants.CONTENT_TYPE_ZIP, SDICostanti.SDI_PROTOCOL_NAMESPACE);
  456.                
  457.                 // Serve per forzare il tunnel SOAP che altrimenti non viene abilitato ('tunnel openspcoop2')
  458.                 DumpSoapMessageUtils.dumpMessage(msg, true);

  459.             }else{
  460.                 //soapBody.addChildElement(SoapUtils.getSoapFactory(msg.getMessageType()).createElement(this.xmlUtils.newElement(xmlNotifica)));
  461.                 // Bug Fix: OP-752
  462.                
  463.                 // USO Comunque il tunnel SOAP altrimenti l'xml viene modificato e la firma non e' piu' valida
  464.                 // Uso esattamente il codice sopra utilizzando pero' come content type text/xml:
  465.                 TunnelSoapUtils.
  466.                 imbustamentoMessaggioConAttachment(msg,xmlNotifica,HttpConstants.CONTENT_TYPE_OPENSPCOOP2_TUNNEL_SOAP,
  467.                         MailcapActivationReader.existsDataContentHandler(HttpConstants.CONTENT_TYPE_OPENSPCOOP2_TUNNEL_SOAP),
  468.                         HttpConstants.CONTENT_TYPE_TEXT_XML, SDICostanti.SDI_PROTOCOL_NAMESPACE);
  469.            
  470.                 // Serve per forzare il tunnel SOAP che altrimenti non viene abilitato ('tunnel openspcoop2')
  471.                 DumpSoapMessageUtils.dumpMessage(msg, true);
  472.             }
  473.                
  474.             return element;
  475.            
  476.         }catch(Exception e){
  477.             throw new ProtocolException(e.getMessage(),e);
  478.         }
  479.        
  480.     }
  481.    
  482.    
  483.     private AttachmentPart getAttachmentPart(OpenSPCoop2SoapMessage msg,Element e) throws MessageException, MessageNotSupportedException{
  484.         String contentId = MTOMUtilities.getCidXomReference(e);
  485.         MimeHeaders mhs = new MimeHeaders();
  486.         mhs.addHeader(HttpConstants.CONTENT_ID, contentId);
  487.         Iterator<?> itAttachments = msg.getAttachments(mhs);
  488.         if(itAttachments == null || itAttachments.hasNext()==false){
  489.             throw new MessageException("Found XOM Reference with attribute ["+
  490.                     org.openspcoop2.message.soap.mtom.Costanti.XOP_INCLUDE_ATTRIBUTE_HREF+"]=["+contentId+"] but the message hasn't attachments");
  491.         }
  492.         AttachmentPart ap = null;
  493.         while (itAttachments.hasNext()) {
  494.             if(ap!=null){
  495.                 throw new MessageException("Found XOM Reference with attribute ["+
  496.                         org.openspcoop2.message.soap.mtom.Costanti.XOP_INCLUDE_ATTRIBUTE_HREF+"]=["+contentId+"] but exists more than one attachment with same id");
  497.             }
  498.             ap = (AttachmentPart) itAttachments.next();
  499.         }
  500.         return ap;
  501.     }
  502. }