PorteNamingUtils.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.utils;

  21. import java.util.List;

  22. import org.openspcoop2.message.constants.ServiceBinding;
  23. import org.openspcoop2.protocol.sdk.IProtocolFactory;
  24. import org.openspcoop2.protocol.sdk.ProtocolException;
  25. import org.openspcoop2.protocol.sdk.config.IProtocolConfiguration;

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

  34.     private IProtocolFactory<?> protocolFactory;
  35.     private IProtocolConfiguration protocolConfiguration;
  36.     private ServiceBinding serviceBindingDefault = null;
  37.    
  38.     public PorteNamingUtils(IProtocolFactory<?> protocolFactory) throws ProtocolException {
  39.         this.protocolFactory = protocolFactory;
  40.         this.protocolConfiguration = protocolFactory.createProtocolConfiguration();
  41.        
  42.         if(this.protocolFactory.getManifest().getBinding().getRest()!=null &&
  43.                 this.protocolFactory.getManifest().getBinding().getSoap()!=null) {
  44.             // deve essere specificato un service binding di default
  45.             switch(this.protocolFactory.getManifest().getBinding().getDefault()) {
  46.             case REST:
  47.                 this.serviceBindingDefault = ServiceBinding.REST;
  48.                 break;
  49.             case SOAP:
  50.                 this.serviceBindingDefault = ServiceBinding.SOAP;
  51.                 break;
  52.             }
  53.         }
  54.         else if(this.protocolFactory.getManifest().getBinding().getRest()!=null) {
  55.             this.serviceBindingDefault = ServiceBinding.REST;
  56.         }
  57.         else {
  58.             this.serviceBindingDefault = ServiceBinding.SOAP;
  59.         }
  60.     }
  61.    
  62.    
  63.     public String normalizePD(String nome) throws ProtocolException {
  64.         // Convenzione PD: FRUITORE/EROGATORE/SERVIZIO/...
  65.         // Ogni oggetto viene separato dal tipo "_"
  66.          int [] posizioneSoggetto = new int[2];
  67.          posizioneSoggetto[0] = 0;
  68.          posizioneSoggetto[1] = 1;
  69.          int [] posizioneServizio = new int[1];
  70.          posizioneServizio[0] = 2;
  71.          return _normalize(nome, posizioneSoggetto, posizioneServizio);
  72.     }
  73.     public String normalizePA(String nome) throws ProtocolException {
  74.         // Convenzione PA: EROGATORE/SERVIZIO/...
  75.         // Ogni oggetto viene separato dal tipo "_"
  76.          int [] posizioneSoggetto = new int[1];
  77.          posizioneSoggetto[0] = 0;
  78.          int [] posizioneServizio = new int[1];
  79.          posizioneServizio[0] = 1;
  80.          return _normalize(nome, posizioneSoggetto, posizioneServizio);
  81.     }
  82.     private String _normalize(String nome, int [] posizioneSoggetto, int [] posizioneServizio) throws ProtocolException {
  83.         if(nome.contains("/")) {
  84.            
  85.             String tipoSoggettoDefault = this.protocolConfiguration.getTipoSoggettoDefault();
  86.             String _tipoSoggettoDefault = tipoSoggettoDefault +"_";
  87.             int _lengthTipoSoggettoDefault = _tipoSoggettoDefault.length();
  88.            
  89.             String tipoServizioDefault = this.protocolConfiguration.getTipoServizioDefault(this.serviceBindingDefault);
  90.             String _tipoServizioDefault = tipoServizioDefault +"_";
  91.             int _lengthTipoServizioDefault = _tipoServizioDefault.length();
  92.            
  93.             String [] tmp = nome.split("/");
  94.             if(tmp.length>=3) {
  95.                 StringBuilder bf = new StringBuilder();
  96.                 for (int i = 0; i < tmp.length; i++) {
  97.                     if(i>0) {
  98.                         bf.append("/");
  99.                     }
  100.                    
  101.                     boolean soggetto = false;
  102.                     for (int j = 0; j < posizioneSoggetto.length; j++) {
  103.                         if(i==posizioneSoggetto[j]) {
  104.                             soggetto = true;
  105.                             break;
  106.                         }
  107.                     }
  108.                    
  109.                     boolean servizio = false;
  110.                     for (int j = 0; j < posizioneServizio.length; j++) {
  111.                         if(i==posizioneServizio[j]) {
  112.                             servizio = true;
  113.                             break;
  114.                         }
  115.                     }
  116.                    
  117.                     String s = tmp[i].trim();
  118.                     if(soggetto || servizio) {
  119.                         if(soggetto) {
  120.                             if(s.startsWith(_tipoSoggettoDefault) && s.length()>_lengthTipoSoggettoDefault) {
  121.                                 bf.append(s.substring(_lengthTipoSoggettoDefault));
  122.                             }
  123.                             else {
  124.                                 bf.append(s);
  125.                             }
  126.                         }
  127.                         else {
  128.                             if(s.startsWith(_tipoServizioDefault) && s.length()>_lengthTipoServizioDefault) {
  129.                                 bf.append(s.substring(_lengthTipoServizioDefault));
  130.                             }
  131.                             else {
  132.                                 bf.append(s);
  133.                             }
  134.                         }
  135.                     }
  136.                     else {
  137.                         bf.append(s);
  138.                     }
  139.                 }
  140.                 return bf.toString();
  141.             }
  142.            
  143.         }
  144.         return nome;
  145.     }
  146.    
  147.    
  148.    
  149.     public String enrichPD(String nome) throws ProtocolException {
  150.         // Convenzione PD: FRUITORE/EROGATORE/SERVIZIO/...
  151.         // Ogni oggetto viene separato dal tipo "_"
  152.          int [] posizioneSoggetto = new int[2];
  153.          posizioneSoggetto[0] = 0;
  154.          posizioneSoggetto[1] = 1;
  155.          int [] posizioneServizio = new int[1];
  156.          posizioneServizio[0] = 2;
  157.          return _enrich(nome, posizioneSoggetto, posizioneServizio);
  158.     }
  159.     public String enrichPA(String nome) throws ProtocolException {
  160.         // Convenzione PA: EROGATORE/SERVIZIO/...
  161.         // Ogni oggetto viene separato dal tipo "_"
  162.          int [] posizioneSoggetto = new int[1];
  163.          posizioneSoggetto[0] = 0;
  164.          int [] posizioneServizio = new int[1];
  165.          posizioneServizio[0] = 1;
  166.          return _enrich(nome, posizioneSoggetto, posizioneServizio);
  167.     }
  168.     private String _enrich(String nome, int [] posizioneSoggetto, int [] posizioneServizio) throws ProtocolException {
  169.         if(nome.contains("/")) {
  170.            
  171.             String tipoSoggettoDefault = this.protocolConfiguration.getTipoSoggettoDefault();
  172.             String _tipoSoggettoDefault = tipoSoggettoDefault +"_";
  173.             List<String> tipiSoggetto = this.protocolConfiguration.getTipiSoggetti();
  174.            
  175.             String tipoServizioDefault = this.protocolConfiguration.getTipoServizioDefault(this.serviceBindingDefault);
  176.             String _tipoServizioDefault = tipoServizioDefault +"_";
  177.             List<String> tipiServizi = this.protocolConfiguration.getTipiServizi(this.serviceBindingDefault);
  178.            
  179.             String [] tmp = nome.split("/");
  180.             if(tmp.length>=3) {
  181.                 StringBuilder bf = new StringBuilder();
  182.                 for (int i = 0; i < tmp.length; i++) {
  183.                     if(i>0) {
  184.                         bf.append("/");
  185.                     }
  186.                    
  187.                     boolean soggetto = false;
  188.                     for (int j = 0; j < posizioneSoggetto.length; j++) {
  189.                         if(i==posizioneSoggetto[j]) {
  190.                             soggetto = true;
  191.                             break;
  192.                         }
  193.                     }
  194.                    
  195.                     boolean servizio = false;
  196.                     for (int j = 0; j < posizioneServizio.length; j++) {
  197.                         if(i==posizioneServizio[j]) {
  198.                             servizio = true;
  199.                             break;
  200.                         }
  201.                     }
  202.                    
  203.                     String s = tmp[i].trim();
  204.                     if(soggetto || servizio) {
  205.                         if(soggetto) {
  206.                             if(s.contains("_")==false) {
  207.                                 bf.append(_tipoSoggettoDefault);    
  208.                             }
  209.                             else {
  210.                                 boolean found = false;
  211.                                 for (String tipoSoggetto : tipiSoggetto) {
  212.                                     if(s.startsWith(tipoSoggetto+"_")) {
  213.                                         found = true;
  214.                                         break;
  215.                                     }
  216.                                 }
  217.                                 if(!found) {
  218.                                     bf.append(_tipoSoggettoDefault);    
  219.                                 }
  220.                             }
  221.                             bf.append(s);
  222.                         }
  223.                         else {
  224.                             if(s.contains("_")==false) {
  225.                                 bf.append(_tipoServizioDefault);    
  226.                             }
  227.                             else {
  228.                                 boolean found = false;
  229.                                 for (String tipoServizio : tipiServizi) {
  230.                                     if(s.startsWith(tipoServizio+"_")) {
  231.                                         found = true;
  232.                                         break;
  233.                                     }
  234.                                 }
  235.                                 if(!found) {
  236.                                     bf.append(_tipoServizioDefault);    
  237.                                 }
  238.                             }
  239.                             bf.append(s);
  240.                         }
  241.                     }
  242.                     else {
  243.                         bf.append(s);
  244.                     }
  245.                 }
  246.                 return bf.toString();
  247.             }
  248.            
  249.         }
  250.         return nome;
  251.     }
  252.    
  253. }