HttpRequestConfig.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.utils.transport.http;

  21. import java.util.Arrays;
  22. import java.util.function.Function;
  23. import java.util.function.UnaryOperator;

  24. import org.openspcoop2.utils.UtilsException;
  25. import org.openspcoop2.utils.properties.PropertiesReader;

  26. /**
  27.  * HttpRequestConfig
  28.  *
  29.  * @author Tommaso Burlon (tommaso.burlon@link.it)
  30.  * @author $Author$
  31.  * @version $Rev$, $Date$
  32.  */
  33. public class HttpRequestConfig {
  34.    
  35.     private final PropertiesReader props;
  36.     private final Function<String, String> propSupplier;
  37.     private String prefix;
  38.    
  39.     private static final String PROP_URL = "baseUrl";
  40.     private static final String PROP_READ_TIMEOUT = "readTimeout";
  41.     private static final String PROP_CONNECTION_TIMEOUT = "connectTimeout";
  42.     private static final String PROP_HTTP_USERNAME = "http.username";
  43.     private static final String PROP_HTTP_PASSWORD = "http.password";
  44.     private static final String PROP_HTTP_QUERY = "http.queryParameters";
  45.     private static final String PROP_HTTP_HEADER = "http.headers";
  46.     private static final String PROP_HTTPS_HOSTNAME_VERIFIER = "https.hostnameVerifier";
  47.     private static final String PROP_HTTPS_TRUST_ALL_CERTS = "https.trustAllCerts";
  48.     private static final String PROP_HTTPS_TRUSTSTORE = "https.trustStore";
  49.     private static final String PROP_HTTPS_TRUSTSTORE_PASSWORD = "https.trustStore.password";
  50.     private static final String PROP_HTTPS_TRUSTSTORE_TYPE = "https.trustStore.type";
  51.     private static final String PROP_HTTPS_CRL = "https.trustStore.crl";
  52.     private static final String PROP_HTTPS_KEYSTORE = "https.keyStore";
  53.     private static final String PROP_HTTPS_KEYSTORE_PASSWORD = "https.keyStore.password";
  54.     private static final String PROP_HTTPS_KEYSTORE_TYPE = "https.keyStore.type";
  55.     private static final String PROP_HTTPS_KEY_ALIAS = "https.key.alias";
  56.     private static final String PROP_HTTPS_KEY_PASSWORD = "https.key.password";
  57.    
  58.     public HttpRequestConfig(String prefix, PropertiesReader props) {
  59.         this.prefix = prefix;
  60.         this.props = props;
  61.         this.propSupplier = null;
  62.     }
  63.    
  64.     public HttpRequestConfig(String prefix, UnaryOperator<String> props) {
  65.         this.prefix = prefix;
  66.         this.props = null;
  67.         this.propSupplier = props;
  68.     }
  69.    
  70.    
  71.     private String getProperty(String code, String key) {
  72.         return getProperty(code, key, true);
  73.     }
  74.    
  75.     private String getProperty(String code, String baseKey, boolean searchDefault) {
  76.         String value = null;
  77.         String key;
  78.        
  79.         if (code == null)
  80.             key = this.prefix + "." + baseKey;
  81.         else
  82.             key = this.prefix + "." + code + "." + baseKey;

  83.         if (this.props != null){
  84.             try {
  85.                 value = this.props.getValue(key);
  86.             } catch (UtilsException e) {
  87.                 value = null;
  88.             }
  89.         }
  90.        
  91.         if (this.propSupplier != null) {
  92.             value = this.propSupplier.apply(key);
  93.         }
  94.        
  95.         if (!searchDefault || value != null)
  96.             return value;
  97.         return this.getProperty(null, baseKey, false);
  98.     }

  99.    
  100.     private void fillHttpProperties(HttpRequest req, String code) {
  101.         req.setUsername(getProperty(code, PROP_HTTP_USERNAME));
  102.         req.setPassword(getProperty(code, PROP_HTTP_PASSWORD));
  103.     }
  104.    
  105.     private void fillHttpsProperties(HttpRequest req, String code) {
  106.         String hostnameVerifier = getProperty(code, PROP_HTTPS_HOSTNAME_VERIFIER);
  107.         if (hostnameVerifier != null)
  108.             req.setHostnameVerifier(Boolean.valueOf(hostnameVerifier));
  109.         String trustAllCerts =  getProperty(code, PROP_HTTPS_TRUST_ALL_CERTS);
  110.         if (trustAllCerts != null)
  111.             req.setTrustAllCerts(Boolean.valueOf(trustAllCerts));
  112.         req.setTrustStorePath(getProperty(code, PROP_HTTPS_TRUSTSTORE));
  113.         req.setTrustStorePassword(getProperty(code, PROP_HTTPS_TRUSTSTORE_PASSWORD));
  114.         req.setTrustStoreType(getProperty(code, PROP_HTTPS_TRUSTSTORE_TYPE));
  115.         req.setCrlPath(getProperty(code, PROP_HTTPS_CRL));
  116.        
  117.         req.setKeyStorePath(getProperty(code, PROP_HTTPS_KEYSTORE));
  118.         req.setKeyStorePassword(getProperty(code, PROP_HTTPS_KEYSTORE_PASSWORD));
  119.         req.setKeyStoreType(getProperty(code, PROP_HTTPS_KEYSTORE_TYPE));
  120.         req.setKeyAlias(getProperty(code, PROP_HTTPS_KEY_ALIAS));
  121.         req.setKeyPassword(getProperty(code, PROP_HTTPS_KEY_PASSWORD));
  122.     }
  123.    
  124.     public HttpRequest getBaseRequest() {
  125.         return this.getBaseRequest(null);
  126.     }

  127.     public HttpRequest getBaseRequest(String code) {
  128.         HttpRequest req = new HttpRequest();
  129.        
  130.         req.setUrl(getProperty(code, PROP_URL));
  131.        
  132.         String connectTimeout = getProperty(code, PROP_CONNECTION_TIMEOUT);
  133.         if (!"false".equals(connectTimeout))
  134.             req.setConnectTimeout(Integer.valueOf(connectTimeout));
  135.        
  136.         String readTimeout = getProperty(code, PROP_READ_TIMEOUT);
  137.         if (!"false".equals(readTimeout))
  138.             req.setReadTimeout(Integer.valueOf(readTimeout));
  139.        
  140.         fillHttpProperties(req, code);
  141.         fillHttpsProperties(req, code);
  142.        
  143.         String queryParameters = getProperty(code, PROP_HTTP_QUERY);
  144.         if (queryParameters != null) {
  145.             Arrays.stream(queryParameters.split(";"))
  146.                 .map(str -> str.split(":"))
  147.                 .forEach(arr -> req.addParam(arr[0], arr[1]));
  148.         }
  149.        
  150.         String headers = getProperty(code, PROP_HTTP_HEADER);
  151.         if (headers != null) {
  152.             Arrays.stream(headers.split(";"))
  153.             .map(str -> str.split(":"))
  154.             .forEach(arr -> req.addHeader(arr[0], arr[1]));
  155.         }
  156.        
  157.        
  158.         return req;
  159.     }
  160.    
  161. }