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 2011/10/21 20:33:40 UTC

svn commit: r1187487 - in /camel/branches/camel-2.8.x: ./ camel-core/src/main/java/org/apache/camel/builder/xml/XsltBuilder.java components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltFromFileExceptionTest.java

Author: davsclaus
Date: Fri Oct 21 18:33:40 2011
New Revision: 1187487

URL: http://svn.apache.org/viewvc?rev=1187487&view=rev
Log:
CAMEL-4560: xslt component will now ensure underlying stream is closed for Source. For example on windows this would otherwise lock files.

Added:
    camel/branches/camel-2.8.x/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltFromFileExceptionTest.java   (with props)
Modified:
    camel/branches/camel-2.8.x/   (props changed)
    camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/builder/xml/XsltBuilder.java

Propchange: camel/branches/camel-2.8.x/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Fri Oct 21 18:33:40 2011
@@ -1 +1 @@
-/camel/trunk:1186106,1186625,1186772,1187221
+/camel/trunk:1186106,1186625,1186772,1187221,1187485

Propchange: camel/branches/camel-2.8.x/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/builder/xml/XsltBuilder.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/builder/xml/XsltBuilder.java?rev=1187487&r1=1187486&r2=1187487&view=diff
==============================================================================
--- camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/builder/xml/XsltBuilder.java (original)
+++ camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/builder/xml/XsltBuilder.java Fri Oct 21 18:33:40 2011
@@ -30,9 +30,9 @@ import javax.xml.transform.Source;
 import javax.xml.transform.Templates;
 import javax.xml.transform.Transformer;
 import javax.xml.transform.TransformerConfigurationException;
-import javax.xml.transform.TransformerException;
 import javax.xml.transform.TransformerFactory;
 import javax.xml.transform.URIResolver;
+import javax.xml.transform.stax.StAXSource;
 import javax.xml.transform.stream.StreamSource;
 
 import org.apache.camel.Exchange;
@@ -45,6 +45,7 @@ import org.apache.camel.converter.jaxp.X
 import org.apache.camel.impl.SynchronizationAdapter;
 import org.apache.camel.util.ExchangeHelper;
 import org.apache.camel.util.FileUtil;
+import org.apache.camel.util.IOHelper;
 
 import static org.apache.camel.util.ObjectHelper.notNull;
 
@@ -52,7 +53,7 @@ import static org.apache.camel.util.Obje
  * Creates a <a href="http://camel.apache.org/processor.html">Processor</a>
  * which performs an XSLT transformation of the IN message body.
  * <p/>
- * Will by defult output the result as a String. You can chose which kind of output
+ * Will by default output the result as a String. You can chose which kind of output
  * you want using the <tt>outputXXX</tt> methods.
  *
  * @version 
@@ -91,7 +92,6 @@ public class XsltBuilder implements Proc
         Transformer transformer = getTemplate().newTransformer();
         configureTransformer(transformer, exchange);
         transformer.setErrorListener(new DefaultTransformErrorHandler());
-        Source source = getSource(exchange);
         ResultHandler resultHandler = resultHandlerFactory.createResult(exchange);
         Result result = resultHandler.getResult();
 
@@ -99,8 +99,16 @@ public class XsltBuilder implements Proc
         Message out = exchange.getOut();
         out.copyFrom(exchange.getIn());
 
-        transformer.transform(source, result);
-        resultHandler.setBody(out);
+        // the underlying input stream, which we need to close to avoid locking files or other resources
+        InputStream is = null;
+        try {
+            is = exchange.getIn().getBody(InputStream.class);
+            Source source = getSource(exchange, is);
+            transformer.transform(source, result);
+            resultHandler.setBody(out);
+        } finally {
+            IOHelper.close(is);
+        }
     }
 
     // Builder methods
@@ -323,11 +331,17 @@ public class XsltBuilder implements Proc
     // -------------------------------------------------------------------------
 
     /**
-     * Converts the inbound body to a {@link Source}
+     * Converts the inbound stream to a {@link Source}.
+     * <p/>
+     * This implementation will prefer StAX first, and fallback to other kinds of Source types.
      */
-    protected Source getSource(Exchange exchange) {
-        Message in = exchange.getIn();
-        Source source = in.getBody(Source.class);
+    protected Source getSource(Exchange exchange, InputStream is) {
+        // try StAX first
+        Source source = exchange.getContext().getTypeConverter().convertTo(StAXSource.class, exchange, is);
+        if (source == null) {
+            // fallback and try other kind of source
+            source = exchange.getContext().getTypeConverter().convertTo(StAXSource.class, exchange, is);
+        }
         if (source == null) {
             if (isFailOnNullBody()) {
                 throw new ExpectedBodyTypeException(exchange, Source.class);

Added: camel/branches/camel-2.8.x/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltFromFileExceptionTest.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.8.x/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltFromFileExceptionTest.java?rev=1187487&view=auto
==============================================================================
--- camel/branches/camel-2.8.x/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltFromFileExceptionTest.java (added)
+++ camel/branches/camel-2.8.x/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltFromFileExceptionTest.java Fri Oct 21 18:33:40 2011
@@ -0,0 +1,85 @@
+/**
+ * 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 java.io.File;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+
+/**
+ *
+ */
+public class XsltFromFileExceptionTest extends ContextTestSupport {
+
+    @Override
+    protected void setUp() throws Exception {
+        deleteDirectory("target/xslt");
+        super.setUp();
+    }
+
+    public void testXsltFromFileExceptionOk() throws Exception {
+        getMockEndpoint("mock:result").expectedMessageCount(1);
+        getMockEndpoint("mock:error").expectedMessageCount(0);
+
+        template.sendBodyAndHeader("file:target/xslt", "<hello>world!</hello>", Exchange.FILE_NAME, "hello.xml");
+
+        assertMockEndpointsSatisfied();
+
+        oneExchangeDone.matchesMockWaitTime();
+
+        File file = new File("target/xslt/hello.xml");
+        assertFalse("File should not exists " + file, file.exists());
+
+        file = new File("target/xslt/ok/hello.xml");
+        assertTrue("File should exists " + file, file.exists());
+    }
+
+    public void testXsltFromFileExceptionFail() throws Exception {
+        getMockEndpoint("mock:result").expectedMessageCount(0);
+        getMockEndpoint("mock:error").expectedMessageCount(1);
+
+        // the last tag is not ended properly
+        template.sendBodyAndHeader("file:target/xslt", "<hello>world!</hello", Exchange.FILE_NAME, "hello2.xml");
+
+        assertMockEndpointsSatisfied();
+
+        oneExchangeDone.matchesMockWaitTime();
+
+        File file = new File("target/xslt/hello2.xml");
+        assertFalse("File should not exists " + file, file.exists());
+
+        file = new File("target/xslt/error/hello2.xml");
+        assertTrue("File should exists " + file, file.exists());
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("file:target/xslt?moveFailed=error&move=ok")
+                    .onException(Exception.class)
+                        .to("mock:error")
+                    .end()
+                    .to("xslt:org/apache/camel/component/xslt/example.xsl")
+                    .to("mock:result");
+            }
+        };
+    }
+}
\ No newline at end of file

Propchange: camel/branches/camel-2.8.x/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltFromFileExceptionTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/branches/camel-2.8.x/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltFromFileExceptionTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date