ConnettoreUtils.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.pdd.core.connettori;

  21. import java.util.HashMap;
  22. import java.util.Iterator;
  23. import java.util.List;
  24. import java.util.Map;

  25. import org.apache.commons.lang.StringUtils;
  26. import org.openspcoop2.core.config.GenericProperties;
  27. import org.openspcoop2.core.config.InvocazioneCredenziali;
  28. import org.openspcoop2.core.config.InvocazioneServizio;
  29. import org.openspcoop2.core.config.ServizioApplicativo;
  30. import org.openspcoop2.core.config.constants.CostantiConfigurazione;
  31. import org.openspcoop2.core.config.constants.StatoFunzionalita;
  32. import org.openspcoop2.core.constants.CostantiConnettori;
  33. import org.openspcoop2.core.constants.CostantiLabel;
  34. import org.openspcoop2.core.constants.TipiConnettore;
  35. import org.openspcoop2.message.OpenSPCoop2Message;
  36. import org.openspcoop2.message.OpenSPCoop2MessageProperties;
  37. import org.openspcoop2.message.constants.ServiceBinding;
  38. import org.openspcoop2.message.rest.RestUtilities;
  39. import org.openspcoop2.pdd.config.ForwardProxy;
  40. import org.openspcoop2.pdd.config.OpenSPCoop2Properties;
  41. import org.openspcoop2.pdd.core.CostantiPdD;
  42. import org.openspcoop2.pdd.core.PdDContext;
  43. import org.openspcoop2.pdd.core.dynamic.DynamicUtils;
  44. import org.openspcoop2.pdd.core.token.PolicyNegoziazioneToken;
  45. import org.openspcoop2.pdd.core.token.TokenUtilities;
  46. import org.openspcoop2.pdd.logger.OpenSPCoop2Logger;
  47. import org.openspcoop2.pdd.mdb.ConsegnaContenutiApplicativi;
  48. import org.openspcoop2.protocol.sdk.Busta;
  49. import org.openspcoop2.protocol.sdk.Context;
  50. import org.openspcoop2.protocol.sdk.IProtocolFactory;
  51. import org.openspcoop2.protocol.sdk.ProtocolException;
  52. import org.openspcoop2.protocol.utils.PorteNamingUtils;
  53. import org.openspcoop2.utils.Utilities;
  54. import org.openspcoop2.utils.certificate.byok.BYOKManager;
  55. import org.openspcoop2.utils.certificate.hsm.HSMUtils;
  56. import org.openspcoop2.utils.certificate.ocsp.OCSPManager;
  57. import org.openspcoop2.utils.transport.TransportUtils;
  58. import org.openspcoop2.utils.transport.http.HttpRequestMethod;
  59. import org.slf4j.Logger;


  60. /**
  61.  * ConnettoreUtils
  62.  *
  63.  *
  64.  * @author Poli Andrea (apoli@link.it)
  65.  * @author $Author$
  66.  * @version $Rev$, $Date$
  67.  */
  68. public class ConnettoreUtils {

  69.     public static String formatLocation(HttpRequestMethod httpMethod, String location){
  70.         if(httpMethod!=null)
  71.             return location+" http-method:"+httpMethod;
  72.         else
  73.             return location;
  74.     }
  75.    
  76.     public static String getAndReplaceLocationWithBustaValues(IConnettore connector, ConnettoreMsg connettoreMsg,Busta busta,PdDContext pddContext,IProtocolFactory<?> protocolFactory,Logger log) throws ConnettoreException{
  77.        
  78.         boolean dynamicLocation = false;
  79.        
  80.         String location = null;
  81.         if(TipiConnettore.NULL.getNome().equals(connettoreMsg.getTipoConnettore())){
  82.             location = ConnettoreNULL.LOCATION;
  83.         }
  84.         else if(TipiConnettore.NULLECHO.getNome().equals(connettoreMsg.getTipoConnettore())){
  85.             location = ConnettoreNULLEcho.LOCATION;
  86.         }
  87.         else if(TipiConnettore.STATUS.getNome().equals(connettoreMsg.getTipoConnettore())){
  88.             location = ConnettoreStatus.LOCATION;
  89.         }
  90.         else if(ConnettoreStresstest.ENDPOINT_TYPE.equals(connettoreMsg.getTipoConnettore())){
  91.             location = ConnettoreStresstest.LOCATION;
  92.         }
  93.         else if(TipiConnettore.FILE.getNome().equals(connettoreMsg.getTipoConnettore())){
  94.             try{
  95.                 location = ((ConnettoreFILE)connector).buildLocation(connettoreMsg);
  96.                 //dynamicLocation = true; la dinamicita viene gia gestita nel metodo buildLocation
  97.             }catch(Exception e){
  98.                 log.error("Errore durante la costruzione della location: "+e.getMessage(),e);
  99.                 location = "N.D.";
  100.             }
  101.         }
  102.         else if(ConnettoreRicezioneBusteDirectVM.TIPO.equals(connettoreMsg.getTipoConnettore())){
  103.             try{
  104.                 ((ConnettoreRicezioneBusteDirectVM)connector).validate(connettoreMsg);
  105.                 ((ConnettoreRicezioneBusteDirectVM)connector).buildLocation(connettoreMsg.getConnectorProperties(),false);
  106.                 location = connector.getLocation();
  107.                 dynamicLocation = true;
  108.             }catch(Exception e){
  109.                 log.error("Errore durante la costruzione della location: "+e.getMessage(),e);
  110.                 location = "N.D.";
  111.             }
  112.         }
  113.         else if(ConnettoreRicezioneContenutiApplicativiDirectVM.TIPO.equals(connettoreMsg.getTipoConnettore())){
  114.             try{
  115.                 ((ConnettoreRicezioneContenutiApplicativiDirectVM)connector).validate(connettoreMsg);
  116.                 ((ConnettoreRicezioneContenutiApplicativiDirectVM)connector).buildLocation(connettoreMsg.getConnectorProperties(),false);
  117.                 location = connector.getLocation();
  118.                 dynamicLocation = true;
  119.             }catch(Exception e){
  120.                 log.error("Errore durante la costruzione della location: "+e.getMessage(),e);
  121.                 location = "N.D.";
  122.             }
  123.         }
  124.         else if(ConnettoreRicezioneContenutiApplicativiHTTPtoSOAPDirectVM.TIPO.equals(connettoreMsg.getTipoConnettore())){
  125.             try{
  126.                 ((ConnettoreRicezioneContenutiApplicativiHTTPtoSOAPDirectVM)connector).validate(connettoreMsg);
  127.                 ((ConnettoreRicezioneContenutiApplicativiHTTPtoSOAPDirectVM)connector).buildLocation(connettoreMsg.getConnectorProperties(),false);
  128.                 location = connector.getLocation();
  129.                 dynamicLocation = true;
  130.             }catch(Exception e){
  131.                 log.error("Errore durante la costruzione della location: "+e.getMessage(),e);
  132.                 location = "N.D.";
  133.             }
  134.         }
  135.         else{
  136.             if(connettoreMsg.getConnectorProperties().get(CostantiConnettori.CONNETTORE_LOCATION)!=null){
  137.                 location = connettoreMsg.getConnectorProperties().get(CostantiConnettori.CONNETTORE_LOCATION);
  138.                 dynamicLocation = true; // http, jms, ...
  139.             }
  140.         }
  141.        
  142.         if(location !=null && (location.equals("")==false) ){
  143.            
  144.             // Keyword old
  145.             location = location.replace(CostantiConnettori.CONNETTORE_JMS_LOCATION_REPLACE_TOKEN_TIPO_SERVIZIO,busta.getTipoServizio());
  146.             location = location.replace(CostantiConnettori.CONNETTORE_JMS_LOCATION_REPLACE_TOKEN_NOME_SERVIZIO,busta.getServizio());
  147.             if(busta.getAzione()!=null){
  148.                 location = location.replace(CostantiConnettori.CONNETTORE_JMS_LOCATION_REPLACE_TOKEN_AZIONE,busta.getAzione());
  149.             }
  150.            
  151.             // Dynamic
  152.             // Costruisco Mappa per dynamic name
  153.             if(dynamicLocation) {
  154.                 try {
  155.                     Map<String, Object> dynamicMap = ((ConnettoreBase)connector).buildDynamicMap(connettoreMsg);
  156.                     location = DynamicUtils.convertDynamicPropertyValue(CostantiConnettori.CONNETTORE_LOCATION, location, dynamicMap, pddContext, false);
  157.                 }catch(Exception e){
  158.                     log.error("Errore durante la costruzione della location (dynamic): "+e.getMessage(),e);
  159.                 }
  160.             }
  161.            
  162.             connettoreMsg.getConnectorProperties().put(CostantiConnettori.CONNETTORE_LOCATION,location);
  163.         }
  164.        
  165.         return location;
  166.     }
  167.    
  168.     public static String buildLocationWithURLBasedParameter(Logger log, OpenSPCoop2Message msg, String tipoConnettore, Map<String, List<String>> propertiesURLBased, String locationParam,
  169.             IProtocolFactory<?> protocolFactory, String idModulo) throws ConnettoreException{
  170.        
  171.         if(TipiConnettore.HTTP.toString().equals(tipoConnettore) ||
  172.                 TipiConnettore.HTTPS.toString().equals(tipoConnettore) ||
  173.                 ConnettoreHTTPCORE.ENDPOINT_TYPE.equals(tipoConnettore) ||
  174.                 ConnettoreSAAJ.ENDPOINT_TYPE.equals(tipoConnettore)  ||
  175.                 ConnettoreStresstest.ENDPOINT_TYPE.equals(tipoConnettore)){
  176.    
  177.             try{
  178.            
  179.                 OpenSPCoop2MessageProperties forwardParameter = null;
  180.                 if(ServiceBinding.REST.equals(msg.getServiceBinding())) {
  181.                     forwardParameter = msg.getForwardUrlProperties(OpenSPCoop2Properties.getInstance().getRESTServicesUrlParametersForwardConfig());
  182.                 }
  183.                 else {
  184.                     forwardParameter = msg.getForwardUrlProperties(OpenSPCoop2Properties.getInstance().getSOAPServicesUrlParametersForwardConfig());
  185.                 }
  186.                
  187.                 Map<String, List<String>>  p = propertiesURLBased;
  188.                 if(forwardParameter!=null && forwardParameter.size()>0){
  189.                     if(p==null){
  190.                         p = new HashMap<> ();
  191.                     }
  192.                     Iterator<String> keys = forwardParameter.getKeys();
  193.                     while (keys.hasNext()) {
  194.                         String key = (String) keys.next();
  195.                         List<String> values = forwardParameter.getPropertyValues(key);
  196.                         if(values!=null && !values.isEmpty()){
  197.                             if(p.containsKey(key)){
  198.                                 p.remove(key);
  199.                             }
  200.                             p.put(key, values);
  201.                         }
  202.                     }
  203.                 }
  204.                
  205.                 String location = locationParam;
  206.                 if(ServiceBinding.REST.equals(msg.getServiceBinding())){
  207.                    
  208.                     String normalizedInterfaceName = normalizeInterfaceName(msg, idModulo, protocolFactory);
  209.                    
  210.                     return RestUtilities.buildUrl(log, location, p, msg.getTransportRequestContext(),
  211.                             normalizedInterfaceName);
  212.                 }
  213.                 else{
  214.                     boolean encodeBaseLocation = true; // la base location può contenere dei parametri
  215.                     return TransportUtils.buildUrlWithParameters(p, location, encodeBaseLocation, OpenSPCoop2Logger.getLoggerOpenSPCoopCore());
  216.                 }
  217.                
  218.             }catch(Exception e){
  219.                 throw new ConnettoreException(e.getMessage(),e);
  220.             }
  221.         }
  222.         return locationParam;
  223.     }

  224.     public static String normalizeInterfaceName(OpenSPCoop2Message msg, String idModulo, IProtocolFactory<?> protocolFactory) throws ProtocolException {
  225.         Object nomePortaInvocataObject = msg.getContextProperty(CostantiPdD.NOME_PORTA_INVOCATA);
  226.         String nomePortaInvocata = null;
  227.         if(nomePortaInvocataObject!=null && nomePortaInvocataObject instanceof String) {
  228.             nomePortaInvocata = (String) nomePortaInvocataObject;
  229.         }
  230.         else if(msg.getTransportRequestContext()!=null && msg.getTransportRequestContext().getInterfaceName()!=null) {
  231.             nomePortaInvocata = msg.getTransportRequestContext().getInterfaceName();
  232.         }
  233.        
  234.         String normalizedInterfaceName = null;
  235.         if(nomePortaInvocata!=null) {
  236.             PorteNamingUtils namingUtils = new PorteNamingUtils(protocolFactory);
  237.             if(ConsegnaContenutiApplicativi.ID_MODULO.equals(idModulo)){
  238.                 normalizedInterfaceName = namingUtils.normalizePA(nomePortaInvocata);
  239.             }
  240.             else {
  241.                 normalizedInterfaceName = namingUtils.normalizePD(nomePortaInvocata);
  242.             }
  243.         }
  244.        
  245.         return normalizedInterfaceName;
  246.     }
  247.    
  248.     public static String limitLocation255Character(String location){
  249.         return TransportUtils.limitLocation255Character(location);
  250.     }
  251.    
  252.     public static String addProxyInfoToLocationForHTTPConnector(String tipoConnettore, Map<String, String> properties, String location){
  253.         if(TipiConnettore.HTTP.toString().equals(tipoConnettore) ||
  254.                 TipiConnettore.HTTPS.toString().equals(tipoConnettore)){
  255.             if(properties.get(CostantiConnettori.CONNETTORE_HTTP_PROXY_TYPE)!=null){
  256.                 String proxyHostname = properties.get(CostantiConnettori.CONNETTORE_HTTP_PROXY_HOSTNAME);
  257.                 String proxyPort = properties.get(CostantiConnettori.CONNETTORE_HTTP_PROXY_PORT);
  258.                 return location+" [proxy: "+proxyHostname+":"+proxyPort+"]";
  259.             }
  260.         }
  261.         return location;
  262.     }
  263.    
  264.     public static String addGovWayProxyInfoToLocationForHTTPConnector(ForwardProxy forwardProxy, IConnettore connectorSender, String location) throws ConnettoreException {
  265.         if(forwardProxy!=null && connectorSender instanceof ConnettoreBaseHTTP) {
  266.             ConnettoreBaseHTTP http = (ConnettoreBaseHTTP) connectorSender;
  267.             http.updateForwardProxy(forwardProxy);
  268.             if(http.updateLocation_forwardProxy(location)) {
  269.                 return http.getLocation();
  270.             }
  271.         }
  272.         return location;
  273.     }
  274.    

  275.     public static void printDatiConnettore(org.openspcoop2.core.registry.Connettore connettore, String labelTipoConnettore, String labelNomeConnettore,
  276.             boolean connettoreStatic,
  277.             StringBuilder sb,
  278.             String separator, String newLine,
  279.             boolean printIntestazione) {
  280.         printDatiConnettore(connettore.mappingIntoConnettoreConfigurazione(), labelTipoConnettore, labelNomeConnettore,
  281.                 null, connettoreStatic,
  282.                 sb,
  283.                 separator, newLine,
  284.                 printIntestazione);
  285.     }
  286.     public static void printDatiConnettore(org.openspcoop2.core.config.Connettore connettore, String labelTipoConnettore, String labelNomeConnettore,
  287.             ServizioApplicativo sa,
  288.             boolean connettoreStatic,
  289.             StringBuilder sb,
  290.             String separator, String newLine,
  291.             boolean printIntestazione) {

  292.         if(connettoreStatic) {
  293.             return; // non visualizzo nulla
  294.         }
  295.        
  296.         Boolean integrationManager = null;
  297.         InvocazioneCredenziali invCredenziali = null;
  298.         if(sa!=null) {
  299.             InvocazioneServizio is = sa.getInvocazioneServizio();
  300.             integrationManager = is.getGetMessage()!=null && StatoFunzionalita.ABILITATO.equals(is.getGetMessage());
  301.             invCredenziali = is.getCredenziali();
  302.         }
  303.        
  304.         TipiConnettore tipo = TipiConnettore.toEnumFromName(connettore.getTipo());
  305.         if(tipo==null) {
  306.             tipo = TipiConnettore.CUSTOM;
  307.         }
  308.        
  309.         if(printIntestazione) {
  310.             sb.append(newLine);
  311.             String label = labelNomeConnettore!=null ? " "+labelNomeConnettore : "";
  312.             sb.append("- "+CostantiLabel.LABEL_CONNETTORE+label+" -");
  313.         }
  314.        
  315.         String tipoConnettore = labelTipoConnettore;
  316.         if(TipiConnettore.DISABILITATO.equals(tipo)) {
  317.             if(integrationManager!=null && integrationManager) {
  318.                 tipoConnettore = "MessageBox";
  319.             }
  320.         }
  321.         else {
  322.             if(integrationManager!=null && integrationManager) {
  323.                 tipoConnettore = tipoConnettore + " + MessageBox";
  324.             }
  325.         }
  326.         sb.append(newLine);
  327.         sb.append(CostantiLabel.LABEL_TIPO_CONNETTORE);
  328.         sb.append(separator);
  329.         sb.append(tipoConnettore);
  330.        
  331.         String labelEndpoint = CostantiLabel.LABEL_CONNETTORE_ENDPOINT;
  332.         String endpoint = null;
  333.         if(TipiConnettore.HTTP.getNome().equals(connettore.getTipo()) || TipiConnettore.HTTPS.getNome().equals(connettore.getTipo())){
  334.             endpoint = getProperty(CostantiConnettori.CONNETTORE_LOCATION, connettore.getPropertyList());
  335.         }
  336.         else if(TipiConnettore.JMS.getNome().equals(connettore.getTipo())){
  337.             String tipoCoda = getProperty(CostantiConnettori.CONNETTORE_JMS_TIPO, connettore.getPropertyList());
  338.             labelEndpoint = CostantiLabel.LABEL_CONNETTORE_JMS_NOME_CODA+" "+tipoCoda;
  339.             endpoint = getProperty(CostantiConnettori.CONNETTORE_LOCATION, connettore.getPropertyList());
  340.         }
  341.         else if(TipiConnettore.FILE.getNome().equals(connettore.getTipo())){
  342.             labelEndpoint = CostantiLabel.LABEL_OUTPUT_FILE;
  343.             endpoint = getProperty(CostantiConnettori.CONNETTORE_FILE_REQUEST_OUTPUT_FILE, connettore.getPropertyList());
  344.         }
  345.         else if(TipiConnettore.NULL.getNome().equals(connettore.getTipo())){
  346.             //endpoint = org.openspcoop2.pdd.core.connettori.ConnettoreNULL.LOCATION;
  347.         }
  348.         else if(TipiConnettore.NULLECHO.getNome().equals(connettore.getTipo())){
  349.             //endpoint = org.openspcoop2.pdd.core.connettori.ConnettoreNULLEcho.LOCATION;
  350.         }
  351.         else if(TipiConnettore.STATUS.getNome().equals(connettore.getTipo())){
  352.             //endpoint = org.openspcoop2.pdd.core.connettori.ConnettoreStatus.LOCATION;
  353.         }
  354.         else {
  355.             String endpointV = getProperty(CostantiConnettori.CONNETTORE_LOCATION, connettore.getPropertyList());
  356.             if(StringUtils.isNotEmpty(endpointV)) {
  357.                 endpoint = endpointV;
  358.             }
  359.         }
  360.         if(endpoint!=null) {
  361.             sb.append(newLine);
  362.             sb.append(labelEndpoint);
  363.             sb.append(separator);
  364.             sb.append(endpoint);
  365.         }
  366.        
  367.         if(sa!=null && CostantiConfigurazione.SERVER.equals(sa.getTipo())) {
  368.             sb.append(newLine);
  369.             sb.append(CostantiLabel.LABEL_SERVER);
  370.             sb.append(separator);
  371.             sb.append(sa.getNome());
  372.         }
  373.        
  374.         String connectionTimeout = getProperty(CostantiConnettori.CONNETTORE_CONNECTION_TIMEOUT, connettore.getPropertyList());
  375.         if(connectionTimeout!=null) {
  376.             try {
  377.                 long l = Long.valueOf(connectionTimeout);
  378.                 if(l>0) {
  379.                     sb.append(newLine);
  380.                     sb.append(CostantiLabel.LABEL_CONNETTORE_TEMPI_RISPOSTA_CONNECTION_TIMEOUT);
  381.                     sb.append(separator);
  382.                     sb.append(Utilities.convertSystemTimeIntoStringMillisecondi(l, true, false, " "," ",""));
  383.                 }
  384.             }catch(Throwable t) {}
  385.         }
  386.         String readConnectionTimeout = getProperty(CostantiConnettori.CONNETTORE_READ_CONNECTION_TIMEOUT, connettore.getPropertyList());
  387.         if(readConnectionTimeout!=null) {
  388.             try {
  389.                 long l = Long.valueOf(readConnectionTimeout);
  390.                 if(l>0) {
  391.                     sb.append(newLine);
  392.                     sb.append(CostantiLabel.LABEL_CONNETTORE_TEMPI_RISPOSTA_READ_TIMEOUT);
  393.                     sb.append(separator);
  394.                     sb.append(Utilities.convertSystemTimeIntoStringMillisecondi(l, true, false, " "," ",""));
  395.                 }
  396.             }catch(Throwable t) {}
  397.         }
  398.        
  399.         if(TipiConnettore.HTTP.getNome().equals(connettore.getTipo()) || TipiConnettore.HTTPS.getNome().equals(connettore.getTipo())){
  400.            
  401.             String token = getProperty(CostantiConnettori.CONNETTORE_TOKEN_POLICY, connettore.getPropertyList());
  402.             if(token!=null) {
  403.                 sb.append(newLine);
  404.                 sb.append(CostantiLabel.LABEL_VERIFICA_CONNETTORE_DETAILS_TOKEN).append(" - ").append(CostantiLabel.LABEL_CONNETTORE_TOKEN_POLICY);
  405.                 sb.append(separator);
  406.                 sb.append(token);
  407.             }
  408.            
  409.             String username = null;
  410.             if(invCredenziali!=null){
  411.                 username = invCredenziali.getUser();
  412.             }
  413.             else{
  414.                 username = getProperty(CostantiConnettori.CONNETTORE_USERNAME, connettore.getPropertyList());
  415.             }
  416.             if(username!=null) {
  417.                 sb.append(newLine);
  418.                 sb.append(CostantiLabel.LABEL_VERIFICA_CONNETTORE_DETAILS_HTTP).append(" - ").append(CostantiLabel.LABEL_CREDENZIALI_AUTENTICAZIONE_USERNAME);
  419.                 sb.append(separator);
  420.                 sb.append(username);
  421.             }
  422.            
  423.             String bearerToken = getProperty(CostantiConnettori.CONNETTORE_BEARER_TOKEN, connettore.getPropertyList());
  424.             if(bearerToken!=null) {
  425.                 sb.append(newLine);
  426.                 sb.append(CostantiLabel.LABEL_VERIFICA_CONNETTORE_DETAILS_BEARER).append(" ").append(CostantiLabel.LABEL_CONNETTORE_BEARER_TOKEN);
  427.                 sb.append(separator);
  428.                 sb.append(bearerToken);
  429.             }
  430.            
  431.             String apiKey = getProperty(CostantiConnettori.CONNETTORE_APIKEY, connettore.getPropertyList());
  432.             if(apiKey!=null && StringUtils.isNotEmpty(apiKey)){
  433.                
  434.                 String apiKeyHeader = getProperty(CostantiConnettori.CONNETTORE_APIKEY_HEADER, connettore.getPropertyList());
  435.                 if(apiKeyHeader==null || StringUtils.isEmpty(apiKeyHeader)) {
  436.                     apiKeyHeader = CostantiConnettori.DEFAULT_HEADER_API_KEY;
  437.                 }
  438.                
  439.                 sb.append(newLine);
  440.                 sb.append(CostantiLabel.LABEL_VERIFICA_CONNETTORE_DETAILS_API_KEY).append(" '").append(apiKeyHeader).append("'");
  441.                 /**sb.append(separator);
  442.                 sb.append(apiKey); Informazione sensibile*/
  443.                
  444.                 String appId = getProperty(CostantiConnettori.CONNETTORE_APIKEY_APPID, connettore.getPropertyList());
  445.                 if(appId!=null && StringUtils.isNotEmpty(appId)){
  446.                    
  447.                     String appIdHeader = getProperty(CostantiConnettori.CONNETTORE_APIKEY_APPID_HEADER, connettore.getPropertyList());
  448.                     if(appIdHeader==null || StringUtils.isEmpty(appIdHeader)) {
  449.                         appIdHeader = CostantiConnettori.DEFAULT_HEADER_APP_ID;
  450.                     }
  451.                    
  452.                     sb.append(newLine);
  453.                     sb.append(CostantiLabel.LABEL_VERIFICA_CONNETTORE_DETAILS_API_KEY_APP_ID).append(" '").append(appIdHeader).append("'");
  454.                     sb.append(separator);
  455.                     sb.append(appId);
  456.                    
  457.                 }
  458.             }
  459.            
  460.         }
  461.        
  462.         if(TipiConnettore.HTTPS.getNome().equals(connettore.getTipo())){
  463.            
  464.             boolean trustAllCerts = false;
  465.             String trustAllCertsV = getProperty(CostantiConnettori.CONNETTORE_HTTPS_TRUST_ALL_CERTS, connettore.getPropertyList());
  466.             if("true".equalsIgnoreCase(trustAllCertsV)) {
  467.                 trustAllCerts = true;
  468.             }
  469.            
  470.             String trustLocation = getProperty(CostantiConnettori.CONNETTORE_HTTPS_TRUST_STORE_LOCATION, connettore.getPropertyList());
  471.            
  472.             if(trustAllCerts || trustLocation!=null) {
  473.                
  474.                 String sslType = getProperty(CostantiConnettori.CONNETTORE_HTTPS_SSL_TYPE, connettore.getPropertyList());
  475.                 if(sslType!=null) {
  476.                     sb.append(newLine);
  477.                     sb.append(CostantiLabel.LABEL_VERIFICA_CONNETTORE_DETAILS_HTTPS);
  478.                     sb.append(separator);
  479.                     sb.append(sslType);
  480.                 }
  481.                
  482.                 String hostnameVerifier = getProperty(CostantiConnettori.CONNETTORE_HTTPS_HOSTNAME_VERIFIER, connettore.getPropertyList());
  483.                 if(hostnameVerifier!=null) {
  484.                     sb.append(newLine);
  485.                     sb.append(CostantiLabel.LABEL_CONNETTORE_HTTPS_HOST_VERIFY);
  486.                     sb.append(separator);
  487.                     sb.append(hostnameVerifier);
  488.                 }
  489.                
  490.                 sb.append(newLine);
  491.                 sb.append(CostantiLabel.LABEL_VERIFICA_CONNETTORE_DETAILS_HTTPS_TRUSTSTORE);
  492.                 sb.append(separator);
  493.                 if(trustAllCerts) {
  494.                     sb.append(CostantiLabel.LABEL_VERIFICA_CONNETTORE_DETAILS_HTTPS_TRUST_ALL_CERTS);
  495.                 }
  496.                 else {
  497.                     String trustType = getProperty(CostantiConnettori.CONNETTORE_HTTPS_TRUST_STORE_TYPE, connettore.getPropertyList());
  498.                     boolean hsm = HSMUtils.isKeystoreHSM(trustType);
  499.                     if(hsm) {
  500.                         trustLocation = CostantiLabel.CONNETTORE_HSM;
  501.                     }
  502.                     sb.append("(").append(trustType).append(") ").append(trustLocation);
  503.                 }
  504.                
  505.                 String trustCRL = getProperty(CostantiConnettori.CONNETTORE_HTTPS_TRUST_STORE_CRLS, connettore.getPropertyList());
  506.                 if(trustCRL!=null) {
  507.                     sb.append(newLine);
  508.                     sb.append(CostantiLabel.LABEL_VERIFICA_CONNETTORE_DETAILS_HTTPS_TRUSTSTORE_CRLS);
  509.                     sb.append(separator);
  510.                     sb.append(trustCRL);
  511.                 }
  512.                
  513.                 String trustOCSP = getProperty(CostantiConnettori.CONNETTORE_HTTPS_TRUST_STORE_OCSP_POLICY, connettore.getPropertyList());
  514.                 if(trustOCSP!=null) {
  515.                     sb.append(newLine);
  516.                     sb.append(CostantiLabel.LABEL_VERIFICA_CONNETTORE_DETAILS_HTTPS_TRUSTSTORE_OCSP_POLICY);
  517.                     sb.append(separator);
  518.                     try {
  519.                         String label = OCSPManager.getInstance().getOCSPConfig(trustOCSP).getLabel();
  520.                         sb.append((label!=null && StringUtils.isNotEmpty(label)) ? label : trustOCSP);
  521.                     }catch(Throwable t) {
  522.                         sb.append(trustOCSP);  
  523.                     }
  524.                 }
  525.             }
  526.            
  527.             String keyLocation = getProperty(CostantiConnettori.CONNETTORE_HTTPS_KEY_STORE_LOCATION, connettore.getPropertyList());
  528.             if(keyLocation!=null) {
  529.                
  530.                 sb.append(newLine);
  531.                 sb.append(CostantiLabel.LABEL_VERIFICA_CONNETTORE_DETAILS_HTTPS_KEYSTORE);
  532.                 sb.append(separator);
  533.                
  534.                 String keyType = getProperty(CostantiConnettori.CONNETTORE_HTTPS_KEY_STORE_TYPE, connettore.getPropertyList());
  535.                 boolean hsm = HSMUtils.isKeystoreHSM(keyType);
  536.                 if(hsm) {
  537.                     keyLocation = CostantiLabel.CONNETTORE_HSM;
  538.                 }
  539.                 sb.append("(").append(keyType).append(") ").append(keyLocation);
  540.                
  541.                 String keyAlias = getProperty(CostantiConnettori.CONNETTORE_HTTPS_KEY_ALIAS, connettore.getPropertyList());
  542.                 if(keyAlias!=null) {
  543.                     sb.append(newLine);
  544.                     sb.append(CostantiLabel.LABEL_VERIFICA_CONNETTORE_DETAILS_HTTPS_KEY_ALIAS);
  545.                     sb.append(separator);
  546.                     sb.append(keyAlias);
  547.                 }
  548.                
  549.                 String keyBYOK = getProperty(CostantiConnettori.CONNETTORE_HTTPS_KEY_STORE_BYOK_POLICY, connettore.getPropertyList());
  550.                 if(keyBYOK!=null) {
  551.                     sb.append(newLine);
  552.                     sb.append(CostantiLabel.LABEL_VERIFICA_CONNETTORE_DETAILS_HTTPS_KEYSTORE_BYOK_POLICY);
  553.                     sb.append(separator);
  554.                     try {
  555.                         String label = BYOKManager.getInstance().getKMSConfigByType(keyBYOK).getLabel();
  556.                         sb.append((label!=null && StringUtils.isNotEmpty(label)) ? label : keyBYOK);
  557.                     }catch(Exception t) {
  558.                         sb.append(keyBYOK);
  559.                     }
  560.                 }
  561.             }
  562.         }
  563.        
  564.         if(TipiConnettore.HTTP.getNome().equals(connettore.getTipo()) || TipiConnettore.HTTPS.getNome().equals(connettore.getTipo())){
  565.             String proxyHostname = getProperty(CostantiConnettori.CONNETTORE_HTTP_PROXY_HOSTNAME, connettore.getPropertyList());
  566.             if(proxyHostname!=null) {
  567.                 sb.append(newLine);
  568.                 sb.append(CostantiLabel.LABEL_VERIFICA_CONNETTORE_DETAILS_PROXY);
  569.                 sb.append(separator);
  570.                 String proxyPort = getProperty(CostantiConnettori.CONNETTORE_HTTP_PROXY_PORT, connettore.getPropertyList());
  571.                 if(proxyPort!=null) {
  572.                     sb.append(proxyHostname).append(":").append(proxyPort);
  573.                 }
  574.             }
  575.         }
  576.        
  577.         if(TipiConnettore.JMS.getNome().equals(connettore.getTipo())){
  578.              
  579.             String username = null;
  580.             if(invCredenziali!=null){
  581.                 username = invCredenziali.getUser();
  582.             }
  583.             else{
  584.                 username = getProperty(CostantiConnettori.CONNETTORE_USERNAME, connettore.getPropertyList());
  585.             }
  586.             if(username!=null) {
  587.                 sb.append(newLine);
  588.                 sb.append(CostantiLabel.LABEL_CONNETTORE_JMS_USERNAME);
  589.                 sb.append(separator);
  590.                 sb.append(username);
  591.             }
  592.            
  593.         }

  594.         if(TipiConnettore.FILE.getNome().equals(connettore.getTipo())){
  595.            
  596.             String f = getProperty(CostantiConnettori.CONNETTORE_FILE_REQUEST_OUTPUT_FILE_HEADERS, connettore.getPropertyList());
  597.             if(f!=null) {
  598.                 sb.append(newLine);
  599.                 sb.append(CostantiLabel.LABEL_OUTPUT_FILE_HEADER);
  600.                 sb.append(separator);
  601.                 sb.append(f);
  602.             }
  603.            
  604.             f = getProperty(CostantiConnettori.CONNETTORE_FILE_RESPONSE_INPUT_FILE, connettore.getPropertyList());
  605.             if(f!=null) {
  606.                 sb.append(newLine);
  607.                 sb.append(CostantiLabel.LABEL_INPUT_FILE);
  608.                 sb.append(separator);
  609.                 sb.append(f);
  610.             }
  611.            
  612.             f = getProperty(CostantiConnettori.CONNETTORE_FILE_RESPONSE_INPUT_FILE_HEADERS, connettore.getPropertyList());
  613.             if(f!=null) {
  614.                 sb.append(newLine);
  615.                 sb.append(CostantiLabel.LABEL_INPUT_FILE_HEADER);
  616.                 sb.append(separator);
  617.                 sb.append(f);
  618.             }
  619.         }
  620.        
  621.     }
  622.    
  623.     public static String getEndpointConnettore(org.openspcoop2.core.registry.Connettore connettore, boolean connettoreStatic) {
  624.         return getEndpointConnettore(connettore.mappingIntoConnettoreConfigurazione(), connettoreStatic);
  625.     }
  626.     public static String getEndpointConnettore(org.openspcoop2.core.config.Connettore connettore, boolean connettoreStatic) {

  627.         if(connettoreStatic) {
  628.             return null; // non visualizzo nulla
  629.         }
  630.        
  631.         TipiConnettore tipo = TipiConnettore.toEnumFromName(connettore.getTipo());
  632.         if(tipo==null) {
  633.             tipo = TipiConnettore.CUSTOM;
  634.         }
  635.        
  636.         String endpoint = null;
  637.         if(TipiConnettore.HTTP.getNome().equals(connettore.getTipo()) || TipiConnettore.HTTPS.getNome().equals(connettore.getTipo())){
  638.             endpoint = getProperty(CostantiConnettori.CONNETTORE_LOCATION, connettore.getPropertyList());
  639.         }
  640.         else if(TipiConnettore.JMS.getNome().equals(connettore.getTipo())){
  641.             endpoint = getProperty(CostantiConnettori.CONNETTORE_LOCATION, connettore.getPropertyList());
  642.         }
  643.         else if(TipiConnettore.FILE.getNome().equals(connettore.getTipo())){
  644.             endpoint = getProperty(CostantiConnettori.CONNETTORE_FILE_REQUEST_OUTPUT_FILE, connettore.getPropertyList());
  645.         }
  646.         else if(TipiConnettore.NULL.getNome().equals(connettore.getTipo())){
  647.             endpoint = org.openspcoop2.pdd.core.connettori.ConnettoreNULL.LOCATION;
  648.         }
  649.         else if(TipiConnettore.NULLECHO.getNome().equals(connettore.getTipo())){
  650.             endpoint = org.openspcoop2.pdd.core.connettori.ConnettoreNULLEcho.LOCATION;
  651.         }
  652.         else if(TipiConnettore.STATUS.getNome().equals(connettore.getTipo())){
  653.             endpoint = org.openspcoop2.pdd.core.connettori.ConnettoreStatus.LOCATION;
  654.         }
  655.         else {
  656.             String endpointV = getProperty(CostantiConnettori.CONNETTORE_LOCATION, connettore.getPropertyList());
  657.             if(StringUtils.isNotEmpty(endpointV)) {
  658.                 endpoint = endpointV;
  659.             }
  660.         }
  661.         return endpoint;
  662.     }
  663.    
  664.     public static String getNegoziazioneTokenPolicyConnettore(org.openspcoop2.core.registry.Connettore connettore) {
  665.         return getNegoziazioneTokenPolicyConnettore(connettore.mappingIntoConnettoreConfigurazione());
  666.     }
  667.     public static String getNegoziazioneTokenPolicyConnettore(org.openspcoop2.core.config.Connettore connettore) {
  668.         return getProperty(CostantiConnettori.CONNETTORE_TOKEN_POLICY, connettore.getPropertyList());
  669.     }
  670.    
  671.     public static String getNegoziazioneTokenEndpoint(GenericProperties gp, Logger log) {
  672.         PolicyNegoziazioneToken policy = null;
  673.         try {
  674.             policy = TokenUtilities.convertTo(gp);
  675.             return policy.getEndpoint();
  676.         }catch(Exception e) {
  677.             log.error("Errore durante il reperimento dell'endpoint della policy '"+gp.getNome()+"': "+e.getMessage(),e);
  678.             return null;
  679.         }
  680.     }
  681.    
  682.     private static String getProperty(String nome,List<org.openspcoop2.core.config.Property> list){
  683.         if(list!=null && list.size()>0){
  684.             for (org.openspcoop2.core.config.Property property : list) {
  685.                 if(property.getNome().equals(nome)){
  686.                     return property.getValore();
  687.                 }
  688.             }
  689.         }
  690.         return null;
  691.     }
  692.    
  693.    
  694.     public static String getNomeConnettori(Context context) {
  695.         String connettoriMultipli = null;
  696.         if(context.containsKey(org.openspcoop2.core.constants.Costanti.CONSEGNA_MULTIPLA_CONNETTORI_BY_ID)) {
  697.             Object oConnettori = context.getObject(org.openspcoop2.core.constants.Costanti.CONSEGNA_MULTIPLA_CONNETTORI_BY_ID );
  698.             if (oConnettori instanceof List){
  699.                 List<?> l = (List<?>) oConnettori;
  700.                 String s = getNomeConnettori(l);
  701.                 if(s!=null && s.length()>0) {
  702.                     return s;
  703.                 }
  704.             }
  705.         }
  706.         return connettoriMultipli;
  707.     }
  708.     private static String getNomeConnettori(List<?> l) {
  709.         if(!l.isEmpty()) {
  710.             StringBuilder sb = new StringBuilder();
  711.             for (Object object : l) {
  712.                 if(object instanceof String) {
  713.                     String s = (String) object;
  714.                     if(sb.length()>0) {
  715.                         sb.append(",");
  716.                     }
  717.                     sb.append(s);
  718.                 }
  719.             }
  720.             if(sb.length()>0) {
  721.                 return sb.toString();
  722.             }
  723.         }
  724.         return null;
  725.     }
  726. }