DumpRestMessageUtils.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.rest;

  21. import java.io.ByteArrayOutputStream;
  22. import java.io.InputStream;
  23. import java.util.ArrayList;
  24. import java.util.Enumeration;
  25. import java.util.Iterator;
  26. import java.util.List;
  27. import java.util.Map;

  28. import javax.mail.BodyPart;

  29. import org.openspcoop2.message.OpenSPCoop2RestMessage;
  30. import org.openspcoop2.message.OpenSPCoop2RestMimeMultipartMessage;
  31. import org.openspcoop2.message.constants.MessageType;
  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.utils.dch.MailcapActivationReader;
  38. import org.openspcoop2.utils.mime.MimeMultipart;
  39. import org.openspcoop2.utils.transport.http.HttpConstants;


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

  49.     public static DumpMessaggio dumpMessage(OpenSPCoop2RestMessage<?> msg, boolean dumpAllBodyParts) throws MessageException{
  50.         return dumpMessage(msg, new DumpMessaggioConfig(), dumpAllBodyParts);
  51.     }
  52.     @SuppressWarnings("incomplete-switch")
  53.     public static DumpMessaggio dumpMessage(OpenSPCoop2RestMessage<?> msg,
  54.             DumpMessaggioConfig config,
  55.             boolean dumpAllBodyParts) throws MessageException{
  56.         try{
  57.             DumpMessaggio dumpMessaggio = new DumpMessaggio();
  58.             dumpMessaggio.setMessageType(msg.getMessageType());
  59.                        
  60.             Map<String, List<String>> pTrasporto = null;
  61.             if(msg.getTransportRequestContext()!=null) {
  62.                 if(msg.getTransportRequestContext().getHeaders()!=null &&
  63.                         msg.getTransportRequestContext().getHeaders().size()>0){
  64.                     if(config.isDumpHeaders()) {
  65.                         pTrasporto = msg.getTransportRequestContext().getHeaders();
  66.                     }
  67.                 }
  68.             }
  69.             else if(msg.getTransportResponseContext()!=null) {
  70.                 if(msg.getTransportResponseContext().getHeaders()!=null &&
  71.                         msg.getTransportResponseContext().getHeaders().size()>0){
  72.                     if(config.isDumpHeaders()) {
  73.                         pTrasporto = msg.getTransportResponseContext().getHeaders();
  74.                     }
  75.                 }
  76.             }
  77.             if(config.isDumpHeaders() && pTrasporto!=null) {
  78.                 Iterator<String> keys = pTrasporto.keySet().iterator();
  79.                 while (keys.hasNext()) {
  80.                     String key = (String) keys.next();
  81.                     if(key!=null){
  82.                         List<String> values = pTrasporto.get(key);
  83.                         dumpMessaggio.getHeadersValues().put(key, values);
  84.                     }
  85.                 }
  86.             }
  87.            
  88.             boolean hasContent = msg.hasContent();
  89.             if(hasContent){
  90.                
  91.                 dumpMessaggio.setContentType(msg.getContentType());
  92.                                                                
  93.                 // La registrazione su log dello stream completo non e' una funzionalita' simmetrica rispetto al dump fatto su soap
  94.                 if(config.isDumpBody()) {
  95.                     if(!MessageType.MIME_MULTIPART.equals(msg.getMessageType())) {
  96.                         ByteArrayOutputStream bout = new ByteArrayOutputStream();
  97.                         switch (msg.getMessageType()) {
  98.                         case XML:
  99.                             bout.write(msg.castAsRestXml().getContentAsByteArray());
  100.                             break;
  101.                         case JSON:
  102.                             bout.write(msg.castAsRestJson().getContentAsByteArray());
  103.                             break;
  104.                         case BINARY:
  105.                             bout.write(msg.castAsRestBinary().getContent().getContent());
  106.                             break;
  107.                         default:
  108.                             throw new MessageException("MessageType ["+msg.getMessageType()+"] unsupported");
  109.                         }
  110.                         bout.flush();
  111.                         bout.close();
  112.                         dumpMessaggio.setBody(bout);
  113.                     }
  114.                 }
  115.             }
  116.            
  117.             if((config.isDumpBody() || config.isDumpAttachments()) && hasContent &&
  118.                     MessageType.MIME_MULTIPART.equals(msg.getMessageType())){
  119.                 OpenSPCoop2RestMimeMultipartMessage msgMime = msg.castAsRestMimeMultipart();
  120.                 MultipartContent mc = msgMime.getContent();
  121.                 MimeMultipart mimeMultipart = (mc!=null) ? mc.getMimeMultipart() : null;
  122.                 if(mimeMultipart!=null) {
  123.                     for (int i = 0; i < mimeMultipart.countBodyParts(); i++) {
  124.                         BodyPart bodyPart = mimeMultipart.getBodyPart(i);
  125.                        
  126.                         if(i>0) {
  127.                             if(!config.isDumpAttachments()) {
  128.                                 break;
  129.                             }
  130.                         }
  131.                        
  132.                         DumpMessaggioMultipartInfo multipartInfoBody = null;    
  133.                         DumpAttachment dumpAttach = null;
  134.                         if(i==0 && config.isDumpBody()) {
  135.                            
  136.                             multipartInfoBody = new DumpMessaggioMultipartInfo();
  137.                            
  138.                             multipartInfoBody.setContentId(mimeMultipart.getContentID(bodyPart));
  139.                             multipartInfoBody.setContentLocation(mimeMultipart.getContentDisposition(bodyPart)); // Uso Disposition in REST, più opportuna
  140.                             multipartInfoBody.setContentType(bodyPart.getContentType());
  141.                            
  142.                         }
  143.                         else {
  144.                        
  145.                             dumpAttach = new DumpAttachment();
  146.                            
  147.                             dumpAttach.setContentId(mimeMultipart.getContentID(bodyPart));
  148.                             dumpAttach.setContentLocation(mimeMultipart.getContentDisposition(bodyPart)); // Uso Disposition in REST, più opportuna
  149.                             dumpAttach.setContentType(bodyPart.getContentType());
  150.                         }
  151.                            
  152.                         if(config.isDumpMultipartHeaders()) {
  153.                             Enumeration<?> en = bodyPart.getAllHeaders();
  154.                             if(en!=null) {
  155.                                 while(en.hasMoreElements()) {
  156.                                     Object keyO = en.nextElement();
  157.                                     if(keyO instanceof String) {
  158.                                         String key = (String) keyO;
  159.                                         String [] values = bodyPart.getHeader(key);
  160.                                         List<String> lValues = new ArrayList<>();
  161.                                         if(values!=null && values.length>0) {
  162.                                             for (int j = 0; j < values.length; j++) {
  163.                                                 lValues.add(values[j]);
  164.                                             }
  165.                                         }
  166.                                         if(!lValues.isEmpty()) {
  167.                                             if(multipartInfoBody!=null) {
  168.                                                 multipartInfoBody.getHeadersValues().put(key, lValues);
  169.                                             }
  170.                                             else {
  171.                                                 dumpAttach.getHeadersValues().put(key, lValues);
  172.                                             }
  173.                                         }
  174.                                     }
  175.                                     else if(keyO instanceof javax.mail.Header) {
  176.                                         javax.mail.Header hdr = (javax.mail.Header) keyO;
  177.                                         if(hdr!=null && hdr.getName()!=null) {
  178.                                             if(multipartInfoBody!=null) {
  179.                                                 List<String> lValues = null;
  180.                                                 if(multipartInfoBody.getHeadersValues().containsKey(hdr.getName())) {
  181.                                                     lValues = multipartInfoBody.getHeadersValues().get(hdr.getName());
  182.                                                 }
  183.                                                 else {
  184.                                                     lValues = new ArrayList<>();
  185.                                                     multipartInfoBody.getHeadersValues().put(hdr.getName(), lValues);
  186.                                                 }
  187.                                                 lValues.add(hdr.getValue());
  188.                                             }
  189.                                             else {
  190.                                                 List<String> lValues = null;
  191.                                                 if(dumpAttach.getHeadersValues().containsKey(hdr.getName())) {
  192.                                                     lValues = dumpAttach.getHeadersValues().get(hdr.getName());
  193.                                                 }
  194.                                                 else {
  195.                                                     lValues = new ArrayList<>();
  196.                                                     dumpAttach.getHeadersValues().put(hdr.getName(), lValues);
  197.                                                 }
  198.                                                 lValues.add(hdr.getValue());
  199.                                             }
  200.                                         }
  201.                                     }
  202.                                 }
  203.                             }
  204.                         }
  205.    
  206.                         ByteArrayOutputStream boutAttach = null;
  207.                         if(dumpAllBodyParts){
  208.                             boutAttach = (ByteArrayOutputStream) DumpRestMessageUtils._dumpBodyPart(msg, bodyPart, true);
  209.                         }else{
  210.                             Object o = _dumpBodyPart(msg, bodyPart, false);
  211.                             if(o == null){
  212.                                 dumpAttach.setErrorContentNotSerializable("Contenuto attachment non recuperato??");
  213.                             }
  214.                             else if(o instanceof String){
  215.                                 boutAttach = new ByteArrayOutputStream();
  216.                                 boutAttach.write(((String)o).getBytes());
  217.                                 boutAttach.flush();
  218.                                 boutAttach.close();
  219.                             }
  220.                             else if(o instanceof java.io.ByteArrayOutputStream){
  221.                                 boutAttach = (java.io.ByteArrayOutputStream) o;
  222.                             }
  223.                             else{
  224.                                 dumpAttach.setErrorContentNotSerializable("Contenuto attachment non è visualizzabile, tipo: "+o.getClass().getName());
  225.                             }
  226.                         }
  227.                         if(multipartInfoBody!=null) {
  228.                             dumpMessaggio.setBody(boutAttach);
  229.                            
  230.                             dumpMessaggio.setMultipartInfoBody(multipartInfoBody);
  231.                         }
  232.                         else {
  233.                             dumpAttach.setContent(boutAttach);
  234.                            
  235.                             if(dumpMessaggio.getAttachments()==null) {
  236.                                 dumpMessaggio.setAttachments(new ArrayList<>());
  237.                             }
  238.                             dumpMessaggio.getAttachments().add(dumpAttach);
  239.                         }
  240.                     }
  241.                 }
  242.             }
  243.            
  244.             return dumpMessaggio;
  245.         }catch(Exception e){
  246.             throw new MessageException(e.getMessage(),e);
  247.         }
  248.     }
  249.    
  250.     public static String dumpMessageAsString(DumpMessaggio msg, boolean dumpAllAttachments) throws MessageException{
  251.         return dumpMessageAsString(msg,  new DumpMessaggioConfig(), dumpAllAttachments);
  252.     }
  253.     public static String dumpMessageAsString(DumpMessaggio msg,
  254.             DumpMessaggioConfig config, boolean dumpAllAttachments) throws MessageException{
  255.         try{
  256.             StringBuilder out = new StringBuilder(msg.toString(config,dumpAllAttachments));
  257.             return out.toString();
  258.         }catch(Exception e){
  259.             throw new MessageException(e.getMessage(),e);
  260.         }
  261.     }
  262.    
  263.     public static String dumpMessageAsString(OpenSPCoop2RestMessage<?> msg,
  264.             boolean dumpAllBodyParts) throws MessageException{
  265.         return dumpMessageAsString(msg,  new DumpMessaggioConfig(), dumpAllBodyParts);
  266.     }
  267.     public static String dumpMessageAsString(OpenSPCoop2RestMessage<?> msg,
  268.             DumpMessaggioConfig config,
  269.             boolean dumpAllBodyParts) throws MessageException{
  270.         try{
  271.             StringBuilder out = new StringBuilder();
  272.            
  273.             Map<String,List<String>> pTrasporto = null;
  274.             if(msg.getTransportRequestContext()!=null) {
  275.                 if(msg.getTransportRequestContext().getHeaders()!=null &&
  276.                         msg.getTransportRequestContext().getHeaders().size()>0){
  277.                     if(config.isDumpHeaders()) {
  278.                         pTrasporto = msg.getTransportRequestContext().getHeaders();
  279.                     }
  280.                 }
  281.             }
  282.             else if(msg.getTransportResponseContext()!=null) {
  283.                 if(msg.getTransportResponseContext().getHeaders()!=null &&
  284.                         msg.getTransportResponseContext().getHeaders().size()>0){
  285.                     if(config.isDumpHeaders()) {
  286.                         pTrasporto = msg.getTransportResponseContext().getHeaders();
  287.                     }
  288.                 }
  289.             }
  290.             if(config.isDumpHeaders()) {
  291.                 out.append("------ Header di trasporto ------\n");
  292.                 if(pTrasporto!=null && pTrasporto.size()>0) {
  293.                     Iterator<String> keys = pTrasporto.keySet().iterator();
  294.                     while (keys.hasNext()) {
  295.                         String key = (String) keys.next();
  296.                         if(key!=null){
  297.                             List<String> values = pTrasporto.get(key);
  298.                             if(values!=null && !values.isEmpty()) {
  299.                                 for (String value : values) {
  300.                                     out.append("- "+key+": "+value+"\n");
  301.                                 }
  302.                             }
  303.                         }
  304.                     }
  305.                 }
  306.                 else {
  307.                     out.append("Non presenti\n");
  308.                 }
  309.             }
  310.            
  311.             boolean hasContent = msg.hasContent();
  312.             String contentString = "Body";
  313.             String contentType = "";
  314.             if(!hasContent){
  315.                 contentString = "Empty Body";
  316.             }
  317.             if(hasContent) {
  318.                 if(msg.getContentType()!=null) {
  319.                     contentType = " (ContentType: "+msg.getContentType()+")";
  320.                 }
  321.             }
  322.             if(config.isDumpBody()) {
  323.                 out.append("------ "+contentString+contentType+" ("+msg.getMessageType()+") ------\n");

  324.                 // Nel caso di multipart non il body e gli attachments devono essere gestiti nell'iterazione successiva
  325.                 // Se si usa il metodo 'msg.getContentAsString()' si ottiene lo stream completo
  326.                 if(!MessageType.MIME_MULTIPART.equals(msg.getMessageType())) {
  327.                    
  328.                     out.append("\n");
  329.                    
  330.                     if(hasContent) {
  331.                         out.append(msg.getContentAsString());
  332.                     }
  333.                 }
  334.             }
  335.            
  336.             if((config.isDumpBody() || config.isDumpAttachments()) && hasContent && MessageType.MIME_MULTIPART.equals(msg.getMessageType())){
  337.                 OpenSPCoop2RestMimeMultipartMessage msgMime = msg.castAsRestMimeMultipart();
  338.                 MultipartContent mc = msgMime.getContent();
  339.                 MimeMultipart mimeMultipart = mc!=null ? mc.getMimeMultipart() : null;
  340.                 if(mimeMultipart!=null) {
  341.                     for (int i = 0; i < mimeMultipart.countBodyParts(); i++) {
  342.                        
  343.                         if(i>0) {
  344.                             if(!config.isDumpAttachments()) {
  345.                                 break;
  346.                             }
  347.                         }
  348.                        
  349.                         BodyPart bodyPart = mimeMultipart.getBodyPart(i);
  350.                        
  351.                         if(i>0 || !config.isDumpBody()) {
  352.                             out.append("\n------ BodyPart-"+(i+1)+" ------\n");
  353.                         }
  354.                        
  355.                         out.append("\n*** MimePart Header ***\n");
  356.                         String contentIdBodyPart = mimeMultipart.getContentID(bodyPart);
  357.                         if(contentIdBodyPart!=null) {
  358.                             out.append("- "+HttpConstants.CONTENT_ID+": "+contentIdBodyPart+"\n");
  359.                         }
  360.                         String contentLocationBodyPart = mimeMultipart.getContentDisposition(bodyPart); // Uso Disposition in REST, più opportuna
  361.                         if(contentLocationBodyPart!=null) {
  362.                             out.append("- "+HttpConstants.CONTENT_DISPOSITION+": "+contentLocationBodyPart+"\n");
  363.                         }
  364.                         if(bodyPart.getContentType()!=null) {
  365.                             out.append("- "+HttpConstants.CONTENT_TYPE+": "+bodyPart.getContentType()+"\n");
  366.                         }
  367.                         if(config.isDumpMultipartHeaders()) {
  368.                             Enumeration<?> en = bodyPart.getAllHeaders();
  369.                             if(en!=null) {
  370.                                 while(en.hasMoreElements()) {
  371.                                     Object keyO = en.nextElement();
  372.                                     if(keyO instanceof String) {
  373.                                         String key = (String) keyO;
  374.                                         if(HttpConstants.CONTENT_ID.equalsIgnoreCase(key) ||
  375.                                                 HttpConstants.CONTENT_DISPOSITION.equalsIgnoreCase(key) ||
  376.                                                 HttpConstants.CONTENT_TYPE.equalsIgnoreCase(key)) {
  377.                                             continue;
  378.                                         }
  379.                                         String [] values = bodyPart.getHeader(key);
  380.                                         if(values!=null && values.length>0) {
  381.                                             for (int j = 0; j < values.length; j++) {
  382.                                                 out.append("- "+key+": "+values[j]+"\n");
  383.                                             }
  384.                                         }
  385.                                     }
  386.                                 }
  387.                             }
  388.                         }
  389.                         out.append("\n");
  390.    
  391.                         if(dumpAllBodyParts){
  392.                             out.append(DumpRestMessageUtils.dumpBodyPart(msg, bodyPart));
  393.                         }else{
  394.                             //Object o = ap.getContent(); NON FUNZIONA CON TOMCAT
  395.                             Object o = bodyPart.getDataHandler().getContent();
  396.                             //System.out.println("["+o.getClass().getName()+"])"+ap.getContentType()+"(");                      
  397.                             if(o instanceof String){
  398.                                 out.append((String)o);
  399.                             }else{
  400.                                  out.append("Contenuto attachments non è visualizzabile, tipo: "+o.getClass().getName());
  401.                             }
  402.                         }
  403.                     }
  404.                 }
  405.             }
  406.            
  407.             return out.toString();
  408.         }catch(Exception e){
  409.             throw new MessageException(e.getMessage(),e);
  410.         }
  411.     }
  412.    
  413.     public static String dumpBodyPart(OpenSPCoop2RestMessage<?> msg,BodyPart bodyPart) throws MessageException{
  414.         Object o = _dumpBodyPart(msg, bodyPart, false);
  415.         // Metodo sopra non torna mai null, segnalato da sonarqube
  416.         /*if(o == null){
  417.             throw new MessageException("Dump error (return null reference)");
  418.         }*/
  419.         if(o instanceof String){
  420.             return (String) o;
  421.         }
  422.         else if(o instanceof java.io.ByteArrayOutputStream){
  423.             java.io.ByteArrayOutputStream bout = null;
  424.             try{
  425.                 bout = (java.io.ByteArrayOutputStream) o;
  426.                 return bout.toString();
  427.             }finally{
  428.                 try{
  429.                     if(bout!=null){
  430.                         bout.close();
  431.                     }
  432.                 }catch(Exception eClose){
  433.                     // close
  434.                 }
  435.             }
  436.         }
  437.         else{
  438.             throw new MessageException("Dump error (return type "+o.getClass().getName()+" unknown)");
  439.         }
  440.     }
  441.     public static byte[] dumpBodyPartAsByteArray(OpenSPCoop2RestMessage<?> msg,BodyPart bodyPart) throws MessageException{
  442.         Object o = _dumpBodyPart(msg, bodyPart, false);
  443.         // Metodo sopra non torna mai null, segnalato da sonarqube
  444.         /*if(o == null){
  445.             throw new MessageException("Dump error (return null reference)");
  446.         }*/
  447.         if(o instanceof String){
  448.             return ((String) o).getBytes();
  449.         }
  450.         else if(o instanceof java.io.ByteArrayOutputStream){
  451.             java.io.ByteArrayOutputStream bout = null;
  452.             try{
  453.                 bout = (java.io.ByteArrayOutputStream) o;
  454.                 return bout.toByteArray();
  455.             }finally{
  456.                 try{
  457.                     if(bout!=null){
  458.                         bout.close();
  459.                     }
  460.                 }catch(Exception eClose){
  461.                     // close
  462.                 }
  463.             }
  464.         }
  465.         else{
  466.             throw new MessageException("Dump error (return type "+o.getClass().getName()+" unknown)");
  467.         }
  468.     }
  469.    
  470.     private static boolean convert = false;
  471.     public static void setConvert(boolean convert) {
  472.         DumpRestMessageUtils.convert = convert;
  473.     }
  474.     private static Object _dumpBodyPart(OpenSPCoop2RestMessage<?> msg,BodyPart bodyPart, boolean forceReturnAsByteArrayOutputStream) throws MessageException{
  475.         try{        
  476.             java.io.ByteArrayOutputStream bout = null;
  477.             //Object o = ap.getContent(); NON FUNZIONA CON TOMCAT
  478.             //java.io.InputStream inputDH = dh.getInputStream(); NON FUNZIONA CON JBOSS7 e JAVA7 e imbustamentoSOAP con GestioneManifest e rootElementMaggioreUno (tipo: application/octet-stream)
  479.             Object o = bodyPart.getDataHandler().getContent();
  480.             String s = null;
  481.             if(o!=null){
  482.                 if(o instanceof byte[]){
  483.                     byte[] b = (byte[]) o;
  484.                     bout = new ByteArrayOutputStream();
  485.                     bout.write(b);
  486.                     bout.flush();
  487.                     bout.close();
  488.                 }
  489.                 else if(o instanceof InputStream){
  490.                     InputStream is = (InputStream) o;
  491.                     bout = new java.io.ByteArrayOutputStream();
  492.                     byte [] readB = new byte[8192];
  493.                     int readByte = 0;
  494.                     while((readByte = is.read(readB))!= -1)
  495.                         bout.write(readB,0,readByte);
  496.                     is.close();
  497.                     bout.flush();
  498.                     bout.close();
  499.                 }
  500.                 else if(o instanceof String){
  501.                     s = (String) o;
  502.                     bout = new ByteArrayOutputStream();
  503.                     bout.write(s.getBytes());
  504.                     bout.flush();
  505.                     bout.close();
  506.                 }
  507.                 else{
  508.                     // Provo con codiceOriginale ma in jboss non funziona sempre
  509.                     javax.activation.DataHandler dh= bodyPart.getDataHandler();  
  510.                     java.io.InputStream inputDH = dh.getInputStream();
  511.                     bout = new java.io.ByteArrayOutputStream();
  512.                     byte [] readB = new byte[8192];
  513.                     int readByte = 0;
  514.                     while((readByte = inputDH.read(readB))!= -1)
  515.                         bout.write(readB,0,readByte);
  516.                     inputDH.close();
  517.                     bout.flush();
  518.                     bout.close();      
  519.                 }
  520.             }
  521.             else{
  522.                 // Provo con codiceOriginale ma in jboss non funziona sempre
  523.                 javax.activation.DataHandler dh= bodyPart.getDataHandler();  
  524.                 java.io.InputStream inputDH = dh.getInputStream();
  525.                 bout = new java.io.ByteArrayOutputStream();
  526.                 byte [] readB = new byte[8192];
  527.                 int readByte = 0;
  528.                 while((readByte = inputDH.read(readB))!= -1)
  529.                     bout.write(readB,0,readByte);
  530.                 inputDH.close();
  531.                 bout.flush();
  532.                 bout.close();      
  533.             }
  534.             // Per non perdere quanto letto
  535.             // in rest con le api utilizzate non sembra necessario ricostruirlo come si fa in soap.
  536.             // anche il caso del tunnel soap non esiste.
  537.             if(convert && MailcapActivationReader.existsDataContentHandler(bodyPart.getContentType())){
  538.                 if(bodyPart.getContentType()!=null && bodyPart.getContentType().startsWith(HttpConstants.CONTENT_TYPE_PLAIN)){
  539.                     processContentTypeTextPlain(s, bodyPart, bout);
  540.                 }
  541.                 else{
  542.                     //bodyPart.setDataHandler(new javax.activation.DataHandler(bout.toByteArray(),bodyPart.getContentType()));
  543.                     // Nel caso sia xml si ottiene il seguente errore:
  544.                     //Invalid Object type = class [B. XmlDCH can only convert DataSource or Source to XML.
  545.                     // Si potrebbe vederlo di gestire come e' stato fatto per il rebuild dell'attachment su SOAP.
  546.                 }
  547.             }
  548.             if(s!=null){
  549.                 if(forceReturnAsByteArrayOutputStream) {
  550.                     return bout;
  551.                 }
  552.                 else {
  553.                     return s;
  554.                 }
  555.             }else{
  556.                 return bout;
  557.             }
  558.         }catch(Exception e){
  559.             throw new MessageException(e.getMessage(),e);
  560.         }
  561.     }

  562.     private static void processContentTypeTextPlain(String s, BodyPart bodyPart, java.io.ByteArrayOutputStream bout) throws Exception {
  563.         // Se siamo in text plain non devo fare nulla. Comunque non l'ho perso.
  564.         // Se uso il codice sotto, poi si perde il content-type, non viene serializzato
  565.         /*
  566.         if(s!=null){
  567.             bodyPart.setDataHandler(new javax.activation.DataHandler(s,bodyPart.getContentType()));
  568.             //bodyPart.setContent(s,bodyPart.getContentType());
  569.             //bodyPart.setText(s);
  570.         }
  571.         else{
  572.             //bodyPart.setDataHandler(new javax.activation.DataHandler(bout.toByteArray(),bodyPart.getContentType()));
  573.             // devo comunque impostare una stringa nel caso di text/plain, senno ottengo un errore:
  574.             // "text/plain" DataContentHandler requires String object, was given object of type class [B
  575.             bodyPart.setDataHandler(new javax.activation.DataHandler(bout.toString(),bodyPart.getContentType()));
  576.             //bodyPart.setContent(bout.toString(),bodyPart.getContentType());
  577.             //bodyPart.setText(bout.toString());
  578.         }*/
  579.     }
  580.    
  581. }