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.core.transazioni.utils;

  21. import java.util.Date;

  22. import org.openspcoop2.core.transazioni.DumpContenuto;
  23. import org.openspcoop2.utils.UtilsException;
  24. import org.openspcoop2.utils.io.CompressorType;
  25. import org.openspcoop2.utils.io.CompressorUtilities;

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

  34.     public static final String KEY_VALUE_TOO_LONG = "_____ValueTooLong_SaveBinaryInfo____";
  35.     public static final int SOGLIA_VALUE_TOO_LONG = 4000;
  36.    
  37.     public static final String KEY_COMPRESSED = "_____Compressed_____";
  38.    
  39.     public static DumpContenuto createDumpContenuto(String nome, String valore, Date dumpTimestamp){
  40.         DumpContenuto dumpContenuto = new DumpContenuto();
  41.         dumpContenuto.setNome(nome);
  42.         setDumpContenutoValue(dumpContenuto, valore);
  43.         dumpContenuto.setDumpTimestamp(dumpTimestamp);
  44.         return dumpContenuto;
  45.     }
  46.    
  47.     public static void setDumpContenutoValue(DumpContenuto dumpContenuto, String valore){
  48.         if(valore!=null && valore.length()>SOGLIA_VALUE_TOO_LONG){
  49.             dumpContenuto.setValore(KEY_VALUE_TOO_LONG);
  50.             dumpContenuto.setValoreAsBytes(valore.getBytes());
  51.         }
  52.         else{
  53.             dumpContenuto.setValore(valore);
  54.             dumpContenuto.setValoreAsBytes(null);
  55.         }
  56.     }
  57.    
  58.     public static String getDumpContenutoValue(DumpContenuto dumpContenuto){
  59.         if(KEY_VALUE_TOO_LONG.equals(dumpContenuto.getValore())){
  60.             return new String(dumpContenuto.getValoreAsBytes());
  61.         }
  62.         else{
  63.             return dumpContenuto.getValore();
  64.         }
  65.     }
  66.    
  67.     public static void compress(DumpContenuto dumpContenuto,CompressorType tipoCompressione) throws UtilsException{
  68.         dumpContenuto.setValoreAsBytes(CompressorUtilities.compress(TransactionContentUtils.getDumpContenutoValue(dumpContenuto).getBytes(),tipoCompressione));
  69.         dumpContenuto.setValore(KEY_COMPRESSED);
  70.     }
  71.    
  72.     public static void decompress(DumpContenuto dumpContenuto,CompressorType tipoCompressione) throws UtilsException{
  73.         String valoreDecompresso = new String(CompressorUtilities.decompress(dumpContenuto.getValoreAsBytes(),tipoCompressione));
  74. //      dumpContenuto.setValore(valoreDecompresso);
  75. //      dumpContenuto.setValoreAsBytes(null);
  76.         setDumpContenutoValue(dumpContenuto, valoreDecompresso);
  77.     }
  78. }