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/08/08 11:44:14 UTC

svn commit: r1154892 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/component/xslt/ camel-core/src/main/java/org/apache/camel/util/ components/camel-flatpack/src/main/java/org/apache/camel/component/flatpack/ tests/camel-itest/src/test/ja...

Author: davsclaus
Date: Mon Aug  8 09:44:13 2011
New Revision: 1154892

URL: http://svn.apache.org/viewvc?rev=1154892&view=rev
Log:
CAMEL-4031: Added test for loading xslt template over HTTP.

Added:
    camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettyXsltHttpTemplateTest.java
    camel/trunk/tests/camel-itest/src/test/resources/org/apache/camel/itest/jetty/transform.xsl
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/xslt/XsltComponent.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/util/ResourceHelper.java
    camel/trunk/components/camel-flatpack/src/main/java/org/apache/camel/component/flatpack/FixedLengthEndpoint.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/xslt/XsltComponent.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/xslt/XsltComponent.java?rev=1154892&r1=1154891&r2=1154892&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/xslt/XsltComponent.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/xslt/XsltComponent.java Mon Aug  8 09:44:13 2011
@@ -18,6 +18,7 @@ package org.apache.camel.component.xslt;
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.net.URL;
 import java.util.Map;
 import javax.xml.transform.TransformerConfigurationException;
 import javax.xml.transform.TransformerFactory;
