Repository: camel
Updated Branches:
  refs/heads/master 1fab7c5ae -> b72dcf075


CAMEL-9078: HTTP producers allow to configure the HTTP status code range for ok.


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

Branch: refs/heads/master
Commit: b72dcf07581671d9c5bf3333fcbec7de310641b4
Parents: 1fab7c5
Author: Claus Ibsen <[email protected]>
Authored: Thu Aug 13 14:55:13 2015 +0200
Committer: Claus Ibsen <[email protected]>
Committed: Thu Aug 13 14:56:30 2015 +0200

----------------------------------------------------------------------
 .../netty/http/NettyHttpConfiguration.java      | 15 +++++
 .../component/netty/http/NettyHttpHelper.java   | 13 +++++
 .../component/netty/http/NettyHttpProducer.java | 11 ++--
 .../netty/http/NettyHttpOkStatusCodeTest.java   | 59 ++++++++++++++++++++
 .../netty4/http/NettyHttpConfiguration.java     | 15 +++++
 .../component/netty4/http/NettyHttpHelper.java  | 13 +++++
 .../netty4/http/NettyHttpProducer.java          | 11 ++--
 .../netty4/http/NettyHttpOkStatusCodeTest.java  | 59 ++++++++++++++++++++
 8 files changed, 188 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/b72dcf07/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConfiguration.java
----------------------------------------------------------------------
diff --git 
a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConfiguration.java
 
