URLProtocolContextImpl.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.engine;

  21. import javax.servlet.http.HttpServletRequest;

  22. import org.openspcoop2.protocol.manifest.constants.Costanti;
  23. import org.openspcoop2.protocol.sdk.IProtocolFactory;
  24. import org.openspcoop2.protocol.sdk.ProtocolException;
  25. import org.openspcoop2.protocol.sdk.constants.IDService;
  26. import org.openspcoop2.protocol.sdk.state.FunctionContextsCustom;
  27. import org.openspcoop2.utils.UtilsException;
  28. import org.slf4j.Logger;

  29. /**
  30.  * URL Protocol Context
  31.  *
  32.  * @author Poli Andrea (apoli@link.it)
  33.  * @author $Author$
  34.  * @version $Rev$, $Date$
  35.  */


  36. public class URLProtocolContextImpl extends org.openspcoop2.protocol.sdk.state.URLProtocolContext implements java.io.Serializable {


  37.     /**
  38.      *
  39.      */
  40.     private static final long serialVersionUID = 1L;
  41.    
  42.     public URLProtocolContextImpl(Logger logCore) throws UtilsException{
  43.         super(logCore);
  44.     }
  45.     public URLProtocolContextImpl(HttpServletRequest req,Logger logCore, boolean debug, FunctionContextsCustom customContexts) throws ProtocolException, UtilsException{
  46.         super(req, logCore, debug, customContexts);
  47.     }
  48.     public URLProtocolContextImpl(HttpServletRequest req,Logger logCore, boolean debug, boolean integrationManagerEngine, FunctionContextsCustom customContexts) throws ProtocolException, UtilsException{
  49.         super(req, logCore, debug, integrationManagerEngine, customContexts);
  50.     }
  51.    
  52.     @Override
  53.     protected void init(HttpServletRequest req,Logger logCore, boolean debug, boolean integrationManagerEngine, FunctionContextsCustom customContexts) throws ProtocolException, UtilsException{
  54.                
  55.         String servletContext = req.getContextPath();
  56.         String urlInvocazione = req.getRequestURI();
  57.         String servizioInvocato = null;
  58.         if(logCore!=null)
  59.             logCore.debug("SERVLET CONTEXT ["+servletContext+"] URL["+urlInvocazione+"]");
  60.             // es SERVLET CONTEXT [/govway] URL[/govway/altriParametri]
  61.         try {
  62.            
  63.             ProtocolFactoryManager protocolFactoryManager = ProtocolFactoryManager.getInstance();
  64.             IProtocolFactory<?> pfEmptyContext = null;
  65.             IDService idServiceDefaultEmptyContext = null;
  66.             try {
  67.                 pfEmptyContext = protocolFactoryManager.getProtocolFactoryWithEmptyContext();
  68.                 idServiceDefaultEmptyContext = protocolFactoryManager.getDefaultServiceForEmptyContext();
  69.             }catch(Throwable e) {
  70.                 // ignore
  71.             }
  72.            
  73.             // Altro...
  74.             if(urlInvocazione.startsWith(servletContext+"/")){
  75.                 servizioInvocato = urlInvocazione.substring((servletContext+"/").length());
  76.             }
  77.             else if(urlInvocazione.equals(servletContext)){
  78.                 servizioInvocato = null;
  79.             }
  80.             else {
  81.                 throw new Exception("GovWay [protocol/]service to be used not supplied (context error)");
  82.             }
  83.            
  84.            
  85.             if(logCore!=null)
  86.                 logCore.debug("SERVIZIO RICHIESTO: ["+servizioInvocato+"]");
  87.            
  88.             // verifico che dopo "openspcoop2" sia stato fornito qualcosa
  89.             if(servizioInvocato==null || "".equals(servizioInvocato.trim())){
  90.                 if(pfEmptyContext!=null && idServiceDefaultEmptyContext!=null) {
  91.                     this.protocolName = pfEmptyContext.getProtocol();
  92.                     this.idServiceCustom = idServiceDefaultEmptyContext;
  93.                     switch (idServiceDefaultEmptyContext) {
  94.                     case PORTA_DELEGATA:
  95.                         this.function = org.openspcoop2.protocol.sdk.state.URLProtocolContext.PD_FUNCTION_GOVWAY;
  96.                         break;
  97.                     case PORTA_APPLICATIVA:
  98.                         this.function = org.openspcoop2.protocol.sdk.state.URLProtocolContext.PA_FUNCTION_GOVWAY;
  99.                         break;
  100.                     case PORTA_DELEGATA_XML_TO_SOAP:
  101.                         this.function = org.openspcoop2.protocol.sdk.state.URLProtocolContext.PDtoSOAP_FUNCTION_GOVWAY;
  102.                         break;
  103.                     default:
  104.                         throw new Exception("GovWay [protocol/]service to be used not supplied");
  105.                     }
  106.                     this.functionParameters = servizioInvocato;
  107.                     this.protocolWebContext = Costanti.CONTEXT_EMPTY;
  108.                 }
  109.                 else {
  110.                     throw new Exception("GovWay [protocol/]service to be used not supplied");
  111.                 }
  112.             }
  113.             else {
  114.            
  115.                 // Esamino il servizio fornito. Puo' darsi che prima del servizio ci sia il protocollo.
  116.                 String protocollo = null;
  117.                 String function = null;
  118.                 String functionParameters = null;
  119.                 if(servizioInvocato.indexOf("/") >= 0){
  120.                     protocollo = servizioInvocato.substring(0,servizioInvocato.indexOf("/"));
  121.                     function = servizioInvocato.substring(servizioInvocato.indexOf("/")+1,servizioInvocato.length());
  122.                 }else{
  123.                     protocollo = servizioInvocato;
  124.                     function = servizioInvocato;
  125.                 }
  126.                 if(logCore!=null)
  127.                     logCore.debug("PROTOCOLLO["+protocollo+"] FUNCTION["+function+"]");
  128.                
  129.                 // Vedo se ho un protocollo prima della funzione o direttamente il protocollo
  130.                 boolean IMengine = false;
  131.                 if(integrationManagerEngine && protocollo.equals(org.openspcoop2.protocol.sdk.state.URLProtocolContext.IntegrationManager_ENGINE)) {
  132.                     if(logCore!=null)
  133.                         logCore.debug("SERVLET INTEGRATION MANAGER SERVICE");
  134.                     function = protocollo;
  135.                    
  136.                     IMengine = true;
  137.                    
  138.                     Object o = getHttpServletRequest().getAttribute(org.openspcoop2.core.constants.Costanti.PROTOCOL_NAME.getValue());
  139.                     if(o == null || !(o instanceof String)){
  140.                         throw new Exception("Indicazione del protocollo non presente");
  141.                     }
  142.                     this.protocolName = (String) o;
  143.                     IProtocolFactory<?> pf = protocolFactoryManager.getProtocolFactoryByName(this.protocolName);
  144.                     if(pf==null){
  145.                         throw new Exception("Non risulta registrato un protocollo con nome ["+this.protocolName+"]");
  146.                     }
  147.                    
  148.                     o = getHttpServletRequest().getAttribute(org.openspcoop2.core.constants.Costanti.PROTOCOL_WEB_CONTEXT.getValue());
  149.                     if(o == null || !(o instanceof String)){
  150.                         throw new Exception("Indicazione del web context del protocollo non presente");
  151.                     }
  152.                     this.protocolWebContext = (String) o;
  153.                     pf = protocolFactoryManager.getProtocolFactoryByServletContext(this.protocolWebContext);
  154.                     if(pf==null){
  155.                         if(!Costanti.CONTEXT_EMPTY.equals(this.protocolWebContext))
  156.                             throw new Exception("Non risulta registrato un protocollo con contesto ["+this.protocolWebContext+"]");
  157.                         else
  158.                             throw new Exception("Non risulta registrato un protocollo con contesto speciale 'vuoto'");
  159.                     }
  160.                    
  161.                     int sizePrefix = (req.getContextPath() + "/" + function + "/").length();
  162.                     if(req.getRequestURI().length()>sizePrefix){
  163.                         functionParameters = req.getRequestURI().substring(sizePrefix);
  164.                     }
  165.                    
  166.                     if(functionParameters!=null && functionParameters.startsWith(IntegrationManager_SERVICE_PD)) {
  167.                         function+="_"+IntegrationManager_SERVICE_PD;
  168.                        
  169.                         Object oPD = getHttpServletRequest().getAttribute(org.openspcoop2.core.constants.Costanti.PORTA_DELEGATA.getValue());
  170.                         if(oPD == null || !(oPD instanceof String)){
  171.                             throw new Exception("Indicazione della porta delegata non presente");
  172.                         }
  173.                        
  174.                         functionParameters=(String)oPD;
  175.                        
  176.                     }
  177.                     else if(functionParameters!=null && functionParameters.startsWith(IntegrationManager_SERVICE_MessageBox)) {
  178.                         function+="_"+IntegrationManager_SERVICE_MessageBox;
  179.                        
  180.                         if(functionParameters.length()>IntegrationManager_SERVICE_MessageBox.length()) {
  181.                             functionParameters = functionParameters.substring(IntegrationManager_SERVICE_MessageBox.length());
  182.                         }
  183.     //                  else {
  184.     //                      functionParameters = null;
  185.     //                  }
  186.                     }
  187.                 }
  188.                 else if(protocollo.equals(org.openspcoop2.protocol.sdk.state.URLProtocolContext.PA_FUNCTION) ||
  189.                         protocollo.equals(org.openspcoop2.protocol.sdk.state.URLProtocolContext.PD_FUNCTION) ||
  190.                         protocollo.equals(org.openspcoop2.protocol.sdk.state.URLProtocolContext.PDtoSOAP_FUNCTION) ||
  191.                         protocollo.equals(org.openspcoop2.protocol.sdk.state.URLProtocolContext.IntegrationManager_FUNCTION) ||
  192.                         protocollo.equals(org.openspcoop2.protocol.sdk.state.URLProtocolContext.Check_FUNCTION) ||
  193.                         protocollo.equals(org.openspcoop2.protocol.sdk.state.URLProtocolContext.Proxy_FUNCTION) ||
  194.                         (customContexts!=null && customContexts.isMatch(protocollo, function))) {
  195.                     // ContextProtocol Empty
  196.                     if(logCore!=null)
  197.                         logCore.debug("SERVLET PATH EMPTY");
  198.                     if((customContexts!=null && customContexts.isMatch(protocollo, function))) {
  199.                         this.idServiceCustom = customContexts.getServiceMatch(protocollo, function);
  200.                         function = customContexts.getFunctionMatch(protocollo, function);
  201.                         if(logCore!=null)
  202.                             logCore.debug("CUSTOM FUNCTION ["+function+"] ["+this.idServiceCustom+"]");
  203.                     }
  204.                     else {
  205.                         function = protocollo;
  206.                     }
  207.                     protocollo = Costanti.CONTEXT_EMPTY;
  208.                    
  209.                     int sizePrefix = (req.getContextPath() + "/" + function + "/").length();
  210.                     if(req.getRequestURI().length()>sizePrefix){
  211.                         functionParameters = req.getRequestURI().substring(sizePrefix);
  212.                     }
  213.                     else {
  214.                         // Serve nei casi custom
  215.                         functionParameters = null;
  216.                     }
  217.                 }
  218.                 else{
  219.                     // Verifico se esiste o meno il protocollo tra quelli registrati
  220.                     boolean casoSpeciale_noProtocollo_noFunction = false;
  221.                     try {
  222.                         IProtocolFactory<?> pfCheck = protocolFactoryManager.getProtocolFactoryByServletContext(protocollo);
  223.                         if(pfCheck==null) {
  224.                             throw new Exception("NotFound");
  225.                         }
  226.                     }catch(Exception e) {
  227.                         if(pfEmptyContext!=null) {
  228.                             if(function!=null && !"".equals(function)) {
  229.                                 function = protocollo + "/" + function;
  230.                             }
  231.                             else {
  232.                                 function = protocollo;
  233.                             }
  234.                             protocollo = Costanti.CONTEXT_EMPTY;
  235.                             casoSpeciale_noProtocollo_noFunction = true;
  236.                         }
  237.                     }
  238.                        
  239.                    
  240.                     // Calcolo function
  241.                     String functionParameterForCheckCustom = null;
  242.                     if(function.indexOf("/") > 0){
  243.                         functionParameterForCheckCustom = function.substring(function.indexOf("/"));
  244.                         if(functionParameterForCheckCustom.length()>1 && functionParameterForCheckCustom.startsWith("/")) {
  245.                             functionParameterForCheckCustom = functionParameterForCheckCustom.substring(1);
  246.                         }
  247.                     }
  248.                     if(function.indexOf("/") > 0){
  249.                         function = function.substring(0,function.indexOf("/"));
  250.                     }
  251.                     else if(function.indexOf("?") > 0){
  252.                         function = function.substring(0,function.indexOf("?"));
  253.                     }
  254.                     if(logCore!=null)
  255.                         logCore.debug("FUNCTION ["+function+"]");
  256.                    
  257.                     boolean emptyFunction = false;
  258.                     if(casoSpeciale_noProtocollo_noFunction ||
  259.                             (
  260.                                     ! (function.equals(org.openspcoop2.protocol.sdk.state.URLProtocolContext.PA_FUNCTION) ||
  261.                                             function.equals(org.openspcoop2.protocol.sdk.state.URLProtocolContext.PD_FUNCTION) ||
  262.                                             function.equals(org.openspcoop2.protocol.sdk.state.URLProtocolContext.PDtoSOAP_FUNCTION) ||
  263.                                             function.equals(org.openspcoop2.protocol.sdk.state.URLProtocolContext.IntegrationManager_FUNCTION) ||
  264.                                             function.equals(org.openspcoop2.protocol.sdk.state.URLProtocolContext.Check_FUNCTION) ||
  265.                                             function.equals(org.openspcoop2.protocol.sdk.state.URLProtocolContext.Proxy_FUNCTION) ||
  266.                                         (customContexts!=null && customContexts.isMatch(function, functionParameterForCheckCustom)))
  267.                                     )
  268.                             ){
  269.                         IDService idS = null;
  270.                         if(casoSpeciale_noProtocollo_noFunction) {
  271.                             if(pfEmptyContext!=null && idServiceDefaultEmptyContext!=null) {
  272.                                 idS = idServiceDefaultEmptyContext;                        
  273.                             }
  274.                         }
  275.                         else {
  276.                             idS = protocolFactoryManager.getDefaultServiceForWebContext(protocollo);
  277.                         }
  278.                         if(idS!=null) {
  279.                             this.protocolName = pfEmptyContext.getProtocol();
  280.                             this.idServiceCustom = idS;
  281.                             switch (idS) {
  282.                             case PORTA_DELEGATA:
  283.                                 function = org.openspcoop2.protocol.sdk.state.URLProtocolContext.PD_FUNCTION_GOVWAY;
  284.                                 emptyFunction = true;
  285.                                 break;
  286.                             case PORTA_APPLICATIVA:
  287.                                 function = org.openspcoop2.protocol.sdk.state.URLProtocolContext.PA_FUNCTION_GOVWAY;
  288.                                 emptyFunction = true;
  289.                                 break;
  290.                             case PORTA_DELEGATA_XML_TO_SOAP:
  291.                                 function = org.openspcoop2.protocol.sdk.state.URLProtocolContext.PDtoSOAP_FUNCTION_GOVWAY;
  292.                                 emptyFunction = true;
  293.                                 break;
  294.                             default:
  295.                             }
  296.                         }
  297.                     }
  298.                    
  299.                     StringBuilder bfSizePrefix = new StringBuilder(req.getContextPath());
  300.                     if(!Costanti.CONTEXT_EMPTY.equals(protocollo)) {
  301.                         bfSizePrefix.append("/").append(protocollo);
  302.                     }
  303.                     if(!emptyFunction) {
  304.                         bfSizePrefix.append("/").append(function);
  305.                     }
  306.                     bfSizePrefix.append("/");
  307.                    
  308.                     int sizePrefix = bfSizePrefix.length();
  309.                     if(req.getRequestURI().length()>sizePrefix){
  310.                         functionParameters = req.getRequestURI().substring(sizePrefix);
  311.                     }
  312.                     else {
  313.                         // Serve nei casi custom
  314.                         functionParameters = null;
  315.                     }
  316.                    
  317.                     if(!casoSpeciale_noProtocollo_noFunction && !emptyFunction) {
  318.                         if((customContexts!=null && customContexts.isMatch(function, functionParameters))) {
  319.                             this.idServiceCustom = customContexts.getServiceMatch(function, functionParameters);
  320.                             function = customContexts.getFunctionMatch(function, functionParameters);
  321.                             if(logCore!=null)
  322.                                 logCore.debug("CUSTOM FUNCTION ["+function+"] ["+this.idServiceCustom+"]");
  323.                             // ricalcolo function parameters
  324.                             sizePrefix = (req.getContextPath() + "/"+ protocollo + "/" + function + "/").length();
  325.                             if(req.getRequestURI().length()>sizePrefix){
  326.                                 functionParameters = req.getRequestURI().substring(sizePrefix);
  327.                             }
  328.                             else {
  329.                                 // Serve nei casi custom
  330.                                 functionParameters = null;
  331.                             }
  332.                         }
  333.                     }
  334.                    
  335.                     if(casoSpeciale_noProtocollo_noFunction && emptyFunction) {
  336.                         this.requestURI = this.requestURI.replace(servletContext+"/", servletContext+"/"+function+"/");
  337.                     }
  338.                     else if(emptyFunction) {
  339.                         this.requestURI = this.requestURI.replace(servletContext+"/"+protocollo+"/", servletContext+"/"+protocollo+"/"+function+"/");
  340.                     }
  341.                 }
  342.                                        
  343.                 if(logCore!=null)
  344.                     logCore.debug("Elaborazione finale Protocollo["+protocollo+"] Function["+function+"] FunctionParameters ["+functionParameters+"]");
  345.                
  346.                 if(!IMengine) {
  347.                     this.protocolWebContext = protocollo;
  348.                     IProtocolFactory<?> pf = protocolFactoryManager.getProtocolFactoryByServletContext(this.protocolWebContext);
  349.                     if(pf==null){
  350.                         if(!Costanti.CONTEXT_EMPTY.equals(this.protocolWebContext))
  351.                             throw new Exception("Non risulta registrato un protocollo con contesto ["+this.protocolWebContext+"]");
  352.                         else
  353.                             throw new Exception("Non risulta registrato un protocollo con contesto speciale 'vuoto'");
  354.                     }
  355.                     this.protocolName = pf.getProtocol();
  356.                 }
  357.    
  358.                 this.function = function;
  359.                 this.functionParameters = functionParameters;
  360.                
  361.             }
  362.            
  363.         }catch(Exception e){
  364.             throw new ProtocolException(e.getMessage(),e);
  365.         }
  366.     }
  367.    
  368.     @Override
  369.     public String toString() {
  370.         return this.toString("");
  371.     }
  372.     @Override
  373.     public String toString(String prefix) {
  374.         StringBuilder sb = new StringBuilder(super.toString(prefix));
  375.         if(this.idServiceCustom!=null) {
  376.             sb.append("\n").append(prefix).append("idServiceCustom: ").append(this.idServiceCustom);
  377.         }
  378.         return sb.toString();
  379.     }
  380. }