TransactionContentUtils.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.monitor.engine.transaction;

  21. import java.util.Date;

  22. import org.openspcoop2.utils.UtilsException;
  23. import org.openspcoop2.utils.io.CompressorType;
  24. import org.openspcoop2.utils.io.CompressorUtilities;

  25. import org.openspcoop2.monitor.engine.config.transazioni.constants.TipoCompressione;
  26. import org.openspcoop2.core.transazioni.DumpContenuto;

  27. /**
  28.  * TransactionContentUtils
  29.  *
  30.  * @author Poli Andrea (apoli@link.it)
  31.  * @author $Author$
  32.  * @version $Rev$, $Date$
  33.  */
  34. public class TransactionContentUtils {

  35.     public static final String KEY_VALUE_TOO_LONG = "_____ValueTooLong_SaveBinaryInfo____";
  36.     public static final int SOGLIA_VALUE_TOO_LONG = 4000;
  37.    
  38.     public static final String KEY_COMPRESSED = "_____Compressed_____";
  39.    
  40.     public static DumpContenuto createDumpContenuto(String nome, String valore, Date dumpTimestamp){
  41.         DumpContenuto dumpContenuto = new DumpContenuto();
  42.         dumpContenuto.setNome(nome);
  43.         setDumpContenutoValue(dumpContenuto, valore);
  44.         dumpContenuto.setDumpTimestamp(dumpTimestamp);
  45.         return dumpContenuto;
  46.     }
  47.    
  48.     public static void setDumpContenutoValue(DumpContenuto dumpContenuto, String valore){
  49.         if(valore!=null && valore.length()>SOGLIA_VALUE_TOO_LONG){
  50.             dumpContenuto.setValore(KEY_VALUE_TOO_LONG);
  51.             dumpContenuto.setValoreAsBytes(valore.getBytes());
  52.         }
  53.         else{
  54.             dumpContenuto.setValore(valore);
  55.             dumpContenuto.setValoreAsBytes(null);
  56.         }
  57.     }
  58.    
  59.     public static String getDumpContenutoValue(DumpContenuto dumpContenuto){
  60.         if(KEY_VALUE_TOO_LONG.equals(dumpContenuto.getValore())){
  61.             return new String(dumpContenuto.getValoreAsBytes());
  62.         }
  63.         else{
  64.             return dumpContenuto.getValore();
  65.         }
  66.     }
  67.    
  68.     public static void compress(DumpContenuto dumpContenuto,TipoCompressione tipoCompressione) throws UtilsException{
  69.         switch (tipoCompressione) {
  70.         case DEFLATER:
  71.             dumpContenuto.setValoreAsBytes(CompressorUtilities.compress(TransactionContentUtils.getDumpContenutoValue(dumpContenuto).getBytes(),CompressorType.DEFLATER));
  72.             break;
  73.         case GZIP:
  74.             dumpContenuto.setValoreAsBytes(CompressorUtilities.compress(TransactionContentUtils.getDumpContenutoValue(dumpContenuto).getBytes(),CompressorType.GZIP));
  75.             break;
  76.         case ZIP:
  77.             dumpContenuto.setValoreAsBytes(CompressorUtilities.compress(TransactionContentUtils.getDumpContenutoValue(dumpContenuto).getBytes(),CompressorType.ZIP));
  78.             break;
  79.         }
  80.         dumpContenuto.setValore(KEY_COMPRESSED);
  81.     }
  82.    
  83.     public static void decompress(DumpContenuto dumpContenuto,TipoCompressione tipoCompressione) throws UtilsException{
  84.         switch (tipoCompressione) {
  85.         case DEFLATER:
  86.             dumpContenuto.setValore(new String(CompressorUtilities.decompress(dumpContenuto.getValoreAsBytes(),CompressorType.DEFLATER)));
  87.             break;
  88.         case GZIP:
  89.             dumpContenuto.setValore(new String(CompressorUtilities.decompress(dumpContenuto.getValoreAsBytes(),CompressorType.GZIP)));
  90.             break;
  91.         case ZIP:
  92.             dumpContenuto.setValore(new String(CompressorUtilities.decompress(dumpContenuto.getValoreAsBytes(),CompressorType.ZIP)));
  93.             break;
  94.         }
  95.         dumpContenuto.setValoreAsBytes(null);
  96.     }
  97. }