NamingUtils.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.engine.utils;

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

  23. import org.openspcoop2.core.id.IDAccordo;
  24. import org.openspcoop2.core.id.IDAccordoCooperazione;
  25. import org.openspcoop2.core.id.IDServizio;
  26. import org.openspcoop2.core.id.IDServizioApplicativo;
  27. import org.openspcoop2.core.id.IDSoggetto;
  28. import org.openspcoop2.core.registry.AccordoCooperazione;
  29. import org.openspcoop2.core.registry.AccordoServizioParteComune;
  30. import org.openspcoop2.core.registry.AccordoServizioParteSpecifica;
  31. import org.openspcoop2.core.registry.beans.AccordoServizioParteComuneSintetico;
  32. import org.openspcoop2.core.registry.driver.IDAccordoCooperazioneFactory;
  33. import org.openspcoop2.core.registry.driver.IDAccordoFactory;
  34. import org.openspcoop2.core.registry.driver.IDServizioFactory;
  35. import org.openspcoop2.protocol.engine.ProtocolFactoryManager;
  36. import org.openspcoop2.protocol.sdk.IProtocolFactory;
  37. import org.openspcoop2.protocol.sdk.ProtocolException;

  38. /**
  39.  * PorteNamingUtils
  40.  *
  41.  * @author Poli Andrea (apoli@link.it)
  42.  * @author $Author$
  43.  * @version $Rev$, $Date$
  44.  */
  45. public class NamingUtils {
  46.    
  47.     private NamingUtils() {}

  48.    
  49.     // PROTOCOLLI
  50.    
  51.     public static List<String> getLabelsProtocolli(List<String> protocolli) throws ProtocolException{
  52.         List<String> l= null;
  53.         if(protocolli==null || protocolli.isEmpty()) {
  54.             return l;
  55.         }
  56.         l = new ArrayList<>();
  57.         for (String protocollo : protocolli) {
  58.             l.add(NamingUtils.getLabelProtocollo(protocollo));
  59.         }
  60.         return l;
  61.     }
  62.    
  63.     public static String getLabelProtocollo(String protocollo) throws ProtocolException{
  64.         ProtocolFactoryManager protocolFactoryManager = ProtocolFactoryManager.getInstance();
  65.         return protocolFactoryManager.getProtocolFactoryByName(protocollo).getInformazioniProtocol().getLabel();
  66.     }
  67.    
  68.     public static String getDescrizioneProtocollo(String protocollo) throws ProtocolException {
  69.         ProtocolFactoryManager protocolFactoryManager = ProtocolFactoryManager.getInstance();
  70.         return protocolFactoryManager.getProtocolFactoryByName(protocollo).getInformazioniProtocol().getDescription();
  71.     }
  72.    
  73.     public static String getWebSiteProtocollo(String protocollo) throws ProtocolException {
  74.         ProtocolFactoryManager protocolFactoryManager = ProtocolFactoryManager.getInstance();
  75.         return protocolFactoryManager.getProtocolFactoryByName(protocollo).getInformazioniProtocol().getWebSite();
  76.     }
  77.    
  78.    
  79.    
  80.     // SOGGETTI
  81.    
  82.     public static String getLabelSoggetto(IDSoggetto idSoggetto) throws ProtocolException{
  83.         ProtocolFactoryManager protocolFactoryManager = ProtocolFactoryManager.getInstance();
  84.         String protocollo = protocolFactoryManager.getProtocolByOrganizationType(idSoggetto.getTipo());
  85.         return getLabelSoggetto(protocollo, idSoggetto.getTipo(), idSoggetto.getNome());
  86.     }
  87.    
  88.     public static String getLabelSoggetto(String protocollo, IDSoggetto idSoggetto) throws ProtocolException{
  89.         return getLabelSoggetto(protocollo, idSoggetto.getTipo(), idSoggetto.getNome());
  90.     }
  91.     public static String getLabelSoggetto(String protocollo, String tipoSoggetto, String nomeSoggetto) throws ProtocolException{
  92.         StringBuilder bf = new StringBuilder();
  93.         ProtocolFactoryManager protocolFactoryManager = ProtocolFactoryManager.getInstance();
  94.         if(protocolFactoryManager.getOrganizationTypes().get(protocollo).size()>1) {
  95.             IProtocolFactory<?> protocolFactory = protocolFactoryManager.getProtocolFactoryByName(protocollo);
  96.             if(tipoSoggetto.equals(protocolFactory.createProtocolConfiguration().getTipoSoggettoDefault())) {
  97.                 bf.append(nomeSoggetto);
  98.             }
  99.             else{
  100.                 bf.append(tipoSoggetto).append("/").append(nomeSoggetto);
  101.             }
  102.         }
  103.         else {
  104.             bf.append(nomeSoggetto);
  105.         }
  106.         return bf.toString();
  107.     }
  108.    
  109.     public static IDSoggetto getSoggettoFromLabel(String protocollo, String labelSoggetto) throws ProtocolException{
  110.         ProtocolFactoryManager protocolFactoryManager = ProtocolFactoryManager.getInstance();
  111.         IProtocolFactory<?> protocolFactory = protocolFactoryManager.getProtocolFactoryByName(protocollo);
  112.         String tipoSoggettoDefault = protocolFactory.createProtocolConfiguration().getTipoSoggettoDefault();
  113.         String tipo = null;
  114.         String nome = null;
  115.         if(labelSoggetto.contains("/")) {
  116.             String [] tmp = labelSoggetto.split("/");
  117.             tipo = tmp[0];
  118.             nome = tmp[1];
  119.         }
  120.         else {
  121.             tipo = tipoSoggettoDefault;
  122.             nome = labelSoggetto;
  123.         }
  124.         return new IDSoggetto(tipo, nome);
  125.     }
  126.    
  127.    
  128.     // APPLICATIVI
  129.    
  130.     public static String getLabelServizioApplicativo(IDServizioApplicativo idServizioApplicativo) throws ProtocolException{
  131.         ProtocolFactoryManager protocolFactoryManager = ProtocolFactoryManager.getInstance();
  132.         String protocollo = protocolFactoryManager.getProtocolByOrganizationType(idServizioApplicativo.getIdSoggettoProprietario().getTipo());
  133.         return getLabelServizioApplicativo(protocollo, idServizioApplicativo);
  134.     }
  135.     public static String getLabelServizioApplicativo(String protocollo, IDServizioApplicativo idServizioApplicativo) throws ProtocolException{
  136.         StringBuilder bf = new StringBuilder();
  137.         bf.append(idServizioApplicativo.getNome());
  138.         bf.append(" (");
  139.         bf.append(getLabelSoggetto(protocollo, idServizioApplicativo.getIdSoggettoProprietario()));
  140.         bf.append(")");
  141.         return bf.toString();
  142.     }
  143.    
  144.    
  145.    
  146.     // API
  147.    
  148.     private static IDAccordo getIDAccordoFromAccordo(AccordoServizioParteComune as) throws ProtocolException{
  149.         try {
  150.             return IDAccordoFactory.getInstance().getIDAccordoFromAccordo(as);
  151.         }catch(Exception e) {
  152.             throw new ProtocolException(e.getMessage(),e);
  153.         }
  154.     }
  155.     public static String getLabelAccordoServizioParteComune(AccordoServizioParteComune as) throws ProtocolException{
  156.         ProtocolFactoryManager protocolFactoryManager = ProtocolFactoryManager.getInstance();
  157.         String protocollo = protocolFactoryManager.getProtocolByOrganizationType(as.getSoggettoReferente().getTipo());
  158.         return getLabelAccordoServizioParteComune(protocollo, getIDAccordoFromAccordo(as));
  159.     }
  160.     private static IDAccordo getIDAccordoFromAccordo(AccordoServizioParteComuneSintetico as) throws ProtocolException{
  161.         try {
  162.             return IDAccordoFactory.getInstance().getIDAccordoFromAccordo(as);
  163.         }catch(Exception e) {
  164.             throw new ProtocolException(e.getMessage(),e);
  165.         }
  166.     }
  167.     public static String getLabelAccordoServizioParteComune(AccordoServizioParteComuneSintetico as) throws ProtocolException{
  168.         ProtocolFactoryManager protocolFactoryManager = ProtocolFactoryManager.getInstance();
  169.         String protocollo = protocolFactoryManager.getProtocolByOrganizationType(as.getSoggettoReferente().getTipo());
  170.         return getLabelAccordoServizioParteComune(protocollo, getIDAccordoFromAccordo(as));
  171.     }
  172.     public static String getLabelAccordoServizioParteComune(String protocollo, AccordoServizioParteComune as) throws ProtocolException{
  173.         return getLabelAccordoServizioParteComune(protocollo, getIDAccordoFromAccordo(as));
  174.     }
  175.     public static String getLabelAccordoServizioParteComune(IDAccordo idAccordo) throws ProtocolException{
  176.         ProtocolFactoryManager protocolFactoryManager = ProtocolFactoryManager.getInstance();
  177.         String protocollo = protocolFactoryManager.getProtocolByOrganizationType(idAccordo.getSoggettoReferente().getTipo());
  178.         return getLabelAccordoServizioParteComune(protocollo, idAccordo);
  179.     }
  180.     public static String getLabelAccordoServizioParteComune(String protocollo, IDAccordo idAccordo) throws ProtocolException{
  181.         return getLabelAccordoServizioParteComune(protocollo, idAccordo, true);
  182.     }
  183.     public static String getLabelAccordoServizioParteComune(String protocollo, IDAccordo idAccordo, boolean addSoggettoReferente) throws ProtocolException{
  184.         StringBuilder bf = new StringBuilder();
  185.         bf.append(idAccordo.getNome());
  186.         bf.append(" v");
  187.         bf.append(idAccordo.getVersione());
  188.         if(addSoggettoReferente) {
  189.             ProtocolFactoryManager protocolFactoryManager = ProtocolFactoryManager.getInstance();
  190.             boolean supportatoSoggettoReferente = protocolFactoryManager.getProtocolFactoryByName(protocollo).createProtocolConfiguration().isSupportoSoggettoReferenteAccordiParteComune();
  191.             if(supportatoSoggettoReferente &&
  192.                 idAccordo.getSoggettoReferente()!=null){
  193.                 bf.append(" (");
  194.                 bf.append(getLabelSoggetto(protocollo, idAccordo.getSoggettoReferente().getTipo(), idAccordo.getSoggettoReferente().getNome()));
  195.                 bf.append(")");
  196.             }
  197.         }
  198.         return bf.toString();
  199.     }
  200.    
  201.     // SERVIZI
  202.    
  203.     public static String getLabelAccordoServizioParteSpecificaSenzaErogatore(String protocollo, String tipoServizio, String nomeServizio, Integer versioneInt) throws ProtocolException{
  204.        
  205.         String versione = "";
  206.         if(ProtocolFactoryManager.getInstance().getProtocolFactoryByName(protocollo).createProtocolConfiguration().isSupportoVersionamentoAccordiParteSpecifica()) {
  207.             versione = " v"+versioneInt;
  208.         }
  209.        
  210.         ProtocolFactoryManager protocolFactoryManager = ProtocolFactoryManager.getInstance();
  211.         if(protocolFactoryManager._getServiceTypes().get(protocollo).size()>1) {
  212.             IProtocolFactory<?> protocolFactory = protocolFactoryManager.getProtocolFactoryByName(protocollo);
  213.             if(tipoServizio.equals(protocolFactory.createProtocolConfiguration().getTipoServizioDefault(null))) {
  214.                 return nomeServizio+versione;
  215.             }
  216.             else {
  217.                 return tipoServizio+"/"+nomeServizio+versione;  
  218.             }
  219.         }
  220.         else {
  221.             return nomeServizio+versione;
  222.         }
  223.     }
  224.    
  225.     private static IDServizio getIDAccordoFromAccordo(AccordoServizioParteSpecifica as) throws ProtocolException{
  226.         try {
  227.             return IDServizioFactory.getInstance().getIDServizioFromAccordo(as);
  228.         }catch(Exception e) {
  229.             throw new ProtocolException(e.getMessage(),e);
  230.         }
  231.     }
  232.     public static String getLabelAccordoServizioParteSpecifica(AccordoServizioParteSpecifica as) throws ProtocolException{
  233.         ProtocolFactoryManager protocolFactoryManager = ProtocolFactoryManager.getInstance();
  234.         String protocollo = protocolFactoryManager.getProtocolByOrganizationType(as.getTipoSoggettoErogatore());
  235.         return getLabelAccordoServizioParteSpecifica(protocollo,getIDAccordoFromAccordo(as));
  236.     }
  237.     public static String getLabelAccordoServizioParteSpecifica(String protocollo, AccordoServizioParteSpecifica as) throws ProtocolException{
  238.         return getLabelAccordoServizioParteSpecifica(protocollo,getIDAccordoFromAccordo(as));
  239.     }
  240.     public static String getLabelAccordoServizioParteSpecifica(IDServizio idServizio) throws ProtocolException{
  241.         ProtocolFactoryManager protocolFactoryManager = ProtocolFactoryManager.getInstance();
  242.         String protocollo = protocolFactoryManager.getProtocolByOrganizationType(idServizio.getSoggettoErogatore().getTipo());
  243.         return getLabelAccordoServizioParteSpecifica(protocollo, idServizio);
  244.     }
  245.     public static String getLabelAccordoServizioParteSpecificaSenzaErogatore(IDServizio idServizio) throws ProtocolException{
  246.         ProtocolFactoryManager protocolFactoryManager = ProtocolFactoryManager.getInstance();
  247.         String protocollo = protocolFactoryManager.getProtocolByServiceType(idServizio.getTipo());
  248.         return getLabelAccordoServizioParteSpecificaSenzaErogatore(protocollo, idServizio);
  249.     }
  250.     public static String getLabelAccordoServizioParteSpecificaSenzaErogatore(String protocollo, IDServizio idServizio) throws ProtocolException{
  251.         return getLabelAccordoServizioParteSpecificaSenzaErogatore(protocollo, idServizio.getTipo(), idServizio.getNome(), idServizio.getVersione());
  252.     }
  253.     public static String getLabelAccordoServizioParteSpecifica(String protocollo, IDServizio idServizio) throws ProtocolException{
  254.         StringBuilder bf = new StringBuilder();
  255.         bf.append(getLabelAccordoServizioParteSpecificaSenzaErogatore(protocollo, idServizio.getTipo(), idServizio.getNome(), idServizio.getVersione()));
  256.         bf.append(" (");
  257.         bf.append(getLabelSoggetto(protocollo, idServizio.getSoggettoErogatore().getTipo(), idServizio.getSoggettoErogatore().getNome()));
  258.         bf.append(")");
  259.         return bf.toString();
  260.     }
  261.    
  262.    
  263.    
  264.     // ACCORDI COOPERAZIONE
  265.    
  266.     private static IDAccordoCooperazione getIDAccordoFromAccordo(AccordoCooperazione ac) throws ProtocolException{
  267.         try {
  268.             return IDAccordoCooperazioneFactory.getInstance().getIDAccordoFromAccordo(ac);
  269.         }catch(Exception e) {
  270.             throw new ProtocolException(e.getMessage(),e);
  271.         }
  272.     }
  273.     public static String getLabelAccordoCooperazione(AccordoCooperazione ac) throws ProtocolException{
  274.         ProtocolFactoryManager protocolFactoryManager = ProtocolFactoryManager.getInstance();
  275.         String protocollo = protocolFactoryManager.getProtocolByOrganizationType(ac.getSoggettoReferente().getTipo());
  276.         return getLabelAccordoCooperazione(protocollo, getIDAccordoFromAccordo(ac));
  277.     }
  278.     public static String getLabelAccordoCooperazione(String protocollo, AccordoCooperazione ac) throws ProtocolException{
  279.         return getLabelAccordoCooperazione(protocollo, getIDAccordoFromAccordo(ac));
  280.     }
  281.     public static String getLabelAccordoCooperazione(IDAccordoCooperazione idAccordo) throws ProtocolException{
  282.         ProtocolFactoryManager protocolFactoryManager = ProtocolFactoryManager.getInstance();
  283.         String protocollo = protocolFactoryManager.getProtocolByOrganizationType(idAccordo.getSoggettoReferente().getTipo());
  284.         return getLabelAccordoCooperazione(protocollo, idAccordo);
  285.     }
  286.     public static String getLabelAccordoCooperazione(String protocollo, IDAccordoCooperazione idAccordo) throws ProtocolException{
  287.         StringBuilder bf = new StringBuilder();
  288.         bf.append(idAccordo.getNome());
  289.         bf.append(" v");
  290.         bf.append(idAccordo.getVersione());
  291.         if(idAccordo.getSoggettoReferente()!=null){
  292.             bf.append(" (");
  293.             bf.append(getLabelSoggetto(protocollo, idAccordo.getSoggettoReferente().getTipo(), idAccordo.getSoggettoReferente().getNome()));
  294.             bf.append(")");
  295.         }
  296.         return bf.toString();
  297.     }
  298.    
  299.    
  300.     // RISORSE
  301.    
  302.     public static String getLabelResource(org.openspcoop2.core.registry.Resource resource) {
  303.         String method = null;
  304.         if(resource.getMethod()!=null) {
  305.             method = resource.getMethod().getValue();
  306.         }
  307.         return getLabelResource(method, resource.getPath());
  308.     }
  309.     public static String getLabelResource(org.openspcoop2.core.registry.beans.ResourceSintetica resource) {
  310.         String method = null;
  311.         if(resource.getMethod()!=null) {
  312.             method = resource.getMethod().getValue();
  313.         }
  314.         return getLabelResource(method, resource.getPath());
  315.     }
  316.    
  317.     public static String getLabelResource(String httpmethodParam, String pathParam) {
  318.         String method = null;
  319.         if(httpmethodParam==null || "".equals(httpmethodParam)) {
  320.             //resourcePrefix = "*"; Non mettiamo nulla
  321.         }
  322.         else {
  323.             method = httpmethodParam;
  324.         }
  325.        
  326.         String path = null;
  327.         if(pathParam==null || "".equals(pathParam)) {
  328.             path = "Qualsiasi";
  329.         }
  330.         else {
  331.             path = pathParam;
  332.         }
  333.        
  334.         if(method!=null) {
  335.             return  method + " " + path;
  336.         }
  337.         else {
  338.             return path;
  339.         }
  340.        
  341.     }
  342.    
  343.    
  344.     // API con DOMINIO
  345.    
  346.     public static final String LABEL_DOMINIO = "@";
  347.    
  348.     public static String getLabelServizioConDominioErogatore(String servizio, String erogatore) {
  349.         if(servizio.contains(" ")) {
  350.             String [] split = servizio.split(" ");
  351.             if(split!=null && split.length==2) {
  352.                 StringBuilder bf = new StringBuilder();
  353.                 bf.append(split[0]);
  354.                 bf.append(LABEL_DOMINIO);
  355.                 bf.append(erogatore);
  356.                 bf.append(" ");
  357.                 bf.append(split[1]);
  358.                 return bf.toString();
  359.             }
  360.             else {
  361.                 StringBuilder bf = new StringBuilder();
  362.                 bf.append(servizio);
  363.                 bf.append(LABEL_DOMINIO);
  364.                 bf.append(erogatore);  
  365.                 return bf.toString();
  366.             }
  367.         }
  368.         else {
  369.             StringBuilder bf = new StringBuilder();
  370.             bf.append(servizio);
  371.             bf.append(LABEL_DOMINIO);
  372.             bf.append(erogatore);  
  373.             return bf.toString();
  374.         }

  375.     }
  376. }