PDNDConfigUtilities.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.config;

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

  23. import org.apache.commons.lang.StringUtils;
  24. import org.openspcoop2.core.commons.CoreException;
  25. import org.openspcoop2.core.config.PortaApplicativa;
  26. import org.openspcoop2.core.constants.Costanti;
  27. import org.openspcoop2.core.id.IDPortaApplicativa;
  28. import org.openspcoop2.protocol.sdk.ProtocolException;
  29. import org.openspcoop2.protocol.sdk.state.RequestInfo;
  30. import org.openspcoop2.protocol.utils.ModIUtils;
  31. import org.openspcoop2.utils.certificate.remote.RemoteStoreConfig;
  32. import org.openspcoop2.utils.json.JsonPathExpressionEngine;
  33. import org.openspcoop2.utils.resources.Charset;
  34. import org.openspcoop2.utils.transport.TransportUtils;
  35. import org.openspcoop2.utils.transport.http.ExternalResourceUtils;
  36. import org.slf4j.Logger;

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

  47.     public static List<PDNDConfig> getRemoteStoreConfig(OpenSPCoop2Properties propertiesReader) throws ProtocolException, CoreException {
  48.         List<RemoteStoreConfig> listRSC = ModIUtils.getRemoteStoreConfig();
  49.         List<PDNDConfig> l = null;
  50.         if(listRSC!=null && !listRSC.isEmpty()) {
  51.             List<String> pdndNames = propertiesReader.getGestoreChiaviPDNDRemoteStoreName();
  52.             boolean all = propertiesReader.isGestoreChiaviPDNDEventiCheckAllStores();
  53.             for (RemoteStoreConfig r : listRSC) {
  54.                 if(all || pdndNames.contains(r.getStoreName())) {
  55.                     String pdndName = r.getStoreName();
  56.                     PDNDConfig c = new PDNDConfig();
  57.                     c.setRemoteKeyType(ModIUtils.getRemoteKeyType(pdndName));
  58.                     c.setRemoteStoreConfig(r);
  59.                     if(l==null) {
  60.                         l = new ArrayList<>();
  61.                     }
  62.                     l.add(c);
  63.                 }
  64.             }
  65.         }
  66.         if(l!=null && !l.isEmpty()) {
  67.             return l;
  68.         }
  69.         else {
  70.              l = null;
  71.         }
  72.         return l;
  73.     }
  74.    
  75.     private static final String URL_CHAR_DELIMITER = "/";
  76.     private static String buildBaseUrlPDND(RemoteStoreConfig remoteStore, OpenSPCoop2Properties propertiesReader) throws CoreException {
  77.        
  78.         String baseUrl = remoteStore.getBaseUrl();
  79.        
  80.         // elimino path keys dalla url
  81.         String pathKeys = propertiesReader.getGestoreChiaviPDNDkeysPath();  
  82.         if(!pathKeys.startsWith(URL_CHAR_DELIMITER)) {
  83.             pathKeys = URL_CHAR_DELIMITER + pathKeys;
  84.         }
  85.         if(baseUrl.endsWith(pathKeys)) {
  86.             baseUrl = baseUrl.substring(0,baseUrl.length()-pathKeys.length());
  87.         }
  88.         else {
  89.             if(pathKeys.endsWith(URL_CHAR_DELIMITER)) {
  90.                 // provo senza
  91.                 pathKeys = pathKeys.substring(0, (pathKeys.length()-1));
  92.             }
  93.             else {
  94.                 // provo con
  95.                 pathKeys = pathKeys + URL_CHAR_DELIMITER;
  96.             }
  97.             if(baseUrl.endsWith(pathKeys)) {
  98.                 baseUrl = baseUrl.substring(0,baseUrl.length()-pathKeys.length());
  99.             }
  100.         }
  101.            
  102.         return baseUrl;
  103.     }
  104.    
  105.     public static String buildUrlCheckEventi(RemoteStoreConfig remoteStore, OpenSPCoop2Properties propertiesReader) throws CoreException {
  106.        
  107.         String urlCheckEventi = buildBaseUrlPDND(remoteStore, propertiesReader);
  108.        
  109.         // aggiungo event keys
  110.        
  111.         String pathEventKeys = propertiesReader.getGestoreChiaviPDNDeventsKeysPath();
  112.         if(!pathEventKeys.startsWith(URL_CHAR_DELIMITER)) {
  113.             pathEventKeys = URL_CHAR_DELIMITER + pathEventKeys;
  114.         }
  115.         return urlCheckEventi + pathEventKeys;
  116.        
  117.     }
  118.    
  119.     private static String buildUrlClientId(RemoteStoreConfig remoteStore, OpenSPCoop2Properties propertiesReader, String clientId) throws CoreException {
  120.         String path = propertiesReader.getGestoreChiaviPDNDclientsPath();
  121.         return buildUrlByResourceId(remoteStore, propertiesReader, clientId, path);
  122.     }
  123.     private static String buildUrlOrganizationId(RemoteStoreConfig remoteStore, OpenSPCoop2Properties propertiesReader, String organizationId) throws CoreException {
  124.         String path = propertiesReader.getGestoreChiaviPDNDorganizationsPath();
  125.         return buildUrlByResourceId(remoteStore, propertiesReader, organizationId, path);
  126.     }
  127.     private static String buildUrlByResourceId(RemoteStoreConfig remoteStore, OpenSPCoop2Properties propertiesReader, String valueId, String path) throws CoreException {
  128.        
  129.         String baseUrl = buildBaseUrlPDND(remoteStore, propertiesReader);
  130.        
  131.         // aggiungo event keys
  132.        
  133.         if(!path.startsWith(URL_CHAR_DELIMITER)) {
  134.             path = URL_CHAR_DELIMITER + path;
  135.         }
  136.         int indexOf = path.indexOf("{");
  137.         if(indexOf>0) {
  138.             path = path.substring(0, indexOf);
  139.             try {
  140.                 path = path + TransportUtils.urlEncodePath(valueId, Charset.UTF_8.getValue());
  141.             }catch(Exception e) {
  142.                 throw new CoreException(e.getMessage(),e);
  143.             }
  144.         }
  145.         return baseUrl + path;
  146.        
  147.     }
  148.    
  149.    
  150.     public static String readClientDetails(RemoteStoreConfig remoteStore, OpenSPCoop2Properties propertiesReader, org.openspcoop2.utils.Map<Object> context, String clientId, Logger log) throws CoreException {
  151.        
  152.         String responseJson = null;
  153.         try {
  154.        
  155.             String url = buildUrlClientId(remoteStore, propertiesReader, clientId);
  156.            
  157.             byte[] response = ExternalResourceUtils.readResource(url, remoteStore);
  158.             responseJson = new String(response);
  159.            
  160.         }catch(Exception e) {
  161.            
  162.             if(abortTransaction(false, propertiesReader, context, log)) {
  163.                 throw new CoreException(e.getMessage(),e);
  164.             }
  165.             else {
  166.                 String msgError = "Raccolta informazioni tramite PDND sul client con id '"+clientId+"' fallita: "+e.getMessage();
  167.                 log.error(msgError);
  168.             }
  169.            
  170.         }
  171.        
  172.         return responseJson;
  173.     }
  174.    
  175.     public static String readOrganizationId(OpenSPCoop2Properties propertiesReader, org.openspcoop2.utils.Map<Object> context, String clientDetails, Logger log) throws CoreException {
  176.         String jsonPath = propertiesReader.getGestoreChiaviPDNDclientsOrganizationJsonPath();
  177.         boolean readErrorAbortTransaction = abortTransaction(true, propertiesReader, context, log);
  178.         return readOrganizationId(jsonPath, readErrorAbortTransaction, clientDetails, log);
  179.     }
  180.     public static String readOrganizationId(String jsonPath, boolean readErrorAbortTransaction, String clientDetails, Logger log) throws CoreException {
  181.         if(clientDetails!=null) {
  182.             try {
  183.                 return JsonPathExpressionEngine.extractAndConvertResultAsString(clientDetails, jsonPath, log);
  184.             }catch(Exception e) {
  185.                 if(readErrorAbortTransaction) {
  186.                     throw new CoreException(e.getMessage(),e);
  187.                 }
  188.                 else {
  189.                     String msgError = "Estrazione identificativo organizzazione tramite jsonPath '"+jsonPath+"' fallita (clientDetails: "+clientDetails+"): "+e.getMessage();
  190.                     log.error(msgError);
  191.                 }
  192.             }
  193.         }
  194.         return null;
  195.     }
  196.    
  197.     public static String readOrganizationDetails(RemoteStoreConfig remoteStore, OpenSPCoop2Properties propertiesReader, org.openspcoop2.utils.Map<Object> context, String organizationId, Logger log) throws CoreException {
  198.        
  199.         String responseJson = null;
  200.         try {
  201.        
  202.             String url = buildUrlOrganizationId(remoteStore, propertiesReader, organizationId);
  203.            
  204.             byte[] response = ExternalResourceUtils.readResource(url, remoteStore);
  205.             responseJson = new String(response);
  206.            
  207.         }catch(Exception e) {
  208.             if(abortTransaction(true, propertiesReader, context, log)) {
  209.                 throw new CoreException(e.getMessage(),e);
  210.             }
  211.             else {
  212.                 String msgError = "Raccolta informazioni tramite PDND sull'organizzazione con id '"+organizationId+"' fallita: "+e.getMessage();
  213.                 log.error(msgError);
  214.             }
  215.         }
  216.        
  217.         return responseJson;
  218.     }
  219.    
  220.     private static boolean abortTransaction(boolean organization, OpenSPCoop2Properties propertiesReader, org.openspcoop2.utils.Map<Object> context, Logger log) throws CoreException {
  221.        
  222.         boolean abort = organization ? propertiesReader.isGestoreChiaviPDNDorganizationsErrorAbortTransaction() : propertiesReader.isGestoreChiaviPDNDclientsErrorAbortTransaction();
  223.        
  224.         RequestInfo requestInfo = null;
  225.         if(context!=null && context.containsKey(Costanti.REQUEST_INFO)) {
  226.             Object o = context.get(Costanti.REQUEST_INFO);
  227.             if(o instanceof RequestInfo) {
  228.                 requestInfo = (RequestInfo) o;
  229.             }
  230.         }
  231.        
  232.         if(requestInfo!=null && requestInfo.getProtocolContext()!=null && requestInfo.getProtocolContext().getInterfaceName()!=null &&
  233.                 StringUtils.isNotEmpty(requestInfo.getProtocolContext().getInterfaceName())) {
  234.             IDPortaApplicativa idPA = new IDPortaApplicativa();
  235.             idPA.setNome(requestInfo.getProtocolContext().getInterfaceName());
  236.             try {
  237.                 PortaApplicativa pa = ConfigurazionePdDManager.getInstance().getPortaApplicativaSafeMethod(idPA, requestInfo);
  238.                 if(pa!=null && pa.sizeProprieta()>0) {
  239.                     abort = organization ?
  240.                             CostantiProprieta.isPdndReadByApiInteropOrganizationFailedAbortTransaction(pa.getProprieta(), abort)
  241.                             :
  242.                             CostantiProprieta.isPdndReadByApiInteropClientFailedAbortTransaction(pa.getProprieta(), abort);
  243.                 }
  244.             }catch(Exception e) {
  245.                 log.error("Accesso porta applicativa ["+requestInfo.getProtocolContext().getInterfaceName()+"] fallito: "+e.getMessage(),e);
  246.             }
  247.         }
  248.         return abort;
  249.        
  250.     }
  251. }