AbstractIdentificazionePorta.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 org.openspcoop2.protocol.registry.RegistroServiziManager;
  22. import org.openspcoop2.protocol.sdk.IProtocolFactory;
  23. import org.openspcoop2.protocol.sdk.ProtocolException;
  24. import org.openspcoop2.protocol.sdk.constants.CodiceErroreIntegrazione;
  25. import org.openspcoop2.protocol.sdk.constants.ErroreIntegrazione;
  26. import org.openspcoop2.protocol.sdk.constants.ErroriIntegrazione;
  27. import org.openspcoop2.protocol.sdk.registry.IConfigIntegrationReader;
  28. import org.openspcoop2.protocol.sdk.registry.IRegistryReader;
  29. import org.openspcoop2.protocol.sdk.registry.RegistryException;
  30. import org.openspcoop2.protocol.sdk.registry.RegistryNotFound;
  31. import org.openspcoop2.protocol.sdk.state.RequestInfo;
  32. import org.openspcoop2.protocol.utils.PorteNamingUtils;
  33. import org.openspcoop2.utils.transport.TransportRequestContext;
  34. import org.slf4j.Logger;


  35. /**
  36.  * Classe utilizzabile per identificare il servizio applicativo e la porta delegata richiesta.
  37.  *
  38.  * @author Andrea Poli (apoli@link.it)
  39.  * @author $Author$
  40.  * @version $Rev$, $Date$
  41.  */

  42. public abstract class AbstractIdentificazionePorta {


  43.     /* ---- Location della Porta --- */
  44.     protected String location;
  45.     protected String urlCompleta;
  46.    
  47.     /* ----- Individuazione Nome Porta UrlBased -------- */
  48.     protected boolean nomePortaURLBased = true;
  49.    
  50.     /* ----- Utility per enrich porta -------- */
  51.     protected PorteNamingUtils porteNamingUtils;
  52.    
  53.     /* ---- messaggio di errore --- */
  54.     protected ErroreIntegrazione erroreIntegrazione;

  55.     /* ---- IdentificativoPorta --- */
  56.     protected Object identificativoPorta;
  57.    
  58.     protected IProtocolFactory<?> protocolFactory = null;

  59.     protected IRegistryReader registryReader;
  60.     protected IConfigIntegrationReader configIntegrationReader;
  61.    
  62.     protected RequestInfo requestInfo;

  63.     /* ---- Log ----- */
  64.     protected Logger log;
  65.    
  66.    
  67.     /**
  68.      * Costruttore
  69.      *
  70.      * @param urlProtocolContext Parametri identificativi della porta delegata.
  71.      * @throws ProtocolException
  72.      *
  73.      */
  74.     public AbstractIdentificazionePorta(TransportRequestContext urlProtocolContext, Logger log,
  75.             boolean portaUrlBased, IRegistryReader registryReader, IConfigIntegrationReader configIntegrationReader,
  76.             IProtocolFactory<?> protocolFactory) throws ProtocolException {
  77.         this.location = urlProtocolContext.getFunctionParameters();
  78.         this.urlCompleta = urlProtocolContext.getUrlInvocazione_formBased();
  79.         this.nomePortaURLBased = portaUrlBased;
  80.         this.protocolFactory = protocolFactory;
  81.         this.log = log;
  82.         this.registryReader = registryReader;
  83.         this.configIntegrationReader = configIntegrationReader;
  84.         this.porteNamingUtils = new PorteNamingUtils(this.protocolFactory);
  85.     }
  86.    
  87.     /*public AbstractIdentificazionePorta(Logger log,
  88.             IProtocolFactory<?> protocolFactory, org.openspcoop2.protocol.sdk.state.IState state) throws ProtocolException {
  89.         this.protocolFactory = protocolFactory;
  90.         this.log = log;
  91.         this.registryReader = this.protocolFactory.getCachedRegistryReader(state);
  92.         this.configIntegrationReader = this.protocolFactory.getCachedConfigIntegrationReader(state);
  93.     }*/
  94.     public AbstractIdentificazionePorta(Logger log,
  95.             IProtocolFactory<?> protocolFactory, RegistroServiziManager registroServiziManager, Object configurazioneManager, RequestInfo requestInfo) throws ProtocolException {
  96.         this.protocolFactory = protocolFactory;
  97.         this.log = log;
  98.         this.registryReader = this.protocolFactory.getCachedRegistryReader(registroServiziManager, requestInfo);
  99.         this.configIntegrationReader = this.protocolFactory.getCachedConfigIntegrationReader(configurazioneManager, requestInfo);
  100.         this.requestInfo = requestInfo;
  101.     }
  102.     public AbstractIdentificazionePorta(Logger log,
  103.             IProtocolFactory<?> protocolFactory,
  104.             IRegistryReader registryReader, IConfigIntegrationReader configIntegrationReader, RequestInfo requestInfo) throws ProtocolException {
  105.         this.protocolFactory = protocolFactory;
  106.         this.log = log;
  107.         this.registryReader = registryReader;
  108.         this.configIntegrationReader = configIntegrationReader;
  109.         this.requestInfo = requestInfo;
  110.     }



  111.     protected abstract Object getIDPorta(String porta) throws RegistryNotFound, RegistryException;
  112.    
  113.     protected abstract String enrichPorta(String porta) throws RegistryException;

  114.     /**
  115.      * Avvia il processo di identificazione.
  116.      *
  117.      * @return true in caso di identificazione con successo, false altrimenti.
  118.      *
  119.      */
  120.     public boolean process() {

  121.         try{

  122.             if(this.location==null || "".equals(this.location)){
  123.                 this.erroreIntegrazione =
  124.                         ErroriIntegrazione.ERRORE_401_PORTA_INESISTENTE.
  125.                             getErrore401_PortaInesistente("nella url di invocazione non e' stata fornita il nome di una Porta");
  126.                 return false;
  127.             }
  128.            
  129.             // Controllo Esistenza Porta appartenente ad un soggetto.
  130.             // Viene controllata la url 'scalandola' di '/'
  131.             if(this.nomePortaURLBased){
  132.                 String porta = new String(this.location);
  133.                 if(porta.endsWith("/"))
  134.                     porta = porta.substring(0,porta.length()-1);
  135.                 while(porta.contains("/")){
  136.                     //this.log.info("Cerco con nome porta delegata ["+porta+"]");
  137.                     try{
  138.                         this.identificativoPorta = getIDPorta(porta);
  139.                     }catch(RegistryNotFound dNotFound){}
  140.                     if(this.identificativoPorta!=null){
  141.                         //this.log.info("TROVATA porta delegata ["+porta+"]");
  142.                         break;
  143.                     }else{
  144.                        
  145.                         String enrichPorta = this.enrichPorta(porta);
  146.                         try{
  147.                             this.identificativoPorta = getIDPorta(enrichPorta);
  148.                         }catch(RegistryNotFound dNotFound){}
  149.                         if(this.identificativoPorta!=null){
  150.                             //this.log.info("TROVATA porta delegata con enrich ["+enrichPorta+"]");
  151.                             break;
  152.                         }else{
  153.                        
  154.                             int indexCut = -1;
  155.                             for(int i=(porta.length()-1); i>=0 ;i--){
  156.                                 if(porta.charAt(i) == '/'){
  157.                                     indexCut = i;
  158.                                     break;
  159.                                 }
  160.                             }
  161.                             //this.log.info("indexCut = "+indexCut);
  162.                             //this.log.info("elimino parte '/'");
  163.                             porta = porta.substring(0,indexCut);
  164.                             //this.log.info("dopo eliminazione: "+porta);
  165.                         }
  166.                     }
  167.                 }
  168.                 // Provo ad effettuare ultima ricerca
  169.                 //this.log.info("ULTIMA... Cerco con nome porta delegata ["+porta+"]");
  170.                 if(this.identificativoPorta==null){
  171.                     try{
  172.                         this.identificativoPorta = getIDPorta(porta);
  173.                     }catch(RegistryNotFound dNotFound){}
  174.                 }
  175.                 if(this.identificativoPorta!=null){
  176.                     //this.log.info("ULTIMA....TROVATA porta delegata ["+porta+"]");
  177.                 }
  178.             }
  179.            
  180.             if(this.identificativoPorta==null){
  181.                 //log.info("Cerco con nome giusto porta delegata ["+this.location+"]");
  182.                 // check per nome PD univoco
  183.                 try{
  184.                     this.identificativoPorta = getIDPorta(this.location);
  185.                 }catch(RegistryNotFound dNotFound){
  186.                     this.erroreIntegrazione =
  187.                             ErroriIntegrazione.ERRORE_401_PORTA_INESISTENTE.
  188.                                 getErrore401_PortaInesistente("verificare i parametri di accesso utilizzati",this.location,this.urlCompleta);
  189.                     this.log.error(this.erroreIntegrazione.getDescrizione(this.protocolFactory)+": "+dNotFound.getMessage(),dNotFound);
  190.                     return false;
  191.                 }  
  192.                 if(this.identificativoPorta!=null){
  193.                     //log.info("TROVATO con nome giusto porta delegata ["+this.location+"]");
  194.                 }
  195.             }
  196.            
  197.            
  198.             if(this.identificativoPorta == null){
  199.                 this.erroreIntegrazione =
  200.                         ErroriIntegrazione.ERRORE_401_PORTA_INESISTENTE.
  201.                             getErrore401_PortaInesistente("verificare i parametri di accesso utilizzati",this.location,this.urlCompleta);
  202.                 return false;
  203.             }

  204.             return true;

  205.         }catch(Exception e){
  206.             this.log.error("Identificazione porta non riuscita location["+this.location+"] urlInvocazione["+this.urlCompleta+"]",e);
  207.             try{
  208.                 this.erroreIntegrazione = ErroriIntegrazione.ERRORE_5XX_GENERICO_PROCESSAMENTO_MESSAGGIO.
  209.                         get5XX_ErroreProcessamento(CodiceErroreIntegrazione.CODICE_502_IDENTIFICAZIONE_PORTA);
  210.             }catch(Exception eError){
  211.                 throw new RuntimeException(eError.getMessage(), eError);
  212.             }
  213.             return false;
  214.         }
  215.     }



  216.     public CodiceErroreIntegrazione getCodiceErrore(){
  217.         return this.erroreIntegrazione.getCodiceErrore();
  218.     }
  219.    
  220.     public String getMessaggioErrore() throws ProtocolException {
  221.         return this.erroreIntegrazione.getDescrizione(this.protocolFactory);
  222.     }
  223.    
  224.     public ErroreIntegrazione getErroreIntegrazione() {
  225.         return this.erroreIntegrazione;
  226.     }


  227. }