Author: janstey
Date: Wed Nov 16 18:09:22 2011
New Revision: 1202819

URL: http://svn.apache.org/viewvc?rev=1202819&view=rev
Log:
CAMEL-4689 - Add option to override XSLT stylesheet used via header

Added:
    
camel/trunk/camel-core/src/main/java/org/apache/camel/component/xslt/XsltConstants.java
    
camel/trunk/camel-core/src/test/java/org/apache/camel/component/xslt/XsltFromHeaderOverrideEndpointUriTest.java
    
camel/trunk/camel-core/src/test/java/org/apache/camel/component/xslt/XsltFromHeaderTest.java
    
camel/trunk/camel-core/src/test/resources/org/apache/camel/component/xslt/transform_to_foo.xsl
Modified:
    
camel/trunk/camel-core/src/main/java/org/apache/camel/component/xslt/XsltEndpoint.java
    
camel/trunk/camel-core/src/test/java/org/apache/camel/component/xslt/InvalidXsltFileTest.java
    
camel/trunk/camel-core/src/test/java/org/apache/camel/component/xslt/XsltFileNotFoundTest.java

Added: 
camel/trunk/camel-core/src/main/java/org/apache/camel/component/xslt/XsltConstants.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/xslt/XsltConstants.java?rev=1202819&view=auto
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/component/xslt/XsltConstants.java
 (added)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/component/xslt/XsltConstants.java
 Wed Nov 16 18:09:22 2011
@@ -0,0 +1,29 @@
+/**
+ * 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;
+
+/**
+ * XSLT component constants.
+ */
+public final class XsltConstants {
+
+    public static final String XSLT_RESOURCE_URI = "CamelXsltResourceUri";
+
+    private XsltConstants() {
+        // Utility class
+    }
+}

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/component/xslt/XsltEndpoint.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/xslt/XsltEndpoint.java?rev=1202819&r1=1202818&r2=1202819&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/component/xslt/XsltEndpoint.java
 (original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/component/xslt/XsltEndpoint.java
 Wed Nov 16 18:09:22 2011
@@ -48,7 +48,9 @@ public class XsltEndpoint extends Proces
         this.xslt = xslt;
         this.resourceUri = resourceUri;
         this.cacheStylesheet = cacheStylesheet;
-        loadResource(xslt, resourceUri);
+        
+        // load stylesheet on first exchange
+        clearCachedStylesheet();
     }
 
     @ManagedOperation(description = "Clears the cached XSLT stylesheet, 
forcing to re-load the stylesheet on next request")
@@ -70,12 +72,28 @@ public class XsltEndpoint extends Proces
         cacheCleared = false;
     }
 
+    public XsltEndpoint findOrCreateEndpoint(String uri, String 
newResourceUri) {
+        String newUri = uri.replace(resourceUri, newResourceUri);
+        LOG.trace("Getting endpoint with URI: {}", newUri);
+        return (XsltEndpoint) getCamelContext().getEndpoint(newUri);
+    }
+    
     @Override
     protected void onExchange(Exchange exchange) throws Exception {
-        if (!cacheStylesheet || cacheCleared) {
-            loadResource(xslt, resourceUri);
-        }    
-        super.onExchange(exchange);
+        String newResourceUri = 
exchange.getIn().getHeader(XsltConstants.XSLT_RESOURCE_URI, String.class);
+        if (newResourceUri != null) {
+            exchange.getIn().removeHeader(XsltConstants.XSLT_RESOURCE_URI);
+
+            LOG.trace("{} set to {} creating new endpoint to handle exchange", 
XsltConstants.XSLT_RESOURCE_URI, newResourceUri);
+            XsltEndpoint newEndpoint = findOrCreateEndpoint(getEndpointUri(), 
newResourceUri);
+            newEndpoint.onExchange(exchange);
+            return;
+        } else {
+            if (!cacheStylesheet || cacheCleared) {
+                loadResource(xslt, resourceUri);
+            }    
+            super.onExchange(exchange);
+        }
     }
 
 }

Modified: 
camel/trunk/camel-core/src/test/java/org/apache/camel/component/xslt/InvalidXsltFileTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/xslt/InvalidXsltFileTest.java?rev=1202819&r1=1202818&r2=1202819&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/test/java/org/apache/camel/component/xslt/InvalidXsltFileTest.java
 (original)
+++ 
camel/trunk/camel-core/src/test/java/org/apache/camel/component/xslt/InvalidXsltFileTest.java
 Wed Nov 16 18:09:22 2011
@@ -16,36 +16,31 @@
  */
 package org.apache.camel.component.xslt;
 
-import org.apache.camel.CamelContext;
-import org.apache.camel.FailedToCreateRouteException;
-import org.apache.camel.ResolveEndpointFailedException;
-import org.apache.camel.TestSupport;
+import javax.xml.transform.TransformerConfigurationException;
+
+import org.apache.camel.CamelExecutionException;
+import org.apache.camel.ContextTestSupport;
 import org.apache.camel.builder.RouteBuilder;
