Repository: camel Updated Branches: refs/heads/master 13a92cc13 -> 68d0f1419
CAMEL-7753: xslt component - Store warning/errors etc as exchange properties so end users can get hold of those Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/68d0f141 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/68d0f141 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/68d0f141 Branch: refs/heads/master Commit: 68d0f141946e77c28b61f45fb85f6f4f848c70a2 Parents: 13a92cc Author: Claus Ibsen <davscl...@apache.org> Authored: Wed Aug 27 11:05:47 2014 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Wed Aug 27 11:12:10 2014 +0200 ---------------------------------------------------------------------- .../main/java/org/apache/camel/Exchange.java | 5 +- .../xml/DefaultTransformErrorHandler.java | 25 ++++++--- .../apache/camel/builder/xml/XsltBuilder.java | 5 +- camel-core/src/test/data/terminate.xml | 36 +++++++++++++ .../xslt/XsltMessageTerminateTest.java | 57 ++++++++++++++++++++ .../apache/camel/component/xslt/terminate.xsl | 34 ++++++++++++ 6 files changed, 151 insertions(+), 11 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/68d0f141/camel-core/src/main/java/org/apache/camel/Exchange.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/Exchange.java b/camel-core/src/main/java/org/apache/camel/Exchange.java index 587f17d..a33253a 100644 --- a/camel-core/src/main/java/org/apache/camel/Exchange.java +++ b/camel-core/src/main/java/org/apache/camel/Exchange.java @@ -210,7 +210,10 @@ public interface Exchange { @Deprecated String UNIT_OF_WORK_PROCESS_SYNC = "CamelUnitOfWorkProcessSync"; - String XSLT_FILE_NAME = "CamelXsltFileName"; + String XSLT_FILE_NAME = "CamelXsltFileName"; + String XSLT_ERROR = "CamelXsltError"; + String XSLT_FATAL_ERROR = "CamelXsltFatalError"; + String XSLT_WARNING = "CamelXsltWarning"; /** * Returns the {@link ExchangePattern} (MEP) of this exchange. http://git-wip-us.apache.org/repos/asf/camel/blob/68d0f141/camel-core/src/main/java/org/apache/camel/builder/xml/DefaultTransformErrorHandler.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/builder/xml/DefaultTransformErrorHandler.java b/camel-core/src/main/java/org/apache/camel/builder/xml/DefaultTransformErrorHandler.java index c37df88..006545f 100644 --- a/camel-core/src/main/java/org/apache/camel/builder/xml/DefaultTransformErrorHandler.java +++ b/camel-core/src/main/java/org/apache/camel/builder/xml/DefaultTransformErrorHandler.java @@ -19,42 +19,53 @@ package org.apache.camel.builder.xml; import javax.xml.transform.ErrorListener; import javax.xml.transform.TransformerException; +import org.apache.camel.Exchange; import org.xml.sax.ErrorHandler; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - /** - * {@link ErrorHandler} and {@link ErrorListener} which will log warnings, + * {@link ErrorHandler} and {@link ErrorListener} which will ignore warnings, * and throws error and fatal as exception, which ensures those can be caught by Camel and dealt-with. + * <p/> + * Also any warning, error or fatal error is stored on the {@link Exchange} as a property with the keys + * {@link Exchange#XSLT_WARNING}, {@link Exchange#XSLT_ERROR}, and {@link Exchange#XSLT_FATAL_ERROR} which + * allows end users to access those information form the exchange. */ public class DefaultTransformErrorHandler implements ErrorHandler, ErrorListener { - private static final Logger LOG = LoggerFactory.getLogger(DefaultTransformErrorHandler.class); + + private final Exchange exchange; + + public DefaultTransformErrorHandler(Exchange exchange) { + this.exchange = exchange; + } public void error(SAXParseException exception) throws SAXException { + exchange.setProperty(Exchange.XSLT_ERROR, exception); throw exception; } public void fatalError(SAXParseException exception) throws SAXException { + exchange.setProperty(Exchange.XSLT_FATAL_ERROR, exception); throw exception; } public void warning(SAXParseException exception) throws SAXException { - LOG.warn("parser warning", exception); + exchange.setProperty(Exchange.XSLT_WARNING, exception); } public void error(TransformerException exception) throws TransformerException { + exchange.setProperty(Exchange.XSLT_ERROR, exception); throw exception; } public void fatalError(TransformerException exception) throws TransformerException { + exchange.setProperty(Exchange.XSLT_FATAL_ERROR, exception); throw exception; } public void warning(TransformerException exception) throws TransformerException { - LOG.warn("parser warning", exception); + exchange.setProperty(Exchange.XSLT_WARNING, exception); } } http://git-wip-us.apache.org/repos/asf/camel/blob/68d0f141/camel-core/src/main/java/org/apache/camel/builder/xml/XsltBuilder.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/builder/xml/XsltBuilder.java b/camel-core/src/main/java/org/apache/camel/builder/xml/XsltBuilder.java index 944e214..3803def 100644 --- a/camel-core/src/main/java/org/apache/camel/builder/xml/XsltBuilder.java +++ b/camel-core/src/main/java/org/apache/camel/builder/xml/XsltBuilder.java @@ -106,7 +106,7 @@ public class XsltBuilder implements Processor { Transformer transformer = getTransformer(); configureTransformer(transformer, exchange); - transformer.setErrorListener(new DefaultTransformErrorHandler()); + transformer.setErrorListener(new DefaultTransformErrorHandler(exchange)); ResultHandler resultHandler = resultHandlerFactory.createResult(exchange); Result result = resultHandler.getResult(); exchange.setProperty("isXalanTransformer", isXalanTransformer(transformer)); @@ -131,8 +131,6 @@ public class XsltBuilder implements Processor { LOG.trace("Transform complete with result {}", result); resultHandler.setBody(out); } finally { - // clean up the setting on the exchange - releaseTransformer(transformer); // IOHelper can handle if is is null IOHelper.close(is); @@ -409,6 +407,7 @@ public class XsltBuilder implements Processor { private void releaseTransformer(Transformer transformer) { if (transformers != null) { transformer.reset(); + transformer.setErrorListener(errorListener); transformers.offer(transformer); } } http://git-wip-us.apache.org/repos/asf/camel/blob/68d0f141/camel-core/src/test/data/terminate.xml ---------------------------------------------------------------------- diff --git a/camel-core/src/test/data/terminate.xml b/camel-core/src/test/data/terminate.xml new file mode 100644 index 0000000..3beae11 --- /dev/null +++ b/camel-core/src/test/data/terminate.xml @@ -0,0 +1,36 @@ +<?xml version="1.0"?> +<!-- + 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. +--> +<staff> + + <programmer> + <name>Bugs Bunny</name> + <dob>03/21/1970</dob> + <age>31</age> + <address>4895 Wabbit Hole Road</address> + <phone>865-111-1111</phone> + </programmer> + + <programmer> + <name>Daisy Duck</name> + <dob></dob> + <age></age> + <address>748 Golden Pond</address> + <phone>865-222-2222</phone> + </programmer> + +</staff> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/68d0f141/camel-core/src/test/java/org/apache/camel/component/xslt/XsltMessageTerminateTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/component/xslt/XsltMessageTerminateTest.java b/camel-core/src/test/java/org/apache/camel/component/xslt/XsltMessageTerminateTest.java new file mode 100644 index 0000000..3ccaa68 --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/component/xslt/XsltMessageTerminateTest.java @@ -0,0 +1,57 @@ +/** + * 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.xslt; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.Exchange; +import org.apache.camel.builder.RouteBuilder; + +public class XsltMessageTerminateTest extends ContextTestSupport { + + public void testXsltTerminate() throws Exception { + getMockEndpoint("mock:result").expectedMessageCount(0); + getMockEndpoint("mock:dead").expectedMessageCount(1); + + assertMockEndpointsSatisfied(); + + Exchange out = getMockEndpoint("mock:dead").getReceivedExchanges().get(0); + assertNotNull(out); + // this exception is just a generic xslt error + Exception cause = out.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class); + assertNotNull(cause); + + // we have the xsl termination message as a warning property on the exchange + Exception warning = out.getProperty(Exchange.XSLT_WARNING, Exception.class); + assertNotNull(warning); + assertEquals("Error: DOB is an empty string!", warning.getMessage()); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + errorHandler(deadLetterChannel("mock:dead")); + + from("file:src/test/data/?fileName=terminate.xml&noop=true") + .to("xslt:org/apache/camel/component/xslt/terminate.xsl") + .to("log:foo") + .to("mock:result"); + } + }; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/68d0f141/camel-core/src/test/resources/org/apache/camel/component/xslt/terminate.xsl ---------------------------------------------------------------------- diff --git a/camel-core/src/test/resources/org/apache/camel/component/xslt/terminate.xsl b/camel-core/src/test/resources/org/apache/camel/component/xslt/terminate.xsl new file mode 100644 index 0000000..d4d9860 --- /dev/null +++ b/camel-core/src/test/resources/org/apache/camel/component/xslt/terminate.xsl @@ -0,0 +1,34 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. +--> +<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> + + <xsl:template match="/"> + <html> + <body> + <xsl:for-each select="staff/programmer"> + <p>Name: <xsl:value-of select="name"/><br /> + <xsl:if test="dob=''"> + <xsl:message terminate="yes">Error: DOB is an empty string!</xsl:message> + </xsl:if> + </p> + </xsl:for-each> + </body> + </html> + </xsl:template> + +</xsl:stylesheet> \ No newline at end of file