GestoreRichieste.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.pdd.core;

  21. import java.util.ArrayList;
  22. import java.util.HashMap;
  23. import java.util.List;
  24. import java.util.Map;

  25. import org.openspcoop2.core.commons.CoreException;
  26. import org.openspcoop2.core.config.ServizioApplicativo;
  27. import org.openspcoop2.core.config.constants.CostantiConfigurazione;
  28. import org.openspcoop2.core.constants.TipoPdD;
  29. import org.openspcoop2.core.id.IDAccordo;
  30. import org.openspcoop2.core.id.IDConnettore;
  31. import org.openspcoop2.core.id.IDGenericProperties;
  32. import org.openspcoop2.core.id.IDPortaApplicativa;
  33. import org.openspcoop2.core.id.IDPortaDelegata;
  34. import org.openspcoop2.core.id.IDRuolo;
  35. import org.openspcoop2.core.id.IDScope;
  36. import org.openspcoop2.core.id.IDServizio;
  37. import org.openspcoop2.core.id.IDServizioApplicativo;
  38. import org.openspcoop2.core.id.IDSoggetto;
  39. import org.openspcoop2.core.mapping.MappingErogazionePortaApplicativa;
  40. import org.openspcoop2.core.mapping.MappingFruizionePortaDelegata;
  41. import org.openspcoop2.core.registry.driver.IDAccordoFactory;
  42. import org.openspcoop2.message.constants.ServiceBinding;
  43. import org.openspcoop2.message.soap.reader.OpenSPCoop2MessageSoapStreamReader;
  44. import org.openspcoop2.pdd.config.ConfigurazionePdD;
  45. import org.openspcoop2.pdd.logger.OpenSPCoop2Logger;
  46. import org.openspcoop2.protocol.sdk.state.RequestConfig;
  47. import org.openspcoop2.protocol.sdk.state.RequestFruitore;
  48. import org.openspcoop2.protocol.sdk.state.RequestInfo;
  49. import org.openspcoop2.protocol.sdk.state.RequestRateLimitingConfig;
  50. import org.openspcoop2.protocol.sdk.state.RequestThreadContext;
  51. import org.openspcoop2.utils.SemaphoreLock;
  52. import org.openspcoop2.utils.UtilsException;
  53. import org.openspcoop2.utils.cache.Cache;
  54. import org.openspcoop2.utils.cache.CacheAlgorithm;
  55. import org.openspcoop2.utils.cache.CacheResponse;
  56. import org.openspcoop2.utils.cache.CacheType;
  57. import org.openspcoop2.utils.cache.Constants;
  58. import org.openspcoop2.utils.certificate.CertificateInfo;
  59. import org.openspcoop2.utils.transport.http.HttpConstants;
  60. import org.slf4j.Logger;

  61. /**
  62.  * GestoreRichieste
  63.  *
  64.  * @author Andrea Poli (apoli@link.it)
  65.  * @author $Author$
  66.  * @version $Rev$, $Date$
  67.  */
  68. public class GestoreRichieste {

  69.     private GestoreRichieste() {}
  70.    
  71.     private static boolean useCache = true;
  72.     public static boolean isUseCache() {
  73.         return useCache;
  74.     }
  75.     public static void setUseCache(boolean useCache) {
  76.         GestoreRichieste.useCache = useCache;
  77.     }

  78.     /** Chiave della cache per il Gestore delle Richieste  */
  79.     public static final String GESTORE_RICHIESTE_PREFIX_CACHE_NAME = "gestoreRichieste-";
  80.     private static final String GESTORE_RICHIESTE_API_CACHE_NAME = GESTORE_RICHIESTE_PREFIX_CACHE_NAME+"API";
  81.     private static final String GESTORE_RICHIESTE_RATE_LIMITING_CACHE_NAME = GESTORE_RICHIESTE_PREFIX_CACHE_NAME+"RateLimiting";
  82.     private static final String GESTORE_RICHIESTE_FRUITORI_CACHE_NAME = GESTORE_RICHIESTE_PREFIX_CACHE_NAME+"Fruitori";
  83.    
  84.     private static Cache cacheApi = null;
  85.     private static Cache cacheRateLimiting = null;
  86.     private static Cache cacheFruitori = null;
  87.     private static Map<String, Cache> caches = null;
  88.     private static List<String> cacheKeys = null;
  89.     private static synchronized void initCacheKeys() {
  90.         if(cacheKeys==null) {
  91.             cacheKeys = new ArrayList<>();
  92.             cacheKeys.add(GESTORE_RICHIESTE_API_CACHE_NAME);
  93.             cacheKeys.add(GESTORE_RICHIESTE_RATE_LIMITING_CACHE_NAME);
  94.             cacheKeys.add(GESTORE_RICHIESTE_FRUITORI_CACHE_NAME);
  95.         }
  96.     }
  97.    
  98.     private static final org.openspcoop2.utils.Semaphore lockCache = new org.openspcoop2.utils.Semaphore(GESTORE_RICHIESTE_API_CACHE_NAME);
  99.     private static final org.openspcoop2.utils.Semaphore lockCache_rateLimiting = new org.openspcoop2.utils.Semaphore(GESTORE_RICHIESTE_RATE_LIMITING_CACHE_NAME);
  100.     private static final org.openspcoop2.utils.Semaphore lockCache_fruitori = new org.openspcoop2.utils.Semaphore(GESTORE_RICHIESTE_FRUITORI_CACHE_NAME);
  101.    
  102.     /** Logger log */
  103.     private static Logger logger = null;
  104.     private static Logger logConsole = OpenSPCoop2Logger.getLoggerOpenSPCoopConsole();
  105.    
  106.    
  107.     /* --------------- Cache --------------------*/
  108.     public static void resetCache() throws CoreException{
  109.         if(GestoreRichieste.cacheKeys!=null && !GestoreRichieste.cacheKeys.isEmpty()){
  110.             try{
  111.                 for (String cacheKey : GestoreRichieste.cacheKeys) {
  112.                     Cache cache = GestoreRichieste.caches.get(cacheKey);
  113.                     if(cache!=null) {
  114.                         cache.clear();
  115.                     }
  116.                 }
  117.             }catch(Exception e){
  118.                 throw new CoreException(e.getMessage(),e);
  119.             }
  120.         }
  121.     }
  122.     public static String printStatsCache(String separator) throws CoreException{
  123.         try{
  124.             if(GestoreRichieste.cacheKeys!=null && !GestoreRichieste.cacheKeys.isEmpty()){
  125.                 StringBuilder sb = new StringBuilder();
  126.                 for (String cacheKey : GestoreRichieste.cacheKeys) {
  127.                     Cache cache = GestoreRichieste.caches.get(cacheKey);
  128.                     if(cache!=null) {
  129.                         if(sb.length()>0) {
  130.                             sb.append(separator);
  131.                         }
  132.                         /** String funzione = cacheKey.substring(cacheKey.indexOf("-")+1, cacheKey.length());
  133.                         sb.append(funzione).append(separator); */
  134.                         sb.append(cache.printStats(separator));
  135.                     }
  136.                 }
  137.                 return sb.toString();
  138.             }
  139.             else{
  140.                 throw new CoreException(Constants.MSG_CACHE_NON_ABILITATA);
  141.             }
  142.         }catch(Exception e){
  143.             throw new CoreException("Visualizzazione Statistiche riguardante la cache dei dati di gestione delle richieste non riuscita: "+e.getMessage(),e);
  144.         }
  145.     }
  146.     public static void abilitaCache() throws CoreException{
  147.         if(GestoreRichieste.cacheKeys!=null)
  148.             throw new CoreException("Cache gia' abilitata");
  149.         else{
  150.             try{
  151.                 initCacheKeys();
  152.                 GestoreRichieste.cacheApi = new Cache(CacheType.JCS, GESTORE_RICHIESTE_API_CACHE_NAME); // lascio JCS come default abilitato via jmx
  153.                 GestoreRichieste.cacheRateLimiting = new Cache(CacheType.JCS, GESTORE_RICHIESTE_RATE_LIMITING_CACHE_NAME); // lascio JCS come default abilitato via jmx
  154.                 GestoreRichieste.cacheFruitori = new Cache(CacheType.JCS, GESTORE_RICHIESTE_FRUITORI_CACHE_NAME); // lascio JCS come default abilitato via jmx
  155.                
  156.                 GestoreRichieste.cacheApi.build();
  157.                 GestoreRichieste.cacheRateLimiting.build();
  158.                 GestoreRichieste.cacheFruitori.build();
  159.                
  160.                 GestoreRichieste.caches = new HashMap<>();
  161.                 GestoreRichieste.caches.put(GESTORE_RICHIESTE_API_CACHE_NAME, GestoreRichieste.cacheApi);
  162.                 GestoreRichieste.caches.put(GESTORE_RICHIESTE_RATE_LIMITING_CACHE_NAME, GestoreRichieste.cacheRateLimiting);
  163.                 GestoreRichieste.caches.put(GESTORE_RICHIESTE_FRUITORI_CACHE_NAME, GestoreRichieste.cacheFruitori);
  164.             }catch(Exception e){
  165.                 throw new CoreException(e.getMessage(),e);
  166.             }
  167.         }
  168.     }
  169.     public static void abilitaCache(Long dimensioneCache,Boolean algoritmoCacheLRU,Long itemIdleTime,Long itemLifeSecond) throws CoreException{
  170.         if(GestoreRichieste.cacheKeys!=null)
  171.             throw new CoreException("Cache giĆ  abilitata");
  172.         else{
  173.             try{
  174.                 int dimensioneCacheInt = -1;
  175.                 if(dimensioneCache!=null){
  176.                     dimensioneCacheInt = dimensioneCache.intValue();
  177.                 }
  178.                
  179.                 String algoritmoCache = null;
  180.                 if(algoritmoCacheLRU!=null){
  181.                     if(algoritmoCacheLRU)
  182.                          algoritmoCache = CostantiConfigurazione.CACHE_LRU.toString();
  183.                     else
  184.                          algoritmoCache = CostantiConfigurazione.CACHE_MRU.toString();
  185.                 }else{
  186.                     algoritmoCache = CostantiConfigurazione.CACHE_LRU.toString();
  187.                 }
  188.                
  189.                 long itemIdleTimeLong = -1;
  190.                 if(itemIdleTime!=null){
  191.                     itemIdleTimeLong = itemIdleTime;
  192.                 }
  193.                
  194.                 long itemLifeSecondLong = -1;
  195.                 if(itemLifeSecond!=null){
  196.                     itemLifeSecondLong = itemLifeSecond;
  197.                 }
  198.                
  199.                 GestoreRichieste.initCacheGestoreRichieste(CacheType.JCS, dimensioneCacheInt, algoritmoCache, itemIdleTimeLong, itemLifeSecondLong, null); // lascio JCS come default abilitato via jmx
  200.             }catch(Exception e){
  201.                 throw new CoreException(e.getMessage(),e);
  202.             }
  203.         }
  204.     }
  205.     public static void disabilitaCache() throws CoreException{
  206.         if(GestoreRichieste.cacheKeys==null)
  207.             throw new CoreException("Cache giĆ  disabilitata");
  208.         else{
  209.             disabilitaCacheEngine();
  210.         }
  211.     }
  212.     private static synchronized void disabilitaCacheEngine() throws CoreException{
  213.         if(GestoreRichieste.cacheKeys!=null) {
  214.             try{
  215.                 GestoreRichieste.cacheApi.clear();
  216.                 GestoreRichieste.cacheApi = null;
  217.                
  218.                 GestoreRichieste.cacheRateLimiting.clear();
  219.                 GestoreRichieste.cacheRateLimiting = null;
  220.                
  221.                 GestoreRichieste.cacheFruitori.clear();
  222.                 GestoreRichieste.cacheFruitori = null;
  223.                
  224.                 GestoreRichieste.caches.clear();
  225.                 GestoreRichieste.caches = null;
  226.                
  227.                 GestoreRichieste.cacheKeys.clear();
  228.                 GestoreRichieste.cacheKeys = null;
  229.             }catch(Exception e){
  230.                 throw new CoreException(e.getMessage(),e);
  231.             }
  232.         }
  233.     }
  234.     public static boolean isCacheAbilitata(){
  235.         return GestoreRichieste.cacheKeys != null;
  236.     }
  237.     public static String listKeysCache(String separator) throws CoreException{
  238.         if(GestoreRichieste.cacheKeys!=null && !GestoreRichieste.cacheKeys.isEmpty()){
  239.             StringBuilder sb = new StringBuilder();
  240.             for (String cacheKey : GestoreRichieste.cacheKeys) {
  241.                 Cache cache = GestoreRichieste.caches.get(cacheKey);
  242.                 if(cache!=null) {
  243.                     if(sb.length()>0) {
  244.                         sb.append(separator);
  245.                     }
  246.                     try{
  247.                         String nomeCache = cacheKey;
  248.                         if(nomeCache.startsWith(GestoreRichieste.GESTORE_RICHIESTE_PREFIX_CACHE_NAME)) {
  249.                             nomeCache = nomeCache.substring(GestoreRichieste.GESTORE_RICHIESTE_PREFIX_CACHE_NAME.length());
  250.                         }
  251.                         sb.append( cache.printKeys(separator).replaceFirst(Constants.MSG_CACHE,Constants.MSG_CACHE_PREFIX+nomeCache ) );
  252.                     }catch(Exception e){
  253.                         throw new CoreException(e.getMessage(),e);
  254.                     }
  255.                 }
  256.             }
  257.             return sb.toString();
  258.         }else{
  259.             throw new CoreException(Constants.MSG_CACHE_NON_ABILITATA);
  260.         }
  261.     }
  262.     /**
  263.      * non viene gestito correttamente la chiave rispetto alla cache, potrebbe essere presente la solita chiave
  264.      */
  265.     @Deprecated
  266.     public static List<String> listKeysCache() throws CoreException{
  267.         if(GestoreRichieste.cacheKeys!=null && !GestoreRichieste.cacheKeys.isEmpty()){
  268.             List<String> keys = new ArrayList<>();
  269.             for (String cacheKey : GestoreRichieste.cacheKeys) {
  270.                 Cache cache = GestoreRichieste.caches.get(cacheKey);
  271.                 if(cache!=null) {
  272.                     try{
  273.                         List<String> k = cache.keys();
  274.                         if(k!=null && !k.isEmpty()) {
  275.                             keys.addAll(k);
  276.                         }
  277.                     }catch(Exception e){
  278.                         throw new CoreException(e.getMessage(),e);
  279.                     }
  280.                 }
  281.             }
  282.             return keys;
  283.         }else{
  284.             throw new CoreException(Constants.MSG_CACHE_NON_ABILITATA);
  285.         }
  286.     }
  287.     public static String getObjectCache(String key) throws CoreException{
  288.         if(GestoreRichieste.cacheKeys!=null && !GestoreRichieste.cacheKeys.isEmpty()){
  289.             try{
  290.                 Object o = null;
  291.                 for (String cacheKey : GestoreRichieste.cacheKeys) {
  292.                    
  293.                     // trucco per avere il valore di una precisa cache              
  294.                     if(key.startsWith(Constants.MSG_CACHE_PREFIX)) {
  295.                        
  296.                         String nomeCache = cacheKey;
  297.                         if(nomeCache.startsWith(GestoreRichieste.GESTORE_RICHIESTE_PREFIX_CACHE_NAME)) {
  298.                             nomeCache = nomeCache.substring(GestoreRichieste.GESTORE_RICHIESTE_PREFIX_CACHE_NAME.length());
  299.                         }
  300.                         nomeCache = Constants.MSG_CACHE_PREFIX+nomeCache+" ";
  301.                        
  302.                         if(!key.startsWith(nomeCache)) {
  303.                             continue; // cerco in altra cache
  304.                         }
  305.                         else {
  306.                             key = key.substring(nomeCache.length());
  307.                         }
  308.                     }
  309.                    
  310.                     Cache cache = GestoreRichieste.caches.get(cacheKey);
  311.                     if(cache!=null) {
  312.                         o = cache.get(key);
  313.                         if(o!=null) {
  314.                             break;
  315.                         }
  316.                     }
  317.                 }
  318.                 if(o!=null){
  319.                     return o.toString();
  320.                 }else{
  321.                     return "oggetto con chiave ["+key+"] non presente";
  322.                 }
  323.             }catch(Exception e){
  324.                 throw new CoreException(e.getMessage(),e);
  325.             }
  326.         }else{
  327.             throw new CoreException(Constants.MSG_CACHE_NON_ABILITATA);
  328.         }
  329.     }
  330.     public static Object getRawObjectCache(String key) throws CoreException{
  331.         if(GestoreRichieste.cacheKeys!=null && !GestoreRichieste.cacheKeys.isEmpty()){
  332.             try{
  333.                 Object o = null;
  334.                 for (String cacheKey : GestoreRichieste.cacheKeys) {
  335.                    
  336.                     // trucco per avere il valore di una precisa cache      
  337.                     if(key.startsWith(Constants.MSG_CACHE_PREFIX)) {
  338.                        
  339.                         String nomeCache = cacheKey;
  340.                         if(nomeCache.startsWith(GestoreRichieste.GESTORE_RICHIESTE_PREFIX_CACHE_NAME)) {
  341.                             nomeCache = nomeCache.substring(GestoreRichieste.GESTORE_RICHIESTE_PREFIX_CACHE_NAME.length());
  342.                         }
  343.                         nomeCache = Constants.MSG_CACHE_PREFIX+nomeCache+" ";
  344.                        
  345.                         if(!key.startsWith(nomeCache)) {
  346.                             continue; // cerco in altra cache
  347.                         }
  348.                         else {
  349.                             key = key.substring(nomeCache.length());
  350.                         }
  351.                     }
  352.                    
  353.                     Cache cache = GestoreRichieste.caches.get(cacheKey);
  354.                     if(cache!=null) {
  355.                         o = cache.get(key);
  356.                         if(o!=null) {
  357.                             break;
  358.                         }
  359.                     }
  360.                 }
  361.                 return getRawObjectCacheEngine(o);
  362.             }catch(Exception e){
  363.                 throw new CoreException(e.getMessage(),e);
  364.             }
  365.         }else{
  366.             throw new CoreException(Constants.MSG_CACHE_NON_ABILITATA);
  367.         }
  368.     }
  369.     public static Object getRawObjectCacheEngine(Object o) throws CoreException{
  370.         if(o!=null){
  371.             if(o instanceof CacheResponse) {
  372.                 CacheResponse cR = (CacheResponse) o;
  373.                 if(cR.getObject()!=null) {
  374.                     o = cR.getObject();
  375.                 }
  376.                 else if(cR.getException()!=null) {
  377.                     o = cR.getException();
  378.                 }
  379.             }
  380.             return o;
  381.         }else{
  382.             return null;
  383.         }
  384.     }
  385.     public static void removeObjectCache(String key) throws CoreException{
  386.         if(GestoreRichieste.cacheKeys!=null && !GestoreRichieste.cacheKeys.isEmpty()){
  387.             try{
  388.                 for (String cacheKey : GestoreRichieste.cacheKeys) {
  389.                    
  390.                     // trucco per avere il valore di una precisa cache      
  391.                     if(key.startsWith(Constants.MSG_CACHE_PREFIX)) {
  392.                        
  393.                         String nomeCache = cacheKey;
  394.                         if(nomeCache.startsWith(GestoreRichieste.GESTORE_RICHIESTE_PREFIX_CACHE_NAME)) {
  395.                             nomeCache = nomeCache.substring(GestoreRichieste.GESTORE_RICHIESTE_PREFIX_CACHE_NAME.length());
  396.                         }
  397.                         nomeCache = Constants.MSG_CACHE_PREFIX+nomeCache+" ";
  398.                        
  399.                         if(!key.startsWith(nomeCache)) {
  400.                             continue; // cerco in altra cache
  401.                         }
  402.                         else {
  403.                             key = key.substring(nomeCache.length());
  404.                         }
  405.                     }
  406.                    
  407.                     Cache cache = GestoreRichieste.caches.get(cacheKey);
  408.                     if(cache!=null) {
  409.                         cache.remove(key);
  410.                     }
  411.                 }
  412.             }catch(Exception e){
  413.                 throw new CoreException(e.getMessage(),e);
  414.             }
  415.         }else{
  416.             throw new CoreException(Constants.MSG_CACHE_NON_ABILITATA);
  417.         }
  418.     }
  419.    


  420.     /*----------------- INIZIALIZZAZIONE --------------------*/
  421.     public static void initialize(Logger log) throws Exception{
  422.         GestoreRichieste.initialize(null, false, -1,null,-1l,-1l, log);
  423.     }
  424.     public static void initialize(CacheType cacheType, int dimensioneCache,String algoritmoCache,
  425.             long idleTime, long itemLifeSecond, Logger log) throws Exception{
  426.         GestoreRichieste.initialize(cacheType, true, dimensioneCache,algoritmoCache,idleTime,itemLifeSecond, log);
  427.     }

  428.     private static void initialize(CacheType cacheType, boolean cacheAbilitata,int dimensioneCache,String algoritmoCache,
  429.             long idleTime, long itemLifeSecond, Logger log) throws Exception{

  430.         // Inizializzo log
  431.         GestoreRichieste.logger = log;
  432.        
  433.         // Inizializzazione Cache
  434.         if(cacheAbilitata){
  435.             GestoreRichieste.initCacheGestoreRichieste(cacheType, dimensioneCache, algoritmoCache, idleTime, itemLifeSecond, log);
  436.         }

  437.     }


  438.     public static void initCacheGestoreRichieste(CacheType cacheType, int dimensioneCache,String algoritmoCache,
  439.             long idleTime, long itemLifeSecond, Logger log) throws UtilsException, CoreException {
  440.        
  441.         if(log!=null)
  442.             log.info("Inizializzazione cache GestoreRichieste");

  443.         initCacheKeys();
  444.        
  445.         GestoreRichieste.cacheApi = new Cache(cacheType, GESTORE_RICHIESTE_API_CACHE_NAME);
  446.         GestoreRichieste.cacheRateLimiting = new Cache(cacheType, GESTORE_RICHIESTE_RATE_LIMITING_CACHE_NAME);
  447.         GestoreRichieste.cacheFruitori = new Cache(cacheType, GESTORE_RICHIESTE_FRUITORI_CACHE_NAME);
  448.        
  449.         GestoreRichieste.caches = new HashMap<>();
  450.         GestoreRichieste.caches.put(GESTORE_RICHIESTE_API_CACHE_NAME, GestoreRichieste.cacheApi);
  451.         GestoreRichieste.caches.put(GESTORE_RICHIESTE_RATE_LIMITING_CACHE_NAME, GestoreRichieste.cacheRateLimiting);
  452.         GestoreRichieste.caches.put(GESTORE_RICHIESTE_FRUITORI_CACHE_NAME, GestoreRichieste.cacheFruitori);
  453.        
  454.         if( (dimensioneCache>0) ||
  455.                 (algoritmoCache != null) ){

  456.             if( dimensioneCache>0 ){
  457.                 try{
  458.                     // divido per il numero di cache interne
  459.                     int dimensioneCacheInterna = dimensioneCache / GestoreRichieste.cacheKeys.size();
  460.                    
  461.                     String msg = "Dimensione della cache (GestoreRichieste) impostata al valore: "+dimensioneCache+" ("+GestoreRichieste.cacheKeys.size()+" cache interne di "+dimensioneCacheInterna+" elementi ciascuna)";
  462.                     if(log!=null)
  463.                         log.info(msg);
  464.                     GestoreRichieste.logConsole.info(msg);
  465.                     for (String cacheKey : GestoreRichieste.cacheKeys) {
  466.                         Cache cache = GestoreRichieste.caches.get(cacheKey);
  467.                         cache.setCacheSize(dimensioneCacheInterna);
  468.                     }
  469.                 }catch(Exception error){
  470.                     throw new CoreException("Parametro errato per la dimensione della cache (Gestore Messaggi): "+error.getMessage(),error);
  471.                 }
  472.             }
  473.             if(algoritmoCache != null ){
  474.                 String msg = "Algoritmo di cache (GestoreRichieste) impostato al valore: "+algoritmoCache;
  475.                 if(log!=null)
  476.                     log.info(msg);
  477.                 GestoreRichieste.logConsole.info(msg);
  478.                 CacheAlgorithm ca = null;
  479.                 if(CostantiConfigurazione.CACHE_MRU.toString().equalsIgnoreCase(algoritmoCache)) {
  480.                     ca = CacheAlgorithm.MRU;
  481.                 }else {
  482.                     ca = CacheAlgorithm.LRU;
  483.                 }
  484.                 for (String cacheKey : GestoreRichieste.cacheKeys) {
  485.                     Cache cache = GestoreRichieste.caches.get(cacheKey);
  486.                     cache.setCacheAlgoritm(ca);
  487.                 }
  488.             }

  489.         }

  490.         if( idleTime > 0  ){
  491.             try{
  492.                 String msg = "Attributo 'IdleTime' (GestoreRichieste) impostato al valore: "+idleTime;
  493.                 if(log!=null)
  494.                     log.info(msg);
  495.                 GestoreRichieste.logConsole.info(msg);
  496.                 for (String cacheKey : GestoreRichieste.cacheKeys) {
  497.                     Cache cache = GestoreRichieste.caches.get(cacheKey);
  498.                     cache.setItemIdleTime(idleTime);
  499.                 }
  500.             }catch(Exception error){
  501.                 throw new CoreException("Parametro errato per l'attributo 'IdleTime' (Gestore Messaggi): "+error.getMessage(),error);
  502.             }
  503.         }

  504.         try{
  505.             String msg = "Attributo 'MaxLifeSecond' (GestoreRichieste) impostato al valore: "+itemLifeSecond;
  506.             if(log!=null)
  507.                 log.info(msg);
  508.             GestoreRichieste.logConsole.info(msg);
  509.             for (String cacheKey : GestoreRichieste.cacheKeys) {
  510.                 Cache cache = GestoreRichieste.caches.get(cacheKey);
  511.                 cache.setItemLifeTime(itemLifeSecond);
  512.             }
  513.         }catch(Exception error){
  514.             throw new CoreException("Parametro errato per l'attributo 'MaxLifeSecond' (Gestore Messaggi): "+error.getMessage(),error);
  515.         }

  516.         GestoreRichieste.cacheApi.build();
  517.         GestoreRichieste.cacheRateLimiting.build();
  518.         GestoreRichieste.cacheFruitori.build();
  519.     }
  520.    
  521.     @SuppressWarnings("deprecation")
  522.     @Deprecated
  523.     public static void disableSyncronizedGet() throws UtilsException {
  524.         if(GestoreRichieste.cacheKeys!=null && !GestoreRichieste.cacheKeys.isEmpty()){
  525.             try{
  526.                 for (String cacheKey : GestoreRichieste.cacheKeys) {
  527.                     Cache cache = GestoreRichieste.caches.get(cacheKey);
  528.                     if(cache!=null) {
  529.                         cache.disableSyncronizedGet();
  530.                     }
  531.                 }
  532.             }catch(Exception e){
  533.                 throw new UtilsException(e.getMessage(),e);
  534.             }
  535.         }else{
  536.             throw new UtilsException("Cache disabled");
  537.         }
  538.     }
  539.     @SuppressWarnings("deprecation")
  540.     @Deprecated
  541.     public static boolean isDisableSyncronizedGet() throws UtilsException {
  542.         if(GestoreRichieste.cacheKeys!=null && !GestoreRichieste.cacheKeys.isEmpty()){
  543.             try{
  544.                 boolean isDisabled = true;
  545.                 for (String cacheKey : GestoreRichieste.cacheKeys) {
  546.                     Cache cache = GestoreRichieste.caches.get(cacheKey);
  547.                     if(cache!=null &&
  548.                         (!cache.isDisableSyncronizedGet())
  549.                         ) {
  550.                         isDisabled = false;
  551.                         break;
  552.                     }
  553.                 }
  554.                 return isDisabled;
  555.             }catch(Exception e){
  556.                 throw new UtilsException(e.getMessage(),e);
  557.             }
  558.         }else{
  559.             throw new UtilsException("Cache disabled");
  560.         }
  561.     }
  562.    

  563.    
  564.    
  565.    
  566.     /*----------------- CLEANER - RATELIMITING --------------------*/
  567.    
  568.     public static void removeRateLimitingConfigGlobale() throws UtilsException {
  569.         if(GestoreRichieste.cacheRateLimiting!=null) {
  570.             GestoreRichieste.cacheRateLimiting.clear();
  571.         }
  572.     }
  573.    
  574.     public static void removeRateLimitingConfig(TipoPdD tipoPdD, String nomePorta) throws UtilsException, CoreException {
  575.        
  576.         if(GestoreRichieste.cacheRateLimiting!=null) {
  577.             List<String> keyForClean = new ArrayList<>();
  578.             List<String> keys = GestoreRichieste.cacheRateLimiting.keys();
  579.             if(keys!=null && !keys.isEmpty()) {
  580.                 for (String key : keys) {
  581.                     if(key!=null) {
  582.                         Object o =  getRawObjectCacheEngine(GestoreRichieste.cacheRateLimiting.get(key));
  583.                         if(o instanceof RequestRateLimitingConfig) {
  584.                             RequestRateLimitingConfig rl = (RequestRateLimitingConfig) o;
  585.                             if(tipoPdD.equals(rl.getTipoPdD()) && nomePorta.equals(rl.getNomePorta())) {
  586.                                 keyForClean.add(key);
  587.                             }
  588.                         }
  589.                     }
  590.                 }
  591.             }
  592.             if(keyForClean!=null && !keyForClean.isEmpty()) {
  593.                 for (String key : keyForClean) {
  594.                     GestoreRichieste.cacheRateLimiting.remove(key);
  595.                 }
  596.             }
  597.         }
  598.        
  599.     }

  600.    
  601.    
  602.     /*----------------- CLEANER --------------------*/
  603.    
  604.     public static void removeApi(IDAccordo idAccordo) throws UtilsException, CoreException {
  605.        
  606.         IDAccordoFactory idAccordoFactory = IDAccordoFactory.getInstance();
  607.        
  608.         // RequestConfig
  609.         {
  610.             List<String> keyForClean = new ArrayList<>();
  611.             List<String> keys = GestoreRichieste.cacheApi.keys();
  612.             if(keys!=null && !keys.isEmpty()) {
  613.                 for (String key : keys) {
  614.                     if(key!=null) {
  615.                         Object o =  getRawObjectCacheEngine(GestoreRichieste.cacheApi.get(key));
  616.                         if(o instanceof RequestConfig) {
  617.                             RequestConfig rc = (RequestConfig) o;
  618.                             IDAccordo idCheck = null;
  619.                             try {
  620.                                 idCheck = idAccordoFactory.getIDAccordoFromAccordo(rc.getAspc());
  621.                             }catch(Exception e) {
  622.                                 throw new CoreException(e.getMessage(),e);
  623.                             }
  624.                             if(rc.getAspc()!=null && idAccordo.equals(idCheck)) {
  625.                                 keyForClean.add(key);
  626.                             }
  627.                         }
  628.                     }
  629.                 }
  630.             }
  631.             if(keyForClean!=null && !keyForClean.isEmpty()) {
  632.                 for (String key : keyForClean) {
  633.                     GestoreRichieste.cacheApi.remove(key);
  634.                 }
  635.             }
  636.         }
  637.        
  638.     }
  639.     public static void removeErogazione(IDServizio idServizio) throws UtilsException, CoreException {
  640.         removeErogazioneRequestConfig(idServizio);
  641.         removeErogazioneRequestRateLimitingConfig(idServizio);
  642.     }
  643.     private static void removeErogazioneRequestConfig(IDServizio idServizio) throws UtilsException, CoreException {
  644.         // RequestConfig
  645.         List<String> keyForClean = new ArrayList<>();
  646.         List<String> keys = GestoreRichieste.cacheApi.keys();
  647.         if(keys!=null && !keys.isEmpty()) {
  648.             for (String key : keys) {
  649.                 if(key!=null) {
  650.                     Object o =  getRawObjectCacheEngine(GestoreRichieste.cacheApi.get(key));
  651.                     if(o instanceof RequestConfig) {
  652.                         RequestConfig rc = (RequestConfig) o;
  653.                         if(rc.getIdServizio()!=null && idServizio.equals(rc.getIdServizio(),false)) {
  654.                             keyForClean.add(key);
  655.                         }
  656.                     }
  657.                 }
  658.             }
  659.         }
  660.         if(keyForClean!=null && !keyForClean.isEmpty()) {
  661.             for (String key : keyForClean) {
  662.                 GestoreRichieste.cacheApi.remove(key);
  663.             }
  664.         }
  665.     }
  666.     private static void removeErogazioneRequestRateLimitingConfig(IDServizio idServizio) throws UtilsException, CoreException {
  667.         // RequestRateLimitingConfig
  668.         List<String> keyForClean = new ArrayList<>();
  669.         List<String> keys = GestoreRichieste.cacheRateLimiting.keys();
  670.         if(keys!=null && !keys.isEmpty()) {
  671.             for (String key : keys) {
  672.                 if(key!=null) {
  673.                     Object o =  getRawObjectCacheEngine(GestoreRichieste.cacheRateLimiting.get(key));
  674.                     if(o instanceof RequestRateLimitingConfig) {
  675.                         RequestRateLimitingConfig rl = (RequestRateLimitingConfig) o;
  676.                         if(rl.getIdServizio()!=null && idServizio.equals(rl.getIdServizio(),false)) {
  677.                             keyForClean.add(key);
  678.                         }
  679.                     }
  680.                 }
  681.             }
  682.         }
  683.         if(keyForClean!=null && !keyForClean.isEmpty()) {
  684.             for (String key : keyForClean) {
  685.                 GestoreRichieste.cacheRateLimiting.remove(key);
  686.             }
  687.         }
  688.     }
  689.    
  690.     public static void removeFruizione(IDSoggetto fruitore, IDServizio idServizio) throws UtilsException, CoreException {
  691.         removeFruizioneRequestConfig(fruitore, idServizio);
  692.         removeFruizioneRequestRateLimitingConfig(fruitore, idServizio);
  693.         removeFruizioneRequestFruitore(fruitore, idServizio);
  694.     }
  695.     private static void removeFruizioneRequestConfig(IDSoggetto fruitore, IDServizio idServizio) throws UtilsException, CoreException {
  696.         // RequestConfig
  697.         List<String> keyForClean = new ArrayList<>();
  698.         List<String> keys = GestoreRichieste.cacheApi.keys();
  699.         if(keys!=null && !keys.isEmpty()) {
  700.             for (String key : keys) {
  701.                 if(key!=null) {
  702.                     Object o =  getRawObjectCacheEngine(GestoreRichieste.cacheApi.get(key));
  703.                     if(o instanceof RequestConfig) {
  704.                         RequestConfig rc = (RequestConfig) o;
  705.                         if(rc.getIdServizio()!=null && idServizio.equals(rc.getIdServizio(),false) &&
  706.                                 rc.getIdFruitore()!=null && fruitore.equals(rc.getIdFruitore())) {
  707.                             keyForClean.add(key);
  708.                         }
  709.                     }
  710.                 }
  711.             }
  712.         }
  713.         if(keyForClean!=null && !keyForClean.isEmpty()) {
  714.             for (String key : keyForClean) {
  715.                 GestoreRichieste.cacheApi.remove(key);
  716.             }
  717.         }
  718.     }
  719.     private static void removeFruizioneRequestRateLimitingConfig(IDSoggetto fruitore, IDServizio idServizio) throws UtilsException, CoreException {
  720.         // RequestRateLimitingConfig
  721.         List<String> keyForClean = new ArrayList<>();
  722.         List<String> keys = GestoreRichieste.cacheRateLimiting.keys();
  723.         if(keys!=null && !keys.isEmpty()) {
  724.             for (String key : keys) {
  725.                 if(key!=null) {
  726.                     Object o =  getRawObjectCacheEngine(GestoreRichieste.cacheRateLimiting.get(key));
  727.                     if(o instanceof RequestRateLimitingConfig) {
  728.                         RequestRateLimitingConfig rl = (RequestRateLimitingConfig) o;
  729.                         if(rl.getIdServizio()!=null && idServizio.equals(rl.getIdServizio(),false) &&
  730.                                 rl.getIdFruitore()!=null && fruitore.equals(rl.getIdFruitore())) {
  731.                             keyForClean.add(key);
  732.                         }
  733.                     }
  734.                 }
  735.             }
  736.         }
  737.         if(keyForClean!=null && !keyForClean.isEmpty()) {
  738.             for (String key : keyForClean) {
  739.                 GestoreRichieste.cacheRateLimiting.remove(key);
  740.             }
  741.         }
  742.     }
  743.     private static void removeFruizioneRequestFruitore(IDSoggetto fruitore, IDServizio idServizio) throws UtilsException, CoreException {
  744.         // RequestFruitore
  745.         List<String> keyForClean = new ArrayList<>();
  746.         List<String> keys = GestoreRichieste.cacheFruitori.keys();
  747.         if(keys!=null && !keys.isEmpty()) {
  748.             for (String key : keys) {
  749.                 if(key!=null) {
  750.                     Object o =  getRawObjectCacheEngine(GestoreRichieste.cacheFruitori.get(key));
  751.                     if(o instanceof RequestFruitore) {
  752.                         RequestFruitore rf = (RequestFruitore) o;
  753.                         if(rf.getIdSoggettoFruitore()!=null && fruitore.equals(rf.getIdSoggettoFruitore())) {
  754.                             keyForClean.add(key);
  755.                         }
  756.                     }
  757.                 }
  758.             }
  759.         }
  760.         if(keyForClean!=null && !keyForClean.isEmpty()) {
  761.             for (String key : keyForClean) {
  762.                 GestoreRichieste.cacheFruitori.remove(key);
  763.             }
  764.         }
  765.     }
  766.    
  767.     public static void removePortaApplicativa(IDPortaApplicativa idPA) throws UtilsException, CoreException {
  768.         removePortaApplicativaRequestConfig(idPA);
  769.         removePortaApplicativaRequestRateLimitingConfig(idPA) ;
  770.     }
  771.     private static void removePortaApplicativaRequestConfig(IDPortaApplicativa idPA) throws UtilsException, CoreException {
  772.         // RequestConfig
  773.         List<String> keyForClean = new ArrayList<>();
  774.         List<String> keys = GestoreRichieste.cacheApi.keys();
  775.         if(keys!=null && !keys.isEmpty()) {
  776.             for (String key : keys) {
  777.                 if(key!=null) {
  778.                     Object o =  getRawObjectCacheEngine(GestoreRichieste.cacheApi.get(key));
  779.                     if(o instanceof RequestConfig) {
  780.                         RequestConfig rc = (RequestConfig) o;
  781.                         if(rc.getIdPortaApplicativaDefault()!=null && idPA.equals(rc.getIdPortaApplicativaDefault())) {
  782.                             keyForClean.add(key);
  783.                         }
  784.                         else if(rc.getIdPortaApplicativa()!=null && idPA.equals(rc.getIdPortaApplicativa())) {
  785.                             keyForClean.add(key);
  786.                         }
  787.                         else if(rc.getPortaApplicativaDefault()!=null && rc.getPortaApplicativaDefault().getNome()!=null && idPA!=null &&
  788.                                 rc.getPortaApplicativaDefault().getNome().equals(idPA.getNome())) {
  789.                             keyForClean.add(key);
  790.                         }
  791.                         else if(rc.getPortaApplicativa()!=null && rc.getPortaApplicativa().getNome()!=null && idPA!=null &&
  792.                                 rc.getPortaApplicativa().getNome().equals(idPA.getNome())) {
  793.                             keyForClean.add(key);
  794.                         }
  795.                         else {
  796.                            
  797.                             boolean find = false;
  798.                             if(rc.getListPorteApplicativeByFiltroRicerca()!=null && !rc.getListPorteApplicativeByFiltroRicerca().isEmpty()) {
  799.                                 for (String keyID : rc.getListPorteApplicativeByFiltroRicerca().keySet()) {
  800.                                     List<IDPortaApplicativa> ids =  rc.getListPorteApplicativeByFiltroRicerca().get(keyID);
  801.                                     if(ids!=null && ids.contains(idPA)) {
  802.                                         find = true;
  803.                                         break;
  804.                                     }
  805.                                 }
  806.                             }
  807.                             if(!find &&
  808.                                 rc.getListMappingErogazionePortaApplicativa()!=null && !rc.getListMappingErogazionePortaApplicativa().isEmpty()) {
  809.                                 for (MappingErogazionePortaApplicativa mapping : rc.getListMappingErogazionePortaApplicativa()) {
  810.                                     if(mapping!=null && mapping.getIdPortaApplicativa()!=null && mapping.getIdPortaApplicativa().equals(idPA)) {
  811.                                         find = true;
  812.                                         break;
  813.                                     }
  814.                                 }
  815.                             }
  816.                             if(find) {
  817.                                 keyForClean.add(key);
  818.                             }
  819.                            
  820.                         }
  821.                     }
  822.                 }
  823.             }
  824.         }
  825.         if(keyForClean!=null && !keyForClean.isEmpty()) {
  826.             for (String key : keyForClean) {
  827.                 GestoreRichieste.cacheApi.remove(key);
  828.             }
  829.         }
  830.     }
  831.     private static void removePortaApplicativaRequestRateLimitingConfig(IDPortaApplicativa idPA) throws UtilsException, CoreException {
  832.         // RequestRateLimitingConfig
  833.         List<String> keyForClean = new ArrayList<>();
  834.         List<String> keys = GestoreRichieste.cacheRateLimiting.keys();
  835.         if(keys!=null && !keys.isEmpty()) {
  836.             for (String key : keys) {
  837.                 if(key!=null) {
  838.                     Object o =  getRawObjectCacheEngine(GestoreRichieste.cacheRateLimiting.get(key));
  839.                     if(o instanceof RequestRateLimitingConfig) {
  840.                         RequestRateLimitingConfig rl = (RequestRateLimitingConfig) o;
  841.                         if(TipoPdD.APPLICATIVA.equals(rl.getTipoPdD()) && idPA.getNome().equals(rl.getNomePorta())) {
  842.                             keyForClean.add(key);
  843.                         }
  844.                     }
  845.                 }
  846.             }
  847.         }
  848.         if(keyForClean!=null && !keyForClean.isEmpty()) {
  849.             for (String key : keyForClean) {
  850.                 GestoreRichieste.cacheRateLimiting.remove(key);
  851.             }
  852.         }
  853.     }
  854.    
  855.     public static void removeConnettore(IDConnettore idConnettore)throws UtilsException, CoreException {
  856.        
  857.         // RequestConfig
  858.         List<String> keyForClean = new ArrayList<>();
  859.         List<String> keys = GestoreRichieste.cacheApi.keys();
  860.         if(keys!=null && !keys.isEmpty()) {
  861.             for (String key : keys) {
  862.                 if(key!=null) {
  863.                     Object o =  getRawObjectCacheEngine(GestoreRichieste.cacheApi.get(key));
  864.                     if(o instanceof RequestConfig) {
  865.                         RequestConfig rc = (RequestConfig) o;
  866.                         if(rc.getIdServizio()!=null && rc.getIdServizio().getSoggettoErogatore()!=null && idConnettore.getIdSoggettoProprietario().equals(rc.getIdServizio().getSoggettoErogatore())) {
  867.                             ServizioApplicativo sa = rc.getServizioApplicativoErogatore(idConnettore.getNome());
  868.                             if(sa!=null) {
  869.                                 keyForClean.add(key);
  870.                             }
  871.                         }
  872.                     }
  873.                 }
  874.             }
  875.         }
  876.         if(keyForClean!=null && !keyForClean.isEmpty()) {
  877.             for (String key : keyForClean) {
  878.                 GestoreRichieste.cacheApi.remove(key);
  879.             }
  880.         }
  881.        
  882.     }
  883.    
  884.     public static void removePortaDelegata(IDPortaDelegata idPD) throws UtilsException, CoreException {
  885.         removePortaDelegataRequestConfig(idPD);
  886.         removePortaDelegataRequestRateLimitingConfig(idPD);
  887.     }
  888.     private static void removePortaDelegataRequestConfig(IDPortaDelegata idPD) throws UtilsException, CoreException {
  889.         // RequestConfig
  890.         List<String> keyForClean = new ArrayList<>();
  891.         List<String> keys = GestoreRichieste.cacheApi.keys();
  892.         if(keys!=null && !keys.isEmpty()) {
  893.             for (String key : keys) {
  894.                 if(key!=null) {
  895.                     Object o =  getRawObjectCacheEngine(GestoreRichieste.cacheApi.get(key));
  896.                     if(o instanceof RequestConfig) {
  897.                         RequestConfig rc = (RequestConfig) o;
  898.                         if(rc.getIdPortaDelegataDefault()!=null && idPD.equals(rc.getIdPortaDelegataDefault())) {
  899.                             keyForClean.add(key);
  900.                         }
  901.                         else if(rc.getIdPortaDelegata()!=null && idPD.equals(rc.getIdPortaDelegata())) {
  902.                             keyForClean.add(key);
  903.                         }
  904.                         else if(rc.getPortaDelegataDefault()!=null && rc.getPortaDelegataDefault().getNome()!=null && idPD!=null &&
  905.                                 rc.getPortaDelegataDefault().getNome().equals(idPD.getNome())) {
  906.                             keyForClean.add(key);
  907.                         }
  908.                         else if(rc.getPortaDelegata()!=null && rc.getPortaDelegata().getNome()!=null && idPD!=null &&
  909.                                 rc.getPortaDelegata().getNome().equals(idPD.getNome())) {
  910.                             keyForClean.add(key);
  911.                         }
  912.                         else {
  913.                            
  914.                             boolean find = false;
  915.                             if(rc.getListPorteDelegateByFiltroRicerca()!=null && !rc.getListPorteDelegateByFiltroRicerca().isEmpty()) {
  916.                                 for (String keyID : rc.getListPorteDelegateByFiltroRicerca().keySet()) {
  917.                                     List<IDPortaDelegata> ids =  rc.getListPorteDelegateByFiltroRicerca().get(keyID);
  918.                                     if(ids!=null && ids.contains(idPD)) {
  919.                                         find = true;
  920.                                         break;
  921.                                     }
  922.                                 }
  923.                             }
  924.                             if(!find &&
  925.                                 rc.getListMappingFruizionePortaDelegata()!=null && !rc.getListMappingFruizionePortaDelegata().isEmpty()) {
  926.                                 for (MappingFruizionePortaDelegata mapping : rc.getListMappingFruizionePortaDelegata()) {
  927.                                     if(mapping!=null && mapping.getIdPortaDelegata()!=null && mapping.getIdPortaDelegata().equals(idPD)) {
  928.                                         find = true;
  929.                                         break;
  930.                                     }
  931.                                 }
  932.                             }
  933.                             if(find) {
  934.                                 keyForClean.add(key);
  935.                             }
  936.                            
  937.                         }
  938.                     }
  939.                 }
  940.             }
  941.         }
  942.         if(keyForClean!=null && !keyForClean.isEmpty()) {
  943.             for (String key : keyForClean) {
  944.                 GestoreRichieste.cacheApi.remove(key);
  945.             }
  946.         }
  947.     }
  948.     private static void removePortaDelegataRequestRateLimitingConfig(IDPortaDelegata idPD) throws UtilsException, CoreException {  
  949.         // RequestRateLimitingConfig
  950.         List<String> keyForClean = new ArrayList<>();
  951.         List<String> keys = GestoreRichieste.cacheRateLimiting.keys();
  952.         if(keys!=null && !keys.isEmpty()) {
  953.             for (String key : keys) {
  954.                 if(key!=null) {
  955.                     Object o =  getRawObjectCacheEngine(GestoreRichieste.cacheRateLimiting.get(key));
  956.                     if(o instanceof RequestRateLimitingConfig) {
  957.                         RequestRateLimitingConfig rl = (RequestRateLimitingConfig) o;
  958.                         if(TipoPdD.DELEGATA.equals(rl.getTipoPdD()) && idPD.getNome().equals(rl.getNomePorta())) {
  959.                             keyForClean.add(key);
  960.                         }
  961.                     }
  962.                 }
  963.             }
  964.         }
  965.         if(keyForClean!=null && !keyForClean.isEmpty()) {
  966.             for (String key : keyForClean) {
  967.                 GestoreRichieste.cacheRateLimiting.remove(key);
  968.             }
  969.         }
  970.     }
  971.    
  972.     public static void removePdd(String portaDominio) throws UtilsException, CoreException {
  973.        
  974.         // RequestConfig
  975.         List<String> keyForClean = new ArrayList<>();
  976.         List<String> keys = GestoreRichieste.cacheApi.keys();
  977.         if(keys!=null && !keys.isEmpty()) {
  978.             for (String key : keys) {
  979.                 if(key!=null) {
  980.                     Object o =  getRawObjectCacheEngine(GestoreRichieste.cacheApi.get(key));
  981.                     if(o instanceof RequestConfig) {
  982.                         RequestConfig rc = (RequestConfig) o;
  983.                         if(
  984.                                 (rc.getSoggettoErogatorePdd()!=null && rc.getSoggettoErogatorePdd().getNome()!=null && portaDominio.equals(rc.getSoggettoErogatorePdd().getNome()))
  985.                                 ||
  986.                                 (rc.getSoggettoFruitorePdd()!=null && rc.getSoggettoFruitorePdd().getNome()!=null && portaDominio.equals(rc.getSoggettoFruitorePdd().getNome()))
  987.                             ) {
  988.                             keyForClean.add(key);
  989.                         }
  990.                     }
  991.                 }
  992.             }
  993.         }
  994.         if(keyForClean!=null && !keyForClean.isEmpty()) {
  995.             for (String key : keyForClean) {
  996.                 GestoreRichieste.cacheApi.remove(key);
  997.             }
  998.         }
  999.        
  1000.     }
  1001.    
  1002.     public static void removeSoggetto(IDSoggetto idSoggetto) throws UtilsException, CoreException {
  1003.         removeSoggettoRequestConfig(idSoggetto);
  1004.         removeSoggettoRequestRateLimitingConfig(idSoggetto);
  1005.         removeSoggettoRequestFruitore(idSoggetto);
  1006.     }
  1007.     private static void removeSoggettoRequestConfig(IDSoggetto idSoggetto) throws UtilsException, CoreException {  
  1008.         // RequestConfig
  1009.         List<String> keyForClean = new ArrayList<>();
  1010.         List<String> keys = GestoreRichieste.cacheApi.keys();
  1011.         if(keys!=null && !keys.isEmpty()) {
  1012.             for (String key : keys) {
  1013.                 if(key!=null) {
  1014.                     Object o =  getRawObjectCacheEngine(GestoreRichieste.cacheApi.get(key));
  1015.                     if(o instanceof RequestConfig) {
  1016.                         RequestConfig rc = (RequestConfig) o;
  1017.                         if(
  1018.                                 (rc.getIdServizio()!=null && rc.getIdServizio().getSoggettoErogatore()!=null && idSoggetto.equals(rc.getIdServizio().getSoggettoErogatore()))
  1019.                                 ||
  1020.                                 (rc.getIdFruitore()!=null && idSoggetto.equals(rc.getIdFruitore()))
  1021.                             ) {
  1022.                             keyForClean.add(key);
  1023.                         }
  1024.                     }
  1025.                 }
  1026.             }
  1027.         }
  1028.         if(keyForClean!=null && !keyForClean.isEmpty()) {
  1029.             for (String key : keyForClean) {
  1030.                 GestoreRichieste.cacheApi.remove(key);
  1031.             }
  1032.         }
  1033.     }
  1034.     private static void removeSoggettoRequestRateLimitingConfig(IDSoggetto idSoggetto) throws UtilsException, CoreException {
  1035.         // RequestRateLimitingConfig
  1036.         List<String> keyForClean = new ArrayList<>();
  1037.         List<String> keys = GestoreRichieste.cacheRateLimiting.keys();
  1038.         if(keys!=null && !keys.isEmpty()) {
  1039.             for (String key : keys) {
  1040.                 if(key!=null) {
  1041.                     Object o =  getRawObjectCacheEngine(GestoreRichieste.cacheRateLimiting.get(key));
  1042.                     if(o instanceof RequestRateLimitingConfig) {
  1043.                         RequestRateLimitingConfig rl = (RequestRateLimitingConfig) o;
  1044.                         if(
  1045.                                 (rl.getIdServizio()!=null && rl.getIdServizio().getSoggettoErogatore()!=null && idSoggetto.equals(rl.getIdServizio().getSoggettoErogatore()))
  1046.                                 ||
  1047.                                 (rl.getIdFruitore()!=null && idSoggetto.equals(rl.getIdFruitore()))
  1048.                             ) {
  1049.                             keyForClean.add(key);
  1050.                         }
  1051.                     }
  1052.                 }
  1053.             }
  1054.         }
  1055.         if(keyForClean!=null && !keyForClean.isEmpty()) {
  1056.             for (String key : keyForClean) {
  1057.                 GestoreRichieste.cacheRateLimiting.remove(key);
  1058.             }
  1059.         }
  1060.     }
  1061.     private static void removeSoggettoRequestFruitore(IDSoggetto idSoggetto) throws UtilsException, CoreException {
  1062.         // RequestFruitore
  1063.         List<String> keyForClean = new ArrayList<>();
  1064.         List<String> keys = GestoreRichieste.cacheFruitori.keys();
  1065.         if(keys!=null && !keys.isEmpty()) {
  1066.             for (String key : keys) {
  1067.                 if(key!=null) {
  1068.                     Object o =  getRawObjectCacheEngine(GestoreRichieste.cacheFruitori.get(key));
  1069.                     if(o instanceof RequestFruitore) {
  1070.                         RequestFruitore rf = (RequestFruitore) o;
  1071.                         if(
  1072.                                 (rf.getIdServizioApplicativoFruitore()!=null && rf.getIdServizioApplicativoFruitore().getIdSoggettoProprietario()!=null && idSoggetto.equals(rf.getIdServizioApplicativoFruitore().getIdSoggettoProprietario()))
  1073.                                 ||
  1074.                                 (rf.getIdSoggettoFruitore()!=null && idSoggetto.equals(rf.getIdSoggettoFruitore()))
  1075.                             ) {
  1076.                             keyForClean.add(key);
  1077.                         }
  1078.                     }
  1079.                 }
  1080.             }
  1081.         }
  1082.         if(keyForClean!=null && !keyForClean.isEmpty()) {
  1083.             for (String key : keyForClean) {
  1084.                 GestoreRichieste.cacheFruitori.remove(key);
  1085.             }
  1086.         }
  1087.     }
  1088.    
  1089.     public static void removeApplicativo(IDServizioApplicativo idApplicativo) throws UtilsException, CoreException {
  1090.         removeApplicativoRequestConfig(idApplicativo);
  1091.         removeApplicativoRequestFruitore(idApplicativo);
  1092.     }
  1093.     private static void removeApplicativoRequestConfig(IDServizioApplicativo idApplicativo) throws UtilsException, CoreException {
  1094.         // RequestConfig
  1095.         List<String> keyForClean = new ArrayList<>();
  1096.         List<String> keys = GestoreRichieste.cacheApi.keys();
  1097.         if(keys!=null && !keys.isEmpty()) {
  1098.             for (String key : keys) {
  1099.                 if(key!=null) {
  1100.                     Object o =  getRawObjectCacheEngine(GestoreRichieste.cacheApi.get(key));
  1101.                     if(o instanceof RequestConfig) {
  1102.                         RequestConfig rc = (RequestConfig) o;
  1103.                         if(rc.getIdServizio()!=null && rc.getIdServizio().getSoggettoErogatore()!=null && idApplicativo.getIdSoggettoProprietario().equals(rc.getIdServizio().getSoggettoErogatore())) {
  1104.                             ServizioApplicativo sa = rc.getServizioApplicativoErogatore(idApplicativo.getNome());
  1105.                             if(sa!=null) {
  1106.                                 keyForClean.add(key);
  1107.                             }
  1108.                         }
  1109.                     }
  1110.                 }
  1111.             }
  1112.         }
  1113.         if(keyForClean!=null && !keyForClean.isEmpty()) {
  1114.             for (String key : keyForClean) {
  1115.                 GestoreRichieste.cacheApi.remove(key);
  1116.             }
  1117.         }
  1118.     }
  1119.     private static void removeApplicativoRequestFruitore(IDServizioApplicativo idApplicativo) throws UtilsException, CoreException {    
  1120.         // RequestFruitore
  1121.         List<String> keyForClean = new ArrayList<>();
  1122.         List<String> keys = GestoreRichieste.cacheFruitori.keys();
  1123.         if(keys!=null && !keys.isEmpty()) {
  1124.             for (String key : keys) {
  1125.                 if(key!=null) {
  1126.                     Object o =  getRawObjectCacheEngine(GestoreRichieste.cacheFruitori.get(key));
  1127.                     if(o instanceof RequestFruitore) {
  1128.                         RequestFruitore rf = (RequestFruitore) o;
  1129.                         if(rf.getIdServizioApplicativoFruitore()!=null && idApplicativo.equals(rf.getIdServizioApplicativoFruitore())) {
  1130.                             keyForClean.add(key);
  1131.                         }
  1132.                     }
  1133.                 }
  1134.             }
  1135.         }
  1136.         if(keyForClean!=null && !keyForClean.isEmpty()) {
  1137.             for (String key : keyForClean) {
  1138.                 GestoreRichieste.cacheFruitori.remove(key);
  1139.             }
  1140.         }
  1141.     }
  1142.    
  1143.    
  1144.     public static void removeRuolo(IDRuolo idRuolo) throws UtilsException, CoreException {
  1145.         // RequestConfig
  1146.         List<String> keyForClean = new ArrayList<>();
  1147.         List<String> keys = GestoreRichieste.cacheApi.keys();
  1148.         if(keys!=null && !keys.isEmpty()) {
  1149.             for (String key : keys) {
  1150.                 if(key!=null) {
  1151.                     Object o =  getRawObjectCacheEngine(GestoreRichieste.cacheApi.get(key));
  1152.                     if(o instanceof RequestConfig) {
  1153.                         RequestConfig rc = (RequestConfig) o;
  1154.                        
  1155.                         List<String> ruoloKeys = rc.getRuoloKeys();
  1156.                         if(ruoloKeys!=null && ruoloKeys.contains(idRuolo.getNome())) {
  1157.                             keyForClean.add(key);
  1158.                         }
  1159.                     }
  1160.                 }
  1161.             }
  1162.         }
  1163.         if(keyForClean!=null && !keyForClean.isEmpty()) {
  1164.             for (String key : keyForClean) {
  1165.                 GestoreRichieste.cacheApi.remove(key);
  1166.             }
  1167.         }
  1168.     }
  1169.    
  1170.     public static void removeScope(IDScope idScope) throws UtilsException, CoreException {
  1171.         // RequestConfig
  1172.         List<String> keyForClean = new ArrayList<>();
  1173.         List<String> keys = GestoreRichieste.cacheApi.keys();
  1174.         if(keys!=null && !keys.isEmpty()) {
  1175.             for (String key : keys) {
  1176.                 if(key!=null) {
  1177.                     Object o =  getRawObjectCacheEngine(GestoreRichieste.cacheApi.get(key));
  1178.                     if(o instanceof RequestConfig) {
  1179.                         RequestConfig rc = (RequestConfig) o;
  1180.                        
  1181.                         List<String> scopeKeys = rc.getScopeKeys();
  1182.                         if(scopeKeys!=null && scopeKeys.contains(idScope.getNome())) {
  1183.                             keyForClean.add(key);
  1184.                         }
  1185.                     }
  1186.                 }
  1187.             }
  1188.         }
  1189.         if(keyForClean!=null && !keyForClean.isEmpty()) {
  1190.             for (String key : keyForClean) {
  1191.                 GestoreRichieste.cacheApi.remove(key);
  1192.             }
  1193.         }
  1194.     }
  1195.    
  1196.    
  1197.     public static void removeGenericProperties(IDGenericProperties idGP) throws UtilsException, CoreException {
  1198.         // RequestConfig
  1199.         List<String> keyForClean = new ArrayList<>();
  1200.         List<String> keys = GestoreRichieste.cacheApi.keys();
  1201.         if(keys!=null && !keys.isEmpty()) {
  1202.             for (String key : keys) {
  1203.                 if(key!=null) {
  1204.                     Object o =  getRawObjectCacheEngine(GestoreRichieste.cacheApi.get(key));
  1205.                     if(o instanceof RequestConfig) {
  1206.                         RequestConfig rc = (RequestConfig) o;
  1207.                        
  1208.                         if(org.openspcoop2.pdd.core.token.Costanti.TIPOLOGIA.equals(idGP.getTipologia())) {
  1209.                             Object oT = rc.getPolicyValidazioneToken(idGP.getNome());
  1210.                             if(oT!=null) {
  1211.                                 keyForClean.add(key);
  1212.                                 continue;
  1213.                             }
  1214.                         }
  1215.                        
  1216.                         if(org.openspcoop2.pdd.core.token.Costanti.TIPOLOGIA_RETRIEVE.equals(idGP.getTipologia())) {
  1217.                             Object oT = rc.getPolicyNegoziazioneToken(idGP.getNome());
  1218.                             if(oT!=null) {
  1219.                                 keyForClean.add(key);
  1220.                                 continue;
  1221.                             }
  1222.                         }
  1223.                        
  1224.                         if(org.openspcoop2.pdd.core.token.Costanti.ATTRIBUTE_AUTHORITY.equals(idGP.getTipologia())) {
  1225.                             Object oT = rc.getAttributeAuthority(idGP.getNome());
  1226.                             if(oT!=null) {
  1227.                                 keyForClean.add(key);
  1228.                                 continue;
  1229.                             }
  1230.                         }
  1231.                        
  1232.                         List<String> forwardProxyKeys = (rc.getForwardProxyEnabled()!=null && rc.getForwardProxyEnabled()) ? rc.getForwardProxyKeys() : null;
  1233.                         String forwardProxyGP = ConfigurazionePdD._toKey_ForwardProxyConfigSuffix(idGP);
  1234.                         if(forwardProxyKeys!=null && forwardProxyKeys.contains(forwardProxyGP)) {
  1235.                             keyForClean.add(key);
  1236.                             continue;
  1237.                         }
  1238.                     }
  1239.                 }
  1240.             }
  1241.         }
  1242.         if(keyForClean!=null && !keyForClean.isEmpty()) {
  1243.             for (String key : keyForClean) {
  1244.                 GestoreRichieste.cacheApi.remove(key);
  1245.             }
  1246.         }
  1247.     }
  1248.    
  1249.    
  1250.    

  1251.    
  1252.     // ******** RICHIESTE **********
  1253.    
  1254.     private static final String KEY_IN_MEMORY_ONLY = "@@InMemoryOnly@@";
  1255.     private static final String INIT_SEPARATOR = "\n\n=======================";
  1256.     private static final String END_SEPARATOR = "=======================";
  1257.    
  1258.     public static void setRequestConfigInMemory(RequestInfo requestInfo) {
  1259.         if(org.openspcoop2.utils.cache.Cache.DEBUG_CACHE) {
  1260.             System.out.println(INIT_SEPARATOR);
  1261.             System.out.println("Creato RequestConfig in ram");
  1262.             System.out.println(END_SEPARATOR);
  1263.         }
  1264.                    
  1265.         RequestConfig rc = new RequestConfig();
  1266.         rc.setKey(KEY_IN_MEMORY_ONLY);
  1267.         rc.setCached(false);
  1268.         requestInfo.setRequestConfig(rc);
  1269.        
  1270.         RequestRateLimitingConfig rcRT = new RequestRateLimitingConfig();
  1271.         rcRT.setKey(KEY_IN_MEMORY_ONLY);
  1272.         rcRT.setCached(false);
  1273.         requestInfo.setRequestRateLimitingConfig(rcRT);
  1274.        
  1275.         RequestThreadContext rt = new RequestThreadContext(KEY_IN_MEMORY_ONLY, logger!=null ? logger : logConsole);
  1276.         requestInfo.setRequestThreadContext(rt);
  1277.     }
  1278.    
  1279.     public static void readRequestConfig(RequestInfo requestInfo) {
  1280.        
  1281.         if(!useCache) {
  1282.             setRequestConfigInMemory(requestInfo);
  1283.             return;
  1284.         }
  1285.        
  1286.         String key = buildKey(requestInfo, null, null);
  1287.         if(key==null) {
  1288.             return;
  1289.         }
  1290.         readRequestConfigEngine(requestInfo, key, null);
  1291.         readRequestRateLimitingConfigEngine(requestInfo, key);
  1292.         requestInfo.setRequestThreadContext(RequestThreadContext.getRequestThreadContext());
  1293.         if(requestInfo.getRequestThreadContext()!=null) {
  1294.             // ripulisco precedenti assegnamenti
  1295.             requestInfo.getRequestThreadContext().setRequestFruitoreTrasportoInfo(null);
  1296.             requestInfo.getRequestThreadContext().setRequestFruitoreTokenInfo(null);
  1297.         }
  1298.        
  1299.         if(org.openspcoop2.utils.cache.Cache.DEBUG_CACHE) {
  1300.             System.out.println(INIT_SEPARATOR);
  1301.             System.out.println("Creato RequestConfig con chiave '"+key+"'");
  1302.             System.out.println("\tRequestThreadContext '"+requestInfo.getRequestThreadContext().gettName()+"'");
  1303.             System.out.println(END_SEPARATOR);
  1304.         }
  1305.     }
  1306.    
  1307.     public static void updateRequestConfig(RequestInfo requestInfo, ServiceBinding serviceBinding, OpenSPCoop2MessageSoapStreamReader soapStreamReader) {
  1308.        
  1309.         if(!useCache) {
  1310.             return; // solo in memory, la differenza sull'azione non ĆØ importante
  1311.         }
  1312.        
  1313.         if(requestInfo==null || requestInfo.getRequestConfig()==null) {
  1314.             return;
  1315.         }
  1316.         String oldKey = requestInfo.getRequestConfig().getKey();
  1317.         String key = buildKey(requestInfo, serviceBinding, soapStreamReader);
  1318.         if(key==null) {
  1319.             return;
  1320.         }
  1321.         if(key.equals(oldKey)) {
  1322.             return; // non e' stato aggiunto il soapStreamReader o modifiche derivanti all'azione specifica
  1323.         }
  1324.        
  1325.         if(org.openspcoop2.utils.cache.Cache.DEBUG_CACHE) {
  1326.             System.out.println(INIT_SEPARATOR);
  1327.             System.out.println("AGGIORNO RequestConfig");
  1328.             System.out.println("OLD: "+oldKey);
  1329.             System.out.println("NEW: "+key);
  1330.             System.out.println(END_SEPARATOR);
  1331.         }
  1332.        
  1333.         RequestConfig rcOld = requestInfo.getRequestConfig();
  1334.        
  1335.         // Aggiorno nel contesto nuovi oggetti con nuova chiave
  1336.         readRequestConfigEngine(requestInfo, key, rcOld);
  1337.         readRequestRateLimitingConfigEngine(requestInfo, key);

  1338.         requestInfo.setPreRequestConfig(rcOld);

  1339.     }
  1340.    
  1341.     private static void readRequestConfigEngine(RequestInfo requestInfo, String key, RequestConfig srcClone) {
  1342.        
  1343.         if(cacheApi==null) {
  1344.             return;
  1345.         }
  1346.        
  1347.         org.openspcoop2.utils.cache.CacheResponse response =
  1348.                 (org.openspcoop2.utils.cache.CacheResponse) cacheApi.get(key);
  1349.         if(response != null &&
  1350.             response.getObject()!=null){
  1351.             RequestConfig rc = (RequestConfig) response.getObject();
  1352.             /**System.out.println("TROVATA IN CACHE ("+rc.isCached()+")");*/
  1353.             if(rc.getIdServizio()!=null) {
  1354.                 rc.setIdServizio(rc.getIdServizio().clone()); // per l'azione
  1355.             }
  1356.             requestInfo.setRequestConfig(rc);
  1357.             return;
  1358.         }
  1359.        
  1360.         /**System.out.println("CONFIG NON TROVATA IN CACHE");*/
  1361.         RequestConfig rc = new RequestConfig();
  1362.         if(srcClone!=null) {
  1363.             rc.copyFrom(srcClone);
  1364.         }
  1365.         else {
  1366.             rc = new RequestConfig();
  1367.         }
  1368.         rc.setKey(key);
  1369.         rc.setCached(false);
  1370.         requestInfo.setRequestConfig(rc);
  1371.        
  1372.     }
  1373.    
  1374.     private static void readRequestRateLimitingConfigEngine(RequestInfo requestInfo, String key) {
  1375.        
  1376.         if(cacheRateLimiting==null) {
  1377.             return;
  1378.         }
  1379.        
  1380.         org.openspcoop2.utils.cache.CacheResponse response =
  1381.                 (org.openspcoop2.utils.cache.CacheResponse) cacheRateLimiting.get(key);
  1382.         if(response != null &&
  1383.             response.getObject()!=null){
  1384.             RequestRateLimitingConfig rc = (RequestRateLimitingConfig) response.getObject();
  1385.             /** System.out.println("TROVATA IN CACHE ("+rc.isCached()+")"); */
  1386.             requestInfo.setRequestRateLimitingConfig(rc);
  1387.             return;
  1388.         }
  1389.        
  1390.         /** System.out.println("RATE LIMIT NON TROVATA IN CACHE"); */
  1391.         RequestRateLimitingConfig rc = new RequestRateLimitingConfig();
  1392.         rc.setKey(key);
  1393.         rc.setCached(false);
  1394.         requestInfo.setRequestRateLimitingConfig(rc);
  1395.        
  1396.     }
  1397.    
  1398.     public static void saveRequestConfig(RequestInfo requestInfo)throws UtilsException, CoreException {
  1399.        
  1400.         if(!useCache) {
  1401.             return;
  1402.         }
  1403.        
  1404.         if(requestInfo==null) {
  1405.             return;
  1406.         }      
  1407.        
  1408.         String idTransazione = requestInfo.getIdTransazione();
  1409.        
  1410.         if(cacheApi!=null && requestInfo.getPreRequestConfig()!=null && !requestInfo.getPreRequestConfig().isCached()) {
  1411.             SemaphoreLock lock = lockCache.acquire("savePreRequestConfig", idTransazione);
  1412.             try {
  1413.                 String key = requestInfo.getPreRequestConfig().getKey();
  1414.                 org.openspcoop2.utils.cache.CacheResponse response =
  1415.                         (org.openspcoop2.utils.cache.CacheResponse) cacheApi.get(key);
  1416.                 if(response == null){
  1417.                     org.openspcoop2.utils.cache.CacheResponse responseCache = new org.openspcoop2.utils.cache.CacheResponse();
  1418.                     requestInfo.getPreRequestConfig().setCached(true);
  1419.                     responseCache.setObject(requestInfo.getPreRequestConfig());
  1420.                     cacheApi.put(key,responseCache);
  1421.                     /** System.out.println("SALVATA IN CACHE con chiave ["+requestInfo.getPreRequestConfig().getKey()+"]"); */
  1422.                 }
  1423.                 else {
  1424.                     /** System.out.println("NON SALVATA IN CACHE, GIA PRESENTE"); */
  1425.                 }
  1426.                
  1427.             }finally {
  1428.                 lockCache.release(lock, "savePreRequestConfig", idTransazione);
  1429.             }
  1430.         }
  1431.        
  1432.         if(cacheApi!=null && requestInfo.getRequestConfig()!=null && !requestInfo.getRequestConfig().isCached()) {
  1433.             SemaphoreLock lock = lockCache.acquire("saveRequestConfig", idTransazione);
  1434.             try {
  1435.                 String key = requestInfo.getRequestConfig().getKey();
  1436.                 org.openspcoop2.utils.cache.CacheResponse response =
  1437.                         (org.openspcoop2.utils.cache.CacheResponse) cacheApi.get(key);
  1438.                 if(response == null){
  1439.                     org.openspcoop2.utils.cache.CacheResponse responseCache = new org.openspcoop2.utils.cache.CacheResponse();
  1440.                     requestInfo.getRequestConfig().setCached(true);
  1441.                     responseCache.setObject(requestInfo.getRequestConfig());
  1442.                     cacheApi.put(key,responseCache);
  1443.                     /** System.out.println("SALVATA IN CACHE con chiave ["+requestInfo.getRequestConfig().getKey()+"]");*/
  1444.                 }
  1445.                 else {
  1446.                     /**System.out.println("NON SALVATA IN CACHE, GIA PRESENTE");*/
  1447.                 }
  1448.                
  1449.             }finally {
  1450.                 lockCache.release(lock, "saveRequestConfig", idTransazione);
  1451.             }
  1452.         }
  1453.        
  1454.         if(cacheRateLimiting!=null && requestInfo.getRequestRateLimitingConfig()!=null && !requestInfo.getRequestRateLimitingConfig().isCached()) {
  1455.             SemaphoreLock lock = lockCache_rateLimiting.acquire("saveRequestRateLimitingConfig", idTransazione);
  1456.             try {
  1457.                 String key = requestInfo.getRequestConfig().getKey();
  1458.                 org.openspcoop2.utils.cache.CacheResponse response =
  1459.                         (org.openspcoop2.utils.cache.CacheResponse) cacheRateLimiting.get(key);
  1460.                 if(response == null){
  1461.                     org.openspcoop2.utils.cache.CacheResponse responseCache = new org.openspcoop2.utils.cache.CacheResponse();
  1462.                     requestInfo.getRequestRateLimitingConfig().setCached(true);
  1463.                     responseCache.setObject(requestInfo.getRequestRateLimitingConfig());
  1464.                     cacheRateLimiting.put(key,responseCache);
  1465.                     /** System.out.println("RATE LIMIT SALVATA IN CACHE con chiave ["+requestInfo.getRequestConfig().getKey()+"]");*/
  1466.                 }
  1467.                 else {
  1468.                     /** System.out.println("RATE LIMIT NON SALVATA IN CACHE, GIA PRESENTE");*/
  1469.                 }
  1470.                
  1471.             }finally {
  1472.                 lockCache_rateLimiting.release(lock, "saveRequestRateLimitingConfig", idTransazione);
  1473.             }
  1474.         }
  1475.        
  1476.         if(requestInfo.getRequestThreadContext()!=null) {
  1477.             requestInfo.getRequestThreadContext().clear();
  1478.             requestInfo.setRequestThreadContext(null);
  1479.         }
  1480.     }
  1481.    
  1482.    
  1483.     private static String buildKey(RequestInfo requestInfo, ServiceBinding serviceBinding, OpenSPCoop2MessageSoapStreamReader soapStreamReader) {
  1484.        
  1485.         // Lo scopo e' individuare solo la porta applicativa/delegata di default e specifica (azione), il servizio e l'api
  1486.         // NOTA:
  1487.         //     Se durante la gestione (in *ServiceUtils, tramite la chiamata del metodo RequestInfoConfigUtilities.checkRequestInfoConfig)
  1488.         //     viene rilevata una identificazione dell'azione CONTENT_BASED,PROTOCOL_BASED,INPUT_BASED,DELEGATED_BY, la gestione della richiesta tramite url non viene attivata.
  1489.         //     Inoltre se si rileva una API SOAP con il riconoscimento dell'azione basata su wsdl (quindi rootElement), e tale informazione non ĆØ disponibile nel soapStreamReader
  1490.         //     la gestione della richiesta tramite url non viene attivata.
  1491.        
  1492.         StringBuilder bf = new StringBuilder();
  1493.        
  1494.         List<String> headerInKey = new ArrayList<>();
  1495.         headerInKey.add(HttpConstants.SOAP11_MANDATORY_HEADER_HTTP_SOAP_ACTION);
  1496.        
  1497.         if(requestInfo.getProtocolContext()!=null) {
  1498.            
  1499.             // NOTA il meccanismo NON deve funzionare se non viene fornita il nome di una porta applicativa come per SPCoop
  1500.             if(requestInfo.getProtocolContext().getFunctionParameters()==null || "".equals(requestInfo.getProtocolContext().getFunctionParameters().trim())) {
  1501.                 return null;
  1502.             }
  1503.            
  1504.             if(requestInfo.getProtocolContext().getRequestType()!=null){
  1505.                 if(bf.length()>0) {
  1506.                     bf.append("\t");
  1507.                 }
  1508.                 bf.append("Method:").append(requestInfo.getProtocolContext().getRequestType());
  1509.             }
  1510.             else {
  1511.                 return null;
  1512.             }
  1513.            
  1514.             if(requestInfo.getProtocolContext()!=null){
  1515.                 if(bf.length()>0) {
  1516.                     bf.append("\t");
  1517.                 }
  1518.                 // i parametri non servono per identificare una risorsa
  1519.                 bf.append("URL:").append(requestInfo.getProtocolContext().getUrlInvocazioneWithoutParameters());
  1520.             }
  1521.            
  1522.             if(!headerInKey.isEmpty()) {
  1523.                 for (String header : headerInKey) {
  1524.                     String s = requestInfo.getProtocolContext().getHeader_compactMultipleValues(header);
  1525.                     if(s!=null) {
  1526.                         if(bf.length()>0) {
  1527.                             bf.append("\t");
  1528.                         }
  1529.                         bf.append(header).append(":").append(s);
  1530.                     }
  1531.                 }
  1532.             }

  1533.         }
  1534.         else {
  1535.             return null;
  1536.         }
  1537.        
  1538.         if( ServiceBinding.SOAP.equals(serviceBinding) && soapStreamReader!=null) {
  1539.             if(soapStreamReader.getRootElementLocalName()!=null && soapStreamReader.getRootElementNamespace()!=null) {
  1540.                 if(bf.length()>0) {
  1541.                     bf.append("\t");
  1542.                 }
  1543.                 bf.append("soapRootElement:{").
  1544.                     append(soapStreamReader.getRootElementNamespace()).
  1545.                     append("}").
  1546.                     append(soapStreamReader.getRootElementLocalName());
  1547.             }
  1548.             else {
  1549.                 return null;
  1550.             }
  1551.         }
  1552.        
  1553.         return bf.toString();
  1554.     }
  1555.    
  1556.    
  1557.     // ******** FRUITORI **********
  1558.     private static final String FRUITORE_TRASPORTO = "trasparto";
  1559.     private static final String FRUITORE_TOKEN = "token";
  1560.     private static final String FRUITORE_TOKEN_MODI = "token-modi";
  1561.     public static RequestFruitore readFruitoreTrasporto(RequestInfo requestInfo, IDSoggetto idSoggetto, IDServizioApplicativo servizioApplicativo) {
  1562.         return readFruitoreEngine(requestInfo, idSoggetto, servizioApplicativo, null, FRUITORE_TRASPORTO, true, false);
  1563.     }
  1564.     public static RequestFruitore readFruitoreToken(RequestInfo requestInfo, IDSoggetto idSoggetto, IDServizioApplicativo servizioApplicativo) {
  1565.         return readFruitoreEngine(requestInfo, idSoggetto, servizioApplicativo, null, FRUITORE_TOKEN, false, false);
  1566.     }
  1567.     public static RequestFruitore readFruitoreTokenModI(RequestInfo requestInfo, String certificateKey) {
  1568.         return readFruitoreEngine(requestInfo, null, null, certificateKey, FRUITORE_TOKEN_MODI, false, true);
  1569.     }
  1570.     private static RequestFruitore readFruitoreEngine(RequestInfo requestInfo, IDSoggetto idSoggetto, IDServizioApplicativo servizioApplicativo, String certificateKey,
  1571.             String tipo, boolean trasporto, boolean tokenModi) {
  1572.         String key = null;
  1573.         if(tokenModi) {    
  1574.             key = buildKey(null, null, certificateKey, tipo, tokenModi);
  1575.         }
  1576.         else {
  1577.             key = buildKey(idSoggetto, servizioApplicativo,null, tipo, tokenModi);
  1578.         }
  1579.         if(key==null &&
  1580.             useCache) {
  1581.             return null;
  1582.         }
  1583.         if(useCache) {
  1584.             readFruitoreEngine(requestInfo, key, trasporto);
  1585.         }
  1586.         if(requestInfo!=null && requestInfo.getRequestThreadContext()!=null) {
  1587.             if(trasporto) {
  1588.                 if(requestInfo.getRequestThreadContext().getRequestFruitoreTrasportoInfo()!=null) {
  1589.                     return requestInfo.getRequestThreadContext().getRequestFruitoreTrasportoInfo();
  1590.                 }  
  1591.                 else {
  1592.                     return null;
  1593.                 }
  1594.             }
  1595.             else {
  1596.                 if(requestInfo.getRequestThreadContext().getRequestFruitoreTokenInfo()!=null) {
  1597.                     return requestInfo.getRequestThreadContext().getRequestFruitoreTokenInfo();
  1598.                 }  
  1599.                 else {
  1600.                     return null;
  1601.                 }
  1602.             }
  1603.         }
  1604.         else {
  1605.             return null;
  1606.         }
  1607.     }
  1608.        
  1609.     public static RequestFruitore readFruitoreEngine(RequestInfo requestInfo, String key, boolean trasporto) {
  1610.        
  1611.         if(requestInfo==null || requestInfo.getRequestThreadContext()==null) {
  1612.             return null;
  1613.         }
  1614.        
  1615.         if(cacheFruitori==null) {
  1616.             return null;
  1617.         }
  1618.        
  1619.         org.openspcoop2.utils.cache.CacheResponse response =
  1620.                 (org.openspcoop2.utils.cache.CacheResponse) cacheFruitori.get(key);
  1621.         if(response != null &&
  1622.             response.getObject()!=null){
  1623.             RequestFruitore rf = (RequestFruitore) response.getObject();
  1624.             /** System.out.println("TROVATO FUITORE IN CACHE ("+rf.isCached()+")");*/
  1625.             if(trasporto) {
  1626.                 requestInfo.getRequestThreadContext().setRequestFruitoreTrasportoInfo(rf);
  1627.             }
  1628.             else {
  1629.                 requestInfo.getRequestThreadContext().setRequestFruitoreTokenInfo(rf);
  1630.             }
  1631.             return rf;
  1632.         }
  1633.        
  1634.         /** System.out.println("FRUITORE NON TROVATO IN CACHE");*/
  1635.         return null;
  1636.        
  1637.     }
  1638.    
  1639.     public static void saveRequestFruitoreTrasporto(RequestInfo requestInfo, RequestFruitore rf)throws UtilsException {
  1640.          saveRequestFruitoreEngine(requestInfo, rf, FRUITORE_TRASPORTO, true, false);
  1641.     }
  1642.     public static void saveRequestFruitoreToken(RequestInfo requestInfo, RequestFruitore rf)throws UtilsException {
  1643.          saveRequestFruitoreEngine(requestInfo, rf, FRUITORE_TOKEN, false, false);
  1644.     }
  1645.     public static void saveRequestFruitoreTokenModI(RequestInfo requestInfo, RequestFruitore rf)throws UtilsException {
  1646.          saveRequestFruitoreEngine(requestInfo, rf, FRUITORE_TOKEN_MODI, false, true);
  1647.     }
  1648.     private static void saveRequestFruitoreEngine(RequestInfo requestInfo, RequestFruitore rf,
  1649.             String tipo, boolean trasporto, boolean tokenModi) throws UtilsException {
  1650.         if(requestInfo==null || requestInfo.getRequestThreadContext()==null || rf==null) {
  1651.             return;
  1652.         }  
  1653.        
  1654.         String key = null;
  1655.         if(useCache) {
  1656.             if(tokenModi) {
  1657.                 if(rf.getCertificateKey()==null) {
  1658.                     return;
  1659.                 }
  1660.                 key = buildKey(null, null, rf.getCertificateKey(), tipo, tokenModi);
  1661.             }
  1662.             else {
  1663.                 if(rf.getIdSoggettoFruitore()==null) {
  1664.                     return;
  1665.                 }
  1666.                 key = buildKey(rf.getIdSoggettoFruitore(), rf.getIdServizioApplicativoFruitore(), null, tipo, tokenModi);
  1667.             }
  1668.             if(key==null) {
  1669.                 return;
  1670.             }
  1671.         }
  1672.         else {
  1673.             key=KEY_IN_MEMORY_ONLY;
  1674.         }
  1675.        
  1676.         rf.setKey(key);
  1677.         rf.setCached(false);
  1678.         if(trasporto) {
  1679.             requestInfo.getRequestThreadContext().setRequestFruitoreTrasportoInfo(rf);
  1680.         }
  1681.         else {
  1682.             requestInfo.getRequestThreadContext().setRequestFruitoreTokenInfo(rf);
  1683.         }
  1684.        
  1685.        
  1686.         if(!useCache || cacheFruitori==null) {
  1687.             return; // salvo solo nel thread context
  1688.         }
  1689.        
  1690.         String idTransazione = requestInfo.getIdTransazione();
  1691.        
  1692.         RequestFruitore requestFruitore = null;
  1693.         if(trasporto) {
  1694.             requestFruitore = requestInfo.getRequestThreadContext().getRequestFruitoreTrasportoInfo();
  1695.         }
  1696.         else {
  1697.             requestFruitore = requestInfo.getRequestThreadContext().getRequestFruitoreTokenInfo();
  1698.         }
  1699.        
  1700.         if(!requestFruitore.isCached()) {
  1701.             SemaphoreLock lock = lockCache_fruitori.acquire("saveRequestFruitore", idTransazione);
  1702.             try {
  1703.                 org.openspcoop2.utils.cache.CacheResponse response =
  1704.                         (org.openspcoop2.utils.cache.CacheResponse) cacheFruitori.get(key);
  1705.                 if(response == null){
  1706.                     org.openspcoop2.utils.cache.CacheResponse responseCache = new org.openspcoop2.utils.cache.CacheResponse();
  1707.                     requestFruitore.setCached(true);
  1708.                     responseCache.setObject(requestFruitore);
  1709.                     cacheFruitori.put(key,responseCache);
  1710.                     /** System.out.println("FRUITORE SALVATO IN CACHE con chiave ["+requestInfo.getRequestThreadContext().getRequestFruitoreInfo().getKey()+"]"); */
  1711.                 }
  1712.                 else {
  1713.                     /** System.out.println("FUITORE NON SALVATO IN CACHE, GIA PRESENTE"); */
  1714.                 }
  1715.                
  1716.             }finally {
  1717.                 lockCache_fruitori.release(lock, "saveRequestFruitore", idTransazione);
  1718.             }
  1719.         }
  1720.        
  1721.     }
  1722.    
  1723.     private static String buildKey(IDSoggetto idSoggetto, IDServizioApplicativo servizioApplicativo, String certificateKey, String tipo, boolean tokenModi) {
  1724.                
  1725.         StringBuilder bf = new StringBuilder();
  1726.         bf.append("auth:").append(tipo);
  1727.        
  1728.         if(tokenModi) {
  1729.             if(certificateKey==null) {
  1730.                 return null;
  1731.             }
  1732.             bf.append(" cert:");
  1733.             bf.append(certificateKey);
  1734.         }
  1735.         else {
  1736.             if(idSoggetto==null) {
  1737.                 return null;
  1738.             }
  1739.             bf.append(" soggetto:");
  1740.             bf.append(idSoggetto.toString());
  1741.            
  1742.             bf.append(" sa:");
  1743.             if(servizioApplicativo!=null) {
  1744.                 bf.append(servizioApplicativo.toFormatString());
  1745.             }
  1746.             else {
  1747.                 bf.append("-");
  1748.             }
  1749.         }
  1750.        
  1751.         return bf.toString();
  1752.     }
  1753.    
  1754.     public static String toCertificateKey(CertificateInfo certificateInfo) {
  1755.         StringBuilder sb = new StringBuilder();
  1756.         sb.append("[");
  1757.         if(certificateInfo.getSubject()!=null) {
  1758.             sb.append("subject:");
  1759.             sb.append(certificateInfo.getSubject().toString());
  1760.         }
  1761.         else {
  1762.             return null;
  1763.         }
  1764.         if(certificateInfo.getIssuer()!=null) {
  1765.             sb.append(" issuer:");
  1766.             sb.append(certificateInfo.getIssuer().toString());
  1767.         }
  1768.         else {
  1769.             return null;
  1770.         }
  1771.         if(certificateInfo.getSerialNumber()!=null) {
  1772.             sb.append(" serialNumber:");
  1773.             sb.append(certificateInfo.getSerialNumber());
  1774.         }
  1775.         else {
  1776.             return null;
  1777.         }
  1778.         sb.append("]");
  1779.         return sb.toString();
  1780.     }
  1781. }