RequestInfoConfigUtilities.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.sdk.state;

  21. import java.util.Map;

  22. import org.openspcoop2.core.config.PortaApplicativa;
  23. import org.openspcoop2.core.config.PortaDelegata;
  24. import org.openspcoop2.message.OpenSPCoop2Message;
  25. import org.openspcoop2.message.constants.ServiceBinding;
  26. import org.openspcoop2.message.soap.reader.OpenSPCoop2MessageSoapStreamReader;
  27. import org.openspcoop2.protocol.sdk.Context;
  28. import org.slf4j.Logger;

  29. /**
  30.  * RequestInfoConfigUtilities
  31.  *
  32.  * @author Poli Andrea (apoli@link.it)
  33.  * @author $Author$
  34.  * @version $Rev$, $Date$
  35.  */
  36. public class RequestInfoConfigUtilities {
  37.    
  38.     private RequestInfoConfigUtilities() {}

  39.     private static final String CACHE_NON_UTILIZZABILE_AZIONE = "Cache della richiesta non utilizzabile con la modalità di identificazione dell'azione '";
  40.    
  41.     public static void checkRequestInfoConfig(PortaApplicativa paDefault, Logger logCore,
  42.             ServiceBinding serviceBinding, OpenSPCoop2MessageSoapStreamReader soapStreamReader,
  43.             RequestInfo requestInfo) {
  44.        
  45.         if(requestInfo==null || requestInfo.getRequestConfig()==null || requestInfo.getRequestRateLimitingConfig()==null) {
  46.             if(requestInfo!=null) {
  47.                 requestInfo.setRequestConfig(null);
  48.                 requestInfo.setRequestRateLimitingConfig(null);
  49.             }
  50.             return;
  51.         }
  52.        
  53.         // Verifico che la modalità di riconoscimento dell'azione sia compatibile
  54.         boolean identificazioneByInterfaccia = false;
  55.         boolean identificazioneByInterfacciaForceMode = false;
  56.         if(paDefault!=null && paDefault.getAzione()!=null && paDefault.getAzione().getIdentificazione()!=null) {
  57.             if(org.openspcoop2.core.config.constants.PortaApplicativaAzioneIdentificazione.CONTENT_BASED.equals(paDefault.getAzione().getIdentificazione())
  58.                     ||
  59.                 org.openspcoop2.core.config.constants.PortaApplicativaAzioneIdentificazione.PROTOCOL_BASED.equals(paDefault.getAzione().getIdentificazione())  
  60.                     ||
  61.                 org.openspcoop2.core.config.constants.PortaApplicativaAzioneIdentificazione.INPUT_BASED.equals(paDefault.getAzione().getIdentificazione())  
  62.                     ||
  63.                 org.openspcoop2.core.config.constants.PortaApplicativaAzioneIdentificazione.DELEGATED_BY.equals(paDefault.getAzione().getIdentificazione())) {
  64.                 String msg = CACHE_NON_UTILIZZABILE_AZIONE+paDefault.getAzione().getIdentificazione()+"'";
  65.                 logCore.debug(msg);
  66.                 requestInfo.setRequestConfig(null);
  67.                 requestInfo.setRequestRateLimitingConfig(null);
  68.                 return;
  69.             }
  70.            
  71.             identificazioneByInterfaccia = org.openspcoop2.core.config.constants.PortaApplicativaAzioneIdentificazione.INTERFACE_BASED.equals(paDefault.getAzione().getIdentificazione());
  72.            
  73.             identificazioneByInterfacciaForceMode = paDefault.getAzione().getForceInterfaceBased() == null ||
  74.                     org.openspcoop2.core.config.constants.StatoFunzionalita.ABILITATO.equals(paDefault.getAzione().getForceInterfaceBased());
  75.         }
  76.        
  77.         if(identificazioneByInterfaccia && ServiceBinding.SOAP.equals(serviceBinding) &&
  78.             // Verifico che il soapStreamReader per il rootElement sia disponibile, utilizzato per riconoscere l'azione di default
  79.             (soapStreamReader==null || soapStreamReader.getRootElementLocalName()==null || soapStreamReader.getRootElementNamespace()==null)
  80.             ){
  81.             String msg = CACHE_NON_UTILIZZABILE_AZIONE+paDefault.getAzione().getIdentificazione()+"' e soapStreamReader root element non disponibile";
  82.             logCore.debug(msg);
  83.             requestInfo.setRequestConfig(null);
  84.             requestInfo.setRequestRateLimitingConfig(null);
  85.             return;
  86.         }
  87.         if(identificazioneByInterfacciaForceMode && ServiceBinding.SOAP.equals(serviceBinding) &&
  88.             // Verifico che il soapStreamReader per il rootElement sia disponibile, utilizzato per riconoscere l'azione di default
  89.             (soapStreamReader==null || soapStreamReader.getRootElementLocalName()==null || soapStreamReader.getRootElementNamespace()==null)
  90.             ){
  91.             logCore.debug("Cache della richiesta non utilizzabile con la modalità di identificazione dell'azione basata sull'interfaccia (force) e soapStreamReader root element non disponibile");
  92.             requestInfo.setRequestConfig(null);
  93.             requestInfo.setRequestRateLimitingConfig(null);
  94.         }
  95.        
  96.     }
  97.    
  98.     public static void checkRequestInfoConfig(PortaDelegata pdDefault, Logger logCore,
  99.             ServiceBinding serviceBinding, OpenSPCoop2MessageSoapStreamReader soapStreamReader,
  100.             RequestInfo requestInfo) {
  101.        
  102.         boolean identificazioneByInterfaccia = false;
  103.         boolean identificazioneByInterfacciaForceMode = false;
  104.         if(pdDefault!=null && pdDefault.getAzione()!=null && pdDefault.getAzione().getIdentificazione()!=null) {
  105.             if(org.openspcoop2.core.config.constants.PortaDelegataAzioneIdentificazione.CONTENT_BASED.equals(pdDefault.getAzione().getIdentificazione())
  106.                     ||
  107.                 org.openspcoop2.core.config.constants.PortaDelegataAzioneIdentificazione.INPUT_BASED.equals(pdDefault.getAzione().getIdentificazione())
  108.                     ||
  109.                 org.openspcoop2.core.config.constants.PortaDelegataAzioneIdentificazione.DELEGATED_BY.equals(pdDefault.getAzione().getIdentificazione())) {
  110.                 String msg = CACHE_NON_UTILIZZABILE_AZIONE+pdDefault.getAzione().getIdentificazione()+"'";
  111.                 logCore.debug(msg);
  112.                 requestInfo.setRequestConfig(null);
  113.                 requestInfo.setRequestRateLimitingConfig(null);
  114.                 return;
  115.             }
  116.            
  117.             identificazioneByInterfaccia = org.openspcoop2.core.config.constants.PortaDelegataAzioneIdentificazione.INTERFACE_BASED.equals(pdDefault.getAzione().getIdentificazione());
  118.            
  119.             identificazioneByInterfacciaForceMode = pdDefault.getAzione().getForceInterfaceBased() == null ||
  120.                     org.openspcoop2.core.config.constants.StatoFunzionalita.ABILITATO.equals(pdDefault.getAzione().getForceInterfaceBased());
  121.         }
  122.        
  123.         if(identificazioneByInterfaccia && ServiceBinding.SOAP.equals(serviceBinding) &&
  124.             // Verifico che il soapStreamReader per il rootElement sia disponibile, utilizzato per riconoscere l'azione di default
  125.             (soapStreamReader==null || soapStreamReader.getRootElementLocalName()==null || soapStreamReader.getRootElementNamespace()==null)
  126.                 ){
  127.             String msg = CACHE_NON_UTILIZZABILE_AZIONE+pdDefault.getAzione().getIdentificazione()+"' e soapStreamReader root element non disponibile";
  128.             logCore.debug(msg);
  129.             requestInfo.setRequestConfig(null);
  130.             requestInfo.setRequestRateLimitingConfig(null);
  131.             return;
  132.         }
  133.         if(identificazioneByInterfacciaForceMode && ServiceBinding.SOAP.equals(serviceBinding) &&
  134.             // Verifico che il soapStreamReader per il rootElement sia disponibile, utilizzato per riconoscere l'azione di default
  135.             (soapStreamReader==null || soapStreamReader.getRootElementLocalName()==null || soapStreamReader.getRootElementNamespace()==null)
  136.             ){
  137.             logCore.debug("Cache della richiesta non utilizzabile con la modalità di identificazione dell'azione basata sull'interfaccia (force) e soapStreamReader root element non disponibile");
  138.             requestInfo.setRequestConfig(null);
  139.             requestInfo.setRequestRateLimitingConfig(null);
  140.         }
  141.        
  142.     }
  143.    
  144.    
  145.     public static RequestInfo normalizeRequestInfoBeforeSerialization(OpenSPCoop2Message msg) {
  146.         Object oRequestInfo = msg.getContextProperty(org.openspcoop2.core.constants.Costanti.REQUEST_INFO);
  147.         if(oRequestInfo instanceof RequestInfo) {
  148.             RequestInfo requestInfo = (RequestInfo) oRequestInfo;
  149.             return normalizeRequestInfoBeforeSerializationEngine(requestInfo);
  150.         }
  151.         return null;
  152.     }
  153.     public static RequestInfo normalizeRequestInfoBeforeSerialization(Context context) {
  154.         Object oRequestInfo = context.get(org.openspcoop2.core.constants.Costanti.REQUEST_INFO);
  155.         if(oRequestInfo instanceof RequestInfo) {
  156.             RequestInfo requestInfo = (RequestInfo) oRequestInfo;
  157.             return normalizeRequestInfoBeforeSerializationEngine(requestInfo);
  158.         }
  159.         return null;
  160.     }
  161.     private static RequestInfo normalizeRequestInfoBeforeSerializationEngine(RequestInfo requestInfo) {
  162.        
  163.         RequestConfig preRequestConfig = requestInfo.getPreRequestConfig();
  164.         requestInfo.setPreRequestConfig(null);
  165.         RequestConfig requestConfig = requestInfo.getRequestConfig();
  166.         requestInfo.setRequestConfig(null);
  167.         RequestRateLimitingConfig requestRateLimitingConfig = requestInfo.getRequestRateLimitingConfig();
  168.         requestInfo.setRequestRateLimitingConfig(null);
  169.         RequestThreadContext requestThreadContext = requestInfo.getRequestThreadContext();
  170.         requestInfo.setRequestThreadContext(null);
  171.         Map<String, Object> dynamicMap = requestInfo.getDynamicMap();
  172.         requestInfo.setDynamicMap(null);
  173.        
  174.         RequestInfo requestInfoBackup = new RequestInfo();
  175.         requestInfoBackup.setPreRequestConfig(preRequestConfig);
  176.         requestInfoBackup.setRequestConfig(requestConfig);
  177.         requestInfoBackup.setRequestRateLimitingConfig(requestRateLimitingConfig);
  178.         requestInfoBackup.setRequestThreadContext(requestThreadContext);
  179.         requestInfoBackup.setDynamicMap(dynamicMap);
  180.         return requestInfoBackup;

  181.     }
  182.    
  183.     public static void restoreRequestInfoAfterSerialization(OpenSPCoop2Message msg, RequestInfo requestInfoBackup) {
  184.         if(requestInfoBackup==null) {
  185.             return;
  186.         }
  187.        
  188.         Object oRequestInfo = msg.getContextProperty(org.openspcoop2.core.constants.Costanti.REQUEST_INFO);
  189.         if(oRequestInfo instanceof RequestInfo) {
  190.             RequestInfo requestInfo = (RequestInfo) oRequestInfo;
  191.             restoreRequestInfoAfterSerializationEngine(requestInfo, requestInfoBackup);
  192.         }
  193.     }
  194.     public static void restoreRequestInfoAfterSerialization(Context context, RequestInfo requestInfoBackup) {
  195.         if(requestInfoBackup==null) {
  196.             return;
  197.         }
  198.        
  199.         Object oRequestInfo = context.get(org.openspcoop2.core.constants.Costanti.REQUEST_INFO);
  200.         if(oRequestInfo instanceof RequestInfo) {
  201.             RequestInfo requestInfo = (RequestInfo) oRequestInfo;
  202.             restoreRequestInfoAfterSerializationEngine(requestInfo, requestInfoBackup);
  203.         }
  204.     }
  205.     private static void restoreRequestInfoAfterSerializationEngine(RequestInfo requestInfo, RequestInfo requestInfoBackup) {
  206.         requestInfo.setPreRequestConfig(requestInfoBackup.getPreRequestConfig());
  207.         requestInfo.setRequestConfig(requestInfoBackup.getRequestConfig());
  208.         requestInfo.setRequestRateLimitingConfig(requestInfoBackup.getRequestRateLimitingConfig());
  209.         requestInfo.setRequestThreadContext(requestInfoBackup.getRequestThreadContext());
  210.         requestInfo.setDynamicMap(requestInfoBackup.getDynamicMap());
  211.     }
  212. }