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 2009/06/02 07:23:21 UTC
svn commit: r780937 - in /camel/trunk/camel-core/src:
main/java/org/apache/camel/builder/ test/java/org/apache/camel/builder/
test/java/org/apache/camel/processor/
Author: davsclaus
Date: Tue Jun 2 05:23:20 2009
New Revision: 780937
URL: http://svn.apache.org/viewvc?rev=780937&view=rev
Log:
CAMEL-1665: Added to() and prepend() to ValueBuilder to be used as Expression in the DSL.
Added:
camel/trunk/camel-core/src/test/java/org/apache/camel/builder/ValueBuilderTest.java (with props)
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/TransformToTest.java (with props)
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/builder/Builder.java
camel/trunk/camel-core/src/main/java/org/apache/camel/builder/BuilderSupport.java
camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java
Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/Builder.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/Builder.java?rev=780937&r1=780936&r2=780937&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/builder/Builder.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/builder/Builder.java Tue Jun 2 05:23:20 2009
@@ -188,4 +188,16 @@
Expression newExp = ExpressionBuilder.regexReplaceAll(content, regex, replacement);
return new ValueBuilder(newExp);
}
+
+ /**
+ * Returns an expression processing the exchange to the given endpoint uri.
+ *
+ * @param uri endpoint uri
+ * @return the builder
+ */
+ public static ValueBuilder to(String uri) {
+ Expression expression = ExpressionBuilder.toExpression(uri);
+ return new ValueBuilder(expression);
+ }
+
}
Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/BuilderSupport.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/BuilderSupport.java?rev=780937&r1=780936&r2=780937&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/builder/BuilderSupport.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/builder/BuilderSupport.java Tue Jun 2 05:23:20 2009
@@ -175,6 +175,16 @@
}
/**
+ * Returns an expression processing the exchange to the given endpoint uri
+ *
+ * @param uri endpoint uri to send the exchange to
+ * @return the builder
+ */
+ public ValueBuilder to(String uri) {
+ return Builder.to(uri);
+ }
+
+ /**
* Returns an expression value builder that replaces all occurrences of the
* regular expression with the given replacement
*/
Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java?rev=780937&r1=780936&r2=780937&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java Tue Jun 2 05:23:20 2009
@@ -26,12 +26,16 @@
import java.util.Scanner;
import java.util.regex.Pattern;
+import org.apache.camel.Endpoint;
import org.apache.camel.Exchange;
import org.apache.camel.Expression;
import org.apache.camel.Message;
+import org.apache.camel.NoSuchEndpointException;
+import org.apache.camel.Producer;
import org.apache.camel.impl.ExpressionAdapter;
import org.apache.camel.language.bean.BeanLanguage;
import org.apache.camel.spi.Language;
+import org.apache.camel.util.ExchangeHelper;
import org.apache.camel.util.ObjectHelper;
/**
@@ -695,6 +699,22 @@
}
/**
+ * Prepends the String evaluations of the two expressions together
+ */
+ public static Expression prepend(final Expression left, final Expression right) {
+ return new ExpressionAdapter() {
+ public Object evaluate(Exchange exchange) {
+ return right.evaluate(exchange, String.class) + left.evaluate(exchange, String.class);
+ }
+
+ @Override
+ public String toString() {
+ return "prepend(" + left + ", " + right + ")";
+ }
+ };
+ }
+
+ /**
* Returns an expression which returns the string concatenation value of the various
* expressions
*
@@ -829,4 +849,44 @@
return beanExpression(expression);
}
+ /**
+ * Returns an expression processing the exchange to the given endpoint uri
+ *
+ * @param uri endpoint uri to send the exchange to
+ * @return an expression object which will return the OUT body
+ */
+ public static Expression toExpression(final String uri) {
+ return new ExpressionAdapter() {
+ public Object evaluate(Exchange exchange) {
+ Endpoint endpoint = exchange.getContext().getEndpoint(uri);
+ if (endpoint == null) {
+ throw new NoSuchEndpointException(uri);
+ }
+
+ Producer producer;
+ try {
+ producer = endpoint.createProducer();
+ producer.start();
+ producer.process(exchange);
+ producer.stop();
+ } catch (Exception e) {
+ throw ObjectHelper.wrapRuntimeCamelException(e);
+ }
+
+ // return the OUT body, but check for exchange pattern
+ if (ExchangeHelper.isOutCapable(exchange)) {
+ return exchange.getOut().getBody();
+ } else {
+ return exchange.getIn().getBody();
+ }
+ }
+
+ @Override
+ public String toString() {
+ return "to(" + uri + ")";
+ }
+ };
+ }
+
+
}
Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java?rev=780937&r1=780936&r2=780937&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java Tue Jun 2 05:23:20 2009
@@ -210,6 +210,16 @@
}
/**
+ * Prepends the string evaluation of this expression with the given value
+ *
+ * @param value the value or expression to prepend
+ * @return the current builder
+ */
+ public ValueBuilder prepend(Object value) {
+ return new ValueBuilder(ExpressionBuilder.prepend(expression, asExpression(value)));
+ }
+
+ /**
* Sorts the current value using the given comparator. The current value must be convertable
* to a {@link List} to allow sorting using the comparator.
*
Added: camel/trunk/camel-core/src/test/java/org/apache/camel/builder/ValueBuilderTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/ValueBuilderTest.java?rev=780937&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/builder/ValueBuilderTest.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/builder/ValueBuilderTest.java Tue Jun 2 05:23:20 2009
@@ -0,0 +1,65 @@
+/**
+ * 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.builder;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.component.mock.MockEndpoint;
+
+/**
+ * @version $Revision$
+ */
+public class ValueBuilderTest extends ContextTestSupport {
+
+ @Override
+ public boolean isUseRouteBuilder() {
+ return false;
+ }
+
+ public void testAppend() throws Exception {
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("direct:start").transform(body().append(" World")).to("mock:result");
+ }
+ });
+ context.start();
+
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.expectedBodiesReceived("Hello World");
+
+ template.sendBody("direct:start", "Hello");
+
+ assertMockEndpointsSatisfied();
+ }
+
+ public void testPrepend() throws Exception {
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("direct:start").transform(body().prepend("Hello ")).to("mock:result");
+ }
+ });
+ context.start();
+
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.expectedBodiesReceived("Hello World");
+
+ template.sendBody("direct:start", "World");
+
+ assertMockEndpointsSatisfied();
+ }
+}
Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/builder/ValueBuilderTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/builder/ValueBuilderTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/TransformToTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/TransformToTest.java?rev=780937&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/TransformToTest.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/TransformToTest.java Tue Jun 2 05:23:20 2009
@@ -0,0 +1,89 @@
+/**
+ * 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.Exchange;
+import org.apache.camel.NoSuchEndpointException;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+
+/**
+ * @version $Revision$
+ */
+public class TransformToTest extends ContextTestSupport {
+
+ public void testTransformTo() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.expectedBodiesReceived("Bye World");
+
+ String out = template.requestBody("direct:start", "Hello World", String.class);
+ assertEquals("Bye World", out);
+
+ assertMockEndpointsSatisfied();
+ }
+
+ public void testTransformToInOnly() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.expectedBodiesReceived("InOnly");
+
+ template.sendBody("direct:start", "Hello In");
+
+ assertMockEndpointsSatisfied();
+ }
+
+ public void testTransformToInvalidEndpoint() throws Exception {
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("direct:bar").transform(to("bar"));
+ }
+ });
+ context.start();
+
+ try {
+ template.requestBody("direct:bar", "Hello World");
+ fail("Should thrown an exception");
+ } catch (CamelExecutionException e) {
+ assertIsInstanceOf(NoSuchEndpointException.class, e.getCause());
+ }
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("direct:start")
+ .transform(to("direct:foo")).to("mock:result");
+
+ from("direct:foo").process(new Processor() {
+ public void process(Exchange exchange) throws Exception {
+ String body = exchange.getIn().getBody(String.class);
+ if ("Hello World".equals(body)) {
+ exchange.getOut().setBody("Bye World");
+ } else {
+ exchange.getIn().setBody("InOnly");
+ }
+ }
+ });
+ }
+ };
+ }
+}
Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/TransformToTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/TransformToTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date