DumpSoapMessageUtils.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.ByteArrayOutputStream;
  22. import java.io.InputStream;
  23. import java.util.ArrayList;
  24. import java.util.HashMap;
  25. import java.util.Iterator;
  26. import java.util.List;
  27. import java.util.Map;

  28. import javax.activation.DataHandler;
  29. import javax.xml.soap.AttachmentPart;
  30. import javax.xml.soap.SOAPPart;

  31. import org.openspcoop2.message.OpenSPCoop2SoapMessage;
  32. import org.openspcoop2.message.exception.MessageException;
  33. import org.openspcoop2.message.utils.DumpAttachment;
  34. import org.openspcoop2.message.utils.DumpMessaggio;
  35. import org.openspcoop2.message.utils.DumpMessaggioConfig;
  36. import org.openspcoop2.message.utils.DumpMessaggioMultipartInfo;
  37. import org.openspcoop2.message.xml.MessageXMLUtils;
  38. import org.openspcoop2.utils.UtilsMultiException;
  39. import org.openspcoop2.utils.dch.MailcapActivationReader;
  40. import org.openspcoop2.utils.transport.http.HttpConstants;


  41. /**
  42.  * DumpSoapMessageUtils
  43.  *
  44.  *
  45.  * @author Poli Andrea (apoli@link.it)
  46.  * @author $Author$
  47.  * @version $Rev$, $Date$
  48.  */
  49. public class DumpSoapMessageUtils {

  50.     public static DumpMessaggio dumpMessage(OpenSPCoop2SoapMessage msg,boolean dumpAllAttachments) throws MessageException{
  51.         return dumpMessage(msg, new DumpMessaggioConfig(), dumpAllAttachments);
  52.     }
  53.     public static DumpMessaggio dumpMessage(OpenSPCoop2SoapMessage msg,
  54.             DumpMessaggioConfig config,
  55.             boolean dumpAllAttachments) throws MessageException{
  56.         try{
  57.             DumpMessaggio dumpMessaggio = new DumpMessaggio();
  58.             dumpMessaggio.setMessageType(msg.getMessageType());
  59.             dumpMessaggio.setContentType(msg.getContentType());
  60.                        
  61.             Map<String, List<String>> pTrasporto = null;
  62.             if(msg.getTransportRequestContext()!=null) {
  63.                 if(msg.getTransportRequestContext().getHeaders()!=null &&
  64.                         msg.getTransportRequestContext().getHeaders().size()>0){
  65.                     if(config.isDumpHeaders()) {
  66.                         pTrasporto = msg.getTransportRequestContext().getHeaders();
  67.                     }
  68.                 }
  69.             }
  70.             else if(msg.getTransportResponseContext()!=null) {
  71.                 if(msg.getTransportResponseContext().getHeaders()!=null &&
  72.                         msg.getTransportResponseContext().getHeaders().size()>0){
  73.                     if(config.isDumpHeaders()) {
  74.                         pTrasporto = msg.getTransportResponseContext().getHeaders();
  75.                     }
  76.                 }
  77.             }
  78.             if(config.isDumpHeaders() && pTrasporto!=null) {
  79.                 Iterator<String> keys = pTrasporto.keySet().iterator();
  80.                 while (keys.hasNext()) {
  81.                     String key = (String) keys.next();
  82.                     if(key!=null){
  83.                         List<String> values = pTrasporto.get(key);
  84.                         dumpMessaggio.getHeadersValues().put(key, values);
  85.                     }
  86.                 }
  87.             }
  88.            
  89.             boolean msgWithAttachments = msg.countAttachments()>0;
  90.            
  91.             if(config.isDumpBody()) {
  92.                
  93.                 SOAPPart soapPart = msg.getSOAPPart();
  94.                
  95.                 DumpMessaggioMultipartInfo multipartInfoBody = null;
  96.                 if(msgWithAttachments) {
  97.                    
  98.                     Iterator<?> itM = soapPart.getAllMimeHeaders();
  99.                     if(itM!=null) {
  100.                         while(itM.hasNext()) {
  101.                             Object keyO = itM.next();
  102.                             if(keyO instanceof javax.xml.soap.MimeHeader) {
  103.                                 javax.xml.soap.MimeHeader mh = (javax.xml.soap.MimeHeader) keyO;
  104.                                 String key = mh.getName();
  105.                                
  106.                                 if(!HttpConstants.CONTENT_ID.equalsIgnoreCase(key) &&
  107.                                         !HttpConstants.CONTENT_LOCATION.equalsIgnoreCase(key) &&
  108.                                         !HttpConstants.CONTENT_TYPE.equalsIgnoreCase(key) &&
  109.                                         !config.isDumpMultipartHeaders()) {
  110.                                     continue;
  111.                                 }
  112.                                
  113.                                 List<String> l = new ArrayList<>();
  114.                                 String [] values = soapPart.getMimeHeader(key);
  115.                                 if(values!=null && values.length>0) {
  116.                                     for (String value : values) {
  117.                                         l.add(value);
  118.                                     }
  119.                                 }
  120.                                 else {
  121.                                     l.add(mh.getValue());
  122.                                 }
  123.                                
  124.                                 if(!l.isEmpty()) {
  125.                                     if(multipartInfoBody==null) {
  126.                                         multipartInfoBody = new DumpMessaggioMultipartInfo();
  127.                                     }
  128.                                    
  129.                                     if(HttpConstants.CONTENT_ID.equalsIgnoreCase(key)) {
  130.                                         multipartInfoBody.setContentId(l.get(0));
  131.                                     }
  132.                                     else if(HttpConstants.CONTENT_LOCATION.equalsIgnoreCase(key)) {
  133.                                         multipartInfoBody.setContentLocation(l.get(0));
  134.                                     }
  135.                                     else if(HttpConstants.CONTENT_TYPE.equalsIgnoreCase(key)) {
  136.                                         multipartInfoBody.setContentType(l.get(0));
  137.                                     }
  138.                                    
  139.                                     if(config.isDumpMultipartHeaders()) {
  140.                                         multipartInfoBody.getHeadersValues().put(key, l);
  141.                                     }
  142.                                 }
  143.                                
  144.                             }
  145.                         }
  146.                     }
  147.                    
  148.                 }
  149.                 if(multipartInfoBody!=null) {
  150.                     dumpMessaggio.setMultipartInfoBody(multipartInfoBody);
  151.                 }
  152.                
  153.                 ByteArrayOutputStream bout = new ByteArrayOutputStream();
  154.                 bout.write(msg.getAsByte(soapPart.getEnvelope(),false));
  155.                 bout.flush();
  156.                 bout.close();
  157.                 dumpMessaggio.setBody(bout);
  158.             }
  159.            
  160.             if(config.isDumpAttachments()) {
  161.                
  162.                 java.util.Iterator<?> it = msg.getAttachments();
  163.                 while(it.hasNext()){
  164.                     AttachmentPart ap =
  165.                         (AttachmentPart) it.next();
  166.                     DumpAttachment dumpAttach = new DumpAttachment();
  167.                    
  168.                     dumpAttach.setContentId(ap.getContentId());
  169.                     dumpAttach.setContentLocation(ap.getContentLocation());
  170.                     dumpAttach.setContentType(ap.getContentType());
  171.                    
  172.                     if(config.isDumpMultipartHeaders()) {
  173.                         Iterator<?> itM = ap.getAllMimeHeaders();
  174.                         if(itM!=null) {
  175.                             while(itM.hasNext()) {
  176.                                 Object keyO = itM.next();
  177.                                 if(keyO instanceof javax.xml.soap.MimeHeader) {
  178.                                     javax.xml.soap.MimeHeader mh = (javax.xml.soap.MimeHeader) keyO;
  179.                                     String key = mh.getName();
  180.                                    
  181.                                     List<String> l = new ArrayList<>();
  182.                                     String [] values = ap.getMimeHeader(key);
  183.                                     if(values!=null && values.length>0) {
  184.                                         for (String value : values) {
  185.                                             l.add(value);
  186.                                         }
  187.                                     }
  188.                                     else {
  189.                                         l.add(mh.getValue());
  190.                                     }
  191.                                    
  192.                                     if(!l.isEmpty()) {
  193.                                         dumpAttach.getHeadersValues().put(key, l);
  194.                                     }
  195.                                 }
  196.                             }
  197.                         }
  198.                     }
  199.                    
  200.                     ByteArrayOutputStream boutAttach = null;
  201.                     if(dumpAllAttachments){
  202.                         boutAttach = (ByteArrayOutputStream) _dumpAttachment(msg, ap, true);
  203.                     }else{
  204.                         Object o = _dumpAttachment(msg, ap, false);
  205.                         if(o == null){
  206.                             dumpAttach.setErrorContentNotSerializable("Contenuto attachment non recuperato??");
  207.                         }
  208.                         else if(o instanceof String){
  209.                             boutAttach = new ByteArrayOutputStream();
  210.                             boutAttach.write(((String)o).getBytes());
  211.                             boutAttach.flush();
  212.                             boutAttach.close();
  213.                         }
  214.                         else if(o instanceof java.io.ByteArrayOutputStream){
  215.                             boutAttach = (java.io.ByteArrayOutputStream) o;
  216.                         }
  217.                         else{
  218.                             dumpAttach.setErrorContentNotSerializable("Contenuto attachment non è visualizzabile, tipo: "+o.getClass().getName());
  219.                         }
  220.                     }
  221.                     dumpAttach.setContent(boutAttach);
  222.                    
  223.                     if(dumpMessaggio.getAttachments()==null) {
  224.                         dumpMessaggio.setAttachments(new ArrayList<>());
  225.                     }
  226.                     dumpMessaggio.getAttachments().add(dumpAttach);
  227.                 }
  228.                
  229.             }
  230.            
  231.             return dumpMessaggio;
  232.         }catch(Exception e){
  233.             throw new MessageException(e.getMessage(),e);
  234.         }
  235.     }
  236.    
  237.     public static String dumpMessageAsString(DumpMessaggio msg, boolean dumpAllAttachments) throws MessageException{
  238.         return dumpMessageAsString(msg, new DumpMessaggioConfig(),dumpAllAttachments);
  239.     }
  240.     public static String dumpMessageAsString(DumpMessaggio msg,
  241.             DumpMessaggioConfig config, boolean dumpAllAttachments) throws MessageException{
  242.         try{
  243.             StringBuilder out = new StringBuilder(msg.toString(config,dumpAllAttachments));
  244.             return out.toString();
  245.         }catch(Exception e){
  246.             throw new MessageException(e.getMessage(),e);
  247.         }
  248.     }
  249.    
  250.     public static String dumpMessageAsString(OpenSPCoop2SoapMessage msg,
  251.             boolean dumpAllAttachments) throws MessageException{
  252.         return dumpMessageAsString(msg, new DumpMessaggioConfig(), dumpAllAttachments);
  253.     }
  254.     public static String dumpMessageAsString(OpenSPCoop2SoapMessage msg,
  255.             DumpMessaggioConfig config,
  256.             boolean dumpAllAttachments) throws MessageException{
  257.         try{
  258.             StringBuilder out = new StringBuilder();
  259.            
  260.             Map<String, List<String>> pTrasporto = null;
  261.             if(msg.getTransportRequestContext()!=null) {
  262.                 if(msg.getTransportRequestContext().getHeaders()!=null &&
  263.                         msg.getTransportRequestContext().getHeaders().size()>0){
  264.                     if(config.isDumpHeaders()) {
  265.                         pTrasporto = msg.getTransportRequestContext().getHeaders();
  266.                     }
  267.                 }
  268.             }
  269.             else if(msg.getTransportResponseContext()!=null) {
  270.                 if(msg.getTransportResponseContext().getHeaders()!=null &&
  271.                         msg.getTransportResponseContext().getHeaders().size()>0){
  272.                     if(config.isDumpHeaders()) {
  273.                         pTrasporto = msg.getTransportResponseContext().getHeaders();
  274.                     }
  275.                 }
  276.             }
  277.             if(config.isDumpHeaders()) {
  278.                 out.append("------ Header di trasporto ------\n");
  279.                 if(pTrasporto!=null && pTrasporto.size()>0) {
  280.                     Iterator<String> keys = pTrasporto.keySet().iterator();
  281.                     while (keys.hasNext()) {
  282.                         String key = (String) keys.next();
  283.                         if(key!=null){
  284.                             List<String> values = pTrasporto.get(key);
  285.                             if(values!=null && !values.isEmpty()) {
  286.                                 for (String value : values) {
  287.                                     out.append("- "+key+": "+value+"\n");  
  288.                                 }
  289.                             }
  290.                         }
  291.                     }
  292.                 }
  293.                 else {
  294.                     out.append("Non presenti\n");
  295.                 }
  296.             }
  297.            
  298.             boolean msgWithAttachments = msg.countAttachments()>0;
  299.            
  300.             if(config.isDumpBody()) {
  301.                 out.append("------ SOAPEnvelope (ContentType: "+msg.getContentType()+") (MessageType: "+msg.getMessageType()+") ------\n");
  302.                
  303.                 SOAPPart soapPart = msg.getSOAPPart();
  304.                
  305.                 if(msgWithAttachments) {
  306.                    
  307.                     Map<String, List<String>> mime = new HashMap<>();
  308.                     Iterator<?> itM = soapPart.getAllMimeHeaders();
  309.                     if(itM!=null) {
  310.                         while(itM.hasNext()) {
  311.                             Object keyO = itM.next();
  312.                             if(keyO instanceof String) {
  313.                                 String key = (String) keyO;
  314.                                
  315.                                 if(!HttpConstants.CONTENT_ID.equalsIgnoreCase(key) &&
  316.                                         !HttpConstants.CONTENT_LOCATION.equalsIgnoreCase(key) &&
  317.                                         !HttpConstants.CONTENT_TYPE.equalsIgnoreCase(key) &&
  318.                                         !config.isDumpMultipartHeaders()) {
  319.                                     continue;
  320.                                 }
  321.                                
  322.                                 List<String> l = new ArrayList<>();
  323.                                 String [] values = soapPart.getMimeHeader(key);
  324.                                 if(values!=null && values.length>0) {
  325.                                     for (String value : values) {
  326.                                         l.add(value);
  327.                                     }
  328.                                 }
  329.                                
  330.                                 if(!l.isEmpty()) {
  331.                                     if(HttpConstants.CONTENT_ID.equalsIgnoreCase(key)) {
  332.                                         mime.put(key, l);
  333.                                     }
  334.                                     else if(HttpConstants.CONTENT_LOCATION.equalsIgnoreCase(key)) {
  335.                                         mime.put(key, l);
  336.                                     }
  337.                                     else if(HttpConstants.CONTENT_TYPE.equalsIgnoreCase(key)) {
  338.                                         mime.put(key, l);
  339.                                     }
  340.                                     else if(config.isDumpMultipartHeaders()) {
  341.                                         mime.put(key, l);
  342.                                     }
  343.                                 }

  344.                             }
  345.                         }
  346.                     }
  347.                    
  348.                     if(mime.size()>0) {
  349.                         out.append("\n*** MimePart Header ***\n");
  350.                         Iterator<String> it = mime.keySet().iterator();
  351.                         while (it.hasNext()) {
  352.                             String key = (String) it.next();
  353.                             List<String> l = mime.get(key);
  354.                             if(l!=null && !l.isEmpty()) {
  355.                                 for (String v : l) {
  356.                                     out.append("- "+key+": "+v+"\n");          
  357.                                 }
  358.                             }
  359.                         }
  360.                     }
  361.                    
  362.                 }
  363.                
  364.                 out.append("\n");
  365.                
  366.                 out.append(msg.getAsString(soapPart.getEnvelope(),false));
  367.             }
  368.            
  369.             if(config.isDumpAttachments()) {
  370.                 java.util.Iterator<?> it = msg.getAttachments();
  371.                 int index = 1;
  372.                 while(it.hasNext()){
  373.                     AttachmentPart ap =
  374.                         (AttachmentPart) it.next();
  375.                     out.append("\n------ Attachment-"+index+" ------\n");
  376.                    
  377.                     out.append("\n*** MimePart Header ***\n");
  378.                     if(ap.getContentId()!=null) {
  379.                         out.append("- "+HttpConstants.CONTENT_ID+": "+ap.getContentId()+"\n");
  380.                     }
  381.                     if(ap.getContentLocation()!=null) {
  382.                         out.append("- "+HttpConstants.CONTENT_LOCATION+": "+ap.getContentLocation()+"\n");
  383.                     }
  384.                     if(ap.getContentType()!=null) {
  385.                         out.append("- "+HttpConstants.CONTENT_LOCATION+": "+ap.getContentType()+"\n");
  386.                     }

  387.                     if(config.isDumpMultipartHeaders()) {
  388.                         Iterator<?> itM = ap.getAllMimeHeaders();
  389.                         if(itM!=null) {
  390.                             while(itM.hasNext()) {
  391.                                 Object keyO = itM.next();
  392.                                 if(keyO instanceof String) {
  393.                                     String key = (String) keyO;
  394.                                     if(HttpConstants.CONTENT_ID.equalsIgnoreCase(key) ||
  395.                                             HttpConstants.CONTENT_LOCATION.equalsIgnoreCase(key) ||
  396.                                             HttpConstants.CONTENT_TYPE.equalsIgnoreCase(key)) {
  397.                                         continue;
  398.                                     }
  399.                                     String [] values = ap.getMimeHeader(key);
  400.                                     if(values!=null && values.length>0) {
  401.                                         for (String value : values) {
  402.                                             out.append("- "+key+": "+value+"\n");
  403.                                         }
  404.                                     }
  405.                                 }
  406.                             }
  407.                         }
  408.                     }
  409.                     out.append("\n");
  410.                                    
  411.                     if(dumpAllAttachments){
  412.                         out.append(DumpSoapMessageUtils.dumpAttachment(msg, ap));
  413.                     }else{
  414.                         //Object o = ap.getContent(); NON FUNZIONA CON TOMCAT
  415.                         Object o = ap.getDataHandler().getContent();
  416.                         //System.out.println("["+o.getClass().getName()+"])"+ap.getContentType()+"(");
  417.                         if(HttpConstants.CONTENT_TYPE_OPENSPCOOP2_TUNNEL_SOAP.equals(ap.getContentType())){
  418.                              // reimposto handler
  419.                              DumpSoapMessageUtils.rebuildAttachmentAsByteArray(msg, ap);
  420.                         }
  421.                        
  422.                         if(o instanceof String){
  423.                             out.append((String)o);
  424.                         }else{
  425.                              out.append("Contenuto attachments non è visualizzabile, tipo: "+o.getClass().getName());
  426.                         }
  427.                     }
  428.                     index++;
  429.                 }
  430.             }
  431.             return out.toString();
  432.         }catch(Exception e){
  433.             throw new MessageException(e.getMessage(),e);
  434.         }
  435.     }
  436.    
  437.     public static String dumpAttachment(OpenSPCoop2SoapMessage msg,AttachmentPart ap) throws MessageException{
  438.         Object o = _dumpAttachment(msg, ap, false);
  439.         // Metodo sopra non torna mai null, segnalato da sonarqube
  440.         /*if(o == null){
  441.             throw new MessageException("Dump error (return null reference)");
  442.         }*/
  443.         if(o instanceof String){
  444.             return (String) o;
  445.         }
  446.         else if(o instanceof java.io.ByteArrayOutputStream){
  447.             java.io.ByteArrayOutputStream bout = null;
  448.             try{
  449.                 bout = (java.io.ByteArrayOutputStream) o;
  450.                 return bout.toString();
  451.             }finally{
  452.                 try{
  453.                     if(bout!=null){
  454.                         bout.close();
  455.                     }
  456.                 }catch(Exception eClose){
  457.                     // close
  458.                 }
  459.             }
  460.         }
  461.         else{
  462.             throw new MessageException("Dump error (return type "+o.getClass().getName()+" unknown)");
  463.         }
  464.     }
  465.     public static byte[] dumpAttachmentAsByteArray(OpenSPCoop2SoapMessage msg,AttachmentPart ap) throws MessageException{
  466.         Object o = _dumpAttachment(msg, ap, false);
  467.         // Metodo sopra non torna mai null, segnalato da sonarqube
  468.         /*if(o == null){
  469.             throw new MessageException("Dump error (return null reference)");
  470.         }*/
  471.         if(o instanceof String){
  472.             return ((String) o).getBytes();
  473.         }
  474.         else if(o instanceof java.io.ByteArrayOutputStream){
  475.             java.io.ByteArrayOutputStream bout = null;
  476.             try{
  477.                 bout = (java.io.ByteArrayOutputStream) o;
  478.                 return bout.toByteArray();
  479.             }finally{
  480.                 try{
  481.                     if(bout!=null){
  482.                         bout.close();
  483.                     }
  484.                 }catch(Exception eClose){
  485.                     // close
  486.                 }
  487.             }
  488.         }
  489.         else{
  490.             throw new MessageException("Dump error (return type "+o.getClass().getName()+" unknown)");
  491.         }
  492.     }
  493.    
  494.     private static Object _dumpAttachment(OpenSPCoop2SoapMessage msg,AttachmentPart ap, boolean forceReturnAsByteArrayOutputStream) throws MessageException{
  495.         try{        
  496.             java.io.ByteArrayOutputStream bout = null;
  497.             //Object o = ap.getContent(); NON FUNZIONA CON TOMCAT
  498.             //java.io.InputStream inputDH = dh.getInputStream(); NON FUNZIONA CON JBOSS7 e JAVA7 e imbustamentoSOAP con GestioneManifest e rootElementMaggioreUno (tipo: application/octet-stream)
  499.             Object o = ap.getDataHandler().getContent();
  500.             String s = null;
  501.             boolean forceRebuildDataHandler = false;
  502.             if(o!=null){
  503.                 if(o instanceof byte[]){
  504.                     byte[] b = (byte[]) o;
  505.                     bout = new ByteArrayOutputStream();
  506.                     bout.write(b);
  507.                     bout.flush();
  508.                     bout.close();
  509.                 }
  510.                 else if(o instanceof InputStream){
  511.                     InputStream is = (InputStream) o;
  512.                     bout = new java.io.ByteArrayOutputStream();
  513.                     byte [] readB = new byte[8192];
  514.                     int readByte = 0;
  515.                     while((readByte = is.read(readB))!= -1)
  516.                         bout.write(readB,0,readByte);
  517.                     is.close();
  518.                     bout.flush();
  519.                     bout.close();
  520.                 }
  521.                 else if(o instanceof String){
  522.                     s = (String) o;
  523.                     bout = new ByteArrayOutputStream();
  524.                     bout.write(s.getBytes());
  525.                     bout.flush();
  526.                     bout.close();
  527.                 }
  528.                 else{
  529.                     // Provo con codiceOriginale ma in jboss non funziona sempre
  530.                     try {
  531.                         javax.activation.DataHandler dh= ap.getDataHandler();  
  532.                         java.io.InputStream inputDH = dh.getInputStream();
  533.                         bout = new java.io.ByteArrayOutputStream();
  534.                         byte [] readB = new byte[8192];
  535.                         int readByte = 0;
  536.                         while((readByte = inputDH.read(readB))!= -1)
  537.                             bout.write(readB,0,readByte);
  538.                         inputDH.close();
  539.                         bout.flush();
  540.                         bout.close();      
  541.                     }catch(Exception e) {
  542.                         if(o instanceof javax.xml.transform.dom.DOMSource) {
  543.                             try {
  544.                                 javax.xml.transform.dom.DOMSource domSource = (javax.xml.transform.dom.DOMSource) o;    
  545.                                 bout = new ByteArrayOutputStream();
  546.                                 bout.write(MessageXMLUtils.getInstance(msg.getFactory()).toByteArray(domSource.getNode()));
  547.                                 bout.flush();
  548.                                 bout.close();
  549.                                 forceRebuildDataHandler = true;
  550.                             }catch(Exception eInternal) {
  551.                                 throw new UtilsMultiException(e, eInternal);
  552.                             }
  553.                         }
  554.                         else {
  555.                             throw e;
  556.                         }
  557.                     }
  558.                 }
  559.             }
  560.             else{
  561.                 // Provo con codiceOriginale ma in jboss non funziona sempre
  562.                 javax.activation.DataHandler dh= ap.getDataHandler();  
  563.                 java.io.InputStream inputDH = dh.getInputStream();
  564.                 bout = new java.io.ByteArrayOutputStream();
  565.                 byte [] readB = new byte[8192];
  566.                 int readByte = 0;
  567.                 while((readByte = inputDH.read(readB))!= -1)
  568.                     bout.write(readB,0,readByte);
  569.                 inputDH.close();
  570.                 bout.flush();
  571.                 bout.close();      
  572.             }
  573.             // Per non perdere quanto letto
  574.             if(HttpConstants.CONTENT_TYPE_OPENSPCOOP2_TUNNEL_SOAP.equals(ap.getContentType())){
  575.                  // reimposto handler
  576.                  DumpSoapMessageUtils.rebuildAttachmentAsByteArray(msg, ap);
  577.             }
  578.             else if(forceRebuildDataHandler){
  579.                  // reimposto handler
  580.                  DumpSoapMessageUtils.rebuildDataHandlerAttachment(msg, ap, bout);
  581.             }
  582.             else if(MailcapActivationReader.existsDataContentHandler(ap.getContentType())){
  583.                 //ap.setDataHandler(new javax.activation.DataHandler(bout.toByteArray(),ap.getContentType()));
  584.                 // In axiom l'update del data handler non funziona
  585.                 if(ap.getContentType()!=null && ap.getContentType().startsWith(HttpConstants.CONTENT_TYPE_PLAIN)){
  586.                     if(s!=null){
  587.                         msg.updateAttachmentPart(ap, s, ap.getContentType());
  588.                     }
  589.                     else{
  590.                         msg.updateAttachmentPart(ap, bout.toString(),ap.getContentType());
  591.                     }
  592.                 }else{
  593.                     msg.updateAttachmentPart(ap, bout.toByteArray(),ap.getContentType());
  594.                 }
  595.             }
  596.             if(s!=null){
  597.                 if(forceReturnAsByteArrayOutputStream) {
  598.                     return bout;
  599.                 }
  600.                 else {
  601.                     return s;
  602.                 }
  603.             }else{
  604.                 return bout;
  605.             }
  606.         }catch(Exception e){
  607.             throw new MessageException(e.getMessage(),e);
  608.         }
  609.     }
  610.        
  611.     private static void rebuildAttachmentAsByteArray(OpenSPCoop2SoapMessage msg,AttachmentPart ap) throws MessageException{
  612.         try{
  613.             javax.activation.DataHandler dh= ap.getDataHandler();  
  614.             java.io.InputStream inputDH = dh.getInputStream();
  615.             java.io.ByteArrayOutputStream bout = new java.io.ByteArrayOutputStream();
  616.             byte [] readB = new byte[8192];
  617.             int readByte = 0;
  618.             while((readByte = inputDH.read(readB))!= -1)
  619.                 bout.write(readB,0,readByte);
  620.             inputDH.close();
  621.             bout.flush();
  622.             bout.close();
  623.             //ap.setDataHandler(new javax.activation.DataHandler(bout.toByteArray(),ap.getContentType()));
  624.             // In axiom l'update del data handler non funziona
  625.             msg.updateAttachmentPart(ap, bout.toByteArray(),ap.getContentType());
  626.         }catch(Exception e){
  627.             throw new MessageException(e.getMessage(),e);
  628.         }
  629.     }
  630.    
  631.     private static void rebuildDataHandlerAttachment(OpenSPCoop2SoapMessage msg,AttachmentPart ap, ByteArrayOutputStream boutAlreadyRead) throws MessageException{
  632.         try{
  633.             //ap.setDataHandler(new javax.activation.DataHandler(bout.toByteArray(),ap.getContentType()));
  634.             // In axiom l'update del data handler non funziona
  635.             msg.updateAttachmentPart(ap, new DataHandler(boutAlreadyRead.toByteArray(),ap.getContentType()));
  636.         }catch(Exception e){
  637.             throw new MessageException(e.getMessage(),e);
  638.         }
  639.     }


  640. }