EsitiConfigUtils.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */

  20. package org.openspcoop2.protocol.utils;

  21. import java.util.ArrayList;
  22. import java.util.List;

  23. import org.openspcoop2.protocol.sdk.ProtocolException;
  24. import org.openspcoop2.protocol.sdk.constants.EsitoTransazioneName;
  25. import org.slf4j.Logger;

  26. /**    
  27.  * EsitiConfigUtils
  28.  *
  29.  * @author Poli Andrea (poli@link.it)
  30.  * @author $Author$
  31.  * @version $Rev$, $Date$
  32.  */
  33. public class EsitiConfigUtils {
  34.    
  35.     private EsitiConfigUtils() {}

  36.     public static int TUTTI_ESITI_DISABILITATI = -2;
  37.    
  38.     public static EsitiProperties getEsitiPropertiesForConfiguration(Logger log) throws ProtocolException {
  39.         return EsitiProperties.getInstance(log, EsitiProperties.NO_PROTOCOL_CONFIG);
  40.     }
  41.     public static EsitiProperties getEsitiPropertiesForContext(Logger log) throws ProtocolException {
  42.         return EsitiProperties.getInstance(log, EsitiProperties.NO_PROTOCOL_CONFIG);
  43.     }
  44.    
  45.     // esiti indipendenti dal protocollo. Li inizializzo una volta sola per questione di performance.
  46.    
  47.     private static int esitoMaxThreads = -1;
  48.     private static int esitoViolazionePolicyRateLimiting = -1;
  49.     private static int esitoCorsGateway = -1;
  50.     private static int esitoCorsTrasparente = -1;
  51.     private static void checkInitEsiti(EsitiProperties esiti) throws ProtocolException {
  52.         if(esitoMaxThreads<0) {
  53.             initEsiti(esiti);
  54.         }
  55.     }
  56.     private static synchronized void initEsiti(EsitiProperties esiti) throws ProtocolException {
  57.         if(esitoMaxThreads<0) {
  58.             esitoMaxThreads = esiti.convertNameToCode(EsitoTransazioneName.CONTROLLO_TRAFFICO_MAX_THREADS.name());
  59.             esitoViolazionePolicyRateLimiting = esiti.convertNameToCode(EsitoTransazioneName.CONTROLLO_TRAFFICO_POLICY_VIOLATA.name());
  60.             esitoCorsGateway = esiti.convertNameToCode(EsitoTransazioneName.CORS_PREFLIGHT_REQUEST_VIA_GATEWAY.name());
  61.             esitoCorsTrasparente = esiti.convertNameToCode(EsitoTransazioneName.CORS_PREFLIGHT_REQUEST_TRASPARENTE.name());
  62.         }
  63.     }
  64.    
  65.     public static List<String> getRegistrazioneEsiti(String esitiConfig, Logger log, StringBuilder bf) throws ProtocolException{
  66.         return getRegistrazioneEsiti(esitiConfig, log, bf, getEsitiPropertiesForConfiguration(log));
  67.     }
  68.     public static List<String> getRegistrazioneEsiti(String esitiConfig, Logger log, StringBuilder bf, EsitiProperties esiti) throws ProtocolException {
  69.        
  70.         if(log!=null) {
  71.             // nop
  72.         }
  73.         List<String> listNull = null;
  74.        
  75.         if(esitiConfig==null || "".equals(esitiConfig.trim())){
  76.            
  77.             // creo un default composto da tutti ad eccezione dell'esito (CONTROLLO_TRAFFICO_MAX_THREADS), delle violazioni policy di rate limiting (CONTROLLO_TRAFFICO_POLICY_VIOLATA) e delle richieste CORS OPTIONS
  78.             List<Integer> esitiCodes = esiti.getEsitiCode();
  79.            
  80.             if(esitiCodes!=null && !esitiCodes.isEmpty()){
  81.                 List<String> esitiDaRegistrare = new ArrayList<>();
  82.                 for (Integer esito : esitiCodes) {
  83.                     checkInitEsiti(esiti);
  84.                     if(esito.intValue()!=esitoMaxThreads &&
  85.                             esito.intValue()!=esitoViolazionePolicyRateLimiting &&
  86.                             esito.intValue()!=esitoCorsGateway &&
  87.                             esito.intValue()!=esitoCorsTrasparente){
  88.                         if(bf.length()>0){
  89.                             bf.append(",");
  90.                         }
  91.                         bf.append(esito);
  92.                         esitiDaRegistrare.add(esito+"");
  93.                     }
  94.                 }
  95.                 if(!esitiDaRegistrare.isEmpty()){
  96.                     return esitiDaRegistrare;
  97.                 }
  98.             }
  99.            
  100.             return listNull; // non dovrebbe succedere, degli esiti nell'EsitiProperties dovrebbero esistere
  101.         }
  102.         else{
  103.            
  104.             String [] tmp = esitiConfig.split(",");
  105.             if(tmp!=null && tmp.length>0){
  106.                 List<String> esitiDaRegistrare = new ArrayList<>();
  107.                 for (int i = 0; i < tmp.length; i++) {
  108.                     String t = tmp[i];
  109.                     if(t!=null){
  110.                         t = t.trim();
  111.                         if(!"".equals(t)){
  112.                             if(bf.length()>0){
  113.                                 bf.append(",");
  114.                             }
  115.                             bf.append(t);
  116.                             esitiDaRegistrare.add(t);
  117.                         }
  118.                     }
  119.                 }
  120.                 if(!esitiDaRegistrare.isEmpty()){
  121.                     return esitiDaRegistrare;
  122.                 }
  123.             }
  124.            
  125.             return listNull; // non dovrebbe succedere, si rientra nel ramo then dell'if principale
  126.         }
  127.     }
  128.    
  129. }