You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ni...@apache.org on 2010/05/20 06:30:28 UTC
svn commit: r946522 - in /camel/trunk/camel-core/src:
main/java/org/apache/camel/model/
main/java/org/apache/camel/processor/validation/
test/java/org/apache/camel/processor/
Author: ningjiang
Date: Thu May 20 04:30:28 2010
New Revision: 946522
URL: http://svn.apache.org/viewvc?rev=946522&view=rev
Log:
CAMEL-1537 Predicate for validation by applying patch with thanks to Christian
Added:
camel/trunk/camel-core/src/main/java/org/apache/camel/model/ValidateDefinition.java (with props)
camel/trunk/camel-core/src/main/java/org/apache/camel/processor/validation/PredicateValidatingProcessor.java (with props)
camel/trunk/camel-core/src/main/java/org/apache/camel/processor/validation/PredicateValidationException.java (with props)
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ValidateRegExpTest.java (with props)
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java
Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java?rev=946522&r1=946521&r2=946522&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java Thu May 20 04:30:28 2010
@@ -45,6 +45,7 @@ import org.apache.camel.builder.ErrorHan
import org.apache.camel.builder.ExpressionBuilder;
import org.apache.camel.builder.ExpressionClause;
import org.apache.camel.builder.ProcessorBuilder;
+import org.apache.camel.builder.ValueBuilder;
import org.apache.camel.model.language.ConstantExpression;
import org.apache.camel.model.language.ExpressionDefinition;
import org.apache.camel.model.language.LanguageExpression;
@@ -1108,6 +1109,20 @@ public abstract class ProcessorDefinitio
public FilterDefinition filter(String language, String expression) {
return filter(new LanguageExpression(language, expression));
}
+
+ /**
+ * Creates a validation expression which only if it is <tt>true</tt> then the
+ * exchange is forwarded to the destination. Otherwise a
+ * PredicateExpValidationException is thrown.
+ *
+ * @param builder the value builder
+ * @return the validate definition
+ */
+ public ValidateDefinition validate(ValueBuilder builder) {
+ ValidateDefinition validate = new ValidateDefinition(builder);
+ addOutput(validate);
+ return validate;
+ }
/**
* <a href="http://camel.apache.org/load-balancer.html">Load Balancer EIP:</a>
Added: camel/trunk/camel-core/src/main/java/org/apache/camel/model/ValidateDefinition.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ValidateDefinition.java?rev=946522&view=auto
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/ValidateDefinition.java (added)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/ValidateDefinition.java Thu May 20 04:30:28 2010
@@ -0,0 +1,102 @@
+/**
+ * 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.model;
+
+import java.util.Comparator;
+
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlAttribute;
+import javax.xml.bind.annotation.XmlRootElement;
+
+import org.apache.camel.Processor;
+import org.apache.camel.builder.ExpressionClause;
+import org.apache.camel.builder.PredicateBuilder;
+import org.apache.camel.builder.ValueBuilder;
+import org.apache.camel.model.language.ExpressionDefinition;
+import org.apache.camel.processor.validation.PredicateValidatingProcessor;
+import org.apache.camel.spi.RouteContext;
+
+/**
+ * Represents an XML <validate/> element
+ *
+ * @version $Revision$
+ */
+@XmlRootElement(name = "validate")
+@XmlAccessorType(XmlAccessType.FIELD)
+public class ValidateDefinition extends ExpressionNode {
+ @XmlAttribute(required = false)
+ private String regexExpression;
+
+ public ValidateDefinition() {
+ // TODO need to find a way to set the ValueBuilder from XML
+ super();
+ }
+
+ public ValidateDefinition(ValueBuilder builder) {
+ super(builder);
+ }
+
+ public void setRegexExpression(String regex) {
+ this.regexExpression = regex;
+ regex(regex);
+ }
+
+ public String getComparatorRef() {
+ return regexExpression;
+ }
+
+ @Override
+ public String toString() {
+ return "Validate[" + getExpression() + " -> " + getOutputs() + "]";
+ }
+
+ @Override
+ public String getShortName() {
+ return "validate";
+ }
+
+
+ @Override
+ public PredicateValidatingProcessor createProcessor(RouteContext routeContext) throws Exception {
+ Processor childProcessor = routeContext.createProcessor(this);
+ return new PredicateValidatingProcessor(getExpression().createPredicate(routeContext), childProcessor);
+ }
+
+ // Fluent API
+ //-------------------------------------------------------------------------
+
+ /**
+ * Sets the regular expression
+ *
+ * @param regex the regeular expression
+ * @return the ValidateDefinition
+ */
+ public ValidateDefinition regex(String regex) {
+ setExpression(new ExpressionDefinition(PredicateBuilder.regex(getExpression(), regex)));
+ return this;
+ }
+
+
+ /**
+ * Set the expression that this FilterType will use
+ * @return the builder
+ */
+ public ExpressionClause<ValidateDefinition> expression() {
+ return ExpressionClause.createAndSetExpression(this);
+ }
+}
\ No newline at end of file
Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/model/ValidateDefinition.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/model/ValidateDefinition.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/validation/PredicateValidatingProcessor.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/validation/PredicateValidatingProcessor.java?rev=946522&view=auto
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/processor/validation/PredicateValidatingProcessor.java (added)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/processor/validation/PredicateValidatingProcessor.java Thu May 20 04:30:28 2010
@@ -0,0 +1,70 @@
+/**
+ * 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.processor.validation;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Predicate;
+import org.apache.camel.Processor;
+import org.apache.camel.processor.DelegateProcessor;
+import org.apache.camel.processor.Traceable;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * A processor which validates the content of the inbound message body
+ * against a regular expression.
+ *
+ * @version $Revision$
+ */
+public class PredicateValidatingProcessor extends DelegateProcessor implements Traceable {
+
+ private static final Log LOG = LogFactory.getLog(PredicateValidatingProcessor.class);
+
+ private final Predicate predicate;
+
+ public PredicateValidatingProcessor(Predicate predicate, Processor processor) {
+ super(processor);
+ this.predicate = predicate;
+ }
+
+ public void process(Exchange exchange) throws Exception {
+ boolean matches = predicate.matches(exchange);
+
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Validation " + (matches ? "succeed " : "failed ") + "for " + exchange + " and Predicate[" + predicate + "]");
+ }
+
+ if (matches) {
+ super.process(exchange);
+ } else {
+ throw new PredicateValidationException(exchange, predicate);
+ }
+ }
+
+ @Override
+ public String toString() {
+ return "validate[if: " + predicate + " matches]";
+ }
+
+ public String getTraceLabel() {
+ return toString();
+ }
+
+ public Predicate getPredicate() {
+ return predicate;
+ }
+}
\ No newline at end of file
Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/validation/PredicateValidatingProcessor.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/validation/PredicateValidatingProcessor.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/validation/PredicateValidationException.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/validation/PredicateValidationException.java?rev=946522&view=auto
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/processor/validation/PredicateValidationException.java (added)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/processor/validation/PredicateValidationException.java Thu May 20 04:30:28 2010
@@ -0,0 +1,50 @@
+/**
+ * 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.processor.validation;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Predicate;
+import org.apache.camel.ValidationException;
+
+/**
+ * A predicate validation exception occurred
+ *
+ * @version $Revision$
+ */
+public class PredicateValidationException extends ValidationException {
+
+ private static final long serialVersionUID = 5767438583860347105L;
+
+ private final Predicate predicate;
+
+ public PredicateValidationException(Exchange exchange, Predicate predicate) {
+ super(exchange, buildMessage(predicate, exchange));
+ this.predicate = predicate;
+ }
+
+ protected static String buildMessage(Predicate predicate, Exchange exchange) {
+ StringBuilder builder = new StringBuilder("Validation failed for Predicate[");
+ builder.append(predicate.toString());
+ builder.append("]");
+
+ return builder.toString();
+ }
+
+ public Predicate getPredicate() {
+ return predicate;
+ }
+}
\ No newline at end of file
Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/validation/PredicateValidationException.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/validation/PredicateValidationException.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ValidateRegExpTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ValidateRegExpTest.java?rev=946522&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ValidateRegExpTest.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ValidateRegExpTest.java Thu May 20 04:30:28 2010
@@ -0,0 +1,76 @@
+/**
+ * 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.processor;
+
+import org.apache.camel.CamelExecutionException;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Endpoint;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.processor.validation.PredicateValidationException;
+
+/**
+ * @version $Revision$
+ */
+public class ValidateRegExpTest extends ContextTestSupport {
+
+ private Endpoint startEndpoint;
+ private MockEndpoint resultEndpoint;
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ startEndpoint = resolveMandatoryEndpoint("direct:start", Endpoint.class);
+ resultEndpoint = resolveMandatoryEndpoint("mock:result", MockEndpoint.class);
+ }
+
+ public void testSendMatchingMessage() throws Exception {
+ resultEndpoint.expectedMessageCount(1);
+
+ template.sendBody(startEndpoint, "01.01.2010");
+
+ resultEndpoint.assertIsSatisfied();
+ }
+
+ public void testSendNotMatchingMessage() throws Exception {
+ resultEndpoint.expectedMessageCount(0);
+
+ try {
+ template.sendBody(startEndpoint, "1.1.2010");
+ fail("CamelExecutionException expected");
+ } catch (CamelExecutionException e) {
+ // expected
+ assertTrue(e.getCause() instanceof PredicateValidationException);
+ String message = ((PredicateValidationException) e.getCause()).getMessage();
+ assertEquals("Validation failed for Predicate[{bodyAs[java.lang.String]}.matches('^\\d{2}\\.\\d{2}\\.\\d{4}$')]. Exchange[Message: 1.1.2010]", message);
+ }
+
+ resultEndpoint.assertIsSatisfied();
+ }
+
+
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ public void configure() {
+ from("direct:start")
+ .validate(body(String.class)).regex("^\\d{2}\\.\\d{2}\\.\\d{4}$")
+ .to("mock:result");
+ }
+ };
+ }
+}
\ No newline at end of file
Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ValidateRegExpTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ValidateRegExpTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date