DynamicMapBuilderUtils.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.dynamic;

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

  24. import javax.servlet.http.HttpServletRequest;

  25. import org.openspcoop2.core.constants.Costanti;
  26. import org.openspcoop2.pdd.core.token.TokenException;
  27. import org.openspcoop2.pdd.services.connector.FormUrlEncodedHttpServletRequest;
  28. import org.openspcoop2.protocol.sdk.Busta;
  29. import org.openspcoop2.protocol.sdk.Context;
  30. import org.openspcoop2.protocol.sdk.state.RequestInfo;
  31. import org.openspcoop2.utils.transport.http.HttpServletTransportRequestContext;
  32. import org.slf4j.Logger;

  33. /**
  34.  * DynamicMapBuilderUtils
  35.  *
  36.  * @author Andrea Poli (apoli@link.it)
  37.  * @author $Author$
  38.  * @version $Rev$, $Date$
  39.  */
  40. public class DynamicMapBuilderUtils {
  41.    
  42.     private DynamicMapBuilderUtils() {}

  43.     public static void injectDynamicMap(Busta busta,
  44.             RequestInfo requestInfo, org.openspcoop2.utils.Map<Object> contextParam, Logger log) {
  45.        
  46.         /**&& !context.containsKey(Costanti.DYNAMIC_MAP_CONTEXT)) { aggiorno */
  47.        
  48.         Context context = null;
  49.         if(contextParam instanceof Context) {
  50.             context = (Context) contextParam;
  51.         }
  52.            
  53.         Map<String,Object> dynamicMap = DynamicMapBuilderUtils.buildDynamicMap(busta, requestInfo, context, log);
  54.        
  55.         if(context!=null) {
  56.             context.put(Costanti.DYNAMIC_MAP_CONTEXT, dynamicMap);
  57.         }
  58.         if(requestInfo!=null) {
  59.             requestInfo.setDynamicMap(dynamicMap);
  60.         }
  61.        
  62.     }
  63.     public static Map<String,Object> readDynamicMap(org.openspcoop2.utils.Map<Object> context){
  64.         return Costanti.readDynamicMap(context);
  65.     }
  66.    
  67.     public static Map<String, Object> buildDynamicMap(Busta busta,
  68.             RequestInfo requestInfo, Context pddContext, Logger log) {
  69.    
  70.         Map<String, Object> dynamicMap = new HashMap<>();
  71.        
  72.         Map<String, List<String>> pTrasporto = null;
  73.         String urlInvocazione = null;
  74.         Map<String, List<String>> pQuery = null;
  75.         Map<String, List<String>> pForm = null;
  76.         if(requestInfo!=null && requestInfo.getProtocolContext()!=null) {
  77.             pTrasporto = requestInfo.getProtocolContext().getHeaders();
  78.             urlInvocazione = requestInfo.getProtocolContext().getUrlInvocazione_formBased();
  79.             pQuery = requestInfo.getProtocolContext().getParameters();
  80.             if(requestInfo.getProtocolContext() instanceof HttpServletTransportRequestContext) {
  81.                 HttpServletTransportRequestContext httpServletContext = requestInfo.getProtocolContext();
  82.                 HttpServletRequest httpServletRequest = httpServletContext.getHttpServletRequest();
  83.                 if(httpServletRequest instanceof FormUrlEncodedHttpServletRequest) {
  84.                     FormUrlEncodedHttpServletRequest formServlet = (FormUrlEncodedHttpServletRequest) httpServletRequest;
  85.                     if(formServlet.getFormUrlEncodedParametersValues()!=null &&
  86.                             !formServlet.getFormUrlEncodedParametersValues().isEmpty()) {
  87.                         pForm = formServlet.getFormUrlEncodedParametersValues();
  88.                     }
  89.                 }
  90.             }
  91.         }
  92.                
  93.         ErrorHandler errorHandler = new ErrorHandler();
  94.         DynamicUtils.fillDynamicMapRequest(log, dynamicMap, pddContext, urlInvocazione,
  95.                 null,
  96.                 null,
  97.                 busta,
  98.                 pTrasporto,
  99.                 pQuery,
  100.                 pForm,
  101.                 errorHandler);
  102.        
  103.         return dynamicMap;
  104.     }
  105.     public static String convertDynamicPropertyValue(String v, String nome, Map<String, Object> dynamicMap, Context context) throws TokenException {
  106.         if(v!=null && !"".equals(v) && dynamicMap!=null) {
  107.             try {
  108.                 v = DynamicUtils.convertDynamicPropertyValue(nome+".gwt", v, dynamicMap, context);
  109.             }catch(Exception e) {
  110.                 throw new TokenException(e.getMessage(),e);
  111.             }
  112.         }
  113.         return v;
  114.     }
  115.    
  116. }