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 2013/09/01 11:14:48 UTC
[2/3] git commit: CAMEL-6687: Bean using OGNL should accept null as
valid response from OGNL invocation.
CAMEL-6687: Bean using OGNL should accept null as valid response from OGNL invocation.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/08689b9f
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/08689b9f
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/08689b9f
Branch: refs/heads/camel-2.12.x
Commit: 08689b9fc896a74854b02d49ad0cedb68be8b64d
Parents: ffe4174
Author: Claus Ibsen <da...@apache.org>
Authored: Sun Sep 1 11:08:28 2013 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Sun Sep 1 11:08:55 2013 +0200
----------------------------------------------------------------------
.../apache/camel/component/bean/MethodInfo.java | 63 +++++++--------
.../BeanOgnlBodyMethodReturnNullValueTest.java | 82 ++++++++++++++++++++
2 files changed, 114 insertions(+), 31 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/08689b9f/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java b/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java
index 7160d3d..cc58c50 100644
--- a/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java
+++ b/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java
@@ -512,46 +512,47 @@ public class MethodInfo {
try {
expression = exchange.getContext().resolveLanguage("simple").createExpression(exp);
parameterValue = expression.evaluate(exchange, Object.class);
+ // use "null" to indicate the expression returned a null value which is a valid response we need to honor
+ if (parameterValue == null) {
+ parameterValue = "null";
+ }
} catch (Exception e) {
throw new ExpressionEvaluationException(expression, "Cannot create/evaluate simple expression: " + exp
+ " to be bound to parameter at index: " + index + " on method: " + getMethod(), exchange, e);
}
- if (parameterValue != null) {
+ // special for explicit null parameter values (as end users can explicit indicate they want null as parameter)
+ // see method javadoc for details
+ if ("null".equals(parameterValue)) {
+ return Void.TYPE;
+ }
- // special for explicit null parameter values (as end users can explicit indicate they want null as parameter)
- // see method javadoc for details
- if ("null".equals(parameterValue)) {
- return Void.TYPE;
- }
+ // the parameter value was not already valid, but since the simple language have evaluated the expression
+ // which may change the parameterValue, so we have to check it again to see if its now valid
+ exp = exchange.getContext().getTypeConverter().convertTo(String.class, parameterValue);
+ // String values from the simple language is always valid
+ if (!valid) {
+ // re validate if the parameter was not valid the first time (String values should be accepted)
+ valid = parameterValue instanceof String || BeanHelper.isValidParameterValue(exp);
+ }
- // the parameter value was not already valid, but since the simple language have evaluated the expression
- // which may change the parameterValue, so we have to check it again to see if its now valid
- exp = exchange.getContext().getTypeConverter().convertTo(String.class, parameterValue);
- // String values from the simple language is always valid
- if (!valid) {
- // re validate if the parameter was not valid the first time (String values should be accepted)
- valid = parameterValue instanceof String || BeanHelper.isValidParameterValue(exp);
+ if (valid) {
+ // we need to unquote String parameters, as the enclosing quotes is there to denote a parameter value
+ if (parameterValue instanceof String) {
+ parameterValue = StringHelper.removeLeadingAndEndingQuotes((String) parameterValue);
}
-
- if (valid) {
- // we need to unquote String parameters, as the enclosing quotes is there to denote a parameter value
- if (parameterValue instanceof String) {
- parameterValue = StringHelper.removeLeadingAndEndingQuotes((String) parameterValue);
- }
- if (parameterValue != null) {
- try {
- // its a valid parameter value, so convert it to the expected type of the parameter
- answer = exchange.getContext().getTypeConverter().mandatoryConvertTo(parameterType, exchange, parameterValue);
- if (LOG.isTraceEnabled()) {
- LOG.trace("Parameter #{} evaluated as: {} type: ", new Object[]{index, answer, ObjectHelper.type(answer)});
- }
- } catch (Exception e) {
- if (LOG.isDebugEnabled()) {
- LOG.debug("Cannot convert from type: {} to type: {} for parameter #{}", new Object[]{ObjectHelper.type(parameterValue), parameterType, index});
- }
- throw new ParameterBindingException(e, method, index, parameterType, parameterValue);
+ if (parameterValue != null) {
+ try {
+ // its a valid parameter value, so convert it to the expected type of the parameter
+ answer = exchange.getContext().getTypeConverter().mandatoryConvertTo(parameterType, exchange, parameterValue);
+ if (LOG.isTraceEnabled()) {
+ LOG.trace("Parameter #{} evaluated as: {} type: ", new Object[]{index, answer, ObjectHelper.type(answer)});
+ }
+ } catch (Exception e) {
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Cannot convert from type: {} to type: {} for parameter #{}", new Object[]{ObjectHelper.type(parameterValue), parameterType, index});
}
+ throw new ParameterBindingException(e, method, index, parameterType, parameterValue);
}
}
}
http://git-wip-us.apache.org/repos/asf/camel/blob/08689b9f/camel-core/src/test/java/org/apache/camel/component/bean/BeanOgnlBodyMethodReturnNullValueTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/component/bean/BeanOgnlBodyMethodReturnNullValueTest.java b/camel-core/src/test/java/org/apache/camel/component/bean/BeanOgnlBodyMethodReturnNullValueTest.java
new file mode 100644
index 0000000..b477479
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/component/bean/BeanOgnlBodyMethodReturnNullValueTest.java
@@ -0,0 +1,82 @@
+/**
+ * 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.component.bean;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+
+/**
+ *
+ */
+public class BeanOgnlBodyMethodReturnNullValueTest extends ContextTestSupport {
+
+ public void testBothValue() {
+ ExamplePojo fooBar = new ExamplePojo();
+ fooBar.setFoo("foo1");
+ fooBar.setBar("bar2");
+
+ String result = template.requestBody("direct:start", fooBar, String.class);
+ assertEquals("foo: foo1; bar: bar2", result);
+ }
+
+ public void testNullValue() {
+ ExamplePojo fooBar = new ExamplePojo();
+ fooBar.setFoo(null);
+ fooBar.setBar("test");
+
+ String result = template.requestBody("direct:start", fooBar, String.class);
+ assertEquals("foo: null; bar: test", result);
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("direct:start")
+ .bean(new ExampleBean(), "doWithFooBar(${body.foo}, ${body.bar})");
+ }
+ };
+ }
+
+ public static class ExampleBean {
+ public String doWithFooBar(String foo, String bar) {
+ return String.format("foo: %s; bar: %s", foo, bar);
+ }
+ }
+
+ public static class ExamplePojo {
+ private String foo;
+ private String bar;
+
+ public String getFoo() {
+ return foo;
+ }
+
+ public void setFoo(String foo) {
+ this.foo = foo;
+ }
+
+ public String getBar() {
+ return bar;
+ }
+
+ public void setBar(String bar) {
+ this.bar = bar;
+ }
+ }
+}