@@ -151,8 +152,9 @@ public class XsltComponent extends Defau
 
     private void loadResource(XsltBuilder xslt, String resourceUri) throws TransformerConfigurationException, IOException {
         LOG.trace("{} loading schema resource: {}", this, resourceUri);
-        InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(getCamelContext().getClassResolver(), resourceUri);
-        xslt.setTransformerInputStream(is);
+        // prefer to use URL over InputStream as it loads better with http
+        URL url = ResourceHelper.resolveMandatoryResourceAsUrl(getCamelContext().getClassResolver(), resourceUri);
+        xslt.setTransformerURL(url);
     }
 
     protected void configureXslt(XsltBuilder xslt, String uri, String remaining, Map<String, Object> parameters) throws Exception {

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/util/ResourceHelper.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ResourceHelper.java?rev=1154892&r1=1154891&r2=1154892&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/ResourceHelper.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/ResourceHelper.java Mon Aug  8 09:44:13 2011
@@ -18,9 +18,12 @@ package org.apache.camel.util;
 
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
+import java.io.IOException;
 import java.io.InputStream;
+import java.net.HttpURLConnection;
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.net.URLConnection;
 
 import org.apache.camel.spi.ClassResolver;
 
@@ -35,16 +38,33 @@ public final class ResourceHelper {
 
     /**
      * Resolves the mandatory resource.
+     * <p/>
+     * If possible prefer to use {@link #resolveMandatoryResourceAsUrl(org.apache.camel.spi.ClassResolver, String)}
+     * if possible.
      *
      * @param classResolver the class resolver to load the resource from the classpath
      * @param uri uri of the resource
      * @return the resource as an {@link InputStream}, remember to close the stream after usage.
-     * @throws java.io.FileNotFoundException is thrown if the resource file could not be found
+     * @throws java.io.IOException is thrown if the resource file could not be found or loaded as {@link InputStream}
      */
-    public static InputStream resolveMandatoryResourceAsInputStream(ClassResolver classResolver, String uri) throws FileNotFoundException {
+    public static InputStream resolveMandatoryResourceAsInputStream(ClassResolver classResolver, String uri) throws IOException {
         if (uri.startsWith("file:")) {
             uri = ObjectHelper.after(uri, "file:");
             return new FileInputStream(uri);
+        } else if (uri.startsWith("http:")) {
+            URL url = new URL(uri);
+            URLConnection con = url.openConnection();
+            con.setUseCaches(false);
+            try {
+                return con.getInputStream();
+            } catch (IOException e) {
+                // close the http connection to avoid
+                // leaking gaps in case of an exception
+                if (con instanceof HttpURLConnection) {
+                    ((HttpURLConnection) con).disconnect();
+                }
+                throw e;
+            }
         } else if (uri.startsWith("classpath:")) {
             uri = ObjectHelper.after(uri, "classpath:");
         }
@@ -70,6 +90,8 @@ public final class ResourceHelper {
     public static URL resolveMandatoryResourceAsUrl(ClassResolver classResolver,  String uri) throws FileNotFoundException, MalformedURLException {
         if (uri.startsWith("file:")) {
             return new URL(uri);
+        } else if (uri.startsWith("http:")) {
+            return new URL(uri);
         } else if (uri.startsWith("classpath:")) {
             uri = ObjectHelper.after(uri, "classpath:");
         }

Modified: camel/trunk/components/camel-flatpack/src/main/java/org/apache/camel/component/flatpack/FixedLengthEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-flatpack/src/main/java/org/apache/camel/component/flatpack/FixedLengthEndpoint.java?rev=1154892&r1=1154891&r2=1154892&view=diff
==============================================================================
--- camel/trunk/components/camel-flatpack/src/main/java/org/apache/camel/component/flatpack/FixedLengthEndpoint.java (original)
+++ camel/trunk/components/camel-flatpack/src/main/java/org/apache/camel/component/flatpack/FixedLengthEndpoint.java Mon Aug  8 09:44:13 2011
@@ -80,7 +80,7 @@ public class FixedLengthEndpoint extends
         Exchange answer = createExchange();
         Message in = answer.getIn();
         in.setBody(dataSet);
-        in.setHeader("camelFlatpackCounter", counter);
+        in.setHeader("CamelFlatpackCounter", counter);
         return answer;
     }
 

Added: camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettyXsltHttpTemplateTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettyXsltHttpTemplateTest.java?rev=1154892&view=auto
==============================================================================
--- camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettyXsltHttpTemplateTest.java (added)
+++ camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettyXsltHttpTemplateTest.java Mon Aug  8 09:44:13 2011
@@ -0,0 +1,55 @@
+/**
+ * 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.itest.jetty;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+/**
+ *
+ */
+public class JettyXsltHttpTemplateTest extends CamelTestSupport {
+
+    @Test
+    public void testXsltHttpTemplate() throws Exception {
+        // give Jetty a bit time to startup and be ready
+        Thread.sleep(1000);
+
+        String xml = template.requestBody("xslt:http://0.0.0.0:8227/myxslt",
+                "<mail><subject>Hey</subject><body>Hello world!</body></mail>", 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);
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("jetty:http://0.0.0.0:8227/myxslt")
+                    .pollEnrich("file://src/test/resources/org/apache/camel/itest/jetty/?fileName=transform.xsl&noop=true&readLock=none");
+            }
+        };
+    }
+
+}

Added: camel/trunk/tests/camel-itest/src/test/resources/org/apache/camel/itest/jetty/transform.xsl
URL: http://svn.apache.org/viewvc/camel/trunk/tests/camel-itest/src/test/resources/org/apache/camel/itest/jetty/transform.xsl?rev=1154892&view=auto
==============================================================================
--- camel/trunk/tests/camel-itest/src/test/resources/org/apache/camel/itest/jetty/transform.xsl (added)
+++ camel/trunk/tests/camel-itest/src/test/resources/org/apache/camel/itest/jetty/transform.xsl Mon Aug  8 09:44:13 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}">
+      <cheese>
+        <xsl:apply-templates select="*|@*"/>
+      </cheese>
+    </transformed>
+  </xsl:template>
+
+  <xsl:template match="*">
+    <xsl:copy>
+      <xsl:copy-of select="attribute::*"/>
+      <xsl:apply-templates/>
+    </xsl:copy>
+  </xsl:template>
+
+</xsl:stylesheet>