IdentificazionePortaApplicativa.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.PortaApplicativa;
  25. import org.openspcoop2.core.config.constants.PortaApplicativaAzioneIdentificazione;
  26. import org.openspcoop2.core.config.constants.StatoFunzionalita;
  27. import org.openspcoop2.core.id.IDPortaApplicativa;
  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.ProtocolFiltroRicercaPorteApplicative;
  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 applicativa richiesta.
  44.  *
  45.  * @author Andrea Poli (apoli@link.it)
  46.  * @author $Author$
  47.  * @version $Rev$, $Date$
  48.  */

  49. public class IdentificazionePortaApplicativa extends AbstractIdentificazionePorta {


  50.     /* ---- Porta Applicativa ---- */
  51.     private PortaApplicativa pa = null;
  52.    
  53.     /* ---- Porta Applicativa (Azione specifica) ---- */
  54.     private HashMap<String, PortaApplicativa> paDelegatedByAction = new HashMap<String, PortaApplicativa>();
  55.     private HashMap<String, IDPortaApplicativa> idPaDelegatedByAction = new HashMap<String, IDPortaApplicativa>();
  56.    
  57.    
  58.     /**
  59.      * Costruttore
  60.      *
  61.      * @param urlProtocolContext Parametri identificativi della porta applicativa.
  62.      * @throws ProtocolException
  63.      *
  64.      */
  65.     public IdentificazionePortaApplicativa(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 IdentificazionePortaApplicativa(Logger log, IProtocolFactory<?> protocolFactory, org.openspcoop2.protocol.sdk.state.IState state,
  71.             PortaApplicativa pa)
  72.             throws ProtocolException {
  73.         super(log, protocolFactory, state);
  74.         this.pa = pa;
  75.         IDPortaApplicativa idPA = new IDPortaApplicativa();
  76.         idPA.setNome(this.pa.getNome());
  77.         this.identificativoPorta = idPA;
  78.     }*/
  79.     public IdentificazionePortaApplicativa(Logger log, IProtocolFactory<?> protocolFactory,
  80.             RegistroServiziManager registroServiziManager, Object configurazioneManager, RequestInfo requestInfo,
  81.             PortaApplicativa pa)
  82.             throws ProtocolException {
  83.         super(log, protocolFactory, registroServiziManager, configurazioneManager, requestInfo);
  84.         this.pa = pa;
  85.         IDPortaApplicativa idPA = new IDPortaApplicativa();
  86.         idPA.setNome(this.pa.getNome());
  87.         this.identificativoPorta = idPA;
  88.     }
  89.     public IdentificazionePortaApplicativa(Logger log, IProtocolFactory<?> protocolFactory,
  90.             IRegistryReader registryReader, IConfigIntegrationReader configIntegrationReader, RequestInfo requestInfo,
  91.             PortaApplicativa pa)
  92.             throws ProtocolException {
  93.         super(log, protocolFactory, registryReader, configIntegrationReader, requestInfo);
  94.         this.pa = pa;
  95.         IDPortaApplicativa idPA = new IDPortaApplicativa();
  96.         idPA.setNome(this.pa.getNome());
  97.         this.identificativoPorta = idPA;
  98.     }

  99.     @Override
  100.     protected Object getIDPorta(String porta) throws RegistryNotFound, RegistryException{
  101.         return this.configIntegrationReader.getIdPortaApplicativa(porta, this.protocolFactory);
  102.     }

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

  120.         try{

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

  145.             return true;

  146.         }catch(Exception e){
  147.             this.log.error("Identificazione porta applicativa 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 IDPortaApplicativa getIDPortaApplicativa(){
  158.         return (IDPortaApplicativa) this.identificativoPorta;
  159.     }

  160.     public PortaApplicativa getPortaApplicativa() {
  161.         return this.pa;
  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.pa.getRicercaPortaAzioneDelegata()!=null && StatoFunzionalita.ABILITATO.equals(this.pa.getRicercaPortaAzioneDelegata())) {
  173.                
  174.                 ProtocolFiltroRicercaPorteApplicative filtroPA = new ProtocolFiltroRicercaPorteApplicative();
  175.                 filtroPA.setNomePortaDelegante(getIDPortaApplicativa().getNome());
  176.                 filtroPA.setAzione(action);
  177.                 List<IDPortaApplicativa> list = null;
  178.                 IDPortaApplicativa idPA = 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().getPorteApplicativeByFiltroRicerca(filtroPA, 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.findIdPorteApplicative(filtroPA);
  194.                     }
  195.                                        
  196.                 }catch(RegistryNotFound notFound) {
  197.                     list=new ArrayList<>();
  198.                 }
  199.                
  200.                 if(useRequestInfo && addRequestInfo) {
  201.                     this.requestInfo.getRequestConfig().addPorteApplicativeByFiltroRicerca(filtroPA, 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?? ("+filtroPA+")");
  208.                     }
  209.                     idPA = list.get(0);
  210.                     this.idPaDelegatedByAction.put(action, idPA);
  211.                    
  212.                     try{
  213.                         PortaApplicativa pa = this.configIntegrationReader.getPortaApplicativa(idPA);
  214.                         this.paDelegatedByAction.put(action, pa);
  215.                     }catch(RegistryNotFound notFound){
  216.                         this.erroreIntegrazione =
  217.                                 ErroriIntegrazione.ERRORE_401_PORTA_INESISTENTE.
  218.                                     getErrore401_PortaInesistente(notFound.getMessage(),idPA.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 applicativa non riuscita per azione specifica ["+action+"], porta applicativa di base["+getIDPortaApplicativa().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 IDPortaApplicativa getIDPortaApplicativa(String action){
  240.         if(this.idPaDelegatedByAction.containsKey(action)) {
  241.             return this.idPaDelegatedByAction.get(action);
  242.         }
  243.         return null;
  244.     }

  245.     public PortaApplicativa getPortaApplicativa(String action) {
  246.         if(this.paDelegatedByAction.containsKey(action)) {
  247.             return this.paDelegatedByAction.get(action);
  248.         }
  249.         return null;
  250.     }
  251. }