MappingProperties.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.mapping;

  21. import java.io.IOException;
  22. import java.util.ArrayList;
  23. import java.util.Enumeration;
  24. import java.util.Iterator;
  25. import java.util.List;
  26. import java.util.Properties;

  27. import org.slf4j.Logger;
  28. import org.openspcoop2.protocol.engine.ProtocolFactoryManager;
  29. import org.openspcoop2.protocol.engine.constants.ModalitaIdentificazione;
  30. import org.openspcoop2.protocol.manifest.Openspcoop2;
  31. import org.openspcoop2.protocol.sdk.IProtocolFactory;
  32. import org.openspcoop2.protocol.sdk.ProtocolException;
  33. import org.openspcoop2.protocol.sdk.constants.IDService;
  34. import org.openspcoop2.protocol.sdk.state.FunctionContextCustom;
  35. import org.openspcoop2.protocol.sdk.state.FunctionContextsCustom;

  36. /**
  37.  * Service Mappings
  38.  *
  39.  * @author Poli Andrea (apoli@link.it)
  40.  * @author $Author$
  41.  * @version $Rev$, $Date$
  42.  */
  43. public class MappingProperties {
  44.     private Logger log;
  45.     private Properties propertiesReader;
  46.     protected MappingProperties(String fileNameProperties,java.io.InputStream properties,Logger log) throws IOException{

  47.         this.log = log;
  48.         this.propertiesReader = new Properties();
  49.         try {  
  50.             this.propertiesReader.load(properties);
  51.             properties.close();
  52.         } catch(IOException e) {
  53.             this.log.error("Riscontrato errore durante la lettura del file '"+fileNameProperties+"': \n\n"+e.getMessage());
  54.             try {
  55.                 if(properties!=null)
  56.                     properties.close();
  57.             } catch(Exception er) {
  58.                 // close
  59.             }
  60.             throw e;
  61.         }  
  62.     }
  63.    
  64.     public String getUrlWithoutContext(String protocol,String urlWithContext,IDService idService, FunctionContextsCustom customContexts) throws ProtocolException{
  65.         IProtocolFactory<?> pf = ProtocolFactoryManager.getInstance().getProtocolFactoryByName(protocol);
  66.         Openspcoop2 manifestProtocol = pf.getManifest();
  67.         String urlWithoutContext = null;
  68.        
  69.         List<String> paContexts = new ArrayList<>();
  70.         if(IDService.PORTA_APPLICATIVA.equals(idService) || IDService.PORTA_APPLICATIVA_NIO.equals(idService)){
  71.             paContexts.add("/PA");
  72.             if(customContexts!=null) {
  73.                 List<FunctionContextCustom> list = customContexts.getContexts();
  74.                 if(list!=null && list.size()>0) {
  75.                     for (FunctionContextCustom functionContextCustom : list) {
  76.                         if(functionContextCustom.getIdService()!=null) {
  77.                             if(idService.equals(functionContextCustom.getIdService())){
  78.                                 paContexts.add("/"+functionContextCustom.getContext());
  79.                             }
  80.                         }
  81.                         else if(functionContextCustom.getSubcontext()!=null && functionContextCustom.getSubcontext().size()>0){
  82.                             Iterator<String> it = functionContextCustom.getSubcontext().keySet().iterator();
  83.                             while (it.hasNext()) {
  84.                                 String subcontext = (String) it.next();
  85.                                 IDService idServiceSubContext = functionContextCustom.getSubcontext().get(subcontext);
  86.                                 if(idService.equals(idServiceSubContext)){
  87.                                     paContexts.add("/"+functionContextCustom.getContext()+"/"+subcontext);
  88.                                 }
  89.                             }
  90.                         }
  91.                     }
  92.                 }
  93.             }
  94.         }
  95.         else{
  96.             throw new ProtocolException("Service ["+idService+"] non gestito tramite UrlMapping");
  97.         }
  98.        
  99.         // context con un nome
  100.         for (int i = 0; i < manifestProtocol.getWeb().sizeContextList(); i++) {
  101.             String context = manifestProtocol.getWeb().getContext(i).getName();
  102.             boolean found = false;
  103.             for (int j = 0; j < paContexts.size(); j++) {
  104.                 String prefixProtocol = context + paContexts.get(j);
  105.                 if(urlWithContext.contains(prefixProtocol)){
  106.                     if(urlWithContext.endsWith(prefixProtocol)){
  107.                         urlWithoutContext = "";
  108.                     }
  109.                     else{
  110.                         int offset = urlWithContext.indexOf(prefixProtocol);
  111.                         urlWithoutContext = urlWithContext.substring(offset+prefixProtocol.length(), urlWithContext.length());
  112.                     }
  113.                     found = true;
  114.                     break;
  115.                 }
  116.             }
  117.             if(found){
  118.                 break;
  119.             }
  120.         }
  121.         // empty context
  122.         if(urlWithoutContext==null){
  123.             if(manifestProtocol.getWeb().getEmptyContext()!=null && manifestProtocol.getWeb().getEmptyContext().getEnabled() ){
  124.                 for (int j = 0; j < paContexts.size(); j++) {
  125.                     String prefixProtocol = paContexts.get(j);
  126.                     if(urlWithContext.contains(prefixProtocol)){
  127.                         if(urlWithContext.endsWith(prefixProtocol)){
  128.                             urlWithoutContext = "";
  129.                         }
  130.                         else{
  131.                             int offset = urlWithContext.indexOf(prefixProtocol);
  132.                             urlWithoutContext = urlWithContext.substring(offset+prefixProtocol.length(), urlWithContext.length());
  133.                         }
  134.                         break;
  135.                     }
  136.                 }
  137.             }
  138.         }
  139.         if(urlWithoutContext==null){
  140.             throw new ProtocolException("Identificazione URL senza contesto applicazione e protocollo non riuscita (protocollo ["+ protocol + "], url [" + urlWithContext + "])");
  141.         }
  142.         //System.out.println("URL["+urlWithContext+"] URL_SENZA_CONTEXT["+urlWithoutContext+"]");
  143.         return urlWithoutContext;
  144.     }
  145.    
  146.     protected String getMappingName(String protocol, String urlWithContext,IDService idService, FunctionContextsCustom customContexts) throws ProtocolException{
  147.        
  148.         //devo recuperare tutte le url configurate e vedere se una matcha
  149.         //le proprieta' che mi interessano sono nella forma:
  150.         //protocol.pa.xxxxx.url
  151.         String prefix = protocol + ".pa.";
  152.         String suffix = ".url";    
  153.                
  154.         String urlWithoutContext = this.getUrlWithoutContext(protocol,urlWithContext, idService, customContexts);
  155.        
  156.         Enumeration<?> keys = this.propertiesReader.keys();
  157.        
  158.         while(keys.hasMoreElements()){
  159.             String propertyName = (String) keys.nextElement();
  160.             if(propertyName.startsWith(prefix) && propertyName.endsWith(suffix)){
  161.                 //Trovata una URL registrata per il protocollo che mi interessa
  162.                 String tmpurl = this.propertiesReader.getProperty(propertyName);
  163.                
  164.                 if(tmpurl != null) {
  165.                     tmpurl = tmpurl.trim();
  166.                 } else {
  167.                     continue;
  168.                 }
  169.                
  170.                 if(tmpurl.startsWith("/")==false && !tmpurl.equals("*")){
  171.                     tmpurl = "/" + tmpurl;
  172.                 }
  173.                
  174. //              if(tmpurl.endsWith("/")==false && !tmpurl.equals("*"))
  175. //                  tmpurl = tmpurl + "/";
  176.                
  177.                 //Verifico se la URL matcha.
  178.                 //Due casi: sono uguali o il prefisso e' lo stesso e c'e' la wildcard (*)
  179.                
  180.                 boolean match = false;
  181.                 if(tmpurl.endsWith("*")){
  182.                     match = urlWithoutContext.startsWith(tmpurl.substring(0,tmpurl.length()-1));
  183.                 } else {
  184.                     match = urlWithoutContext.equals(tmpurl);
  185.                 }
  186.                
  187.                 if(match){
  188.                     //Trovato. Estraggo il nome del mapping.
  189.                     //Mi accerto che sia valido, altrimenti continuo nella ricerca.
  190.                     String mapping = propertyName.substring(prefix.length(), propertyName.length() - suffix.length());
  191.                     if(mapping != null && !mapping.equals(""))
  192.                         return mapping;
  193.                 }
  194.             }
  195.         }
  196.         throw new ProtocolException("Non risulta configurata nessuna configurazione di mapping nel protocollo ["+ protocol + "] per la url [" + urlWithContext + "]");
  197.     }
  198.    
  199.     protected ModalitaIdentificazione getModalita(String protocol, String mappingName, String attribute) throws ProtocolException{
  200.         String propertyName = protocol + ".pa." + mappingName + "." + attribute;    
  201.         String modalita = this.propertiesReader.getProperty(propertyName);
  202.         if(modalita != null){
  203.             modalita = modalita.trim();
  204.         } else {
  205.             throw new ProtocolException("Nessuna modalita configurata per la proprieta' [" + propertyName + "].");
  206.         }
  207.         ModalitaIdentificazione m = ModalitaIdentificazione.toEnumConstant(modalita);
  208.         if(m==null){
  209.             throw new ProtocolException("La modalita' [" + modalita + "] configurata per la proprieta [" + propertyName + "] non e' supportata.");
  210.         }
  211.         return m;
  212.     }
  213.    
  214.     protected String getValue(String protocol, String mappingName, String attribute) {
  215.         String propertyName = protocol + ".pa." + mappingName + "." + attribute + ".value";
  216.         String value = this.propertiesReader.getProperty(propertyName);
  217.         if(value != null)
  218.             value = value.trim();
  219.         return value;
  220.     }
  221.    
  222.     protected String getPattern(String protocol, String mappingName, String attribute) {
  223.         String propertyName = protocol + ".pa." + mappingName + "." + attribute + ".pattern";  
  224.         String value = this.propertiesReader.getProperty(propertyName);
  225.         if(value != null) value = value.trim();
  226.         return value;
  227.     }
  228.    
  229.     protected String getName(String protocol, String mappingName, String attribute) {
  230.         String propertyName = protocol + ".pa." + mappingName + "." + attribute + ".name";  
  231.         String value = this.propertiesReader.getProperty(propertyName);
  232.         if(value != null) value = value.trim();
  233.         return value;
  234.     }
  235.    
  236.     protected String getAnonymous(String protocol, String mappingName, String attribute) {
  237.         String propertyName = protocol + ".pa." + mappingName + "." + attribute + ".anonymous";
  238.         String value = this.propertiesReader.getProperty(propertyName);
  239.         if(value != null) value = value.trim();
  240.         return value;
  241.     }
  242.    
  243. }