Utilities.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;

  21. import org.openspcoop2.core.id.IDAccordo;
  22. import org.openspcoop2.core.id.IDServizio;
  23. import org.openspcoop2.core.id.IDSoggetto;
  24. import org.openspcoop2.core.registry.AccordoServizioParteComune;
  25. import org.openspcoop2.core.registry.AccordoServizioParteSpecifica;
  26. import org.openspcoop2.core.registry.Resource;
  27. import org.openspcoop2.core.registry.driver.IDAccordoFactory;
  28. import org.openspcoop2.message.OpenSPCoop2Message;
  29. import org.openspcoop2.message.OpenSPCoop2MessageFactory;
  30. import org.openspcoop2.message.constants.MessageRole;
  31. import org.openspcoop2.pdd.config.ClassNameProperties;
  32. import org.openspcoop2.pdd.config.OpenSPCoop2Properties;
  33. import org.openspcoop2.pdd.services.connector.ConnectorException;
  34. import org.openspcoop2.protocol.registry.RegistroServiziManager;
  35. import org.openspcoop2.protocol.sdk.Busta;
  36. import org.openspcoop2.protocol.sdk.IProtocolFactory;
  37. import org.openspcoop2.protocol.sdk.ProtocolException;
  38. import org.openspcoop2.protocol.sdk.Trasmissione;
  39. import org.openspcoop2.protocol.sdk.state.IState;
  40. import org.openspcoop2.protocol.sdk.state.RequestInfo;
  41. import org.openspcoop2.utils.date.DateEngineType;
  42. import org.openspcoop2.utils.id.IDUtilities;
  43. import org.openspcoop2.utils.resources.Loader;
  44. import org.openspcoop2.utils.rest.api.ApiOperation;
  45. import org.openspcoop2.utils.transport.http.HttpRequestMethod;
  46. import org.slf4j.Logger;

  47. /**
  48.  * Utilities PdD
  49.  *
  50.  * @author Poli Andrea (apoli@link.it)
  51.  * @author $Author$
  52.  * @version $Rev$, $Date$
  53.  */
  54. public class Utilities {

  55.     public static void refreshIdentificativiPorta(Busta busta,IDSoggetto idSoggettoDefault,RegistroServiziManager registroServiziManager,IProtocolFactory<?> protocolFactory, RequestInfo requestInfo){
  56.         if(busta==null) {
  57.             return;
  58.         }
  59.         if(busta.getMittente()!=null && busta.getTipoMittente()!=null && busta.getIdentificativoPortaMittente()==null){
  60.             try{
  61.                 if(idSoggettoDefault.getTipo().equals(busta.getTipoMittente()) &&
  62.                         idSoggettoDefault.getNome().equals(busta.getMittente())){
  63.                     busta.setIdentificativoPortaMittente(idSoggettoDefault.getCodicePorta());
  64.                 }
  65.                 else{
  66.                     busta.setIdentificativoPortaMittente(registroServiziManager.getDominio(new IDSoggetto(busta.getTipoMittente(), busta.getMittente()), null, protocolFactory, requestInfo));
  67.                 }
  68.             }catch(Exception e){}
  69.         }
  70.         if(busta.getDestinatario()!=null && busta.getTipoDestinatario()!=null && busta.getIdentificativoPortaDestinatario()==null){
  71.             try{
  72.                 if(idSoggettoDefault.getTipo().equals(busta.getTipoDestinatario()) &&
  73.                         idSoggettoDefault.getNome().equals(busta.getDestinatario())){
  74.                     busta.setIdentificativoPortaDestinatario(idSoggettoDefault.getCodicePorta());
  75.                 }
  76.                 else{
  77.                     busta.setIdentificativoPortaDestinatario(registroServiziManager.getDominio(new IDSoggetto(busta.getTipoDestinatario(), busta.getDestinatario()), null, protocolFactory, requestInfo));
  78.                 }
  79.             }catch(Exception e){}
  80.         }
  81.         for (int i = 0; i < busta.sizeListaTrasmissioni(); i++) {
  82.             Trasmissione tr = busta.getTrasmissione(i);
  83.             if(tr.getOrigine()!=null && tr.getTipoOrigine()!=null && tr.getIdentificativoPortaOrigine()==null){
  84.                 try{
  85.                     if(idSoggettoDefault.getTipo().equals(tr.getTipoOrigine()) &&
  86.                             idSoggettoDefault.getNome().equals(tr.getOrigine())){
  87.                         tr.setIdentificativoPortaOrigine(idSoggettoDefault.getCodicePorta());
  88.                     }
  89.                     else{
  90.                         tr.setIdentificativoPortaOrigine(registroServiziManager.getDominio(new IDSoggetto(tr.getTipoOrigine(), tr.getOrigine()), null, protocolFactory, requestInfo));
  91.                     }
  92.                 }catch(Exception e){}
  93.             }
  94.             if(tr.getDestinazione()!=null && tr.getTipoDestinazione()!=null && tr.getIdentificativoPortaDestinazione()==null){
  95.                 try{
  96.                     if(idSoggettoDefault.getTipo().equals(tr.getTipoDestinazione()) &&
  97.                             idSoggettoDefault.getNome().equals(tr.getDestinazione())){
  98.                         tr.setIdentificativoPortaDestinazione(idSoggettoDefault.getCodicePorta());
  99.                     }
  100.                     else{
  101.                         tr.setIdentificativoPortaDestinazione(registroServiziManager.getDominio(new IDSoggetto(tr.getTipoDestinazione(), tr.getDestinazione()), null, protocolFactory, requestInfo));
  102.                     }
  103.                 }catch(Exception e){}
  104.             }
  105.         }
  106.     }

  107.    
  108.     private static OpenSPCoop2MessageFactory _factory = OpenSPCoop2MessageFactory.getDefaultMessageFactory();
  109.     public static OpenSPCoop2MessageFactory getOpenspcoop2MessageFactory(Logger log, RequestInfo requestInfo, MessageRole role) throws ConnectorException{
  110.         return getOpenspcoop2MessageFactory(log, null, requestInfo, role);
  111.     }
  112.     public static OpenSPCoop2MessageFactory getOpenspcoop2MessageFactory(Logger log, OpenSPCoop2Message requestMessage, RequestInfo requestInfo, MessageRole role) throws ConnectorException{
  113.         OpenSPCoop2MessageFactory useFactory = _factory;
  114.         if(requestMessage!=null && requestMessage.getFactory()!=null) {
  115.             useFactory = requestMessage.getFactory();
  116.         }
  117.         else {
  118.             if(requestInfo!=null && requestInfo.getMessageFactory()!=null) {
  119.                 String classNameMessageFactory = ClassNameProperties.getInstance().getOpenSPCoop2MessageFactory(requestInfo.getMessageFactory());
  120.                 if(classNameMessageFactory==null) {
  121.                     throw new ConnectorException("MessageFactory '"+requestInfo.getMessageFactory()+"' not found");
  122.                 }
  123.                 try {
  124.                     useFactory = (OpenSPCoop2MessageFactory) Loader.getInstance().newInstance(classNameMessageFactory);
  125.                 }catch(Exception e){
  126.                     throw new ConnectorException(e.getMessage(),e);
  127.                 }  
  128.             }
  129.         }
  130.         log.debug("MessageEngineFactory ["+role+"] ["+useFactory.getClass().getName()+"]");
  131.         return useFactory;
  132.     }
  133.    
  134.     public static String generateIDDateTime(String format, int syncMs, String clusterIdSeparator, boolean clusterIdAsPrefix) throws ProtocolException {
  135.         return _generateIDDateTime(IDUtilities.generateDateTime(format, syncMs), clusterIdSeparator, clusterIdAsPrefix);
  136.     }
  137.     public static String generateIDDateTime(DateEngineType type, String format, int syncMs, String clusterIdSeparator, boolean clusterIdAsPrefix) throws ProtocolException {
  138.         return _generateIDDateTime(IDUtilities.generateDateTime(type, format, syncMs), clusterIdSeparator, clusterIdAsPrefix);
  139.     }
  140.     public static String generateIDDateTime_ISO_8601_TZ(String format, int syncMs, String clusterIdSeparator, boolean clusterIdAsPrefix) throws ProtocolException {
  141.         return _generateIDDateTime(IDUtilities.generateDateTime_ISO_8601_TZ(format, syncMs), clusterIdSeparator, clusterIdAsPrefix);
  142.     }
  143.     public static String generateIDDateTime_ISO_8601_TZ(DateEngineType type, String format, int syncMs, String clusterIdSeparator, boolean clusterIdAsPrefix) throws ProtocolException {
  144.         return _generateIDDateTime(IDUtilities.generateDateTime_ISO_8601_TZ(type, format, syncMs), clusterIdSeparator, clusterIdAsPrefix);
  145.     }
  146.     private static String _generateIDDateTime(String timeId, String clusterIdSeparator, boolean clusterIdAsPrefix) throws ProtocolException {
  147.         Integer prefix = null;
  148.         String prefixSop2 = OpenSPCoop2Properties.getInstance().getClusterIdNumerico();
  149.         if(prefixSop2!=null) {
  150.             prefix = Integer.valueOf(prefixSop2);
  151.         }
  152.         String prefixS = "00";
  153.         if(prefix!=null) {
  154.             if(prefix<10) {
  155.                 prefixS = "0"+prefix.intValue();
  156.             }
  157.             else {
  158.                 prefixS = ""+prefix.intValue();
  159.             }
  160.         }
  161.         if(clusterIdAsPrefix) {
  162.             return prefixS + clusterIdSeparator + timeId;
  163.         }
  164.         else {
  165.             return timeId + clusterIdSeparator + prefixS;
  166.         }
  167.     }
  168.    
  169.    
  170.     /* *********** INDIVIDUAZIONE RISORSA REST ************ */
  171.    
  172.     private static final String SPECIAL = "*";
  173.    
  174.     public static String [] parseResourceRest(String element) {
  175.        
  176.         String check = element.toLowerCase();
  177.        
  178.         String checkMethod = SPECIAL+" ";
  179.         if(check.startsWith(checkMethod) && check.length()>checkMethod.length()) {
  180.             String [] tmp = new String[2];
  181.             tmp[0] = SPECIAL;
  182.             tmp[1] = check.substring(checkMethod.length());
  183.             return tmp;
  184.         }
  185.        
  186.         HttpRequestMethod [] methods = HttpRequestMethod.values();
  187.         if(methods!=null && methods.length>0) {
  188.             for (HttpRequestMethod httpRequestMethod : methods) {
  189.                 checkMethod = httpRequestMethod.name().toLowerCase()+" ";
  190.                 if(check.startsWith(checkMethod) && check.length()>checkMethod.length()) {
  191.                     String [] tmp = new String[2];
  192.                     tmp[0] = httpRequestMethod.name();
  193.                     tmp[1] = check.substring(checkMethod.length());
  194.                     return tmp;
  195.                 }
  196.             }
  197.         }
  198.        
  199.         return null;
  200.        
  201.     }
  202.    
  203.     public static boolean isRestResourceMatch(String [] parseResourceRest, Resource restResource) {
  204.         boolean isResourceRest = false;
  205.         String metodo = parseResourceRest[0];
  206.         String path = parseResourceRest[1];
  207.         if(SPECIAL.equals(metodo) ||
  208.                 (restResource.getMethod()!=null && metodo.toLowerCase().equals(restResource.getMethod().name().toLowerCase())) ||
  209.                 (restResource.getMethod()==null) // qualsiasi
  210.                 ) {
  211.             if(path.equals(SPECIAL)) {
  212.                 isResourceRest = true;
  213.             }
  214.             else if(restResource.getPath()!=null){
  215.                 String resourcePathNormalized = ApiOperation.normalizePath(restResource.getPath());
  216.                 resourcePathNormalized = resourcePathNormalized.trim().toLowerCase();
  217.                 path = ApiOperation.normalizePath(path);
  218.                 path = path.trim().toLowerCase();
  219.                 if(path.endsWith(SPECIAL)) {
  220.                     String prefix = path.substring(0, path.length()-1);
  221.                     if(resourcePathNormalized.startsWith(prefix)) {
  222.                         isResourceRest = true;
  223.                     }
  224.                 }
  225.                 else if(path.equals(resourcePathNormalized)) {
  226.                     isResourceRest = true;
  227.                 }
  228.             }                  
  229.         }
  230.         return isResourceRest;
  231.     }
  232.    
  233.     public static Resource getRestResource(Logger log, IState state, IDServizio idServizio, RequestInfo requestInfo) {
  234.         if(idServizio!=null && idServizio.getAzione()!=null && !"".equals(idServizio.getAzione())) {
  235.             RegistroServiziManager registroServiziManager = RegistroServiziManager.getInstance(state);
  236.             try {
  237.                 AccordoServizioParteSpecifica asps = registroServiziManager.getAccordoServizioParteSpecifica(idServizio, null, false, requestInfo);
  238.                 if(asps.getAccordoServizioParteComune()!=null && !"".equals(asps.getAccordoServizioParteComune())) {
  239.                     IDAccordo idAccordo = IDAccordoFactory.getInstance().getIDAccordoFromUri(asps.getAccordoServizioParteComune());
  240.                     AccordoServizioParteComune as = registroServiziManager.getAccordoServizioParteComune(idAccordo, null, false, false, requestInfo);
  241.                     for (Resource resourceCheck : as.getResourceList()) {
  242.                         if(resourceCheck.getNome().equals(idServizio.getAzione())){
  243.                             return resourceCheck;
  244.                         }
  245.                     }
  246.                 }
  247.             }catch(Throwable e) {
  248.                 log.error(e.getMessage(),e);
  249.             }
  250.         }
  251.         return null;
  252.     }
  253. }