camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r1331877 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/component/xslt/ camel-core/src/main/java/org/apache/camel/util/ camel-core/src/test/java/org/apache/camel/util/ tests/camel-itest/src/test/java/org/apache/camel/itest/jet...
Date Sun, 29 Apr 2012 08:27:10 GMT
Author: davsclaus
Date: Sun Apr 29 08:27:09 2012
New Revision: 1331877

URL: http://svn.apache.org/viewvc?rev=1331877&view=rev
Log:
CAMEL-5232: xslt component with http resource supports query parameters

Added:
    camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettyXsltTest.java
      - copied, changed from r1331871, camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettyValidatorTest.java
    camel/trunk/tests/camel-itest/src/test/resources/org/apache/camel/itest/jetty/greeting.xsl
      - copied, changed from r1331871, 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/camel-core/src/test/java/org/apache/camel/util/ResourceHelperTest.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=1331877&r1=1331876&r2=1331877&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
Sun Apr 29 08:27:09 2012
@@ -27,6 +27,7 @@ import org.apache.camel.builder.xml.Xslt
 import org.apache.camel.converter.jaxp.XmlConverter;
 import org.apache.camel.impl.DefaultComponent;
 import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.ResourceHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -65,7 +66,7 @@ public class XsltComponent extends Defau
     }
 
     protected Endpoint createEndpoint(String uri, final String remaining, Map<String,
Object> parameters) throws Exception {
-        final String resourceUri = remaining;
+        String resourceUri = remaining;
         LOG.debug("{} using schema resource: {}", this, resourceUri);
         final XsltBuilder xslt = getCamelContext().getInjector().newInstance(XsltBuilder.class);
 
@@ -98,19 +99,6 @@ public class XsltComponent extends Defau
             xslt.getConverter().setTransformerFactory(factory);
         }
 
-        // lookup custom resolver to use
-        URIResolver resolver = resolveAndRemoveReferenceParameter(parameters, "uriResolver",
URIResolver.class);
-        if (resolver == null) {
-            // not in endpoint then use component specific resolver
-            resolver = getUriResolver();
-        }
-        if (resolver == null) {
-            // fallback to use a Camel specific resolver
-            resolver = new XsltUriResolver(getCamelContext().getClassResolver(), remaining);
-        }
-        // set resolver before input stream as resolver is used when loading the input stream
-        xslt.setUriResolver(resolver);
-
         ResultHandlerFactory resultHandlerFactory = resolveAndRemoveReferenceParameter(parameters,
"resultHandlerFactory", ResultHandlerFactory.class);
         if (resultHandlerFactory != null) {
             xslt.setResultHandlerFactory(resultHandlerFactory);
@@ -123,11 +111,29 @@ public class XsltComponent extends Defau
         String output = getAndRemoveParameter(parameters, "output", String.class);
         configureOutput(xslt, output);
 
-        configureXslt(xslt, uri, remaining, parameters);
-
         // default to use the cache option from the component if the endpoint did not have
the contentCache parameter
         boolean cache = getAndRemoveParameter(parameters, "contentCache", Boolean.class,
contentCache);
 
+        configureXslt(xslt, uri, remaining, parameters);
+
+        // if its a http uri, then append additional parameters as they are part of the uri
+        if (ResourceHelper.isHttpUri(resourceUri)) {
+            resourceUri = ResourceHelper.appendParameters(resourceUri, parameters);
+        }
+
+        // lookup custom resolver to use
+        URIResolver resolver = resolveAndRemoveReferenceParameter(parameters, "uriResolver",
URIResolver.class);
+        if (resolver == null) {
+            // not in endpoint then use component specific resolver
+            resolver = getUriResolver();
+        }
+        if (resolver == null) {
+            // fallback to use a Camel specific resolver
+            resolver = new XsltUriResolver(getCamelContext().getClassResolver(), remaining);
+        }
+        // set resolver before input stream as resolver is used when loading the input stream
+        xslt.setUriResolver(resolver);
+
         return new XsltEndpoint(uri, this, xslt, resourceUri, cache);
     }
 

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=1331877&r1=1331876&r2=1331877&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 Sun Apr
29 08:27:09 2012
@@ -23,8 +23,11 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.net.HttpURLConnection;
 import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URISyntaxException;
 import java.net.URL;
 import java.net.URLConnection;