-import org.apache.camel.impl.DefaultCamelContext;
 
 /**
  *
  */
-public class InvalidXsltFileTest extends TestSupport {
+public class InvalidXsltFileTest extends ContextTestSupport {
 
     public void testInvalidStylesheet() throws Exception {
         try {
-            RouteBuilder builder = createRouteBuilder();
-            CamelContext context = new DefaultCamelContext();
-            context.addRoutes(builder);
-            context.start();
-
+            template.sendBody("direct:a", "foo");
             fail("Should have thrown an exception due XSL compilation error");
-        } catch (FailedToCreateRouteException e) {
+        } catch (CamelExecutionException e) {
             // expected
-            assertIsInstanceOf(ResolveEndpointFailedException.class, 
e.getCause());
-        }
+            assertIsInstanceOf(TransformerConfigurationException.class, 
e.getCause());            
+        }        
     }
 
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {
             public void configure() throws Exception {
-                
from("seda:a").to("xslt:org/apache/camel/component/xslt/invalid.xsl");
+                
from("direct:a").to("xslt:org/apache/camel/component/xslt/invalid.xsl");
             }
         };
     }

Modified: 
camel/trunk/camel-core/src/test/java/org/apache/camel/component/xslt/XsltFileNotFoundTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/xslt/XsltFileNotFoundTest.java?rev=1202819&r1=1202818&r2=1202819&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/test/java/org/apache/camel/component/xslt/XsltFileNotFoundTest.java
 (original)
+++ 
camel/trunk/camel-core/src/test/java/org/apache/camel/component/xslt/XsltFileNotFoundTest.java
 Wed Nov 16 18:09:22 2011
@@ -17,39 +17,30 @@
 package org.apache.camel.component.xslt;
 
 import java.io.FileNotFoundException;
-import javax.xml.transform.TransformerConfigurationException;
 
-import org.apache.camel.CamelContext;
-import org.apache.camel.FailedToCreateRouteException;
-import org.apache.camel.ResolveEndpointFailedException;
-import org.apache.camel.TestSupport;
+import org.apache.camel.CamelExecutionException;
+import org.apache.camel.ContextTestSupport;
 import org.apache.camel.builder.RouteBuilder;
-import org.apache.camel.impl.DefaultCamelContext;
 
 /**
  *
  */
-public class XsltFileNotFoundTest extends TestSupport {
+public class XsltFileNotFoundTest extends ContextTestSupport {
 
     public void testNoXsltFile() throws Exception {
         try {
-            RouteBuilder builder = createRouteBuilder();
-            CamelContext context = new DefaultCamelContext();
-            context.addRoutes(builder);
-            context.start();
-
+            template.sendBody("direct:a", "foo");
             fail("Should have thrown an exception due XSLT file not found");
-        } catch (FailedToCreateRouteException e) {
+        } catch (CamelExecutionException e) {
             // expected
-            assertIsInstanceOf(ResolveEndpointFailedException.class, 
e.getCause());
-            assertIsInstanceOf(FileNotFoundException.class, 
e.getCause().getCause());
+            assertIsInstanceOf(FileNotFoundException.class, e.getCause());
         }
     }
 
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {
             public void configure() throws Exception {
-                
from("seda:a").to("xslt:org/apache/camel/component/xslt/notfound.xsl");
+                
from("direct:a").to("xslt:org/apache/camel/component/xslt/notfound.xsl");
             }
         };
     }

Added: 
camel/trunk/camel-core/src/test/java/org/apache/camel/component/xslt/XsltFromHeaderOverrideEndpointUriTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/xslt/XsltFromHeaderOverrideEndpointUriTest.java?rev=1202819&view=auto
==============================================================================
--- 
camel/trunk/camel-core/src/test/java/org/apache/camel/component/xslt/XsltFromHeaderOverrideEndpointUriTest.java
 (added)
+++ 
camel/trunk/camel-core/src/test/java/org/apache/camel/component/xslt/XsltFromHeaderOverrideEndpointUriTest.java
 Wed Nov 16 18:09:22 2011
@@ -0,0 +1,80 @@
+/**
+ * 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.util.List;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.impl.JndiRegistry;
+
+/**
+ *
+ */
+public class XsltFromHeaderOverrideEndpointUriTest extends ContextTestSupport {
+
+    public void testSendMessageAndHaveItTransformed() throws Exception {
+        MockEndpoint endpoint = getMockEndpoint("mock:result");
+        endpoint.expectedMessageCount(1);
+
+        template.sendBodyAndHeader("direct:start",
+                "<mail><subject>Hey</subject><body>Hello world!</body></mail>",
+                XsltConstants.XSLT_RESOURCE_URI, 
"org/apache/camel/component/xslt/transform_to_foo.xsl");        
+        
+        assertMockEndpointsSatisfied();
+
+        List<Exchange> list = endpoint.getReceivedExchanges();
+        Exchange exchange = list.get(0);
+        String xml = exchange.getIn().getBody(String.class);
+
+        assertNotNull("The transformed XML should not be null", xml);
+        assertTrue(xml.indexOf("transformed") > -1);
+        // the foo tag is in the transform_to_foo.xsl which is what we want. 
If this didn't
+        // work then transform.xsl would be used and we'd have a cheese element
+        assertTrue(xml.indexOf("foo") > -1);
+        assertTrue(xml.indexOf("<subject>Hey</subject>") > -1);
+        assertTrue(xml.indexOf("<body>Hello world!</body>") > -1);
+
+        TestBean bean = context.getRegistry().lookup("testBean", 
TestBean.class);
+        assertNotNull(bean);
+        assertEquals("bean.subject", "Hey", bean.getSubject());
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start")
+                    .to("xslt:org/apache/camel/component/xslt/transform.xsl") 
+                    .multicast()
+                        .beanRef("testBean")
+                        .to("mock:result");
+            }
+        };
+    }
+
+    @Override
+    protected JndiRegistry createRegistry() throws Exception {
+        JndiRegistry jndi = super.createRegistry();
+        jndi.bind("testBean", new TestBean());
+        return jndi;
+    }
+
+}

