camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject camel git commit: CAMEL-9078: HTTP producers allow to configure the HTTP status code range for ok.
Date Thu, 13 Aug 2015 08:42:36 GMT
Repository: camel
Updated Branches:
  refs/heads/master 0a77a10e4 -> e0436334f


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/e0436334
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/e0436334
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/e0436334

Branch: refs/heads/master
Commit: e0436334f20e2965ed087cfcde9094598c64344a
Parents: 0a77a10
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Thu Aug 13 10:50:23 2015 +0200
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Thu Aug 13 10:50:23 2015 +0200

----------------------------------------------------------------------
 .../camel/http/common/HttpCommonEndpoint.java   | 19 ++++--
 .../apache/camel/http/common/HttpHelper.java    | 13 ++++
 .../camel/component/http/HttpProducer.java      |  3 +-
 .../camel/component/http4/HttpProducer.java     | 14 ++++-
 .../jetty/DefaultJettyHttpBinding.java          | 12 +++-
 .../camel/component/jetty/JettyHttpBinding.java | 20 ++++++-
 .../component/jetty/JettyHttpEndpoint.java      |  1 +
 .../HttpProducerOkStatusCodeRangeTest.java      | 62 ++++++++++++++++++++
 8 files changed, 132 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/e0436334/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpCommonEndpoint.java
----------------------------------------------------------------------
diff --git a/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpCommonEndpoint.java
b/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpCommonEndpoint.java
index 4ba2163..c233e1f 100644
--- a/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpCommonEndpoint.java
+++ b/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpCommonEndpoint.java
@@ -25,15 +25,11 @@ import org.apache.camel.spi.HeaderFilterStrategyAware;
 import org.apache.camel.spi.Metadata;
 import org.apache.camel.spi.UriParam;
 import org.apache.camel.spi.UriPath;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 public abstract class HttpCommonEndpoint extends DefaultEndpoint implements HeaderFilterStrategyAware
{
 
     // Note: all options must be documented with description in annotations so extended components
can access the documentation
 
-    private static final Logger LOG = LoggerFactory.getLogger(HttpCommonEndpoint.class);
-
     HttpCommonComponent component;
     UrlRewrite urlRewrite;
 
@@ -96,6 +92,9 @@ public abstract class HttpCommonEndpoint extends DefaultEndpoint implements
Head
             description = "Whether to eager check whether the HTTP requests has content if
the content-length header is 0 or not present."
                     + " This can be turned on in case HTTP clients do not send streamed data.")
     boolean eagerCheckContentAvailable;
+    @UriParam(label = "producer", defaultValue = "200-299",
+            description = "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.")
+    private String okStatusCodeRange = "200-299";
 
     public HttpCommonEndpoint() {
     }
@@ -380,4 +379,16 @@ public abstract class HttpCommonEndpoint extends DefaultEndpoint implements
Head
         this.eagerCheckContentAvailable = eagerCheckContentAvailable;
     }
 
+    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/e0436334/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpHelper.java
----------------------------------------------------------------------
diff --git a/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpHelper.java
b/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpHelper.java
index fad8889..20e949d 100644
--- a/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpHelper.java
+++ b/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpHelper.java
@@ -504,4 +504,17 @@ public final class HttpHelper {
         return answer;
     }
 
