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

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

  23. import org.openspcoop2.core.constants.CostantiDB;
  24. import org.openspcoop2.core.registry.AccordoServizioParteComune;
  25. import org.openspcoop2.core.registry.Operation;
  26. import org.openspcoop2.core.registry.PortType;
  27. import org.openspcoop2.core.registry.ProtocolProperty;
  28. import org.openspcoop2.core.registry.Resource;
  29. import org.openspcoop2.core.registry.constants.CostantiRegistroServizi;
  30. import org.openspcoop2.core.registry.constants.ServiceBinding;
  31. import org.w3c.dom.Document;
  32. import org.w3c.dom.Element;
  33. import org.w3c.dom.Node;

  34. /**
  35.  * RegistroServiziUtils    
  36.  *
  37.  * @author Poli Andrea (poli@link.it)
  38.  * @author $Author$
  39.  * @version $Rev$, $Date$
  40.  */

  41. public class RegistroServiziUtils {

  42.     public static boolean isRegistroServizi(byte [] doc){
  43.         return RegistroServiziUtils.isRegistroServizi(doc,CostantiRegistroServizi.ROOT_LOCAL_NAME_REGISTRO);
  44.     }
  45.     public static boolean isRegistroServizi(byte [] doc,String localName){
  46.         try{
  47.             org.openspcoop2.message.xml.MessageXMLUtils xmlUtils = org.openspcoop2.message.xml.MessageXMLUtils.DEFAULT;
  48.             Document docXML = xmlUtils.newDocument(doc);
  49.             Element elemXML = docXML.getDocumentElement();
  50.             return RegistroServiziUtils.isRegistroServiziEngine(elemXML,localName);
  51.         }catch(Exception e){
  52.             /** System.out.println("NON e' un DOCUMENTO VALIDO: "+e.getMessage()); */
  53.             return false;
  54.         }
  55.     }
  56.     public static boolean isRegistroServizi(Document docXML){
  57.         return RegistroServiziUtils.isRegistroServizi(docXML,CostantiRegistroServizi.ROOT_LOCAL_NAME_REGISTRO);
  58.     }
  59.     public static boolean isRegistroServizi(Document docXML,String localName){
  60.         try{
  61.             Element elemXML = docXML.getDocumentElement();
  62.             return RegistroServiziUtils.isRegistroServiziEngine(elemXML,localName);
  63.         }catch(Exception e){
  64.             /** System.out.println("NON e' un DOCUMENTO VALIDO: "+e.getMessage()); */
  65.             return false;
  66.         }
  67.     }
  68.     public static boolean isRegistroServizi(Element elemXML,String localName){
  69.         return isRegistroServiziEngine(elemXML,localName);
  70.     }
  71.     public static boolean isRegistroServizi(Node nodeXml,String localName){
  72.         return isRegistroServiziEngine(nodeXml,localName);
  73.     }
  74.     private static boolean isRegistroServiziEngine(Node nodeXml,String localName){
  75.         try{
  76.             /** System.out.println("LOCAL["+Costanti.ROOT_LOCAL_NAME_DETTAGLIO_ECCEZIONE+"]vs["+elemXML.getLocalName()+"]  NAMESPACE["+Costanti.TARGET_NAMESPACE+"]vs["+elemXML.getNamespaceURI()+"]"); */
  77.             return localName.equals(nodeXml.getLocalName()) &&
  78.                     CostantiRegistroServizi.TARGET_NAMESPACE.equals(nodeXml.getNamespaceURI() );
  79.         }catch(Exception e){
  80.             /** System.out.println("NON e' un DOCUMENTO VALIDO: "+e.getMessage()); */
  81.             return false;
  82.         }
  83.     }
  84.    
  85.     public org.openspcoop2.core.registry.constants.ServiceBinding convertToRegistry(org.openspcoop2.message.constants.ServiceBinding serviceBinding){
  86.         if(serviceBinding==null) {
  87.             return null;
  88.         }
  89.         if(org.openspcoop2.message.constants.ServiceBinding.REST.equals(serviceBinding)) {
  90.             return org.openspcoop2.core.registry.constants.ServiceBinding.REST;
  91.         }
  92.         if(org.openspcoop2.message.constants.ServiceBinding.SOAP.equals(serviceBinding)) {
  93.             return org.openspcoop2.core.registry.constants.ServiceBinding.SOAP;
  94.         }
  95.         return null;
  96.     }
  97.    
  98.     public static org.openspcoop2.message.constants.ServiceBinding convertToMessage(org.openspcoop2.core.registry.constants.ServiceBinding serviceBinding) {
  99.         if(serviceBinding==null) {
  100.             return null;
  101.         }
  102.         if(org.openspcoop2.core.registry.constants.ServiceBinding.REST.equals(serviceBinding)) {
  103.             return org.openspcoop2.message.constants.ServiceBinding.REST;
  104.         }
  105.         if(org.openspcoop2.core.registry.constants.ServiceBinding.SOAP.equals(serviceBinding)) {
  106.             return org.openspcoop2.message.constants.ServiceBinding.SOAP;
  107.         }
  108.         return null;
  109.     }
  110.    
  111.     private static String getBooleanValueAsString(ProtocolProperty pp) {
  112.         return pp.getBooleanValue()!=null ? pp.getBooleanValue().toString() : "false";
  113.     }
  114.     public static List<String> fillPropertyProtocollo(String propertyName, AccordoServizioParteComune api, String portType, boolean booleanValue) {
  115.         return fillPropertyProtocollo(propertyName, null, api, portType, booleanValue);
  116.     }
  117.     public static final String PROPERTY_SEPARATOR = " -- ";
  118.     public static List<String> splitPropertyProtocolloResult(String value){
  119.         List<String> l = new ArrayList<>();
  120.         if(value.contains(PROPERTY_SEPARATOR)) {
  121.             String [] tmp = value.split(PROPERTY_SEPARATOR);
  122.             if(tmp!=null && tmp.length>0) {
  123.                 for (String s : tmp) {
  124.                     l.add(s.trim());
  125.                 }
  126.             }
  127.         }
  128.         else {
  129.             l.add(value);
  130.         }
  131.         return l;
  132.     }
  133.     private static void addPropertyProtocolloResult(String propertyName, String propertyName2,
  134.             String apiValue, String apiValue2, List<String> apiValues) {
  135.        
  136.         // backward compatibility
  137.         if(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SORGENTE_TOKEN_IDAUTH.equals(propertyName) &&
  138.             apiValue==null) {
  139.             apiValue = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SORGENTE_TOKEN_IDAUTH_VALUE_LOCALE;
  140.         }
  141.         if(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SORGENTE_TOKEN_IDAUTH.equals(propertyName2) &&
  142.             apiValue2==null) {
  143.             apiValue2 = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SORGENTE_TOKEN_IDAUTH_VALUE_LOCALE;
  144.         }
  145.        
  146.         String insertValue = apiValue2!=null ? apiValue+PROPERTY_SEPARATOR+apiValue2 : apiValue;
  147.        
  148.         if(insertValue!=null && !apiValues.contains(insertValue)) {
  149.             apiValues.add(insertValue);
  150.         }
  151.     }
  152.     public static List<String> fillPropertyProtocollo(String propertyName, String propertyName2, AccordoServizioParteComune api, String portType, boolean booleanValue) {
  153.        
  154.         List<String> apiValues = new ArrayList<>();
  155.        
  156.         for (ProtocolProperty pp : api.getProtocolPropertyList()) {
  157.             if(propertyName.equals(pp.getName())) {
  158.                 String apiValue = booleanValue ? getBooleanValueAsString(pp) : pp.getValue();
  159.                
  160.                 String apiValue2 = getPropertyProtocolloValue(propertyName2, booleanValue, api.getProtocolPropertyList());
  161.                
  162.                 addPropertyProtocolloResult(propertyName, propertyName2,
  163.                         apiValue, apiValue2, apiValues);
  164.                
  165.                 break;
  166.             }
  167.         }
  168.        
  169.         fillPropertyProtocollo(propertyName, propertyName2, api, portType, booleanValue,
  170.                 apiValues);
  171.        
  172.         return apiValues;
  173.     }
  174.     private static String getPropertyProtocolloValue(String propertyName2, boolean booleanValue, List<ProtocolProperty> properties) {
  175.         String apiValue2 = null;
  176.         if(propertyName2!=null) {
  177.             for (ProtocolProperty pp2 : properties) {
  178.                 if(propertyName2.equals(pp2.getName())) {
  179.                     apiValue2 = booleanValue ? getBooleanValueAsString(pp2) : pp2.getValue();
  180.                     break;
  181.                 }
  182.             }
  183.         }
  184.         return apiValue2;
  185.     }
  186.     private static void fillPropertyProtocollo(String propertyName, String propertyName2, AccordoServizioParteComune api, String portType, boolean booleanValue,
  187.             List<String> apiValues) {
  188.         if(ServiceBinding.REST.equals(api.getServiceBinding())) {
  189.             fillPropertyProtocolloREST(propertyName, propertyName2, api, booleanValue,
  190.                     apiValues);
  191.         }
  192.         else {
  193.             fillPropertyProtocolloSOAP(propertyName, propertyName2, api, portType, booleanValue,
  194.                     apiValues);
  195.         }
  196.     }
  197.     private static boolean isRidefinito(List<ProtocolProperty> properties) {
  198.         boolean ridefinito = false;                
  199.         for (ProtocolProperty pp : properties) {
  200.             if(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_ACTION_MODE.equals(pp.getName())) {
  201.                 String v = pp.getValue();
  202.                 ridefinito = CostantiDB.MODIPA_PROFILO_RIDEFINISCI.equals(v);
  203.                 break;
  204.             }
  205.         }
  206.         return ridefinito;
  207.     }
  208.     private static void fillPropertyProtocolloREST(String propertyName, String propertyName2, AccordoServizioParteComune api, boolean booleanValue,
  209.             List<String> apiValues) {
  210.         for (Resource resource : api.getResourceList()) {
  211.             if(resource.sizeProtocolPropertyList()>0) {
  212.                 boolean ridefinito = isRidefinito(resource.getProtocolPropertyList());
  213.                 if(ridefinito) {
  214.                     fillPropertyProtocolloREST(propertyName, propertyName2, booleanValue,
  215.                             apiValues, resource);
  216.                 }
  217.             }
  218.         }
  219.     }
  220.     private static void fillPropertyProtocolloREST(String propertyName, String propertyName2, boolean booleanValue,
  221.             List<String> apiValues, Resource resource) {
  222.         for (ProtocolProperty pp : resource.getProtocolPropertyList()) {
  223.             if(propertyName.equals(pp.getName())) {
  224.                 String apiValue = booleanValue ? getBooleanValueAsString(pp) : pp.getValue();
  225.                
  226.                 String apiValue2 = getPropertyProtocolloValue(propertyName2, booleanValue, resource.getProtocolPropertyList());
  227.                
  228.                 addPropertyProtocolloResult(propertyName, propertyName2,
  229.                         apiValue, apiValue2, apiValues);
  230.                
  231.                 break;
  232.             }
  233.         }
  234.     }
  235.     private static void fillPropertyProtocolloSOAP(String propertyName, String propertyName2, AccordoServizioParteComune api, String portType, boolean booleanValue,
  236.             List<String> apiValues) {
  237.         for (PortType pt : api.getPortTypeList()) {
  238.             if(pt.getNome().equals(portType)) {
  239.                
  240.                 for (Operation op : pt.getAzioneList()) {
  241.                     if(op.sizeProtocolPropertyList()>0) {
  242.                         boolean ridefinito = isRidefinito(op.getProtocolPropertyList());
  243.                         if(ridefinito) {
  244.                             fillPropertyProtocolloSOAP(propertyName, propertyName2, booleanValue,
  245.                                     apiValues, op);
  246.                         }
  247.                     }
  248.                 }
  249.                
  250.                 break;
  251.             }
  252.         }
  253.     }
  254.     private static void fillPropertyProtocolloSOAP(String propertyName, String propertyName2, boolean booleanValue,
  255.             List<String> apiValues, Operation op) {
  256.         for (ProtocolProperty pp : op.getProtocolPropertyList()) {
  257.             if(propertyName.equals(pp.getName())) {
  258.                 String apiValue = booleanValue ? getBooleanValueAsString(pp) : pp.getValue();
  259.                
  260.                 String apiValue2 = getPropertyProtocolloValue(propertyName2, booleanValue, op.getProtocolPropertyList());

  261.                 addPropertyProtocolloResult(propertyName, propertyName2,
  262.                         apiValue, apiValue2, apiValues);

  263.                 break;
  264.             }
  265.         }
  266.     }
  267. }