Added: 
camel/trunk/camel-core/src/test/java/org/apache/camel/component/xslt/XsltFromHeaderTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/xslt/XsltFromHeaderTest.java?rev=1202819&view=auto
==============================================================================
--- 
camel/trunk/camel-core/src/test/java/org/apache/camel/component/xslt/XsltFromHeaderTest.java
 (added)
+++ 
camel/trunk/camel-core/src/test/java/org/apache/camel/component/xslt/XsltFromHeaderTest.java
 Wed Nov 16 18:09:22 2011
@@ -0,0 +1,79 @@
+/**
+ * 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.util.List;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.impl.JndiRegistry;
+
+/**
+ *
+ */
+public class XsltFromHeaderTest extends ContextTestSupport {
+
+    public void testSendMessageAndHaveItTransformed() throws Exception {
+        MockEndpoint endpoint = getMockEndpoint("mock:result");
+        endpoint.expectedMessageCount(1);
+
+        template.sendBodyAndHeader("direct:start",
+                "<mail><subject>Hey</subject><body>Hello world!</body></mail>",
+                XsltConstants.XSLT_RESOURCE_URI, 
"org/apache/camel/component/xslt/transform.xsl");        
+        
+        assertMockEndpointsSatisfied();
+
+        List<Exchange> list = endpoint.getReceivedExchanges();
+        Exchange exchange = list.get(0);
+        String xml = exchange.getIn().getBody(String.class);
+
+        assertNotNull("The transformed XML should not be null", xml);
+        assertTrue(xml.indexOf("transformed") > -1);
+        // the cheese tag is in the transform.xsl
+        assertTrue(xml.indexOf("cheese") > -1);
+        assertTrue(xml.indexOf("<subject>Hey</subject>") > -1);
+        assertTrue(xml.indexOf("<body>Hello world!</body>") > -1);
+
+        TestBean bean = context.getRegistry().lookup("testBean", 
TestBean.class);
+        assertNotNull(bean);
+        assertEquals("bean.subject", "Hey", bean.getSubject());
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start")
+                    .to("xslt:dummy") // real xsl should come from header
+                    .multicast()
+                        .beanRef("testBean")
+                        .to("mock:result");
+            }
+        };
+    }
+
+    @Override
+    protected JndiRegistry createRegistry() throws Exception {
+        JndiRegistry jndi = super.createRegistry();
+        jndi.bind("testBean", new TestBean());
+        return jndi;
+    }
+
+}

Added: 
camel/trunk/camel-core/src/test/resources/org/apache/camel/component/xslt/transform_to_foo.xsl
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/resources/org/apache/camel/component/xslt/transform_to_foo.xsl?rev=1202819&view=auto
==============================================================================
--- 
camel/trunk/camel-core/src/test/resources/org/apache/camel/component/xslt/transform_to_foo.xsl
 (added)
+++ 
camel/trunk/camel-core/src/test/resources/org/apache/camel/component/xslt/transform_to_foo.xsl
 Wed Nov 16 18:09:22 2011
@@ -0,0 +1,39 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<!--
+    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
+  xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
+  version='1.0'>
+
+  <xsl:output method="xml" indent="yes" encoding="ISO-8859-1"/>
+
+  <xsl:template match="/">
+    <transformed subject="{/mail/subject}">
+      <foo>
+        <xsl:apply-templates select="*|@*"/>
+      </foo>
+    </transformed>
+  </xsl:template>
+
+  <xsl:template match="*">
+    <xsl:copy>
+      <xsl:copy-of select="attribute::*"/>
+      <xsl:apply-templates/>
+    </xsl:copy>
+  </xsl:template>
+
+</xsl:stylesheet>


Reply via email to