DriverRegistroServiziDB_connettoriLIB.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.registry.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.sql.Types;
  26. import java.util.ArrayList;
  27. import java.util.HashMap;
  28. import java.util.List;
  29. import java.util.Map;

  30. import org.apache.commons.lang.StringUtils;
  31. import org.openspcoop2.core.byok.BYOKWrappedValue;
  32. import org.openspcoop2.core.byok.IDriverBYOK;
  33. import org.openspcoop2.core.constants.CostantiConnettori;
  34. import org.openspcoop2.core.constants.CostantiDB;
  35. import org.openspcoop2.core.constants.TipiConnettore;
  36. import org.openspcoop2.core.registry.Connettore;
  37. import org.openspcoop2.core.registry.Property;
  38. import org.openspcoop2.core.registry.driver.DriverRegistroServiziException;
  39. import org.openspcoop2.utils.jdbc.JDBCUtilities;
  40. import org.openspcoop2.utils.sql.ISQLQueryObject;
  41. import org.openspcoop2.utils.sql.SQLObjectFactory;

  42. /**
  43.  * Classe utilizzata per effettuare query ad un registro dei servizi openspcoop
  44.  * formato db.
  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 DriverRegistroServiziDB_connettoriLIB {


  53.     /**
  54.      * CRUD oggetto Connettore. In caso di CREATE inserisce nel db il dati del
  55.      * connettore passato e ritorna l'id dell'oggetto creato Non si occupa di
  56.      * chiudere la connessione con il db in caso di errore in quanto verra'
  57.      * gestita dal metodo chiamante
  58.      *
  59.      * @param type
  60.      *            Tipo operazione {1 (CREATE),2 (UPDATE),3 (DELETE)}
  61.      * @param connettore
  62.      * @return id del connettore in caso di type 1 (CREATE)
  63.      */
  64.     public static long CRUDConnettore(int type, Connettore connettore, Connection connection, IDriverBYOK driverBYOK) throws DriverRegistroServiziException {
  65.         PreparedStatement stm = null;
  66.         String sqlQuery;

  67.         if(connettore == null) throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::CRUDConnettore] L'oggetto Connettore non puo essere NULL.");
  68.         if (type!=CostantiDB.DELETE &&
  69.             (connettore.getNome() == null || connettore.getNome().trim().equals(""))
  70.             ){
  71.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::CRUDConnettore] il nome connettore non puo essere NULL.");
  72.         }

  73.         String nomeConnettore = null;
  74.         String endpointtype = null;
  75.         boolean debug = false;
  76.         String url = null;
  77.         String nome = null;
  78.         String tipo = null;
  79.         String utente = null;
  80.         String password = null;
  81.         String initcont = null;
  82.         String urlpkg = null;
  83.         String provurl = null;
  84.         String connectionfactory = null;
  85.         String sendas = null;

  86.         String transferMode = null; // in caso di tipo http e https
  87.         Integer transferModeChunkSize = null; // in caso di tipo http e https

  88.         boolean proxy = false;
  89.         String proxyType = null;
  90.         String proxyHostname = null;
  91.         String proxyPort = null;
  92.         String proxyUsername = null;
  93.         String proxyPassword = null;
  94.        
  95.         Integer tempiRispostaConnectionTimeout = null;
  96.         Integer tempiRispostaReadTimeout = null;
  97.         Integer tempiRispostaAvgResponseTime = null;

  98.         String redirectMode = null; // in caso di tipo http e https
  99.         Integer redirectMaxHop = null; // in caso di tipo http e https
  100.        
  101.         String tokenPolicy = null;
  102.        
  103.         String apiKey = null;
  104.         String apiKeyHeader = null;
  105.         String appId = null;
  106.         String appIdHeader = null;
  107.        
  108.         // setto i dati, se le property non sono presenti il loro valore rimarra
  109.         // a null e verra settato come tale nel DB
  110.         String nomeProperty = null;
  111.         String valoreProperty = null;

  112.         boolean isAbilitato = false;
  113.        
  114.         nomeConnettore = connettore.getNome();
  115.         endpointtype = connettore.getTipo();

  116.         if (endpointtype == null || endpointtype.trim().equals(""))
  117.             endpointtype = TipiConnettore.DISABILITATO.getNome();

  118.         Map<String, String> extendedProperties = new HashMap<>();
  119.        
  120.         List<String> propertiesGestiteAttraversoColonneAdHoc = new ArrayList<>();
  121.        
  122.         for (int i = 0; i < connettore.sizePropertyList(); i++) {
  123.             nomeProperty = connettore.getProperty(i).getNome();

  124.             valoreProperty = connettore.getProperty(i).getValore();
  125.             if (valoreProperty != null && valoreProperty.equals(""))
  126.                 valoreProperty = null;

  127.             // Debug
  128.             if (nomeProperty.equals(CostantiDB.CONNETTORE_DEBUG) &&
  129.                 "true".equals(valoreProperty)){
  130.                 debug=true;
  131.             }
  132.            
  133.             // Proxy
  134.             if (nomeProperty.equals(CostantiDB.CONNETTORE_PROXY_TYPE)){
  135.                 proxy = true;
  136.                 proxyType = valoreProperty;
  137.                
  138.                 propertiesGestiteAttraversoColonneAdHoc.add(nomeProperty);
  139.                
  140.                 // cerco altri valori del proxy
  141.                 for (Property propertyCheck: connettore.getPropertyList()) {
  142.                     if (propertyCheck.getNome().equals(CostantiDB.CONNETTORE_PROXY_HOSTNAME)){
  143.                         propertiesGestiteAttraversoColonneAdHoc.add(propertyCheck.getNome());
  144.                         proxyHostname = propertyCheck.getValore();
  145.                     }
  146.                     if (propertyCheck.getNome().equals(CostantiDB.CONNETTORE_PROXY_PORT)){
  147.                         propertiesGestiteAttraversoColonneAdHoc.add(propertyCheck.getNome());
  148.                         proxyPort = propertyCheck.getValore();
  149.                     }
  150.                     if (propertyCheck.getNome().equals(CostantiDB.CONNETTORE_PROXY_USERNAME)){
  151.                         propertiesGestiteAttraversoColonneAdHoc.add(propertyCheck.getNome());
  152.                         proxyUsername = propertyCheck.getValore();
  153.                     }
  154.                     if (propertyCheck.getNome().equals(CostantiDB.CONNETTORE_PROXY_PASSWORD)){
  155.                         propertiesGestiteAttraversoColonneAdHoc.add(propertyCheck.getNome());
  156.                         proxyPassword = propertyCheck.getValore();
  157.                     }
  158.                 }
  159.             }
  160.            
  161.             // Tempi Risposta
  162.             if (nomeProperty.equals(CostantiDB.CONNETTORE_CONNECTION_TIMEOUT)){
  163.                 propertiesGestiteAttraversoColonneAdHoc.add(nomeProperty);
  164.                 tempiRispostaConnectionTimeout = Integer.parseInt(valoreProperty);
  165.             }
  166.             if (nomeProperty.equals(CostantiDB.CONNETTORE_READ_CONNECTION_TIMEOUT)){
  167.                 propertiesGestiteAttraversoColonneAdHoc.add(nomeProperty);
  168.                 tempiRispostaReadTimeout = Integer.parseInt(valoreProperty);
  169.             }
  170.             if (nomeProperty.equals(CostantiDB.CONNETTORE_TEMPO_MEDIO_RISPOSTA)){
  171.                 propertiesGestiteAttraversoColonneAdHoc.add(nomeProperty);
  172.                 tempiRispostaAvgResponseTime = Integer.parseInt(valoreProperty);
  173.             }
  174.            
  175.             // TransferMode
  176.             if (nomeProperty.equals(CostantiDB.CONNETTORE_HTTP_DATA_TRANSFER_MODE)){
  177.                 propertiesGestiteAttraversoColonneAdHoc.add(nomeProperty);
  178.                 transferMode = valoreProperty;
  179.             }
  180.             if (nomeProperty.equals(CostantiDB.CONNETTORE_HTTP_DATA_TRANSFER_MODE_CHUNK_SIZE)){
  181.                 propertiesGestiteAttraversoColonneAdHoc.add(nomeProperty);
  182.                 transferModeChunkSize = Integer.parseInt(valoreProperty);
  183.             }
  184.            
  185.             // RedirectMode
  186.             if (nomeProperty.equals(CostantiDB.CONNETTORE_HTTP_REDIRECT_FOLLOW)){
  187.                 propertiesGestiteAttraversoColonneAdHoc.add(nomeProperty);
  188.                 redirectMode = valoreProperty;
  189.             }
  190.             if (nomeProperty.equals(CostantiDB.CONNETTORE_HTTP_REDIRECT_MAX_HOP)){
  191.                 propertiesGestiteAttraversoColonneAdHoc.add(nomeProperty);
  192.                 redirectMaxHop = Integer.parseInt(valoreProperty);
  193.             }
  194.            
  195.             // TokenPolicy
  196.             if (nomeProperty.equals(CostantiDB.CONNETTORE_TOKEN_POLICY)){
  197.                 propertiesGestiteAttraversoColonneAdHoc.add(nomeProperty);
  198.                 tokenPolicy = valoreProperty;
  199.             }
  200.            
  201.             // ApiKey
  202.             if (nomeProperty.equals(CostantiDB.CONNETTORE_APIKEY)){
  203.                 propertiesGestiteAttraversoColonneAdHoc.add(nomeProperty);
  204.                 apiKey = valoreProperty;
  205.             }
  206.             if (nomeProperty.equals(CostantiDB.CONNETTORE_APIKEY_HEADER)){
  207.                 propertiesGestiteAttraversoColonneAdHoc.add(nomeProperty);
  208.                 apiKeyHeader = valoreProperty;
  209.             }
  210.             if (nomeProperty.equals(CostantiDB.CONNETTORE_APIKEY_APPID)){
  211.                 propertiesGestiteAttraversoColonneAdHoc.add(nomeProperty);
  212.                 appId = valoreProperty;
  213.             }
  214.             if (nomeProperty.equals(CostantiDB.CONNETTORE_APIKEY_APPID_HEADER)){
  215.                 propertiesGestiteAttraversoColonneAdHoc.add(nomeProperty);
  216.                 appIdHeader = valoreProperty;
  217.             }
  218.            
  219.             if(TipiConnettore.HTTP.getNome().equals(endpointtype)){
  220.                 if (nomeProperty.equals(CostantiDB.CONNETTORE_HTTP_LOCATION))
  221.                     url = valoreProperty;
  222.                 else if (nomeProperty.equals(CostantiDB.CONNETTORE_USER))
  223.                     utente = valoreProperty;
  224.                 else if (nomeProperty.equals(CostantiDB.CONNETTORE_PWD))
  225.                     password = valoreProperty;
  226.             }
  227.             else if(TipiConnettore.JMS.getNome().equals(endpointtype)){
  228.                 if (nomeProperty.equals(CostantiDB.CONNETTORE_JMS_NOME))
  229.                     nome = valoreProperty;
  230.                 else if (nomeProperty.equals(CostantiDB.CONNETTORE_JMS_TIPO))
  231.                     tipo = valoreProperty;
  232.                 else if (nomeProperty.equals(CostantiDB.CONNETTORE_USER))
  233.                     utente = valoreProperty;
  234.                 else if (nomeProperty.equals(CostantiDB.CONNETTORE_PWD))
  235.                     password = valoreProperty;
  236.                 else if (nomeProperty.equals(CostantiDB.CONNETTORE_JMS_CONTEXT_JAVA_NAMING_FACTORY_INITIAL))
  237.                     initcont = valoreProperty;
  238.                 else if (nomeProperty.equals(CostantiDB.CONNETTORE_JMS_CONTEXT_JAVA_NAMING_FACTORY_URL_PKG))
  239.                     urlpkg = valoreProperty;
  240.                 else if (nomeProperty.equals(CostantiDB.CONNETTORE_JMS_CONTEXT_JAVA_NAMING_PROVIDER_URL))
  241.                     provurl = valoreProperty;
  242.                 else if (nomeProperty.equals(CostantiDB.CONNETTORE_JMS_CONNECTION_FACTORY))
  243.                     connectionfactory = valoreProperty;
  244.                 else if (nomeProperty.equals(CostantiDB.CONNETTORE_JMS_SEND_AS))
  245.                     sendas = valoreProperty;
  246.             }

  247.             // se endpointype != disabilitato allora lo setto abilitato
  248.             if (!endpointtype.equalsIgnoreCase(TipiConnettore.DISABILITATO.getNome()))
  249.                 isAbilitato = true;
  250.            
  251.             // extendedProperties
  252.             if(nomeProperty.startsWith(CostantiConnettori.CONNETTORE_EXTENDED_PREFIX)){
  253.                 extendedProperties.put(nomeProperty, valoreProperty);
  254.             }
  255.         }

  256.         try {

  257.             long idConnettore = 0;
  258.             switch (type) {
  259.             case 1:

  260.                 // create
  261.                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  262.                 sqlQueryObject.addInsertTable(CostantiDB.CONNETTORI);
  263.                 sqlQueryObject.addInsertField(CostantiDB.CONNETTORI_COLUMN_ENDPOINT_TYPE, "?");
  264.                 sqlQueryObject.addInsertField("url", "?");
  265.                 sqlQueryObject.addInsertField("transfer_mode", "?");
  266.                 sqlQueryObject.addInsertField("transfer_mode_chunk_size", "?");
  267.                 sqlQueryObject.addInsertField("redirect_mode", "?");
  268.                 sqlQueryObject.addInsertField("redirect_max_hop", "?");
  269.                 sqlQueryObject.addInsertField("nome", "?");
  270.                 sqlQueryObject.addInsertField("tipo", "?");
  271.                 sqlQueryObject.addInsertField("utente", "?");
  272.                 sqlQueryObject.addInsertField("password", "?");
  273.                 sqlQueryObject.addInsertField("enc_password", "?");
  274.                 sqlQueryObject.addInsertField("initcont", "?");
  275.                 sqlQueryObject.addInsertField("urlpkg", "?");
  276.                 sqlQueryObject.addInsertField("provurl", "?");
  277.                 sqlQueryObject.addInsertField("connection_factory", "?");
  278.                 sqlQueryObject.addInsertField("send_as", "?");
  279.                 sqlQueryObject.addInsertField(CostantiDB.CONNETTORI_COLUMN_NOME, "?");
  280.                 sqlQueryObject.addInsertField("debug", "?");
  281.                 sqlQueryObject.addInsertField("proxy", "?");        
  282.                 sqlQueryObject.addInsertField("proxy_type", "?");      
  283.                 sqlQueryObject.addInsertField("proxy_hostname", "?");      
  284.                 sqlQueryObject.addInsertField("proxy_port", "?");      
  285.                 sqlQueryObject.addInsertField("proxy_username", "?");      
  286.                 sqlQueryObject.addInsertField("proxy_password", "?");
  287.                 sqlQueryObject.addInsertField("enc_proxy_password", "?");
  288.                 sqlQueryObject.addInsertField("connection_timeout", "?");      
  289.                 sqlQueryObject.addInsertField("read_timeout", "?");    
  290.                 sqlQueryObject.addInsertField("avg_response_time", "?");
  291.                 sqlQueryObject.addInsertField("custom", "?");
  292.                 sqlQueryObject.addInsertField("token_policy", "?");
  293.                 sqlQueryObject.addInsertField("api_key", "?");
  294.                 sqlQueryObject.addInsertField("api_key_header", "?");
  295.                 sqlQueryObject.addInsertField("app_id", "?");
  296.                 sqlQueryObject.addInsertField("app_id_header", "?");
  297.                 sqlQuery = sqlQueryObject.createSQLInsert();
  298.                 stm = connection.prepareStatement(sqlQuery);

  299.                 int index = 1;
  300.                 stm.setString(index++, endpointtype);
  301.                 stm.setString(index++, url);
  302.                 stm.setString(index++, (isAbilitato ? transferMode : null));
  303.                 if(isAbilitato && transferModeChunkSize!=null){
  304.                     stm.setInt(index++, transferModeChunkSize);
  305.                 }
  306.                 else{
  307.                     stm.setNull(index++, Types.INTEGER);
  308.                 }
  309.                 stm.setString(index++, (isAbilitato ? redirectMode : null));
  310.                 if(isAbilitato && redirectMaxHop!=null){
  311.                     stm.setInt(index++, redirectMaxHop);
  312.                 }
  313.                 else{
  314.                     stm.setNull(index++, Types.INTEGER);
  315.                 }
  316.                 stm.setString(index++, isAbilitato ? nome : null);
  317.                 stm.setString(index++, isAbilitato ? tipo : null);
  318.                 stm.setString(index++, (isAbilitato ? utente : null));
  319.                
  320.                 String plainPassword = isAbilitato ? password : null;
  321.                 String encPassword = null;
  322.                 if(isAbilitato && driverBYOK!=null && plainPassword!=null) {
  323.                     BYOKWrappedValue byokValue = driverBYOK.wrap(plainPassword);
  324.                     if(byokValue!=null) {
  325.                         encPassword = byokValue.getWrappedValue();
  326.                         plainPassword = byokValue.getWrappedPlainValue();
  327.                     }
  328.                 }
  329.                 stm.setString(index++, plainPassword);
  330.                 stm.setString(index++, encPassword);
  331.                
  332.                 stm.setString(index++, (isAbilitato ? initcont : null));
  333.                 stm.setString(index++, (isAbilitato ? urlpkg : null));
  334.                 stm.setString(index++, (isAbilitato ? provurl : null));
  335.                 stm.setString(index++, (isAbilitato ? connectionfactory : null));
  336.                 stm.setString(index++, (isAbilitato ? sendas : null));
  337.                 stm.setString(index++, nomeConnettore);
  338.                 if(debug){
  339.                     stm.setInt(index++, 1);
  340.                 }else{
  341.                     stm.setInt(index++, 0);
  342.                 }
  343.                 if(proxy){
  344.                     stm.setInt(index++, 1);
  345.                 }else{
  346.                     stm.setInt(index++, 0);
  347.                 }
  348.                 stm.setString(index++, isAbilitato && proxy ? proxyType : null);
  349.                 stm.setString(index++, isAbilitato && proxy ? proxyHostname : null);
  350.                 stm.setString(index++, isAbilitato && proxy ? proxyPort : null);
  351.                 stm.setString(index++, isAbilitato && proxy ? proxyUsername : null);
  352.                
  353.                 String plainProxyPassword = isAbilitato ? proxyPassword : null;
  354.                 String encProxyPassword = null;
  355.                 if(isAbilitato && driverBYOK!=null && plainProxyPassword!=null) {
  356.                     BYOKWrappedValue byokValue = driverBYOK.wrap(plainProxyPassword);
  357.                     if(byokValue!=null) {
  358.                         encProxyPassword = byokValue.getWrappedValue();
  359.                         plainProxyPassword = byokValue.getWrappedPlainValue();
  360.                     }
  361.                 }
  362.                 stm.setString(index++, plainProxyPassword);
  363.                 stm.setString(index++, encProxyPassword);
  364.                
  365.                 if(tempiRispostaConnectionTimeout!=null) {
  366.                     stm.setInt(index++, tempiRispostaConnectionTimeout);
  367.                 }
  368.                 else {
  369.                     stm.setNull(index++, Types.INTEGER);
  370.                 }
  371.                 if(tempiRispostaReadTimeout!=null) {
  372.                     stm.setInt(index++, tempiRispostaReadTimeout);
  373.                 }
  374.                 else {
  375.                     stm.setNull(index++, Types.INTEGER);
  376.                 }
  377.                 if(tempiRispostaAvgResponseTime!=null) {
  378.                     stm.setInt(index++, tempiRispostaAvgResponseTime);
  379.                 }
  380.                 else {
  381.                     stm.setNull(index++, Types.INTEGER);
  382.                 }
  383.                 if(connettore.getCustom()!=null && connettore.getCustom()){
  384.                     stm.setInt(index++, 1);
  385.                 }else{
  386.                     stm.setInt(index++, 0);
  387.                 }
  388.                 stm.setString(index++, tokenPolicy);
  389.                
  390.                 String apiKeyInsert = isAbilitato ? apiKey : null;
  391.                 if(isAbilitato && apiKey!=null && StringUtils.isNotEmpty(apiKey) && driverBYOK!=null && CostantiConnettori.isConfidential(CostantiDB.CONNETTORE_APIKEY)) {
  392.                     BYOKWrappedValue byokValue = driverBYOK.wrap(apiKey);
  393.                     if(byokValue!=null) {
  394.                         apiKeyInsert = byokValue.getWrappedValue();
  395.                         stm.setString(index++, byokValue.getWrappedValue());
  396.                     }
  397.                     else {
  398.                         stm.setString(index++, apiKey);
  399.                     }
  400.                 }
  401.                 else {
  402.                     stm.setString(index++, apiKey);
  403.                 }
  404.                 stm.setString(index++, isAbilitato ? apiKeyHeader : null);
  405.                 stm.setString(index++, isAbilitato ? appId : null);
  406.                 stm.setString(index++, isAbilitato ? appIdHeader : null);
  407.                
  408.                 DriverRegistroServiziDB_LIB.logDebug("CRUDConnettore CREATE : \n" +
  409.                         DriverRegistroServiziDB_LIB.formatSQLString(sqlQuery, endpointtype, url,
  410.                                 transferMode, transferModeChunkSize, redirectMode, redirectMaxHop,
  411.                                 nome, tipo, utente, plainPassword, encPassword,
  412.                                 initcont, urlpkg, provurl, connectionfactory, sendas, nomeConnettore, debug,
  413.                                 proxy, proxyType, proxyHostname, proxyPort, proxyUsername, plainProxyPassword, encProxyPassword,
  414.                                 tempiRispostaConnectionTimeout, tempiRispostaReadTimeout, tempiRispostaAvgResponseTime,
  415.                                 (connettore.getCustom()!=null && connettore.getCustom()),
  416.                                 tokenPolicy,
  417.                                 apiKeyInsert, apiKeyHeader, appId, appIdHeader));
  418.                 int n = stm.executeUpdate();
  419.                 DriverRegistroServiziDB_LIB.logDebug("CRUDConnettore type = " + type + " row affected =" + n);
  420.                 stm.close();
  421.                
  422.                 ResultSet rs;
  423.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  424.                 sqlQueryObject.addFromTable(CostantiDB.CONNETTORI);
  425.                 sqlQueryObject.addSelectField("*");
  426.                 sqlQueryObject.addWhereCondition("nome_connettore = ?");
  427.                 sqlQueryObject.addWhereCondition("endpointtype = ?");
  428.                 sqlQueryObject.setANDLogicOperator(true);
  429.                 sqlQuery = sqlQueryObject.createSQLQuery();
  430.                 stm = connection.prepareStatement(sqlQuery);

  431.                 stm.setString(1, nomeConnettore);
  432.                 stm.setString(2, endpointtype);

  433.                 DriverRegistroServiziDB_LIB.logDebug("Recupero idConnettore inserito : \n" + DriverRegistroServiziDB_LIB.formatSQLString(sqlQuery, nomeConnettore, endpointtype));

  434.                 rs = stm.executeQuery();

  435.                 if (rs.next()) {
  436.                     idConnettore = rs.getLong("id");
  437.                 } else {
  438.                     throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::CRUDConnettore] Errore tentanto di effettuare la select dopo una create, non riesco a recuperare l'id!");
  439.                 }

  440.                 rs.close();
  441.                 stm.close();
  442.                                
  443.                
  444.                 // Custom properties
  445.                 if(connettore.getCustom()!=null && connettore.getCustom()){                
  446.                     sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  447.                     sqlQueryObject.addInsertTable(CostantiDB.CONNETTORI_CUSTOM);
  448.                     sqlQueryObject.addInsertField(CostantiDB.CONNETTORI_CUSTOM_COLUMN_NAME, "?");
  449.                     sqlQueryObject.addInsertField(CostantiDB.CONNETTORI_CUSTOM_COLUMN_VALUE, "?");
  450.                     sqlQueryObject.addInsertField(CostantiDB.CONNETTORI_CUSTOM_COLUMN_ENC_VALUE, "?");
  451.                     sqlQueryObject.addInsertField(CostantiDB.CONNETTORI_CUSTOM_COLUMN_ID_CONNETTORE, "?");
  452.                     sqlQuery = sqlQueryObject.createSQLInsert();
  453.                    
  454.                     for (int i = 0; i < connettore.sizePropertyList(); i++) {
  455.                         nomeProperty = connettore.getProperty(i).getNome();
  456.                         if(propertiesGestiteAttraversoColonneAdHoc.contains(nomeProperty)){
  457.                             continue;
  458.                         }
  459.                         valoreProperty = connettore.getProperty(i).getValore();
  460.                         if (valoreProperty != null && valoreProperty.equals(""))
  461.                             valoreProperty = null;
  462.                    
  463.                         if(valoreProperty==null){
  464.                             throw new DriverRegistroServiziException("Property ["+nomeProperty+"] without value");
  465.                         }
  466.                        
  467.                         String plainValue = valoreProperty;
  468.                         String encValue = null;
  469.                         if(driverBYOK!=null && CostantiConnettori.isConfidential(nomeProperty)) {
  470.                             BYOKWrappedValue byokValue = driverBYOK.wrap(valoreProperty);
  471.                             if(byokValue!=null) {
  472.                                 encValue = byokValue.getWrappedValue();
  473.                                 plainValue = byokValue.getWrappedPlainValue();
  474.                             }
  475.                         }
  476.                        
  477.                         stm = connection.prepareStatement(sqlQuery);
  478.                         stm.setString(1, nomeProperty);
  479.                         stm.setString(2, plainValue);
  480.                         stm.setString(3, encValue);
  481.                         stm.setLong(4, idConnettore);
  482.                         stm.executeUpdate();
  483.                         stm.close();
  484.                     }              
  485.                 }
  486.                 else if(extendedProperties.size()>0){
  487.                     sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  488.                     sqlQueryObject.addInsertTable(CostantiDB.CONNETTORI_CUSTOM);
  489.                     sqlQueryObject.addInsertField(CostantiDB.CONNETTORI_CUSTOM_COLUMN_NAME, "?");
  490.                     sqlQueryObject.addInsertField(CostantiDB.CONNETTORI_CUSTOM_COLUMN_VALUE, "?");
  491.                     sqlQueryObject.addInsertField(CostantiDB.CONNETTORI_CUSTOM_COLUMN_ENC_VALUE, "?");
  492.                     sqlQueryObject.addInsertField(CostantiDB.CONNETTORI_CUSTOM_COLUMN_ID_CONNETTORE, "?");
  493.                     sqlQuery = sqlQueryObject.createSQLInsert();
  494.                    
  495.                     for (String nomeP : extendedProperties.keySet()) {
  496.                         valoreProperty = extendedProperties.get(nomeP);
  497.                         if (valoreProperty != null && valoreProperty.equals(""))
  498.                             valoreProperty = null;
  499.                    
  500.                         if(valoreProperty==null){
  501.                             throw new DriverRegistroServiziException("Property ["+nomeP+"] without value");
  502.                         }
  503.                        
  504.                         String plainValue = valoreProperty;
  505.                         String encValue = null;
  506.                         if(driverBYOK!=null && CostantiConnettori.isConfidential(nomeProperty)) {
  507.                             BYOKWrappedValue byokValue = driverBYOK.wrap(valoreProperty);
  508.                             if(byokValue!=null) {
  509.                                 encValue = byokValue.getWrappedValue();
  510.                                 plainValue = byokValue.getWrappedPlainValue();
  511.                             }
  512.                         }
  513.                        
  514.                         stm = connection.prepareStatement(sqlQuery);
  515.                         stm.setString(1, nome);
  516.                         stm.setString(2, plainValue);
  517.                         stm.setString(3, encValue);
  518.                         stm.setLong(4, idConnettore);
  519.                         stm.executeUpdate();
  520.                         stm.close();
  521.                     }              
  522.                 }
  523.                
  524.                 break;

  525.             case 2:
  526.                 // update

  527.                 idConnettore = connettore.getId();

  528.                 if (idConnettore <= 0)
  529.                     throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::CRUDConnettore] L'id del connettore non puo essere 0 tentando di fare una operazione di update.");

  530.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  531.                 sqlQueryObject.addUpdateTable(CostantiDB.CONNETTORI);
  532.                 sqlQueryObject.addUpdateField(CostantiDB.CONNETTORI_COLUMN_ENDPOINT_TYPE, "?");
  533.                 sqlQueryObject.addUpdateField("url", "?");
  534.                 sqlQueryObject.addUpdateField("transfer_mode", "?");
  535.                 sqlQueryObject.addUpdateField("transfer_mode_chunk_size", "?");
  536.                 sqlQueryObject.addUpdateField("redirect_mode", "?");
  537.                 sqlQueryObject.addUpdateField("redirect_max_hop", "?");
  538.                 sqlQueryObject.addUpdateField("nome", "?");
  539.                 sqlQueryObject.addUpdateField("tipo", "?");
  540.                 sqlQueryObject.addUpdateField("utente", "?");
  541.                 sqlQueryObject.addUpdateField("password", "?");
  542.                 sqlQueryObject.addUpdateField("enc_password", "?");
  543.                 sqlQueryObject.addUpdateField("initcont", "?");
  544.                 sqlQueryObject.addUpdateField("urlpkg", "?");
  545.                 sqlQueryObject.addUpdateField("provurl", "?");
  546.                 sqlQueryObject.addUpdateField("connection_factory", "?");
  547.                 sqlQueryObject.addUpdateField("send_as", "?");
  548.                 sqlQueryObject.addUpdateField(CostantiDB.CONNETTORI_COLUMN_NOME, "?");
  549.                 sqlQueryObject.addUpdateField("debug", "?");
  550.                 sqlQueryObject.addUpdateField("proxy", "?");        
  551.                 sqlQueryObject.addUpdateField("proxy_type", "?");      
  552.                 sqlQueryObject.addUpdateField("proxy_hostname", "?");      
  553.                 sqlQueryObject.addUpdateField("proxy_port", "?");      
  554.                 sqlQueryObject.addUpdateField("proxy_username", "?");      
  555.                 sqlQueryObject.addUpdateField("proxy_password", "?");
  556.                 sqlQueryObject.addUpdateField("enc_proxy_password", "?");
  557.                 sqlQueryObject.addUpdateField("connection_timeout", "?");      
  558.                 sqlQueryObject.addUpdateField("read_timeout", "?");    
  559.                 sqlQueryObject.addUpdateField("avg_response_time", "?");
  560.                 sqlQueryObject.addUpdateField("custom", "?");
  561.                 sqlQueryObject.addUpdateField("token_policy", "?");
  562.                 sqlQueryObject.addUpdateField("api_key", "?");
  563.                 sqlQueryObject.addUpdateField("api_key_header", "?");
  564.                 sqlQueryObject.addUpdateField("app_id", "?");
  565.                 sqlQueryObject.addUpdateField("app_id_header", "?");
  566.                 sqlQueryObject.addWhereCondition("id=?");
  567.                 sqlQuery = sqlQueryObject.createSQLUpdate();
  568.                 stm = connection.prepareStatement(sqlQuery);

  569.                 index = 1;
  570.                 stm.setString(index++, endpointtype);
  571.                 stm.setString(index++, url);
  572.                 stm.setString(index++, (isAbilitato ? transferMode : null));
  573.                 if(isAbilitato && transferModeChunkSize!=null){
  574.                     stm.setInt(index++, transferModeChunkSize);
  575.                 }
  576.                 else{
  577.                     stm.setNull(index++, Types.INTEGER);
  578.                 }
  579.                 stm.setString(index++, (isAbilitato ? redirectMode : null));
  580.                 if(isAbilitato && redirectMaxHop!=null){
  581.                     stm.setInt(index++, redirectMaxHop);
  582.                 }
  583.                 else{
  584.                     stm.setNull(index++, Types.INTEGER);
  585.                 }
  586.                 stm.setString(index++, isAbilitato ? nome : null);
  587.                 stm.setString(index++, isAbilitato ? tipo : null);
  588.                 stm.setString(index++, (isAbilitato ? utente : null));

  589.                 plainPassword = isAbilitato ? password : null;
  590.                 encPassword = null;
  591.                 if(isAbilitato && driverBYOK!=null && plainPassword!=null) {
  592.                     BYOKWrappedValue byokValue = driverBYOK.wrap(plainPassword);
  593.                     if(byokValue!=null) {
  594.                         encPassword = byokValue.getWrappedValue();
  595.                         plainPassword = byokValue.getWrappedPlainValue();
  596.                     }
  597.                 }
  598.                 stm.setString(index++, plainPassword);
  599.                 stm.setString(index++, encPassword);
  600.                
  601.                 stm.setString(index++, (isAbilitato ? initcont : null));
  602.                 stm.setString(index++, (isAbilitato ? urlpkg : null));
  603.                 stm.setString(index++, (isAbilitato ? provurl : null));
  604.                 stm.setString(index++, (isAbilitato ? connectionfactory : null));
  605.                 stm.setString(index++, (isAbilitato ? sendas : null));
  606.                 stm.setString(index++, nomeConnettore);
  607.                 if(debug){
  608.                     stm.setInt(index++, 1);
  609.                 }else{
  610.                     stm.setInt(index++, 0);
  611.                 }
  612.                 if(proxy){
  613.                     stm.setInt(index++, 1);
  614.                 }else{
  615.                     stm.setInt(index++, 0);
  616.                 }
  617.                 stm.setString(index++, isAbilitato && proxy ? proxyType : null);
  618.                 stm.setString(index++, isAbilitato && proxy ? proxyHostname : null);
  619.                 stm.setString(index++, isAbilitato && proxy ? proxyPort : null);
  620.                 stm.setString(index++, isAbilitato && proxy ? proxyUsername : null);

  621.                 plainProxyPassword = isAbilitato ? proxyPassword : null;
  622.                 encProxyPassword = null;
  623.                 if(isAbilitato && driverBYOK!=null && plainProxyPassword!=null) {
  624.                     BYOKWrappedValue byokValue = driverBYOK.wrap(plainProxyPassword);
  625.                     if(byokValue!=null) {
  626.                         encProxyPassword = byokValue.getWrappedValue();
  627.                         plainProxyPassword = byokValue.getWrappedPlainValue();
  628.                     }
  629.                 }
  630.                 stm.setString(index++, plainProxyPassword);
  631.                 stm.setString(index++, encProxyPassword);
  632.                
  633.                 if(tempiRispostaConnectionTimeout!=null) {
  634.                     stm.setInt(index++, tempiRispostaConnectionTimeout);
  635.                 }
  636.                 else {
  637.                     stm.setNull(index++, Types.INTEGER);
  638.                 }
  639.                 if(tempiRispostaReadTimeout!=null) {
  640.                     stm.setInt(index++, tempiRispostaReadTimeout);
  641.                 }
  642.                 else {
  643.                     stm.setNull(index++, Types.INTEGER);
  644.                 }
  645.                 if(tempiRispostaAvgResponseTime!=null) {
  646.                     stm.setInt(index++, tempiRispostaAvgResponseTime);
  647.                 }
  648.                 else {
  649.                     stm.setNull(index++, Types.INTEGER);
  650.                 }
  651.                 if(connettore.getCustom()!=null && connettore.getCustom()){
  652.                     stm.setInt(index++, 1);
  653.                 }else{
  654.                     stm.setInt(index++, 0);
  655.                 }
  656.                 stm.setString(index++, tokenPolicy);
  657.                
  658.                 apiKeyInsert = isAbilitato ? apiKey : null;
  659.                 if(isAbilitato && apiKey!=null && StringUtils.isNotEmpty(apiKey) && driverBYOK!=null && CostantiConnettori.isConfidential(CostantiDB.CONNETTORE_APIKEY)) {
  660.                     BYOKWrappedValue byokValue = driverBYOK.wrap(apiKey);
  661.                     if(byokValue!=null) {
  662.                         apiKeyInsert = byokValue.getWrappedValue();
  663.                         stm.setString(index++, byokValue.getWrappedValue());
  664.                     }
  665.                     else {
  666.                         stm.setString(index++, apiKey);
  667.                     }
  668.                 }
  669.                 else {
  670.                     stm.setString(index++, apiKey);
  671.                 }
  672.                 stm.setString(index++, isAbilitato ? apiKeyHeader : null);
  673.                 stm.setString(index++, isAbilitato ? appId : null);
  674.                 stm.setString(index++, isAbilitato ? appIdHeader : null);
  675.                
  676.                 stm.setLong(index++, idConnettore);

  677.                 DriverRegistroServiziDB_LIB.logDebug("CRUDConnettore UPDATE : \n" +
  678.                         DriverRegistroServiziDB_LIB.formatSQLString(sqlQuery, endpointtype, url,
  679.                                 transferMode, transferModeChunkSize, redirectMode, redirectMaxHop,
  680.                                 nome, tipo, utente, plainPassword, encPassword,
  681.                                 initcont, urlpkg, provurl, connectionfactory, sendas, nomeConnettore, debug,
  682.                                 proxy, proxyType, proxyHostname, proxyPort, proxyUsername, plainProxyPassword, encProxyPassword,
  683.                                 tempiRispostaConnectionTimeout, tempiRispostaReadTimeout, tempiRispostaAvgResponseTime,
  684.                                 (connettore.getCustom()!=null && connettore.getCustom()),
  685.                                 tokenPolicy,
  686.                                 apiKeyInsert, apiKeyHeader, appId, appIdHeader,
  687.                                 idConnettore));
  688.                 n = stm.executeUpdate();
  689.                 DriverRegistroServiziDB_LIB.logDebug("CRUDConnettore type = " + type + " row affected =" + n);
  690.                 stm.close();
  691.                
  692.                
  693.                 // Custom properties
  694.                
  695.                 // Delete eventuali vecchie properties
  696.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  697.                 sqlQueryObject.addDeleteTable(CostantiDB.CONNETTORI_CUSTOM);
  698.                 sqlQueryObject.addWhereCondition("id_connettore=?");
  699.                 sqlQuery = sqlQueryObject.createSQLDelete();
  700.                 stm = connection.prepareStatement(sqlQuery);
  701.                 stm.setLong(1, idConnettore);
  702.                 stm.executeUpdate();
  703.                 stm.close();
  704.                
  705.                 // Aggiungo attuali
  706.                 if(connettore.getCustom()!=null && connettore.getCustom()){                
  707.                     sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  708.                     sqlQueryObject.addInsertTable(CostantiDB.CONNETTORI_CUSTOM);
  709.                     sqlQueryObject.addInsertField(CostantiDB.CONNETTORI_CUSTOM_COLUMN_NAME, "?");
  710.                     sqlQueryObject.addInsertField(CostantiDB.CONNETTORI_CUSTOM_COLUMN_VALUE, "?");
  711.                     sqlQueryObject.addInsertField(CostantiDB.CONNETTORI_CUSTOM_COLUMN_ENC_VALUE, "?");
  712.                     sqlQueryObject.addInsertField(CostantiDB.CONNETTORI_CUSTOM_COLUMN_ID_CONNETTORE, "?");
  713.                     sqlQuery = sqlQueryObject.createSQLInsert();
  714.                    
  715.                     for (int i = 0; i < connettore.sizePropertyList(); i++) {
  716.                         nomeProperty = connettore.getProperty(i).getNome();
  717.                         if(propertiesGestiteAttraversoColonneAdHoc.contains(nomeProperty)){
  718.                             continue;
  719.                         }
  720.                         valoreProperty = connettore.getProperty(i).getValore();
  721.                         if (valoreProperty != null && valoreProperty.equals(""))
  722.                             valoreProperty = null;
  723.                    
  724.                         if(valoreProperty==null){
  725.                             throw new DriverRegistroServiziException("Property ["+nomeProperty+"] without value");
  726.                         }
  727.                        
  728.                         String plainValue = valoreProperty;
  729.                         String encValue = null;
  730.                         if(driverBYOK!=null && CostantiConnettori.isConfidential(nomeProperty)) {
  731.                             BYOKWrappedValue byokValue = driverBYOK.wrap(valoreProperty);
  732.                             if(byokValue!=null) {
  733.                                 encValue = byokValue.getWrappedValue();
  734.                                 plainValue = byokValue.getWrappedPlainValue();
  735.                             }
  736.                         }
  737.                        
  738.                         stm = connection.prepareStatement(sqlQuery);
  739.                         stm.setString(1, nomeProperty);
  740.                         stm.setString(2, plainValue);
  741.                         stm.setString(3, encValue);
  742.                         stm.setLong(4, idConnettore);
  743.                         stm.executeUpdate();
  744.                         stm.close();
  745.                     }              
  746.                 }
  747.                 else if(extendedProperties.size()>0){
  748.                     sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  749.                     sqlQueryObject.addInsertTable(CostantiDB.CONNETTORI_CUSTOM);
  750.                     sqlQueryObject.addInsertField(CostantiDB.CONNETTORI_CUSTOM_COLUMN_NAME, "?");
  751.                     sqlQueryObject.addInsertField(CostantiDB.CONNETTORI_CUSTOM_COLUMN_VALUE, "?");
  752.                     sqlQueryObject.addInsertField(CostantiDB.CONNETTORI_CUSTOM_COLUMN_ENC_VALUE, "?");
  753.                     sqlQueryObject.addInsertField(CostantiDB.CONNETTORI_CUSTOM_COLUMN_ID_CONNETTORE, "?");
  754.                     sqlQuery = sqlQueryObject.createSQLInsert();
  755.                    
  756.                     for (String nomeP : extendedProperties.keySet()) {
  757.                         valoreProperty = extendedProperties.get(nomeP);
  758.                         if (valoreProperty != null && valoreProperty.equals(""))
  759.                             valoreProperty = null;
  760.                    
  761.                         if(valoreProperty==null){
  762.                             throw new DriverRegistroServiziException("Property ["+nomeP+"] without value");
  763.                         }
  764.                        
  765.                         String plainValue = valoreProperty;
  766.                         String encValue = null;
  767.                         if(driverBYOK!=null && CostantiConnettori.isConfidential(nomeProperty)) {
  768.                             BYOKWrappedValue byokValue = driverBYOK.wrap(valoreProperty);
  769.                             if(byokValue!=null) {
  770.                                 encValue = byokValue.getWrappedValue();
  771.                                 plainValue = byokValue.getWrappedPlainValue();
  772.                             }
  773.                         }
  774.                        
  775.                         stm = connection.prepareStatement(sqlQuery);
  776.                         stm.setString(1, nomeP);
  777.                         stm.setString(2, plainValue);
  778.                         stm.setString(3, encValue);
  779.                         stm.setLong(4, idConnettore);
  780.                         stm.executeUpdate();
  781.                         stm.close();
  782.                     }          
  783.                 }
  784.                
  785.                 break;

  786.             case 3:
  787.                 // delete
  788.                 idConnettore = connettore.getId();

  789.                 if (idConnettore <= 0)
  790.                     throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::CRUDConnettore] L'id del connettore non puo essere 0 tentando di fare una operazione di delete.");

  791.                 // Delete eventuali vecchie properties
  792.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  793.                 sqlQueryObject.addDeleteTable(CostantiDB.CONNETTORI_CUSTOM);
  794.                 sqlQueryObject.addWhereCondition("id_connettore=?");
  795.                 sqlQuery = sqlQueryObject.createSQLDelete();
  796.                 stm = connection.prepareStatement(sqlQuery);
  797.                 stm.setLong(1, idConnettore);
  798.                 stm.executeUpdate();
  799.                 stm.close();
  800.                
  801.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  802.                 sqlQueryObject.addDeleteTable(CostantiDB.CONNETTORI);
  803.                 sqlQueryObject.addWhereCondition("id=?");
  804.                 sqlQuery = sqlQueryObject.createSQLDelete();
  805.                 stm = connection.prepareStatement(sqlQuery);
  806.                 stm.setLong(1, idConnettore);
  807.                 DriverRegistroServiziDB_LIB.logDebug("CRUDConnettore DELETE : \n" + DriverRegistroServiziDB_LIB.formatSQLString(sqlQuery, idConnettore));
  808.                 n = stm.executeUpdate();
  809.                 DriverRegistroServiziDB_LIB.logDebug("CRUDConnettore type = " + type + " row affected =" + n);
  810.                 stm.close();
  811.                
  812.                 break;
  813.             }

  814.             // ritorno l id del connettore questo e' utile in caso di create
  815.             connettore.setId(idConnettore);
  816.             return idConnettore;

  817.         } catch (SQLException se) {
  818.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::CRUDConnettore] SQLException : " + se.getMessage(),se);
  819.         } catch (Exception se) {
  820.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::CRUDConnettore] Exception : " + se.getMessage(),se);
  821.         }finally {
  822.             JDBCUtilities.closeResources(stm);
  823.         }
  824.     }
  825.    

  826. }