DriverConfigurazioneDB_connettoriDriver.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.config.driver.db;

  21. import java.sql.Connection;
  22. import java.sql.PreparedStatement;
  23. import java.sql.ResultSet;
  24. import java.util.ArrayList;
  25. import java.util.List;

  26. import org.apache.commons.lang.StringUtils;
  27. import org.openspcoop2.core.byok.IDriverBYOK;
  28. import org.openspcoop2.core.config.Connettore;
  29. import org.openspcoop2.core.config.Property;
  30. import org.openspcoop2.core.config.driver.ConnettorePropertiesUtilities;
  31. import org.openspcoop2.core.config.driver.DriverConfigurazioneException;
  32. import org.openspcoop2.core.config.driver.DriverConfigurazioneNotFound;
  33. import org.openspcoop2.core.constants.CostantiConnettori;
  34. import org.openspcoop2.core.constants.CostantiDB;
  35. import org.openspcoop2.utils.UtilsException;
  36. import org.openspcoop2.utils.jdbc.JDBCUtilities;
  37. import org.openspcoop2.utils.sql.ISQLQueryObject;
  38. import org.openspcoop2.utils.sql.SQLObjectFactory;

  39. /**
  40.  * DriverConfigurazioneDB_connettoriDriver
  41.  *
  42.  *
  43.  * @author Sandra Giangrandi (sandra@link.it)
  44.  * @author Stefano Corallo (corallo@link.it)
  45.  * @author $Author$
  46.  * @version $Rev$, $Date$
  47.  */
  48. public class DriverConfigurazioneDB_connettoriDriver {

  49.     private DriverConfigurazioneDB driver = null;
  50.    
  51.     protected DriverConfigurazioneDB_connettoriDriver(DriverConfigurazioneDB driver) {
  52.         this.driver = driver;
  53.     }
  54.    
  55.     protected void createConnettore(Connettore connettore) throws DriverConfigurazioneException {
  56.         if (connettore == null)
  57.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB::createConnettore] Parametro non valido.");

  58.         Connection con = null;
  59.         boolean error = false;

  60.         if (this.driver.atomica) {
  61.             try {
  62.                 con = this.driver.getConnectionFromDatasource("createConnettore");
  63.                 con.setAutoCommit(false);
  64.             } catch (Exception e) {
  65.                 throw new DriverConfigurazioneException("[DriverConfigurazioneDB::createConnettore] Exception accedendo al datasource :" + e.getMessage(),e);

  66.             }

  67.         } else
  68.             con = this.driver.globalConnection;

  69.         this.driver.logDebug("operazione this.driver.atomica = " + this.driver.atomica);

  70.         try {
  71.             this.driver.logDebug("CRUDConnettore type = 1");
  72.             // creo connettore
  73.             DriverConfigurazioneDB_connettoriLIB.CRUDConnettore(1, connettore, con, this.driver.getDriverWrapBYOK());

  74.         } catch (Exception qe) {
  75.             error = true;
  76.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB::createConnettore] Errore durante la creazione del connettore : " + qe.getMessage(),qe);
  77.         } finally {
  78.             this.driver.closeConnection(error,con);
  79.         }
  80.     }

  81.     protected void updateConnettore(Connettore connettore) throws DriverConfigurazioneException {
  82.         if (connettore == null)
  83.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB::updateConnettore] Parametro non valido.");

  84.         Connection con = null;
  85.         boolean error = false;

  86.         if (this.driver.atomica) {
  87.             try {
  88.                 con = this.driver.getConnectionFromDatasource("updateConnettore");
  89.                 con.setAutoCommit(false);
  90.             } catch (Exception e) {
  91.                 throw new DriverConfigurazioneException("[DriverConfigurazioneDB::updateConnettore] Exception accedendo al datasource :" + e.getMessage(),e);

  92.             }

  93.         } else
  94.             con = this.driver.globalConnection;

  95.         this.driver.logDebug("operazione this.driver.atomica = " + this.driver.atomica);

  96.         try {
  97.             this.driver.logDebug("CRUDConnettore type = 2");
  98.             // update connettore
  99.             DriverConfigurazioneDB_connettoriLIB.CRUDConnettore(2, connettore, con, this.driver.getDriverWrapBYOK());

  100.         } catch (Exception qe) {
  101.             error = true;
  102.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB::updateConnettore] Errore durante l'aggiornamento del connettore : " + qe.getMessage(),qe);
  103.         } finally {

  104.             this.driver.closeConnection(error,con);
  105.         }
  106.     }

  107.     protected void deleteConnettore(Connettore connettore) throws DriverConfigurazioneException {
  108.         if (connettore == null)
  109.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB::deleteConnettore] Parametro non valido.");

  110.         Connection con = null;
  111.         boolean error = false;

  112.         if (this.driver.atomica) {
  113.             try {
  114.                 con = this.driver.getConnectionFromDatasource("deleteConnettore");
  115.                 con.setAutoCommit(false);
  116.             } catch (Exception e) {
  117.                 throw new DriverConfigurazioneException("[DriverConfigurazioneDB::deleteConnettore] Exception accedendo al datasource :" + e.getMessage(),e);

  118.             }

  119.         } else
  120.             con = this.driver.globalConnection;

  121.         this.driver.logDebug("operazione this.driver.atomica = " + this.driver.atomica);

  122.         try {
  123.             this.driver.logDebug("CRUDConnettore type = 3");
  124.             // delete connettore
  125.             DriverConfigurazioneDB_connettoriLIB.CRUDConnettore(3, connettore, con, this.driver.getDriverWrapBYOK());

  126.         } catch (Exception qe) {
  127.             error = true;
  128.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB::deleteConnettore] Errore durante la rimozione del connettore : " + qe.getMessage(),qe);
  129.         } finally {

  130.             this.driver.closeConnection(error,con);
  131.         }
  132.     }
  133.    
  134.     protected List<String> connettoriList() throws DriverConfigurazioneException {
  135.         String nomeMetodo = "connettoriList";
  136.         Connection con = null;
  137.         PreparedStatement stmt=null;
  138.         ResultSet risultato=null;
  139.         ArrayList<String> lista = new ArrayList<>();

  140.         if (this.driver.atomica) {
  141.             try {
  142.                 con = this.driver.getConnectionFromDatasource(nomeMetodo);
  143.             } catch (Exception e) {
  144.                 throw new DriverConfigurazioneException("[DriverConfigurazioneDB::" + nomeMetodo + "] Exception accedendo al datasource :" + e.getMessage(),e);

  145.             }

  146.         } else
  147.             con = this.driver.globalConnection;

  148.         this.driver.logDebug("operazione this.driver.atomica = " + this.driver.atomica);

  149.         try {

  150.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  151.             sqlQueryObject.addFromTable(CostantiDB.CONNETTORI_PROPERTIES);
  152.             sqlQueryObject.addSelectField(CostantiDB.CONNETTORI_COLUMN_NOME);
  153.             String queryString = sqlQueryObject.createSQLQuery();
  154.             stmt = con.prepareStatement(queryString);
  155.             risultato = stmt.executeQuery();

  156.             while (risultato.next())
  157.                 lista.add(risultato.getString(CostantiDB.CONNETTORI_COLUMN_NOME));

  158.             return lista;

  159.         } catch (Exception qe) {
  160.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB::" + nomeMetodo + "] Errore : " + qe.getMessage(),qe);
  161.         } finally {

  162.             //Chiudo statement and resultset
  163.             JDBCUtilities.closeResources(risultato, stmt);

  164.             this.driver.closeConnection(con);
  165.         }
  166.     }
  167.    
  168.     protected Property[] getPropertiesConnettore(String nomeConnettore) throws DriverConfigurazioneException {
  169.         Connection con = null;
  170.         if (this.driver.atomica) {
  171.             try {
  172.                 con = this.driver.getConnectionFromDatasource("getPropertiesConnettore");

  173.             } catch (Exception e) {
  174.                 throw new DriverConfigurazioneException("[DriverDB::getPropertiesConnettore] Exception accedendo al datasource :" + e.getMessage(),e);

  175.             }

  176.         } else
  177.             con = this.driver.globalConnection;
  178.         return getPropertiesConnettore(nomeConnettore,con);
  179.     }
  180.     protected Property[] getPropertiesConnettore(String nomeConnettore, Connection connection) throws DriverConfigurazioneException {
  181.         try {
  182.             List<Property> cList = ConnettorePropertiesUtilities.getPropertiesConnettore(nomeConnettore, connection,this.driver.tipoDB);
  183.             return cList.toArray(new Property[cList.size()]);
  184.         } catch (Exception e) {
  185.             throw new DriverConfigurazioneException("[DriverDB::getPropertiesConnettore] DriverConfigurazioneException : " + e.getMessage(),e);
  186.         }
  187.     }
  188.    
  189.    
  190.     protected boolean isPolicyNegoziazioneTokenUsedInConnettore(String nome) throws DriverConfigurazioneException{
  191.         String nomeMetodo = "isPolicyNegoziazioneTokenUsedInConnettore";

  192.         Connection con = null;
  193.         PreparedStatement stmt=null;
  194.         ResultSet risultato=null;
  195.        
  196.         if (this.driver.atomica) {
  197.             try {
  198.                 con = this.driver.getConnectionFromDatasource(nomeMetodo);
  199.             } catch (Exception e) {
  200.                 throw new DriverConfigurazioneException("[DriverConfigurazioneDB::" + nomeMetodo + "] Exception accedendo al datasource :" + e.getMessage(),e);

  201.             }

  202.         } else
  203.             con = this.driver.globalConnection;

  204.         this.driver.logDebug("operazione this.driver.atomica = " + this.driver.atomica);

  205.         try {

  206.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  207.             sqlQueryObject.addFromTable(CostantiDB.CONNETTORI);
  208.             sqlQueryObject.addSelectField("id");
  209.             sqlQueryObject.addWhereCondition("token_policy=?");
  210.             String queryString = sqlQueryObject.createSQLQuery();
  211.             stmt = con.prepareStatement(queryString);
  212.             stmt.setString(1, nome);
  213.             risultato = stmt.executeQuery();

  214.             return risultato.next();

  215.         } catch (Exception qe) {
  216.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB::" + nomeMetodo + "] Errore : " + qe.getMessage(),qe);
  217.         } finally {
  218.             //Chiudo statement and resultset
  219.             JDBCUtilities.closeResources(risultato, stmt);
  220.             this.driver.closeConnection(con);
  221.         }
  222.     }
  223.    
  224.    
  225.     protected Connettore getConnettore(long idConnettore) throws DriverConfigurazioneException, DriverConfigurazioneNotFound {
  226.         String nomeMetodo = "getConnettore(id)";
  227.        
  228.         Connection con = null;
  229.         if (this.driver.atomica) {
  230.             try {
  231.                 con = this.driver.getConnectionFromDatasource(nomeMetodo);
  232.             } catch (Exception e) {
  233.                 throw new DriverConfigurazioneException("[DriverConfigurazioneDB::" + nomeMetodo + "] Exception accedendo al datasource :" + e.getMessage(),e);

  234.             }

  235.         } else
  236.             con = this.driver.globalConnection;

  237.         this.driver.logDebug("operazione this.driver.atomica = " + this.driver.atomica);

  238.         try {

  239.             Connettore connettore = DriverConfigurazioneDB_connettoriLIB.getConnettore(idConnettore, con, this.driver.getDriverUnwrapBYOK());
  240.             if(connettore==null) {
  241.                 throw new DriverConfigurazioneNotFound("Connettore con id '"+idConnettore+"' non esistente");
  242.             }
  243.                        
  244.             // Recupero anche eventuale username e password in invocazione servizio.
  245.             readCredenzialiBasicConnettore(connettore, idConnettore, con);
  246.            
  247.             return connettore;
  248.            
  249.         } catch (Exception qe) {
  250.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB::" + nomeMetodo + "] Errore : " + qe.getMessage(),qe);
  251.         } finally {

  252.             this.driver.closeConnection(con);
  253.         }
  254.     }
  255.     protected Connettore getConnettore(String nomeConnettore) throws DriverConfigurazioneException {
  256.         String nomeMetodo = "getConnettore(nome)";
  257.        
  258.         Connection con = null;
  259.         PreparedStatement stmt=null;
  260.         ResultSet risultato=null;
  261.        
  262.         if (this.driver.atomica) {
  263.             try {
  264.                 con = this.driver.getConnectionFromDatasource(nomeMetodo);
  265.             } catch (Exception e) {
  266.                 throw new DriverConfigurazioneException("[DriverConfigurazioneDB::" + nomeMetodo + "] Exception accedendo al datasource :" + e.getMessage(),e);

  267.             }

  268.         } else
  269.             con = this.driver.globalConnection;

  270.         this.driver.logDebug("operazione this.driver.atomica = " + this.driver.atomica);

  271.         try {

  272.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  273.             sqlQueryObject.addFromTable(CostantiDB.CONNETTORI);
  274.             sqlQueryObject.addSelectField("id");
  275.             sqlQueryObject.addWhereCondition("nome_connettore=?");
  276.             String queryString = sqlQueryObject.createSQLQuery();
  277.             stmt = con.prepareStatement(queryString);
  278.             stmt.setString(1, nomeConnettore);
  279.             risultato = stmt.executeQuery();

  280.             Long idConnettore = null;
  281.             if (risultato.next()) {
  282.                 idConnettore = risultato.getLong("id");
  283.             }
  284.             else {
  285.                 throw new DriverConfigurazioneNotFound("Connettore con nome '"+nomeConnettore+"' non esistente");
  286.             }
  287.                    
  288.             Connettore connettore = DriverConfigurazioneDB_connettoriLIB.getConnettore(idConnettore, con, this.driver.getDriverUnwrapBYOK());
  289.             if(connettore==null) {
  290.                 throw new DriverConfigurazioneNotFound("Connettore con id '"+idConnettore+"' non esistente");
  291.             }
  292.            
  293.             risultato.close();
  294.             stmt.close();
  295.            
  296.             // Recupero anche eventuale username e password in invocazione servizio.
  297.             readCredenzialiBasicConnettore(connettore, idConnettore, con);
  298.            
  299.             return connettore;
  300.            
  301.         } catch (Exception qe) {
  302.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB::" + nomeMetodo + "] Errore : " + qe.getMessage(),qe);
  303.         } finally {

  304.             //Chiudo statement and resultset
  305.             JDBCUtilities.closeResources(risultato, stmt);

  306.             this.driver.closeConnection(con);
  307.         }
  308.     }
  309.     private void readCredenzialiBasicConnettore(Connettore connettore, long idConnettore,
  310.             Connection con) throws DriverConfigurazioneException {
  311.         String nomeMetodo = "getConnettore(nome)";
  312.        
  313.         PreparedStatement stmt=null;
  314.         ResultSet risultato=null;
  315.         try {
  316.            
  317.             // Recupero anche eventuale username e password in invocazione servizio.
  318.             if(!connettore.getProperties().containsKey(CostantiConnettori.CONNETTORE_USERNAME)) {
  319.                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  320.                 sqlQueryObject.addFromTable(CostantiDB.SERVIZI_APPLICATIVI);
  321.                 sqlQueryObject.addSelectField("utenteinv");
  322.                 sqlQueryObject.addSelectField("passwordinv");
  323.                 sqlQueryObject.addSelectField("enc_passwordinv");
  324.                 sqlQueryObject.addWhereCondition("id_connettore_inv=?");
  325.                 String queryString = sqlQueryObject.createSQLQuery();
  326.                 stmt = con.prepareStatement(queryString);
  327.                 stmt.setLong(1, idConnettore);
  328.                 risultato = stmt.executeQuery();
  329.                 String user = null;
  330.                 String encPassword = null;
  331.                 String plainPassword = null;
  332.                 if (risultato.next()) {
  333.                     user = risultato.getString("utenteinv");
  334.                     plainPassword = risultato.getString("passwordinv");
  335.                     encPassword = risultato.getString("enc_passwordinv");
  336.                 }
  337.                 else {
  338.                     // cerco come risposta asincrona
  339.                     risultato.close();
  340.                     stmt.close();
  341.                    
  342.                     sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  343.                     sqlQueryObject.addFromTable(CostantiDB.SERVIZI_APPLICATIVI);
  344.                     sqlQueryObject.addSelectField("utenterisp");
  345.                     sqlQueryObject.addSelectField("passwordrisp");
  346.                     sqlQueryObject.addSelectField("enc_passwordrisp");
  347.                     sqlQueryObject.addWhereCondition("id_connettore_risp=?");
  348.                     queryString = sqlQueryObject.createSQLQuery();
  349.                     stmt = con.prepareStatement(queryString);
  350.                     stmt.setLong(1, idConnettore);
  351.                     risultato = stmt.executeQuery();
  352.                     if (risultato.next()) {
  353.                         user = risultato.getString("utenterisp");
  354.                         plainPassword = risultato.getString("passwordrisp");
  355.                         encPassword = risultato.getString("enc_passwordrisp");
  356.                     }
  357.                 }
  358.                
  359.                 setUsernameProperty(user, connettore) ;
  360.                 setPasswordProperty(plainPassword, encPassword, connettore);

  361.             }
  362.            
  363.         } catch (Exception qe) {
  364.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB::" + nomeMetodo + "] Errore : " + qe.getMessage(),qe);
  365.         } finally {

  366.             //Chiudo statement and resultset
  367.             JDBCUtilities.closeResources(risultato, stmt);

  368.         }
  369.     }
  370.    
  371.     private void setUsernameProperty(String user, Connettore connettore) {
  372.         if(user!=null) {
  373.             Property property = new Property();
  374.             property.setNome(CostantiConnettori.CONNETTORE_USERNAME);
  375.             property.setValore(user);
  376.             connettore.addProperty(property);
  377.         }
  378.     }
  379.     private void setPasswordProperty(String plainPassword, String encPassword, Connettore connettore) throws UtilsException {
  380.         if(plainPassword!=null || encPassword!=null) {
  381.             Property property = new Property();
  382.             property.setNome(CostantiConnettori.CONNETTORE_PASSWORD);
  383.            
  384.             if(encPassword!=null && StringUtils.isNotEmpty(encPassword)) {
  385.                 IDriverBYOK driverBYOK = this.driver.getDriverUnwrapBYOK();
  386.                 if(driverBYOK!=null) {
  387.                     property.setValore(driverBYOK.unwrapAsString(encPassword));
  388.                 }
  389.                 else {
  390.                     property.setValore(encPassword);
  391.                 }
  392.             }
  393.             else {
  394.                 property.setValore(plainPassword);
  395.             }
  396.        
  397.             connettore.addProperty(property);
  398.         }
  399.     }
  400. }