DriverConfigurazioneDB_genericPropertiesDriver.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.sql.SQLException;
  25. import java.util.ArrayList;
  26. import java.util.HashMap;
  27. import java.util.List;
  28. import java.util.Map;

  29. import org.apache.commons.lang.StringUtils;
  30. import org.openspcoop2.core.byok.IDriverBYOK;
  31. import org.openspcoop2.core.commons.Filtri;
  32. import org.openspcoop2.core.commons.ISearch;
  33. import org.openspcoop2.core.commons.SearchUtils;
  34. import org.openspcoop2.core.config.GenericProperties;
  35. import org.openspcoop2.core.config.Property;
  36. import org.openspcoop2.core.config.driver.DriverConfigurazioneException;
  37. import org.openspcoop2.core.config.driver.DriverConfigurazioneNotFound;
  38. import org.openspcoop2.core.constants.CostantiDB;
  39. import org.openspcoop2.core.constants.CostantiProprieta;
  40. import org.openspcoop2.utils.jdbc.JDBCUtilities;
  41. import org.openspcoop2.utils.sql.ISQLQueryObject;
  42. import org.openspcoop2.utils.sql.SQLObjectFactory;

  43. /**
  44.  * DriverConfigurazioneDB_genericPropertiesDriver
  45.  *
  46.  *
  47.  * @author Sandra Giangrandi (sandra@link.it)
  48.  * @author Stefano Corallo (corallo@link.it)
  49.  * @author $Author$
  50.  * @version $Rev$, $Date$
  51.  */
  52. public class DriverConfigurazioneDB_genericPropertiesDriver {

  53.     private static Map<String, List<String>> propertiesConfidentials = new HashMap<>();
  54.     static {
  55.         propertiesConfidentials.put(CostantiProprieta.TOKEN_VALIDATION_ID, CostantiProprieta.getTokenValidationProperties());
  56.         propertiesConfidentials.put(CostantiProprieta.TOKEN_NEGOZIAZIONE_ID, CostantiProprieta.getTokenRetrieveProperties());
  57.         propertiesConfidentials.put(CostantiProprieta.ATTRIBUTE_AUTHORITY_ID, CostantiProprieta.getAttributeAuthorityProperties());
  58.         List<String> messageSecurityIds = CostantiProprieta.getMessageSecurityIds();
  59.         if(messageSecurityIds!=null && !messageSecurityIds.isEmpty()) {
  60.             for (String id : messageSecurityIds) {
  61.                 propertiesConfidentials.put(id, CostantiProprieta.getMessageSecurityProperties(id));
  62.             }
  63.         }
  64.     }
  65.     public static void addConfidentialProperty(String tipo, String nome){
  66.         List<String> l = propertiesConfidentials.computeIfAbsent(tipo, k -> new ArrayList<>());

  67.         if(!l.contains(nome)) {
  68.             l.add(nome);
  69.         }
  70.     }
  71.     public static boolean isConfidentialProperty(String tipo, String nome) {
  72.         if(tipo==null || nome==null) {
  73.             return false;
  74.         }
  75.         List<String> l = propertiesConfidentials.get(tipo);
  76.         if(l!=null) {
  77.             return  isConfidentialProperty(l, nome);
  78.         }
  79.         return false;
  80.     }
  81.     private static boolean isConfidentialProperty(List<String> l, String nome) {
  82.         if(l.isEmpty()) {
  83.             return false;
  84.         }
  85.         if(l.contains(nome)) {
  86.             return true;
  87.         }
  88.        
  89.         if(nome.contains(CostantiProprieta.KEY_PROPERTIES_CUSTOM_SEPARATOR)) {
  90.             return isConfidentialPropertyCustomSeparator(l, nome);
  91.         }
  92.         else if(nome.contains(CostantiProprieta.KEY_PROPERTIES_DEFAULT_SEPARATOR)) {
  93.             return isConfidentialPropertyDefaultSeparator(l, nome);
  94.         }
  95.        
  96.         return false;
  97.     }
  98.     private static boolean isConfidentialPropertyCustomSeparator(List<String> l, String nome) {
  99.         String [] tmp = nome.split(CostantiProprieta.KEY_PROPERTIES_CUSTOM_SEPARATOR);
  100.         if(tmp!=null && tmp.length>1 && tmp[1]!=null){
  101.             for (String s : l) {
  102.                 if(tmp[1].equals(s)) {
  103.                     return true;
  104.                 }
  105.             }
  106.         }
  107.         return false;
  108.     }
  109.     private static boolean isConfidentialPropertyDefaultSeparator(List<String> l, String nome) {
  110.         String [] tmp = nome.split(CostantiProprieta.KEY_PROPERTIES_DEFAULT_SEPARATOR);
  111.         if(tmp!=null && tmp.length>1 && tmp[1]!=null){
  112.             for (String s : l) {
  113.                 if(tmp[1].equals(s)) {
  114.                     return true;
  115.                 }
  116.             }
  117.         }
  118.         return false;
  119.     }
  120.    
  121.    
  122.     private DriverConfigurazioneDB driver = null;
  123.     private DriverConfigurazioneDBUtils utilsDriver = null;
  124.    
  125.     protected DriverConfigurazioneDB_genericPropertiesDriver(DriverConfigurazioneDB driver) {
  126.         this.driver = driver;
  127.         this.utilsDriver = new DriverConfigurazioneDBUtils(driver);
  128.     }
  129.    
  130.     /**
  131.      * Restituisce le proprieta' generiche di una tipologia utilizzate dalla PdD
  132.      *
  133.      * @return proprieta' generiche
  134.      *
  135.      */
  136.     protected List<GenericProperties> getGenericProperties() throws DriverConfigurazioneException,DriverConfigurazioneNotFound{
  137.         return getGenericProperties(null);
  138.     }
  139.    
  140.     /**
  141.      * Restituisce le proprieta' generiche utilizzate dalla PdD
  142.      *
  143.      * @return proprieta' generiche
  144.      *
  145.      */
  146.     protected List<GenericProperties> getGenericProperties(String tipologia) throws DriverConfigurazioneException,DriverConfigurazioneNotFound{
  147.         List<String> listTipologia = new ArrayList<>();
  148.         if(tipologia!=null) {
  149.             listTipologia.add(tipologia);
  150.         }
  151.         return getGenericProperties(listTipologia, null, null,true, null);
  152.     }
  153.    
  154.     protected GenericProperties getGenericProperties(String tipologia, String name) throws DriverConfigurazioneException,DriverConfigurazioneNotFound{
  155.         List<String> listTipologia = new ArrayList<>();
  156.         if(tipologia!=null) {
  157.             listTipologia.add(tipologia);
  158.         }
  159.         List<GenericProperties> l = getGenericProperties(listTipologia, null, null,true, name);
  160.         if(l==null || l.isEmpty()) {
  161.             throw new DriverConfigurazioneNotFound("[getGenericProperties] Configurazione Generic Properties non presenti con tipologia '"+tipologia+"' e nome '"+name+"'");
  162.         }
  163.         else if(l.size()>1) {
  164.             throw new DriverConfigurazioneException("[getGenericProperties] Trovata più di una collezione di proprietà con tipologia '"+tipologia+"' e nome '"+name+"'");
  165.         }
  166.         return l.get(0);
  167.     }
  168.    
  169.     /**
  170.      * Restituisce le proprieta' generiche utilizzate dalla PdD
  171.      *
  172.      * @return proprieta' generiche
  173.      *
  174.      */
  175.     protected List<GenericProperties> getGenericProperties(List<String> tipologia, Integer idLista, ISearch ricerca, boolean throwNotFoundException) throws DriverConfigurazioneException,DriverConfigurazioneNotFound{
  176.         return getGenericProperties(tipologia, idLista, ricerca, throwNotFoundException, null);
  177.     }
  178.     private List<GenericProperties> getGenericProperties(List<String> tipologia, Integer idLista, ISearch ricerca, boolean throwNotFoundException, String nomeEsatto) throws DriverConfigurazioneException,DriverConfigurazioneNotFound{

  179.         Connection con = null;
  180.         PreparedStatement stm = null;
  181.         ResultSet rs = null;
  182.        
  183.         Integer offset = null;
  184.         Integer limit =null;
  185.         String search = "";
  186.         String filterTipoTokenPolicy = null;
  187.         if(idLista != null && ricerca != null) {
  188.             limit = ricerca.getPageSize(idLista);
  189.             offset = ricerca.getIndexIniziale(idLista);
  190.             search = (org.openspcoop2.core.constants.Costanti.SESSION_ATTRIBUTE_VALUE_RICERCA_UNDEFINED.equals(ricerca.getSearchString(idLista)) ? "" : ricerca.getSearchString(idLista));
  191.            
  192.             filterTipoTokenPolicy = SearchUtils.getFilter(ricerca, idLista,  Filtri.FILTRO_TIPO_TOKEN_POLICY);
  193.            
  194.             this.driver.logDebug("search : " + search);
  195.             this.driver.logDebug("filterTipoTokenPolicy : " + filterTipoTokenPolicy);
  196.         }
  197.        
  198.        
  199.         String sqlQuery = "";

  200.         if (this.driver.atomica) {
  201.             try {
  202.                 con = this.driver.getConnectionFromDatasource("getGenericProperties");
  203.             } catch (Exception e) {
  204.                 throw new DriverConfigurazioneException("[getGenericProperties] Exception accedendo al datasource :" + e.getMessage(),e);

  205.             }
  206.         } else
  207.             con = this.driver.globalConnection;

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

  209.         List<Long> listIdLong = new ArrayList<>();
  210.        
  211.         try {
  212.            
  213.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  214.             sqlQueryObject.addFromTable(CostantiDB.CONFIG_GENERIC_PROPERTIES);
  215.             sqlQueryObject.addSelectCountField("*", "cont");
  216.             if(tipologia!=null && !tipologia.isEmpty()) {
  217.                 sqlQueryObject.addWhereINCondition("tipologia", true, tipologia.toArray(new String[1]));
  218.             }
  219.             if(filterTipoTokenPolicy!=null && !"".equals(filterTipoTokenPolicy)) {
  220.                 sqlQueryObject.addWhereCondition("tipo=?");
  221.             }
  222.             if(nomeEsatto!=null) {
  223.                 sqlQueryObject.addWhereCondition("nome=?");
  224.             }
  225.             if (!search.equals("")) {
  226.                 //query con search
  227.                 sqlQueryObject.addWhereLikeCondition("nome", search, true, true);
  228.             }
  229.             sqlQueryObject.setANDLogicOperator(true);
  230.             sqlQuery = sqlQueryObject.createSQLQuery();
  231.              
  232.             stm = con.prepareStatement(sqlQuery);
  233.             int index = 1;
  234.             if(filterTipoTokenPolicy!=null && !"".equals(filterTipoTokenPolicy)) {
  235.                 stm.setString(index++, filterTipoTokenPolicy);
  236.             }
  237.             if(nomeEsatto!=null) {
  238.                 stm.setString(index++, nomeEsatto);
  239.             }
  240.             rs = stm.executeQuery();
  241.             if (rs.next() && ricerca != null)
  242.                 ricerca.setNumEntries(idLista,rs.getInt(1));
  243.             rs.close();
  244.             stm.close();

  245.             // ricavo le entries
  246.             if (limit!= null && limit == 0) // con limit
  247.                 limit = ISQLQueryObject.LIMIT_DEFAULT_VALUE;

  248.             sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  249.             sqlQueryObject.addFromTable(CostantiDB.CONFIG_GENERIC_PROPERTIES);
  250.             sqlQueryObject.addSelectField("id");
  251.             sqlQueryObject.addSelectField("nome");
  252.             if(tipologia!=null && !tipologia.isEmpty()) {
  253.                 sqlQueryObject.addWhereINCondition("tipologia", true, tipologia.toArray(new String[1]));
  254.             }
  255.             if(filterTipoTokenPolicy!=null && !"".equals(filterTipoTokenPolicy)) {
  256.                 sqlQueryObject.addWhereCondition("tipo=?");
  257.             }
  258.             if(nomeEsatto!=null) {
  259.                 sqlQueryObject.addWhereCondition("nome=?");
  260.             }
  261.             if (!search.equals("")) {
  262.                 //query con search
  263.                 sqlQueryObject.addWhereLikeCondition("nome", search, true, true);
  264.             }
  265.             sqlQueryObject.setANDLogicOperator(true);
  266.             sqlQueryObject.addOrderBy("nome");
  267.             sqlQueryObject.setSortType(true);
  268.             if(limit!= null)
  269.                 sqlQueryObject.setLimit(limit);
  270.             if(offset != null)
  271.                 sqlQueryObject.setOffset(offset);
  272.            
  273.             sqlQuery = sqlQueryObject.createSQLQuery();
  274.             stm = con.prepareStatement(sqlQuery);
  275.             index = 1;
  276.             if(filterTipoTokenPolicy!=null && !"".equals(filterTipoTokenPolicy)) {
  277.                 stm.setString(index++, filterTipoTokenPolicy);
  278.             }
  279.             if(nomeEsatto!=null) {
  280.                 stm.setString(index++, nomeEsatto);
  281.             }
  282.             rs = stm.executeQuery();
  283.            
  284.             while(rs.next()){
  285.                
  286.                 long idP = rs.getLong("id");
  287.                 listIdLong.add(idP);
  288.                
  289.             }
  290.             rs.close();
  291.             stm.close();
  292.            
  293.         } catch (SQLException se) {
  294.             throw new DriverConfigurazioneException("[getGenericProperties]  SqlException: " + se.getMessage(),se);
  295.         }catch (Exception se) {
  296.             throw new DriverConfigurazioneException("[getGenericProperties]  Exception: " + se.getMessage(),se);
  297.         } finally {
  298.             JDBCUtilities.closeResources(rs, stm);
  299.             this.driver.closeConnection(con);
  300.         }

  301.        
  302.         List<GenericProperties> genericPropertiesList = new ArrayList<>();
  303.        
  304.         if(!listIdLong.isEmpty()) {
  305.             for (Long id : listIdLong) {
  306.                 genericPropertiesList.add(this.getGenericProperties(id));
  307.             }
  308.         }

  309.         if((genericPropertiesList==null || genericPropertiesList.isEmpty()) && throwNotFoundException)
  310.             throw new DriverConfigurazioneNotFound("Generic Properties non presenti");
  311.        
  312.         return genericPropertiesList;
  313.     }
  314.    
  315.     protected void createGenericProperties(GenericProperties genericProperties) throws DriverConfigurazioneException{
  316.         Connection con = null;
  317.         boolean error = false;

  318.         if (this.driver.atomica) {
  319.             try {
  320.                 con = this.driver.getConnectionFromDatasource("createGenericProperties");
  321.                 con.setAutoCommit(false);
  322.             } catch (Exception e) {
  323.                 throw new DriverConfigurazioneException("[DriverConfigurazioneDB::createGenericProperties] Exception accedendo al datasource :" + e.getMessage(),e);

  324.             }

  325.         } else
  326.             con = this.driver.globalConnection;

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

  328.         try {
  329.             this.driver.logDebug("CRUDGenericPropertiesPdD type = 1");
  330.             DriverConfigurazioneDB_configLIB.CRUDGenericProperties(1, genericProperties, con, this.driver.getDriverWrapBYOK());

  331.         } catch (Exception qe) {
  332.             error = true;
  333.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB::createGenericProperties] Errore durante la createSystemPropertiesPdD : " + qe.getMessage(),qe);
  334.         } finally {

  335.             this.driver.closeConnection(error,con);
  336.         }
  337.     }

  338.     /**
  339.      * Aggiorna le informazioni sulle proprieta' generiche della PdD
  340.      *
  341.      * @param genericProperties
  342.      * @throws DriverConfigurazioneException
  343.      */
  344.     protected void updateGenericProperties(GenericProperties genericProperties) throws DriverConfigurazioneException{
  345.         Connection con = null;
  346.         boolean error = false;

  347.         if (this.driver.atomica) {
  348.             try {
  349.                 con = this.driver.getConnectionFromDatasource("updateGenericProperties");
  350.                 con.setAutoCommit(false);
  351.             } catch (Exception e) {
  352.                 throw new DriverConfigurazioneException("[DriverConfigurazioneDB::updateGenericProperties] Exception accedendo al datasource :" + e.getMessage(),e);

  353.             }

  354.         } else
  355.             con = this.driver.globalConnection;

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

  357.         try {
  358.             this.driver.logDebug("updateGenericProperties type = 2");
  359.             DriverConfigurazioneDB_configLIB.CRUDGenericProperties(2, genericProperties, con, this.driver.getDriverWrapBYOK());

  360.         } catch (Exception qe) {
  361.             error = true;
  362.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB::updateGenericProperties] Errore durante la updateSystemPropertiesPdD : " + qe.getMessage(),qe);
  363.         } finally {

  364.             this.driver.closeConnection(error,con);
  365.         }
  366.     }


  367.     /**
  368.      * Elimina le informazioni sulle proprieta' generiche della PdD
  369.      *
  370.      * @param genericProperties
  371.      * @throws DriverConfigurazioneException
  372.      */
  373.     protected void deleteGenericProperties(GenericProperties genericProperties) throws DriverConfigurazioneException{
  374.         Connection con = null;
  375.         boolean error = false;

  376.         if (this.driver.atomica) {
  377.             try {
  378.                 con = this.driver.getConnectionFromDatasource("deleteGenericProperties");
  379.                 con.setAutoCommit(false);
  380.             } catch (Exception e) {
  381.                 throw new DriverConfigurazioneException("[DriverConfigurazioneDB::deleteGenericProperties] Exception accedendo al datasource :" + e.getMessage(),e);

  382.             }

  383.         } else
  384.             con = this.driver.globalConnection;

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

  386.         try {
  387.             this.driver.logDebug("deleteGenericProperties type = 3");
  388.             DriverConfigurazioneDB_configLIB.CRUDGenericProperties(3, genericProperties, con, this.driver.getDriverWrapBYOK());

  389.         } catch (Exception qe) {
  390.             error = true;
  391.             throw new DriverConfigurazioneException("[DriverConfigurazioneDB::deleteGenericProperties] Errore durante la deleteSystemPropertiesPdD : " + qe.getMessage(),qe);
  392.         } finally {

  393.             this.driver.closeConnection(error,con);
  394.         }
  395.     }
  396.    
  397.     protected GenericProperties getGenericProperties(long idGenericProperties) throws DriverConfigurazioneException,DriverConfigurazioneNotFound{
  398.         Connection con = null;
  399.         PreparedStatement stm = null;
  400.         ResultSet rs = null;
  401.         PreparedStatement stm2 = null;
  402.         ResultSet rs2 = null;
  403.        
  404.         String sqlQuery = "";

  405.         if (this.driver.atomica) {
  406.             try {
  407.                 con = this.driver.getConnectionFromDatasource("getGenericProperties");
  408.             } catch (Exception e) {
  409.                 throw new DriverConfigurazioneException("[getGenericProperties] Exception accedendo al datasource :" + e.getMessage(),e);

  410.             }
  411.         } else
  412.             con = this.driver.globalConnection;

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

  414.         try {
  415.             GenericProperties genericProperties = null;
  416.            
  417.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  418.             sqlQueryObject.addFromTable(CostantiDB.CONFIG_GENERIC_PROPERTIES);
  419.             sqlQueryObject.addSelectField("*");
  420.             sqlQueryObject.addWhereCondition("id=?");
  421.            
  422.             sqlQuery = sqlQueryObject.createSQLQuery();
  423.             stm = con.prepareStatement(sqlQuery);
  424.             stm.setLong(1, idGenericProperties);
  425.             rs = stm.executeQuery();

  426.             if(rs.next()){
  427.                
  428.                 genericProperties = new GenericProperties();
  429.                 genericProperties.setNome(rs.getString("nome"));
  430.                 genericProperties.setDescrizione(rs.getString("descrizione"));
  431.                 genericProperties.setTipologia(rs.getString("tipologia"));
  432.                 genericProperties.setTipo(rs.getString("tipo"));
  433.                
  434.                 // Proprieta Oggetto
  435.                 genericProperties.setProprietaOggetto(this.utilsDriver.readProprietaOggetto(rs,false));
  436.                
  437.                 long idP = rs.getLong("id");
  438.                 genericProperties.setId(idP);
  439.                
  440.                 //prendo le proprieta
  441.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  442.                 sqlQueryObject.addFromTable(CostantiDB.CONFIG_GENERIC_PROPERTY);
  443.                 sqlQueryObject.addSelectField("*");
  444.                 sqlQueryObject.addWhereCondition("id_props = ?");
  445.                 sqlQuery = sqlQueryObject.createSQLQuery();
  446.                 stm2 = con.prepareStatement(sqlQuery);
  447.                 stm2.setLong(1, idP);
  448.                 rs2 = stm2.executeQuery();
  449.                 Property genericProperty = null;
  450.                 while(rs2.next())
  451.                 {
  452.                     genericProperty = new Property();
  453.                     //proprieta
  454.                     genericProperty.setId(rs2.getLong("id"));
  455.                     genericProperty.setNome(rs2.getString("nome"));
  456.                    
  457.                     String plainValue = rs2.getString("valore");
  458.                     String encValue = rs2.getString("enc_value");
  459.                     if(encValue!=null && StringUtils.isNotEmpty(encValue)) {
  460.                         IDriverBYOK driverBYOK = this.driver.getDriverUnwrapBYOK();
  461.                         if(driverBYOK!=null) {
  462.                             genericProperty.setValore(driverBYOK.unwrapAsString(encValue));
  463.                         }
  464.                         else {
  465.                             genericProperty.setValore(encValue);
  466.                         }
  467.                     }
  468.                     else {
  469.                         genericProperty.setValore(plainValue);
  470.                     }

  471.                     genericProperties.addProperty(genericProperty);
  472.                 }
  473.                 rs2.close();
  474.                 stm2.close();
  475.                
  476.             }
  477.             rs.close();
  478.             stm.close();

  479.             if(genericProperties==null )
  480.                 throw new DriverConfigurazioneNotFound("Generic Properties non presenti");
  481.            
  482.             return genericProperties;

  483.         } catch (SQLException se) {
  484.             throw new DriverConfigurazioneException("[getGenericProperties]  SqlException: " + se.getMessage(),se);
  485.         }catch (DriverConfigurazioneNotFound e) {
  486.             throw new DriverConfigurazioneNotFound(e);
  487.         }catch (Exception se) {
  488.             throw new DriverConfigurazioneException("[getGenericProperties]  Exception: " + se.getMessage(),se);
  489.         } finally {
  490.             //Chiudo statement and resultset
  491.             JDBCUtilities.closeResources(rs2, stm2);
  492.             JDBCUtilities.closeResources(rs, stm);
  493.             this.driver.closeConnection(con);
  494.         }
  495.     }
  496. }