camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r1044391 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/builder/xml/ camel-core/src/test/java/org/apache/camel/builder/xml/ components/camel-spring/src/test/java/org/apache/camel/component/xslt/ components/camel-spring/src/tes...
Date Fri, 10 Dec 2010 15:01:34 GMT
Author: davsclaus
Date: Fri Dec 10 15:01:34 2010
New Revision: 1044391

URL: http://svn.apache.org/viewvc?rev=1044391&view=rev
Log:
CAMEL-3400: Added option deleteOutputFile to xslt component.

Added:
    camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltOutputFileDeleteTest.java
      - copied, changed from r1044353, camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltOutputFileTest.java
    camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/XsltOutputFileDeleteTest.xml
      - copied, changed from r1044353, camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/XsltOutputFileTest.xml
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/XsltBuilder.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XsltBuilderTest.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/XsltBuilder.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/XsltBuilder.java?rev=1044391&r1=1044390&r2=1044391&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/XsltBuilder.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/XsltBuilder.java Fri
Dec 10 15:01:34 2010
@@ -39,6 +39,9 @@ import org.apache.camel.Message;
 import org.apache.camel.Processor;
 import org.apache.camel.RuntimeTransformException;
 import org.apache.camel.converter.jaxp.XmlConverter;
+import org.apache.camel.impl.SynchronizationAdapter;
+import org.apache.camel.util.ExchangeHelper;
+import org.apache.camel.util.FileUtil;
 
 import static org.apache.camel.util.ObjectHelper.notNull;
 
@@ -58,6 +61,7 @@ public class XsltBuilder implements Proc
     private ResultHandlerFactory resultHandlerFactory = new StringResultHandlerFactory();
     private boolean failOnNullBody = true;
     private URIResolver uriResolver;
+    private boolean deleteOutputFile;
 
     public XsltBuilder() {
     }
@@ -74,6 +78,12 @@ public class XsltBuilder implements Proc
     public void process(Exchange exchange) throws Exception {
         notNull(getTemplate(), "template");
 
+        if (isDeleteOutputFile()) {
+            // add on completion so we can delete the file when the Exchange is done
+            String fileName = ExchangeHelper.getMandatoryHeader(exchange, Exchange.XSLT_FILE_NAME,
String.class);
+            exchange.addOnCompletion(new XsltBuilderOnCompletion(fileName));
+        }
+
         Transformer transformer = getTemplate().newTransformer();
         configureTransformer(transformer, exchange);
         Source source = getSource(exchange);
@@ -165,6 +175,16 @@ public class XsltBuilder implements Proc
         return this;
     }
 
+    /**
+     * Should the output file be deleted when the {@link Exchange} is done.
+     * <p/>
+     * This option should only be used if you use {@link #outputFile()} as well.
+     */
+    public XsltBuilder deleteOutputFile() {
+        this.deleteOutputFile = true;
+        return this;
+    }
+
     public XsltBuilder parameter(String name, Object value) {
         parameters.put(name, value);
         return this;
@@ -277,6 +297,14 @@ public class XsltBuilder implements Proc
         this.uriResolver = uriResolver;
     }
 
+    public boolean isDeleteOutputFile() {
+        return deleteOutputFile;
+    }
+
+    public void setDeleteOutputFile(boolean deleteOutputFile) {
+        this.deleteOutputFile = deleteOutputFile;
+    }
+
     // Implementation methods
     // -------------------------------------------------------------------------
 
@@ -330,4 +358,23 @@ public class XsltBuilder implements Proc
             }
         }
     }
