AzioniUtils.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.Collections;
  23. import java.util.HashMap;
  24. import java.util.LinkedHashMap;
  25. import java.util.List;
  26. import java.util.Map;

  27. import org.openspcoop2.core.commons.CoreException;
  28. import org.openspcoop2.core.config.driver.DriverConfigurazioneException;
  29. import org.openspcoop2.core.constants.CostantiDB;
  30. import org.openspcoop2.core.id.IDServizio;
  31. import org.openspcoop2.core.registry.AccordoServizioParteComune;
  32. import org.openspcoop2.core.registry.AccordoServizioParteSpecifica;
  33. import org.openspcoop2.core.registry.Operation;
  34. import org.openspcoop2.core.registry.PortType;
  35. import org.openspcoop2.core.registry.ProtocolProperty;
  36. import org.openspcoop2.core.registry.Resource;
  37. import org.openspcoop2.core.registry.beans.AccordoServizioParteComuneSintetico;
  38. import org.openspcoop2.core.registry.beans.PortTypeSintetico;
  39. import org.openspcoop2.core.registry.driver.IDServizioFactory;
  40. import org.slf4j.Logger;


  41. /**
  42.  * AzioniUtils
  43.  *
  44.  * @author Poli Andrea (apoli@link.it)
  45.  * @author $Author$
  46.  * @version $Rev$, $Date$
  47.  */
  48. public class AzioniUtils {
  49.    
  50.     private AzioniUtils() {}

  51.     public static List<String> getAzioni(AccordoServizioParteSpecifica asps,AccordoServizioParteComuneSintetico aspc,
  52.             boolean addTrattinoSelezioneNonEffettuata, boolean throwException, List<String> filtraAzioniUtilizzate,
  53.             String defaultValueAzioneRisorsaNonSelezionata, Logger log) throws DriverConfigurazioneException{
  54.         String nomeMetodo = "getAzioni";
  55.         try {
  56.             // Prendo le azioni associate al servizio
  57.             List<String> azioniList = null;
  58.             try {
  59.                 if(aspc!=null) {
  60.                     org.openspcoop2.core.registry.constants.ServiceBinding sb = aspc.getServiceBinding();
  61.                     switch (sb) {
  62.                     case SOAP:
  63.                         if (asps != null) {
  64.                            
  65.                             IDServizio idServizio = IDServizioFactory.getInstance().getIDServizioFromAccordo(asps);
  66.                            
  67.                             if(asps.getPortType()!=null){
  68.                                 // Bisogna prendere le operations del port type
  69.                                 PortTypeSintetico pt = null;
  70.                                 for (int i = 0; i < aspc.getPortType().size(); i++) {
  71.                                     if(aspc.getPortType().get(i).getNome().equals(asps.getPortType())){
  72.                                         pt = aspc.getPortType().get(i);
  73.                                         break;
  74.                                     }
  75.                                 }
  76.                                 if(pt==null){
  77.                                     throw new CoreException("Servizio ["+idServizio.toString()+"] possiede il port type ["+asps.getPortType()+"] che non risulta essere registrato nell'accordo di servizio ["+asps.getAccordoServizioParteComune()+"]");
  78.                                 }
  79.                                 if(!pt.getAzione().isEmpty()){
  80.                                     azioniList = new ArrayList<>();
  81.                                     for (int i = 0; i < pt.getAzione().size(); i++) {
  82.                                         if(filtraAzioniUtilizzate==null || !filtraAzioniUtilizzate.contains(pt.getAzione().get(i).getNome())) {
  83.                                             azioniList.add(pt.getAzione().get(i).getNome());
  84.                                         }
  85.                                     }
  86.                                 }
  87.                             }else{
  88.                                 if(!aspc.getAzione().isEmpty()){
  89.                                     azioniList = new ArrayList<>();
  90.                                     for (int i = 0; i < aspc.getAzione().size(); i++) {
  91.                                         if(filtraAzioniUtilizzate==null || !filtraAzioniUtilizzate.contains(aspc.getAzione().get(i).getNome())) {
  92.                                             azioniList.add(aspc.getAzione().get(i).getNome());
  93.                                         }
  94.                                     }
  95.                                 }
  96.                             }              
  97.                         }
  98.                         break;

  99.                     case REST:
  100.                         if(!aspc.getResource().isEmpty()){
  101.                             azioniList = new ArrayList<>();
  102.                             for (int i = 0; i < aspc.getResource().size(); i++) {
  103.                                 if(filtraAzioniUtilizzate==null || !filtraAzioniUtilizzate.contains(aspc.getResource().get(i).getNome())) {
  104.                                     azioniList.add(aspc.getResource().get(i).getNome());
  105.                                 }
  106.                             }
  107.                         }
  108.                         break;
  109.                     }
  110.                    
  111.                 }
  112.             } catch (Exception e) {
  113.                 if(throwException) {
  114.                     throw e;
  115.                 }
  116.             }
  117.            
  118.             List<String> azioniListReturn = null;
  119.             if(azioniList!=null && !azioniList.isEmpty()) {
  120.                 Collections.sort(azioniList);
  121.                
  122.                 azioniListReturn = new ArrayList<>();
  123.                 if(addTrattinoSelezioneNonEffettuata) {
  124.                     azioniListReturn.add(defaultValueAzioneRisorsaNonSelezionata);
  125.                 }
  126.                 azioniListReturn.addAll(azioniList);
  127.             }
  128.                
  129.             return azioniListReturn;
  130.            
  131.         } catch (Exception e) {
  132.             log.error("["+nomeMetodo + "] Exception :" + e.getMessage(), e);
  133.             throw new DriverConfigurazioneException("[" + nomeMetodo + "] Error :" + e.getMessage(),e);
  134.         }
  135.        
  136.     }
  137.    
  138.     public static Map<String,String> getMapAzioni(AccordoServizioParteSpecifica asps,AccordoServizioParteComuneSintetico aspc,
  139.             boolean addTrattinoSelezioneNonEffettuata, boolean throwException, List<String> filtraAzioniUtilizzate,
  140.             boolean sortByLabel, boolean sortFirstByPath, // per soap questi due parametri sono  ininfluenti
  141.             String defaultValueAzioneRisorsaNonSelezionata, String defaultLabelAzioneRisorsaNonSelezionata, Logger log
  142.             ) throws DriverConfigurazioneException{
  143.         String nomeMetodo = "getAzioni";
  144.         try {
  145.             // Prendo le azioni associate al servizio
  146.             Map<String,String> azioniMap = null; // <id,label>
  147.             List<String> sortList = null;
  148.             Map<String,String> sortMap = null; // <sort,id>
  149.             try {
  150.                 if(aspc!=null) {
  151.                     org.openspcoop2.core.registry.constants.ServiceBinding sb = aspc.getServiceBinding();
  152.                     switch (sb) {
  153.                     case SOAP:
  154.                         if (asps != null) {
  155.                            
  156.                             IDServizio idServizio = IDServizioFactory.getInstance().getIDServizioFromAccordo(asps);
  157.                            
  158.                             if(asps.getPortType()!=null){
  159.                                 // Bisogna prendere le operations del port type
  160.                                 PortTypeSintetico pt = null;
  161.                                 for (int i = 0; i < aspc.getPortType().size(); i++) {
  162.                                     if(aspc.getPortType().get(i).getNome().equals(asps.getPortType())){
  163.                                         pt = aspc.getPortType().get(i);
  164.                                         break;
  165.                                     }
  166.                                 }
  167.                                 if(pt==null){
  168.                                     throw new CoreException("Servizio ["+idServizio.toString()+"] possiede il port type ["+asps.getPortType()+"] che non risulta essere registrato nell'accordo di servizio ["+asps.getAccordoServizioParteComune()+"]");
  169.                                 }
  170.                                 if(!pt.getAzione().isEmpty()){
  171.                                     azioniMap = new HashMap<>();
  172.                                     sortList = new ArrayList<>();
  173.                                     for (int i = 0; i < pt.getAzione().size(); i++) {
  174.                                         if(filtraAzioniUtilizzate==null || !filtraAzioniUtilizzate.contains(pt.getAzione().get(i).getNome())) {
  175.                                             sortList.add(pt.getAzione().get(i).getNome());
  176.                                             azioniMap.put(pt.getAzione().get(i).getNome(),pt.getAzione().get(i).getNome());
  177.                                         }
  178.                                     }
  179.                                 }
  180.                             }else{
  181.                                 if(!aspc.getAzione().isEmpty()){
  182.                                     azioniMap = new HashMap<>();
  183.                                     sortList = new ArrayList<>();
  184.                                     for (int i = 0; i < aspc.getAzione().size(); i++) {
  185.                                         if(filtraAzioniUtilizzate==null || !filtraAzioniUtilizzate.contains(aspc.getAzione().get(i).getNome())) {
  186.                                             sortList.add(aspc.getAzione().get(i).getNome());
  187.                                             azioniMap.put(aspc.getAzione().get(i).getNome(),aspc.getAzione().get(i).getNome());
  188.                                         }
  189.                                     }
  190.                                 }
  191.                             }              
  192.                         }
  193.                         break;

  194.                     case REST:
  195.                         if(!aspc.getResource().isEmpty()){
  196.                             azioniMap = new HashMap<>();
  197.                             sortList = new ArrayList<>();
  198.                             if(sortByLabel) {
  199.                                 sortMap = new HashMap<>();
  200.                             }
  201.                             for (int i = 0; i < aspc.getResource().size(); i++) {
  202.                                 if(filtraAzioniUtilizzate==null || !filtraAzioniUtilizzate.contains(aspc.getResource().get(i).getNome())) {
  203.                                     if(sortByLabel) {
  204.                                         String sortLabelId = null;
  205.                                         if(!sortFirstByPath) {
  206.                                             sortLabelId = NamingUtils.getLabelResource(aspc.getResource().get(i));
  207.                                         }
  208.                                         else {
  209.                                             String path = aspc.getResource().get(i).getPath()!=null ? aspc.getResource().get(i).getPath() : CostantiDB.API_RESOURCE_PATH_ALL_VALUE;
  210.                                             String method = aspc.getResource().get(i).getMethod()!=null ? aspc.getResource().get(i).getMethod().getValue() : CostantiDB.API_RESOURCE_HTTP_METHOD_ALL_VALUE;
  211.                                             sortLabelId = path+" "+method;
  212.                                         }
  213.                                         sortList.add(sortLabelId);
  214.                                         sortMap.put(sortLabelId, aspc.getResource().get(i).getNome());
  215.                                     }
  216.                                     else {
  217.                                         sortList.add(aspc.getResource().get(i).getNome());
  218.                                     }
  219.                                     azioniMap.put(aspc.getResource().get(i).getNome(),NamingUtils.getLabelResource(aspc.getResource().get(i)));
  220.                                 }
  221.                             }
  222.                         }
  223.                         break;
  224.                     }
  225.                    
  226.                 }
  227.             } catch (Exception e) {
  228.                 if(throwException) {
  229.                     throw e;
  230.                 }
  231.             }
  232.            
  233.             Map<String, String> mapAzioniReturn = new LinkedHashMap<>();
  234.             if(sortList!=null && !sortList.isEmpty()) {
  235.                 Collections.sort(sortList);
  236.                
  237.                 if(addTrattinoSelezioneNonEffettuata) {
  238.                     mapAzioniReturn.put(defaultValueAzioneRisorsaNonSelezionata,defaultLabelAzioneRisorsaNonSelezionata);
  239.                 }
  240.                
  241.                 if(sortMap!=null) {
  242.                     for (String idSort : sortList) {
  243.                         String id  = sortMap.get(idSort);
  244.                         mapAzioniReturn.put(id, azioniMap.get(id));
  245.                     }
  246.                 }
  247.                 else {
  248.                     for (String id : sortList) { // nelle sortList ci sono gli id
  249.                         mapAzioniReturn.put(id, azioniMap.get(id));
  250.                     }
  251.                 }
  252.             }
  253.                
  254.             return mapAzioniReturn;
  255.            
  256.         } catch (Exception e) {
  257.             log.error("[ControlStationCore::" + nomeMetodo + "] Exception :" + e.getMessage(), e);
  258.             throw new DriverConfigurazioneException("[ControlStationCore::" + nomeMetodo + "] Error :" + e.getMessage(),e);
  259.         }
  260.     }
  261.    
  262.     public static Map<String,String> getAzioniConLabel(AccordoServizioParteSpecifica asps,AccordoServizioParteComuneSintetico aspc,
  263.             boolean addTrattinoSelezioneNonEffettuata, boolean throwException, List<String> filtraAzioniUtilizzate,
  264.             String defaultValueAzioneRisorsaNonSelezionata, String defaultLabelAzioneRisorsaNonSelezionata, Logger log) throws DriverConfigurazioneException {
  265.         return getMapAzioni(asps, aspc, addTrattinoSelezioneNonEffettuata, throwException, filtraAzioniUtilizzate,
  266.                 true, true,
  267.                 defaultValueAzioneRisorsaNonSelezionata, defaultLabelAzioneRisorsaNonSelezionata, log);
  268.     }
  269.    
  270.    
  271.    
  272.     public static String getProtocolPropertyStringValue(AccordoServizioParteComune aspc, String idPortType, String idAzione, String propertyName) {
  273.         if(idPortType!=null) {
  274.             for (PortType pt : aspc.getPortTypeList()) {
  275.                 if(pt.getNome().equals(idPortType)) {
  276.                     for (Operation op : pt.getAzioneList()) {
  277.                         if(op.getNome().equals(idAzione)) {
  278.                             for (ProtocolProperty pp : op.getProtocolPropertyList()) {
  279.                                 if(pp.getName().equals(propertyName)) {
  280.                                     return pp.getValue();
  281.                                 }
  282.                             }
  283.                             break;
  284.                         }
  285.                     }
  286.                     break;
  287.                 }
  288.             }
  289.         }
  290.         else {
  291.             for (Resource resource : aspc.getResourceList()) {
  292.                 if(resource.getNome().equals(idAzione)) {
  293.                     for (ProtocolProperty pp : resource.getProtocolPropertyList()) {
  294.                         if(pp.getName().equals(propertyName)) {
  295.                             return pp.getValue();
  296.                         }
  297.                     }
  298.                     break;
  299.                 }
  300.             }
  301.         }
  302.         return null;
  303.     }
  304.    
  305. }