DumpAttachment.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.List;
  25. import java.util.Map;

  26. import org.openspcoop2.utils.UtilsException;
  27. import org.openspcoop2.utils.digest.DigestEncoding;
  28. import org.openspcoop2.utils.transport.TransportUtils;

  29. /**
  30.  * Allegato
  31.  *
  32.  * @author Poli Andrea (apoli@link.it)
  33.  * @author $Author$
  34.  * @version $Rev$, $Date$
  35.  */
  36. public class DumpAttachment implements Serializable{

  37.     /**
  38.      *
  39.      */
  40.     private static final long serialVersionUID = 1L;
  41.    
  42.     private String contentId;
  43.     private String contentLocation;
  44.     private String contentType;
  45.     private Map<String, List<String>> headers = new HashMap<>();
  46.    
  47.     private String errorContentNotSerializable;
  48.     private transient ByteArrayOutputStream content;
  49.     private StringBuilder printableContent;
  50.    
  51.     public String getContentId() {
  52.         return this.contentId;
  53.     }
  54.     public void setContentId(String contentId) {
  55.         this.contentId = contentId;
  56.     }
  57.     public String getContentLocation() {
  58.         return this.contentLocation;
  59.     }
  60.     public void setContentLocation(String contentLocation) {
  61.         this.contentLocation = contentLocation;
  62.     }
  63.     public String getContentType() {
  64.         return this.contentType;
  65.     }
  66.     public void setContentType(String contentType) {
  67.         this.contentType = contentType;
  68.     }
  69.     public String getErrorContentNotSerializable() {
  70.         return this.errorContentNotSerializable;
  71.     }
  72.     public void setErrorContentNotSerializable(String errorContentNotSerializable) {
  73.         this.errorContentNotSerializable = errorContentNotSerializable;
  74.     }
  75.     public long getContentLength() {
  76.         if(this.content!=null) {
  77.             return this.content.size();
  78.         }
  79.         return 0;
  80.     }
  81.     public byte[] getContent() {
  82.         if(this.content!=null) {
  83.             return this.content.toByteArray();
  84.         }
  85.         return null;
  86.     }
  87.     private static String getTestoVisualizzabile(byte [] b,StringBuilder stringBuffer) {
  88.          try{
  89.              // 1024 = 1K
  90.              // Visualizzo al massimo 50K (per i log)
  91.              int max = 50 * 1024;
  92.              stringBuffer.append(org.openspcoop2.utils.Utilities.convertToPrintableText(b, max));
  93.              return null;  
  94.          }catch(Exception e){
  95.              return e.getMessage();
  96.          }

  97.      }
  98.     public String getContentAsString(boolean ifPrintableContent) {
  99.         if(this.content!=null) {
  100.             if(ifPrintableContent) {
  101.                 if(this.printableContent!=null) {
  102.                     return this.printableContent.toString();
  103.                 }
  104.                 else {
  105.                     this.printableContent = new StringBuilder();
  106.                     String errore = getTestoVisualizzabile(this.content.toByteArray(),this.printableContent);
  107.                     if(errore!=null) {
  108.                         this.printableContent = new StringBuilder();
  109.                         this.printableContent.append(errore);
  110.                     }
  111.                     return this.printableContent.toString();
  112.                 }
  113.             }
  114.             else {
  115.                 return this.content.toString();
  116.             }
  117.         }
  118.         return null;
  119.     }
  120.     public void setContent(ByteArrayOutputStream content) {
  121.         this.content = content;
  122.     }
  123.    
  124.     public String getContentBase64Digest(String algorithm) throws UtilsException{
  125.         return getContentDigest(algorithm, DigestEncoding.BASE64, false);
  126.     }
  127.     public String getContentBase64Digest(String algorithm, String rfc3230) throws UtilsException{
  128.         // // per invocazioni dinamiche
  129.         boolean v = false;
  130.         try {
  131.             v = Boolean.valueOf(rfc3230);
  132.         }catch(Exception e) {
  133.             throw new UtilsException("Uncorrect boolean value '"+rfc3230+"': "+e.getMessage(),e);
  134.         }
  135.         return getContentBase64Digest(algorithm, v);
  136.     }
  137.     public String getContentBase64Digest(String algorithm, boolean rfc3230) throws UtilsException{
  138.         return getContentDigest(algorithm, DigestEncoding.BASE64, rfc3230);
  139.     }
  140.    
  141.     public String getContentHexDigest(String algorithm) throws UtilsException{
  142.         return getContentDigest(algorithm, DigestEncoding.HEX, false);
  143.     }
  144.     public String getContentHexDigest(String algorithm, String rfc3230) throws UtilsException{
  145.         // // per invocazioni dinamiche
  146.         boolean v = false;
  147.         try {
  148.             v = Boolean.valueOf(rfc3230);
  149.         }catch(Exception e) {
  150.             throw new UtilsException("Uncorrect boolean value '"+rfc3230+"': "+e.getMessage(),e);
  151.         }
  152.         return getContentHexDigest(algorithm, v);
  153.     }
  154.     public String getContentHexDigest(String algorithm, boolean rfc3230) throws UtilsException{
  155.         return getContentDigest(algorithm, DigestEncoding.HEX, rfc3230);
  156.     }
  157.    
  158.     public String getContentDigest(String algorithm, String digestEncodingParam) throws UtilsException{
  159.         return getContentDigest(algorithm, digestEncodingParam, false);
  160.     }
  161.     public String getContentDigest(String algorithm, DigestEncoding digestEncoding) throws UtilsException{
  162.         return getContentDigest(algorithm, digestEncoding, false);
  163.     }
  164.     public String getContentDigest(String algorithm, String digestEncodingParam, String rfc3230) throws UtilsException{
  165.         // // per invocazioni dinamiche
  166.         boolean v = false;
  167.         try {
  168.             v = Boolean.valueOf(rfc3230);
  169.         }catch(Exception e) {
  170.             throw new UtilsException("Uncorrect boolean value '"+rfc3230+"': "+e.getMessage(),e);
  171.         }
  172.         return getContentDigest(algorithm, digestEncodingParam, v);
  173.     }
  174.     public String getContentDigest(String algorithm, String digestEncodingParam,
  175.             boolean rfc3230 // aggiunge prefisso algoritmo=
  176.             ) throws UtilsException{
  177.         DigestEncoding digestEncoding = null;
  178.         try {
  179.             digestEncoding = DigestEncoding.valueOf(digestEncodingParam);
  180.         }catch(Throwable t) {
  181.             throw new UtilsException("DigestEncoding '"+digestEncodingParam+"' unsupported");
  182.         }
  183.         return getContentDigest(algorithm, digestEncoding, rfc3230);
  184.     }
  185.     public String getContentDigest(String algorithm, DigestEncoding digestEncoding, String rfc3230) throws UtilsException{
  186.         // // per invocazioni dinamiche
  187.         boolean v = false;
  188.         try {
  189.             v = Boolean.valueOf(rfc3230);
  190.         }catch(Exception e) {
  191.             throw new UtilsException("Uncorrect boolean value '"+rfc3230+"': "+e.getMessage(),e);
  192.         }
  193.         return getContentDigest(algorithm, digestEncoding, v);
  194.     }
  195.     public String getContentDigest(String algorithm, DigestEncoding digestEncoding,
  196.             boolean rfc3230 // aggiunge prefisso algoritmo=
  197.             ) throws UtilsException{
  198.         byte[] content = getContent();
  199.         if(content==null) {
  200.             throw new UtilsException("Content null");
  201.         }
  202.         return org.openspcoop2.utils.digest.DigestUtils.getDigestValue(content, algorithm, digestEncoding, rfc3230);
  203.     }
  204.    
  205.     @Deprecated
  206.     public Map<String, String> getHeaders() {
  207.         return TransportUtils.convertToMapSingleValue(this.headers);
  208.     }
  209.     public Map<String, List<String>> getHeadersValues() {
  210.         return this.headers;
  211.     }

  212.     @Deprecated
  213.     public void setHeaders(Map<String, String> headers) {
  214.         this.headers = TransportUtils.convertToMapListValues(headers);
  215.     }
  216.     public void setHeadersValues(Map<String, List<String>> headers) {
  217.         this.headers = headers;
  218.     }
  219. }