SubscriptionConfiguration.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.basic.config;

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

  23. import org.openspcoop2.core.allarmi.Allarme;
  24. import org.openspcoop2.core.allarmi.utils.AllarmiDriverUtils;
  25. import org.openspcoop2.core.config.PortaDelegata;
  26. import org.openspcoop2.core.config.PortaDelegataAzione;
  27. import org.openspcoop2.core.config.constants.PortaDelegataAzioneIdentificazione;
  28. import org.openspcoop2.core.controllo_traffico.AttivazionePolicy;
  29. import org.openspcoop2.core.controllo_traffico.utils.ControlloTrafficoDriverUtils;
  30. import org.openspcoop2.core.id.IDPortaDelegata;
  31. import org.openspcoop2.core.id.IDServizio;
  32. import org.openspcoop2.core.id.IDSoggetto;
  33. import org.openspcoop2.core.mapping.MappingFruizionePortaDelegata;
  34. import org.openspcoop2.core.mapping.SubscriptionUtils;
  35. import org.openspcoop2.protocol.manifest.IntegrationConfiguration;
  36. import org.openspcoop2.protocol.manifest.IntegrationConfigurationResourceIdentificationMode;
  37. import org.openspcoop2.protocol.manifest.constants.ResourceIdentificationType;
  38. import org.openspcoop2.protocol.sdk.ProtocolException;
  39. import org.openspcoop2.protocol.sdk.config.ProtocolSubscription;
  40. import org.openspcoop2.protocol.sdk.constants.ConsoleInterfaceType;
  41. import org.openspcoop2.protocol.sdk.registry.IConfigIntegrationReader;

  42. /**
  43.  * SubscriptionConfiguration
  44.  *
  45.  *
  46.  * @author Poli Andrea (apoli@link.it)
  47.  * @author $Author$
  48.  * @version $Rev$, $Date$
  49.  */

  50. public class SubscriptionConfiguration extends AbstractIntegrationConfiguration {

  51.     /**
  52.      *
  53.      */
  54.     private static final long serialVersionUID = 1L;

  55.     protected SubscriptionConfiguration(IntegrationConfiguration integrationConfiguration) {
  56.         super(integrationConfiguration);
  57.     }

  58.     public static boolean isDescriptionDefault(String descrizione) {
  59.         return descrizione!=null &&
  60.                 (descrizione.startsWith(SubscriptionConfiguration.SUBSCRIPTION_PREFIX) ||
  61.                 descrizione.startsWith(SubscriptionConfiguration.SUBSCRIPTION_PREFIX_2)||
  62.                 descrizione.startsWith(SubscriptionConfiguration.INTERNAL_SUBSCRIPTION_PREFIX));
  63.     }
  64.    
  65.     private static final String SUBSCRIPTION_PREFIX = "Subscription from ";
  66.     private static final String SUBSCRIPTION_PREFIX_2 = "Subscription for service ";
  67.     public ProtocolSubscription createDefaultSubscription(IDSoggetto idFruitore, IDServizio idServizio) throws ProtocolException {
  68.        
  69.         ProtocolSubscription subscription = new ProtocolSubscription();
  70.        
  71.         PortaDelegata portaDelegata = new PortaDelegata();
  72.         portaDelegata.setNome(this.getNome(idServizio, idFruitore, null, null,
  73.                 this.integrationConfiguration.getName().getParamList()));
  74.         portaDelegata.setDescrizione(SUBSCRIPTION_PREFIX+idFruitore.toString()+" for service "+idServizio.toString());
  75.         if(portaDelegata.getDescrizione().length()>255) {
  76.             portaDelegata.setDescrizione(SUBSCRIPTION_PREFIX_2+idServizio.toString());
  77.         }
  78.         if(portaDelegata.getDescrizione().length()>255) {
  79.             portaDelegata.setDescrizione(null);
  80.         }
  81.        
  82.         IDPortaDelegata idPortaDelegata = SubscriptionUtils.setCommonParameter(portaDelegata, idFruitore, idServizio, true, false);

  83.         PortaDelegataAzione pdAzione = new PortaDelegataAzione();
  84.         ResourceIdentificationType defaultIdentification = this.integrationConfiguration.getResourceIdentification().getIdentificationModes().getDefault();
  85.         if(defaultIdentification==null) {
  86.             defaultIdentification = this.integrationConfiguration.getResourceIdentification().getIdentificationModes().getMode(0).getName();
  87.         }
  88.         boolean setPattern = false;
  89.         switch (defaultIdentification) {
  90.         case CONTENT:
  91.             pdAzione.setIdentificazione(PortaDelegataAzioneIdentificazione.CONTENT_BASED);      
  92.             setPattern = true;
  93.             break;
  94.         case HEADER:
  95.             pdAzione.setIdentificazione(PortaDelegataAzioneIdentificazione.HEADER_BASED);      
  96.             setPattern = true;
  97.             break;
  98.         case URL:
  99.             pdAzione.setIdentificazione(PortaDelegataAzioneIdentificazione.URL_BASED);      
  100.             setPattern = true;
  101.             break;
  102.         case INPUT:
  103.             pdAzione.setIdentificazione(PortaDelegataAzioneIdentificazione.INPUT_BASED);        
  104.             break;
  105.         case INTERFACE:
  106.             pdAzione.setIdentificazione(PortaDelegataAzioneIdentificazione.INTERFACE_BASED);        
  107.             break;
  108.         case SOAP_ACTION:
  109.             pdAzione.setIdentificazione(PortaDelegataAzioneIdentificazione.SOAP_ACTION_BASED);      
  110.             break;
  111.         case PROTOCOL:
  112.             throw new ProtocolException("IdentificationMode '"+defaultIdentification+"' unsupported");
  113.         }
  114.         if(setPattern) {
  115.             pdAzione.setPattern(this.getNome(idServizio, idFruitore, portaDelegata.getNome(), null,
  116.                     this.integrationConfiguration.getResourceIdentification().getIdentificationParameter().getParamList()));
  117.         }      
  118.         if(this.integrationConfiguration.getResourceIdentification().getIdentificationModes().isForceInterfaceMode()) {
  119.             pdAzione.setForceInterfaceBased(org.openspcoop2.core.config.constants.StatoFunzionalita.ABILITATO);
  120.         }
  121.         else {
  122.             pdAzione.setForceInterfaceBased(org.openspcoop2.core.config.constants.StatoFunzionalita.DISABILITATO);
  123.         }
  124.         portaDelegata.setAzione(pdAzione);

  125.         portaDelegata.setRicercaPortaAzioneDelegata(org.openspcoop2.core.config.constants.StatoFunzionalita.ABILITATO);
  126.        
  127.         subscription.setPortaDelegata(portaDelegata);      

  128.         MappingFruizionePortaDelegata mappingFruizione = SubscriptionUtils.createMappingDefault(idFruitore, idServizio, idPortaDelegata);
  129.        
  130.         subscription.setMapping(mappingFruizione);
  131.        
  132.         return subscription;
  133.        
  134.        
  135.     }
  136.    
  137.     public List<PortaDelegataAzioneIdentificazione> supportedIdentificationModes(ConsoleInterfaceType consoleType) throws ProtocolException{
  138.         List<PortaDelegataAzioneIdentificazione> list = new ArrayList<>();
  139.         for (IntegrationConfigurationResourceIdentificationMode mode :
  140.             this.integrationConfiguration.getResourceIdentification().getIdentificationModes().getModeList()) {
  141.            
  142.             if(mode.isOnlyAdvancedMode() &&
  143.                 ConsoleInterfaceType.STANDARD.equals(consoleType)) {
  144.                 continue;
  145.             }
  146.            
  147.             ResourceIdentificationType type = mode.getName();
  148.             switch (type) {
  149.             case CONTENT:
  150.                 list.add(PortaDelegataAzioneIdentificazione.CONTENT_BASED);    
  151.                 break;
  152.             case HEADER:
  153.                 list.add(PortaDelegataAzioneIdentificazione.HEADER_BASED);      
  154.                 break;
  155.             case URL:
  156.                 list.add(PortaDelegataAzioneIdentificazione.URL_BASED);    
  157.                 break;
  158.             case INPUT:
  159.                 list.add(PortaDelegataAzioneIdentificazione.INPUT_BASED);      
  160.                 break;
  161.             case INTERFACE:
  162.                 list.add(PortaDelegataAzioneIdentificazione.INTERFACE_BASED);      
  163.                 break;
  164.             case SOAP_ACTION:
  165.                 list.add(PortaDelegataAzioneIdentificazione.SOAP_ACTION_BASED);    
  166.                 break;
  167.             case PROTOCOL:
  168.                 throw new ProtocolException("IdentificationMode '"+type+"' unsupported");
  169.             }
  170.         }
  171.         return list;
  172.     }
  173.    
  174.     public ProtocolSubscription createSubscription(IConfigIntegrationReader configIntegrationReader, IDSoggetto idFruitore, IDServizio idServizio,
  175.             PortaDelegata portaDelegataDefault,
  176.             String ruleName, String description, String ... azione ) throws ProtocolException {
  177.         return createSubscription(configIntegrationReader, idFruitore, idServizio,
  178.                 portaDelegataDefault, null,
  179.                 ruleName, description, azione);
  180.     }
  181.     private static final String INTERNAL_SUBSCRIPTION_PREFIX = "Internal Subscription '";
  182.     public ProtocolSubscription createSubscription(IConfigIntegrationReader configIntegrationReader, IDSoggetto idFruitore, IDServizio idServizio,
  183.             PortaDelegata portaDelegataDefault, PortaDelegata portaDelegataDaClonare,
  184.             String ruleName, String description, String ... azione ) throws ProtocolException {
  185.        
  186.         if(azione==null || azione.length<=0) {
  187.             throw new ProtocolException("Actions undefined");
  188.         }
  189.        
  190.         String nomePortaDelegante = portaDelegataDefault.getNome();
  191.         String nomeNuovaPortaDelegata = this.getNome(idServizio, idFruitore, nomePortaDelegante, ruleName,
  192.                 this.integrationConfiguration.getResourceIdentification().getSpecificResource().getName().getParamList());
  193.         String descrizioneNuovaPortaDelegata = INTERNAL_SUBSCRIPTION_PREFIX+ruleName+"' for "+nomePortaDelegante;  
  194.         if(descrizioneNuovaPortaDelegata.length()>255) {
  195.             descrizioneNuovaPortaDelegata = INTERNAL_SUBSCRIPTION_PREFIX+ruleName+"'";
  196.         }
  197.         if(descrizioneNuovaPortaDelegata.length()>255) {
  198.             descrizioneNuovaPortaDelegata = null;
  199.         }
  200.        
  201.         ProtocolSubscription subscription = new ProtocolSubscription();
  202.        
  203.         PortaDelegata portaDelegata = null;
  204.        
  205.         // creo una nuova porta applicativa clonando quella selezionata
  206.         boolean setDatiServizio = false;
  207.         boolean portaClonata = false;
  208.         if(portaDelegataDaClonare!=null) {
  209.            
  210.             portaDelegata = (PortaDelegata) portaDelegataDaClonare.clone();
  211.             portaDelegata.setId(null);// annullo il table id
  212.             portaDelegata.setGestioneCors(null); // annulla la gestione Cors poiche' gestito solo nella porta di default
  213.             portaDelegata.setCanale(null); // annullo il canale poiche' gestito solo nella porta di default
  214.            
  215.             IDPortaDelegata idPD = new IDPortaDelegata();
  216.             idPD.setNome(portaDelegataDaClonare.getNome());
  217.                        
  218.             // riporto Rate Limiting
  219.             List<AttivazionePolicy> listAP = null;
  220.             try {
  221.                 listAP = configIntegrationReader.getRateLimitingPolicy(idPD);
  222.             }catch(Exception e) {
  223.                 // ignore
  224.             }
  225.             List<String> idPolicyCreate = new ArrayList<>();
  226.             if(listAP!=null && !listAP.isEmpty()) {
  227.                 for (AttivazionePolicy attivazionePolicy : listAP) {
  228.                    
  229.                     AttivazionePolicy apCloned = (AttivazionePolicy) attivazionePolicy.clone();
  230.                     if(apCloned.getIdPolicy()!=null && apCloned.getFiltro()!=null && portaDelegataDaClonare.getNome().equals(apCloned.getFiltro().getNomePorta())){
  231.                         try {
  232.                             apCloned.getFiltro().setNomePorta(nomeNuovaPortaDelegata);
  233.                             String serialId = configIntegrationReader.getNextPolicyInstanceSerialId(apCloned.getIdPolicy());
  234.                             String idActive = ControlloTrafficoDriverUtils.buildIdActivePolicy(apCloned.getIdPolicy(), serialId);
  235.                             int limit = 0;
  236.                             while(idPolicyCreate.contains(idActive) && limit<1000) { // provo 1000 volte
  237.                                 limit++;
  238.                                 serialId = ControlloTrafficoDriverUtils.incrementPolicyInstanceSerialId(serialId);
  239.                                 idActive = ControlloTrafficoDriverUtils.buildIdActivePolicy(apCloned.getIdPolicy(), serialId);
  240.                             }
  241.                             idPolicyCreate.add(idActive);
  242.                             apCloned.setIdActivePolicy(idActive);
  243.                            
  244.                             if(subscription.getRateLimitingPolicies()==null) {
  245.                                 subscription.setRateLimitingPolicies(new ArrayList<>());
  246.                             }
  247.                             subscription.getRateLimitingPolicies().add(apCloned);
  248.                            
  249.                         }catch(Exception e) {
  250.                             // ignore
  251.                         }
  252.                     }
  253.                 }
  254.             }
  255.            
  256.             // riporto Allarmi
  257.             List<Allarme> listAllarmi = null;
  258.             try {
  259.                 listAllarmi = configIntegrationReader.getAllarmi(idPD);
  260.             }catch(Exception e) {
  261.                 // ignore
  262.             }
  263.             List<String> idAllarmiCreate = new ArrayList<>();
  264.             if(listAllarmi!=null && !listAllarmi.isEmpty()) {
  265.                 for (Allarme allarme : listAllarmi) {
  266.                    
  267.                     Allarme allarmeCloned = (Allarme) allarme.clone();
  268.                     if(allarmeCloned.getTipo()!=null && allarmeCloned.getFiltro()!=null && portaDelegataDaClonare.getNome().equals(allarmeCloned.getFiltro().getNomePorta())){
  269.                         try {
  270.                             allarmeCloned.getFiltro().setNomePorta(nomeNuovaPortaDelegata);
  271.                             String serialId = configIntegrationReader.getNextAlarmInstanceSerialId(allarmeCloned.getTipo());
  272.                             String uniqueName = AllarmiDriverUtils.buildIdAlarm(allarmeCloned.getTipo(), serialId);
  273.                             int limit = 0;
  274.                             while(idAllarmiCreate.contains(uniqueName) && limit<1000) { // provo 1000 volte
  275.                                 limit++;
  276.                                 serialId = AllarmiDriverUtils.incrementAlarmInstanceSerialId(serialId);
  277.                                 uniqueName = AllarmiDriverUtils.buildIdAlarm(allarmeCloned.getTipo(), serialId);
  278.                             }
  279.                             idAllarmiCreate.add(uniqueName);
  280.                             allarmeCloned.setNome(uniqueName);
  281.                            
  282.                             if(subscription.getAllarmi()==null) {
  283.                                 subscription.setAllarmi(new ArrayList<>());
  284.                             }
  285.                             subscription.getAllarmi().add(allarmeCloned);
  286.                            
  287.                         }catch(Exception e) {
  288.                             // ignore
  289.                         }
  290.                     }
  291.                 }
  292.             }
  293.            
  294.             portaClonata = true;
  295.            
  296.         } else {
  297.            
  298.             portaDelegata = new PortaDelegata();
  299.             setDatiServizio = true;    
  300.            
  301.         }

  302.         portaDelegata.setNome(nomeNuovaPortaDelegata);
  303.         portaDelegata.setDescrizione(descrizioneNuovaPortaDelegata);
  304.        
  305.         IDPortaDelegata idPortaDelegata = SubscriptionUtils.setCommonParameter(portaDelegata, idFruitore, idServizio, setDatiServizio, portaClonata);
  306.        
  307.         SubscriptionUtils.setAzioneDelegate(portaDelegata, nomePortaDelegante, azione);
  308.                        
  309.         subscription.setPortaDelegata(portaDelegata);      

  310.         MappingFruizionePortaDelegata mappingFruizione = SubscriptionUtils.createMapping(idFruitore, idServizio, idPortaDelegata, ruleName, description);
  311.        
  312.         subscription.setMapping(mappingFruizione);
  313.        
  314.         return subscription;
  315.        
  316.     }
  317. }