StreamJDBCAdapter.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.utils.jdbc;

  21. import java.io.ByteArrayInputStream;
  22. import java.io.ByteArrayOutputStream;
  23. import java.io.InputStream;
  24. import java.sql.PreparedStatement;
  25. import java.sql.ResultSet;
  26. import java.sql.SQLException;

  27. import org.openspcoop2.utils.CopyStream;
  28. import org.openspcoop2.utils.TipiDatabase;
  29. import org.openspcoop2.utils.UtilsException;

  30. /**
  31.  * Implementazione dell'interfaccia JDBCAdapter
  32.  * che definisce un adapter JDBC per la gestione del repository del messaggi
  33.  *
  34.  * @author Poli Andrea (apoli@link.it)
  35.  * @author $Author$
  36.  * @version $Rev$, $Date$
  37.  */

  38. public class StreamJDBCAdapter extends AbstractJDBCAdapter {

  39.    
  40.     public StreamJDBCAdapter(TipiDatabase tipoDatabase) {
  41.         super(tipoDatabase);
  42.     }
  43.    
  44.    
  45.    
  46.     /* ***** BYTES ****** */

  47.     /**
  48.      * Si occupa di ottenere il messaggio precedentemente salvato sul DB,
  49.      * effettuando una get all'indice <var>index</var>.
  50.      *
  51.      * @param rs ResultSet da utilizzare.
  52.      * @param index Indice su cui prelevare il messaggio
  53.      *
  54.      */
  55.     @Override
  56.     public byte[] getBinaryData(ResultSet rs, int index) throws UtilsException,SQLException{
  57.         return readIs(rs.getBinaryStream(index),index+"");
  58.     }
  59.    
  60.     /**
  61.      * Si occupa di ottenere il messaggio precedentemente salvato sul DB,
  62.      * effettuando una get all'indice <var>index</var>.
  63.      *
  64.      * @param rs ResultSet da utilizzare.
  65.      * @param rsName Nome rs su cui prelevare il messaggio
  66.      *
  67.      */
  68.     @Override
  69.     public byte[] getBinaryData(ResultSet rs, String rsName) throws UtilsException,SQLException{
  70.         return readIs(rs.getBinaryStream(rsName),rsName);  
  71.     }
  72.    
  73.     /**
  74.      * Si occupa di registrare il messaggio sul DB,
  75.      * all'indice <var>index</var>.
  76.      *
  77.      * @param s PreparedStatement da utilizzare utilizzare.
  78.      * @param index Indice su cui registrare il messaggio
  79.      * @param data Messaggio
  80.      *
  81.      */
  82.     @Override
  83.     public void setBinaryData(PreparedStatement s, int index, byte[] data) throws UtilsException,SQLException{
  84.        
  85.         if(data!=null && data.length>0){
  86.             ByteArrayInputStream bin = null;
  87.             try{
  88.                 bin = new ByteArrayInputStream(data);
  89.             }catch (Exception e) {
  90.                 throw new UtilsException("StreamJDBCAppender error, set binary parameter [indice:"+index+"]"+e.getMessage());
  91.             }  
  92.             s.setBinaryStream(index, bin , data.length);
  93.         }
  94.         else{
  95.             s.setBinaryStream(index, null , 0);
  96.         }
  97.     }
  98.    
  99.    
  100.  
  101.    
  102.    
  103.    
  104.    
  105.    
  106.     /* ***** UTILITIES ****** */
  107.    
  108.     private byte[] readIs(InputStream is,String indice) throws UtilsException{
  109.        
  110.         if(is==null){
  111.             return null;
  112.         }
  113.        
  114.         ByteArrayOutputStream os = null;
  115.         try {
  116.             os = new ByteArrayOutputStream();
  117. //          byte [] readB = new byte[Utilities.DIMENSIONE_BUFFER];
  118. //          int readByte = 0;
  119. //          while((readByte = is.read(readB))!= -1){
  120. //              os.write(readB,0,readByte);
  121. //          }
  122.             CopyStream.copy(is, os);
  123.             is.close();
  124.            
  125.             byte[]dati = os.toByteArray();
  126.             os.close();

  127.             return dati;
  128.         } catch (Exception e) {
  129.             try{
  130.                 if(is!=null)
  131.                     is.close();
  132.             }catch(Exception io){}
  133.             try{
  134.                 if(os!=null)
  135.                     os.close();
  136.             }catch(Exception io){
  137.                 // close
  138.             }
  139.             throw new UtilsException("StreamJDBCAppender error, reading binary parameter [indice:"+indice+"]"+e.getMessage(),e);
  140.         }
  141.     }
  142.    
  143. }