You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2021/02/16 06:40:37 UTC

[camel] 03/04: CAMEL-16215: camel-netty-http - Optimize isStatusCodeOk

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

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

commit 137a34c04271a04af30b82b089f2139ec0c42829
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue Feb 16 07:35:46 2021 +0100

    CAMEL-16215: camel-netty-http - Optimize isStatusCodeOk
---
 .../component/netty/http/NettyHttpHelper.java      |  6 +++---
 .../component/netty/http/NettyHttpProducer.java    | 25 ++++++++++++++++++++--
 2 files changed, 26 insertions(+), 5 deletions(-)

diff --git a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpHelper.java b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpHelper.java
index 8e21e42..7a82272 100644
--- a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpHelper.java
+++ b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpHelper.java
@@ -280,11 +280,11 @@ public final class NettyHttpHelper {
         for (String range : ranges) {
             boolean ok;
             if (range.contains("-")) {
-                int from = Integer.valueOf(StringHelper.before(range, "-"));
-                int to = Integer.valueOf(StringHelper.after(range, "-"));
+                int from = Integer.parseInt(StringHelper.before(range, "-"));
+                int to = Integer.parseInt(StringHelper.after(range, "-"));
                 ok = statusCode >= from && statusCode <= to;
             } else {
-                int exact = Integer.valueOf(range);
+                int exact = Integer.parseInt(range);
                 ok = exact == statusCode;
             }
             if (ok) {
diff --git a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpProducer.java b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpProducer.java
index d1f27ab..eec30b1 100644
--- a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpProducer.java
+++ b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpProducer.java
@@ -32,6 +32,7 @@ import org.apache.camel.component.netty.NettyConstants;
 import org.apache.camel.component.netty.NettyProducer;
 import org.apache.camel.http.base.cookie.CookieHandler;
 import org.apache.camel.support.SynchronizationAdapter;
+import org.apache.camel.util.StringHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -42,11 +43,26 @@ public class NettyHttpProducer extends NettyProducer {
 
     private static final Logger LOG = LoggerFactory.getLogger(NettyHttpProducer.class);
 
+    private int minOkRange;
+    private int maxOkRange;
+
     public NettyHttpProducer(NettyHttpEndpoint nettyEndpoint, NettyConfiguration configuration) {
         super(nettyEndpoint, configuration);
     }
 
     @Override
+    protected void doInit() throws Exception {
+        super.doInit();
+
+        String range = getEndpoint().getConfiguration().getOkStatusCodeRange();
+        if (!range.contains(",")) {
+            // default is 200-299 so lets optimize for this
+            minOkRange = Integer.parseInt(StringHelper.before(range, "-"));
+            maxOkRange = Integer.parseInt(StringHelper.after(range, "-"));
+        }
+    }
+
+    @Override
     public NettyHttpEndpoint getEndpoint() {
         return (NettyHttpEndpoint) super.getEndpoint();
     }
@@ -127,7 +143,7 @@ public class NettyHttpProducer extends NettyProducer {
                                 @Override
                                 public void onDone(Exchange exchange) {
                                     if (response.refCnt() > 0) {
-                                        LOG.debug("Releasing Netty HttpResonse ByteBuf");
+                                        LOG.debug("Releasing Netty HttpResponse ByteBuf");
                                         ReferenceCountUtil.release(response);
                                     }
                                 }
@@ -139,7 +155,12 @@ public class NettyHttpProducer extends NettyProducer {
                             LOG.debug("Http responseCode: {}", code);
 
                             // if there was a http error code then check if we should throw an exception
-                            boolean ok = NettyHttpHelper.isStatusCodeOk(code, configuration.getOkStatusCodeRange());
+                            boolean ok;
+                            if (minOkRange > 0) {
+                                ok = code >= minOkRange && code <= maxOkRange;
+                            } else {
+                                ok = NettyHttpHelper.isStatusCodeOk(code, configuration.getOkStatusCodeRange());
+                            }
                             if (!ok && getConfiguration().isThrowExceptionOnFailure()) {
                                 // operation failed so populate exception to throw
                                 Exception cause = NettyHttpHelper.populateNettyHttpOperationFailedException(exchange, actualUrl,