DBOggettiInUsoUtils_rateLimiting.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.protocol.engine.utils;

  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 java.util.Map;
  27. import java.util.Set;

  28. import org.openspcoop2.core.commons.ErrorsHandlerCostant;
  29. import org.openspcoop2.core.constants.CostantiDB;
  30. import org.openspcoop2.core.controllo_traffico.IdPolicy;
  31. import org.openspcoop2.utils.UtilsException;
  32. import org.openspcoop2.utils.jdbc.JDBCUtilities;
  33. import org.openspcoop2.utils.sql.ISQLQueryObject;
  34. import org.openspcoop2.utils.sql.SQLObjectFactory;

  35. /**
  36.  * DBOggettiInUsoUtils_rateLimiting
  37.  *
  38.  * @author Andrea Poli (apoli@link.it)
  39.  * @author $Author$
  40.  * @version $Rev$, $Date$
  41.  *
  42.  */
  43. public class DBOggettiInUsoUtils_rateLimiting {

  44.     protected static boolean isRateLimitingPolicyInUso(Connection con, String tipoDB, IdPolicy idPolicy,
  45.             Map<ErrorsHandlerCostant, List<String>> whereIsInUso, boolean normalizeObjectIds) throws UtilsException {
  46.        
  47.         String nomeMetodo = "isRateLimitingPolicyInUso";
  48.        
  49.         PreparedStatement stmt = null;
  50.         ResultSet risultato = null;
  51.         PreparedStatement stmt2 = null;
  52.         ResultSet risultato2 = null;
  53.         String queryString;
  54.         try {
  55.             boolean isInUso = false;
  56.            
  57.            
  58.             List<String> ct_list = whereIsInUso.get(ErrorsHandlerCostant.CONTROLLO_TRAFFICO);
  59.             if (ct_list == null) {
  60.                 ct_list = new ArrayList<>();
  61.                 whereIsInUso.put(ErrorsHandlerCostant.CONTROLLO_TRAFFICO, ct_list);
  62.             }  
  63.            
  64.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  65.             sqlQueryObject.addFromTable(CostantiDB.CONTROLLO_TRAFFICO_ACTIVE_POLICY);
  66.             sqlQueryObject.addSelectField("active_policy_id");
  67.             sqlQueryObject.addSelectField("policy_alias");
  68.             sqlQueryObject.addSelectField("filtro_ruolo");
  69.             sqlQueryObject.addSelectField("filtro_porta");
  70.             sqlQueryObject.setANDLogicOperator(false); // OR
  71.             sqlQueryObject.addWhereCondition(CostantiDB.CONTROLLO_TRAFFICO_ACTIVE_POLICY+".policy_id = ?");
  72.             sqlQueryObject.addOrderBy("filtro_ruolo");
  73.             sqlQueryObject.addOrderBy("filtro_porta");
  74.             queryString = sqlQueryObject.createSQLQuery();
  75.             stmt = con.prepareStatement(queryString);
  76.             int index = 1;
  77.             stmt.setString(index++, idPolicy.getNome());
  78.             risultato = stmt.executeQuery();
  79.             while (risultato.next()) {
  80.                
  81.                 String alias = risultato.getString("policy_alias");
  82.                 if(alias== null || "".equals(alias)) {
  83.                     alias = risultato.getString("active_policy_id");
  84.                 }
  85.                
  86.                 String nomePorta = risultato.getString("filtro_porta");
  87.                 String filtro_ruolo = risultato.getString("filtro_ruolo");
  88.                 if(nomePorta!=null) {
  89.                     String tipoPorta = null;
  90.                     String labelPorta = null;
  91.                     if("delegata".equals(filtro_ruolo)) {
  92.                         try {
  93.                             ResultPorta resultPorta = DBOggettiInUsoUtils.formatPortaDelegata(nomePorta, tipoDB, con, normalizeObjectIds);
  94.                             if(resultPorta.mapping) {
  95.                                 labelPorta = "Fruizione di Servizio "+ resultPorta.label;
  96.                             }
  97.                         }catch(Exception e) {
  98.                             tipoPorta = "Outbound";
  99.                         }
  100.                     }
  101.                     else if("applicativa".equals(filtro_ruolo)) {
  102.                         try {
  103.                             ResultPorta resultPorta = DBOggettiInUsoUtils.formatPortaApplicativa(nomePorta, tipoDB, con, normalizeObjectIds);
  104.                             if(resultPorta.mapping) {
  105.                                 labelPorta = "Erogazione di Servizio "+ resultPorta.label;
  106.                             }
  107.                         }catch(Exception e) {
  108.                             tipoPorta = "Inbound";
  109.                         }
  110.                     }
  111.                     else {
  112.                         tipoPorta = filtro_ruolo;
  113.                     }
  114.                     if(labelPorta==null) {
  115.                         ct_list.add("Policy '"+alias+"' attiva nella porta '"+tipoPorta+"' '"+nomePorta+"' ");
  116.                     }
  117.                     else {
  118.                         ct_list.add("Policy '"+alias+"' attiva nella "+labelPorta);
  119.                     }
  120.                 }
  121.                 else {
  122.                     ct_list.add("Policy '"+alias+"'");
  123.                 }

  124.                 isInUso = true;
  125.             }
  126.             risultato.close();
  127.             stmt.close();
  128.            
  129.            
  130.             return isInUso;

  131.         } catch (Exception se) {
  132.             throw new UtilsException("[DBOggettiInUsoUtils::" + nomeMetodo + "] Exception: " + se.getMessage(),se);
  133.         } finally {
  134.             // Chiudo statement and resultset
  135.             JDBCUtilities.closeResources(risultato2, stmt2);
  136.             JDBCUtilities.closeResources(risultato, stmt);
  137.         }
  138.     }
  139.    

  140.     protected static String toString(IdPolicy idPolicy, Map<ErrorsHandlerCostant, List<String>> whereIsInUso, boolean prefix, String separator){
  141.        
  142.         StringBuilder bf = new StringBuilder();
  143.    
  144.         bf.append(idPolicy.getNome());
  145.        
  146.         Set<ErrorsHandlerCostant> keys = whereIsInUso.keySet();
  147.         String msg = "Policy '" +bf.toString() +"' non eliminabile perch&egrave; :"+separator;
  148.         if(prefix==false){
  149.             msg = "";
  150.         }
  151.         String separatorCategorie = "";
  152.         if(whereIsInUso.size()>1) {
  153.             separatorCategorie = separator;
  154.         }
  155.         for (ErrorsHandlerCostant key : keys) {
  156.             List<String> messages = whereIsInUso.get(key);

  157.             if ( messages!=null && messages.size() > 0) {
  158.                 msg += separatorCategorie;
  159.             }
  160.            
  161.             switch (key) {
  162.                            
  163.             case CONTROLLO_TRAFFICO:
  164.                 if ( messages!=null && messages.size() > 0 ) {
  165.                     msg += "utilizzato in: " + DBOggettiInUsoUtils.formatList(messages,separator) + separator;
  166.                 }
  167.                 break;
  168.                                
  169.             default:
  170.                 msg += "utilizzato in oggetto non codificato ("+key+")"+separator;
  171.                 break;
  172.             }

  173.         }// chiudo for

  174.         return msg;
  175.     }
  176.    
  177. }