ModIAuditConfig.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.modipa.config;

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

  24. import org.apache.commons.lang.StringUtils;
  25. import org.openspcoop2.protocol.sdk.ProtocolException;

  26. /**
  27.  * ModIAuditConfig
  28.  *
  29.  * @author Poli Andrea (apoli@link.it)
  30.  * @author $Author$
  31.  * @version $Rev$, $Date$
  32.  */
  33. public class ModIAuditConfig {

  34.     public static final String PROPERTY_NOME = "nome";
  35.     public static final String PROPERTY_LABEL = "label";
  36.     public static final String PROPERTY_CLAIMS = "claims";
  37.    
  38.     public static final String PROPERTY_ISS_LOCALE = "iss.locale";
  39.     public static final String PROPERTY_ISS_OAUTH = "iss.oauth";
  40.    
  41.     public static final String PROPERTY_SUB_LOCALE = "sub.locale";
  42.     public static final String PROPERTY_SUB_OAUTH = "sub.oauth";
  43.    
  44.     public static final String PROPERTY_CLIENT_ID_LOCALE = "client_id.locale";
  45.     public static final String PROPERTY_CLIENT_ID_OAUTH = "client_id.oauth";
  46.    
  47.     private String propertyId;
  48.    
  49.     private String nome;
  50.     private String label;
  51.     private List<ModIAuditClaimConfig> claims;
  52.    
  53.     private boolean issLocale = false;
  54.     private boolean issOAuth = true;
  55.    
  56.     private boolean subLocale = false;
  57.     private boolean subOAuth = false;
  58.    
  59.     private boolean clientIdLocale = false;
  60.     private boolean clientIdOAuth = false;
  61.    
  62.     public ModIAuditConfig copyNewInstance() {
  63.         ModIAuditConfig config = new ModIAuditConfig();
  64.        
  65.         config.propertyId = this.propertyId;
  66.        
  67.         config.nome = this.nome;
  68.         config.label = this.label;
  69.        
  70.         config.claims = new ArrayList<>();
  71.         if(this.claims!=null && !this.claims.isEmpty()) {          
  72.             for (ModIAuditClaimConfig modIAuditClaimConfig : this.claims) {
  73.                 config.claims.add(modIAuditClaimConfig.copyNewInstance());
  74.             }
  75.         }
  76.        
  77.         config.issLocale = this.issLocale;
  78.         config.issOAuth = this.issOAuth;
  79.        
  80.         config.subLocale = this.subLocale;
  81.         config.subOAuth = this.subOAuth;
  82.        
  83.         config.clientIdLocale = this.clientIdLocale;
  84.         config.clientIdOAuth = this.clientIdOAuth;
  85.        
  86.         return config;
  87.     }
  88.    
  89.     private ModIAuditConfig() {}
  90.     ModIAuditConfig(String prefix, String propertyId, Properties p) throws ProtocolException {
  91.        
  92.         this.propertyId = propertyId;
  93.        
  94.         this.nome = getProperty(prefix, p, PROPERTY_NOME, true);
  95.         this.label = getProperty(prefix, p, PROPERTY_LABEL, true);
  96.        
  97.         String claimsP = getProperty(prefix, p, PROPERTY_CLAIMS, true);
  98.         String [] tmp = claimsP.split(",");
  99.         if(tmp==null || tmp.length<=0) {
  100.             throw new ProtocolException("Property '"+PROPERTY_CLAIMS+"' empty");
  101.         }
  102.         this.claims = new ArrayList<>();
  103.         for (String c : tmp) {
  104.             c = c.trim();
  105.             this.claims.add(new ModIAuditClaimConfig(prefix, c, p));
  106.         }
  107.        
  108.         // Verifico univocita
  109.         if(this.claims!=null && !this.claims.isEmpty()) {
  110.             for (ModIAuditClaimConfig modIAuditClaimConfig : this.claims) {
  111.                 String name = modIAuditClaimConfig.getNome();
  112.                 int count = 0;
  113.                 for (ModIAuditClaimConfig modIAuditClaimConfigCheck : this.claims) {
  114.                     if(name.equals(modIAuditClaimConfigCheck.getNome())) {
  115.                         count++;
  116.                     }
  117.                 }
  118.                 if(count>1) {
  119.                     throw new ProtocolException("Property "+PROPERTY_CLAIMS+".xx."+ModIAuditClaimConfig.PROPERTY_NOME+"="+name+" defined more then one time ("+count+")");
  120.                 }
  121.             }
  122.         }
  123.        
  124.         this.issLocale = getBooleanProperty(prefix, p, PROPERTY_ISS_LOCALE, false, false);
  125.         this.issOAuth = getBooleanProperty(prefix, p, PROPERTY_ISS_OAUTH, false, true);
  126.        
  127.         this.subLocale = getBooleanProperty(prefix, p, PROPERTY_SUB_LOCALE, false, false);
  128.         this.subOAuth = getBooleanProperty(prefix, p, PROPERTY_SUB_OAUTH, false, false);
  129.        
  130.         this.clientIdLocale = getBooleanProperty(prefix, p, PROPERTY_CLIENT_ID_LOCALE, false, false);
  131.         this.clientIdOAuth = getBooleanProperty(prefix, p, PROPERTY_CLIENT_ID_OAUTH, false, false);
  132.     }
  133.    
  134.    
  135.    
  136.     static String getProperty(String prefixProperty, Properties p, String name, boolean required) throws ProtocolException {
  137.         String tmp = p.getProperty(name);
  138.         if(tmp!=null) {
  139.             return tmp.trim();
  140.         }
  141.         else {
  142.             if(required) {
  143.                 throw new ProtocolException("Property '"+prefixProperty+"."+name+"' notFound");
  144.             }
  145.             return null;
  146.         }
  147.     }
  148.     static boolean getBooleanProperty(String prefixProperty, Properties p, String name, boolean required, boolean defaultValue) throws ProtocolException {
  149.         String tmp = getProperty(prefixProperty, p, name, required);
  150.         if(tmp!=null && StringUtils.isNotEmpty(tmp)) {
  151.             try {
  152.                 return Boolean.valueOf(tmp);
  153.             }catch(Exception t) {
  154.                 throw new ProtocolException("Boolean property '"+prefixProperty+"."+name+"' invalid (found value:["+tmp+"]): "+t.getMessage(),t);
  155.             }
  156.         }
  157.         return defaultValue;
  158.     }
  159.     static int getIntProperty(String prefixProperty, Properties p, String name, boolean required, int defaultValue) throws ProtocolException {
  160.         String tmp = getProperty(prefixProperty, p, name, required);
  161.         if(tmp!=null && StringUtils.isNotEmpty(tmp)) {
  162.             try {
  163.                 return Integer.valueOf(tmp);
  164.             }catch(Exception t) {
  165.                 throw new ProtocolException("Int property '"+prefixProperty+"."+name+"' invalid (found value:["+tmp+"]): "+t.getMessage(),t);
  166.             }
  167.         }
  168.         return defaultValue;
  169.     }
  170.    
  171.     public String getPropertyId() {
  172.         return this.propertyId;
  173.     }
  174.    
  175.     public String getNome() {
  176.         return this.nome;
  177.     }

  178.     public String getLabel() {
  179.         return this.label;
  180.     }

  181.     public List<ModIAuditClaimConfig> getClaims() {
  182.         return this.claims;
  183.     }
  184.     public void setClaims(List<ModIAuditClaimConfig> claims) {
  185.         this.claims = claims;
  186.     }

  187.     public boolean isIssLocale() {
  188.         return this.issLocale;
  189.     }

  190.     public boolean isIssOAuth() {
  191.         return this.issOAuth;
  192.     }

  193.     public boolean isSubLocale() {
  194.         return this.subLocale;
  195.     }

  196.     public boolean isSubOAuth() {
  197.         return this.subOAuth;
  198.     }

  199.     public boolean isClientIdLocale() {
  200.         return this.clientIdLocale;
  201.     }

  202.     public boolean isClientIdOAuth() {
  203.         return this.clientIdOAuth;
  204.     }
  205. }