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 2022/01/10 08:16:47 UTC
[camel] 01/01: CAMEL-17307: Add exception factory for predicate validation. WIP.
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch throw-predicate
in repository https://gitbox.apache.org/repos/asf/camel.git
commit 3ad876a5816a0eef4add96f9fe3ae325cdaad071
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Jan 10 09:15:18 2022 +0100
CAMEL-17307: Add exception factory for predicate validation. WIP.
---
.../camel/spi/PredicateExceptionFactory.java | 38 ++++++++++++++++++++++
.../processor/PredicateValidatingProcessor.java | 25 +++++++++++++-
2 files changed, 62 insertions(+), 1 deletion(-)
diff --git a/core/camel-api/src/main/java/org/apache/camel/spi/PredicateExceptionFactory.java b/core/camel-api/src/main/java/org/apache/camel/spi/PredicateExceptionFactory.java
new file mode 100644
index 0000000..df5711ff
--- /dev/null
+++ b/core/camel-api/src/main/java/org/apache/camel/spi/PredicateExceptionFactory.java
@@ -0,0 +1,38 @@
+/*
+ * 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.spi;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Predicate;
+
+/**
+ * A factory that can be used to create a specific exception when a {@link Predicate} returning false, which can be used
+ * by camel-validator and other components.
+ */
+public interface PredicateExceptionFactory {
+
+ /**
+ * Allows to return a specific exception for the given predicate in case it failed
+ *
+ * @param exchange the current exchange
+ * @param predicate the predicate that returned false
+ * @return the exception, or <tt>null</tt> to not use a specific exception but let Camel use a standard
+ * exception such as PredicateValidationException.
+ */
+ Exception newPredicateException(Exchange exchange, Predicate predicate);
+
+}
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/processor/PredicateValidatingProcessor.java b/core/camel-support/src/main/java/org/apache/camel/support/processor/PredicateValidatingProcessor.java
index c318869..02c64d2 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/processor/PredicateValidatingProcessor.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/processor/PredicateValidatingProcessor.java
@@ -20,6 +20,7 @@ import org.apache.camel.Exchange;
import org.apache.camel.Predicate;
import org.apache.camel.Processor;
import org.apache.camel.Traceable;
+import org.apache.camel.spi.PredicateExceptionFactory;
import org.apache.camel.support.service.ServiceSupport;
import org.apache.camel.util.ObjectHelper;
import org.slf4j.Logger;
@@ -33,6 +34,7 @@ public class PredicateValidatingProcessor extends ServiceSupport implements Proc
private static final Logger LOG = LoggerFactory.getLogger(PredicateValidatingProcessor.class);
private final Predicate predicate;
+ private PredicateExceptionFactory predicateExceptionFactory;
public PredicateValidatingProcessor(Predicate predicate) {
ObjectHelper.notNull(predicate, "predicate", this);
@@ -48,7 +50,14 @@ public class PredicateValidatingProcessor extends ServiceSupport implements Proc
}
if (!matches) {
- throw new PredicateValidationException(exchange, predicate);
+ Exception cause = null;
+ if (predicateExceptionFactory != null) {
+ cause = predicateExceptionFactory.newPredicateException(exchange, predicate);
+ }
+ if (cause == null) {
+ cause = new PredicateValidationException(exchange, predicate);
+ }
+ throw cause;
}
}
@@ -56,6 +65,20 @@ public class PredicateValidatingProcessor extends ServiceSupport implements Proc
return predicate;
}
+ /**
+ * To use a custom factory for creating the exception to throw if predicate does not match
+ */
+ public PredicateExceptionFactory getPredicateExceptionFactory() {
+ return predicateExceptionFactory;
+ }
+
+ /**
+ * To use a custom factory for creating the exception to throw if predicate does not match
+ */
+ public void setPredicateExceptionFactory(PredicateExceptionFactory predicateExceptionFactory) {
+ this.predicateExceptionFactory = predicateExceptionFactory;
+ }
+
@Override
public String toString() {
return "validate(" + predicate + ")";