This is an automated email from the ASF dual-hosted git repository.

amagyar pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/knox.git


The following commit(s) were added to refs/heads/master by this push:
     new fcee4ecff KNOX-3007 - Make http client cookie spec parameter 
configurable (#841)
fcee4ecff is described below

commit fcee4ecffd850cbb3f03ded84b0cdd0dc22578af
Author: Attila Magyar <[email protected]>
AuthorDate: Mon Feb 26 10:37:19 2024 +0100

    KNOX-3007 - Make http client cookie spec parameter configurable (#841)
---
 .../knox/gateway/config/impl/GatewayConfigImpl.java       |  6 ++++++
 .../java/org/apache/knox/gateway/GatewayTestConfig.java   |  5 +++++
 .../java/org/apache/knox/gateway/SpiGatewayMessages.java  |  3 +++
 .../org/apache/knox/gateway/config/GatewayConfig.java     |  4 ++++
 .../knox/gateway/dispatch/DefaultHttpClientFactory.java   | 15 +++++++++++++++
 .../gateway/dispatch/DefaultHttpClientFactoryTest.java    |  8 ++++++++
 6 files changed, 41 insertions(+)

diff --git 
a/gateway-server/src/main/java/org/apache/knox/gateway/config/impl/GatewayConfigImpl.java
 
b/gateway-server/src/main/java/org/apache/knox/gateway/config/impl/GatewayConfigImpl.java
index 2501010b4..a5d10a099 100644
--- 
a/gateway-server/src/main/java/org/apache/knox/gateway/config/impl/GatewayConfigImpl.java
+++ 
b/gateway-server/src/main/java/org/apache/knox/gateway/config/impl/GatewayConfigImpl.java
@@ -128,6 +128,7 @@ public class GatewayConfigImpl extends Configuration 
implements GatewayConfig {
   private static final String HTTP_CLIENT_MAX_CONNECTION = 
GATEWAY_CONFIG_FILE_PREFIX + ".httpclient.maxConnections";
   private static final String HTTP_CLIENT_CONNECTION_TIMEOUT = 
GATEWAY_CONFIG_FILE_PREFIX + ".httpclient.connectionTimeout";
   private static final String HTTP_CLIENT_SOCKET_TIMEOUT = 
GATEWAY_CONFIG_FILE_PREFIX + ".httpclient.socketTimeout";
+  private static final String HTTP_CLIENT_COOKIE_SPEC = 
GATEWAY_CONFIG_FILE_PREFIX + ".httpclient.cookieSpec";
   private static final String THREAD_POOL_MAX = GATEWAY_CONFIG_FILE_PREFIX + 
".threadpool.max";
   public static final String HTTP_SERVER_REQUEST_BUFFER = 
GATEWAY_CONFIG_FILE_PREFIX + ".httpserver.requestBuffer";
   public static final String HTTP_SERVER_REQUEST_HEADER_BUFFER = 
GATEWAY_CONFIG_FILE_PREFIX + ".httpserver.requestHeaderBuffer";
@@ -1565,4 +1566,9 @@ public class GatewayConfigImpl extends Configuration 
implements GatewayConfig {
     return getInt(TOKEN_MIGRATION_PROGRESS_COUNT, 10);
   }
 
+  @Override
+  public String getHttpClientCookieSpec() {
+    return get(HTTP_CLIENT_COOKIE_SPEC);
+  }
+
 }
diff --git 
a/gateway-spi-common/src/main/java/org/apache/knox/gateway/GatewayTestConfig.java
 
b/gateway-spi-common/src/main/java/org/apache/knox/gateway/GatewayTestConfig.java
index d8bf0c663..b5a630300 100644
--- 
a/gateway-spi-common/src/main/java/org/apache/knox/gateway/GatewayTestConfig.java
+++ 
b/gateway-spi-common/src/main/java/org/apache/knox/gateway/GatewayTestConfig.java
@@ -1106,4 +1106,9 @@ public class GatewayTestConfig extends Configuration 
implements GatewayConfig {
     return 1;
   }
 
+  @Override
+  public String getHttpClientCookieSpec() {
+    return null;
+  }
+
 }
diff --git 
a/gateway-spi/src/main/java/org/apache/knox/gateway/SpiGatewayMessages.java 
b/gateway-spi/src/main/java/org/apache/knox/gateway/SpiGatewayMessages.java
index 4eaa6cfac..cbd310a63 100644
--- a/gateway-spi/src/main/java/org/apache/knox/gateway/SpiGatewayMessages.java
+++ b/gateway-spi/src/main/java/org/apache/knox/gateway/SpiGatewayMessages.java
@@ -97,6 +97,9 @@ public interface SpiGatewayMessages {
   @Message( level = MessageLevel.INFO, text = "HTTP client socket timeout is 
set to {0} ms for {1}" )
   void setHttpClientSocketTimeout(int socketTimeout, String serviceRole);
 
+  @Message( level = MessageLevel.INFO, text = "HTTP client cookie spec is set 
to {0}" )
+  void setHttpClientCookieSpec(String cookieSpec);
+
   @Message( level = MessageLevel.INFO, text = "replayBufferSize is set to {0} 
for {1}" )
   void setReplayBufferSize(int replayBufferSize, String serviceRole);
 
diff --git 
a/gateway-spi/src/main/java/org/apache/knox/gateway/config/GatewayConfig.java 
b/gateway-spi/src/main/java/org/apache/knox/gateway/config/GatewayConfig.java
index 9096894b7..f2534dbb6 100644
--- 
a/gateway-spi/src/main/java/org/apache/knox/gateway/config/GatewayConfig.java
+++ 
b/gateway-spi/src/main/java/org/apache/knox/gateway/config/GatewayConfig.java
@@ -934,4 +934,8 @@ public interface GatewayConfig {
    */
   int getTokenMigrationProgressCount();
 
+  /**
+   * @return CookieSpec for the HTTP client used by the dispatch, see 
org.apache.http.client.config.CookieSpecs
+   */
+  String getHttpClientCookieSpec();
 }
diff --git 
a/gateway-spi/src/main/java/org/apache/knox/gateway/dispatch/DefaultHttpClientFactory.java
 
b/gateway-spi/src/main/java/org/apache/knox/gateway/dispatch/DefaultHttpClientFactory.java
index 71b2cdaaf..03a4f891a 100644
--- 
a/gateway-spi/src/main/java/org/apache/knox/gateway/dispatch/DefaultHttpClientFactory.java
+++ 
b/gateway-spi/src/main/java/org/apache/knox/gateway/dispatch/DefaultHttpClientFactory.java
@@ -238,6 +238,11 @@ public class DefaultHttpClientFactory implements 
HttpClientFactory {
       builder.setSocketTimeout( socketTimeout );
       LOG.setHttpClientSocketTimeout(socketTimeout, serviceRole == null ? 
"N/A" : serviceRole);
     }
+    String cookieSpec = getCookieSpec(config);
+    if (cookieSpec != null) {
+      LOG.setHttpClientCookieSpec(cookieSpec);
+      builder.setCookieSpec(cookieSpec);
+    }
 
     // HttpClient 4.5.7 is broken for %2F handling with url normalization.
     // However, HttpClient 4.5.8+ (HTTPCLIENT-1968) has reasonable url
@@ -369,4 +374,14 @@ public class DefaultHttpClientFactory implements 
HttpClientFactory {
     Period p = Period.parse( s, f );
     return p.toStandardDuration().getMillis();
   }
+
+  private static String getCookieSpec(FilterConfig filterConfig) {
+    String cookieSpec = filterConfig.getInitParameter("httpclient.cookieSpec");
+    if (StringUtils.isNotBlank(cookieSpec)) {
+      return cookieSpec;
+    }
+    GatewayConfig globalConfig =
+            
(GatewayConfig)filterConfig.getServletContext().getAttribute(GatewayConfig.GATEWAY_CONFIG_ATTRIBUTE);
+    return globalConfig != null ? globalConfig.getHttpClientCookieSpec() : 
null;
+  }
 }
diff --git 
a/gateway-spi/src/test/java/org/apache/knox/gateway/dispatch/DefaultHttpClientFactoryTest.java
 
b/gateway-spi/src/test/java/org/apache/knox/gateway/dispatch/DefaultHttpClientFactoryTest.java
index 40c2b24b5..d8fe488ff 100644
--- 
a/gateway-spi/src/test/java/org/apache/knox/gateway/dispatch/DefaultHttpClientFactoryTest.java
+++ 
b/gateway-spi/src/test/java/org/apache/knox/gateway/dispatch/DefaultHttpClientFactoryTest.java
@@ -29,6 +29,7 @@ import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 
 import org.apache.http.client.HttpClient;
+import org.apache.http.client.config.CookieSpecs;
 import org.apache.http.client.config.RequestConfig;
 import org.apache.knox.gateway.config.GatewayConfig;
 import org.apache.knox.gateway.services.GatewayServices;
@@ -61,6 +62,7 @@ public class DefaultHttpClientFactoryTest {
     expect(gatewayConfig.getHttpClientMaxConnections()).andReturn(32).once();
     
expect(gatewayConfig.getHttpClientConnectionTimeout()).andReturn(20000).once();
     expect(gatewayConfig.getHttpClientSocketTimeout()).andReturn(20000).once();
+    
expect(gatewayConfig.getHttpClientCookieSpec()).andReturn(CookieSpecs.STANDARD).anyTimes();
 
     GatewayServices gatewayServices = createMock(GatewayServices.class);
     
expect(gatewayServices.getService(ServiceType.KEYSTORE_SERVICE)).andReturn(keystoreService).once();
@@ -77,6 +79,7 @@ public class DefaultHttpClientFactoryTest {
     
expect(filterConfig.getInitParameter("httpclient.socketTimeout")).andReturn(null).once();
     
expect(filterConfig.getInitParameter("serviceRole")).andReturn(null).once();
     expect(filterConfig.getInitParameter("retryCount")).andReturn(null).once();
+    
expect(filterConfig.getInitParameter("httpclient.cookieSpec")).andReturn(null).anyTimes();
 
     replay(keystoreService, gatewayConfig, gatewayServices, servletContext, 
filterConfig);
 
@@ -209,6 +212,7 @@ public class DefaultHttpClientFactoryTest {
     GatewayConfig gatewayConfig = createMock(GatewayConfig.class);
     
expect(gatewayConfig.getHttpClientConnectionTimeout()).andReturn(20000).once();
     expect(gatewayConfig.getHttpClientSocketTimeout()).andReturn(20000).once();
+    
expect(gatewayConfig.getHttpClientCookieSpec()).andReturn(CookieSpecs.STANDARD).anyTimes();
 
     ServletContext servletContext = createMock(ServletContext.class);
     
expect(servletContext.getAttribute(GatewayConfig.GATEWAY_CONFIG_ATTRIBUTE)).andReturn(gatewayConfig).atLeastOnce();
@@ -217,6 +221,7 @@ public class DefaultHttpClientFactoryTest {
     
expect(filterConfig.getServletContext()).andReturn(servletContext).atLeastOnce();
     
expect(filterConfig.getInitParameter("httpclient.connectionTimeout")).andReturn(null).once();
     
expect(filterConfig.getInitParameter("httpclient.socketTimeout")).andReturn(null).once();
+    
expect(filterConfig.getInitParameter("httpclient.cookieSpec")).andReturn(null).anyTimes();
 
     replay(gatewayConfig, servletContext, filterConfig);
 
@@ -246,6 +251,7 @@ public class DefaultHttpClientFactoryTest {
     
expect(gatewayConfig.getHttpClientMaxConnections()).andReturn(32).anyTimes();
     
expect(gatewayConfig.getHttpClientConnectionTimeout()).andReturn(20000).anyTimes();
     
expect(gatewayConfig.getHttpClientSocketTimeout()).andReturn(20000).anyTimes();
+    
expect(gatewayConfig.getHttpClientCookieSpec()).andReturn(CookieSpecs.STANDARD).anyTimes();
 
     GatewayServices gatewayServices = createMock(GatewayServices.class);
     
expect(gatewayServices.getService(ServiceType.KEYSTORE_SERVICE)).andReturn(keystoreService).anyTimes();
@@ -260,6 +266,7 @@ public class DefaultHttpClientFactoryTest {
     
expect(filterConfigSafe.getInitParameter("httpclient.maxConnections")).andReturn(null).once();
     
expect(filterConfigSafe.getInitParameter("httpclient.connectionTimeout")).andReturn(null).once();
     
expect(filterConfigSafe.getInitParameter("httpclient.socketTimeout")).andReturn(null).once();
+    
expect(filterConfigSafe.getInitParameter("httpclient.cookieSpec")).andReturn(null).anyTimes();
     
expect(filterConfigSafe.getInitParameter("serviceRole")).andReturn(null).once();
     
expect(filterConfigSafe.getInitParameter("retryCount")).andReturn("3").anyTimes();
     
expect(filterConfigSafe.getInitParameter("retryNonSafeRequest")).andReturn(null).anyTimes();
@@ -273,6 +280,7 @@ public class DefaultHttpClientFactoryTest {
     
expect(filterConfigUnSafe.getInitParameter("serviceRole")).andReturn(null).once();
     
expect(filterConfigUnSafe.getInitParameter("retryCount")).andReturn("3").anyTimes();
     
expect(filterConfigUnSafe.getInitParameter("retryNonSafeRequest")).andReturn("true").anyTimes();
+    
expect(filterConfigUnSafe.getInitParameter("httpclient.cookieSpec")).andReturn(null).anyTimes();
 
     replay(keystoreService, gatewayConfig, gatewayServices, servletContext, 
filterConfigSafe, filterConfigUnSafe);
 

Reply via email to