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 2008/11/26 20:44:54 UTC
svn commit: r720962 - in /activemq/camel/trunk/camel-core/src:
main/java/org/apache/camel/builder/
main/java/org/apache/camel/language/simple/
main/resources/org/apache/camel/model/ test/java/org/apache/camel/language/
test/java/org/apache/camel/proces...
Author: davsclaus
Date: Wed Nov 26 11:44:54 2008
New Revision: 720962
URL: http://svn.apache.org/viewvc?rev=720962&view=rev
Log:
CAMEL-1120: Added builder for getting exception.message. Fixed jaxb.index for removed stuff.
Modified:
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/Builder.java
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/BuilderSupport.java
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java
activemq/camel/trunk/camel-core/src/main/resources/org/apache/camel/model/jaxb.index
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/language/SimpleTest.java
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionHandleAndTransformTest.java
Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/Builder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/Builder.java?rev=720962&r1=720961&r2=720962&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/Builder.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/Builder.java Wed Nov 26 11:44:54 2008
@@ -142,4 +142,12 @@
public static ValueBuilder systemProperty(final String name, final String defaultValue) {
return new ValueBuilder(ExpressionBuilder.systemProperty(name, defaultValue));
}
+
+ /**
+ * Returns a predicate and value builder for the exception message on an exchange
+ */
+ public static ValueBuilder exceptionMessage() {
+ Expression expression = ExpressionBuilder.exchangeExceptionMessageExpression();
+ return new ValueBuilder(expression);
+ }
}
Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/BuilderSupport.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/BuilderSupport.java?rev=720962&r1=720961&r2=720962&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/BuilderSupport.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/BuilderSupport.java Wed Nov 26 11:44:54 2008
@@ -154,6 +154,13 @@
}
/**
+ * Returns a exception expression value builder
+ */
+ public ValueBuilder exceptionMessage() {
+ return Builder.exceptionMessage();
+ }
+
+ /**
* Resolves the given URI to an endpoint
*
* @param uri the uri to resolve
Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java?rev=720962&r1=720961&r2=720962&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java Wed Nov 26 11:44:54 2008
@@ -122,7 +122,7 @@
* Returns an expression for the outbound message headers
*
* @see Message#getHeaders()
- * @return an expression object which will return the inbound headers
+ * @return an expression object which will return the headers
*/
public static Expression outHeadersExpression() {
return new Expression() {
@@ -161,6 +161,29 @@
}
/**
+ * Returns an expression for an exception message set on the exchange
+ *
+ * @see <tt>Exchange.getException().getMessage()</tt>
+ * @return an expression object which will return the exception message set on the exchange
+ */
+ public static Expression exchangeExceptionMessageExpression() {
+ return new Expression() {
+ public Object evaluate(Exchange exchange) {
+ Throwable exception = exchange.getException();
+ if (exception == null) {
+ exception = exchange.getProperty(DeadLetterChannel.EXCEPTION_CAUSE_PROPERTY, Throwable.class);
+ }
+ return exception != null ? exception.getMessage() : null;
+ }
+
+ @Override
+ public String toString() {
+ return "exchangeExceptionMessage";
+ }
+ };
+ }
+
+ /**
* Returns an expression for the property value with the given name
*
* @see Exchange#getProperty(String)
@@ -203,8 +226,7 @@
/**
* Returns an expression for a system property value with the given name
*
- * @param propertyName the name of the system property the expression will
- * return
+ * @param propertyName the name of the system property the expression will return
* @return an expression object which will return the system property value
*/
public static Expression systemPropertyExpression(final String propertyName) {
@@ -214,12 +236,11 @@
/**
* Returns an expression for a system property value with the given name
*
- * @param propertyName the name of the system property the expression will
- * return
+ * @param propertyName the name of the system property the expression will return
* @return an expression object which will return the system property value
*/
public static Expression systemPropertyExpression(final String propertyName,
- final String defaultValue) {
+ final String defaultValue) {
return new Expression() {
public Object evaluate(Exchange exchange) {
return System.getProperty(propertyName, defaultValue);
@@ -407,8 +428,7 @@
}
/**
- * Returns an expression which converts the given expression to the given
- * type
+ * Returns an expression which converts the given expression to the given type
*/
public static Expression convertTo(final Expression expression, final Class type) {
return new Expression() {
@@ -429,7 +449,7 @@
* given token
*/
public static Expression tokenizeExpression(final Expression expression,
- final String token) {
+ final String token) {
return new Expression() {
public Object evaluate(Exchange exchange) {
Object value = expression.evaluate(exchange);
@@ -449,8 +469,8 @@
* Returns a tokenize expression which will tokenize the string with the
* given regex
*/
- public static Expression regexTokenize(final Expression expression,
- final String regexTokenizer) {
+ public static Expression regexTokenize(final Expression expression,
+ final String regexTokenizer) {
final Pattern pattern = Pattern.compile(regexTokenizer);
return new Expression() {
public Object evaluate(Exchange exchange) {
@@ -508,7 +528,7 @@
* replaceAll to transform the String and return the result
*/
public static Expression regexReplaceAll(final Expression expression,
- final String regex, final String replacement) {
+ final String regex, final String replacement) {
final Pattern pattern = Pattern.compile(regex);
return new Expression() {
public Object evaluate(Exchange exchange) {
@@ -531,8 +551,8 @@
* replaceAll to transform the String and return the result
*/
public static Expression regexReplaceAll(final Expression expression,
- String regex, final Expression replacementExpression) {
-
+ final String regex, final Expression replacementExpression) {
+
final Pattern pattern = Pattern.compile(regex);
return new Expression() {
public Object evaluate(Exchange exchange) {
Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java?rev=720962&r1=720961&r2=720962&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java Wed Nov 26 11:44:54 2008
@@ -27,13 +27,14 @@
* which maps simple property style notations to access headers and bodies.
* Examples of supported expressions are:
* <ul>
- * <li>id to access the inbound message Id</li>
+ * <li>id to access the inbound message id</li>
* <li>in.body or body to access the inbound body</li>
* <li>out.body to access the inbound body</li>
* <li>in.header.foo or header.foo to access an inbound header called 'foo'</li>
* <li>out.header.foo to access an outbound header called 'foo'</li>
* <li>property.foo to access the exchange property called 'foo'</li>
* <li>sys.foo to access the system property called 'foo'</li>
+ * <li>exception.messsage to access the exception message</li>
* <li>date:<command>:<pattern> for date formatting using the {@link java.text.SimpleDateFormat} patterns.
* Supported commands are: <tt>now</tt> for current timestamp,
* <tt>in.header.xxx</tt> or <tt>header.xxx</tt> to use the Date object in the in header.
@@ -59,6 +60,8 @@
return ExpressionBuilder.outBodyExpression();
} else if (ObjectHelper.equal(expression, "id")) {
return ExpressionBuilder.messageIdExpression();
+ } else if (ObjectHelper.equal(expression, "exception.message")) {
+ return ExpressionBuilder.exchangeExceptionMessageExpression();
}
// in header expression
Modified: activemq/camel/trunk/camel-core/src/main/resources/org/apache/camel/model/jaxb.index
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/resources/org/apache/camel/model/jaxb.index?rev=720962&r1=720961&r2=720962&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/resources/org/apache/camel/model/jaxb.index (original)
+++ activemq/camel/trunk/camel-core/src/main/resources/org/apache/camel/model/jaxb.index Wed Nov 26 11:44:54 2008
@@ -50,7 +50,6 @@
RouteBuilderRef
RoutesType
RoutingSlipType
-ServiceActivationType
SetBodyType
SetHeaderType
SetOutHeaderType
Modified: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/language/SimpleTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/language/SimpleTest.java?rev=720962&r1=720961&r2=720962&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/language/SimpleTest.java (original)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/language/SimpleTest.java Wed Nov 26 11:44:54 2008
@@ -66,6 +66,12 @@
assertPredicate("header.madeUpHeader", false);
}
+ public void testExceptionMessage() throws Exception {
+ exchange.setException(new IllegalArgumentException("Just testing"));
+ assertExpression("exception.message", "Just testing");
+ assertExpression("Hello ${exception.message} World", "Hello Just testing World");
+ }
+
protected String getLanguageName() {
return "simple";
}
Modified: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionHandleAndTransformTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionHandleAndTransformTest.java?rev=720962&r1=720961&r2=720962&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionHandleAndTransformTest.java (original)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionHandleAndTransformTest.java Wed Nov 26 11:44:54 2008
@@ -26,14 +26,8 @@
*/
public class OnExceptionHandleAndTransformTest extends ContextTestSupport {
- public void testOnException() throws Exception {
- Object out = template.requestBody("direct:start", "Hello World");
- assertEquals("Sorry", out);
- }
-
- @Override
- protected RouteBuilder createRouteBuilder() throws Exception {
- return new RouteBuilder() {
+ public void testOnExceptionTransformConstant() throws Exception {
+ context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
errorHandler(deadLetterChannel("mock:error").maximumRedeliveries(0));
@@ -52,6 +46,63 @@
}
});
}
- };
+ });
+
+ Object out = template.requestBody("direct:start", "Hello World");
+ assertEquals("Sorry", out);
+ }
+
+ public void testOnExceptionTransformExceptionMessage() throws Exception {
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ errorHandler(deadLetterChannel("mock:error").maximumRedeliveries(0));
+
+ // START SNIPPET: e2
+ // we catch MyFunctionalException and want to mark it as handled (= no failure returned to client)
+ // but we want to return a fixed text response, so we transform OUT body and return the exception message
+ onException(MyFunctionalException.class)
+ .handled(true)
+ .transform(exceptionMessage());
+ // END SNIPPET: e2
+
+ from("direct:start").process(new Processor() {
+ public void process(Exchange exchange) throws Exception {
+ throw new MyFunctionalException("Sorry you can not do this again to me");
+ }
+ });
+ }
+ });
+
+ Object out = template.requestBody("direct:start", "Hello World");
+ assertEquals("Sorry you can not do this again to me", out);
}
+
+ public void testOnExceptionSimpleLangaugeExceptionMessage() throws Exception {
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ errorHandler(deadLetterChannel("mock:error").maximumRedeliveries(0));
+
+ // START SNIPPET: e3
+ // we catch MyFunctionalException and want to mark it as handled (= no failure returned to client)
+ // but we want to return a fixed text response, so we transform OUT body and return a nice message
+ // using the simple language where we want insert the exception message
+ onException(MyFunctionalException.class)
+ .handled(true)
+ .transform().simple("Error reported: ${exception.message} - can not process this message.");
+ // END SNIPPET: e3
+
+ from("direct:start").process(new Processor() {
+ public void process(Exchange exchange) throws Exception {
+ throw new MyFunctionalException("Out of order");
+ }
+ });
+ }
+ });
+
+ Object out = template.requestBody("direct:start", "Hello World");
+ assertEquals("Error reported: Out of order - can not process this message.", out);
+ }
+
}