DumpMessaggio.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.utils;

  21. import java.io.ByteArrayOutputStream;
  22. import java.io.Serializable;
  23. import java.util.HashMap;
  24. import java.util.Iterator;
  25. import java.util.List;
  26. import java.util.Map;

  27. import org.openspcoop2.message.constants.MessageType;
  28. import org.openspcoop2.message.exception.MessageException;
  29. import org.openspcoop2.utils.UtilsException;
  30. import org.openspcoop2.utils.digest.DigestEncoding;
  31. import org.openspcoop2.utils.transport.TransportUtils;
  32. import org.openspcoop2.utils.transport.http.HttpConstants;

  33. /**
  34.  * DumpMessaggio
  35.  *
  36.  * @author Poli Andrea (apoli@link.it)
  37.  * @author $Author$
  38.  * @version $Rev$, $Date$
  39.  */
  40. public class DumpMessaggio implements Serializable{

  41.     /**
  42.      *
  43.      */
  44.     private static final long serialVersionUID = 4718160136521047108L;
  45.        
  46.     private MessageType messageType;
  47.    
  48.     private String contentType;
  49.    
  50.     private Map<String, List<String>> headers = new HashMap<>();
  51.    
  52.     private transient ByteArrayOutputStream body;
  53.     private DumpMessaggioMultipartInfo multipartInfoBody;
  54.    
  55.     private List<DumpAttachment> attachments;
  56.    

  57.     public long getBodyLength() {
  58.         if(this.body!=null) {
  59.             return this.body.size();
  60.         }
  61.         return 0;
  62.     }
  63.    
  64.     public byte[] getBody() {
  65.         if(this.body!=null) {
  66.             return this.body.toByteArray();
  67.         }
  68.         return null;
  69.     }
  70.    
  71.     public String getBodyAsString() {
  72.         if(this.body!=null) {
  73.             return this.body.toString();
  74.         }
  75.         return null;
  76.     }
  77.    
  78.     public void setBody(ByteArrayOutputStream body) {
  79.         this.body = body;
  80.     }

  81.     public String getBodyBase64Digest(String algorithm) throws UtilsException{
  82.         return getBodyDigest(algorithm, DigestEncoding.BASE64, false);
  83.     }
  84.     public String getBodyBase64Digest(String algorithm, String rfc3230) throws UtilsException{
  85.         // // per invocazioni dinamiche
  86.         boolean v = false;
  87.         try {
  88.             v = Boolean.valueOf(rfc3230);
  89.         }catch(Exception e) {
  90.             throw new UtilsException("Uncorrect boolean value '"+rfc3230+"': "+e.getMessage(),e);
  91.         }
  92.         return getBodyBase64Digest(algorithm, v);
  93.     }
  94.     public String getBodyBase64Digest(String algorithm, boolean rfc3230) throws UtilsException{
  95.         return getBodyDigest(algorithm, DigestEncoding.BASE64, rfc3230);
  96.     }
  97.    
  98.     public String getBodyHexDigest(String algorithm) throws UtilsException{
  99.         return getBodyDigest(algorithm, DigestEncoding.HEX, false);
  100.     }
  101.     public String getBodyHexDigest(String algorithm, String rfc3230) throws UtilsException{
  102.         // // per invocazioni dinamiche
  103.         boolean v = false;
  104.         try {
  105.             v = Boolean.valueOf(rfc3230);
  106.         }catch(Exception e) {
  107.             throw new UtilsException("Uncorrect boolean value '"+rfc3230+"': "+e.getMessage(),e);
  108.         }
  109.         return getBodyHexDigest(algorithm, v);
  110.     }
  111.     public String getBodyHexDigest(String algorithm, boolean rfc3230) throws UtilsException{
  112.         return getBodyDigest(algorithm, DigestEncoding.HEX, rfc3230);
  113.     }
  114.    
  115.     public String getBodyDigest(String algorithm, String digestEncodingParam) throws UtilsException{
  116.         return getBodyDigest(algorithm, digestEncodingParam, false);
  117.     }
  118.     public String getBodyDigest(String algorithm, DigestEncoding digestEncoding) throws UtilsException{
  119.         return getBodyDigest(algorithm, digestEncoding, false);
  120.     }
  121.     public String getBodyDigest(String algorithm, String digestEncodingParam, String rfc3230) throws UtilsException{
  122.         // // per invocazioni dinamiche
  123.         boolean v = false;
  124.         try {
  125.             v = Boolean.valueOf(rfc3230);
  126.         }catch(Exception e) {
  127.             throw new UtilsException("Uncorrect boolean value '"+rfc3230+"': "+e.getMessage(),e);
  128.         }
  129.         return getBodyDigest(algorithm, digestEncodingParam, v);
  130.     }
  131.     public String getBodyDigest(String algorithm, String digestEncodingParam,
  132.             boolean rfc3230 // aggiunge prefisso algoritmo=
  133.             ) throws UtilsException{
  134.         DigestEncoding digestEncoding = null;
  135.         try {
  136.             digestEncoding = DigestEncoding.valueOf(digestEncodingParam);
  137.         }catch(Throwable t) {
  138.             throw new UtilsException("DigestEncoding '"+digestEncodingParam+"' unsupported");
  139.         }
  140.         return getBodyDigest(algorithm, digestEncoding, rfc3230);
  141.     }
  142.     public String getBodyDigest(String algorithm, DigestEncoding digestEncoding, String rfc3230) throws UtilsException{
  143.         // // per invocazioni dinamiche
  144.         boolean v = false;
  145.         try {
  146.             v = Boolean.valueOf(rfc3230);
  147.         }catch(Exception e) {
  148.             throw new UtilsException("Uncorrect boolean value '"+rfc3230+"': "+e.getMessage(),e);
  149.         }
  150.         return getBodyDigest(algorithm, digestEncoding, v);
  151.     }
  152.     public String getBodyDigest(String algorithm, DigestEncoding digestEncoding,
  153.             boolean rfc3230 // aggiunge prefisso algoritmo=
  154.             ) throws UtilsException{
  155.         byte[] content = getBody();
  156.         if(content==null) {
  157.             throw new UtilsException("Content null");
  158.         }
  159.         return org.openspcoop2.utils.digest.DigestUtils.getDigestValue(content, algorithm, digestEncoding, rfc3230);
  160.     }
  161.    
  162.     public String getContentType() {
  163.         return this.contentType;
  164.     }

  165.     public void setContentType(String contentType) {
  166.         this.contentType = contentType;
  167.     }
  168.    
  169.     public List<DumpAttachment> getAttachments() {
  170.         return this.attachments;
  171.     }

  172.     public void setAttachments(List<DumpAttachment> attachments) {
  173.         this.attachments = attachments;
  174.     }
  175.    
  176.     public DumpAttachment getAttachmentByIndex(String index) throws UtilsException {
  177.         // // per invocazioni dinamiche
  178.         int pos = -1;
  179.         try {
  180.             pos = Integer.valueOf(index);
  181.             if(pos<0) {
  182.                 throw new Exception("negative index");
  183.             }
  184.         }catch(Exception e) {
  185.             throw new UtilsException("Uncorrect position '"+pos+"': "+e.getMessage(),e);
  186.         }
  187.         return getAttachment(pos);
  188.     }
  189.     public DumpAttachment getAttachment(int index) {
  190.         if(this.attachments==null || this.attachments.isEmpty()) {
  191.             return null;
  192.         }
  193.         if(index>=this.attachments.size()) {
  194.             return null;
  195.         }
  196.         return this.attachments.get(index);
  197.     }
  198.     public DumpAttachment getAttachmentById(String id) {
  199.         return getAttachment(id);
  200.     }
  201.     public DumpAttachment getAttachmentByContentId(String id) {
  202.         return getAttachment(id);
  203.     }
  204.     public DumpAttachment getAttachment(String id) {
  205.         if(this.attachments==null || this.attachments.isEmpty()) {
  206.             return null;
  207.         }
  208.         for (DumpAttachment dumpAttachment : this.attachments) {
  209.             if(id.equals(dumpAttachment.getContentId())) {
  210.                 return dumpAttachment;
  211.             }
  212.         }
  213.         return null;
  214.     }
  215.    
  216.     public DumpMessaggioMultipartInfo getMultipartInfoBody() {
  217.         return this.multipartInfoBody;
  218.     }

  219.     public void setMultipartInfoBody(DumpMessaggioMultipartInfo multipartInfoBody) {
  220.         this.multipartInfoBody = multipartInfoBody;
  221.     }


  222.     @Deprecated
  223.     public Map<String, String> getHeaders() {
  224.         return TransportUtils.convertToMapSingleValue(this.headers);
  225.     }
  226.     public Map<String, List<String>> getHeadersValues() {
  227.         return this.headers;
  228.     }

  229.     @Deprecated
  230.     public void setHeaders(Map<String, String> headers) {
  231.         this.headers = TransportUtils.convertToMapListValues(headers);
  232.     }
  233.     public void setHeadersValues(Map<String, List<String>> headers) {
  234.         this.headers = headers;
  235.     }
  236.    
  237.     public MessageType getMessageType() {
  238.         return this.messageType;
  239.     }

  240.     public void setMessageType(MessageType messageType) {
  241.         this.messageType = messageType;
  242.     }

  243.    
  244.     public String toString(DumpMessaggioConfig config, boolean dumpAllAttachments) throws MessageException{
  245.         try{
  246.             StringBuilder out = new StringBuilder();
  247.                        
  248.             if(config.isDumpHeaders()) {
  249.                 out.append("------ Header di trasporto ------\n");
  250.                 if(this.getHeadersValues()!=null && this.getHeadersValues().size()>0){
  251.                     Iterator<?> it = this.getHeadersValues().keySet().iterator();
  252.                     while (it.hasNext()) {
  253.                         String key = (String) it.next();
  254.                         if(key!=null){
  255.                             List<String> values = this.getHeadersValues().get(key);
  256.                             if(values!=null && !values.isEmpty()) {
  257.                                 for (String value : values) {
  258.                                     out.append("- "+key+": "+value+"\n");
  259.                                 }
  260.                             }
  261.                         }
  262.                     }
  263.                 }
  264.                 else {
  265.                     out.append("Non presenti\n");
  266.                 }
  267.             }
  268.            
  269.             boolean hasContent = this.getBodyLength()>0;
  270.             String contentString = "Body";
  271.             String contentType = "";
  272.             String attachString = "BodyPart";
  273.             if(!hasContent){
  274.                 contentString = "Empty Body";
  275.             }
  276.             if(MessageType.SOAP_11.equals(this.getMessageType()) || MessageType.SOAP_12.equals(this.getMessageType())) {
  277.                 contentString = "SOAPEnvelope";
  278.                 attachString = "Attachment";
  279.             }
  280.             if(hasContent) {
  281.                 if(this.getContentType()!=null) {
  282.                     contentType = " (ContentType: "+this.getContentType()+")";
  283.                 }
  284.             }
  285.            
  286.             if(config.isDumpBody()) {
  287.                 out.append("------ "+contentString+contentType+" (MessageType: "+this.getMessageType()+") ------\n");

  288.                 if(this.getMultipartInfoBody()!=null) {
  289.                     out.append("\n*** MimePart Header ***\n");
  290.                     if(this.getMultipartInfoBody().getContentId()!=null) {
  291.                         out.append("- "+HttpConstants.CONTENT_ID+": "+this.getMultipartInfoBody().getContentId()+"\n");
  292.                     }
  293.                     if(this.getMultipartInfoBody().getContentLocation()!=null) {
  294.                         out.append("- "+HttpConstants.CONTENT_LOCATION+": "+this.getMultipartInfoBody().getContentLocation()+"\n");
  295.                     }
  296.                     if(this.getMultipartInfoBody().getContentType()!=null) {
  297.                         out.append("- "+HttpConstants.CONTENT_TYPE+": "+this.getMultipartInfoBody().getContentType()+"\n");
  298.                     }
  299.                     if(config.isDumpMultipartHeaders() &&  this.getMultipartInfoBody().getHeadersValues()!=null &&
  300.                             this.getMultipartInfoBody().getHeadersValues().size()>0) {
  301.                         Iterator<?> itM = this.getMultipartInfoBody().getHeadersValues().keySet().iterator();
  302.                         while(itM.hasNext()) {
  303.                             Object keyO = itM.next();
  304.                             if(keyO instanceof String) {
  305.                                 String key = (String) keyO;
  306.                                 if(HttpConstants.CONTENT_ID.equalsIgnoreCase(key) ||
  307.                                         HttpConstants.CONTENT_LOCATION.equalsIgnoreCase(key) ||
  308.                                         HttpConstants.CONTENT_TYPE.equalsIgnoreCase(key)) {
  309.                                     continue;
  310.                                 }
  311.                                 List<String> values = this.getMultipartInfoBody().getHeadersValues().get(key);
  312.                                 if(values!=null && !values.isEmpty()) {
  313.                                     for (String value : values) {
  314.                                         out.append("- "+key+": "+value+"\n");
  315.                                     }
  316.                                 }
  317.                             }
  318.                         }
  319.                     }
  320.                 }
  321.                
  322.                 out.append("\n");
  323.                
  324.                 if(this.getBodyLength()>0) {
  325.                     out.append(this.getBodyAsString());
  326.                 }
  327.             }
  328.            
  329.             if(config.isDumpAttachments() && this.getAttachments()!=null && this.getAttachments().size()>0){
  330.                 java.util.Iterator<?> it = this.getAttachments().iterator();
  331.                 int index = 1;
  332.                 while(it.hasNext()){
  333.                     DumpAttachment ap =
  334.                         (DumpAttachment) it.next();
  335.                     out.append("\n------ "+attachString+"-"+(index)+" ------\n");
  336.                
  337.                     out.append("\n*** MimePart Header ***\n");
  338.                     if(ap.getContentId()!=null) {
  339.                         out.append("- "+HttpConstants.CONTENT_ID+": "+ap.getContentId()+"\n");
  340.                     }
  341.                     if(ap.getContentLocation()!=null) {
  342.                         out.append("- "+HttpConstants.CONTENT_LOCATION+": "+ap.getContentLocation()+"\n");
  343.                     }
  344.                     if(ap.getContentType()!=null) {
  345.                         out.append("- "+HttpConstants.CONTENT_TYPE+": "+ap.getContentType()+"\n");
  346.                     }
  347.                     if(config.isDumpMultipartHeaders() && ap.getHeadersValues()!=null &&
  348.                             ap.getHeadersValues().size()>0) {
  349.                         Iterator<?> itM = ap.getHeadersValues().keySet().iterator();
  350.                         while(itM.hasNext()) {
  351.                             Object keyO = itM.next();
  352.                             if(keyO instanceof String) {
  353.                                 String key = (String) keyO;
  354.                                 if(HttpConstants.CONTENT_ID.equalsIgnoreCase(key) ||
  355.                                         HttpConstants.CONTENT_LOCATION.equalsIgnoreCase(key) ||
  356.                                         HttpConstants.CONTENT_TYPE.equalsIgnoreCase(key)) {
  357.                                     continue;
  358.                                 }
  359.                                 List<String> values = ap.getHeadersValues().get(key);
  360.                                 if(values!=null && !values.isEmpty()) {
  361.                                     for (String value : values) {
  362.                                         out.append("- "+key+": "+value+"\n");
  363.                                     }
  364.                                 }
  365.                             }
  366.                         }
  367.                     }
  368.                     out.append("\n");
  369.                                
  370.                     if(ap.getErrorContentNotSerializable()!=null) {
  371.                         out.append(ap.getErrorContentNotSerializable());
  372.                     }
  373.                     else {
  374.                         out.append(ap.getContentAsString(!dumpAllAttachments));
  375.                     }
  376.                    
  377.                     index++;
  378.                 }
  379.             }
  380.            
  381.             return out.toString();
  382.         }catch(Exception e){
  383.             throw new MessageException(e.getMessage(),e);
  384.         }
  385.     }
  386. }