TunnelSoapUtils.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.message.soap;


  21. import java.io.ByteArrayInputStream;
  22. import java.io.ByteArrayOutputStream;
  23. import java.io.InputStream;
  24. import java.io.OutputStream;
  25. import java.util.List;

  26. import javax.activation.DataHandler;
  27. import javax.mail.util.ByteArrayDataSource;
  28. import javax.xml.namespace.QName;
  29. import javax.xml.soap.AttachmentPart;
  30. import javax.xml.soap.SOAPBody;
  31. import javax.xml.soap.SOAPElement;
  32. import javax.xml.soap.SOAPEnvelope;
  33. import javax.xml.soap.SOAPFault;
  34. import javax.xml.transform.Source;
  35. import javax.xml.transform.dom.DOMSource;

  36. import org.apache.commons.io.output.CountingOutputStream;
  37. import org.openspcoop2.message.OpenSPCoop2DataContentHandler;
  38. import org.openspcoop2.message.OpenSPCoop2DataContentHandlerInputStream;
  39. import org.openspcoop2.message.OpenSPCoop2Message;
  40. import org.openspcoop2.message.OpenSPCoop2MessageFactory;
  41. import org.openspcoop2.message.OpenSPCoop2SoapMessage;
  42. import org.openspcoop2.message.constants.Costanti;
  43. import org.openspcoop2.message.constants.MessageRole;
  44. import org.openspcoop2.message.constants.MessageType;
  45. import org.openspcoop2.message.constants.ServiceBinding;
  46. import org.openspcoop2.message.exception.MessageException;
  47. import org.openspcoop2.message.exception.MessageNotSupportedException;
  48. import org.openspcoop2.message.xml.MessageXMLUtils;
  49. import org.openspcoop2.utils.CopyStream;
  50. import org.openspcoop2.utils.UtilsException;
  51. import org.openspcoop2.utils.transport.http.HttpConstants;
  52. import org.w3c.dom.Element;
  53. import org.w3c.dom.Node;


  54. /**
  55.  * Libreria contenente metodi utili per la gestione della busta Soap.
  56.  *
  57.  *
  58.  * @author Poli Andrea (apoli@link.it)
  59.  * @author $Author$
  60.  * @version $Rev$, $Date$
  61.  */

  62. public class TunnelSoapUtils {
  63.    
  64.    
  65.    
  66.    
  67.     /* ********  ALLEGA BODY  ******** */
  68.    
  69.     public static void allegaBody(OpenSPCoop2Message message, String ns) throws MessageException{
  70.        
  71.         if(ServiceBinding.SOAP.equals(message.getServiceBinding())==false){
  72.             throw new MessageException("Funzionalita 'ScartaBody' valida solamente per Service Binding SOAP");
  73.         }
  74.         OpenSPCoop2SoapMessage soapMessage = message.castAsSoap();              
  75.        
  76.         try{
  77.        
  78.             // E' permesso SOLO per messaggi senza attachment
  79.             if (soapMessage.countAttachments() > 0) {
  80.                 throw new MessageException("La funzionalita' non e' permessa per messaggi SOAP With Attachments");
  81.             }
  82.            
  83.             SOAPBody body = soapMessage.getSOAPPart().getEnvelope().getBody();
  84.             if(body==null){
  85.                 throw new Exception("Body non presente");
  86.             }
  87.             if(body.hasFault()){
  88.                 throw new Exception("Body contenente un SOAPFault");
  89.             }
  90.             List<Node> listNode = SoapUtils.getNotEmptyChildNodes(message.getFactory(), body, false);
  91.             boolean bodyWithMultiRootElement = false;
  92.             if(listNode!=null && listNode.size()>1){
  93.                 //System.out.println("MULTI ELEMENT: "+listNode.size());
  94.                 bodyWithMultiRootElement = true;
  95.             }
  96.             byte[] bodySbustato = TunnelSoapUtils.sbustamentoSOAPEnvelope(message.getFactory(), soapMessage.getSOAPPart().getEnvelope());
  97.             AttachmentPart ap = null;
  98.             if(bodyWithMultiRootElement){
  99.                 //System.out.println("OCTECT");
  100.                 org.openspcoop2.utils.dch.InputStreamDataSource isSource = new org.openspcoop2.utils.dch.InputStreamDataSource("MultiRootElement",
  101.                         HttpConstants.CONTENT_TYPE_APPLICATION_OCTET_STREAM, bodySbustato);
  102.                 ap = soapMessage.createAttachmentPart(new DataHandler(isSource));
  103.             }else{
  104.                 //System.out.println("XML");
  105.                 Element e = null;
  106.                 try{
  107.                     e = MessageXMLUtils.getInstance(message.getFactory()).newElement(bodySbustato);
  108.                     Source streamSource = new DOMSource(e);
  109.                     ap = soapMessage.createAttachmentPart();
  110.                     ap.setContent(streamSource, HttpConstants.CONTENT_TYPE_TEXT_XML);
  111.                 }catch(Exception eParse){
  112.                     org.openspcoop2.utils.dch.InputStreamDataSource isSource = new org.openspcoop2.utils.dch.InputStreamDataSource("BodyNotParsable",
  113.                             HttpConstants.CONTENT_TYPE_APPLICATION_OCTET_STREAM, bodySbustato);
  114.                     ap = soapMessage.createAttachmentPart(new DataHandler(isSource));
  115.                 }
  116.             }
  117.             ap.setContentId(soapMessage.createContentID(ns));
  118.             soapMessage.addAttachmentPart(ap);
  119.            
  120.             // Aggiungo contentID all'attachmet contenente la SOAPEnvelope
  121.             // Necessario per essere compatibile con alcune implementazioni, es axis14,
  122.             // altrimenti essendo il ContentType senza Start element, Axis14 utilizza come xml per costruire la SOAPEnvelope
  123.             // il primo attachment nel messaggio MIME che contiene il ContentID.
  124.             soapMessage.getSOAPPart().addMimeHeader(HttpConstants.CONTENT_ID, soapMessage.createContentID(ns));
  125.            
  126.             // Rimuovo contenuti del body
  127.             soapMessage.getSOAPBody().removeContents();
  128.            
  129.         }catch(Exception e){
  130.             throw new MessageException(e.getMessage(),e);
  131.         }
  132.     }
  133.    
  134.    
  135.    
  136.    
  137.    
  138.    
  139.    
  140.     /* ********  S B U S T A M E N T O    M E S S A G G I  ******** */

  141.     public static boolean isTunnelOpenSPCoopSoap(OpenSPCoop2SoapMessage message) throws MessageException, MessageNotSupportedException{
  142.         return isTunnelOpenSPCoopSoap(message.getFactory(), message.getSOAPBody());
  143.     }
  144.     public static boolean isTunnelOpenSPCoopSoap(OpenSPCoop2MessageFactory messageFactory, SOAPBody body){
  145.         List<Node> bodyChildren = SoapUtils.getNotEmptyChildNodes(messageFactory, body);
  146.         if(body!=null &&
  147.                 bodyChildren.size() > 0 &&
  148.                 bodyChildren.get(0)!=null &&
  149.                 "SOAPTunnel".equals(bodyChildren.get(0).getLocalName()) &&
  150.                 "http://www.govway.org/out/xml2soap".equals(bodyChildren.get(0).getNamespaceURI()) &&
  151.                 org.openspcoop2.utils.Costanti.OPENSPCOOP2.equals(bodyChildren.get(0).getPrefix())){
  152.             return true;
  153.         }else{
  154.             return false;
  155.         }
  156.     }
  157.    
  158.     public static String getContentTypeTunnelOpenSPCoopSoap(SOAPBody body) throws MessageException {
  159.         if(body!=null && body.hasChildNodes()){
  160.             return ((SOAPElement)body.getChildElements().next()).getValue();
  161.         }else{
  162.             throw new MessageException("Body non presente");
  163.         }
  164.     }
  165.    

  166.     public static void sbustamentoMessaggio(OpenSPCoop2Message msgParam,OutputStream streamParam) throws MessageException, MessageNotSupportedException{

  167.         if(!MessageType.SOAP_11.equals(msgParam.getMessageType()) && !MessageType.SOAP_12.equals(msgParam.getMessageType())){
  168.             throw MessageNotSupportedException.newMessageNotSupportedException(msgParam.getMessageType());
  169.         }
  170.         OpenSPCoop2SoapMessage msg = msgParam.castAsSoap();
  171.        
  172.         CountingOutputStream cout = null;
  173.         try{
  174.                        
  175.             // Nota: non viene usato DocumentToStream, poiche' altrimenti viene prodotto l'<?xml ... />
  176.             cout = new CountingOutputStream(streamParam);
  177.            
  178.             //  Sbustamento Senza Attachments
  179.             if(msg.countAttachments() == 0){
  180.                 SOAPBody bd = msg.getSOAPBody();
  181.                 if(bd.hasFault()){
  182.                     SOAPFault fault = bd.getFault();
  183.                     cout.write(msg.getAsByte(fault, true));
  184.                 }else{
  185.                     java.util.Iterator<?> it = bd.getChildElements();
  186.                     while(it.hasNext()){
  187.                         Object bodyObject = it.next();
  188.                         if(!(bodyObject instanceof SOAPElement)) continue;
  189.                         SOAPElement bodyElement = (SOAPElement) bodyObject;
  190.                         cout.write(msg.getAsByte(bodyElement, true));
  191.                     }
  192.                 }
  193.             }
  194.             // Sbustamento Con Attachmnets
  195.             else{
  196.                 SOAPBody body = msg.getSOAPBody();
  197.                 if(TunnelSoapUtils.isTunnelOpenSPCoopSoap(msgParam.getFactory(), body)){
  198.                     // Sbustamento OpenSPCoop
  199.                     AttachmentPart ap  = (AttachmentPart) msg.getAttachments().next();
  200.                     Object o = ap.getContent();
  201.                     //DataHandler dh = ap.getDataHandler();
  202.                     //InputStream inputDH = (InputStream) dh.getContent();
  203.                     InputStream inputDH = null;
  204.                     if(o instanceof OpenSPCoop2DataContentHandlerInputStream){
  205.                         inputDH = (OpenSPCoop2DataContentHandlerInputStream) o;
  206.                     }
  207.                     else if(o instanceof InputStream){
  208.                         inputDH = (InputStream) OpenSPCoop2DataContentHandler.getContent((InputStream)o);
  209.                     }
  210.                     else if(o instanceof byte[]){
  211.                         inputDH = (InputStream) OpenSPCoop2DataContentHandler.getContent(new ByteArrayInputStream((byte[]) o));
  212.                     } else {
  213.                         throw new Exception("Tipo non gestito: "+o.getClass().getName());
  214.                     }
  215.                     java.io.ByteArrayOutputStream bout = new java.io.ByteArrayOutputStream();
  216.                     byte [] readB = new byte[8192];
  217.                     int readByte = 0;
  218.                     while((readByte = inputDH.read(readB))!= -1)
  219.                         bout.write(readB,0,readByte);
  220.                     inputDH.close();
  221.                     bout.close();
  222.                     cout.write(bout.toByteArray());
  223.                 }else{
  224.                
  225.                     ByteArrayOutputStream sbustamentoAttachments = new ByteArrayOutputStream();
  226.                     msg.writeTo(sbustamentoAttachments, true);
  227.                     String msgString = sbustamentoAttachments.toString();
  228.                     byte [] msgByte =  sbustamentoAttachments.toByteArray();
  229.                     String soapEnvelopeStart = "<" + msg.getSOAPPart().getEnvelope().getPrefix() + ":" + msg.getSOAPPart().getEnvelope().getLocalName();
  230.                     String xmlTagDecode = "<?xml";
  231.                     String soapEnvelopeStop =  "</" + msg.getSOAPPart().getEnvelope().getPrefix() + ":" + msg.getSOAPPart().getEnvelope().getLocalName()+">";
  232.                     //System.out.println("SoapStart: "+soapEnvelopeStart);
  233.                     //System.out.println("SoapStartIndexOf: "+msgString.indexOf(soapEnvelopeStart));
  234.                     //System.out.println("SoapStop: "+soapEnvelopeStop);
  235.                     //System.out.println("SoapStopIndexOf: "+msgString.indexOf(soapEnvelopeStop));
  236.                     // Prima parte del Multipart
  237.                     if(msgString.indexOf(xmlTagDecode)!=-1){
  238.                         cout.write(msgByte,0,msgString.indexOf(xmlTagDecode));
  239.                     }else{
  240.                         cout.write(msgByte,0,msgString.indexOf(soapEnvelopeStart));
  241.                     }
  242.                     // Body
  243.                     SOAPBody bd = msg.getSOAPBody();
  244.                     if(bd.hasFault()){
  245.                         SOAPFault fault = bd.getFault();
  246.                         cout.write(msg.getAsByte(fault, true));
  247.                     }else{
  248.                         cout.write(msg.getAsByte(msg.getFirstChildElement(bd), true));
  249.                     }
  250.                     // Resto degli attachments
  251.                     int indexOf = msgString.indexOf(soapEnvelopeStop)+soapEnvelopeStop.length();
  252.                     cout.write(msgByte,indexOf,msgByte.length - indexOf);
  253.                 }
  254.             }
  255.            
  256.             // Aggiorno le lunghezze del messaggio (normalmente le aggiorna la writeTo)
  257.             msg.updateIncomingMessageContentLength();
  258.             // Come dimensione di uscita utilizzo i bytes che produco
  259.             cout.flush();
  260.             msg.updateOutgoingMessageContentLength(cout.getByteCount());
  261.            
  262.         }
  263.         catch (MessageException e){
  264.             throw e;
  265.         }
  266.         catch (MessageNotSupportedException e){
  267.             throw e;
  268.         }
  269.         catch (Exception e){
  270.             throw new MessageException("Sbustamento msg_inputStream non riuscito: "+e.getMessage(),e);
  271.         }finally{
  272.             try{
  273.                 if(cout!=null)
  274.                     cout.close();
  275.             }catch(Exception e){
  276.                 // close
  277.             }
  278.         }
  279.        
  280.        
  281.     }
  282.    
  283.     /**
  284.      * Ritorna i bytes del contenuto del messaggio Soap passato come parametro
  285.      *
  286.      * @param msg Messaggio Soap da sbustare
  287.      * @return byte del contenuto (sbustati dalla SoapEnvelope).
  288.      *
  289.      */
  290.     public static byte[] sbustamentoMessaggio(OpenSPCoop2Message msg) throws MessageException, MessageNotSupportedException{
  291.         ByteArrayOutputStream bodySbustato = new ByteArrayOutputStream();
  292.         try{
  293.             TunnelSoapUtils.sbustamentoMessaggio(msg,bodySbustato);
  294.             return bodySbustato.toByteArray();  
  295.         }
  296.         catch (MessageException e){
  297.             throw e;
  298.         }
  299.         catch (MessageNotSupportedException e){
  300.             throw e;
  301.         }
  302.         catch(Exception e){
  303.             try{
  304.                 if(bodySbustato!=null)
  305.                     bodySbustato.close();
  306.             }catch(Exception eis){
  307.                 // close
  308.             }  
  309.             throw new MessageException("Sbustamento msg non riuscito: "+e.getMessage(),e);
  310.         }
  311.     }

  312.     /**
  313.      * Ritorna i bytes del contenuto del messaggio Soap passato come parametro
  314.      *
  315.      * @param env SoapEnvelope da sbustare
  316.      *
  317.      */
  318.     public static byte[] sbustamentoSOAPEnvelope(OpenSPCoop2MessageFactory messageFactory, SOAPEnvelope env) throws MessageException, MessageNotSupportedException{
  319.         return sbustamentoSOAPEnvelope(messageFactory, env, true);
  320.     }
  321.     public static byte[] sbustamentoSOAPEnvelope(OpenSPCoop2MessageFactory messageFactory, SOAPEnvelope env, boolean consume) throws MessageException, MessageNotSupportedException{
  322.         ByteArrayOutputStream bout = null;
  323.        
  324.         try{
  325.             SOAPBody bd = env.getBody();
  326.             byte[] body = null;
  327.             if(bd.hasFault()){
  328.                 SOAPFault fault = bd.getFault();
  329.                 body = OpenSPCoop2MessageFactory.getAsByte(messageFactory, fault, consume);
  330.             }else{
  331.                 bout = new ByteArrayOutputStream();
  332.                 java.util.Iterator<?> it = bd.getChildElements();
  333.                 while(it.hasNext()){
  334.                     Object bodyElementObj = it.next();
  335.                     if(!(bodyElementObj instanceof SOAPElement)){
  336.                         continue;
  337.                     }
  338.                     SOAPElement bodyElement = (SOAPElement) bodyElementObj;
  339.                     bout.write(OpenSPCoop2MessageFactory.getAsByte(messageFactory, bodyElement, consume));
  340.                 }
  341.                 bout.flush();
  342.                 bout.close();
  343.                 body = bout.toByteArray();
  344.                 bout = null;
  345.             }

  346.             return body;

  347.         }
  348.         catch (Exception e){
  349.             throw new MessageException("Sbustamento SoapEnvelope non riuscito: "+e.getMessage(),e);
  350.         }
  351.         finally{
  352.             try{
  353.                 if(bout!=null)
  354.                     bout.close();
  355.             }catch(Exception eis){
  356.                 // close
  357.             }
  358.         }
  359.     }
  360.    
  361.    
  362.    
  363.    
  364.    
  365.    
  366.    

  367.    
  368.    
  369.    
  370.     /* ********  I M B U S T A M E N T O    M E S S A G G I  ******** */

  371.     /**
  372.      * Ritorna un messaggio che contiene i bytes in un attachment
  373.      *
  374.      * @param inputBody contenuto.
  375.      * @return msg Messaggio Soap imbustato
  376.      *
  377.      */
  378.     public static OpenSPCoop2Message imbustamentoMessaggioConAttachment(OpenSPCoop2MessageFactory messageFactory, MessageType messageType, MessageRole messageRole,
  379.             InputStream inputBody,String tipoAttachment,boolean buildAsDataHandler,String contentTypeMessaggioOriginale, String ns) throws MessageException, MessageNotSupportedException{
  380.        
  381.         if(!MessageType.SOAP_11.equals(messageType) && !MessageType.SOAP_12.equals(messageType)){
  382.             throw MessageNotSupportedException.newMessageNotSupportedException(messageType);
  383.         }
  384.         try{
  385.             // Metto inputBody in un byte[] proprio perche' il ByteArrayInputStream non deve essere chiuso.
  386.             java.io.ByteArrayOutputStream byteBuffer = new java.io.ByteArrayOutputStream();
  387. //          byte [] readB = new byte[Utilities.DIMENSIONE_BUFFER];
  388. //          int readByte = 0;
  389. //          while((readByte = inputBody.read(readB))!= -1){
  390. //              byteBuffer.write(readB,0,readByte);
  391. //          }
  392.             CopyStream.copy(inputBody, byteBuffer);
  393.             inputBody.close();
  394.             if(byteBuffer.size()==0){
  395.                 throw new MessageException("Contenuto da imbustare non presente");
  396.             }
  397.            
  398.             return TunnelSoapUtils.imbustamentoMessaggioConAttachment(messageFactory, messageType, messageRole, byteBuffer.toByteArray(), tipoAttachment, buildAsDataHandler, contentTypeMessaggioOriginale, ns);
  399.            
  400.         }
  401.         catch (MessageException e){
  402.             throw e;
  403.         }
  404.         catch (MessageNotSupportedException e){
  405.             throw e;
  406.         }
  407.         catch (Exception e){
  408.             throw new MessageException("Imbustamento msgConAttachment_inputStream non riuscito: "+e.getMessage(),e);
  409.         }
  410.     }
  411.    
  412.     public static OpenSPCoop2Message imbustamentoMessaggioConAttachment(OpenSPCoop2MessageFactory messageFactory, MessageType messageType, MessageRole messageRole,
  413.             byte [] inputBody,String tipoAttachment,boolean buildAsDataHandler,String contentTypeMessaggioOriginale, String ns) throws MessageException, MessageNotSupportedException{
  414.        
  415.         if(!MessageType.SOAP_11.equals(messageType) && !MessageType.SOAP_12.equals(messageType)){
  416.             throw MessageNotSupportedException.newMessageNotSupportedException(messageType);
  417.         }
  418.         OpenSPCoop2Message msg = null;
  419.         try{
  420.             msg = messageFactory.createEmptyMessage(messageType,messageRole);
  421.             return imbustamentoMessaggioConAttachment(msg, inputBody, tipoAttachment, buildAsDataHandler, contentTypeMessaggioOriginale, ns);
  422.         }
  423.         catch (MessageException e){
  424.             throw e;
  425.         }
  426.         catch (MessageNotSupportedException e){
  427.             throw e;
  428.         }
  429.         catch (Exception e){
  430.             throw new MessageException("Imbustamento msgConAttachment_inputStream non riuscito: "+e.getMessage(),e);
  431.         }
  432.     }
  433.     public static OpenSPCoop2Message imbustamentoMessaggioConAttachment(OpenSPCoop2Message msgParam,
  434.             byte [] inputBody,String tipoAttachment,boolean buildAsDataHandler,String contentTypeMessaggioOriginale, String ns) throws MessageException, MessageNotSupportedException{
  435.        
  436.         if(!MessageType.SOAP_11.equals(msgParam.getMessageType()) && !MessageType.SOAP_12.equals(msgParam.getMessageType())){
  437.             throw MessageNotSupportedException.newMessageNotSupportedException(msgParam.getMessageType());
  438.         }
  439.         OpenSPCoop2SoapMessage msg = msgParam.castAsSoap();
  440.         try{
  441.            
  442.             if(inputBody==null || inputBody.length<=0){
  443.                 throw new UtilsException("Contenuto da imbustare non presente");
  444.             }
  445.                        
  446.             SOAPBody soapBody = msg.getSOAPBody();
  447.             QName name = null;
  448.             if(HttpConstants.CONTENT_TYPE_OPENSPCOOP2_TUNNEL_SOAP.equals(tipoAttachment)){
  449.                 name = new QName(Costanti.SOAP_TUNNEL_NAMESPACE,
  450.                         Costanti.SOAP_TUNNEL_ATTACHMENT_ELEMENT_OPENSPCOOP2_TYPE,org.openspcoop2.utils.Costanti.OPENSPCOOP2);      
  451.             }else{
  452.                 name = new QName(Costanti.SOAP_TUNNEL_NAMESPACE,
  453.                         Costanti.SOAP_TUNNEL_ATTACHMENT_ELEMENT,org.openspcoop2.utils.Costanti.OPENSPCOOP2);    
  454.             }
  455.             SOAPElement bodyElement = soapBody.addChildElement(name);
  456.            
  457.             if(HttpConstants.CONTENT_TYPE_OPENSPCOOP2_TUNNEL_SOAP.equals(tipoAttachment)){
  458.                 if(contentTypeMessaggioOriginale==null){
  459.                     throw new Exception("ContentType messaggio per cui applicare il tunnel non definito?");
  460.                 }else{
  461.                     bodyElement.setValue(contentTypeMessaggioOriginale);
  462.                 }
  463.             }
  464.             AttachmentPart ap = msg.createAttachmentPart();        
  465.            
  466.             if(buildAsDataHandler){
  467.                 ap.setDataHandler(new DataHandler(new ByteArrayDataSource(inputBody,tipoAttachment)));
  468.                 TunnelSoapUtils.saveAttachmentOpenSPCoop(ap);      
  469.             }else{
  470.                 ap.setRawContentBytes(inputBody,0,inputBody.length,tipoAttachment);
  471.                 //ap.setContent(new ByteArrayInputStream(inputBody),tipoAttachment);
  472.             }
  473.             ap.setContentId(msg.createContentID(ns));
  474.             msg.addAttachmentPart(ap);
  475.             //System.out.println("debug - start");
  476.             //msg.writeTo(System.out);
  477.             //System.out.println(msg.getAsString(msg.getSOAPPart().getEnvelope()));
  478.             //System.out.println("debug - stop");
  479.             return msg;

  480.         }
  481.         catch (MessageException e){
  482.             throw e;
  483.         }
  484.         catch (MessageNotSupportedException e){
  485.             throw e;
  486.         }
  487.         catch (Exception e){
  488.             throw new MessageException("Imbustamento msgConAttachment_inputStream non riuscito: "+e.getMessage(),e);
  489.         }
  490.     }
  491.     private static void saveAttachmentOpenSPCoop(AttachmentPart ap) throws MessageException{
  492.         try{
  493.             // FIX: se non c'e' il dump abilitato serve il codice seguente per forzare il salvataggio, in caso si passi da DataHandler.
  494.             javax.activation.DataHandler dh= ap.getDataHandler();  
  495.             java.io.InputStream inputDH = dh.getInputStream();
  496.             java.io.ByteArrayOutputStream bout = new java.io.ByteArrayOutputStream();
  497.             byte [] readB = new byte[8192];
  498.             int readByte = 0;
  499.             while((readByte = inputDH.read(readB))!= -1)
  500.                 bout.write(readB,0,readByte);
  501.             inputDH.close();
  502.             bout.flush();
  503.             bout.close();
  504.             ap.setDataHandler(new DataHandler(bout.toByteArray(),ap.getContentType()));
  505.         }catch(Exception e){
  506.             throw new MessageException(e.getMessage(),e);
  507.         }
  508.     }
  509.    
  510.     /**
  511.      * Ritorna un messaggio  che contiene i byte in un attachment
  512.      *
  513.      * @param body byte del contenuto.
  514.      * @return msg Messaggio Soap imbustato
  515.      *
  516.      */
  517.     public static OpenSPCoop2Message imbustamentoMessaggioConAttachment(OpenSPCoop2MessageFactory messageFactory, MessageType messageType, MessageRole messageRole, byte [] body, String contentTypeMessaggioOriginale, String ns) throws MessageException, MessageNotSupportedException{
  518.        
  519.         if(!MessageType.SOAP_11.equals(messageType) && !MessageType.SOAP_12.equals(messageType)){
  520.             throw MessageNotSupportedException.newMessageNotSupportedException(messageType);
  521.         }
  522.        
  523.         OpenSPCoop2Message risposta = null;
  524.         try{        
  525.             risposta = TunnelSoapUtils.imbustamentoMessaggioConAttachment(messageFactory, messageType, messageRole, body,HttpConstants.CONTENT_TYPE_PLAIN,false, contentTypeMessaggioOriginale, ns);
  526.             return risposta;
  527.         }
  528.         catch (MessageException e){
  529.             throw e;
  530.         }
  531.         catch (MessageNotSupportedException e){
  532.             throw e;
  533.         }
  534.         catch (Exception e){
  535.             throw new MessageException("Imbustamento msgConAttachment non riuscito: "+e.getMessage(),e);
  536.         }
  537.     }

  538.    
  539.    
  540. }