b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConfiguration.java
index 4e103d7..c3fe872 100644
--- 
a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConfiguration.java
+++ 
b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConfiguration.java
@@ -58,6 +58,8 @@ public class NettyHttpConfiguration extends 
NettyConfiguration {
     private int chunkedMaxContentLength = 1024 * 1024;
     @UriParam(label = "consumer", defaultValue = "8192")
     private int maxHeaderSize = 8192;
+    @UriParam(label = "producer", defaultValue = "200-299")
+    private String okStatusCodeRange = "200-299";
 
     public NettyHttpConfiguration() {
         // we need sync=true as http is request/reply by nature
@@ -245,4 +247,17 @@ public class NettyHttpConfiguration extends 
NettyConfiguration {
         throw new UnsupportedOperationException("You cannot 
setAllowDefaultCodec here.");
     }
 
+    public String getOkStatusCodeRange() {
+        return okStatusCodeRange;
+    }
+
+    /**
+     * The status codes which is considered a success response. The values are 
inclusive. The range must be defined as from-to with the dash included.
+     * <p/>
+     * The default range is <tt>200-299</tt>
+     */
+    public void setOkStatusCodeRange(String okStatusCodeRange) {
+        this.okStatusCodeRange = okStatusCodeRange;
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/b72dcf07/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpHelper.java
----------------------------------------------------------------------
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 376ec27..e6667ea 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
@@ -240,4 +240,17 @@ public final class NettyHttpHelper {
         return uri;
     }
 
+    /**
+     * Checks whether the given http status code is within the ok range
+     *
+     * @param statusCode the status code
+     * @param okStatusCodeRange the ok range (inclusive)
+     * @return <tt>true</tt> if ok, <tt>false</tt> otherwise
+     */
+    public static boolean isStatusCodeOk(int statusCode, String 
okStatusCodeRange) {
+        int from = Integer.valueOf(ObjectHelper.before(okStatusCodeRange, 
"-"));
+        int to = Integer.valueOf(ObjectHelper.after(okStatusCodeRange, "-"));
+        return statusCode >= from && statusCode <= to;
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/b72dcf07/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpProducer.java
----------------------------------------------------------------------
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 857c875..b0976bc 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
@@ -48,7 +48,7 @@ public class NettyHttpProducer extends NettyProducer {
 
     @Override
     public boolean process(Exchange exchange, AsyncCallback callback) {
-        return super.process(exchange, new NettyHttpProducerCallback(exchange, 
callback));
+        return super.process(exchange, new NettyHttpProducerCallback(exchange, 
callback, getConfiguration()));
     }
 
     @Override
@@ -80,10 +80,12 @@ public class NettyHttpProducer extends NettyProducer {
 
         private final Exchange exchange;
         private final AsyncCallback callback;
+        private final NettyHttpConfiguration configuration;
 
-        private NettyHttpProducerCallback(Exchange exchange, AsyncCallback 
callback) {
+        private NettyHttpProducerCallback(Exchange exchange, AsyncCallback 
callback, NettyHttpConfiguration configuration) {
             this.exchange = exchange;
             this.callback = callback;
+            this.configuration = configuration;
         }
 
         @Override
@@ -98,8 +100,9 @@ public class NettyHttpProducer extends NettyProducer {
                         int code = response.getStatus() != null ? 
response.getStatus().getCode() : -1;
                         log.debug("Http responseCode: {}", code);
 
-                        // if there was a http error code (300 or higher) then 
check if we should throw an exception
-                        if (code >= 300 && 
getConfiguration().isThrowExceptionOnFailure()) {
+                        // if there was a http error code then check if we 
should throw an exception
+                        boolean ok = NettyHttpHelper.isStatusCodeOk(code, 
configuration.getOkStatusCodeRange());
+                        if (!ok && 
getConfiguration().isThrowExceptionOnFailure()) {
                             // operation failed so populate exception to throw
                             Exception cause = 
NettyHttpHelper.populateNettyHttpOperationFailedException(exchange, actualUrl, 
response, code, getConfiguration().isTransferException());
                             exchange.setException(cause);

http://git-wip-us.apache.org/repos/asf/camel/blob/b72dcf07/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpOkStatusCodeTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpOkStatusCodeTest.java
 
b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpOkStatusCodeTest.java
new file mode 100644
index 0000000..d474651
--- /dev/null
+++ 
b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpOkStatusCodeTest.java
@@ -0,0 +1,59 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.netty.http;
+
+import org.apache.camel.CamelExecutionException;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.Test;
+
+public class NettyHttpOkStatusCodeTest extends BaseNettyTest {
+
+    @Test
+    public void testNoOk() throws Exception {
+        byte[] data = "Hello World".getBytes();
+        try {
+            
template.requestBody("netty-http:http://localhost:{{port}}/test?okStatusCodeRange=200-200";,
 data, String.class);
+            fail("Should have thrown exception");
+        } catch (CamelExecutionException e) {
+            NettyHttpOperationFailedException cause = 
assertIsInstanceOf(NettyHttpOperationFailedException.class, e.getCause());
+            assertEquals(209, cause.getStatusCode());
+            String body = context.getTypeConverter().convertTo(String.class, 
cause.getResponse().getContent());
+            assertEquals("Not allowed", body);
+        }
+    }
+
+    @Test
+    public void testOk() throws Exception {
+        byte[] data = "Hello World".getBytes();
+        String out = 
template.requestBody("netty-http:http://localhost:{{port}}/test?okStatusCodeRange=200-209";,
 data, String.class);
+        assertEquals("Not allowed", out);
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("netty-http:http://0.0.0.0:{{port}}/test";)
+                    .setHeader(Exchange.HTTP_RESPONSE_CODE, constant(209))
+                    .transform().constant("Not allowed");
+            }
+        };
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/b72dcf07/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/NettyHttpConfiguration.java
----------------------------------------------------------------------
diff --git 
a/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/NettyHttpConfiguration.java
 
b/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/NettyHttpConfiguration.java
index ff80318..eef2399 100644
--- 
a/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/NettyHttpConfiguration.java
+++ 
b/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/NettyHttpConfiguration.java
@@ -57,6 +57,8 @@ public class NettyHttpConfiguration extends 
NettyConfiguration {
     private int chunkedMaxContentLength = 1024 * 1024;
     @UriParam(label = "consumer", defaultValue = "8192")
     private int maxHeaderSize = 8192;
+    @UriParam(label = "producer", defaultValue = "200-299")
+    private String okStatusCodeRange = "200-299";
 
     public NettyHttpConfiguration() {
         // we need sync=true as http is request/reply by nature
@@ -244,4 +246,17 @@ public class NettyHttpConfiguration extends 
NettyConfiguration {
         throw new UnsupportedOperationException("You cannot 
setAllowDefaultCodec here.");
     }
 
+    public String getOkStatusCodeRange() {
+        return okStatusCodeRange;
+    }
+
+    /**
+     * The status codes which is considered a success response. The values are 
inclusive. The range must be defined as from-to with the dash included.
+     * <p/>
+     * The default range is <tt>200-299</tt>
+     */
+    public void setOkStatusCodeRange(String okStatusCodeRange) {
+        this.okStatusCodeRange = okStatusCodeRange;
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/b72dcf07/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/NettyHttpHelper.java
----------------------------------------------------------------------
diff --git 
a/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/NettyHttpHelper.java
 
b/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/NettyHttpHelper.java
index 7435bb5..86e23a9 100644
--- 
a/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/NettyHttpHelper.java
+++ 
b/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/NettyHttpHelper.java
@@ -240,4 +240,17 @@ public final class NettyHttpHelper {
         return uri;
     }
 
+    /**
+     * Checks whether the given http status code is within the ok range
+     *
+     * @param statusCode the status code
+     * @param okStatusCodeRange the ok range (inclusive)
+     * @return <tt>true</tt> if ok, <tt>false</tt> otherwise
+     */
+    public static boolean isStatusCodeOk(int statusCode, String 
okStatusCodeRange) {
+        int from = Integer.valueOf(ObjectHelper.before(okStatusCodeRange, 
"-"));
+        int to = Integer.valueOf(ObjectHelper.after(okStatusCodeRange, "-"));
+        return statusCode >= from && statusCode <= to;
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/b72dcf07/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 f88b0b2..58344c2 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
@@ -49,7 +49,7 @@ public class NettyHttpProducer extends NettyProducer {
 
     @Override
     public boolean process(Exchange exchange, AsyncCallback callback) {
-        return super.process(exchange, new NettyHttpProducerCallback(exchange, 
callback));
+        return super.process(exchange, new NettyHttpProducerCallback(exchange, 
callback, getConfiguration()));
     }
 
     @Override
@@ -81,10 +81,12 @@ public class NettyHttpProducer extends NettyProducer {
 
         private final Exchange exchange;
         private final AsyncCallback callback;
+        private final NettyHttpConfiguration configuration;
 
-        private NettyHttpProducerCallback(Exchange exchange, AsyncCallback 
callback) {
+        private NettyHttpProducerCallback(Exchange exchange, AsyncCallback 
callback, NettyHttpConfiguration configuration) {
             this.exchange = exchange;
             this.callback = callback;
+            this.configuration = configuration;
         }
 
         @Override
@@ -102,8 +104,9 @@ public class NettyHttpProducer extends NettyProducer {
                         int code = response.getStatus() != null ? 
response.getStatus().code() : -1;
                         log.debug("Http responseCode: {}", code);
 
-                        // if there was a http error code (300 or higher) then 
check if we should throw an exception
-                        if (code >= 300 && 
getConfiguration().isThrowExceptionOnFailure()) {
+                        // if there was a http error code then check if we 
should throw an exception
+                        boolean ok = NettyHttpHelper.isStatusCodeOk(code, 
configuration.getOkStatusCodeRange());
+                        if (!ok && 
getConfiguration().isThrowExceptionOnFailure()) {
                             // operation failed so populate exception to throw
                             Exception cause = 
NettyHttpHelper.populateNettyHttpOperationFailedException(exchange, actualUrl, 
response, code, getConfiguration().isTransferException());
                             exchange.setException(cause);

http://git-wip-us.apache.org/repos/asf/camel/blob/b72dcf07/components/camel-netty4-http/src/test/java/org/apache/camel/component/netty4/http/NettyHttpOkStatusCodeTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-netty4-http/src/test/java/org/apache/camel/component/netty4/http/NettyHttpOkStatusCodeTest.java
 
b/components/camel-netty4-http/src/test/java/org/apache/camel/component/netty4/http/NettyHttpOkStatusCodeTest.java
new file mode 100644
index 0000000..c48c1f7
--- /dev/null
+++ 
b/components/camel-netty4-http/src/test/java/org/apache/camel/component/netty4/http/NettyHttpOkStatusCodeTest.java
@@ -0,0 +1,59 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.netty4.http;
+
+import org.apache.camel.CamelExecutionException;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.Test;
+
+public class NettyHttpOkStatusCodeTest extends BaseNettyTest {
+
+    @Test
+    public void testNoOk() throws Exception {
+        byte[] data = "Hello World".getBytes();
+        try {
+            
template.requestBody("netty4-http:http://localhost:{{port}}/test?okStatusCodeRange=200-200";,
 data, String.class);
+            fail("Should have thrown exception");
+        } catch (CamelExecutionException e) {
+            NettyHttpOperationFailedException cause = 
assertIsInstanceOf(NettyHttpOperationFailedException.class, e.getCause());
+            assertEquals(209, cause.getStatusCode());
+            String body = context.getTypeConverter().convertTo(String.class, 
cause.getHttpContent().content());
+            assertEquals("Not allowed", body);
+        }
+    }
+
+    @Test
+    public void testOk() throws Exception {
+        byte[] data = "Hello World".getBytes();
+        String out = 
template.requestBody("netty4-http:http://localhost:{{port}}/test?okStatusCodeRange=200-209";,
 data, String.class);
+        assertEquals("Not allowed", out);
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("netty4-http:http://0.0.0.0:{{port}}/test";)
+                        .setHeader(Exchange.HTTP_RESPONSE_CODE, constant(209))
+                        .transform().constant("Not allowed");
+            }
+        };
+    }
+
+}

Reply via email to