+
+    private final class XsltBuilderOnCompletion extends SynchronizationAdapter {
+        private final String fileName;
+
+        private XsltBuilderOnCompletion(String fileName) {
+            this.fileName = fileName;
+        }
+
+        @Override
+        public void onDone(Exchange exchange) {
+            FileUtil.deleteFile(new File(fileName));
+        }
+
+        @Override
+        public String toString() {
+            return "XsltBuilderOnCompletion";
+        }
+    }
+
 }

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XsltBuilderTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XsltBuilderTest.java?rev=1044391&r1=1044390&r2=1044391&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XsltBuilderTest.java
(original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XsltBuilderTest.java
Fri Dec 10 15:01:34 2010
@@ -19,6 +19,7 @@ package org.apache.camel.builder.xml;
 import java.io.File;
 import java.io.FileInputStream;
 import java.net.URL;
+import java.util.List;
 import javax.xml.transform.Source;
 import javax.xml.transform.Templates;
 import javax.xml.transform.sax.SAXSource;
@@ -31,6 +32,8 @@ import org.apache.camel.Exchange;
 import org.apache.camel.ExpectedBodyTypeException;
 import org.apache.camel.converter.jaxp.XmlConverter;
 import org.apache.camel.impl.DefaultExchange;
+import org.apache.camel.spi.Synchronization;
+import org.apache.camel.util.UnitOfWorkHelper;
 
 /**
  * @version $Revision$
@@ -214,6 +217,36 @@ public class XsltBuilderTest extends Con
         assertTrue(body.endsWith("<goodbye>world!</goodbye>"));
     }
 
+    public void testXsltOutputFileDelete() throws Exception {
+        // directory must exists
+        deleteDirectory("target/xslt");
+        createDirectory("target/xslt");
+
+        URL styleSheet = getClass().getResource("example.xsl");
+
+        XsltBuilder builder = XsltBuilder.xslt(styleSheet).outputFile().deleteOutputFile();
+
+        Exchange exchange = new DefaultExchange(context);
+        exchange.getIn().setBody("<hello>world!</hello>");
+        exchange.getIn().setHeader(Exchange.XSLT_FILE_NAME, "target/xslt/xsltout.xml");
+
+        builder.process(exchange);
+        assertIsInstanceOf(File.class, exchange.getOut().getBody());
+
+        File file = new File("target/xslt/xsltout.xml").getAbsoluteFile();
+        assertTrue("Output file should exist", file.exists());
+
+        String body = exchange.getOut().getBody(String.class);
+        assertTrue(body.endsWith("<goodbye>world!</goodbye>"));
+
+        // now done the exchange
+        List<Synchronization> onCompletions = exchange.handoverCompletions();
+        UnitOfWorkHelper.doneSynchronizations(exchange, onCompletions, log);
+
+        // the file should be deleted
+        assertFalse("Output file should be deleted", file.exists());
+    }
+
     public void testXsltSetConverter() throws Exception {
         URL styleSheet = getClass().getResource("example.xsl");
 

Copied: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltOutputFileDeleteTest.java
(from r1044353, camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltOutputFileTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltOutputFileDeleteTest.java?p2=camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltOutputFileDeleteTest.java&p1=camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltOutputFileTest.java&r1=1044353&r2=1044391&rev=1044391&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltOutputFileTest.java
(original)
+++ camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltOutputFileDeleteTest.java
Fri Dec 10 15:01:34 2010
@@ -30,33 +30,25 @@ import static org.apache.camel.spring.pr
 /**
  * @version $Revision$
  */
-public class XsltOutputFileTest extends ContextTestSupport {
+public class XsltOutputFileDeleteTest extends ContextTestSupport {
 
     protected CamelContext createCamelContext() throws Exception {
-        return createSpringCamelContext(this, "org/apache/camel/component/xslt/XsltOutputFileTest.xml");
+        return createSpringCamelContext(this, "org/apache/camel/component/xslt/XsltOutputFileDeleteTest.xml");
     }
 
-    public void testXsltOutput() throws Exception {
+    public void testXsltOutputDeleteFile() throws Exception {
         createDirectory("target/xslt");
 
         MockEndpoint mock = getMockEndpoint("mock:result");
         mock.expectedBodiesReceived("<?xml version=\"1.0\" encoding=\"UTF-8\"?><goodbye>world!</goodbye>");
-        mock.expectedFileExists("target/xslt/xsltme.xml");
-        mock.message(0).body().isInstanceOf(File.class);
-
         template.sendBodyAndHeader("direct:start", "<hello>world!</hello>", Exchange.XSLT_FILE_NAME,
"target/xslt/xsltme.xml");
 
         assertMockEndpointsSatisfied();
-    }
+        oneExchangeDone.matchesMockWaitTime();
 
-    public void testXsltOutputFileMissingHeader() throws Exception {
-        try {
-            template.sendBody("direct:start", "<hello>world!</hello>");
-            fail("Should thrown exception");
-        } catch (CamelExecutionException e) {
-            NoSuchHeaderException nshe = assertIsInstanceOf(NoSuchHeaderException.class,
e.getCause());
-            assertEquals(Exchange.XSLT_FILE_NAME, nshe.getHeaderName());
-        }
+        // assert file deleted
+        File file = new File("target/xslt/xsltme.xml").getAbsoluteFile();
+        assertFalse("File should be deleted", file.exists());
     }
 
 }
\ No newline at end of file

Copied: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/XsltOutputFileDeleteTest.xml
(from r1044353, camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/XsltOutputFileTest.xml)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/XsltOutputFileDeleteTest.xml?p2=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/XsltOutputFileDeleteTest.xml&p1=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/XsltOutputFileTest.xml&r1=1044353&r2=1044391&rev=1044391&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/XsltOutputFileTest.xml
(original)
+++ camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/XsltOutputFileDeleteTest.xml
Fri Dec 10 15:01:34 2010
@@ -25,7 +25,7 @@
     <camelContext xmlns="http://camel.apache.org/schema/spring">
         <route>
             <from uri="direct:start"/>
-            <to uri="xslt:org/apache/camel/component/xslt/example.xsl?output=file"/>
+            <to uri="xslt:org/apache/camel/component/xslt/example.xsl?output=file&amp;deleteOutputFile=true"/>
             <to uri="mock:result"/>
         </route>
     </camelContext>



Mime
View raw message