PolicyFiltroApplicativoUtilities.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.controllo_traffico.policy;

  21. import org.openspcoop2.core.constants.Costanti;
  22. import org.openspcoop2.core.controllo_traffico.beans.DatiTransazione;
  23. import org.openspcoop2.core.controllo_traffico.constants.TipoFiltroApplicativo;
  24. import org.openspcoop2.message.MessageUtils;
  25. import org.openspcoop2.message.OpenSPCoop2Message;
  26. import org.openspcoop2.message.constants.MessageType;
  27. import org.openspcoop2.message.soap.SoapUtils;
  28. import org.openspcoop2.pdd.config.OpenSPCoop2Properties;
  29. import org.openspcoop2.pdd.config.dynamic.PddPluginLoader;
  30. import org.openspcoop2.pdd.core.PdDContext;
  31. import org.openspcoop2.pdd.core.connettori.InfoConnettoreIngresso;
  32. import org.openspcoop2.pdd.core.controllo_traffico.plugins.Dati;
  33. import org.openspcoop2.pdd.core.controllo_traffico.plugins.IRateLimiting;
  34. import org.openspcoop2.pdd.core.handlers.InRequestProtocolContext;
  35. import org.openspcoop2.protocol.sdk.state.URLProtocolContext;
  36. import org.openspcoop2.utils.regexp.RegExpNotFoundException;
  37. import org.openspcoop2.utils.regexp.RegularExpressionEngine;
  38. import org.openspcoop2.utils.xml.AbstractXPathExpressionEngine;
  39. import org.openspcoop2.utils.xml2json.JsonXmlPathExpressionEngine;
  40. import org.slf4j.Logger;
  41. import org.w3c.dom.Element;

  42. /**    
  43.  * PolicyFiltroApplicativoUtilities
  44.  *
  45.  * @author Poli Andrea (poli@link.it)
  46.  * @author $Author$
  47.  * @version $Rev$, $Date$
  48.  */
  49. public class PolicyFiltroApplicativoUtilities {

  50.     public static String getValore(Logger log,String tipo, String nome, InRequestProtocolContext context,
  51.             DatiTransazione datiTransazione, boolean forFilter) throws Exception{
  52.        
  53.         if(context==null) {
  54.             throw new Exception("InRequestProtocolContext is null");
  55.         }
  56.        
  57.         OpenSPCoop2Message message = context.getMessaggio();
  58.        
  59.         URLProtocolContext urlProtocolContext = context.getConnettore().getUrlProtocolContext();
  60.    
  61.         String soapAction = context.getConnettore().getSoapAction();
  62.        
  63.         PdDContext pddContext = null;
  64.         if(context!=null && context.getPddContext()!=null) {
  65.             pddContext = context.getPddContext();
  66.         }
  67.        
  68.         InfoConnettoreIngresso connettore = context.getConnettore();
  69.        
  70.         return getValore(log, tipo, nome,
  71.                 datiTransazione, forFilter,
  72.                 message, urlProtocolContext, soapAction, pddContext,
  73.                 connettore);
  74.     }
  75.     public static String getValore(Logger log,String tipo, String nome,
  76.             DatiTransazione datiTransazione, boolean forFilter,
  77.             OpenSPCoop2Message message, URLProtocolContext urlProtocolContext, String soapActionParam, PdDContext pddContext,
  78.             InfoConnettoreIngresso connettore) throws Exception{
  79.        
  80.        
  81.         TipoFiltroApplicativo tipoFiltro = TipoFiltroApplicativo.toEnumConstant(tipo);
  82.        
  83.         switch (tipoFiltro) {
  84.         case CONTENT_BASED:
  85.            
  86.             String idTransazione = null;
  87.             if(pddContext!=null) {
  88.                 idTransazione = (String)pddContext.getObject(org.openspcoop2.core.constants.Costanti.ID_TRANSAZIONE);
  89.             }
  90.            
  91.             AbstractXPathExpressionEngine xPathEngine = null;
  92.             boolean bufferMessage_readOnly =  OpenSPCoop2Properties.getInstance().isReadByPathBufferEnabled();
  93.             boolean checkSoapBodyEmpty = false; // devo poter fare xpath anche su soapBody empty
  94.             Element element = null;
  95.             String elementJson = null;
  96.             if(message!=null) {
  97.                 element = MessageUtils.getContentElement(message, checkSoapBodyEmpty, bufferMessage_readOnly, idTransazione);
  98.                 elementJson = MessageUtils.getContentString(message, bufferMessage_readOnly, idTransazione);
  99.             }
  100.             if(element!=null) {
  101.                 xPathEngine = new org.openspcoop2.message.xml.XPathExpressionEngine(message.getFactory());
  102.                 return AbstractXPathExpressionEngine.extractAndConvertResultAsString(element, xPathEngine, nome,  log);
  103.             }
  104.             else if(elementJson!=null) {
  105.                 return JsonXmlPathExpressionEngine.extractAndConvertResultAsString(elementJson, nome, log);
  106.             }
  107.             else {
  108.                 return null; // semplicemente non deve matchare il filtro
  109.             }
  110.            
  111.         case URLBASED:
  112.            
  113.             String urlInvocazionePD = urlProtocolContext.getUrlInvocazione_formBased();
  114.             try{
  115.                 return RegularExpressionEngine.getStringMatchPattern(urlInvocazionePD, nome);
  116.             }catch(RegExpNotFoundException notFound){
  117.                 return null;
  118.             }

  119.         case FORM_BASED:
  120.            
  121.             return urlProtocolContext.getParameterFirstValue(nome);
  122.            
  123.         case HEADER_BASED:
  124.            
  125.             return urlProtocolContext.getHeaderFirstValue(nome);
  126.                        
  127.         case SOAPACTION_BASED:
  128.            
  129.             String soapAction = soapActionParam;
  130.             if(soapAction==null) {
  131.                 // provo una soluzione veloce di vedere se รจ presente nell'header di trasporto o nel content-type
  132.                 // non so che tipo di message type possiedo
  133.                 try{
  134.                     soapAction = SoapUtils.getSoapAction(urlProtocolContext, MessageType.SOAP_11, urlProtocolContext.getContentType());
  135.                 }catch(Exception e){
  136.                     // ignore
  137.                 }
  138.                 if(soapAction==null){
  139.                     try{
  140.                         soapAction = SoapUtils.getSoapAction(urlProtocolContext, MessageType.SOAP_12, urlProtocolContext.getContentType());
  141.                     }catch(Exception e){
  142.                         // ignore
  143.                     }  
  144.                 }
  145.             }
  146.             if(soapAction!=null) {
  147.                 soapAction = soapAction.trim();
  148.                 if(soapAction.startsWith("\"") && soapAction.length()>1){
  149.                     soapAction = soapAction.substring(1);
  150.                 }
  151.                 if(soapAction.endsWith("\"")  && soapAction.length()>1){
  152.                     soapAction = soapAction.substring(0, (soapAction.length()-1));
  153.                 }
  154.             }
  155.             return soapAction;
  156.            
  157.         case INDIRIZZO_IP:
  158.            
  159.             if(pddContext!=null && pddContext.containsKey(Costanti.CLIENT_IP_REMOTE_ADDRESS)) {
  160.                 return (String) pddContext.getObject(Costanti.CLIENT_IP_REMOTE_ADDRESS);
  161.             }
  162.             return null;
  163.            
  164.         case INDIRIZZO_IP_FORWARDED:
  165.            
  166.             if(pddContext!=null && pddContext.containsKey(Costanti.CLIENT_IP_TRANSPORT_ADDRESS)) {
  167.                 return (String) pddContext.getObject(Costanti.CLIENT_IP_TRANSPORT_ADDRESS);
  168.             }
  169.             return null;
  170.            
  171.         case PLUGIN_BASED:
  172.            
  173.             IRateLimiting rateLimitingPlugin = null;
  174.             try{
  175.                 rateLimitingPlugin = PddPluginLoader.getInstance().newRateLimiting(nome);
  176.             }catch(Exception e){
  177.                 throw e;
  178.             }
  179.            
  180.             String className = null;
  181.             try{
  182.                 className = rateLimitingPlugin.getClass().getName();
  183.                
  184.                 Dati datiRichiesta = new Dati();
  185.                 datiRichiesta.setConnettore(connettore);
  186.                 datiRichiesta.setDatiTransazione(datiTransazione);
  187.                 datiRichiesta.setMessaggio(message);
  188.                 datiRichiesta.setPddContext(pddContext);
  189.                
  190.                 if(forFilter){
  191.                     return rateLimitingPlugin.estraiValoreFiltro(log,datiRichiesta);
  192.                 }
  193.                 else{
  194.                     return rateLimitingPlugin.estraiValoreCollezionamentoDati(log,datiRichiesta);
  195.                 }

  196.             }catch(Exception e){
  197.                 throw new Exception("Instance plugin ["+nome+"] [class:"+className+"] error: "+e.getMessage(),e);
  198.             }

  199.         }
  200.        
  201.         throw new Exception("TipoFiltro ["+tipo+"] non gestito");
  202.     }
  203.    
  204. }