CAMEL-9040: Fixed netty leak

Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/16c5e34b
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/16c5e34b
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/16c5e34b

Branch: refs/heads/master
Commit: 16c5e34b6c6d0f8f48ba51f159dd9897576847d4
Parents: e7782eb
Author: Claus Ibsen <davscl...@apache.org>
Authored: Wed May 4 11:04:31 2016 +0200
Committer: Claus Ibsen <davscl...@apache.org>
Committed: Wed May 4 14:07:56 2016 +0200

----------------------------------------------------------------------
 .../netty4/http/DefaultNettyHttpBinding.java    |  8 +++++++-
 .../http/HttpClientInitializerFactory.java      |  2 --
 .../http/HttpServerInitializerFactory.java      |  3 +--
 .../HttpServerSharedInitializerFactory.java     |  2 +-
 ...ttyChannelBufferStreamCacheOnCompletion.java |  1 -
 .../netty4/http/NettyHttpProducer.java          |  1 -
 .../http/handlers/HttpServerChannelHandler.java | 21 +++++++++++---------
 7 files changed, 21 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/16c5e34b/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/DefaultNettyHttpBinding.java
----------------------------------------------------------------------
diff --git 
a/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/DefaultNettyHttpBinding.java
 
b/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/DefaultNettyHttpBinding.java
index fe87f53..e3a28f7 100644
--- 
a/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/DefaultNettyHttpBinding.java
+++ 
b/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/DefaultNettyHttpBinding.java
@@ -211,7 +211,13 @@ public class DefaultNettyHttpBinding implements 
NettyHttpBinding, Cloneable {
             String charset = "UTF-8";
 
             // Push POST form params into the headers to retain compatibility 
with DefaultHttpBinding
-            String body = request.content().toString(Charset.forName(charset));
+            String body = null;
+            ByteBuf buffer = request.content();
+            try {
+                body = buffer.toString(Charset.forName(charset));
+            } finally {
+                buffer.release();
+            }
             if (ObjectHelper.isNotEmpty(body)) {
                 for (String param : body.split("&")) {
                     String[] pair = param.split("=", 2);

http://git-wip-us.apache.org/repos/asf/camel/blob/16c5e34b/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/HttpClientInitializerFactory.java
----------------------------------------------------------------------
diff --git 
a/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/HttpClientInitializerFactory.java
 
b/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/HttpClientInitializerFactory.java
index 04bae3d..b5775a8 100644
--- 
a/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/HttpClientInitializerFactory.java
+++ 
b/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/HttpClientInitializerFactory.java
@@ -118,8 +118,6 @@ public class HttpClientInitializerFactory extends 
ClientInitializerFactory {
        
         // handler to route Camel messages
         pipeline.addLast("handler", new HttpClientChannelHandler(producer));
-
-        
     }
 
     private SSLContext createSSLContext(NettyProducer producer) throws 
Exception {

http://git-wip-us.apache.org/repos/asf/camel/blob/16c5e34b/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/HttpServerInitializerFactory.java
----------------------------------------------------------------------
diff --git 
a/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/HttpServerInitializerFactory.java
 
b/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/HttpServerInitializerFactory.java
index 84a34c2..4ed4322 100644
--- 
a/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/HttpServerInitializerFactory.java
+++ 
b/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/HttpServerInitializerFactory.java
@@ -97,8 +97,6 @@ public class HttpServerInitializerFactory extends 
ServerInitializerFactory {
             }
             pipeline.addLast("decoder-" + x, decoder);
         }
-        pipeline.addLast("aggregator", new 
HttpObjectAggregator(configuration.getChunkedMaxContentLength()));
-
         pipeline.addLast("encoder", new HttpResponseEncoder());
         List<ChannelHandler> encoders = 
consumer.getConfiguration().getEncoders();
         for (int x = 0; x < encoders.size(); x++) {
@@ -109,6 +107,7 @@ public class HttpServerInitializerFactory extends 
ServerInitializerFactory {
             }
             pipeline.addLast("encoder-" + x, encoder);
         }
+        pipeline.addLast("aggregator", new 
HttpObjectAggregator(configuration.getChunkedMaxContentLength()));
         if (supportCompressed()) {
             pipeline.addLast("deflater", new HttpContentCompressor());
         }

http://git-wip-us.apache.org/repos/asf/camel/blob/16c5e34b/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/HttpServerSharedInitializerFactory.java
----------------------------------------------------------------------
diff --git 
a/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/HttpServerSharedInitializerFactory.java
 
b/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/HttpServerSharedInitializerFactory.java
index 71e9129..698cd15 100644
--- 
a/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/HttpServerSharedInitializerFactory.java
+++ 
b/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/HttpServerSharedInitializerFactory.java
@@ -83,10 +83,10 @@ public class HttpServerSharedInitializerFactory extends 
HttpServerInitializerFac
         }
 
         pipeline.addLast("decoder", new HttpRequestDecoder(409, 
configuration.getMaxHeaderSize(), 8192));
+        pipeline.addLast("encoder", new HttpResponseEncoder());
         if (configuration.isChunked()) {
             pipeline.addLast("aggregator", new 
HttpObjectAggregator(configuration.getChunkedMaxContentLength()));
         }
-        pipeline.addLast("encoder", new HttpResponseEncoder());
         if (configuration.isCompression()) {
             pipeline.addLast("deflater", new HttpContentCompressor());
         }

http://git-wip-us.apache.org/repos/asf/camel/blob/16c5e34b/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/NettyChannelBufferStreamCacheOnCompletion.java
----------------------------------------------------------------------
diff --git 
a/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/NettyChannelBufferStreamCacheOnCompletion.java
 
b/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/NettyChannelBufferStreamCacheOnCompletion.java
index 343fd13..37654da 100644
--- 
a/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/NettyChannelBufferStreamCacheOnCompletion.java
+++ 
b/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/NettyChannelBufferStreamCacheOnCompletion.java
@@ -37,5 +37,4 @@ public class NettyChannelBufferStreamCacheOnCompletion 
extends SynchronizationAd
         cache.release();
     }
 
-
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/16c5e34b/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/NettyHttpProducer.java
----------------------------------------------------------------------
diff --git 
a/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/NettyHttpProducer.java
 
b/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/NettyHttpProducer.java
index 58344c2..ced0bdc 100644
--- 
a/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/NettyHttpProducer.java
+++ 
b/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/NettyHttpProducer.java
@@ -96,7 +96,6 @@ public class NettyHttpProducer extends NettyProducer {
                 if (nettyMessage != null) {
                     FullHttpResponse response = nettyMessage.getHttpResponse();
                     // Need to retain the ByteBuffer for producer to consumer
-                    // TODO Remove this part of ByteBuffer right away
                     if (response != null) {
                         response.content().retain();
                         // the actual url is stored on the IN message in the 
getRequestBody method as its accessed on-demand

http://git-wip-us.apache.org/repos/asf/camel/blob/16c5e34b/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/handlers/HttpServerChannelHandler.java
----------------------------------------------------------------------
diff --git 
a/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/handlers/HttpServerChannelHandler.java
 
b/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/handlers/HttpServerChannelHandler.java
index 39b2dda..d528675 100644
--- 
a/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/handlers/HttpServerChannelHandler.java
+++ 
b/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/handlers/HttpServerChannelHandler.java
@@ -21,7 +21,6 @@ import java.nio.channels.ClosedChannelException;
 import java.nio.charset.Charset;
 import java.util.Iterator;
 import java.util.Locale;
-
 import javax.security.auth.Subject;
 import javax.security.auth.login.LoginException;
 
@@ -43,9 +42,9 @@ import 
org.apache.camel.component.netty4.http.NettyHttpSecurityConfiguration;
 import org.apache.camel.component.netty4.http.SecurityAuthenticator;
 import org.apache.camel.util.CamelLogger;
 import org.apache.camel.util.ObjectHelper;
-
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+
 import static io.netty.handler.codec.http.HttpResponseStatus.BAD_REQUEST;
 import static 
io.netty.handler.codec.http.HttpResponseStatus.METHOD_NOT_ALLOWED;
 import static io.netty.handler.codec.http.HttpResponseStatus.OK;
@@ -247,13 +246,17 @@ public class HttpServerChannelHandler extends 
ServerChannelHandler {
                     // the decoded part is base64 encoded, so we need to 
decode that
                     ByteBuf buf = 
NettyConverter.toByteBuffer(decoded.getBytes());
                     ByteBuf out = Base64.decode(buf);
-                    String userAndPw = out.toString(Charset.defaultCharset());
-                    String username = ObjectHelper.before(userAndPw, ":");
-                    String password = ObjectHelper.after(userAndPw, ":");
-                    HttpPrincipal principal = new HttpPrincipal(username, 
password);
-
-                    LOG.debug("Extracted Basic Auth principal from HTTP 
header: {}", principal);
-                    return principal;
+                    try {
+                        String userAndPw = 
out.toString(Charset.defaultCharset());
+                        String username = ObjectHelper.before(userAndPw, ":");
+                        String password = ObjectHelper.after(userAndPw, ":");
+                        HttpPrincipal principal = new HttpPrincipal(username, 
password);
+                        LOG.debug("Extracted Basic Auth principal from HTTP 
header: {}", principal);
+                        return principal;
+                    } finally {
+                        buf.release();
+                        out.release();
+                    }
                 }
             }
         }

Reply via email to