+    /**
+     * 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/e0436334/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java
----------------------------------------------------------------------
diff --git a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java
b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java
index 45fc4a3..cbbd97c 100644
--- a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java
+++ b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java
@@ -152,7 +152,8 @@ public class HttpProducer extends DefaultProducer {
                 // if we do not use failed exception then populate response for all response
codes
                 populateResponse(exchange, method, in, strategy, responseCode);
             } else {
-                if (responseCode >= 100 && responseCode < 300) {
+                boolean ok = HttpHelper.isStatusCodeOk(responseCode, getEndpoint().getOkStatusCodeRange());
+                if (ok) {
                     // only populate response for OK response
                     populateResponse(exchange, method, in, strategy, responseCode);
                 } else {

http://git-wip-us.apache.org/repos/asf/camel/blob/e0436334/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpProducer.java
----------------------------------------------------------------------
diff --git a/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpProducer.java
b/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpProducer.java
index 026754c..9a22fbf 100644
--- a/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpProducer.java
+++ b/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpProducer.java
@@ -165,10 +165,18 @@ public class HttpProducer extends DefaultProducer {
             int responseCode = httpResponse.getStatusLine().getStatusCode();
             LOG.debug("Http responseCode: {}", responseCode);
 
-            if (throwException && (responseCode < 100 || responseCode >= 300))
{
-                throw populateHttpOperationFailedException(exchange, httpRequest, httpResponse,
responseCode);
-            } else {
+            if (!throwException) {
+                // if we do not use failed exception then populate response for all response
codes
                 populateResponse(exchange, httpRequest, httpResponse, in, strategy, responseCode);
+            } else {
+                boolean ok = HttpHelper.isStatusCodeOk(responseCode, getEndpoint().getOkStatusCodeRange());
+                if (ok) {
+                    // only populate response for OK response
+                    populateResponse(exchange, httpRequest, httpResponse, in, strategy, responseCode);
+                } else {
+                    // operation failed so populate exception to throw
+                    throw populateHttpOperationFailedException(exchange, httpRequest, httpResponse,
responseCode);
+                }
             }
         } finally {
             if (httpResponse != null) {

http://git-wip-us.apache.org/repos/asf/camel/blob/e0436334/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/DefaultJettyHttpBinding.java
----------------------------------------------------------------------
diff --git a/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/DefaultJettyHttpBinding.java
b/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/DefaultJettyHttpBinding.java
index 47a42d8..8e8cb2c 100644
--- a/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/DefaultJettyHttpBinding.java
+++ b/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/DefaultJettyHttpBinding.java
@@ -46,6 +46,7 @@ public class DefaultJettyHttpBinding implements JettyHttpBinding {
     private HeaderFilterStrategy httpProtocolHeaderFilterStrategy = new HttpProtocolHeaderFilterStrategy();
     private boolean throwExceptionOnFailure;
     private boolean transferException;
+    private String okStatusCodeRange;
 
     public DefaultJettyHttpBinding() {
     }
@@ -60,7 +61,8 @@ public class DefaultJettyHttpBinding implements JettyHttpBinding {
             // if we do not use failed exception then populate response for all response
codes
             populateResponse(exchange, httpExchange, in, getHeaderFilterStrategy(), responseCode);
         } else {
-            if (responseCode >= 100 && responseCode < 300) {
+            boolean ok = HttpHelper.isStatusCodeOk(responseCode, okStatusCodeRange);
+            if (ok) {
                 // only populate response for OK response
                 populateResponse(exchange, httpExchange, in, getHeaderFilterStrategy(), responseCode);
             } else {
@@ -99,6 +101,14 @@ public class DefaultJettyHttpBinding implements JettyHttpBinding {
         this.transferException = transferException;
     }
 
+    public String getOkStatusCodeRange() {
+        return okStatusCodeRange;
+    }
+
+    public void setOkStatusCodeRange(String okStatusCodeRange) {
+        this.okStatusCodeRange = okStatusCodeRange;
+    }
+
     protected void populateResponse(Exchange exchange, JettyContentExchange httpExchange,
                                     Message in, HeaderFilterStrategy strategy, int responseCode)
throws IOException {
         Message answer = exchange.getOut();

http://git-wip-us.apache.org/repos/asf/camel/blob/e0436334/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/JettyHttpBinding.java
----------------------------------------------------------------------
diff --git a/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/JettyHttpBinding.java
b/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/JettyHttpBinding.java
index 795fbf2..ec3d006 100644
--- a/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/JettyHttpBinding.java
+++ b/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/JettyHttpBinding.java
@@ -29,8 +29,8 @@ public interface JettyHttpBinding {
     /**
      * Parses the response from the Jetty client.
      *
-     * @param exchange  the Exchange which to populate with the response
-     * @param httpExchange  the response from the Jetty client
+     * @param exchange     the Exchange which to populate with the response
+     * @param httpExchange the response from the Jetty client
      * @throws Exception is thrown if error parsing response
      */
     void populateResponse(Exchange exchange, JettyContentExchange httpExchange) throws Exception;
@@ -83,4 +83,18 @@ public interface JettyHttpBinding {
      */
     boolean isTransferException();
 
-}
+    /**
+     * 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>
+     */
+    String getOkStatusCodeRange();
+
+    /**
+     * 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>
+     */
+    void setOkStatusCodeRange(String okStatusCodeRange);
+
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/e0436334/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/JettyHttpEndpoint.java
----------------------------------------------------------------------
diff --git a/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/JettyHttpEndpoint.java
b/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/JettyHttpEndpoint.java
index 994c7fa..9ba1c6b 100644
--- a/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/JettyHttpEndpoint.java
+++ b/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/JettyHttpEndpoint.java
@@ -192,6 +192,7 @@ public abstract class JettyHttpEndpoint extends HttpCommonEndpoint {
             jettyBinding.setHeaderFilterStrategy(getHeaderFilterStrategy());
             jettyBinding.setThrowExceptionOnFailure(isThrowExceptionOnFailure());
             jettyBinding.setTransferException(isTransferException());
+            jettyBinding.setOkStatusCodeRange(getOkStatusCodeRange());
         }
         return jettyBinding;
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/e0436334/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpProducerOkStatusCodeRangeTest.java
----------------------------------------------------------------------
diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpProducerOkStatusCodeRangeTest.java
b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpProducerOkStatusCodeRangeTest.java
new file mode 100644
index 0000000..2d81db7
--- /dev/null
+++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpProducerOkStatusCodeRangeTest.java
@@ -0,0 +1,62 @@
+/**
+ * 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.jetty;
+
+import org.apache.camel.CamelExecutionException;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.http.common.HttpOperationFailedException;
+import org.junit.Test;
+
+/**
+ * @version 
+ */
+public class HttpProducerOkStatusCodeRangeTest extends BaseJettyTest {
+
+    @Test
+    public void testNoOk() throws Exception {
+        byte[] data = "Hello World".getBytes();
+        try {
+            template.requestBody("http://localhost:{{port}}/test?okStatusCodeRange=200-200",
data, String.class);
+            fail("Should have thrown exception");
+        } catch (CamelExecutionException e) {
+            HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class,
e.getCause());
+            assertEquals(209, cause.getStatusCode());
+            assertEquals("Not allowed", cause.getResponseBody());
+        }
+    }
+
+    @Test
+    public void testOk() throws Exception {
+        byte[] data = "Hello World".getBytes();
+        String out = template.requestBody("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("jetty://http://localhost:{{port}}/test")
+                    .setHeader(Exchange.HTTP_RESPONSE_CODE, constant(209))
+                    .transform(constant("Not allowed"));
+            }
+        };
+    }
+
+}


Mime
View raw message