+import java.util.Map;
 
 import org.apache.camel.spi.ClassResolver;
 
@@ -125,4 +128,37 @@ public final class ResourceHelper {
         }
     }
 
+    /**
+     * Is the given uri a http uri?
+     *
+     * @param uri the uri
+     * @return <tt>true</tt> if the uri starts with <tt>http:</tt>
or <tt>https:</tt>
+     */
+    public static boolean isHttpUri(String uri) {
+        if (uri == null) {
+            return false;
+        }
+        return uri.startsWith("http:") || uri.startsWith("https:");
+    }
+
+    /**
+     * Appends the parameters to the given uri
+     *
+     * @param uri the uri
+     * @param parameters the additional parameters (will clear the map)
+     * @return a new uri with the additional parameters appended
+     * @throws URISyntaxException is thrown if the uri is invalid
+     */
+    public static String appendParameters(String uri, Map<String, Object> parameters)
throws URISyntaxException {
+        // add additional parameters to the resource uri
+        if (!parameters.isEmpty()) {
+            String query = URISupport.createQueryString(parameters);
+            URI u = new URI(uri);
+            u = URISupport.createURIWithQuery(u, query);
+            parameters.clear();
+            return u.toString();
+        } else {
+            return uri;
+        }
+    }
 }

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/util/ResourceHelperTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/util/ResourceHelperTest.java?rev=1331877&r1=1331876&r2=1331877&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/util/ResourceHelperTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/util/ResourceHelperTest.java Sun
Apr 29 08:27:09 2012
@@ -19,6 +19,8 @@ package org.apache.camel.util;
 import java.io.FileNotFoundException;
 import java.io.InputStream;
 import java.net.URL;
+import java.util.LinkedHashMap;
+import java.util.Map;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.TestSupport;
@@ -130,4 +132,23 @@ public class ResourceHelperTest extends 
         context.stop();
     }
 
+    public void testIsHttp() throws Exception {
+        assertFalse(ResourceHelper.isHttpUri("direct:foo"));
+        assertFalse(ResourceHelper.isHttpUri(""));
+        assertFalse(ResourceHelper.isHttpUri(null));
+
+        assertTrue(ResourceHelper.isHttpUri("http://camel.apache.org"));
+        assertTrue(ResourceHelper.isHttpUri("https://camel.apache.org"));
+    }
+
+    public void testAppendParameters() throws Exception {
+        Map<String, Object> params = new LinkedHashMap<String, Object>();
+        params.put("foo", 123);
+        params.put("bar", "yes");
+
+        // should clear the map after usage
+        assertEquals("http://localhost:8080/data?foo=123&bar=yes", ResourceHelper.appendParameters("http://localhost:8080/data",
params));
+        assertEquals(0, params.size());
+    }
+
 }

