IDServizioFactory.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.core.registry.driver;

  21. import org.openspcoop2.core.id.IDServizio;
  22. import org.openspcoop2.core.id.IDSoggetto;
  23. import org.openspcoop2.core.registry.AccordoServizioParteSpecifica;
  24. import org.openspcoop2.core.registry.constants.TipologiaServizio;

  25. /**
  26.  * IDServizioFactory
  27.  *
  28.  *
  29.  * @author Poli Andrea (apoli@link.it)
  30.  * @author $Author$
  31.  * @version $Rev$, $Date$
  32.  */
  33. public class IDServizioFactory {

  34.     private static final String TIPO_NON_FORNITO = "Tipo non fornito";
  35.     private static final String NOME_NON_FORNITO = "Nome non fornito";
  36.     private static final String SOGGETTO_EROGATORE_NON_FORNITO = "Soggetto erogatore non fornito";
  37.     private static final String TIPO_SOGGETTO_EROGATORE_NON_FORNITO = "Tipo soggetto erogatore non fornito";
  38.     private static final String NOME_SOGGETTO_EROGATORE_NON_FORNITO = "Nome soggetto erogatore non fornito";
  39.     private static final String SINTASSI_NON_CORRETTA = "sintassi non corretta, atteso tipoSoggetto/nomeSoggetto:tipoServizio/nomeServizio:versione";
  40.    
  41.     private static IDServizioFactory factory = null;
  42.     private static synchronized void init(){
  43.         if(IDServizioFactory.factory==null){
  44.             IDServizioFactory.factory = new IDServizioFactory();
  45.         }
  46.     }
  47.     public static IDServizioFactory getInstance(){
  48.         if(IDServizioFactory.factory==null){
  49.             // spotbugs warning 'SING_SINGLETON_GETTER_NOT_SYNCHRONIZED'
  50.             synchronized (IDServizioFactory.class) {
  51.                 IDServizioFactory.init();
  52.             }
  53.         }
  54.         return IDServizioFactory.factory;
  55.     }
  56.    
  57.     private IDServizioFactory() {}

  58.     @SuppressWarnings("deprecation")
  59.     private IDServizio build(String tipo,String nome,IDSoggetto soggettoErogatore,int versione){
  60.         IDServizio idServizio = new IDServizio();
  61.         idServizio.setTipo(tipo);
  62.         idServizio.setNome(nome);
  63.         idServizio.setSoggettoErogatore(soggettoErogatore);
  64.         idServizio.setVersione(versione);
  65.         return idServizio;
  66.     }
  67.    
  68.     public String normalizeUri(String uri) throws DriverRegistroServiziException{
  69.         // La uri può non contenere la versione, che invece nella 3.0 è obbligatoria.
  70.         // Facendo la doppia conversione, viene aggiunta la versione di default
  71.         IDServizio idServizio = this.getIDServizioFromUri(uri);
  72.         return this.getUriFromIDServizio(idServizio);
  73.     }
  74.    
  75.     public String getUriFromIDServizio(IDServizio idServizio) throws DriverRegistroServiziException{
  76.         if(idServizio==null){
  77.             throw new DriverRegistroServiziException("IDServizio non fornito");
  78.         }
  79.         if(idServizio.getTipo()==null){
  80.             throw new DriverRegistroServiziException(TIPO_NON_FORNITO);
  81.         }
  82.         if(idServizio.getNome()==null){
  83.             throw new DriverRegistroServiziException(NOME_NON_FORNITO);
  84.         }
  85.         IDSoggetto soggettoErogatore = idServizio.getSoggettoErogatore();
  86.         if(soggettoErogatore==null){
  87.             throw new DriverRegistroServiziException(SOGGETTO_EROGATORE_NON_FORNITO);
  88.         }
  89.         if(soggettoErogatore.getTipo()==null){
  90.             throw new DriverRegistroServiziException(TIPO_SOGGETTO_EROGATORE_NON_FORNITO);
  91.         }
  92.         if(soggettoErogatore.getNome()==null){
  93.             throw new DriverRegistroServiziException(NOME_SOGGETTO_EROGATORE_NON_FORNITO);
  94.         }
  95.         return soggettoErogatore.toString() + ":" + idServizio.getTipo()+"/"+idServizio.getNome() + ":" + idServizio.getVersione();
  96.     }
  97.    
  98.     public String getUriFromAccordo(AccordoServizioParteSpecifica accordo)  throws DriverRegistroServiziException{
  99.         if(accordo==null){
  100.             throw new DriverRegistroServiziException("Accordo non fornito");
  101.         }
  102.         IDServizio idServizio = this.build(accordo.getTipo(),accordo.getNome(),
  103.                 BeanUtilities.getSoggettoErogatore(accordo),accordo.getVersione());
  104.         return this.getUriFromIDServizio(idServizio);
  105.     }
  106.    
  107.     public String getUriFromValues(String tipo, String nome,String tipoSoggettoErogatore,String nomeSoggettoErogatore,int ver)  throws DriverRegistroServiziException{
  108.         if(tipo==null){
  109.             throw new DriverRegistroServiziException(TIPO_NON_FORNITO);
  110.         }
  111.         if(nome==null){
  112.             throw new DriverRegistroServiziException(NOME_NON_FORNITO);
  113.         }
  114.         if(tipoSoggettoErogatore==null){
  115.             throw new DriverRegistroServiziException(TIPO_SOGGETTO_EROGATORE_NON_FORNITO);
  116.         }
  117.         if(nomeSoggettoErogatore==null){
  118.             throw new DriverRegistroServiziException(NOME_SOGGETTO_EROGATORE_NON_FORNITO);
  119.         }
  120.         IDSoggetto soggettoErogatore = new IDSoggetto(tipoSoggettoErogatore,nomeSoggettoErogatore);
  121.         IDServizio idServizio = this.build(tipo,nome,soggettoErogatore,ver);
  122.         return this.getUriFromIDServizio(idServizio);
  123.     }
  124.    
  125.     public String getUriFromValues(String tipo, String nome,IDSoggetto soggettoErogatore,int ver)  throws DriverRegistroServiziException{
  126.         if(soggettoErogatore==null){
  127.             throw new DriverRegistroServiziException(SOGGETTO_EROGATORE_NON_FORNITO);
  128.         }
  129.         return getUriFromValues(tipo, nome, soggettoErogatore.getTipo(), soggettoErogatore.getNome(), ver);
  130.     }
  131.    
  132.     public IDServizio getIDServizioFromUri(String uriAccordo) throws DriverRegistroServiziException{
  133.         try{
  134.        
  135.             // sintassi
  136.             // tipoSoggetto/nomeSoggetto:tipoServizio/nomeServizio:versione
  137.            
  138.             if(uriAccordo==null){
  139.                 throw new DriverRegistroServiziException("Uri accordo non fornita");
  140.             }
  141.             int primoMarcatore = uriAccordo.indexOf(":");
  142.             int secondoMarcatore = -1;
  143.             if(primoMarcatore>=0){
  144.                 secondoMarcatore = uriAccordo.indexOf(":",primoMarcatore+1);
  145.             }
  146.             int terzoMarcatore = -1;
  147.             if(secondoMarcatore>0){
  148.                 terzoMarcatore = uriAccordo.indexOf(":",secondoMarcatore+1);
  149.                 if(terzoMarcatore>0){
  150.                     throw new DriverRegistroServiziException(SINTASSI_NON_CORRETTA);
  151.                 }
  152.             }
  153.            
  154.             if(primoMarcatore<0){
  155.                 throw new DriverRegistroServiziException(SINTASSI_NON_CORRETTA);
  156.             }
  157.             if(secondoMarcatore<=0){
  158.                 throw new DriverRegistroServiziException(SINTASSI_NON_CORRETTA);
  159.             }
  160.            
  161.             String tmp1 = null;
  162.             String tmp2 = null;
  163.             String tmp3 = null;
  164.             tmp1 = uriAccordo.substring(0, primoMarcatore); //soggetto erogatore
  165.             tmp2 = uriAccordo.substring((primoMarcatore+1), secondoMarcatore); // servizio
  166.             tmp3 = uriAccordo.substring((secondoMarcatore+1),uriAccordo.length()); // versione
  167.            
  168.             int divisorioSoggettoErogatore = tmp1.indexOf("/");
  169.             if(divisorioSoggettoErogatore<=0){
  170.                 throw new DriverRegistroServiziException("sintassi del soggetto erogatore non corretta, l'uri deve essere definita con la seguente forma: tipoSoggetto/nomeSoggetto:tipoServizio/nomeServizio:versione");
  171.             }
  172.             String tipoSoggettoErogatore = tmp1.substring(0,divisorioSoggettoErogatore);
  173.             String nomeSoggettoErogatore = tmp1.substring((divisorioSoggettoErogatore+1),tmp1.length());
  174.             IDSoggetto soggettoErogatore = new IDSoggetto(tipoSoggettoErogatore,nomeSoggettoErogatore);
  175.            
  176.             int divisorioServizio = tmp2.indexOf("/");
  177.             if(divisorioServizio<=0){
  178.                 throw new DriverRegistroServiziException("sintassi del servizio non corretta, l'uri deve essere definita con la seguente forma: tipoSoggetto/nomeSoggetto:tipoServizio/nomeServizio:versione");
  179.             }
  180.             String tipoServizio = tmp2.substring(0,divisorioServizio);
  181.             String nomeServizio = tmp2.substring((divisorioServizio+1),tmp2.length());
  182.            
  183.             int versione = 1;
  184.             try{
  185.                 versione = Integer.parseInt(tmp3);
  186.             }catch(Exception e){
  187.                 throw new DriverRegistroServiziException("sintassi della versione non corretta: "+e.getMessage(),e);
  188.             }
  189.            
  190.             return this.build(tipoServizio,nomeServizio,soggettoErogatore,versione);
  191.    
  192.         }catch(Exception e){
  193.             throw new DriverRegistroServiziException("Parsing uriAccordo["+uriAccordo+"] non riusciuto: "+e.getMessage());
  194.         }
  195.     }
  196.    
  197.     public IDServizio getIDServizioFromAccordo(AccordoServizioParteSpecifica accordo) throws DriverRegistroServiziException{
  198.         if(accordo==null){
  199.             throw new DriverRegistroServiziException("Accordo non fornito");
  200.         }
  201.         IDServizio idServizio = this.build(accordo.getTipo(), accordo.getNome(),
  202.                 new IDSoggetto(accordo.getTipoSoggettoErogatore(),accordo.getNomeSoggettoErogatore()),
  203.                 accordo.getVersione());
  204.         if(TipologiaServizio.CORRELATO.equals(accordo.getTipologiaServizio())){
  205.             idServizio.setTipologia(org.openspcoop2.core.constants.TipologiaServizio.CORRELATO);
  206.         }
  207.         else{
  208.             idServizio.setTipologia(org.openspcoop2.core.constants.TipologiaServizio.NORMALE);
  209.         }
  210.         idServizio.setUriAccordoServizioParteComune(accordo.getAccordoServizioParteComune());
  211.         idServizio.setPortType(accordo.getPortType());
  212.         return idServizio;
  213.     }
  214.    
  215.     public IDServizio getIDServizioFromValues(String tipo,String nome,String tipoSoggettoErogatore,String nomeSoggettoErogatore,int ver) throws DriverRegistroServiziException{
  216.         if(tipo==null){
  217.             throw new DriverRegistroServiziException(TIPO_NON_FORNITO);
  218.         }
  219.         if(nome==null){
  220.             throw new DriverRegistroServiziException(NOME_NON_FORNITO);
  221.         }
  222.         if(tipoSoggettoErogatore==null){
  223.             throw new DriverRegistroServiziException(TIPO_SOGGETTO_EROGATORE_NON_FORNITO);
  224.         }
  225.         if(nomeSoggettoErogatore==null){
  226.             throw new DriverRegistroServiziException(NOME_SOGGETTO_EROGATORE_NON_FORNITO);
  227.         }
  228.         IDSoggetto soggettoErogatore = new IDSoggetto(tipoSoggettoErogatore,nomeSoggettoErogatore);
  229.         return this.build(tipo,nome,soggettoErogatore,ver);
  230.     }
  231.     public IDServizio getIDServizioFromValuesWithoutCheck(String tipo,String nome,String tipoSoggettoErogatore,String nomeSoggettoErogatore,int ver) {
  232.         IDSoggetto soggettoErogatore = new IDSoggetto(tipoSoggettoErogatore,nomeSoggettoErogatore);
  233.         return this.build(tipo,nome,soggettoErogatore,ver);
  234.     }
  235.     public IDServizio getIDServizioFromValues(String tipo,String nome,IDSoggetto soggettoErogatore,int ver) throws DriverRegistroServiziException{
  236.         if(soggettoErogatore==null){
  237.             throw new DriverRegistroServiziException(SOGGETTO_EROGATORE_NON_FORNITO);
  238.         }
  239.         IDServizio idServ = this.getIDServizioFromValues(tipo, nome, soggettoErogatore.getTipo(), soggettoErogatore.getNome(), ver);
  240.         idServ.getSoggettoErogatore().setCodicePorta(soggettoErogatore.getCodicePorta());
  241.         return idServ;
  242.     }
  243.    
  244.    
  245. }