ConfigurazioneCondizionale.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.behaviour.conditional;

  21. import java.util.Set;
  22. import java.util.TreeMap;

  23. import org.openspcoop2.core.registry.Resource;
  24. import org.openspcoop2.pdd.core.Utilities;
  25. import org.openspcoop2.pdd.core.behaviour.BehaviourException;
  26. import org.openspcoop2.utils.regexp.RegExpException;
  27. import org.openspcoop2.utils.regexp.RegExpNotFoundException;
  28. import org.openspcoop2.utils.regexp.RegExpNotValidException;
  29. import org.openspcoop2.utils.regexp.RegularExpressionEngine;

  30. /**
  31.  * ConfigurazioneCondizionale
  32.  *
  33.  * @author Andrea Poli (apoli@link.it)
  34.  * @author $Author$
  35.  * @version $Rev$, $Date$
  36.  */
  37. public class ConfigurazioneCondizionale {

  38.     private boolean byFilter = true;
  39.     private ConfigurazioneSelettoreCondizione defaultConfig;
  40.     private TreeMap<String, ConfigurazioneSelettoreCondizioneRegola> regolaList = new TreeMap<>();
  41.    
  42.     private IdentificazioneFallitaConfigurazione condizioneNonIdentificata;
  43.     private IdentificazioneFallitaConfigurazione nessunConnettoreTrovato;
  44.    
  45.     public boolean isByFilter() {
  46.         return this.byFilter;
  47.     }
  48.     public void setByFilter(boolean byFilter) {
  49.         this.byFilter = byFilter;
  50.     }
  51.    
  52.     public ConfigurazioneSelettoreCondizione getDefaultConfig() {
  53.         return this.defaultConfig;
  54.     }
  55.     public void setDefaultConfig(ConfigurazioneSelettoreCondizione defaultConfig) {
  56.         this.defaultConfig = defaultConfig;
  57.     }
  58.    
  59.     public void addRegola(ConfigurazioneSelettoreCondizioneRegola config) throws BehaviourException {
  60.         // check che una regola non sia già registrata
  61.         if(config.getRegola()==null) {
  62.             throw new BehaviourException("Nome Regola non indicata");
  63.         }
  64.         if(!this.regolaList.isEmpty()) {
  65.             for (String nomeRegola : this.regolaList.keySet()) {
  66.                 if(nomeRegola.equalsIgnoreCase(config.getRegola())) {
  67.                     throw new BehaviourException("Esiste già una regola con nome '"+nomeRegola+"'");
  68.                 }
  69.             }
  70.         }
  71.         this.regolaList.put(config.getRegola(), config);
  72.     }
  73.    
  74.     public ConfigurazioneSelettoreCondizioneRegola getRegolaByOperazione(String operazione, Resource restResource) throws RegExpException {
  75.         if(!this.regolaList.isEmpty()) {
  76.             for (String nomeRegola: getRegoleOrdinate()) {
  77.                 ConfigurazioneSelettoreCondizioneRegola config = this.regolaList.get(nomeRegola);
  78.                
  79.                 boolean match = isMatch(operazione, restResource, config);
  80.                
  81.                 if(match) {
  82.                     return config;
  83.                 }
  84.             }
  85.         }
  86.         return null;
  87.     }
  88.    
  89.     private boolean isMatch(String operazione, Resource restResource, ConfigurazioneSelettoreCondizioneRegola config) throws RegExpException {
  90.         boolean match = false;
  91.        
  92.         if(operazione.equals(config.getPatternOperazione())) {
  93.             match=true;
  94.         }
  95.        
  96.         if(!match && restResource!=null &&
  97.             config.getPatternOperazione()!=null && !"".equals(config.getPatternOperazione())) {
  98.             String [] parseResourceRest = Utilities.parseResourceRest(config.getPatternOperazione());
  99.             if(parseResourceRest!=null) {
  100.                 match = Utilities.isRestResourceMatch(parseResourceRest, restResource);
  101.             }
  102.         }
  103.        
  104.         if(!match) {
  105.             boolean exprRegular = false;
  106.             try {
  107.                 RegularExpressionEngine.validate(config.getPatternOperazione());
  108.                 exprRegular = true;
  109.             }catch(Exception e) {
  110.                 // ignore
  111.             }
  112.             if(exprRegular) {
  113.                 try {
  114.                     match = RegularExpressionEngine.isMatch(operazione, config.getPatternOperazione());
  115.                 }
  116.                 catch(RegExpNotValidException | RegExpNotFoundException e) {
  117.                     // RegExpNotValidException: non dovrebbe accadere, garantito da validate sopra
  118.                     // RegExpNotFoundException: ignore
  119.                 }
  120.             }
  121.         }
  122.        
  123.         return match;
  124.     }

  125.     public String getNomeRegolaByOperazione(String operazione, Resource restResource) throws RegExpException {
  126.         ConfigurazioneSelettoreCondizioneRegola c = this.getRegolaByOperazione(operazione, restResource);
  127.         return c!=null ? c.getRegola() : null;
  128.     }
  129.     public ConfigurazioneSelettoreCondizioneRegola getRegola(String nomeRegola) {
  130.         return this.regolaList.get(nomeRegola);
  131.     }
  132.     public Set<String> getRegoleOrdinate(){
  133.        
  134.         return this.regolaList.keySet();
  135.     }
  136.     public void removeRegola(String nomeRegola){
  137.         this.regolaList.remove(nomeRegola);
  138.     }
  139.     public int sizeRegole() {
  140.         return this.regolaList.size();
  141.     }
  142.     public TreeMap<String, ConfigurazioneSelettoreCondizioneRegola> getRegolaList() {
  143.         return this.regolaList;
  144.     }
  145.     public void setRegolaList(TreeMap<String, ConfigurazioneSelettoreCondizioneRegola> regolaList) {
  146.         this.regolaList = regolaList;
  147.     }
  148.    
  149.     public IdentificazioneFallitaConfigurazione getCondizioneNonIdentificata() {
  150.         return this.condizioneNonIdentificata;
  151.     }
  152.     public void setCondizioneNonIdentificata(IdentificazioneFallitaConfigurazione condizioneNonIdentificata) {
  153.         this.condizioneNonIdentificata = condizioneNonIdentificata;
  154.     }
  155.     public IdentificazioneFallitaConfigurazione getNessunConnettoreTrovato() {
  156.         return this.nessunConnettoreTrovato;
  157.     }
  158.     public void setNessunConnettoreTrovato(IdentificazioneFallitaConfigurazione nessunConnettoreTrovato) {
  159.         this.nessunConnettoreTrovato = nessunConnettoreTrovato;
  160.     }
  161.    
  162. }