[cxf] branch 3.3.x-fixes updated: [CXF-8317] Make netty transport HttpObjectAggregator.maxContentLength configurable (#684)

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[cxf] branch 3.3.x-fixes updated: [CXF-8317] Make netty transport HttpObjectAggregator.maxContentLength configurable (#684)

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

coheigea pushed a commit to branch 3.3.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git


The following commit(s) were added to refs/heads/3.3.x-fixes by this push:
     new b6db1db  [CXF-8317] Make netty transport HttpObjectAggregator.maxContentLength configurable (#684)
b6db1db is described below

commit b6db1dbbddb5b8985c3c95dd4136208e9545efab
Author: Vlad Mamaev <[hidden email]>
AuthorDate: Thu Jul 23 19:35:24 2020 +0300

    [CXF-8317] Make netty transport HttpObjectAggregator.maxContentLength configurable (#684)
   
    (cherry picked from commit ce8c085ca78fe9d2c23ede5f999b9467b4db83bc)
---
 .../client/NettyHttpClientPipelineFactory.java     | 13 +++++++---
 .../http/netty/client/NettyHttpClientRequest.java  |  8 ++++++
 .../http/netty/client/NettyHttpConduit.java        | 30 ++++++++++++++++++++--
 3 files changed, 46 insertions(+), 5 deletions(-)

diff --git a/rt/transports/http-netty/netty-client/src/main/java/org/apache/cxf/transport/http/netty/client/NettyHttpClientPipelineFactory.java b/rt/transports/http-netty/netty-client/src/main/java/org/apache/cxf/transport/http/netty/client/NettyHttpClientPipelineFactory.java
index 2196016..658c566 100644
--- a/rt/transports/http-netty/netty-client/src/main/java/org/apache/cxf/transport/http/netty/client/NettyHttpClientPipelineFactory.java
+++ b/rt/transports/http-netty/netty-client/src/main/java/org/apache/cxf/transport/http/netty/client/NettyHttpClientPipelineFactory.java
@@ -44,16 +44,24 @@ public class NettyHttpClientPipelineFactory extends ChannelInitializer<Channel>
 
     private static final Logger LOG =
         LogUtils.getL7dLogger(NettyHttpClientPipelineFactory.class);
+
     private final TLSClientParameters tlsClientParameters;
     private final int readTimeout;
-    
+    private final int maxContentLength;
+
     public NettyHttpClientPipelineFactory(TLSClientParameters clientParameters) {
         this(clientParameters, 0);
     }
 
     public NettyHttpClientPipelineFactory(TLSClientParameters clientParameters, int readTimeout) {
+        this(clientParameters, readTimeout, NettyHttpConduit.DEFAULT_MAX_RESPONSE_CONTENT_LENGTH);
+    }
+
+    public NettyHttpClientPipelineFactory(TLSClientParameters clientParameters, int readTimeout,
+                                          int maxResponseContentLength) {
         this.tlsClientParameters = clientParameters;
         this.readTimeout = readTimeout;
+        this.maxContentLength = maxResponseContentLength;
     }
 
     @Override
@@ -70,8 +78,7 @@ public class NettyHttpClientPipelineFactory extends ChannelInitializer<Channel>
 
 
         pipeline.addLast("decoder", new HttpResponseDecoder());
-        // TODO need to configure the aggregator size
-        pipeline.addLast("aggregator", new HttpObjectAggregator(1048576));
+        pipeline.addLast("aggregator", new HttpObjectAggregator(maxContentLength));
         pipeline.addLast("encoder", new HttpRequestEncoder());
         pipeline.addLast("chunkedWriter", new ChunkedWriteHandler());
         if (readTimeout > 0) {
diff --git a/rt/transports/http-netty/netty-client/src/main/java/org/apache/cxf/transport/http/netty/client/NettyHttpClientRequest.java b/rt/transports/http-netty/netty-client/src/main/java/org/apache/cxf/transport/http/netty/client/NettyHttpClientRequest.java
index 4a47628..6110281 100644
--- a/rt/transports/http-netty/netty-client/src/main/java/org/apache/cxf/transport/http/netty/client/NettyHttpClientRequest.java
+++ b/rt/transports/http-netty/netty-client/src/main/java/org/apache/cxf/transport/http/netty/client/NettyHttpClientRequest.java
@@ -37,6 +37,7 @@ public class NettyHttpClientRequest {
     private CxfResponseCallBack cxfResponseCallback;
     private int connectionTimeout;
     private int receiveTimeout;
+    private int maxResponseContentLength;
 
     public NettyHttpClientRequest(URI requestUri, String method) {
         this.uri = requestUri;
@@ -95,4 +96,11 @@ public class NettyHttpClientRequest {
     }
 
 
+    public void setMaxResponseContentLength(int maxResponseContentLength) {
+        this.maxResponseContentLength = maxResponseContentLength;
+    }
+
+    public int getMaxResponseContentLength() {
+        return maxResponseContentLength;
+    }
 }
diff --git a/rt/transports/http-netty/netty-client/src/main/java/org/apache/cxf/transport/http/netty/client/NettyHttpConduit.java b/rt/transports/http-netty/netty-client/src/main/java/org/apache/cxf/transport/http/netty/client/NettyHttpConduit.java
index 1e11c36..f5d4e7a 100644
--- a/rt/transports/http-netty/netty-client/src/main/java/org/apache/cxf/transport/http/netty/client/NettyHttpConduit.java
+++ b/rt/transports/http-netty/netty-client/src/main/java/org/apache/cxf/transport/http/netty/client/NettyHttpConduit.java
@@ -35,6 +35,7 @@ import java.security.cert.Certificate;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.logging.Level;
 
 import javax.net.ssl.HostnameVerifier;
 import javax.net.ssl.SSLSession;
@@ -74,6 +75,9 @@ import io.netty.handler.ssl.SslHandler;
 
 public class NettyHttpConduit extends URLConnectionHTTPConduit implements BusLifeCycleListener {
     public static final String USE_ASYNC = "use.async.http.conduit";
+    public static final String MAX_RESPONSE_CONTENT_LENGTH =
+        "org.apache.cxf.transport.http.netty.maxResponseContentLength";
+    static final Integer DEFAULT_MAX_RESPONSE_CONTENT_LENGTH = 1048576;
     final NettyHttpConduitFactory factory;
     private Bootstrap bootstrap;
 
@@ -167,8 +171,10 @@ public class NettyHttpConduit extends URLConnectionHTTPConduit implements BusLif
         final NettyHttpClientRequest request = new NettyHttpClientRequest(uri, httpRequestMethod);
         final int ctimeout = determineConnectionTimeout(message, csPolicy);
         final int rtimeout = determineReceiveTimeout(message, csPolicy);
+        final int maxResponseContentLength = determineMaxResponseContentLength(message);
         request.setConnectionTimeout(ctimeout);
         request.setReceiveTimeout(rtimeout);
+        request.setMaxResponseContentLength(maxResponseContentLength);
 
         message.put(NettyHttpClientRequest.class, request);
 
@@ -337,9 +343,11 @@ public class NettyHttpConduit extends URLConnectionHTTPConduit implements BusLif
         protected void connect(boolean output) {
             if ("https".equals(url.getScheme())) {
                 TLSClientParameters clientParameters = findTLSClientParameters();
-                bootstrap.handler(new NettyHttpClientPipelineFactory(clientParameters, entity.getReceiveTimeout()));
+                bootstrap.handler(new NettyHttpClientPipelineFactory(clientParameters, entity.getReceiveTimeout(),
+                    entity.getMaxResponseContentLength()));
             } else {
-                bootstrap.handler(new NettyHttpClientPipelineFactory(null, entity.getReceiveTimeout()));
+                bootstrap.handler(new NettyHttpClientPipelineFactory(null, entity.getReceiveTimeout(),
+                    entity.getMaxResponseContentLength()));
             }
 
             ChannelFuture connFuture =
@@ -651,5 +659,23 @@ public class NettyHttpConduit extends URLConnectionHTTPConduit implements BusLif
     public void preShutdown() {
     }
 
+    protected static int determineMaxResponseContentLength(Message message) {
+        Integer maxResponseContentLength = null;
+        if (message.get(MAX_RESPONSE_CONTENT_LENGTH) != null) {
+            Object obj = message.get(MAX_RESPONSE_CONTENT_LENGTH);
+            try {
+                maxResponseContentLength = Integer.parseInt(obj.toString());
+            } catch (NumberFormatException e) {
+                LOG.log(Level.WARNING, "INVALID_TIMEOUT_FORMAT", new Object[] {
+                    MAX_RESPONSE_CONTENT_LENGTH, obj.toString()
+                });
+            }
+        }
+        if (maxResponseContentLength == null) {
+            maxResponseContentLength = DEFAULT_MAX_RESPONSE_CONTENT_LENGTH;
+        }
+        return maxResponseContentLength;
+    }
+
 
 }