IdentificazionePortaDelegata.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.registry;

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

  24. import org.openspcoop2.core.config.PortaDelegata;
  25. import org.openspcoop2.core.config.constants.PortaDelegataAzioneIdentificazione;
  26. import org.openspcoop2.core.config.constants.StatoFunzionalita;
  27. import org.openspcoop2.core.id.IDPortaDelegata;
  28. import org.openspcoop2.protocol.registry.RegistroServiziManager;
  29. import org.openspcoop2.protocol.sdk.IProtocolFactory;
  30. import org.openspcoop2.protocol.sdk.ProtocolException;
  31. import org.openspcoop2.protocol.sdk.constants.CodiceErroreIntegrazione;
  32. import org.openspcoop2.protocol.sdk.constants.ErroriIntegrazione;
  33. import org.openspcoop2.protocol.sdk.registry.ProtocolFiltroRicercaPorteDelegate;
  34. import org.openspcoop2.protocol.sdk.registry.IConfigIntegrationReader;
  35. import org.openspcoop2.protocol.sdk.registry.IRegistryReader;
  36. import org.openspcoop2.protocol.sdk.registry.RegistryException;
  37. import org.openspcoop2.protocol.sdk.registry.RegistryNotFound;
  38. import org.openspcoop2.protocol.sdk.state.RequestInfo;
  39. import org.openspcoop2.utils.BooleanNullable;
  40. import org.openspcoop2.utils.transport.TransportRequestContext;
  41. import org.slf4j.Logger;


  42. /**
  43.  * Classe utilizzabile per identificare la porta delegata richiesta.
  44.  *
  45.  * @author Andrea Poli (apoli@link.it)
  46.  * @author $Author$
  47.  * @version $Rev$, $Date$
  48.  */

  49. public class IdentificazionePortaDelegata extends AbstractIdentificazionePorta {



  50.     /* ---- Porta Delegata ---- */
  51.     private PortaDelegata pd = null;
  52.    
  53.     /* ---- Porta Delegata (Azione specifica) ---- */
  54.     private HashMap<String, PortaDelegata> pdDelegatedByAction = new HashMap<String, PortaDelegata>();
  55.     private HashMap<String, IDPortaDelegata> idPdDelegatedByAction = new HashMap<String, IDPortaDelegata>();
  56.    
  57.    
  58.     /**
  59.      * Costruttore
  60.      *
  61.      * @param urlProtocolContext Parametri identificativi della porta delegata.
  62.      * @throws ProtocolException
  63.      *
  64.      */
  65.     public IdentificazionePortaDelegata(TransportRequestContext urlProtocolContext, Logger log,
  66.             boolean portaUrlBased, IRegistryReader registryReader, IConfigIntegrationReader configIntegrationReader,
  67.             IProtocolFactory<?> protocolFactory) throws ProtocolException {
  68.         super(urlProtocolContext, log, portaUrlBased, registryReader, configIntegrationReader, protocolFactory);
  69.     }
  70.     /*public IdentificazionePortaDelegata(Logger log, IProtocolFactory<?> protocolFactory, org.openspcoop2.protocol.sdk.state.IState state,
  71.             PortaDelegata pd)
  72.             throws ProtocolException {
  73.         super(log, protocolFactory, state);
  74.         this.pd = pd;
  75.         IDPortaDelegata idPD = new IDPortaDelegata();
  76.         idPD.setNome(this.pd.getNome());
  77.         this.identificativoPorta = idPD;
  78.     }*/
  79.     public IdentificazionePortaDelegata(Logger log, IProtocolFactory<?> protocolFactory,
  80.             RegistroServiziManager registroServiziManager, Object configurazioneManager, RequestInfo requestInfo,
  81.             PortaDelegata pd)
  82.             throws ProtocolException {
  83.         super(log, protocolFactory, registroServiziManager, configurazioneManager, requestInfo);
  84.         this.pd = pd;
  85.         IDPortaDelegata idPD = new IDPortaDelegata();
  86.         idPD.setNome(this.pd.getNome());
  87.         this.identificativoPorta = idPD;
  88.     }
  89.     public IdentificazionePortaDelegata(Logger log, IProtocolFactory<?> protocolFactory,
  90.             IRegistryReader registryReader, IConfigIntegrationReader configIntegrationReader, RequestInfo requestInfo,
  91.             PortaDelegata pd)
  92.             throws ProtocolException {
  93.         super(log, protocolFactory, registryReader, configIntegrationReader, requestInfo);
  94.         this.pd = pd;
  95.         IDPortaDelegata idPD = new IDPortaDelegata();
  96.         idPD.setNome(this.pd.getNome());
  97.         this.identificativoPorta = idPD;
  98.     }


  99.     @Override
  100.     protected Object getIDPorta(String porta) throws RegistryNotFound, RegistryException{
  101.         return this.configIntegrationReader.getIdPortaDelegata(porta, this.protocolFactory);
  102.     }
  103.    
  104.     @Override
  105.     protected String enrichPorta(String porta) throws RegistryException{
  106.         try {
  107.             return this.porteNamingUtils.enrichPD(porta);
  108.         }catch(Exception e) {
  109.             throw new RegistryException(e.getMessage(),e);
  110.         }
  111.     }
  112.    
  113.     /**
  114.      * Avvia il processo di identificazione.
  115.      *
  116.      * @return true in caso di identificazione con successo, false altrimenti.
  117.      *
  118.      */
  119.     @Override
  120.     public boolean process() {

  121.         try{

  122.             if(super.process()==false){
  123.                 return false;
  124.             }
  125.            
  126.             IDPortaDelegata idPD = this.getIDPortaDelegata();
  127.            
  128.             // Get Porta Delegata
  129.             try{
  130.                 this.pd = this.configIntegrationReader.getPortaDelegata(idPD);
  131.             }catch(RegistryNotFound notFound){
  132.                 this.erroreIntegrazione =
  133.                         ErroriIntegrazione.ERRORE_401_PORTA_INESISTENTE.
  134.                             getErrore401_PortaInesistente(notFound.getMessage(),idPD.getNome(),this.urlCompleta);
  135.                 return false;
  136.             }
  137.            
  138.             // Verifico che l'azione non sia in modalità delegatedBy
  139.             if(this.pd.getAzione()!=null && PortaDelegataAzioneIdentificazione.DELEGATED_BY.equals(this.pd.getAzione().getIdentificazione())) {
  140.                 this.erroreIntegrazione =
  141.                         ErroriIntegrazione.ERRORE_441_PORTA_NON_INVOCABILE_DIRETTAMENTE.
  142.                             getErrore441_PortaNonInvocabileDirettamente(idPD.getNome(),this.urlCompleta);
  143.                 return false;
  144.             }

  145.             return true;

  146.         }catch(Exception e){
  147.             this.log.error("Identificazione porta delegata non riuscita location["+this.location+"] urlInvocazione["+this.urlCompleta+"]",e);
  148.             try{
  149.                 this.erroreIntegrazione = ErroriIntegrazione.ERRORE_5XX_GENERICO_PROCESSAMENTO_MESSAGGIO.
  150.                         get5XX_ErroreProcessamento(CodiceErroreIntegrazione.CODICE_502_IDENTIFICAZIONE_PORTA);
  151.             }catch(Exception eError){
  152.                 throw new RuntimeException(eError.getMessage(), eError);
  153.             }
  154.             return false;
  155.         }
  156.     }

  157.     public IDPortaDelegata getIDPortaDelegata(){
  158.         return (IDPortaDelegata) this.identificativoPorta;
  159.     }

  160.     public PortaDelegata getPortaDelegata() {
  161.         return this.pd;
  162.     }

  163.     /**
  164.      * Avvia il processo di identificazione della porta per l'azione specifica.
  165.      *
  166.      * @return true in caso di identificazione con successo, false altrimenti.
  167.      *
  168.      */
  169.     public boolean find(String action) {
  170.         try {
  171.            
  172.             if(this.pd.getRicercaPortaAzioneDelegata()!=null && StatoFunzionalita.ABILITATO.equals(this.pd.getRicercaPortaAzioneDelegata())) {
  173.                
  174.                 ProtocolFiltroRicercaPorteDelegate filtroPD = new ProtocolFiltroRicercaPorteDelegate();
  175.                 filtroPD.setNomePortaDelegante(getIDPortaDelegata().getNome());
  176.                 filtroPD.setAzione(action);
  177.                 List<IDPortaDelegata> list = null;
  178.                 IDPortaDelegata idPD = null;
  179.                 boolean useRequestInfo = false;
  180.                 boolean addRequestInfo = false;
  181.                 try {
  182.                     useRequestInfo = this.requestInfo!=null && this.requestInfo.getRequestConfig()!=null;
  183.                     if(useRequestInfo) {
  184.                         BooleanNullable nullConditionsList = BooleanNullable.NULL();
  185.                         list = this.requestInfo.getRequestConfig().getPorteDelegateByFiltroRicerca(filtroPD, nullConditionsList);
  186.                         if(nullConditionsList==null || nullConditionsList.getValue()==null) {
  187.                             list = null;
  188.                         }
  189.                     }
  190.                    
  191.                     if(list==null) {
  192.                         addRequestInfo = true;
  193.                         list = this.configIntegrationReader.findIdPorteDelegate(filtroPD);
  194.                     }
  195.                                        
  196.                 }catch(RegistryNotFound notFound) {
  197.                     list=new ArrayList<>();
  198.                 }
  199.                
  200.                 if(useRequestInfo && addRequestInfo) {
  201.                     this.requestInfo.getRequestConfig().addPorteDelegateByFiltroRicerca(filtroPD, list,
  202.                             this.requestInfo.getIdTransazione());
  203.                 }
  204.                
  205.                 if(list!=null && list.size()>0) {
  206.                     if(list.size()>1) {
  207.                         throw new Exception("Trovate più porte ("+list.size()+") che corrisponde al filtro?? ("+filtroPD+")");
  208.                     }
  209.                     idPD = list.get(0);
  210.                     this.idPdDelegatedByAction.put(action, idPD);
  211.                    
  212.                     try{
  213.                         PortaDelegata pd = this.configIntegrationReader.getPortaDelegata(idPD);
  214.                         this.pdDelegatedByAction.put(action, pd);
  215.                     }catch(RegistryNotFound notFound){
  216.                         this.erroreIntegrazione =
  217.                                 ErroriIntegrazione.ERRORE_401_PORTA_INESISTENTE.
  218.                                     getErrore401_PortaInesistente(notFound.getMessage(),idPD.getNome(),this.urlCompleta);
  219.                         return false;
  220.                     }
  221.                 }
  222.                
  223.             }
  224.            
  225.             return true;
  226.            
  227.         }catch(Exception e){
  228.             this.log.error("Identificazione porta delegata non riuscita per azione specifica ["+action+"], porta delegata di base["+getIDPortaDelegata().getNome()+"]",e);
  229.             try{
  230.                 this.erroreIntegrazione = ErroriIntegrazione.ERRORE_5XX_GENERICO_PROCESSAMENTO_MESSAGGIO.
  231.                         get5XX_ErroreProcessamento(CodiceErroreIntegrazione.CODICE_502_IDENTIFICAZIONE_PORTA);
  232.             }catch(Exception eError){
  233.                 throw new RuntimeException(eError.getMessage(), eError);
  234.             }
  235.             return false;
  236.         }
  237.     }
  238.    
  239.     public IDPortaDelegata getIDPortaDelegata(String action){
  240.         if(this.idPdDelegatedByAction.containsKey(action)) {
  241.             return this.idPdDelegatedByAction.get(action);
  242.         }
  243.         return null;
  244.     }

  245.     public PortaDelegata getPortaDelegata(String action) {
  246.         if(this.pdDelegatedByAction.containsKey(action)) {
  247.             return this.pdDelegatedByAction.get(action);
  248.         }
  249.         return null;
  250.     }
  251. }