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

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


The following commit(s) were added to refs/heads/main by this push:
     new 0448477324d CAMEL-20163: consolidate the code determining the response 
status (#12232)
0448477324d is described below

commit 0448477324da19e6c39bc42084a81dd033ee2536
Author: Otavio Rodolfo Piske <[email protected]>
AuthorDate: Tue Nov 28 13:55:39 2023 +0100

    CAMEL-20163: consolidate the code determining the response status (#12232)
---
 .../camel/http/common/DefaultHttpBinding.java      | 26 +----------
 .../netty/http/DefaultNettyHttpBinding.java        | 22 +--------
 .../http/vertx/VertxPlatformHttpSupport.java       | 23 +--------
 .../undertow/DefaultUndertowHttpBinding.java       | 22 +--------
 .../org/apache/camel/support/http/HttpUtil.java    | 54 ++++++++++++++++++++++
 5 files changed, 60 insertions(+), 87 deletions(-)

diff --git 
a/components/camel-http-common/src/main/java/org/apache/camel/http/common/DefaultHttpBinding.java
 
b/components/camel-http-common/src/main/java/org/apache/camel/http/common/DefaultHttpBinding.java
index 73a4549b05b..4f37622f612 100644
--- 
a/components/camel-http-common/src/main/java/org/apache/camel/http/common/DefaultHttpBinding.java
+++ 
b/components/camel-http-common/src/main/java/org/apache/camel/http/common/DefaultHttpBinding.java
@@ -60,6 +60,8 @@ import org.apache.camel.util.IOHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import static org.apache.camel.support.http.HttpUtil.determineResponseCode;
+
 /**
  * Binding between {@link HttpMessage} and {@link HttpServletResponse}.
  * <p/>
@@ -451,30 +453,6 @@ public class DefaultHttpBinding implements HttpBinding {
         }
     }
 
-    /*
-     * set the HTTP status code
-     * NOTE: this is similar to the Netty-Http and Undertow approach
-     * TODO: we may want to refactor this class so that
-     * the status code is determined in one place
-     */
-    private int determineResponseCode(Exchange camelExchange, Object body) {
-        boolean failed = camelExchange.isFailed();
-        int defaultCode = failed ? 500 : 200;
-
-        Message message = camelExchange.getMessage();
-        Integer currentCode = message.getHeader(Exchange.HTTP_RESPONSE_CODE, 
Integer.class);
-        int codeToUse = currentCode == null ? defaultCode : currentCode;
-
-        if (codeToUse != 500) {
-            if (body == null || body instanceof String && ((String) 
body).isBlank()) {
-                // no content
-                codeToUse = currentCode == null ? 204 : currentCode;
-            }
-        }
-
-        return codeToUse;
-    }
-
     protected String convertHeaderValueToString(Exchange exchange, Object 
headerValue) {
         if ((headerValue instanceof Date || headerValue instanceof Locale)
                 && convertDateAndLocaleLocally(exchange)) {
diff --git 
a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java
 
b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java
index 6f6a1da30d3..5d29fc06242 100644
--- 
a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java
+++ 
b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java
@@ -63,6 +63,7 @@ import org.slf4j.LoggerFactory;
 
 import static io.netty.handler.codec.http.HttpHeaderNames.TRANSFER_ENCODING;
 import static io.netty.handler.codec.http.HttpHeaderValues.CHUNKED;
+import static org.apache.camel.support.http.HttpUtil.determineResponseCode;
 
 /**
  * Default {@link NettyHttpBinding}.
@@ -560,27 +561,6 @@ public class DefaultNettyHttpBinding implements 
NettyHttpBinding, Cloneable {
         return response;
     }
 
-    /*
-     * set the HTTP status code
-     */
-    private int determineResponseCode(Exchange camelExchange, Object body) {
-        boolean failed = camelExchange.isFailed();
-        int defaultCode = failed ? 500 : 200;
-
-        Message message = camelExchange.getMessage();
-        Integer currentCode = 
message.getHeader(NettyHttpConstants.HTTP_RESPONSE_CODE, Integer.class);
-        int codeToUse = currentCode == null ? defaultCode : currentCode;
-
-        if (codeToUse != 500) {
-            if (body == null || body instanceof String && ((String) 
body).isBlank()) {
-                // no content
-                codeToUse = currentCode == null ? 204 : currentCode;
-            }
-        }
-
-        return codeToUse;
-    }
-
     @Override
     public HttpRequest toNettyRequest(Message message, String fullUri, 
NettyHttpConfiguration configuration) throws Exception {
         LOG.trace("toNettyRequest: {}", message);
diff --git 
a/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpSupport.java
 
b/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpSupport.java
index 6056903b082..52e2f3d8eca 100644
--- 
a/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpSupport.java
+++ 
b/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpSupport.java
@@ -49,6 +49,8 @@ import org.apache.camel.support.ObjectHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import static org.apache.camel.support.http.HttpUtil.determineResponseCode;
+
 /*
  * Supporting class for the platform-http-vertx component.
  *
@@ -177,28 +179,7 @@ public final class VertxPlatformHttpSupport {
         return null;
     }
 
-    /*
-     * Copied from 
org.apache.camel.http.common.DefaultHttpBinding.determineResponseCode(Exchange, 
Object)
-     * If DefaultHttpBinding.determineResponseCode(Exchange, Object) is moved 
to a module without the servlet-api
-     * dependency we could eventually consume it from there.
-     */
-    static int determineResponseCode(Exchange camelExchange, Object body) {
-        boolean failed = camelExchange.isFailed();
-        int defaultCode = failed ? 500 : 200;
-
-        Message message = camelExchange.getMessage();
-        Integer currentCode = message.getHeader(Exchange.HTTP_RESPONSE_CODE, 
Integer.class);
-        int codeToUse = currentCode == null ? defaultCode : currentCode;
-
-        if (codeToUse != 500) {
-            if (body == null || body instanceof String && ((String) 
body).isBlank()) {
-                // no content
-                codeToUse = currentCode == null ? 204 : currentCode;
-            }
-        }
 
-        return codeToUse;
-    }
 
     static Future<Void> writeResponse(
             RoutingContext ctx, Exchange camelExchange, HeaderFilterStrategy 
headerFilterStrategy, boolean muteExceptions) {
diff --git 
a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/DefaultUndertowHttpBinding.java
 
b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/DefaultUndertowHttpBinding.java
index 18fa2048665..ae9eeb0bbc2 100644
--- 
a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/DefaultUndertowHttpBinding.java
+++ 
b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/DefaultUndertowHttpBinding.java
@@ -61,6 +61,7 @@ import org.xnio.channels.BlockingReadableByteChannel;
 import org.xnio.channels.StreamSourceChannel;
 import org.xnio.streams.ChannelInputStream;
 
+import static org.apache.camel.support.http.HttpUtil.determineResponseCode;
 import static org.apache.camel.util.BufferCaster.cast;
 
 /**
@@ -393,27 +394,6 @@ public class DefaultUndertowHttpBinding implements 
UndertowHttpBinding {
         return body;
     }
 
-    /*
-     * set the HTTP status code
-     */
-    private int determineResponseCode(Exchange camelExchange, Object body) {
-        boolean failed = camelExchange.isFailed();
-        int defaultCode = failed ? 500 : 200;
-
-        Message message = camelExchange.getMessage();
-        Integer currentCode = 
message.getHeader(UndertowConstants.HTTP_RESPONSE_CODE, Integer.class);
-        int codeToUse = currentCode == null ? defaultCode : currentCode;
-
-        if (codeToUse != 500) {
-            if (body == null || body instanceof String && ((String) 
body).isBlank()) {
-                // no content
-                codeToUse = currentCode == null ? 204 : currentCode;
-            }
-        }
-
-        return codeToUse;
-    }
-
     @Override
     public Object toHttpRequest(ClientRequest clientRequest, Message message) {
 
diff --git 
a/core/camel-support/src/main/java/org/apache/camel/support/http/HttpUtil.java 
b/core/camel-support/src/main/java/org/apache/camel/support/http/HttpUtil.java
new file mode 100644
index 00000000000..f2d9b256da0
--- /dev/null
+++ 
b/core/camel-support/src/main/java/org/apache/camel/support/http/HttpUtil.java
@@ -0,0 +1,54 @@
+/*
+ * 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.support.http;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+
+public final class HttpUtil {
+    private static final int INTERNAL_SERVER_ERROR = 500;
+    private static final int OK = 200;
+    private static final int NO_CONTENT = 204;
+
+    private HttpUtil() {
+    }
+
+    /**
+     * Given an exchange handling HTTP, determines the status response code to 
return for the caller
+     * @param camelExchange the exchange to evaluate
+     * @param body an optional payload (i.e.: the message body) carrying a 
response code
+     * @return An integer value with the response code
+     */
+    public static int determineResponseCode(Exchange camelExchange, Object 
body) {
+        boolean failed = camelExchange.isFailed();
+        int defaultCode = failed ? INTERNAL_SERVER_ERROR : OK;
+
+        Message message = camelExchange.getMessage();
+        Integer currentCode = message.getHeader(Exchange.HTTP_RESPONSE_CODE, 
Integer.class);
+        int codeToUse = currentCode == null ? defaultCode : currentCode;
+
+        if (codeToUse != INTERNAL_SERVER_ERROR) {
+            if (body == null || body instanceof String && ((String) 
body).isBlank()) {
+                // no content
+                codeToUse = currentCode == null ? NO_CONTENT : currentCode;
+            }
+        }
+
+        return codeToUse;
+    }
+}

Reply via email to