Copied: camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettyXsltTest.java
(from r1331871, camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettyValidatorTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettyXsltTest.java?p2=camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettyXsltTest.java&p1=camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettyValidatorTest.java&r1=1331871&r2=1331877&rev=1331877&view=diff
==============================================================================
--- camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettyValidatorTest.java
(original)
+++ camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettyXsltTest.java
Sun Apr 29 08:27:09 2012
@@ -18,33 +18,42 @@ package org.apache.camel.itest.jetty;
 
 import java.io.InputStream;
 
-import org.apache.camel.ValidationException;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.ResolveEndpointFailedException;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.test.AvailablePortFinder;
 import org.apache.camel.test.junit4.CamelTestSupport;
+import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.ResourceHelper;
 import org.junit.Test;
 
-public class JettyValidatorTest extends CamelTestSupport {
+public class JettyXsltTest extends CamelTestSupport {
 
     private int port;
 
     @Test
-    public void testValidRequest() throws Exception {
-        InputStream inputStream = this.getClass().getResourceAsStream("ValidRequest.xml");
-        assertNotNull("the inputStream should not be null", inputStream);
+    public void testClasspath() throws Exception {
+        String response = template.requestBody("xslt:org/apache/camel/itest/jetty/greeting.xsl",
"<hello>Camel</hello>", String.class);
 
-        String response = template.requestBody("http://localhost:" + port + "/test", inputStream,
String.class);
+        assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><goodbye>Camel</goodbye>",
response);
+    }
 
-        assertEquals("The response should be ok", response, "<ok/>");
+    @Test
+    public void testClasspathInvalidParameter() throws Exception {
+        try {
+            template.requestBody("xslt:org/apache/camel/itest/jetty/greeting.xsl?name=greeting.xsl",
"<hello>Camel</hello>", String.class);
+            fail("Should have thrown exception");
+        } catch (ResolveEndpointFailedException e) {
+            assertTrue(e.getMessage().endsWith("Unknown parameters=[{name=greeting.xsl}]"));
+        }
     }
 
     @Test
-    public void testInvalidRequest() throws Exception {
-        InputStream inputStream = this.getClass().getResourceAsStream("InvalidRequest.xml");
-        assertNotNull("the inputStream should not be null", inputStream);
+    public void testHttp() throws Exception {
+        String response = template.requestBody("xslt://http://localhost:" + port + "/test?name=greeting.xsl",
"<hello>Camel</hello>", String.class);
 
-        String response = template.requestBody("http://localhost:" + port + "/test", inputStream,
String.class);
-        assertEquals("The response should be error", response, "<error/>");
+        assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><goodbye>Camel</goodbye>",
response);
     }
 
     @Override
@@ -54,15 +63,20 @@ public class JettyValidatorTest extends 
         return new RouteBuilder() {
             public void configure() {
                 from("jetty:http://localhost:" + port + "/test")
-                    .convertBodyTo(String.class)
-                    .to("log:in")
-                    .doTry()
-                        .to("validator:OptimizationRequest.xsd")
-                        .transform(constant("<ok/>"))
-                    .doCatch(ValidationException.class)
-                        .transform(constant("<error/>"))
-                    .end()
-                    .to("log:out");
+                    .process(new Processor() {
+                        @Override
+                        public void process(Exchange exchange) throws Exception {
+                            String name = exchange.getIn().getHeader("name", String.class);
+                            ObjectHelper.notNull(name, "name");
+
+                            name = "org/apache/camel/itest/jetty/" + name;
+                            InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(exchange.getContext().getClassResolver(),
name);
+                            String xml = exchange.getContext().getTypeConverter().convertTo(String.class,
is);
+
+                            exchange.getOut().setBody(xml);
+                            exchange.getOut().setHeader(Exchange.CONTENT_TYPE, "text/xml");
+                        }
+                    });
             }
         };
     }

Copied: camel/trunk/tests/camel-itest/src/test/resources/org/apache/camel/itest/jetty/greeting.xsl
(from r1331871, 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/greeting.xsl?p2=camel/trunk/tests/camel-itest/src/test/resources/org/apache/camel/itest/jetty/greeting.xsl&p1=camel/trunk/tests/camel-itest/src/test/resources/org/apache/camel/itest/jetty/transform.xsl&r1=1331871&r2=1331877&rev=1331877&view=diff
==============================================================================
--- camel/trunk/tests/camel-itest/src/test/resources/org/apache/camel/itest/jetty/transform.xsl
(original)
+++ camel/trunk/tests/camel-itest/src/test/resources/org/apache/camel/itest/jetty/greeting.xsl
Sun Apr 29 08:27:09 2012
@@ -15,25 +15,12 @@
     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:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 
   <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>
+    <goodbye>
+      <xsl:value-of select="/hello"/>
+    </goodbye>
   </xsl:template>
 
 </xsl:stylesheet>



Mime
View raw message