TransportUtilities.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.message.utils;

  21. import java.util.List;
  22. import java.util.Map;

  23. import org.openspcoop2.message.ForwardConfig;
  24. import org.openspcoop2.message.OpenSPCoop2MessageProperties;
  25. import org.openspcoop2.message.constants.MessageRole;
  26. import org.openspcoop2.message.exception.MessageException;
  27. import org.openspcoop2.utils.transport.http.HttpConstants;

  28. /**
  29.  * TransportUtilities
  30.  *
  31.  * @author Poli Andrea (apoli@link.it)
  32.  * @author $Author$
  33.  * @version $Rev$, $Date$
  34.  */
  35. public class TransportUtilities {
  36.    
  37.     public static void initializeTransportHeaders(OpenSPCoop2MessageProperties op2MessageProperties, MessageRole messageRole,
  38.             Map<String, List<String>> transportHeaders, ForwardConfig forwardConfig) throws MessageException{
  39.        
  40.         initializeHeaders(true,op2MessageProperties, messageRole, transportHeaders, forwardConfig);
  41.        
  42.     }
  43.    
  44.     public static void initializeForwardUrlParameters(OpenSPCoop2MessageProperties op2MessageProperties, MessageRole messageRole,
  45.             Map<String, List<String>> forwardUrlParameters, ForwardConfig forwardConfig) throws MessageException{
  46.        
  47.         initializeHeaders(false,op2MessageProperties, messageRole, forwardUrlParameters, forwardConfig);
  48.        
  49.     }
  50.    
  51.     private static boolean match(String tipoOp, String key, List<String> listParam) {
  52.         String keyLowerCase = key.toLowerCase();

  53.         return listParam.stream()
  54.                         .map( String::toLowerCase )
  55.                         .anyMatch( (p) -> p.equals(keyLowerCase) ||
  56.                                           p.equals( "*" ) ||
  57.                                           ( p.endsWith( "*" ) && keyLowerCase.startsWith( p.substring(0, p.length()-1) ) ) );
  58.     }
  59.        
  60.     private static void initializeHeaders(boolean trasporto, OpenSPCoop2MessageProperties op2MessageProperties, MessageRole messageRole,
  61.             Map<String, List<String>> applicativeInfo, ForwardConfig forwardConfig) throws MessageException{
  62.        
  63.         try{
  64. //          String tipo = "Header";
  65. //          if(trasporto==false){
  66. //              tipo = "URLParameter";
  67. //          }
  68.            
  69.             //System.out.println(tipo+" =============================== ["+messageRole+"]");
  70.            
  71.             if(applicativeInfo!=null && applicativeInfo.size()>0){
  72.                 applicativeInfo.forEach( (key,values) -> {
  73.                     if(MessageRole.REQUEST.equals(messageRole)==false){
  74.                         if(trasporto && HttpConstants.RETURN_CODE.equalsIgnoreCase(key)){
  75.                             return;
  76.                         }
  77.                     }
  78.                     boolean add = true;
  79.                     boolean white = false;
  80.                     if(forwardConfig.getWhiteList()!=null && forwardConfig.getWhiteList().size()>0) {
  81.                         white = match("whiteList", key, forwardConfig.getWhiteList());
  82.                     }
  83.                     if(!white && forwardConfig.getBlackList()!=null && forwardConfig.getBlackList().size()>0) {
  84.                         if(match("blackList", key, forwardConfig.getBlackList())) {
  85.                             add = false;
  86.                         }
  87.                     }
  88.                     if( add ){
  89.                         //System.out.println("ADD ["+key+"] ["+applicativeInfo.getProperty(key)+"]");
  90.                         op2MessageProperties.setProperty(key, values);
  91.                     }
  92. //                  else {
  93. //                      System.out.println("FILTRO ["+key+"]");
  94. //                  }
  95.                 });
  96.             }
  97.         }catch(Exception e){
  98.             throw new MessageException(e.getMessage(),e);
  99.         }
  100.        
  101.     }
  102. }