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

davsclaus 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 127943437b3 CAMEL-22121: rest-dsl: Add client response validator
127943437b3 is described below

commit 127943437b3ff10ed8bcfa1a2aa1c6f918bbc19a
Author: Claus Ibsen <[email protected]>
AuthorDate: Wed Jun 18 11:17:31 2025 +0200

    CAMEL-22121: rest-dsl: Add client response validator
---
 .../DefaultRestOpenapiProcessorStrategy.java       | 25 +++++++--
 .../camel/support/processor/RestBindingAdvice.java | 60 +++++++++++++++-------
 2 files changed, 62 insertions(+), 23 deletions(-)

diff --git 
a/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/DefaultRestOpenapiProcessorStrategy.java
 
b/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/DefaultRestOpenapiProcessorStrategy.java
index b107edc8de6..086e6d84fdc 100644
--- 
a/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/DefaultRestOpenapiProcessorStrategy.java
+++ 
b/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/DefaultRestOpenapiProcessorStrategy.java
@@ -194,13 +194,31 @@ public class DefaultRestOpenapiProcessorStrategy extends 
ServiceSupport
             RestBindingAdvice binding,
             Exchange exchange, AsyncCallback callback) {
 
+        exchange.setProperty(Exchange.REST_OPENAPI, openAPI);
+
         if ("mock".equalsIgnoreCase(missingOperation) || 
"ignore".equalsIgnoreCase(missingOperation)) {
             // check if there is a route
             Endpoint e = camelContext.hasEndpoint(component + ":" + 
operation.getOperationId());
             if (e == null) {
-                if ("mock".equalsIgnoreCase(missingOperation)) {
-                    // no route then try to load mock data as the answer
-                    loadMockData(operation, verb, path, exchange);
+                try {
+                    var requestError = 
binding.doClientRequestValidation(exchange);
+                    if (requestError != null) {
+                        
exchange.getMessage().setHeader(Exchange.HTTP_RESPONSE_CODE, 
requestError.statusCode());
+                        exchange.getMessage().setBody(requestError.body());
+                        exchange.setRouteStop(true);
+                    } else if ("mock".equalsIgnoreCase(missingOperation)) {
+                        // no route then try to load mock data as the answer
+                        loadMockData(operation, verb, path, exchange);
+                    }
+                    if (requestError == null) {
+                        var responseError = 
binding.doClientResponseValidation(exchange);
+                        if (responseError != null) {
+                            
exchange.getMessage().setHeader(Exchange.HTTP_RESPONSE_CODE, 
responseError.statusCode());
+                            
exchange.getMessage().setBody(responseError.body());
+                        }
+                    }
+                } catch (Exception ex) {
+                    exchange.setException(ex);
                 }
                 callback.done(true);
                 return true;
@@ -210,7 +228,6 @@ public class DefaultRestOpenapiProcessorStrategy extends 
ServiceSupport
         // there is a route so process
         Map<String, Object> state;
         try {
-            exchange.setProperty(Exchange.REST_OPENAPI, openAPI);
             state = binding.before(exchange);
         } catch (Exception e) {
             exchange.setException(e);
diff --git 
a/core/camel-support/src/main/java/org/apache/camel/support/processor/RestBindingAdvice.java
 
b/core/camel-support/src/main/java/org/apache/camel/support/processor/RestBindingAdvice.java
index d60998ee062..be337a4e713 100644
--- 
a/core/camel-support/src/main/java/org/apache/camel/support/processor/RestBindingAdvice.java
+++ 
b/core/camel-support/src/main/java/org/apache/camel/support/processor/RestBindingAdvice.java
@@ -231,17 +231,12 @@ public class RestBindingAdvice extends ServiceSupport 
implements CamelInternalPr
         }
 
         // perform client request validation
-        if (clientRequestValidation && clientRequestValidator != null) {
-            RestClientRequestValidator.ValidationContext vc = new 
RestClientRequestValidator.ValidationContext(
-                    consumes, produces, requiredBody, queryDefaultValues, 
queryAllowedValues, requiredQueryParameters,
-                    requiredHeaders);
-            RestClientRequestValidator.ValidationError error = 
clientRequestValidator.validate(exchange, vc);
-            if (error != null) {
-                exchange.getMessage().setHeader(Exchange.HTTP_RESPONSE_CODE, 
error.statusCode());
-                exchange.getMessage().setBody(error.body());
-                exchange.setRouteStop(true);
-                return;
-            }
+        RestClientRequestValidator.ValidationError error = 
doClientRequestValidation(exchange);
+        if (error != null) {
+            exchange.getMessage().setHeader(Exchange.HTTP_RESPONSE_CODE, 
error.statusCode());
+            exchange.getMessage().setBody(error.body());
+            exchange.setRouteStop(true);
+            return;
         }
 
         String body = null;
@@ -474,14 +469,10 @@ public class RestBindingAdvice extends ServiceSupport 
implements CamelInternalPr
         }
 
         // perform client response validation
-        if (clientResponseValidation && clientResponseValidator != null && 
!exchange.isFailed()) {
-            RestClientResponseValidator.ValidationContext vc = new 
RestClientResponseValidator.ValidationContext(
-                    consumes, produces, responseCodes, responseHeaders);
-            RestClientResponseValidator.ValidationError error = 
clientResponseValidator.validate(exchange, vc);
-            if (error != null) {
-                exchange.getMessage().setHeader(Exchange.HTTP_RESPONSE_CODE, 
error.statusCode());
-                exchange.getMessage().setBody(error.body());
-            }
+        RestClientResponseValidator.ValidationError error = 
doClientResponseValidation(exchange);
+        if (error != null) {
+            exchange.getMessage().setHeader(Exchange.HTTP_RESPONSE_CODE, 
error.statusCode());
+            exchange.getMessage().setBody(error.body());
         }
     }
 
@@ -556,6 +547,37 @@ public class RestBindingAdvice extends ServiceSupport 
implements CamelInternalPr
         }
     }
 
+    /**
+     * Performs the client request validation (if enabled)
+     *
+     * @param  exchange the exchange
+     * @return          null if success otherwise an error is returned
+     */
+    public RestClientRequestValidator.ValidationError 
doClientRequestValidation(Exchange exchange) {
+        if (clientRequestValidation && clientRequestValidator != null) {
+            RestClientRequestValidator.ValidationContext vc = new 
RestClientRequestValidator.ValidationContext(
+                    consumes, produces, requiredBody, queryDefaultValues, 
queryAllowedValues, requiredQueryParameters,
+                    requiredHeaders);
+            return clientRequestValidator.validate(exchange, vc);
+        }
+        return null;
+    }
+
+    /**
+     * Performs the client response validation (if enabled)
+     *
+     * @param  exchange the exchange
+     * @return          null if success otherwise an error is returned
+     */
+    public RestClientResponseValidator.ValidationError 
doClientResponseValidation(Exchange exchange) {
+        if (clientResponseValidation && clientResponseValidator != null && 
!exchange.isFailed()) {
+            RestClientResponseValidator.ValidationContext vc = new 
RestClientResponseValidator.ValidationContext(
+                    consumes, produces, responseCodes, responseHeaders);
+            return clientResponseValidator.validate(exchange, vc);
+        }
+        return null;
+    }
+
     @Override
     protected void doStart() throws Exception {
         ServiceHelper.startService(jsonUnmarshal, xmlUnmarshal, jsonMarshal, 
xmlMarshal);

Reply via email to