DynamicConfig.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.dynamic;

  21. import java.util.Map;

  22. import org.openspcoop2.core.id.IDServizio;
  23. import org.openspcoop2.core.id.IDServizioApplicativo;
  24. import org.openspcoop2.core.id.IDSoggetto;
  25. import org.openspcoop2.core.registry.driver.IDServizioFactory;
  26. import org.openspcoop2.protocol.sdk.Busta;
  27. import org.openspcoop2.protocol.sdk.state.RequestInfo;
  28. import org.slf4j.Logger;

  29. /**
  30.  * DynamicConfig
  31.  *
  32.  * @author Andrea Poli (apoli@link.it)
  33.  * @author $Author$
  34.  * @version $Rev$, $Date$
  35.  */
  36. public class DynamicConfig {
  37.    
  38.     private Logger log;
  39.     private Map<String, Object> dynamicMap;
  40.     private RequestInfo requestInfo;
  41.    
  42.     private Busta busta;
  43.    
  44.    
  45.     public DynamicConfig(Logger log, Map<String, Object> dynamicMap, RequestInfo requestInfo, Busta busta) {
  46.         this.log = log;
  47.         this.dynamicMap = dynamicMap;
  48.         this.requestInfo = requestInfo;
  49.         this.busta = busta;
  50.     }
  51.    
  52.    
  53.     private IDServizio configId = null;
  54.     private IDServizio getConfigId(){
  55.         if(this.configId!=null) {
  56.             return this.configId;
  57.         }
  58.         if(this.requestInfo!=null && this.requestInfo.getIdServizio()!=null) {
  59.             this.configId = this.requestInfo.getIdServizio();
  60.         }
  61.         else if(this.busta==null && this.dynamicMap!=null && !this.dynamicMap.isEmpty()) {
  62.             Object o = this.dynamicMap.get(Costanti.MAP_BUSTA_OBJECT);
  63.             if(o!=null && o instanceof Busta) {
  64.                 this.busta = (Busta) o;
  65.             }
  66.         }
  67.        
  68.         if(this.configId==null && this.busta!=null) {
  69.             try {
  70.                 this.configId = IDServizioFactory.getInstance().getIDServizioFromValues(this.busta.getTipoServizio(), this.busta.getServizio(),
  71.                         this.busta.getTipoDestinatario(), this.busta.getDestinatario(),
  72.                         this.busta.getVersioneServizio());
  73.             }
  74.             catch(Exception e) {
  75.                 this.log.error("Creazione IDServizio per dynamic config fallita: "+e.getMessage(),e);
  76.             }
  77.         }
  78.        
  79.         return this.configId;
  80.     }
  81.    
  82.     private Map<String, String> mapConfig = null;
  83.     @SuppressWarnings("unchecked")
  84.     private Map<String, String> getMapConfig(){
  85.         if(this.mapConfig!=null) {
  86.             return this.mapConfig;
  87.         }
  88.         if(this.dynamicMap!=null && !this.dynamicMap.isEmpty()) {
  89.             Object o = this.dynamicMap.get(Costanti.MAP_API_IMPL_CONFIG_PROPERTY);
  90.             if(o!=null && o instanceof Map<?, ?>) {
  91.                 this.mapConfig = (Map<String, String>) o;
  92.             }
  93.         }
  94.         return this.mapConfig;
  95.     }
  96.    

  97.    
  98.     private IDSoggetto providerOrganizationId;
  99.     @SuppressWarnings("unused")
  100.     private IDSoggetto getProviderOrganizationId(){
  101.         if(this.providerOrganizationId!=null) {
  102.             return this.providerOrganizationId;
  103.         }
  104.         if(this.requestInfo!=null && this.requestInfo.getIdServizio()!=null && this.requestInfo.getIdServizio().getSoggettoErogatore()!=null) {
  105.             this.providerOrganizationId = this.requestInfo.getIdServizio().getSoggettoErogatore();
  106.         }
  107.         else if(this.busta==null && this.dynamicMap!=null && !this.dynamicMap.isEmpty()) {
  108.             Object o = this.dynamicMap.get(Costanti.MAP_BUSTA_OBJECT);
  109.             if(o!=null && o instanceof Busta) {
  110.                 this.busta = (Busta) o;
  111.             }
  112.         }
  113.         if(this.providerOrganizationId==null && this.busta!=null) {
  114.             try {
  115.                 this.providerOrganizationId = new IDSoggetto(this.busta.getTipoDestinatario(), this.busta.getDestinatario());
  116.             }
  117.             catch(Exception e) {
  118.                 this.log.error("Creazione IDSoggetto provider per dynamic config fallita: "+e.getMessage(),e);
  119.             }
  120.         }
  121.         return this.providerOrganizationId;
  122.     }
  123.     private Map<String, String> mapProviderOrganizationConfig = null;
  124.     @SuppressWarnings("unchecked")
  125.     private Map<String, String> getMapProviderOrganizationConfig(){
  126.         if(this.mapProviderOrganizationConfig!=null) {
  127.             return this.mapProviderOrganizationConfig;
  128.         }
  129.         if(this.dynamicMap!=null && !this.dynamicMap.isEmpty()) {
  130.             Object o = this.dynamicMap.get(Costanti.MAP_SOGGETTO_EROGATORE_CONFIG_PROPERTY);
  131.             if(o!=null && o instanceof Map<?, ?>) {
  132.                 this.mapProviderOrganizationConfig = (Map<String, String>) o;
  133.             }
  134.         }
  135.         return this.mapProviderOrganizationConfig;
  136.     }
  137.    
  138.    
  139.    
  140.    
  141.     private IDSoggetto clientOrganizationId;
  142.     private IDSoggetto getClientOrganizationId(){
  143.         if(this.clientOrganizationId!=null) {
  144.             return this.clientOrganizationId;
  145.         }
  146.         if(this.requestInfo!=null && this.requestInfo.getFruitore()!=null) {
  147.             this.clientOrganizationId = this.requestInfo.getFruitore();
  148.         }
  149.         else if(this.busta==null && this.dynamicMap!=null && !this.dynamicMap.isEmpty()) {
  150.             Object o = this.dynamicMap.get(Costanti.MAP_BUSTA_OBJECT);
  151.             if(o!=null && o instanceof Busta) {
  152.                 this.busta = (Busta) o;
  153.             }
  154.         }
  155.        
  156.         if(this.clientOrganizationId==null && this.busta!=null) {
  157.             this.clientOrganizationId = new IDSoggetto(this.busta.getTipoMittente(), this.busta.getMittente());
  158.         }
  159.        
  160.         return this.clientOrganizationId;
  161.     }
  162.     private Map<String, String> mapClientOrganizationConfig = null;
  163.     @SuppressWarnings("unchecked")
  164.     private Map<String, String> getMapClientOrganizationConfig(){
  165.         if(this.mapClientOrganizationConfig!=null) {
  166.             return this.mapClientOrganizationConfig;
  167.         }
  168.         if(this.dynamicMap!=null && !this.dynamicMap.isEmpty()) {
  169.             Object o = this.dynamicMap.get(Costanti.MAP_SOGGETTO_FRUITORE_CONFIG_PROPERTY);
  170.             if(o!=null && o instanceof Map<?, ?>) {
  171.                 this.mapClientOrganizationConfig = (Map<String, String>) o;
  172.             }
  173.         }
  174.         return this.mapClientOrganizationConfig;
  175.     }
  176.    
  177.     private IDServizioApplicativo clientApplicationId;
  178.     private IDServizioApplicativo getClientApplicationId(){
  179.         if(this.clientApplicationId!=null) {
  180.             return this.clientApplicationId;
  181.         }
  182.        
  183.         if(this.busta==null && this.dynamicMap!=null && !this.dynamicMap.isEmpty()) {
  184.             Object o = this.dynamicMap.get(Costanti.MAP_BUSTA_OBJECT);
  185.             if(o!=null && o instanceof Busta) {
  186.                 this.busta = (Busta) o;
  187.             }
  188.         }
  189.        
  190.         if(this.clientOrganizationId==null && this.busta!=null && this.busta.getServizioApplicativoFruitore()!=null) {
  191.             IDSoggetto organization = getClientOrganizationId();
  192.             this.clientApplicationId = new IDServizioApplicativo();
  193.             this.clientApplicationId.setIdSoggettoProprietario(organization);
  194.             this.clientApplicationId.setNome(this.busta.getServizioApplicativoFruitore());
  195.         }
  196.        
  197.         return this.clientApplicationId;
  198.     }
  199.     private Map<String, String> mapClientApplicationConfig = null;
  200.     @SuppressWarnings("unchecked")
  201.     private Map<String, String> getMapClientApplicationConfig(){
  202.         if(this.mapClientApplicationConfig!=null) {
  203.             return this.mapClientApplicationConfig;
  204.         }
  205.         if(this.dynamicMap!=null && !this.dynamicMap.isEmpty()) {
  206.             Object o = this.dynamicMap.get(Costanti.MAP_APPLICATIVO_CONFIG_PROPERTY);
  207.             if(o!=null && o instanceof Map<?, ?>) {
  208.                 this.mapClientApplicationConfig = (Map<String, String>) o;
  209.             }
  210.         }
  211.         return this.mapClientApplicationConfig;
  212.     }
  213.    
  214.    
  215.    
  216.    
  217.    
  218.    
  219.     private IDSoggetto tokenTokenClientOrganizationId;
  220.     @SuppressWarnings("unused")
  221.     private IDSoggetto getTokenClientOrganizationId(){
  222.         if(this.tokenTokenClientOrganizationId!=null) {
  223.             return this.tokenTokenClientOrganizationId;
  224.         }
  225.         if(this.dynamicMap!=null && !this.dynamicMap.isEmpty()) {
  226.             Object o = this.dynamicMap.get(Costanti.MAP_APPLICATIVO_TOKEN);
  227.             if(o!=null && o instanceof IDServizioApplicativo) {
  228.                 this.tokenTokenClientApplicationId = (IDServizioApplicativo) o;
  229.                 this.tokenTokenClientOrganizationId = this.tokenTokenClientApplicationId.getIdSoggettoProprietario();
  230.             }
  231.         }
  232.        
  233.         return this.tokenTokenClientOrganizationId;
  234.     }
  235.     private Map<String, String> mapTokenClientOrganizationConfig = null;
  236.     @SuppressWarnings("unchecked")
  237.     private Map<String, String> getMapTokenClientOrganizationConfig(){
  238.         if(this.mapTokenClientOrganizationConfig!=null) {
  239.             return this.mapTokenClientOrganizationConfig;
  240.         }
  241.         if(this.dynamicMap!=null && !this.dynamicMap.isEmpty()) {
  242.             Object o = this.dynamicMap.get(Costanti.MAP_SOGGETTO_PROPRIETARIO_APPLICATIVO_TOKEN_CONFIG_PROPERTY);
  243.             if(o!=null && o instanceof Map<?, ?>) {
  244.                 this.mapTokenClientOrganizationConfig = (Map<String, String>) o;
  245.             }
  246.         }
  247.         return this.mapTokenClientOrganizationConfig;
  248.     }
  249.    
  250.     private IDServizioApplicativo tokenTokenClientApplicationId;
  251.     private IDServizioApplicativo getTokenClientApplicationId(){
  252.         if(this.tokenTokenClientApplicationId!=null) {
  253.             return this.tokenTokenClientApplicationId;
  254.         }
  255.        
  256.         if(this.dynamicMap!=null && !this.dynamicMap.isEmpty()) {
  257.             Object o = this.dynamicMap.get(Costanti.MAP_APPLICATIVO_TOKEN);
  258.             if(o!=null && o instanceof IDServizioApplicativo) {
  259.                 this.tokenTokenClientApplicationId = (IDServizioApplicativo) o;
  260.                 this.tokenTokenClientOrganizationId = this.tokenTokenClientApplicationId.getIdSoggettoProprietario();
  261.             }
  262.         }
  263.        
  264.         return this.tokenTokenClientApplicationId;
  265.     }
  266.     private Map<String, String> mapTokenClientApplicationConfig = null;
  267.     @SuppressWarnings("unchecked")
  268.     private Map<String, String> getMapTokenClientApplicationConfig(){
  269.         if(this.mapTokenClientApplicationConfig!=null) {
  270.             return this.mapTokenClientApplicationConfig;
  271.         }
  272.         if(this.dynamicMap!=null && !this.dynamicMap.isEmpty()) {
  273.             Object o = this.dynamicMap.get(Costanti.MAP_APPLICATIVO_TOKEN_CONFIG_PROPERTY);
  274.             if(o!=null && o instanceof Map<?, ?>) {
  275.                 this.mapTokenClientApplicationConfig = (Map<String, String>) o;
  276.             }
  277.         }
  278.         return this.mapTokenClientApplicationConfig;
  279.     }
  280.    
  281.    
  282.    
  283.    
  284.    
  285.    
  286.     // metodi diretti
  287.    
  288.     public String getApi(String pName) throws DynamicException {
  289.         return this.getValue(getMapConfig(), pName);
  290.     }
  291.    
  292.     public String getProviderOrganization(String pName) throws DynamicException {
  293.         return this.getValue(getMapProviderOrganizationConfig(), pName);
  294.     }
  295.    
  296.     public String getClientApplication(String pName) throws DynamicException {
  297.         return this.getValue(getMapClientApplicationConfig(), pName);
  298.     }
  299.    
  300.     public String getClientOrganization(String pName) throws DynamicException {
  301.         return this.getValue(getMapClientOrganizationConfig(), pName);
  302.     }
  303.    
  304.     public String getTokenClientApplication(String pName) throws DynamicException {
  305.         return this.getValue(getMapTokenClientApplicationConfig(), pName);
  306.     }
  307.    
  308.     public String getTokenClientOrganization(String pName) throws DynamicException {
  309.         return this.getValue(getMapTokenClientOrganizationConfig(), pName);
  310.     }
  311.    
  312.    
  313.     // ricerche
  314.    
  315.    
  316.     // ** applicativo client **
  317.    
  318.     public String apiSearchByClientApplication(String pNameParam) throws DynamicException {
  319.        
  320.         IDServizioApplicativo clientApplicationId = getClientApplicationId();
  321.        
  322.         // 1. Cerco nell'api con nome '<clientOrganizationName>.<clientApplicationName>.<pName>'
  323.         // 2. Cerco nella fruizione con nome '<clientApplicationName>.<pName>'
  324.         // 3. Cerco nella fruizione con nome '<clientOrganizationName>.<pName>'
  325.         // 4. Proprietà di default
  326.        
  327.         return apiSearchByClientApplication(pNameParam, clientApplicationId);
  328.     }
  329.    
  330.     public String clientApplicationSearch(String pNameParam) throws DynamicException {
  331.        
  332.         Map<String, String> mapClientApplicationConfig = getMapClientApplicationConfig();
  333.        
  334.         // 1. Cerco nell'applicativo con nome '<nomeErogatore>.<nomeApiImpl>.v<versioneApiImpl>.<pName>'
  335.         // 2. Cerco nell'applicativo con nome '<nomeApiImpl>.v<nomeApiImpl>.<pName>'
  336.         // 3. Cerco nell'applicativo con nome '<nomeErogatore>.<pName>'
  337.         // 4. Proprietà di default <pName>
  338.        
  339.         return searchByAPI(pNameParam, mapClientApplicationConfig);
  340.     }
  341.    
  342.     public String clientOrganizationSearch(String pNameParam) throws DynamicException {
  343.        
  344.         Map<String, String> mapClientOrganizationConfig = getMapClientOrganizationConfig();
  345.        
  346.         // 1. Cerco nel soggetto dell'applicativo con nome '<nomeErogatore>.<nomeApiImpl>.v<versioneApiImpl>.<pName>'
  347.         // 2. Cerco nel soggetto dell'applicativo con nome '<nomeApiImpl>.v<nomeApiImpl>.<pName>'
  348.         // 3. Cerco nel soggetto dell'applicativo con nome '<nomeErogatore>.<pName>'
  349.         // 4. Proprietà di default <pName>
  350.        
  351.         return searchByAPI(pNameParam, mapClientOrganizationConfig);
  352.        
  353.     }
  354.    
  355.    
  356.     // ** applicativo token client **
  357.    
  358.     public String apiSearchByTokenClientApplication(String pNameParam) throws DynamicException {
  359.        
  360.         IDServizioApplicativo tokenClientApplicationId = getTokenClientApplicationId();
  361.        
  362.         // 1. Cerco nell'api con nome '<tokenClientOrganizationName>.<tokenClientApplicationName>.<pName>'
  363.         // 2. Cerco nella fruizione con nome '<tokenClientApplicationName>.<pName>'
  364.         // 3. Cerco nella fruizione con nome '<tokenClientOrganizationName>.<pName>'
  365.         // 4. Proprietà di default
  366.        
  367.         return apiSearchByClientApplication(pNameParam, tokenClientApplicationId);
  368.     }
  369.    
  370.     public String tokenClientApplicationSearch(String pNameParam) throws DynamicException {
  371.        
  372.         Map<String, String> mapTokenClientApplicationConfig = getMapTokenClientApplicationConfig();
  373.        
  374.         // 1. Cerco nell'applicativo con nome '<nomeErogatore>.<nomeApiImpl>.v<versioneApiImpl>.<pName>'
  375.         // 2. Cerco nell'applicativo con nome '<nomeApiImpl>.v<nomeApiImpl>.<pName>'
  376.         // 3. Cerco nell'applicativo con nome '<nomeErogatore>.<pName>'
  377.         // 4. Proprietà di default <pName>
  378.        
  379.         return searchByAPI(pNameParam, mapTokenClientApplicationConfig);
  380.     }
  381.    
  382.     public String tokenClientOrganizationSearch(String pNameParam) throws DynamicException {
  383.        
  384.         Map<String, String> mapTokenClientOrganizationConfig = getMapTokenClientOrganizationConfig();
  385.        
  386.         // 1. Cerco nel soggetto dell'applicativo con nome '<nomeErogatore>.<nomeApiImpl>.v<versioneApiImpl>.<pName>'
  387.         // 2. Cerco nel soggetto dell'applicativo con nome '<nomeApiImpl>.v<nomeApiImpl>.<pName>'
  388.         // 3. Cerco nel soggetto dell'applicativo con nome '<nomeErogatore>.<pName>'
  389.         // 4. Proprietà di default <pName>
  390.        
  391.         return searchByAPI(pNameParam, mapTokenClientOrganizationConfig);
  392.        
  393.     }
  394.    
  395.    
  396.     // ** provider **
  397.    
  398.     public String providerSearch(String pNameParam) throws DynamicException {
  399.        
  400.         Map<String, String> mapProviderOrganizationConfig = getMapProviderOrganizationConfig();
  401.        
  402.         // 1. Cerco nel soggetto erogatore con nome '<nomeApiImpl>.v<nomeApiImpl>.<pName>'
  403.         // 2. Proprietà di default <pName>
  404.        
  405.         return searchByAPIwithoutProvider(pNameParam, mapProviderOrganizationConfig);
  406.        
  407.     }
  408.    
  409.    
  410.    
  411.     // Utility
  412.    
  413.     private String apiSearchByClientApplication(String pNameParam, IDServizioApplicativo idSA) throws DynamicException {
  414.        
  415.         Map<String, String> mapConfig = getMapConfig();
  416.        
  417.         // 1. Cerco nell'api con nome '<clientOrganizationName>.<tokenClientApplicationName>.<pName>'
  418.         if(idSA!=null && idSA.getNome()!=null &&
  419.                 idSA.getIdSoggettoProprietario()!=null && idSA.getIdSoggettoProprietario().getNome()!=null) {
  420.             String pName = idSA.getIdSoggettoProprietario().getNome()+"."+idSA.getNome()+"."+pNameParam;
  421.             String v = this.getValue(mapConfig, pName);
  422.             if(v!=null) {
  423.                 return v;
  424.             }
  425.         }
  426.        
  427.         // 2. Cerco nella fruizione con nome '<clientApplicationName>.<pName>'
  428.         if(idSA!=null && idSA.getNome()!=null) {
  429.             String pName = idSA.getNome()+"."+pNameParam;
  430.             String v = this.getValue(mapConfig, pName);
  431.             if(v!=null) {
  432.                 return v;
  433.             }
  434.         }
  435.        
  436.         // 3. Cerco nella fruizione con nome '<clientOrganizationName>.<pName>'
  437.         if(idSA!=null &&
  438.                 idSA.getIdSoggettoProprietario()!=null && idSA.getIdSoggettoProprietario().getNome()!=null) {
  439.             String pName = idSA.getIdSoggettoProprietario().getNome()+"."+pNameParam;
  440.             String v = this.getValue(mapConfig, pName);
  441.             if(v!=null) {
  442.                 return v;
  443.             }
  444.         }
  445.                
  446.         // 4. Proprietà di default <pName>
  447.         return this.getValue(mapConfig, pNameParam);
  448.     }
  449.    
  450.     private String searchByAPI(String pNameParam, Map<String, String> map) throws DynamicException {
  451.        
  452.         IDServizio configId = this.getConfigId();
  453.        
  454.         // 1. Cerco con nome '<nomeErogatore>.<nomeApiImpl>.v<versioneApiImpl>.<pName>'
  455.         if(configId!=null && configId.getNome()!=null && configId.getVersione()!=null &&
  456.                 configId.getSoggettoErogatore()!=null && configId.getSoggettoErogatore().getNome()!=null) {
  457.             String pName = configId.getSoggettoErogatore().getNome() +"." + configId.getNome() + ".v"+configId.getVersione()+"."+pNameParam;
  458.             String v = this.getValue(map, pName);
  459.             if(v!=null) {
  460.                 return v;
  461.             }
  462.         }
  463.        
  464.         // 2. Cerco con nome '<nomeApiImpl>.v<nomeApiImpl>.<pName>'
  465.         if(configId!=null && configId.getNome()!=null && configId.getVersione()!=null) {
  466.             String pName = configId.getNome() + ".v"+configId.getVersione()+"."+pNameParam;
  467.             String v = this.getValue(map, pName);
  468.             if(v!=null) {
  469.                 return v;
  470.             }
  471.         }
  472.        
  473.         // 3. Cerco con nome '<nomeErogatore>.<pName>'
  474.         if(configId!=null &&
  475.                 configId.getSoggettoErogatore()!=null && configId.getSoggettoErogatore().getNome()!=null) {
  476.             String pName = configId.getSoggettoErogatore().getNome() +"." + pNameParam;
  477.             String v = this.getValue(map, pName);
  478.             if(v!=null) {
  479.                 return v;
  480.             }
  481.         }
  482.        
  483.         // 4. Proprietà di default <pName>
  484.         return this.getValue(map, pNameParam);
  485.     }
  486.    
  487.     private String searchByAPIwithoutProvider(String pNameParam, Map<String, String> map) throws DynamicException {
  488.        
  489.         IDServizio configId = this.getConfigId();
  490.        
  491.         // 1. Cerco con nome '<nomeApiImpl>.v<nomeApiImpl>.<pName>'
  492.         if(configId!=null && configId.getNome()!=null && configId.getVersione()!=null) {
  493.             String pName = configId.getNome() + ".v"+configId.getVersione()+"."+pNameParam;
  494.             String v = this.getValue(map, pName);
  495.             if(v!=null) {
  496.                 return v;
  497.             }
  498.         }
  499.        
  500.         // 2. Proprietà di default <pName>
  501.         return this.getValue(map, pNameParam);
  502.     }
  503.    
  504.     private String getValue(Map<String, String> map, String pName) {
  505.         if(map!=null && !map.isEmpty()) {
  506.             for (String name : map.keySet()) {
  507.                 if(name.equals(pName)) {
  508.                     return map.get(name);
  509.                 }
  510.             }
  511.         }
  512.         return null;
  513.     }
  514. }